KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > util > CmsRequestUtil


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/util/CmsRequestUtil.java,v $
3  * Date : $Date: 2006/04/28 15:20:52 $
4  * Version: $Revision: 1.19 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

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 JavaDoc;
41 import java.io.UnsupportedEncodingException JavaDoc;
42 import java.util.ArrayList JavaDoc;
43 import java.util.HashMap JavaDoc;
44 import java.util.Iterator JavaDoc;
45 import java.util.List JavaDoc;
46 import java.util.Map JavaDoc;
47
48 import javax.servlet.ServletException JavaDoc;
49 import javax.servlet.http.Cookie JavaDoc;
50 import javax.servlet.http.HttpServletRequest JavaDoc;
51 import javax.servlet.http.HttpServletResponse JavaDoc;
52 import javax.servlet.http.HttpSession JavaDoc;
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 /**
61  * Provides utility functions for dealing with values a <code>{@link HttpServletRequest}</code>.<p>
62  *
63  * @author Alexander Kandzior
64  *
65  * @version $Revision: 1.19 $
66  *
67  * @since 6.0.0
68  */

69 public final class CmsRequestUtil {
70
71     /** Request attribute that contains the original error code. */
72     public static final String JavaDoc ATTRIBUTE_ERRORCODE = "org.opencms.util.CmsErrorCode";
73
74     /** HTTP Accept-Charset Header for internal requests used during static export. */
75     public static final String JavaDoc HEADER_ACCEPT_CHARSET = "Accept-Charset";
76
77     /** HTTP Accept-Language Header for internal requests used during static export. */
78     public static final String JavaDoc HEADER_ACCEPT_LANGUAGE = "Accept-Language";
79
80     /** HTTP Header "Cache-Control". */
81     public static final String JavaDoc HEADER_CACHE_CONTROL = "Cache-Control";
82
83     /** The "Content-Disposition" http header. */
84     public static final String JavaDoc HEADER_CONTENT_DISPOSITION = "Content-Disposition";
85
86     /** The "Content-Type" http header. */
87     public static final String JavaDoc HEADER_CONTENT_TYPE = "Content-Type";
88
89     /** HTTP Header "Expires". */
90     public static final String JavaDoc HEADER_EXPIRES = "Expires";
91
92     /** HTTP Header "If-Modified-Since". */
93     public static final String JavaDoc HEADER_IF_MODIFIED_SINCE = "If-Modified-Since";
94
95     /** The Header that stores the session id (used by OpenCms upload applet). */
96     public static final String JavaDoc HEADER_JSESSIONID = "JSESSIONID";
97
98     /** HTTP Header "Last-Modified". */
99     public static final String JavaDoc HEADER_LAST_MODIFIED = "Last-Modified";
100
101     /** HTTP Header for internal requests used during static export. */
102     public static final String JavaDoc HEADER_OPENCMS_EXPORT = "OpenCms-Export";
103
104     /** HTTP Header "Pragma". */
105     public static final String JavaDoc HEADER_PRAGMA = "Pragma";
106
107     /** HTTP Header "Server". */
108     public static final String JavaDoc HEADER_SERVER = "Server";
109
110     /** HTTP Header "user-agent". */
111     public static final String JavaDoc HEADER_USER_AGENT = "user-agent";
112
113     /** HTTP Header value "max-age=" (for "Cache-Control"). */
114     public static final String JavaDoc HEADER_VALUE_MAX_AGE = "max-age=";
115
116     /** HTTP Header value "must-revalidate" (for "Cache-Control"). */
117     public static final String JavaDoc HEADER_VALUE_MUST_REVALIDATE = "must-revalidate";
118
119     /** HTTP Header value "no-cache" (for "Cache-Control"). */
120     public static final String JavaDoc HEADER_VALUE_NO_CACHE = "no-cache";
121
122     /** HTTP Header "WWW-Authenticate". */
123     public static final String JavaDoc HEADER_WWW_AUTHENTICATE = "WWW-Authenticate";
124
125     /** Identifier for x-forwarded-for (i.e. proxied) request headers. */
126     public static final String JavaDoc HEADER_X_FORWARDED_FOR = "x-forwarded-for";
127
128     /** The log object for this class. */
129     private static final Log LOG = CmsLog.getLog(CmsRequestUtil.class);
130
131     /**
132      * Default constructor (empty), private because this class has only
133      * static methods.<p>
134      */

135     private CmsRequestUtil() {
136
137         // empty
138
}
139
140     /**
141      * Appends a request parameter to the given URL.<p>
142      *
143      * This method takes care about the adding the parameter as an additional
144      * parameter (appending <code>&param=value</code>) or as the first parameter
145      * (appending <code>?param=value</code>).<p>
146      *
147      * @param url the URL where to append the parameter to
148      * @param paramName the paramter name to append
149      * @param paramValue the parameter value to append
150      *
151      * @return the URL with the given parameter appended
152      */

153     public static String JavaDoc appendParameter(String JavaDoc url, String JavaDoc paramName, String JavaDoc paramValue) {
154
155         if (CmsStringUtil.isEmpty(url)) {
156             return null;
157         }
158         int pos = url.indexOf('?');
159         StringBuffer JavaDoc result = new StringBuffer JavaDoc(256);
160         result.append(url);
161         if (pos >= 0) {
162             // url already has parameters
163
result.append('&');
164         } else {
165             // url does not have parameters
166
result.append('?');
167         }
168         result.append(paramName);
169         result.append('=');
170         result.append(paramValue);
171         return result.toString();
172     }
173
174     /**
175      * Appends a map of request parameters to the given URL.<p>
176      *
177      * The map can cointains values of <code>String[]</code> or
178      * simple <code>String</code> values.<p>
179      *
180      * This method takes care about the adding the parameter as an additional
181      * parameter (appending <code>&param=value</code>) or as the first parameter
182      * (appending <code>?param=value</code>).<p>
183      *
184      * @param url the URL where to append the parameter to
185      * @param params the paramters to append
186      * @param encode if <code>true</code>, the parameter values are encoded before they are appended
187      *
188      * @return the URL with the given parameter appended
189      */

190     public static String JavaDoc appendParameters(String JavaDoc url, Map JavaDoc 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 JavaDoc result = new StringBuffer JavaDoc(256);
200         result.append(url);
201         if (pos >= 0) {
202             // url already has parameters
203
result.append('&');
204         } else {
205             // url does not have parameters
206
result.append('?');
207         }
208         // ensure all values are of type String[]
209
Map JavaDoc newParams = createParameterMap(params);
210         Iterator JavaDoc i = newParams.keySet().iterator();
211         while (i.hasNext()) {
212             String JavaDoc key = (String JavaDoc)i.next();
213             Object JavaDoc value = newParams.get(key);
214             String JavaDoc[] values = (String JavaDoc[])value;
215             for (int j = 0; j < values.length; j++) {
216                 String JavaDoc 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     /**
235      * Creates a valid request parameter map from the given map,
236      * most notably changing the values form <code>String</code>
237      * to <code>String[]</code> if required.<p>
238      *
239      * If the given parameter map is <code>null</code>, then <code>null</code> is returned.<p>
240      *
241      * @param params the map of parameters to create a parameter map from
242      * @return the created parameter map, all values will be instances of <code>String[]</code>
243      */

244     public static Map JavaDoc createParameterMap(Map JavaDoc params) {
245
246         if (params == null) {
247             return null;
248         }
249         HashMap JavaDoc result = new HashMap JavaDoc();
250         Iterator JavaDoc i = params.keySet().iterator();
251         while (i.hasNext()) {
252             String JavaDoc key = i.next().toString();
253             Object JavaDoc values = params.get(key);
254             if (values instanceof String JavaDoc[]) {
255                 result.put(key, values);
256             } else {
257                 result.put(key, new String JavaDoc[] {values.toString()});
258             }
259         }
260         return result;
261     }
262
263     /**
264      * Parses the parameters of the given request query part and creaes a parameter map out of them.<p>
265      *
266      * Please note: This does not parse a full request URI/URL, only the query part that
267      * starts after the "?". For example, in the URI <code>/system/index.html?a=b&amp;c=d</code>,
268      * the query part is <code>a=b&amp;c=d</code>.<p>
269      *
270      * If the given String is empty, an empty map is returned.<p>
271      *
272      * @param query the query to parse
273      * @return the parameter map created from the query
274      */

275     public static Map JavaDoc createParameterMap(String JavaDoc query) {
276
277         if (CmsStringUtil.isEmpty(query)) {
278             // empty query
279
return new HashMap JavaDoc();
280         }
281         if (query.charAt(0) == '?') {
282             // remove leading '?' if required
283
query = query.substring(1);
284         }
285         HashMap JavaDoc parameters = new HashMap JavaDoc();
286         // cut along the different parameters
287
String JavaDoc[] params = CmsStringUtil.splitAsArray(query, '&');
288         for (int i = 0; i < params.length; i++) {
289             String JavaDoc key = null;
290             String JavaDoc value = null;
291             // get key and value, separated by a '='
292
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             // now make sure the values are of type String[]
301
if (key != null) {
302                 String JavaDoc[] values = (String JavaDoc[])parameters.get(key);
303                 if (values == null) {
304                     // this is the first value, create new array
305
values = new String JavaDoc[] {value};
306                 } else {
307                     // append to the existing value array
308
String JavaDoc[] copy = new String JavaDoc[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     /**
320      * Returns all parameters of the given request
321      * as a request parameter URL String, that is in the form <code>key1=value1&key2=value2</code> etc.
322      *
323      * The result will be encoded using the <code>{@link CmsEncoder#encode(String)}</code> function.<p>
324      *
325      * @param req the request to read the parameters from
326      *
327      * @return all initialized parameters of the given request as request parameter URL String
328      */

329     public static String JavaDoc encodeParams(HttpServletRequest JavaDoc req) {
330
331         StringBuffer JavaDoc result = new StringBuffer JavaDoc(512);
332         Map JavaDoc params = req.getParameterMap();
333         Iterator JavaDoc i = params.keySet().iterator();
334         while (i.hasNext()) {
335             String JavaDoc param = (String JavaDoc)i.next();
336             String JavaDoc[] values = (String JavaDoc[])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     /**
353      * Encodes the given uri, with all parameters from the given request appended.<p>
354      *
355      * The result will be encoded using the <code>{@link CmsEncoder#encode(String)}</code> function.<p>
356      *
357      * @param req the request where to read the parameters from
358      * @param uri the uri to encode
359      * @return the encoded uri, with all parameters from the given request appended
360      */

361     public static String JavaDoc encodeParamsWithUri(String JavaDoc uri, HttpServletRequest JavaDoc req) {
362
363         String JavaDoc result;
364         String JavaDoc 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     /**
374      * Forwards the response to the given target, which may contain parameters appended like for example <code>?a=b&amp;c=d</code>.<p>
375      *
376      * Please note: If possible, use <code>{@link #forwardRequest(String, Map, HttpServletRequest, HttpServletResponse)}</code>
377      * where the parameters are passed as a map, since the parsing of the parameters may introduce issues with encoding
378      * and is in general much less effective.<p>
379      *
380      * The parsing of parameters will likley fail for "large values" (e.g. full blown web forms with &lt;textarea&gt;
381      * elements etc. Use this method only if you know that the target will just contain up to 3 parameters which
382      * are relativly short and have no encoding or linebreak issues.<p>
383      *
384      * @param target the target to forward to (may contain parameters like <code>?a=b&amp;c=d</code>)
385      * @param req the request to forward
386      * @param res the response to forward
387      *
388      * @throws IOException in case the forwarding fails
389      * @throws ServletException in case the forwarding fails
390      */

391     public static void forwardRequest(String JavaDoc target, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
392     throws IOException JavaDoc, ServletException JavaDoc {
393
394         // clear the current parameters
395
CmsUriSplitter uri = new CmsUriSplitter(target);
396         Map JavaDoc params = createParameterMap(uri.getQuery());
397         forwardRequest(uri.getPrefix(), params, req, res);
398     }
399
400     /**
401      * Forwards the response to the given target, with the provided parameter map.<p>
402      *
403      * The target uri must NOT have parameters appended like for example <code>?a=b&amp;c=d</code>.
404      * The values in the provided map must be of type <code>String[]</code>. If required, use
405      * <code>{@link #createParameterMap(Map)}</code> before calling this method to make sure
406      * all values are actually of the required array type.<p>
407      *
408      * @param target the target to forward to (may NOT contain parameters like <code>?a=b&amp;c=d</code>)
409      * @param params the parameter map (the values must be of type <code>String[]</code>
410      * @param req the request to forward
411      * @param res the response to forward
412      *
413      * @throws IOException in case the forwarding fails
414      * @throws ServletException in case the forwarding fails
415      */

416     public static void forwardRequest(String JavaDoc target, Map JavaDoc params, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
417     throws IOException JavaDoc, ServletException JavaDoc {
418
419         // cast the request back to a flex request so the parameter map can be accessed
420
CmsFlexRequest f_req = (CmsFlexRequest)req;
421         // set the parameters
422
f_req.setParameterMap(params);
423         // check for links "into" OpenCms, these may need the webapp name to be removed
424
String JavaDoc vfsPrefix = OpenCms.getStaticExportManager().getVfsPrefix();
425         if (target.startsWith(vfsPrefix)) {
426             // remove VFS prefix (will also work for empty vfs prefix in ROOT webapp case with proxy rules)
427
target = target.substring(vfsPrefix.length());
428             // append the servlet name
429
target = OpenCms.getSystemInfo().getServletPath() + target;
430         }
431         // forward the request
432
f_req.getRequestDispatcher(target).forward(f_req, res);
433     }
434
435     /**
436      * Returns the value of the cookie with the given name.<p/>
437      *
438      * @param jsp the CmsJspActionElement to use
439      * @param name the name of the cookie
440      *
441      * @return the value of the cookie with the given name or null, if no cookie exists with the name
442      */

443     public static String JavaDoc getCookieValue(CmsJspActionElement jsp, String JavaDoc name) {
444
445         Cookie JavaDoc[] 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     /**
455      * Reads value from the request parameters,
456      * will return <code>null</code> if the value is not available or only white space.<p>
457      *
458      * The value of the request will also be decoded using <code>{@link CmsEncoder#decode(String)}</code>
459      * and also trimmed using <code>{@link String#trim()}</code>.<p>
460      *
461      * @param request the request to read the parameter from
462      * @param paramName the parameter name to read
463      *
464      * @return the request parameter value for the given parameter
465      */

466     public static String JavaDoc getNotEmptyDecodedParameter(HttpServletRequest JavaDoc request, String JavaDoc paramName) {
467
468         String JavaDoc result = getNotEmptyParameter(request, paramName);
469         if (result != null) {
470             result = CmsEncoder.decode(result.trim());
471         }
472         return result;
473     }
474
475     /**
476      * Reads value from the request parameters,
477      * will return <code>null</code> if the value is not available or only white space.<p>
478      *
479      * @param request the request to read the parameter from
480      * @param paramName the parameter name to read
481      *
482      * @return the request parameter value for the given parameter
483      */

484     public static String JavaDoc getNotEmptyParameter(HttpServletRequest JavaDoc request, String JavaDoc paramName) {
485
486         String JavaDoc result = request.getParameter(paramName);
487         if (CmsStringUtil.isEmptyOrWhitespaceOnly(result)) {
488             result = null;
489         }
490         return result;
491     }
492
493     /**
494      * Reads an object from the session of the given http request.<p>
495      *
496      * A session will be initilaized if the request does not currently have a session.
497      * As a result, the request will always have a session after this method has been called.<p>
498      *
499      * Will return <code>null</code> if no corresponding object is found in the session.<p>
500      *
501      * @param request the request to get the session from
502      * @param key the key of the object to read from the session
503      * @return the object received form the session, or <code>null</code>
504      */

505     public static Object JavaDoc getSessionValue(HttpServletRequest JavaDoc request, String JavaDoc key) {
506
507         HttpSession JavaDoc session = request.getSession(true);
508         return session.getAttribute(key);
509     }
510
511     /**
512      * Parses a request of the form <code>multipart/form-data</code>.
513      *
514      * The result list will contain items of type <code>{@link FileItem}</code>.
515      * If the request is not of type <code>multipart/form-data</code>, then <code>null</code> is returned.<p>
516      *
517      * @param request the HTTP servlet request to parse
518      *
519      * @return the list of <code>{@link FileItem}</code> extracted from the multipart request,
520      * or <code>null</code> if the request was not of type <code>multipart/form-data</code>
521      */

522     public static List JavaDoc readMultipartFileItems(HttpServletRequest JavaDoc request) {
523
524         if (!FileUploadBase.isMultipartContent(request)) {
525             return null;
526         }
527         DiskFileUpload fu = new DiskFileUpload();
528         // maximum size that will be stored in memory
529
fu.setSizeThreshold(4096);
530         // the location for saving data that is larger than getSizeThreshold()
531
fu.setRepositoryPath(OpenCms.getSystemInfo().getPackagesRfsPath());
532         List JavaDoc result = new ArrayList JavaDoc();
533         try {
534             List JavaDoc 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     /**
545      * Creates a "standard" request parameter map from the values of a
546      * <code>multipart/form-data</code> request.<p>
547      *
548      * @param encoding the encoding to use when creating the values
549      * @param multiPartFileItems the list of parsed multi part file items
550      *
551      * @return a map containing all non-file request parameters
552      *
553      * @see #readMultipartFileItems(HttpServletRequest)
554      */

555     public static Map JavaDoc readParameterMapFromMultiPart(String JavaDoc encoding, List JavaDoc multiPartFileItems) {
556
557         Map JavaDoc parameterMap = new HashMap JavaDoc();
558         Iterator JavaDoc i = multiPartFileItems.iterator();
559         while (i.hasNext()) {
560             FileItem item = (FileItem)i.next();
561             String JavaDoc name = item.getFieldName();
562             String JavaDoc value = null;
563             if (name != null && item.getName() == null) {
564                 // only put to map if current item is no file and not null
565
try {
566                     value = item.getString(encoding);
567                 } catch (UnsupportedEncodingException JavaDoc 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 JavaDoc[] {value});
572             }
573         }
574         return parameterMap;
575     }
576
577     /**
578      * Redirects the response to the target link.<p>
579      *
580      * Use this method instead of {@link javax.servlet.http.HttpServletResponse#sendRedirect(java.lang.String)}
581      * to avoid relative links with secure sites (and issues with apache).<p>
582      *
583      * @param jsp the jsp context
584      * @param target the target link
585      *
586      * @throws IOException if something goes wrong during redirection
587      */

588     public static void redirectRequestSecure(CmsJspActionElement jsp, String JavaDoc target) throws IOException JavaDoc {
589
590         jsp.getResponse().sendRedirect(OpenCms.getLinkManager().substituteLink(jsp.getCmsObject(), target, null, true));
591     }
592
593     /**
594      * Removes an object from the session of the given http request.<p>
595      *
596      * A session will be initilaized if the request does not currently have a session.
597      * As a result, the request will always have a session after this method has been called.<p>
598      *
599      * @param request the request to get the session from
600      * @param key the key of the object to be removed from the session
601      */

602     public static void removeSessionValue(HttpServletRequest JavaDoc request, String JavaDoc key) {
603
604         HttpSession JavaDoc session = request.getSession(true);
605         session.removeAttribute(key);
606     }
607
608     /**
609      * Sets the value of a specific cookie.<p>
610      * If no cookie exists with the value, a new cookie will be created.
611      *
612      * @param jsp the CmsJspActionElement to use
613      * @param name the name of the cookie
614      * @param value the value of the cookie
615      */

616     public static void setCookieValue(CmsJspActionElement jsp, String JavaDoc name, String JavaDoc value) {
617
618         Cookie JavaDoc[] 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 JavaDoc cookie = new Cookie JavaDoc(name, value);
626         jsp.getResponse().addCookie(cookie);
627     }
628
629     /**
630      * Sets headers to the given response to prevent client side caching.<p>
631      *
632      * The following headers are set:<p>
633      * <code>
634      * Cache-Control: max-age=0<br>
635      * Cache-Control: must-revalidate<br>
636      * Pragma: no-cache
637      * </code>
638      *
639      * @param res the request where to set the no-cache headers
640      */

641     public static void setNoCacheHeaders(HttpServletResponse JavaDoc 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     /**
649      * Adds an object to the session of the given http request.<p>
650      *
651      * A session will be initilaized if the request does not currently have a session.
652      * As a result, the request will always have a session after this method has been called.<p>
653      *
654      * @param request the request to get the session from
655      * @param key the key of the object to be stored in the session
656      * @param value the object to be stored in the session
657      */

658     public static void setSessionValue(HttpServletRequest JavaDoc request, String JavaDoc key, Object JavaDoc value) {
659
660         HttpSession JavaDoc session = request.getSession(true);
661         session.setAttribute(key, value);
662     }
663 }
Popular Tags