1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.seasar.cubby.tags; |
17 | |
|
18 | |
import static org.seasar.cubby.CubbyConstants.ATTR_CONTEXT_PATH; |
19 | |
import static org.seasar.cubby.internal.util.LogMessages.format; |
20 | |
import static org.seasar.cubby.tags.TagUtils.toAttr; |
21 | |
|
22 | |
import java.io.IOException; |
23 | |
import java.net.MalformedURLException; |
24 | |
import java.util.HashMap; |
25 | |
import java.util.Map; |
26 | |
|
27 | |
import javax.servlet.http.HttpServletRequest; |
28 | |
import javax.servlet.http.HttpServletResponse; |
29 | |
import javax.servlet.jsp.JspException; |
30 | |
import javax.servlet.jsp.JspWriter; |
31 | |
import javax.servlet.jsp.PageContext; |
32 | |
import javax.servlet.jsp.tagext.BodyContent; |
33 | |
import javax.servlet.jsp.tagext.BodyTagSupport; |
34 | |
import javax.servlet.jsp.tagext.DynamicAttributes; |
35 | |
|
36 | |
import org.seasar.cubby.internal.controller.FormWrapper; |
37 | |
import org.seasar.cubby.internal.controller.FormWrapperFactory; |
38 | |
import org.seasar.cubby.internal.controller.impl.FormWrapperFactoryImpl; |
39 | |
import org.seasar.cubby.util.LinkBuilder; |
40 | |
import org.slf4j.Logger; |
41 | |
import org.slf4j.LoggerFactory; |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | 39 | public class FormTag extends BodyTagSupport implements DynamicAttributes, |
54 | |
ParamParent { |
55 | |
|
56 | |
|
57 | |
private static final long serialVersionUID = 1L; |
58 | |
|
59 | |
|
60 | 1 | private static final Logger logger = LoggerFactory.getLogger(FormTag.class); |
61 | |
|
62 | |
|
63 | 39 | private final Map<String, Object> dynamicAttributes = new HashMap<String, Object>(); |
64 | |
|
65 | |
|
66 | |
private Object value; |
67 | |
|
68 | |
|
69 | 39 | private boolean encodeURL = true; |
70 | |
|
71 | |
|
72 | 39 | private final transient LinkSupport linkSupport = new LinkSupport(); |
73 | |
|
74 | |
|
75 | 39 | private final transient LinkBuilder linkBuilder = new LinkBuilder(); |
76 | |
|
77 | |
|
78 | 39 | private final transient FormWrapperFactory formWrapperFactory = new FormWrapperFactoryImpl(); |
79 | |
|
80 | |
|
81 | |
private transient FormWrapper formWrapper; |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
public void setDynamicAttribute(final String uri, final String localName, |
87 | |
final Object value) throws JspException { |
88 | 3 | this.dynamicAttributes.put(localName, value); |
89 | 3 | } |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
protected Map<String, Object> getDynamicAttribute() { |
97 | 6 | return this.dynamicAttributes; |
98 | |
} |
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
public void setValue(final Object value) { |
107 | 6 | this.value = value; |
108 | 6 | } |
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
public void setActionClass(final String actionClass) { |
117 | 3 | linkSupport.setActionClassName(actionClass); |
118 | 3 | } |
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
public void setActionMethod(final String actionMethod) { |
127 | 3 | linkSupport.setActionMethodName(actionMethod); |
128 | 3 | } |
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
public void setEncodeURL(final boolean encodeURL) { |
138 | 0 | this.encodeURL = encodeURL; |
139 | 0 | } |
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
public void setProtocol(final String protocol) { |
148 | 0 | linkBuilder.setProtocol(protocol); |
149 | 0 | } |
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
public void setPort(final int port) { |
158 | 0 | linkBuilder.setPort(port); |
159 | 0 | } |
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
public void addParameter(final String name, final String value) { |
170 | 4 | linkSupport.addParameter(name, value); |
171 | 4 | } |
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
@Override |
177 | |
public int doStartTag() throws JspException { |
178 | 6 | if (this.value == null) { |
179 | 0 | if (logger.isDebugEnabled()) { |
180 | 0 | logger.debug(format("ECUB1006")); |
181 | |
} |
182 | |
} |
183 | 6 | this.formWrapper = formWrapperFactory.create(this.value); |
184 | 6 | return EVAL_BODY_BUFFERED; |
185 | |
} |
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
@Override |
191 | |
public int doEndTag() throws JspException { |
192 | 6 | final String contextPath = (String) pageContext.getAttribute( |
193 | |
ATTR_CONTEXT_PATH, PageContext.REQUEST_SCOPE); |
194 | 6 | if (linkSupport.isLinkable()) { |
195 | 3 | final String characterEncoding = pageContext.getRequest() |
196 | |
.getCharacterEncoding(); |
197 | 3 | final String url = contextPath |
198 | |
+ linkSupport.getPath(characterEncoding); |
199 | 3 | dynamicAttributes.put("action", url); |
200 | |
} |
201 | |
|
202 | 6 | if (encodeURL && dynamicAttributes.containsKey("action")) { |
203 | 6 | final HttpServletRequest request = (HttpServletRequest) pageContext |
204 | |
.getRequest(); |
205 | 6 | final HttpServletResponse response = (HttpServletResponse) pageContext |
206 | |
.getResponse(); |
207 | 6 | final String actionPath = (String) dynamicAttributes.get("action"); |
208 | |
final String url; |
209 | |
try { |
210 | 6 | url = linkBuilder.file(actionPath).toLink(request); |
211 | 0 | } catch (final MalformedURLException e) { |
212 | 0 | throw new JspException(e); |
213 | 6 | } |
214 | 6 | final String encodedUrl = response.encodeURL(url); |
215 | 6 | dynamicAttributes.put("action", encodedUrl); |
216 | |
} |
217 | |
|
218 | 6 | final JspWriter out = pageContext.getOut(); |
219 | |
try { |
220 | 6 | out.write("<form "); |
221 | 6 | out.write(toAttr(getDynamicAttribute())); |
222 | 6 | out.write(">"); |
223 | 6 | final BodyContent bodyContent = getBodyContent(); |
224 | 6 | if (bodyContent != null) { |
225 | 6 | bodyContent.writeOut(out); |
226 | |
} |
227 | 6 | out.write("</form>"); |
228 | 0 | } catch (final IOException e) { |
229 | 0 | throw new JspException(e); |
230 | 6 | } |
231 | 6 | reset(); |
232 | 6 | return EVAL_PAGE; |
233 | |
} |
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
private void reset() { |
239 | 6 | linkSupport.clear(); |
240 | 6 | linkBuilder.clear(); |
241 | 6 | dynamicAttributes.clear(); |
242 | 6 | value = null; |
243 | 6 | formWrapper = null; |
244 | 6 | } |
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
public FormWrapper getFormWrapper() { |
252 | 2 | return formWrapper; |
253 | |
} |
254 | |
|
255 | |
} |