1 2 24 25 package com.lutris.appserver.server; 26 27 import java.net.InetAddress ; 28 import java.net.MalformedURLException ; 29 import java.net.URL ; 30 import java.net.UnknownHostException ; 31 import java.util.StringTokenizer ; 32 33 import javax.servlet.ServletRequest ; 34 import javax.servlet.http.Cookie ; 35 import javax.servlet.http.HttpServletRequest ; 36 37 import com.lutris.appserver.server.httpPresentation.HttpPresentationComms; 38 import com.lutris.appserver.server.httpPresentation.HttpPresentationException; 39 import com.lutris.appserver.server.session.Session; 40 import com.lutris.appserver.server.session.SessionException; 41 import com.lutris.appserver.server.session.SessionManager; 42 43 44 52 public class StandardAppUtil { 53 54 57 public static final String ENHYDRA_SESSION_ID_URL = "jsessionid"; 58 public static final String ENHYDRA_SESSION_ID_COOKIE = "JSESSIONID"; 59 60 63 private StandardAppUtil() { 64 } 65 66 74 public static InetAddress [] getPeerAddress(HttpPresentationComms comms) 76 throws ApplicationException { 77 78 InetAddress [] peer = new InetAddress [1]; 79 String remoteHost; 80 try { 81 remoteHost = comms.request.getRemoteHost(); 82 } catch (HttpPresentationException except) { 83 throw new ApplicationException(except); 84 } 85 if (remoteHost == null) { 86 throw new ApplicationException("Can't find remote host for request."); 87 } 88 if (remoteHost != null) { 89 try { 90 peer[0] = InetAddress.getByName(remoteHost); 91 } catch (UnknownHostException except) { 92 throw new ApplicationException("Can't find remote address for \"" + remoteHost + "\"", except); 93 } 94 } 95 return peer; 96 } 97 98 107 private static String findSessionKey(String applicationName, 108 Cookie [] cookies) { 109 return findSessionKey(cookies); 110 } 111 112 121 private static String findSessionKey(Cookie [] cookies) { 122 if (cookies == null) { 123 return null; 124 } 125 for (int i=0; i<cookies.length; i++) { 126 if (cookies[i].getName().equalsIgnoreCase(ENHYDRA_SESSION_ID_COOKIE)) { 127 return cookies[i].getValue(); 128 } 129 } 130 return null; 131 } 132 133 139 public static String encodeUrl(String url, String id) { 140 String pre = url; 141 String post = ""; 142 int pos; 143 if ((pos = url.indexOf('?')) != -1 || 145 (pos = url.indexOf('#')) != -1) { 146 pre = url.substring(0, pos); 147 post = url.substring(pos); 148 } 149 return pre + ';' + ENHYDRA_SESSION_ID_URL + '=' + id + post; 150 } 151 152 173 public static boolean pointsToPO(String url) { 174 int idx = url.indexOf('?'); if (idx == -1) { 178 idx = url.indexOf('#'); } 180 if (idx > 0) { 181 url = url.substring(0, idx); } 183 184 idx = url.indexOf(".po"); 186 if (idx == -1) { 187 return false; 188 } 189 if (idx == url.length()-3) { 190 return true; 191 } 192 211 if (url.charAt(idx+3) == '/') { 213 try { 216 URL u = new URL (url); 217 int i = u.getFile().indexOf(".po"); 218 return (i != -1); 219 } catch (MalformedURLException e) { 220 return true; 223 } 224 } 225 return false; 226 } 227 228 229 239 public static Session getRequestSession(HttpPresentationComms comms) 240 throws ApplicationException { 241 Session s = null; 242 String sessionKey = null; 243 Cookie [] cookies; 244 SessionManager sessionManager = (SessionManager)comms.application.getSessionManager(); 245 246 try { 247 if (!sessionManager.getEncodeUrlState().equalsIgnoreCase(SessionManager.ENCODE_URL_ALWAYS)) { 248 cookies = comms.request.getCookies(); 249 sessionKey = findSessionKey(ENHYDRA_SESSION_ID_COOKIE, 250 cookies); 251 if (sessionKey != null) { 252 comms.request.setRequestedSessionIdFromCookie(true); 256 comms.request.setRequestedSessionIdFromUrl(false); 257 comms.response.setSessionIdCookieRequired(true); 258 comms.response.setSessionIdEncodeUrlRequired(false); 259 } 260 } 261 262 if (!sessionManager.getEncodeUrlState().equalsIgnoreCase(SessionManager.ENCODE_URL_NEVER) 263 && sessionKey == null) { 264 265 272 HttpServletRequest servletRequest = comms.request.getHttpServletRequest(); 275 sessionKey = servletRequest.getRequestedSessionId(); 276 if (sessionKey != null) { 277 if (servletRequest.isRequestedSessionIdFromURL()) { 278 comms.request.setRequestedSessionIdFromUrl(true); 279 comms.request.setRequestedSessionIdFromCookie(false); 280 comms.response.setSessionIdEncodeUrlRequired(true); 281 comms.response.setSessionIdCookieRequired(false); 282 } 283 } 284 else { 285 String pathInfo = comms.request.getPathInfo(); 286 StringTokenizer tokens = 287 new StringTokenizer (pathInfo, "/\\;=", false); 288 while (tokens.hasMoreTokens()) { 289 String param = tokens.nextToken(); 290 if (param.equalsIgnoreCase(ENHYDRA_SESSION_ID_URL)) { 291 sessionKey = tokens.nextToken(); 292 break; 293 } 294 } 295 296 if (sessionKey != null) { 297 comms.request.setRequestedSessionIdFromUrl(true); 301 comms.request.setRequestedSessionIdFromCookie(false); 302 comms.response.setSessionIdEncodeUrlRequired(true); 303 comms.response.setSessionIdCookieRequired(false); 304 } 305 } 306 } 307 } catch (HttpPresentationException except2) { 308 throw new ApplicationException(except2); 309 } 310 if (sessionKey == null) { 311 return null; 312 } 313 314 if (!sessionManager.getEncodeUrlState().equalsIgnoreCase(SessionManager.ENCODE_URL_NEVER)) { 315 comms.response.setSessionKey(sessionKey); 316 comms.response.setSessionManager(comms.application.getSessionManager()); 317 } 318 try { 319 s = comms.application.getSessionManager().getSession(Thread.currentThread(), 320 sessionKey, comms); 322 } catch (SessionException e) { 323 throw new ApplicationException(e); 324 } 325 return s; 326 } 327 328 345 public static Session getRequestSession(ServletRequest request, 346 Application application) 347 throws ApplicationException { 348 if (application == null) { 349 return null; 350 } 351 Cookie [] cookies; 352 Session s; 353 HttpServletRequest req; 354 try { 355 req = (HttpServletRequest )request; 356 } catch (ClassCastException e) { 357 return null; 358 } 359 cookies = req.getCookies(); 360 String sessionKey = findSessionKey(cookies); 361 if (sessionKey == null) { 362 return null; 363 } 364 SessionManager sm = application.getSessionManager(); 365 if (sm == null) { 366 return null; 367 } 368 try { 369 s = sm.getSession(Thread.currentThread(), sessionKey); 370 } catch (SessionException e) { 371 throw new ApplicationException(e); 372 } 373 return s; 374 } 375 376 377 384 public static void bindSessionToClient(HttpPresentationComms comms) 385 throws ApplicationException { 386 387 Session session = (Session)comms.session; 388 SessionManager sessionManager = (SessionManager)session.getSessionManager(); 389 if (!sessionManager.getEncodeUrlState().equalsIgnoreCase(SessionManager.ENCODE_URL_ALWAYS)) { 390 try { 391 Cookie cookie = new Cookie (ENHYDRA_SESSION_ID_COOKIE, session.getSessionKey()); 392 String cookiePath = comms.request.getApplicationPath(); 394 395 if(cookiePath!=null) 396 { 397 cookiePath.trim(); 398 if(cookiePath.endsWith("/") && cookiePath.length()>1) 399 { 400 cookiePath = cookiePath.substring(0,cookiePath.length()-1); 401 } 402 } 403 cookie.setPath(cookiePath); 404 405 String serverName = comms.request.getServerName(); 406 414 comms.response.addCookie(cookie); 415 } catch (HttpPresentationException except) { 416 throw new ApplicationException(except); 417 } 418 } 419 if (!sessionManager.getEncodeUrlState().equalsIgnoreCase(SessionManager.ENCODE_URL_NEVER)) { 420 comms.response.setSessionKey(comms.session.getSessionKey()); 421 comms.response.setSessionManager(comms.application.getSessionManager()); 422 } 423 } 424 } 425 | Popular Tags |