1 14 package org.wings.session; 15 16 import org.apache.commons.logging.Log; 17 import org.apache.commons.logging.LogFactory; 18 import org.wings.RequestURL; 19 import org.wings.externalizer.AbstractExternalizeManager; 20 import org.wings.externalizer.ExternalizedResource; 21 import org.wings.externalizer.SystemExternalizeManager; 22 import org.wings.io.Device; 23 import org.wings.io.ServletDevice; 24 25 import javax.servlet.ServletConfig ; 26 import javax.servlet.ServletException ; 27 import javax.servlet.http.HttpServlet ; 28 import javax.servlet.http.HttpServletRequest ; 29 import javax.servlet.http.HttpServletResponse ; 30 import javax.servlet.http.HttpSession ; 31 import java.io.File ; 32 import java.io.IOException ; 33 import java.io.UnsupportedEncodingException ; 34 import java.util.Enumeration ; 35 import java.util.Iterator ; 36 37 44 public final class WingServlet 45 extends HttpServlet { 46 private final transient static Log log = LogFactory.getLog(WingServlet.class); 47 48 51 protected ServletConfig servletConfig = null; 52 53 private String lookupName = "SessionServlet"; 54 55 public WingServlet() { 56 } 57 58 59 protected void initLookupName(ServletConfig config) { 60 lookupName = config.getInitParameter("wings.servlet.lookupname"); 63 64 if (lookupName == null || lookupName.trim().length() == 0) { 65 lookupName = "SessionServlet:" + config.getInitParameter("wings.mainclass"); 66 } 67 68 log.info("use session servlet lookup name " + lookupName); 69 } 70 71 92 public final void init(ServletConfig config) throws ServletException { 93 super.init(config); 94 servletConfig = config; 95 96 if (log.isInfoEnabled()) { 97 log.info("init-params:"); 98 for (Enumeration en = config.getInitParameterNames(); en.hasMoreElements();) { 99 String param = (String ) en.nextElement(); 100 log.info(param + " = " + config.getInitParameter(param)); 101 } 102 } 103 104 initLookupName(config); 105 } 106 107 111 protected long getLastModified(HttpServletRequest request) { 112 AbstractExternalizeManager extMgr; 113 try { 114 extMgr = getExternalizeManager(request); 115 } catch (Exception e) { 116 return System.currentTimeMillis(); 117 } 118 String pathInfo = request.getPathInfo(); 119 if (extMgr != null && pathInfo != null && pathInfo.length() > 1) { 120 String identifier = pathInfo.substring(1); 121 ExternalizedResource info = extMgr.getExternalizedResource(identifier); 122 if (info != null) { 123 return info.getLastModified(); 125 } 126 } 127 return -1; 128 } 129 130 133 public final void doPost(HttpServletRequest req, HttpServletResponse res) 134 throws ServletException , IOException { 135 SessionServlet sessionServlet = getSessionServlet(req, res, true); 136 137 if (log.isDebugEnabled()) 138 log.debug((sessionServlet != null) ? 139 lookupName : 140 "no session yet .."); 141 142 try { 145 int maxContentLength = sessionServlet.getSession().getMaxContentLength(); 146 req = new MultipartRequest(req, maxContentLength * 1024); 147 } catch (Exception e) { 148 log.fatal(null, e); 149 } 150 151 if (log.isDebugEnabled()) { 152 if (req instanceof MultipartRequest) { 153 MultipartRequest multi = (MultipartRequest) req; 154 log.debug("Files:"); 155 Iterator files = multi.getFileNames(); 156 while (files.hasNext()) { 157 String name = (String ) files.next(); 158 String filename = multi.getFileName(name); 159 String type = multi.getContentType(name); 160 File f = multi.getFile(name); 161 log.debug("name: " + name); 162 log.debug("filename: " + filename); 163 log.debug("type: " + type); 164 if (f != null) { 165 log.debug("f.toString(): " + f.toString()); 166 log.debug("f.getDescription(): " + f.getName()); 167 log.debug("f.exists(): " + f.exists()); 168 log.debug("f.length(): " + f.length()); 169 log.debug("\n"); 170 } 171 } 172 } 173 } 174 175 doGet(req, res); 176 } 177 178 private final SessionServlet newSession(HttpServletRequest request, 179 HttpServletResponse response) 180 throws ServletException { 181 long timestamp = System.currentTimeMillis(); 182 try { 183 log.debug("new session"); 184 185 SessionServlet sessionServlet = new SessionServlet(); 186 sessionServlet.init(servletConfig, request, response); 187 188 Session session = sessionServlet.getSession(); 189 196 RequestURL requestURL = new RequestURL("", SessionServlet.getSessionEncoding(response)); 197 198 session.setProperty("request.url", requestURL); 199 200 sessionServlet.setParent(this); 201 202 log.debug("time to create a new session " + (System.currentTimeMillis() - timestamp)); 203 return sessionServlet; 204 } catch (Exception e) { 205 log.fatal("Error on creating new wingS session", e); 206 throw new ServletException (e); 207 } 208 } 209 210 public final SessionServlet getSessionServlet(HttpServletRequest request, 211 HttpServletResponse response, 212 boolean createSessionServlet) 213 throws ServletException { 214 HttpSession httpSession = request.getSession(true); 215 216 synchronized (httpSession) { 218 SessionServlet sessionServlet = null; 219 220 if (httpSession != null) { 221 sessionServlet = (SessionServlet) httpSession.getAttribute(lookupName); 222 } 223 224 229 if (sessionServlet == null && createSessionServlet) { 230 log.info("no session servlet, create new one"); 231 sessionServlet = newSession(request, response); 232 httpSession.setAttribute(lookupName, sessionServlet); 233 } 234 235 236 if (log.isDebugEnabled()) { 237 log.debug("session id " + request.getRequestedSessionId()); 238 log.debug("session from cookie " + request.isRequestedSessionIdFromCookie()); 239 log.debug("session from url " + request.isRequestedSessionIdFromURL()); 240 log.debug("session valid " + request.isRequestedSessionIdValid()); 241 log.debug("session created at " + 242 new java.util.Date (httpSession.getCreationTime())); 243 log.debug("session httpsession id " + httpSession.getId()); 244 log.debug("session httpsession new " + httpSession.isNew()); 245 log.debug("session last accessed at " + 246 new java.util.Date (httpSession.getLastAccessedTime())); 247 log.debug("session max inactive interval " + 248 httpSession.getMaxInactiveInterval()); 249 log.debug("session contains wings session " + 250 (httpSession.getAttribute(lookupName) != null)); 251 } 252 253 sessionServlet.getSession().getExternalizeManager().setResponse(response); 254 255 290 291 if ((request.getCharacterEncoding() == null)) { try { 294 String sessionCharacterEncoding = sessionServlet.getSession().getCharacterEncoding(); 295 log.debug("Advising servlet container to interpret request as " + sessionCharacterEncoding); 297 request.setCharacterEncoding(sessionCharacterEncoding); 298 } catch (UnsupportedEncodingException e) { 299 log.warn("Problem on applying current session character encoding", e); 300 } 301 } 302 303 return sessionServlet; 304 } 305 } 306 307 308 309 312 protected boolean isSystemExternalizeRequest(HttpServletRequest request) { 313 String pathInfo = request.getPathInfo(); 314 return (pathInfo != null 315 && pathInfo.length() > 1 316 && pathInfo.startsWith("/-")); 317 } 318 319 320 protected AbstractExternalizeManager getExternalizeManager(HttpServletRequest req) 321 throws ServletException { 322 if (isSystemExternalizeRequest(req)) { 323 return SystemExternalizeManager.getSharedInstance(); 324 } else { 325 SessionServlet sessionServlet = getSessionServlet(req, null, false); 326 if (sessionServlet == null) 327 return null; 328 return sessionServlet.getSession().getExternalizeManager(); 329 } 330 } 331 332 public final void doGet(HttpServletRequest req, 333 HttpServletResponse response) 334 throws ServletException , IOException { 335 336 try { 337 352 String pathInfo = req.getPathInfo(); 353 354 if (pathInfo == null || pathInfo.length() == 0) { 355 StringBuffer pathUrl = req.getRequestURL(); 356 pathUrl.append('/'); 357 if (req.getQueryString() != null) { 358 pathUrl.append('?').append(req.getQueryString()); 359 } 360 361 log.debug("redirect to " + pathUrl.toString()); 362 response.sendRedirect(pathUrl.toString()); 363 return; 364 } 365 366 367 372 if (isSystemExternalizeRequest(req)) { 373 String identifier = pathInfo.substring(1); 374 AbstractExternalizeManager extManager = 375 SystemExternalizeManager.getSharedInstance(); 376 ExternalizedResource extInfo = extManager 377 .getExternalizedResource(identifier); 378 if (extInfo != null) { 379 extManager.deliver(extInfo, response, 380 createOutputDevice(req, response, 381 extInfo)); 382 } 383 return; 384 } 385 386 log.debug("session servlet"); 387 388 SessionServlet sessionServlet = getSessionServlet(req, response, true); 389 390 sessionServlet.doGet(req, response); 391 } catch (ServletException e) { 392 log.fatal("doGet", e); 393 throw e; 394 } catch (Throwable e) { 395 log.fatal("doGet", e); 396 throw new ServletException (e); 397 } 398 } 399 400 419 protected Device createOutputDevice(HttpServletRequest request, 420 HttpServletResponse response, 421 ExternalizedResource extInfo) 422 throws IOException { 423 return new ServletDevice(response.getOutputStream()); 424 } 425 } 426 427 428 | Popular Tags |