Sessions Sessions Everywhere

seagizmo <[email protected]> Wed, 13 Jul 2005 03:20:04 CDT
Newsgroups gmane.comp.web.sitemesh.general,gmane.comp.lang.pnuts.user
Message-ID <18365693.1121243028608.JavaMail.os-j2ee@opensymphony01.contegix.com>
Hey,

As I said already I wrote a filter that also wrapped the HttpServletRequest


RequestWrapper  wrappedRequest  = new RequestWrapper((HttpServletRequest)request);
ResponseWrapper wrappedResponse = new ResponseWrapper((HttpServletResponse)response);
       
chain.doFilter(wrappedRequest, wrappedResponse);
       
   
   /**
     *  This request wrapper class extends the support class HttpServletRequestWrapper,
     *  which implements all the methods in the HttpServletRequest interface, as
     *  delegations to the wrapped request.
     *  You only need to override the methods that you need to change.
     *  You can get access to the wrapped request using the method getRequest()
     */
    class RequestWrapper extends HttpServletRequestWrapper {
        
        /**
         * Create a RequestWrapper for the HttpServletRequest
         * @param request the HttpServletRequest to be wrapped
         */
        public RequestWrapper(HttpServletRequest request) {
            super(request);
        }
        
        /**
         * Create an explicit session
         * @return returns a HttpSession
         */
        public HttpSession getSession() {
            HttpSession session = ((HttpServletRequest)getRequest()).getSession();
            return session;
        }
        
        /**
         * Create a session non-explicit
         * @param create if create is true a session is created even if one already existes, if create is
         * false a session is only created if a sessions does not already exist
         * @return returns a HttpSession
         */
        public HttpSession getSession(boolean create) {
            
            if(create) {
                if(log.isDebugEnabled()) {
                    log.debug("getSession(true) "+((HttpServletRequest)getRequest()).getRequestURI());
                }
                // Check to see if the callerclasses contains opensymphony stuff
                for(int i = 2 ; sun.reflect.Reflection.getCallerClass(i) != null ; i++) {                   
                    if(sun.reflect.Reflection.getCallerClass(i).getName().equals("com.opensymphony.module.sitemesh.filter.PageFilter")) {
                        if(log.isDebugEnabled()) {
                            log.debug("Disabling session creation by "+sun.reflect.Reflection.getCallerClass(i).getName());
                        }   
                        create = false;
                        break;
                    }
                }
            }
            
            HttpSession session = ((HttpServletRequest)getRequest()).getSession(create);
            
            return session;
        }
}

---------------------------------------------------------------------
Posted via Jive Forums
http://forums.opensymphony.com/thread.jspa?threadID=1837&messageID=10481#10481


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]