1 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 DEFAULTENCODING = "com.micronova.jsp.tag.ParamTag.defaultEncoding"; 50 public static final String DEFAULTMAXCONTENTLENGTH = "com.micronova.jsp.tag.ParamTag.maxContentLength"; 51 public static final String DEFAULTSELECTSUFFIX = "com.micronova.jsp.tag.ParamTag.selectSuffix"; 52 public static final String DEFAULTRADIOSUFFIX = "com.micronova.jsp.tag.ParamTag.radioSuffix"; 53 54 public static final String REQUEST = "_request"; 55 public static final String CALLER = "_caller"; 56 public static final String MULTIPART = "multipart"; 57 58 public static final String MAXCONTENTLENGTH = "maxContentLength"; 59 public static final String MULTIPATTERN = "multiPattern"; 60 public static final String SELECTPATTERN = "selectPattern"; 61 public static final String RADIOPATTERN = "radioPattern"; 62 public static final String FILEPATTERN = "filePattern"; 63 public static final String VALUECODEC = "valueCodec"; 64 public static final String NAMECODEC = "nameCodec"; 65 public static final String ENCODING = "encoding"; 66 public static final String EMPTYPATTERN = "emptyPattern"; 67 68 public static final String DEFAULTEMPTYPATTERN = "--"; 69 70 public static final String DEFAULTFILEPATTERN = ".*[Ff]ile.*"; 71 72 protected Object _parameterMap; 73 protected NestedMap _controlMap; 74 75 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 expression) throws Exception 101 { 102 NestedMap controlMap = getControlMap(); 103 104 controlMap.copyFromSource(evaluateAttribute("control", expression, Object .class)); 105 } 106 107 public void setNameCodec(Object expression) throws Exception 108 { 109 getControlMap().put(NAMECODEC, evaluateAttribute(NAMECODEC, expression, String .class)); 110 111 } 112 113 public void setValueCodec(Object expression) throws Exception 114 { 115 getControlMap().put(VALUECODEC, evaluateAttribute(VALUECODEC, expression, String .class)); 116 117 } 118 119 public void setEncoding(Object expression) throws Exception 120 { 121 getControlMap().put(ENCODING, evaluateAttribute(ENCODING, expression, String .class)); 122 123 } 124 125 public void setMaxContentLength(Object expression) throws Exception 126 { 127 getControlMap().put(MAXCONTENTLENGTH, evaluateAttribute(MAXCONTENTLENGTH, expression, String .class)); 128 129 } 130 131 public void setMultiPattern(Object expression) throws Exception 132 { 133 getControlMap().put(MULTIPATTERN, evaluateAttribute(MULTIPATTERN, expression, String .class)); 134 135 } 136 137 public void setSelectPattern(Object expression) throws Exception 138 { 139 getControlMap().put(SELECTPATTERN, evaluateAttribute(SELECTPATTERN, expression, String .class)); 140 141 } 142 143 public void setRadioPattern(Object expression) throws Exception 144 { 145 getControlMap().put(RADIOPATTERN, evaluateAttribute(RADIOPATTERN, expression, String .class)); 146 147 } 148 149 public void setFilePattern(Object expression) throws Exception 150 { 151 getControlMap().put(FILEPATTERN, evaluateAttribute(FILEPATTERN, expression, String .class)); 152 153 } 154 155 public void setEmptyPattern(Object expression) throws Exception 156 { 157 getControlMap().put(EMPTYPATTERN, evaluateAttribute(EMPTYPATTERN, expression, String .class)); 158 } 159 160 public void setParameterMap(Object expression) throws Exception 161 { 162 _parameterMap = evaluateAttribute("parameterMap", expression, Object .class); 163 } 164 165 166 167 protected String applyNameCodec(String nameCodec, String name) throws Exception 168 { 169 if (nameCodec == DEFAULT) 170 { 171 name = StringUtil.applyPattern(name, defaultNamePattern, null); 172 } 173 else if (!isEmptyString(nameCodec)) 174 { 175 name= (String )(applyCodec(nameCodec, name)); 176 } 177 178 return name; 179 } 180 181 182 183 protected Object applyValueCodec(String valueCodec, Object value) throws Exception 184 { 185 if (value instanceof String ) 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 201 202 protected void processParameterMap(ServletRequest request, NestedMap map, NestedMap controlMap, Map parameters) throws Exception 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 valueCodec = controlMap.getString(VALUECODEC, DEFAULT); 210 String nameCodec = controlMap.getString(NAMECODEC, DEFAULT); 211 212 String encoding = controlMap.getString(ENCODING); 213 214 if ("*".equals(encoding)) 215 { 216 String defaultEncoding = (String )getConfiguration(DEFAULTENCODING, "utf-8"); 217 218 String requestEncoding = pageContext.getRequest().getCharacterEncoding(); 219 if (requestEncoding != null) 220 { 221 encoding = requestEncoding; 222 } 223 else 224 { 225 encoding = defaultEncoding; 226 } 227 } 228 229 String [] 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 parameterName = applyNameCodec(nameCodec, (String )(entry.getKey())); 238 239 Object entryValue = entry.getValue(); 240 241 String [] parameterValues = null; 242 243 if (entryValue instanceof String ) 244 { 245 if (parameterArray == null) 246 { 247 parameterArray = new String [1]; 248 } 249 250 parameterArray[0] = (String )entryValue; 251 252 parameterValues = parameterArray; 253 } 254 else if (entryValue instanceof String []) 255 { 256 parameterValues = (String [])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 parameterValue = parameterValues[i]; 268 269 if (!isEmptyString(encoding)) 270 { 271 parameterValue = new String (parameterValue.toString().getBytes("iso-8859-1"), encoding); 272 parameterName = new String (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 selectSuffix = (String )getConfiguration(DEFAULTSELECTSUFFIX, "_SELECTED"); 296 String 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 radioSuffix = (String )getConfiguration(DEFAULTRADIOSUFFIX, "_CHECKED"); 312 String 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 processValue(Object tagValue) throws Exception 343 { 344 if (tagValue != null) 345 { 346 NestedMap map = (NestedMap)tagValue; 347 HttpServletRequest request = (HttpServletRequest)pageContext.getRequest(); 348 NestedMap controlMap = getControlMap(); 349 350 Object 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 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 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 ("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 contentType = request.getHeader("Content-Type"); 417 418 if ((contentType != null) && (contentType.startsWith("multipart/form-data"))) 419 { 420 String 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 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 keyName = applyNameCodec(nameCodec, entry.getKey().toString()); 440 441 NestedMap valueMap = (NestedMap)entry.getValue(); 442 443 Object content = valueMap.get(MailFolder.CONTENT); 444 445 if ((!isEmptyString(filePattern)) && (keyName.matches(filePattern))) 446 { 447 if (content instanceof String ) 448 { 449 content = new StringReader(content.toString()); 450 } 451 } 452 453 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 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 |