KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > cachius > support > SessionUtils


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.cachius.support;
25
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import javax.servlet.http.HttpServletResponse JavaDoc;
28 import javax.servlet.http.HttpSession JavaDoc;
29
30 /**
31  * Utility class for working with URL-based session tracking.
32  */

33 public final class SessionUtils {
34
35     private static final String JavaDoc INITIAL_SESSION_STATE =
36             SessionUtils.class.getName() + ".initialState";
37
38     private SessionUtils() {
39     }
40     
41     /**
42      * Returns whether URLs would be encoded by the servlet container if
43      * {@link HttpServletResponse#encodeURL(java.lang.String)
44      * response.encodeURL()} was called.
45      *
46      * Since the session state may change during a request (due to parallel
47      * request caused by embedded resources) the initial state is stored as
48      * request attribute.
49      */

50     public static boolean urlsNeedEncoding(HttpServletRequest JavaDoc request) {
51         boolean encodeUrls = urlsCurrentlyNeedEncoding(request);
52         Boolean JavaDoc initialState = (Boolean JavaDoc) request.getAttribute(INITIAL_SESSION_STATE);
53         if (initialState == null) {
54             initialState = Boolean.valueOf(encodeUrls);
55             request.setAttribute(INITIAL_SESSION_STATE, initialState);
56         }
57         return encodeUrls;
58     }
59     
60     public static boolean sessionStateChanged(HttpServletRequest JavaDoc request) {
61         Boolean JavaDoc initialState = (Boolean JavaDoc) request.getAttribute(INITIAL_SESSION_STATE);
62         if (initialState == null) {
63             throw new IllegalStateException JavaDoc("urlsNeedEncoding() must be called first.");
64         }
65         return urlsCurrentlyNeedEncoding(request) != initialState.booleanValue();
66     }
67     
68     public static void addStateToCacheKey(HttpServletRequest JavaDoc request,
69             StringBuffer JavaDoc key) {
70         
71         if (urlsNeedEncoding(request)) {
72             key.append(";jsessionid");
73         }
74     }
75     
76     private static boolean urlsCurrentlyNeedEncoding(HttpServletRequest JavaDoc request) {
77         HttpSession JavaDoc session = request.getSession(false);
78         if (session == null) {
79             return false;
80         }
81         return session.isNew() || request.isRequestedSessionIdFromURL();
82     }
83
84 }
85
Popular Tags