KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > micronova > jsp > tag > ParamTag


1 /*
2
3 Copyright 2003-2007 MicroNova (R)
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or
7 without modification, are permitted provided that the following
8 conditions are met:
9
10     * Redistributions of source code must retain the above copyright
11     notice, this list of conditions and the following disclaimer.
12
13     * Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions and the following disclaimer in the
15     documentation and/or other materials provided with the distribution.
16
17     * Neither the name of MicroNova nor the names of its contributors
18     may be used to endorse or promote products derived from this
19     software without specific prior written permission.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE.
32
33 */

34
35
36 package com.micronova.jsp.tag;
37
38 import java.util.*;
39 import java.util.regex.*;
40 import java.io.*;
41 import javax.servlet.*;
42 import javax.servlet.http.*;
43 import javax.servlet.jsp.*;
44 import com.micronova.util.*;
45 import com.micronova.util.servlet.*;
46
47 public class ParamTag extends MapTag
48 {
49     public static final String JavaDoc DEFAULTENCODING = "com.micronova.jsp.tag.ParamTag.defaultEncoding";
50     public static final String JavaDoc DEFAULTMAXCONTENTLENGTH = "com.micronova.jsp.tag.ParamTag.maxContentLength";
51     public static final String JavaDoc DEFAULTSELECTSUFFIX = "com.micronova.jsp.tag.ParamTag.selectSuffix";
52     public static final String JavaDoc DEFAULTRADIOSUFFIX = "com.micronova.jsp.tag.ParamTag.radioSuffix";
53
54     public static final String JavaDoc REQUEST = "_request";
55     public static final String JavaDoc CALLER = "_caller";
56     public static final String JavaDoc MULTIPART = "multipart";
57
58     public static final String JavaDoc MAXCONTENTLENGTH = "maxContentLength";
59     public static final String JavaDoc MULTIPATTERN = "multiPattern";
60     public static final String JavaDoc SELECTPATTERN = "selectPattern";
61     public static final String JavaDoc RADIOPATTERN = "radioPattern";
62     public static final String JavaDoc FILEPATTERN = "filePattern";
63     public static final String JavaDoc VALUECODEC = "valueCodec";
64     public static final String JavaDoc NAMECODEC = "nameCodec";
65     public static final String JavaDoc ENCODING = "encoding";
66     public static final String JavaDoc EMPTYPATTERN = "emptyPattern";
67
68     public static final String JavaDoc DEFAULTEMPTYPATTERN = "--";
69
70     public static final String JavaDoc DEFAULTFILEPATTERN = ".*[Ff]ile.*";
71
72     protected Object JavaDoc _parameterMap;
73     protected NestedMap _controlMap;
74
75     /** default name codec pattern */
76
77     protected static Pattern defaultNamePattern = Pattern.compile("__");
78
79     protected void init()
80     {
81         super.init();
82
83         _parameterMap = null;
84         _controlMap = null;
85     }
86
87     protected NestedMap getControlMap()
88     {
89         NestedMap controlMap = _controlMap;
90
91         if (controlMap == null)
92         {
93             controlMap = new NestedMap();
94             _controlMap = controlMap;
95         }
96
97         return controlMap;
98     }
99
100     public void setControl(Object JavaDoc expression) throws Exception JavaDoc
101     {
102         NestedMap controlMap = getControlMap();
103
104         controlMap.copyFromSource(evaluateAttribute("control", expression, Object JavaDoc.class));
105     }
106
107     public void setNameCodec(Object JavaDoc expression) throws Exception JavaDoc
108     {
109         getControlMap().put(NAMECODEC, evaluateAttribute(NAMECODEC, expression, String JavaDoc.class));
110
111     }
112
113     public void setValueCodec(Object JavaDoc expression) throws Exception JavaDoc
114     {
115         getControlMap().put(VALUECODEC, evaluateAttribute(VALUECODEC, expression, String JavaDoc.class));
116
117     }
118
119     public void setEncoding(Object JavaDoc expression) throws Exception JavaDoc
120     {
121         getControlMap().put(ENCODING, evaluateAttribute(ENCODING, expression, String JavaDoc.class));
122
123     }
124
125     public void setMaxContentLength(Object JavaDoc expression) throws Exception JavaDoc
126     {
127         getControlMap().put(MAXCONTENTLENGTH, evaluateAttribute(MAXCONTENTLENGTH, expression, String JavaDoc.class));
128
129     }
130
131     public void setMultiPattern(Object JavaDoc expression) throws Exception JavaDoc
132     {
133         getControlMap().put(MULTIPATTERN, evaluateAttribute(MULTIPATTERN, expression, String JavaDoc.class));
134
135     }
136
137     public void setSelectPattern(Object JavaDoc expression) throws Exception JavaDoc
138     {
139         getControlMap().put(SELECTPATTERN, evaluateAttribute(SELECTPATTERN, expression, String JavaDoc.class));
140
141     }
142
143     public void setRadioPattern(Object JavaDoc expression) throws Exception JavaDoc
144     {
145         getControlMap().put(RADIOPATTERN, evaluateAttribute(RADIOPATTERN, expression, String JavaDoc.class));
146
147     }
148
149     public void setFilePattern(Object JavaDoc expression) throws Exception JavaDoc
150     {
151         getControlMap().put(FILEPATTERN, evaluateAttribute(FILEPATTERN, expression, String JavaDoc.class));
152
153     }
154
155     public void setEmptyPattern(Object JavaDoc expression) throws Exception JavaDoc
156     {
157         getControlMap().put(EMPTYPATTERN, evaluateAttribute(EMPTYPATTERN, expression, String JavaDoc.class));
158     }
159
160     public void setParameterMap(Object JavaDoc expression) throws Exception JavaDoc
161     {
162         _parameterMap = evaluateAttribute("parameterMap", expression, Object JavaDoc.class);
163     }
164
165     /** apply nameCodec */
166
167     protected String JavaDoc applyNameCodec(String JavaDoc nameCodec, String JavaDoc name) throws Exception JavaDoc
168     {
169         if (nameCodec == DEFAULT)
170         {
171             name = StringUtil.applyPattern(name, defaultNamePattern, null);
172         }
173         else if (!isEmptyString(nameCodec))
174         {
175             name= (String JavaDoc)(applyCodec(nameCodec, name));
176         }
177
178         return name;
179     }
180
181     /** apply valueCodec, if value is a String */
182
183     protected Object JavaDoc applyValueCodec(String JavaDoc valueCodec, Object JavaDoc value) throws Exception JavaDoc
184     {
185         if (value instanceof String JavaDoc)
186         {
187             if (valueCodec == DEFAULT)
188             {
189                 value = XMLUtil.encode(value.toString());
190             }
191             else if (!isEmptyString(valueCodec))
192             {
193                 value = applyCodec(valueCodec, value);
194             }
195         }
196
197         return value;
198     }
199
200     /** processes parameter map */
201
202     protected void processParameterMap(ServletRequest request, NestedMap map, NestedMap controlMap, Map parameters) throws Exception JavaDoc
203     {
204         Pattern selectPattern = TypeUtil.isPattern(controlMap.get(SELECTPATTERN));
205         Pattern multiPattern = TypeUtil.isPattern(controlMap.get(MULTIPATTERN));
206         Pattern radioPattern = TypeUtil.isPattern(controlMap.get(RADIOPATTERN));
207         Pattern emptyPattern = TypeUtil.isPattern(controlMap.get(EMPTYPATTERN, DEFAULTEMPTYPATTERN));
208
209         String JavaDoc valueCodec = controlMap.getString(VALUECODEC, DEFAULT);
210         String JavaDoc nameCodec = controlMap.getString(NAMECODEC, DEFAULT);
211
212         String JavaDoc encoding = controlMap.getString(ENCODING);
213
214         if ("*".equals(encoding))
215         {
216             String JavaDoc defaultEncoding = (String JavaDoc)getConfiguration(DEFAULTENCODING, "utf-8");
217
218             String JavaDoc requestEncoding = pageContext.getRequest().getCharacterEncoding();
219             if (requestEncoding != null)
220             {
221                 encoding = requestEncoding;
222             }
223             else
224             {
225                 encoding = defaultEncoding;
226             }
227         }
228         
229         String JavaDoc[] parameterArray = null;
230
231         Iterator iterator = parameters.entrySet().iterator();
232         
233         while (iterator.hasNext())
234         {
235             Map.Entry entry = (Map.Entry)iterator.next();
236             
237             String JavaDoc parameterName = applyNameCodec(nameCodec, (String JavaDoc)(entry.getKey()));
238             
239             Object JavaDoc entryValue = entry.getValue();
240             
241             String JavaDoc[] parameterValues = null;
242             
243             if (entryValue instanceof String JavaDoc)
244             {
245                 if (parameterArray == null)
246                 {
247                     parameterArray = new String JavaDoc[1];
248                 }
249                 
250                 parameterArray[0] = (String JavaDoc)entryValue;
251                 
252                 parameterValues = parameterArray;
253             }
254             else if (entryValue instanceof String JavaDoc[])
255             {
256                 parameterValues = (String JavaDoc[])entryValue;
257             }
258             else
259             {
260                 map.setElement(parameterName, entryValue);
261             }
262             
263             if (parameterValues != null)
264             {
265                 for (int i = 0; i < parameterValues.length; i ++)
266                 {
267                     Object JavaDoc parameterValue = parameterValues[i];
268
269                     if (!isEmptyString(encoding))
270                     {
271                         parameterValue = new String JavaDoc(parameterValue.toString().getBytes("iso-8859-1"), encoding);
272                         parameterName = new String JavaDoc(parameterName.getBytes("iso-8859-1"), encoding);
273                     }
274                     
275                     parameterValue = applyValueCodec(valueCodec, parameterValue);
276
277                     if (parameterValue != null)
278                     {
279                         if ((emptyPattern != null) && emptyPattern.matcher(parameterValue.toString()).matches())
280                         {
281                             parameterValue = "";
282                         }
283                     }
284                    
285                     if (isEmptyString(parameterValue))
286                     {
287                         map.setElement(parameterName, parameterValue);
288                     }
289                     else
290                     {
291                         boolean isMulti = ((multiPattern != null) && multiPattern.matcher(parameterName).matches());
292                         
293                         if ((selectPattern != null) && selectPattern.matcher(parameterName).matches())
294                         {
295                             String JavaDoc selectSuffix = (String JavaDoc)getConfiguration(DEFAULTSELECTSUFFIX, "_SELECTED");
296                             String JavaDoc selectName = parameterName + selectSuffix;
297
298                             parameterValue = applyNameCodec(nameCodec, parameterValue.toString());
299
300                             map.setElement(parameterName, parameterValue);
301
302                             if (!isMulti)
303                             {
304                                 map.setElement(selectName, null);
305                             }
306
307                             map.setElement(selectName + "." + parameterValue, "SELECTED");
308                         }
309                         else if ((radioPattern != null) && radioPattern.matcher(parameterName).matches())
310                         {
311                             String JavaDoc radioSuffix = (String JavaDoc)getConfiguration(DEFAULTRADIOSUFFIX, "_CHECKED");
312                             String JavaDoc radioName = parameterName + radioSuffix;
313
314                             parameterValue = applyNameCodec(nameCodec, parameterValue.toString());
315
316                             map.setElement(parameterName, parameterValue);
317
318                             if (!isMulti)
319                             {
320                                 map.setElement(radioName, null);
321                             }
322
323                             map.setElement(radioName + "." + parameterValue, "CHECKED");
324                         }
325                         else
326                         {
327                             if (isMulti)
328                             {
329                                 map.setElement(parameterName + "._.*", parameterValue);
330                             }
331                             else
332                             {
333                                 map.setElement(parameterName, parameterValue);
334                             }
335                         }
336                     }
337                 }
338             }
339         }
340     }
341
342     protected Object JavaDoc processValue(Object JavaDoc tagValue) throws Exception JavaDoc
343     {
344         if (tagValue != null)
345         {
346             NestedMap map = (NestedMap)tagValue;
347             HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
348             NestedMap controlMap = getControlMap();
349
350             Object JavaDoc parameterMap = _parameterMap;
351
352             boolean doesRethrow = false;
353
354             try
355             {
356                 if ((request instanceof DispatchHttpServletRequest) && (parameterMap == null))
357                 {
358                     DispatchHttpServletRequest dispatchHttpServletRequest = (DispatchHttpServletRequest)request;
359
360                     map.put(CALLER, dispatchHttpServletRequest.getDispatchCaller());
361
362                     Object JavaDoc dispatchObject = dispatchHttpServletRequest.getDispatchObject();
363
364                     map.copyFromSource(dispatchObject);
365
366                     if (dispatchObject instanceof NestedMap)
367                     {
368                         map.put(CallTag.RETURNPROPERTY, ((NestedMap)dispatchObject).get(CallTag.RETURNPROPERTY));
369                     }
370                 }
371                 else
372                 {
373                     Object JavaDoc maxContentLengthSpec = controlMap.get(MAXCONTENTLENGTH);
374
375                     if (maxContentLengthSpec == null)
376                     {
377                         maxContentLengthSpec = getConfiguration(DEFAULTMAXCONTENTLENGTH, null);
378                     }
379
380                     if (maxContentLengthSpec != null)
381                     {
382                         int maxContentLength = Integer.parseInt(maxContentLengthSpec.toString());
383
384                         boolean doesThrow = false;
385
386                         if (maxContentLength < 0)
387                         {
388                             doesThrow = true;
389                             maxContentLength = -maxContentLength;
390                         }
391
392                         if (maxContentLength > 0)
393                         {
394                             if (request.getContentLength() > maxContentLength)
395                             {
396                                 doesRethrow = doesThrow;
397                                 throw new Exception JavaDoc("max content length exceeded");
398                             }
399                         }
400                     }
401
402                     processParameterMap(request, map, controlMap, (Map)map.get("__param"));
403
404                     NestedMap multipartMap = null;
405                     Map parameters = request.getParameterMap();
406
407                     if (parameterMap != null)
408                     {
409                         processParameterMap(request, map, controlMap, (Map)((new NestedMap(parameterMap)).get("__param")));
410                     }
411                     else
412                     {
413                         processParameterMap(request, map, controlMap, parameters);
414                     }
415
416                     String JavaDoc contentType = request.getHeader("Content-Type");
417
418                     if ((contentType != null) && (contentType.startsWith("multipart/form-data")))
419                     {
420                         String JavaDoc filePattern = controlMap.getString(FILEPATTERN, DEFAULTFILEPATTERN);
421                         
422                         multipartMap = MailFolder.decodeMultipart(request.getInputStream(), contentType, controlMap);
423                         
424                         NestedMap partNameMap = multipartMap.getSubMap(MailFolder.PARTNAME, false);
425
426                         NestedMap multipartParameterMap = new NestedMap();
427
428                         if (partNameMap != null)
429                         {
430                             NestedMap newPartNameMap = new NestedMap();
431                         
432                             String JavaDoc nameCodec = controlMap.getString(NAMECODEC, DEFAULT);
433
434                             Iterator iterator = partNameMap.entrySet().iterator();
435                             while (iterator.hasNext())
436                             {
437                                 Map.Entry entry = (Map.Entry)iterator.next();
438                                 
439                                 String JavaDoc keyName = applyNameCodec(nameCodec, entry.getKey().toString());
440                                 
441                                 NestedMap valueMap = (NestedMap)entry.getValue();
442                                 
443                                 Object JavaDoc content = valueMap.get(MailFolder.CONTENT);
444                                 
445                                 if ((!isEmptyString(filePattern)) && (keyName.matches(filePattern)))
446                                 {
447                                     if (content instanceof String JavaDoc)
448                                     {
449                                         content = new StringReader(content.toString());
450                                     }
451                                 }
452
453                                 // multipartParameterMap.put(keyName, content);
454

455                                 multipartParameterMap.setElement(keyName, content);
456                                 newPartNameMap.setElement(keyName, valueMap);
457                             }
458
459                             multipartMap.put(MailFolder.PARTNAME, newPartNameMap);
460                         }
461                         
462                         processParameterMap(request, map, controlMap, (Map)multipartParameterMap.get("__param"));
463                     }
464
465                     map.put(CALLER, null);
466                     map.put(MULTIPART, multipartMap);
467                 }
468             }
469             catch (Exception JavaDoc e)
470             {
471                 if (doesRethrow)
472                 {
473                     throw e;
474                 }
475
476                 map.clear();
477             }
478             finally
479             {
480                 map.put(REQUEST, request);
481             }
482         }
483
484         return tagValue;
485     }
486 }
487
Popular Tags