1 43 package net.jforum; 44 45 import java.io.File ; 46 import java.io.IOException ; 47 import java.util.Enumeration ; 48 import java.util.HashMap ; 49 import java.util.Iterator ; 50 import java.util.List ; 51 import java.util.Map ; 52 53 import javax.servlet.http.HttpServletRequest ; 54 import javax.servlet.http.HttpServletRequestWrapper ; 55 56 import net.jforum.exceptions.MultipartHandlingException; 57 import net.jforum.util.legacy.commons.fileupload.FileItem; 58 import net.jforum.util.legacy.commons.fileupload.FileUploadException; 59 import net.jforum.util.legacy.commons.fileupload.disk.DiskFileItemFactory; 60 import net.jforum.util.legacy.commons.fileupload.servlet.ServletFileUpload; 61 import net.jforum.util.legacy.commons.fileupload.servlet.ServletRequestContext; 62 import net.jforum.util.preferences.ConfigKeys; 63 import net.jforum.util.preferences.SystemGlobals; 64 65 69 public class ActionServletRequest extends HttpServletRequestWrapper 70 { 71 116 private static class UrlPattern 117 { 118 private String name; 119 private String value; 120 private int size; 121 private String [] vars; 122 123 public UrlPattern(String name, String value) 124 { 125 this.name = name; 126 this.value = value; 127 128 this.processPattern(); 129 } 130 131 private void processPattern() 132 { 133 String [] p = this.value.split(","); 134 135 this.vars = new String [p.length]; 136 this.size = ((((p[0]).trim()).equals("")) ? 0 : p.length); 137 138 for (int i = 0; i < this.size; i++) { 139 this.vars[i] = (p[i]).trim(); 140 } 141 } 142 143 148 public String getName() 149 { 150 return this.name; 151 } 152 153 158 public int getSize() 159 { 160 return this.size; 161 } 162 163 172 public String [] getVars() 173 { 174 return this.vars; 175 } 176 } 177 178 183 private static class UrlPatternCollection 184 { 185 private static HashMap patternsMap = new HashMap (); 186 187 193 public static UrlPattern findPattern(String name) 194 { 195 return (UrlPattern)UrlPatternCollection.patternsMap.get(name); 196 } 197 198 204 public static void addPattern(String name, String value) 205 { 206 UrlPatternCollection.patternsMap.put(name, new UrlPattern(name, value)); 207 } 208 } 209 210 private Map query; 211 private JForumContext jforumcontext; 212 213 219 public ActionServletRequest(HttpServletRequest superRequest) throws IOException 220 { 221 super(superRequest); 222 223 this.query = new HashMap (); 224 boolean isMultipart = false; 225 226 String requestType = (superRequest.getMethod()).toUpperCase(); 227 String requestUri = superRequest.getRequestURI(); 228 229 int index = requestUri.indexOf(';'); 233 234 if (index > -1) { 235 int lastIndex = requestUri.indexOf('?', index); 236 237 if (lastIndex == -1) { 238 lastIndex = requestUri.indexOf('&', index); 239 } 240 241 if (lastIndex == -1) { 242 requestUri = requestUri.substring(0, index); 243 } 244 else { 245 String part1 = requestUri.substring(0, index); 246 requestUri = part1 + requestUri.substring(lastIndex); 247 } 248 } 249 250 String encoding = SystemGlobals.getValue(ConfigKeys.ENCODING); 251 252 if ((("GET").equals(requestType) && (superRequest.getQueryString() == null)) 253 && requestUri.endsWith(SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION))) { 254 superRequest.setCharacterEncoding(encoding); 255 256 requestUri = requestUri.substring(0, requestUri.length() - SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION).length()); 257 258 String [] urlModel = requestUri.split("/"); 259 260 273 int moduleIndex = 2; 274 int actionIndex = 3; 275 int baseLen = 4; 276 277 String contextName = superRequest.getContextPath(); 278 if ((contextName == null) || contextName.equals("")) { 279 moduleIndex = 1; 280 actionIndex = 2; 281 baseLen = 3; 282 } 283 284 UrlPattern url = null; 285 286 if (urlModel.length >= baseLen) { 287 url = UrlPatternCollection.findPattern(urlModel[moduleIndex] 289 + "." 290 + urlModel[actionIndex] 291 + "." 292 + (urlModel.length - baseLen)); 293 } 294 295 if (url != null) { 296 if (url.getSize() >= urlModel.length - baseLen) { 298 for (int i = 0; i < url.getSize(); i++) { 299 this.addParameter(url.getVars()[i], urlModel[i + baseLen]); 300 } 301 } 302 303 this.addParameter("module", urlModel[moduleIndex]); 304 this.addParameter("action", urlModel[actionIndex]); 305 } 306 else { 307 this.addParameter("module", null); 308 this.addParameter("action", null); 309 } 310 } 311 else if (("POST").equals(requestType)) { 312 isMultipart = ServletFileUpload.isMultipartContent(new ServletRequestContext(superRequest)); 313 if (isMultipart) { 314 String tmpDir = SystemGlobals.getApplicationPath() + "/" + SystemGlobals.getValue(ConfigKeys.TMP_DIR); 315 ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory(100 * 1024, new File (tmpDir))); 316 upload.setHeaderEncoding(encoding); 317 318 try { 319 List items = upload.parseRequest(superRequest); 320 for (Iterator iter = items.iterator(); iter.hasNext(); ) { 321 FileItem item = (FileItem)iter.next(); 322 if (item.isFormField()) { 323 this.query.put(item.getFieldName(), item.getString(encoding)); 324 } 325 else { 326 if (item.getSize() > 0) { 327 this.query.put(item.getFieldName(), item); 328 } 329 } 330 } 331 } 332 catch (FileUploadException e) { 333 throw new MultipartHandlingException("Error while processing multipart content: " + e); 334 } 335 } 336 } 337 338 if (isMultipart == false) { 339 superRequest.setCharacterEncoding(encoding); 340 String containerEncoding = SystemGlobals.getValue(ConfigKeys.DEFAULT_CONTAINER_ENCODING); 341 342 if ("POST".equals(requestType)) { 343 containerEncoding = encoding; 344 } 345 346 for (Enumeration e = superRequest.getParameterNames(); e.hasMoreElements(); ) { 347 String name = (String )e.nextElement(); 348 this.query.put(name, new String (superRequest.getParameter(name).getBytes(containerEncoding), encoding)); 349 } 350 } 351 } 352 353 356 public String getParameter(String parameter) 357 { 358 return (String )this.query.get(parameter); 359 } 360 361 368 public int getIntParameter(String parameter) 369 { 370 return Integer.parseInt(this.getParameter(parameter)); 371 } 372 373 379 public Map dumpRequest() 380 { 381 return this.query; 382 } 383 384 391 public void restoreDump(Map query) 392 { 393 this.query = query; 394 } 395 396 405 public Object getObjectParameter(String parameter) 406 { 407 return this.query.get(parameter); 408 } 409 410 416 public static void addUrlPattern(String name, String value) 417 { 418 UrlPatternCollection.addPattern(name, value); 419 } 420 421 430 public void addParameter(String name, Object value) 431 { 432 this.query.put(name, value); 433 } 434 435 461 public String getAction() 462 { 463 return this.getParameter("action"); 464 } 465 466 492 public String getModule() 493 { 494 return this.getParameter("module"); 495 } 496 497 public Object getObjectRequestParameter(String parameter) 498 { 499 return this.query.get(parameter); 500 } 501 502 public JForumContext getJForumContext() 503 { 504 return this.jforumcontext; 505 } 506 507 public void setJForumContext(JForumContext jforumcontext) 508 { 509 this.jforumcontext = jforumcontext; 510 } 511 } 512 | Popular Tags |