|
17 | 17 |
|
18 | 18 | import java.util.Map; |
19 | 19 |
|
| 20 | +import javax.servlet.ServletRequest; |
| 21 | + |
20 | 22 | import com.ocpsoft.rewrite.bind.BindingBuilder; |
21 | 23 | import com.ocpsoft.rewrite.bind.Converter; |
22 | 24 | import com.ocpsoft.rewrite.bind.Validator; |
23 | 25 | import com.ocpsoft.rewrite.bind.util.Maps; |
24 | 26 | import com.ocpsoft.rewrite.context.EvaluationContext; |
25 | 27 | import com.ocpsoft.rewrite.event.Rewrite; |
| 28 | +import com.ocpsoft.rewrite.servlet.event.ServletRewrite; |
26 | 29 | import com.ocpsoft.rewrite.servlet.http.event.HttpServletRewrite; |
27 | 30 | import com.ocpsoft.rewrite.servlet.impl.HttpRewriteWrappedRequest; |
28 | 31 |
|
29 | 32 | /** |
30 | | - * // TODO arquillian test |
| 33 | + * Responsible for binding to {@link ServletRequest#setAttribute(String, Object)} and |
| 34 | + * {@link ServletRequest#getParameterMap()} contexts. |
31 | 35 | * |
32 | 36 | * @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a> |
33 | 37 | * |
34 | 38 | */ |
35 | | -public class Request extends BindingBuilder |
| 39 | +public abstract class Request extends BindingBuilder |
36 | 40 | { |
37 | | - private final String parameter; |
| 41 | + /** |
| 42 | + * Bind a value to the {@link ServletRequest#setAttribute(String, Object)} map. |
| 43 | + */ |
| 44 | + public static BindingBuilder attribute(final String property) |
| 45 | + { |
| 46 | + return new RequestAttributeBinding(property); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Bind a value to the {@link ServletRequest#setAttribute(String, Object)} map. Use the given {@link Converter} when |
| 51 | + * retrieving any values. |
| 52 | + */ |
| 53 | + public static BindingBuilder attribute(final String property, final Class<? extends Converter<?>> type) |
| 54 | + { |
| 55 | + return attribute(property).convertedBy(type); |
| 56 | + } |
38 | 57 |
|
39 | | - private Request(final String property) |
| 58 | + /** |
| 59 | + * Bind a value to the {@link ServletRequest#setAttribute(String, Object)} map. Use the given {@link Validator} |
| 60 | + * before attempting to submit any values. Use the given {@link Converter} when retrieving any values. |
| 61 | + */ |
| 62 | + public static BindingBuilder attribute(final String property, final Class<Converter<?>> converterType, |
| 63 | + final Class<? extends Validator<?>> validatorType) |
40 | 64 | { |
41 | | - this.parameter = property; |
| 65 | + return attribute(property, converterType).validatedBy(validatorType); |
42 | 66 | } |
43 | 67 |
|
| 68 | + /** |
| 69 | + * Bind a value to the {@link ServletRequest#getParameterMap()}. |
| 70 | + */ |
44 | 71 | public static BindingBuilder parameter(final String property) |
45 | 72 | { |
46 | | - return new Request(property); |
| 73 | + return new RequestParameterBinding(property); |
47 | 74 | } |
48 | 75 |
|
| 76 | + /** |
| 77 | + * Bind a value to the {@link ServletRequest#getParameterMap()} map. Use the given {@link Converter} when retrieving |
| 78 | + * any values. |
| 79 | + */ |
49 | 80 | public static BindingBuilder parameter(final String property, final Class<? extends Converter<?>> type) |
50 | 81 | { |
51 | 82 | return parameter(property).convertedBy(type); |
52 | 83 | } |
53 | 84 |
|
| 85 | + /** |
| 86 | + * Bind a value to the {@link ServletRequest#getParameterMap()} map. Use the given {@link Validator} before |
| 87 | + * attempting to submit any values. Use the given {@link Converter} when retrieving any values. |
| 88 | + */ |
54 | 89 | public static BindingBuilder parameter(final String property, final Class<Converter<?>> converterType, |
55 | 90 | final Class<? extends Validator<?>> validatorType) |
56 | 91 | { |
57 | 92 | return parameter(property, converterType).validatedBy(validatorType); |
58 | 93 | } |
59 | 94 |
|
60 | 95 | @Override |
61 | | - public Object submit(final Rewrite event, final EvaluationContext context, final Object value) |
| 96 | + public boolean supportsRetrieval() |
62 | 97 | { |
63 | | - HttpRewriteWrappedRequest wrapper = (HttpRewriteWrappedRequest) ((HttpServletRewrite) event).getRequest() |
64 | | - .getAttribute(HttpRewriteWrappedRequest.class.getName()); |
| 98 | + return true; |
| 99 | + } |
65 | 100 |
|
66 | | - Map<String, String[]> modifiableParameters = wrapper.getModifiableParameters(); |
67 | | - if (value.getClass().isArray()) |
| 101 | + @Override |
| 102 | + public boolean supportsSubmission() |
| 103 | + { |
| 104 | + return true; |
| 105 | + } |
| 106 | + |
| 107 | + private static class RequestParameterBinding extends Request |
| 108 | + { |
| 109 | + private final String parameter; |
| 110 | + |
| 111 | + public RequestParameterBinding(final String parameter) |
68 | 112 | { |
69 | | - Object[] values = (Object[]) value; |
70 | | - for (Object object : values) { |
71 | | - Maps.addArrayValue(modifiableParameters, parameter, object.toString()); |
72 | | - } |
| 113 | + this.parameter = parameter; |
73 | 114 | } |
74 | | - else |
| 115 | + |
| 116 | + @Override |
| 117 | + public Object submit(final Rewrite event, final EvaluationContext context, final Object value) |
75 | 118 | { |
76 | | - Maps.addArrayValue(modifiableParameters, parameter, value.toString()); |
| 119 | + HttpRewriteWrappedRequest wrapper = (HttpRewriteWrappedRequest) ((HttpServletRewrite) event).getRequest() |
| 120 | + .getAttribute(HttpRewriteWrappedRequest.class.getName()); |
| 121 | + |
| 122 | + Map<String, String[]> modifiableParameters = wrapper.getModifiableParameters(); |
| 123 | + if (value.getClass().isArray()) |
| 124 | + { |
| 125 | + Object[] values = (Object[]) value; |
| 126 | + for (Object object : values) { |
| 127 | + Maps.addArrayValue(modifiableParameters, parameter, object.toString()); |
| 128 | + } |
| 129 | + } |
| 130 | + else |
| 131 | + { |
| 132 | + Maps.addArrayValue(modifiableParameters, parameter, value.toString()); |
| 133 | + } |
| 134 | + |
| 135 | + return null; |
77 | 136 | } |
78 | 137 |
|
79 | | - return null; |
| 138 | + @Override |
| 139 | + public Object retrieve(final Rewrite event, final EvaluationContext context) |
| 140 | + { |
| 141 | + if (event instanceof ServletRewrite) |
| 142 | + return ((HttpServletRewrite) event).getRequest().getParameter(parameter); |
| 143 | + else |
| 144 | + return null; |
| 145 | + } |
80 | 146 | } |
81 | 147 |
|
82 | | - @Override |
83 | | - public Object retrieve(final Rewrite event, final EvaluationContext context) |
| 148 | + private static class RequestAttributeBinding extends Request |
84 | 149 | { |
85 | | - if (event instanceof HttpServletRewrite) |
86 | | - return ((HttpServletRewrite) event).getRequest().getParameter(parameter); |
| 150 | + private final String parameter; |
87 | 151 |
|
88 | | - else |
| 152 | + public RequestAttributeBinding(final String attribute) |
| 153 | + { |
| 154 | + this.parameter = attribute; |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + public Object submit(final Rewrite event, final EvaluationContext context, final Object value) |
| 159 | + { |
| 160 | + ((HttpServletRewrite) event).getRequest().setAttribute(parameter, value); |
89 | 161 | return null; |
90 | | - } |
| 162 | + } |
91 | 163 |
|
92 | | - @Override |
93 | | - public boolean supportsRetrieval() |
94 | | - { |
95 | | - return true; |
96 | | - } |
| 164 | + @Override |
| 165 | + public Object retrieve(final Rewrite event, final EvaluationContext context) |
| 166 | + { |
| 167 | + if (event instanceof HttpServletRewrite) |
| 168 | + return ((HttpServletRewrite) event).getRequest().getParameter(parameter); |
97 | 169 |
|
98 | | - @Override |
99 | | - public boolean supportsSubmission() |
100 | | - { |
101 | | - return true; |
| 170 | + else |
| 171 | + return null; |
| 172 | + } |
102 | 173 | } |
103 | 174 | } |
0 commit comments