3 Replies Latest reply on May 28, 2009 7:36 AM by danielwasser

    How to access HttpSession Attributes in a portlet.

    dpaterson

      I have a simple servlet that captures the login credentials and places them in the Web apps HttpSession then forwards the request to "j_security_check".

      protected void doPost(HttpServletRequest req, HttpServletResponse resp)
       throws ServletException, IOException {
       String username = req.getParameter("username");
       String password = req.getParameter("password");
       if (username != null && password != null) {
       req.getSession().setAttribute("api.username", username);
       req.getSession().setAttribute("api.password", password);
       String url = "j_security_check?j_username=" + username + "&j_password=" + password;
       String redirectUrl = resp.encodeRedirectURL(url);
       resp.sendRedirect(redirectUrl);
       }
      
       }
      


      This piece is working fine but the problem is I cannot find a way to access the HttpSession attributes from a portlet. I've tried the following api call to pull these attributes out of the session but I'm only getting nulls .

      public void createContext(RenderRequest request, RenderResponse response, PortletContext pContext) throws Exception{
       PortalRuntimeContext context = Navigation.getPortalRuntimeContext();
       PortalSession ps = context.getSession();
       username = (String)ps.getAttribute(USERNAME_ATTR);
       password = (String)ps.getAttribute(PASSWORD_ATTR);
       }
      


      From what I understand from reading the Portal Wiki is that there are three httpsessions: The portlet session, the portal session and the web app session. My servlet is defined in the portal-server.war/WEB-INF/web.xml:

      <!-- Save the login -->
       <servlet>
       <servlet-name>SaveLogin</servlet-name>
       <servlet-class>....SaveLoginServlet</servlet-class>
       </servlet>
      
       <servlet-mapping>
       <servlet-name>SaveLogin</servlet-name>
       <url-pattern>/auth/portal/default/saveLogin</url-pattern>
       </servlet-mapping>
      


      I would have thought this meant my PortalSession object would be wrapping the HttpSession my servlet is populating the attributes in but this does not seem to be the case.

      I've spent hours looking into the API and Wiki for a way to hook into the original HttpSession via a portlet but I keep hitting a wall. Should be a simple task but the solution has thusfar eluded me.

      <grovel>PLEASE HELP</grovel>


      I am using the jboss-portal-2.6.3.GA all-in-one binary distribution.

        • 1. Re: How to access HttpSession Attributes in a portlet.
          danwardamentracom

          Did you ever get a solution for this? If so, can you please post it?

          • 2. Re: How to access HttpSession Attributes in a portlet.

            Not sure if the original author achieved success, but one way to handle this would be to access the servlet's session from the portlet through the PortletSession scope.

            JSR168 and 286 allow sharing of a web-application's session with a portlet if they're pacakged together by accessing the "application scope" portlet session:

            PortletSession.APPLICATION_SCOPE


            Good luck,
            Andy

            • 3. Re: How to access HttpSession Attributes in a portlet.

              You can try this:


              String sessId = null;
              HttpServletRequest httpServletRequest;
              try {
              httpServletRequest = getHttpServletRequest();
              HttpSession sess = httpServletRequest.getSession();

              } catch (Throwable e) {
              ....
              }


              /**
              * Obtains the HttpServletRequest of the user
              *
              * @return
              * @throws PolicyContextException
              */
              protected HttpServletRequest getHttpServletRequest() throws PolicyContextException {
              HttpServletRequest request = (HttpServletRequest) PolicyContext
              .getContext("javax.servlet.http.HttpServletRequest");
              return request;
              }