1 31 32 package org.opencms.util; 33 34 import org.opencms.flex.CmsFlexRequest; 35 import org.opencms.i18n.CmsEncoder; 36 import org.opencms.jsp.CmsJspActionElement; 37 import org.opencms.main.CmsLog; 38 import org.opencms.main.OpenCms; 39 40 import java.io.IOException ; 41 import java.io.UnsupportedEncodingException ; 42 import java.util.ArrayList ; 43 import java.util.HashMap ; 44 import java.util.Iterator ; 45 import java.util.List ; 46 import java.util.Map ; 47 48 import javax.servlet.ServletException ; 49 import javax.servlet.http.Cookie ; 50 import javax.servlet.http.HttpServletRequest ; 51 import javax.servlet.http.HttpServletResponse ; 52 import javax.servlet.http.HttpSession ; 53 54 import org.apache.commons.fileupload.DiskFileUpload; 55 import org.apache.commons.fileupload.FileItem; 56 import org.apache.commons.fileupload.FileUploadBase; 57 import org.apache.commons.fileupload.FileUploadException; 58 import org.apache.commons.logging.Log; 59 60 69 public final class CmsRequestUtil { 70 71 72 public static final String ATTRIBUTE_ERRORCODE = "org.opencms.util.CmsErrorCode"; 73 74 75 public static final String HEADER_ACCEPT_CHARSET = "Accept-Charset"; 76 77 78 public static final String HEADER_ACCEPT_LANGUAGE = "Accept-Language"; 79 80 81 public static final String HEADER_CACHE_CONTROL = "Cache-Control"; 82 83 84 public static final String HEADER_CONTENT_DISPOSITION = "Content-Disposition"; 85 86 87 public static final String HEADER_CONTENT_TYPE = "Content-Type"; 88 89 90 public static final String HEADER_EXPIRES = "Expires"; 91 92 93 public static final String HEADER_IF_MODIFIED_SINCE = "If-Modified-Since"; 94 95 96 public static final String HEADER_JSESSIONID = "JSESSIONID"; 97 98 99 public static final String HEADER_LAST_MODIFIED = "Last-Modified"; 100 101 102 public static final String HEADER_OPENCMS_EXPORT = "OpenCms-Export"; 103 104 105 public static final String HEADER_PRAGMA = "Pragma"; 106 107 108 public static final String HEADER_SERVER = "Server"; 109 110 111 public static final String HEADER_USER_AGENT = "user-agent"; 112 113 114 public static final String HEADER_VALUE_MAX_AGE = "max-age="; 115 116 117 public static final String HEADER_VALUE_MUST_REVALIDATE = "must-revalidate"; 118 119 120 public static final String HEADER_VALUE_NO_CACHE = "no-cache"; 121 122 123 public static final String HEADER_WWW_AUTHENTICATE = "WWW-Authenticate"; 124 125 126 public static final String HEADER_X_FORWARDED_FOR = "x-forwarded-for"; 127 128 129 private static final Log LOG = CmsLog.getLog(CmsRequestUtil.class); 130 131 135 private CmsRequestUtil() { 136 137 } 139 140 153 public static String appendParameter(String url, String paramName, String paramValue) { 154 155 if (CmsStringUtil.isEmpty(url)) { 156 return null; 157 } 158 int pos = url.indexOf('?'); 159 StringBuffer result = new StringBuffer (256); 160 result.append(url); 161 if (pos >= 0) { 162 result.append('&'); 164 } else { 165 result.append('?'); 167 } 168 result.append(paramName); 169 result.append('='); 170 result.append(paramValue); 171 return result.toString(); 172 } 173 174 190 public static String appendParameters(String url, Map params, boolean encode) { 191 192 if (CmsStringUtil.isEmpty(url)) { 193 return null; 194 } 195 if ((params == null) || params.isEmpty()) { 196 return url; 197 } 198 int pos = url.indexOf('?'); 199 StringBuffer result = new StringBuffer (256); 200 result.append(url); 201 if (pos >= 0) { 202 result.append('&'); 204 } else { 205 result.append('?'); 207 } 208 Map newParams = createParameterMap(params); 210 Iterator i = newParams.keySet().iterator(); 211 while (i.hasNext()) { 212 String key = (String )i.next(); 213 Object value = newParams.get(key); 214 String [] values = (String [])value; 215 for (int j = 0; j < values.length; j++) { 216 String strValue = values[j]; 217 if (encode) { 218 strValue = CmsEncoder.encode(strValue); 219 } 220 result.append(key); 221 result.append('='); 222 result.append(strValue); 223 if ((j + 1) < values.length) { 224 result.append('&'); 225 } 226 } 227 if (i.hasNext()) { 228 result.append('&'); 229 } 230 } 231 return result.toString(); 232 } 233 234 244 public static Map createParameterMap(Map params) { 245 246 if (params == null) { 247 return null; 248 } 249 HashMap result = new HashMap (); 250 Iterator i = params.keySet().iterator(); 251 while (i.hasNext()) { 252 String key = i.next().toString(); 253 Object values = params.get(key); 254 if (values instanceof String []) { 255 result.put(key, values); 256 } else { 257 result.put(key, new String [] {values.toString()}); 258 } 259 } 260 return result; 261 } 262 263 275 public static Map createParameterMap(String query) { 276 277 if (CmsStringUtil.isEmpty(query)) { 278 return new HashMap (); 280 } 281 if (query.charAt(0) == '?') { 282 query = query.substring(1); 284 } 285 HashMap parameters = new HashMap (); 286 String [] params = CmsStringUtil.splitAsArray(query, '&'); 288 for (int i = 0; i < params.length; i++) { 289 String key = null; 290 String value = null; 291 int pos = params[i].indexOf('='); 293 if (pos > 0) { 294 key = params[i].substring(0, pos); 295 value = params[i].substring(pos + 1); 296 } else if (pos < 0) { 297 key = params[i]; 298 value = ""; 299 } 300 if (key != null) { 302 String [] values = (String [])parameters.get(key); 303 if (values == null) { 304 values = new String [] {value}; 306 } else { 307 String [] copy = new String [values.length + 1]; 309 System.arraycopy(values, 0, copy, 0, values.length); 310 copy[copy.length - 1] = value; 311 values = copy; 312 } 313 parameters.put(key, values); 314 } 315 } 316 return parameters; 317 } 318 319 329 public static String encodeParams(HttpServletRequest req) { 330 331 StringBuffer result = new StringBuffer (512); 332 Map params = req.getParameterMap(); 333 Iterator i = params.keySet().iterator(); 334 while (i.hasNext()) { 335 String param = (String )i.next(); 336 String [] values = (String [])params.get(param); 337 for (int j = 0; j < values.length; j++) { 338 result.append(param); 339 result.append("="); 340 result.append(CmsEncoder.encode(values[j])); 341 if ((j + 1) < values.length) { 342 result.append("&"); 343 } 344 } 345 if (i.hasNext()) { 346 result.append("&"); 347 } 348 } 349 return CmsEncoder.encode(result.toString()); 350 } 351 352 361 public static String encodeParamsWithUri(String uri, HttpServletRequest req) { 362 363 String result; 364 String params = encodeParams(req); 365 if (CmsStringUtil.isNotEmpty(params)) { 366 result = CmsEncoder.encode(uri + "?") + params; 367 } else { 368 result = CmsEncoder.encode(uri); 369 } 370 return result; 371 } 372 373 391 public static void forwardRequest(String target, HttpServletRequest req, HttpServletResponse res) 392 throws IOException , ServletException { 393 394 CmsUriSplitter uri = new CmsUriSplitter(target); 396 Map params = createParameterMap(uri.getQuery()); 397 forwardRequest(uri.getPrefix(), params, req, res); 398 } 399 400 416 public static void forwardRequest(String target, Map params, HttpServletRequest req, HttpServletResponse res) 417 throws IOException , ServletException { 418 419 CmsFlexRequest f_req = (CmsFlexRequest)req; 421 f_req.setParameterMap(params); 423 String vfsPrefix = OpenCms.getStaticExportManager().getVfsPrefix(); 425 if (target.startsWith(vfsPrefix)) { 426 target = target.substring(vfsPrefix.length()); 428 target = OpenCms.getSystemInfo().getServletPath() + target; 430 } 431 f_req.getRequestDispatcher(target).forward(f_req, res); 433 } 434 435 443 public static String getCookieValue(CmsJspActionElement jsp, String name) { 444 445 Cookie [] cookies = jsp.getRequest().getCookies(); 446 for (int i = 0; cookies != null && i < cookies.length; i++) { 447 if (name.equalsIgnoreCase(cookies[i].getName())) { 448 return cookies[i].getValue(); 449 } 450 } 451 return null; 452 } 453 454 466 public static String getNotEmptyDecodedParameter(HttpServletRequest request, String paramName) { 467 468 String result = getNotEmptyParameter(request, paramName); 469 if (result != null) { 470 result = CmsEncoder.decode(result.trim()); 471 } 472 return result; 473 } 474 475 484 public static String getNotEmptyParameter(HttpServletRequest request, String paramName) { 485 486 String result = request.getParameter(paramName); 487 if (CmsStringUtil.isEmptyOrWhitespaceOnly(result)) { 488 result = null; 489 } 490 return result; 491 } 492 493 505 public static Object getSessionValue(HttpServletRequest request, String key) { 506 507 HttpSession session = request.getSession(true); 508 return session.getAttribute(key); 509 } 510 511 522 public static List readMultipartFileItems(HttpServletRequest request) { 523 524 if (!FileUploadBase.isMultipartContent(request)) { 525 return null; 526 } 527 DiskFileUpload fu = new DiskFileUpload(); 528 fu.setSizeThreshold(4096); 530 fu.setRepositoryPath(OpenCms.getSystemInfo().getPackagesRfsPath()); 532 List result = new ArrayList (); 533 try { 534 List items = fu.parseRequest(request); 535 if (items != null) { 536 result = items; 537 } 538 } catch (FileUploadException e) { 539 LOG.error(Messages.get().getBundle().key(Messages.LOG_PARSE_MULIPART_REQ_FAILED_0), e); 540 } 541 return result; 542 } 543 544 555 public static Map readParameterMapFromMultiPart(String encoding, List multiPartFileItems) { 556 557 Map parameterMap = new HashMap (); 558 Iterator i = multiPartFileItems.iterator(); 559 while (i.hasNext()) { 560 FileItem item = (FileItem)i.next(); 561 String name = item.getFieldName(); 562 String value = null; 563 if (name != null && item.getName() == null) { 564 try { 566 value = item.getString(encoding); 567 } catch (UnsupportedEncodingException e) { 568 LOG.error(Messages.get().getBundle().key(Messages.LOG_ENC_MULTIPART_REQ_ERROR_0), e); 569 value = item.getString(); 570 } 571 parameterMap.put(name, new String [] {value}); 572 } 573 } 574 return parameterMap; 575 } 576 577 588 public static void redirectRequestSecure(CmsJspActionElement jsp, String target) throws IOException { 589 590 jsp.getResponse().sendRedirect(OpenCms.getLinkManager().substituteLink(jsp.getCmsObject(), target, null, true)); 591 } 592 593 602 public static void removeSessionValue(HttpServletRequest request, String key) { 603 604 HttpSession session = request.getSession(true); 605 session.removeAttribute(key); 606 } 607 608 616 public static void setCookieValue(CmsJspActionElement jsp, String name, String value) { 617 618 Cookie [] cookies = jsp.getRequest().getCookies(); 619 for (int i = 0; cookies != null && i < cookies.length; i++) { 620 if (name.equalsIgnoreCase(cookies[i].getName())) { 621 cookies[i].setValue(value); 622 return; 623 } 624 } 625 Cookie cookie = new Cookie (name, value); 626 jsp.getResponse().addCookie(cookie); 627 } 628 629 641 public static void setNoCacheHeaders(HttpServletResponse res) { 642 643 res.setHeader(CmsRequestUtil.HEADER_CACHE_CONTROL, CmsRequestUtil.HEADER_VALUE_MAX_AGE + "0"); 644 res.addHeader(CmsRequestUtil.HEADER_CACHE_CONTROL, CmsRequestUtil.HEADER_VALUE_MUST_REVALIDATE); 645 res.setHeader(CmsRequestUtil.HEADER_PRAGMA, CmsRequestUtil.HEADER_VALUE_NO_CACHE); 646 } 647 648 658 public static void setSessionValue(HttpServletRequest request, String key, Object value) { 659 660 HttpSession session = request.getSession(true); 661 session.setAttribute(key, value); 662 } 663 } | Popular Tags |