001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: */
019:
020: package de.schlund.pfixxml.serverutil;
021:
022: import java.util.Enumeration;
023: import java.util.Iterator;
024: import java.util.Map;
025: import java.util.Properties;
026:
027: import javax.servlet.http.HttpServletRequest;
028: import javax.servlet.http.HttpSession;
029:
030: import org.apache.log4j.Logger;
031:
032: import de.schlund.pfixxml.PfixServletRequest;
033: import de.schlund.pfixxml.ServletManager;
034:
035: public class SessionHelper {
036:
037: private final static Logger LOG = Logger
038: .getLogger(SessionHelper.class);
039:
040: public static final String SESSION_ID_URL = "__SESSION_ID_URL__";
041:
042: private static final String ENC_STR = "jsessionid";
043:
044: public static void saveSessionData(Map<String, Object> store,
045: HttpSession session) {
046: try {
047: Enumeration<?> enm = session.getAttributeNames();
048: while (enm.hasMoreElements()) {
049: String valName = (String) enm.nextElement();
050: store.put(valName, session.getAttribute(valName));
051: }
052: } catch (NullPointerException e) {
053: LOG.warn("Caught NP-Exception: " + e.getMessage());
054: }
055: }
056:
057: public static void copySessionData(Map<String, Object> store,
058: HttpSession session) {
059: try {
060: Iterator<String> iter = store.keySet().iterator();
061: String key = null;
062: Object value = null;
063: while (iter.hasNext()) {
064: key = iter.next();
065: value = store.get(key);
066: if (value instanceof NoCopySessionData) {
067: LOG
068: .debug("*** Will not copy a object implementing NoCopySessionData!!! ***");
069: } else if (!key.equals(SessionAdmin.LISTENER)) {
070: session.setAttribute(key, value);
071: }
072: }
073: } catch (NullPointerException e) {
074: LOG.warn("Caught NP-Exception: " + e.getMessage());
075: }
076: }
077:
078: public static String getClearedURI(PfixServletRequest req) {
079: StringBuffer rcBuf = new StringBuffer();
080: stripUriSessionId(null, req.getRequestURI(), rcBuf);
081: return rcBuf.toString();
082: }
083:
084: public static String getClearedURI(HttpServletRequest req) {
085: StringBuffer rcBuf = new StringBuffer();
086: stripUriSessionId(null, req.getRequestURI(), rcBuf);
087: return rcBuf.toString();
088: }
089:
090: public static String getClearedURL(String scheme, String host,
091: HttpServletRequest req) {
092: return getClearedURL(scheme, host, req, null);
093: }
094:
095: public static String getClearedURL(String scheme, String host,
096: HttpServletRequest req, Properties props) {
097: if (scheme == null)
098: scheme = req.getScheme();
099: if (host == null)
100: host = req.getServerName();
101: StringBuffer rcBuf = createPrefix(scheme, host, req, props);
102: stripUriSessionId(null, req.getRequestURI(), rcBuf);
103:
104: String query = req.getQueryString();
105: if (query != null && 0 < query.length()) {
106: rcBuf.append('?').append(query);
107: }
108: return rcBuf.toString();
109: }
110:
111: public static String getURLSessionId(HttpServletRequest req) {
112: String rc = ENC_STR + "=" + req.getSession(false).getId();
113: return rc;
114: }
115:
116: public static String encodeURI(HttpServletRequest req) {
117: StringBuffer rcBuf = new StringBuffer();
118:
119: String oldSessionId = stripUriSessionId(null, req
120: .getRequestURI(), rcBuf);
121:
122: HttpSession session = req.getSession(false);
123: if (session != null) {
124: rcBuf.append(';').append(ENC_STR).append('=').append(
125: session.getId());
126: } else if (oldSessionId != null && 0 < oldSessionId.length()) {
127: rcBuf.append(';').append(ENC_STR).append('=').append(
128: oldSessionId);
129: }
130:
131: return rcBuf.toString();
132: }
133:
134: public static String encodeURL(String scheme, String host,
135: HttpServletRequest req) {
136: return encodeURL(scheme, host, req, null, null);
137: }
138:
139: public static String encodeURL(String scheme, String host,
140: HttpServletRequest req, Properties props) {
141: return encodeURL(scheme, host, req, null, props);
142: }
143:
144: public static String encodeURL(String scheme, String host,
145: HttpServletRequest req, String sessid) {
146: return encodeURL(scheme, host, req, sessid, null);
147: }
148:
149: public static String encodeURL(String scheme, String host,
150: HttpServletRequest req, String sessid, Properties props) {
151: if (scheme == null)
152: scheme = req.getScheme();
153: if (host == null)
154: host = req.getServerName();
155: StringBuffer rcBuf = createPrefix(scheme, host, req, props);
156: String oldSessionId = stripUriSessionId(null, req
157: .getRequestURI(), rcBuf);
158: HttpSession session = req.getSession(false);
159:
160: if (sessid != null) {
161: rcBuf.append(';').append(ENC_STR).append('=')
162: .append(sessid);
163: } else if (session != null) {
164: rcBuf.append(';').append(ENC_STR).append('=').append(
165: session.getId());
166: } else if (oldSessionId != null && 0 < oldSessionId.length()) {
167: rcBuf.append(';').append(ENC_STR).append('=').append(
168: oldSessionId);
169: }
170:
171: String query = req.getQueryString();
172: if (query != null && 0 < query.length()) {
173: rcBuf.append('?').append(query);
174: }
175: return rcBuf.toString();
176: }
177:
178: private static StringBuffer createPrefix(String scheme,
179: String host, HttpServletRequest req, Properties props) {
180: StringBuffer rcBuf;
181:
182: rcBuf = new StringBuffer();
183: rcBuf.append(scheme).append("://").append(host);
184: if (ServletManager.isDefault(req.getScheme(), req
185: .getServerPort())) {
186: // don't care about port -- stick with defaults
187: } else {
188: // we are using non-default ports and are redirecting to ssl:
189: // try to get the right ssl port from the configuration
190: if ("https".equals(scheme) && !req.isSecure()) {
191: if (props != null) {
192: String redirectPort = props
193: .getProperty(ServletManager.PROP_SSL_REDIRECT_PORT
194: + String.valueOf(req
195: .getServerPort()));
196: if (redirectPort == null) {
197: // we have not found the right port, so try the default one
198: redirectPort = "";
199: } else {
200: redirectPort = ":" + redirectPort;
201: }
202: rcBuf.append(redirectPort);
203: }
204: } else {
205: rcBuf.append(":").append(req.getServerPort());
206: }
207: }
208: return rcBuf;
209: }
210:
211: protected static String stripUriSessionId(String oldSessionId,
212: String uri, StringBuffer rcUri) {
213: String rc = oldSessionId;
214: try {
215: int semiIdx = uri.indexOf(";jsessionid");
216: if (0 <= semiIdx) {
217: rc = uri.substring(semiIdx + 1);
218: uri = uri.substring(0, semiIdx);
219: }
220:
221: if (uri.startsWith("/jsessionid")) {
222: int nextSlash = uri.indexOf('/', 1);
223: if (0 < nextSlash) {
224: rc = uri.substring(1, nextSlash);
225: uri = uri.substring(nextSlash);
226: } else {
227: rc = uri.substring(1);
228: }
229: }
230:
231: if (uri.length() == 0)
232: uri = "/";
233:
234: rcUri.append(uri);
235: } catch (NullPointerException e) {
236: LOG.warn("Caught NP-Exception: " + e.getMessage());
237: }
238: return rc;
239: }
240:
241: }
|