Source Code Cross Referenced for SessionHelper.java in  » J2EE » Pustefix » de » schlund » pfixxml » serverutil » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » Pustefix » de.schlund.pfixxml.serverutil 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.