3 Replies Latest reply on May 10, 2006 4:27 AM by js8523

    HttpServletRequest from Seam

    Jason Long Master

      Is it possible to directly access the HttpServletRequest in a bean.

      For example I am now using a custom filter to add the follwing items to the request so that I can access them from Seam Components.

      This currently works fine, but I am wondering if I can do away with this filter.

      package pipetracker.web.servlet;
      
      import java.io.IOException;
      
      import javax.servlet.Filter;
      import javax.servlet.FilterChain;
      import javax.servlet.FilterConfig;
      import javax.servlet.ServletContext;
      import javax.servlet.ServletException;
      import javax.servlet.ServletRequest;
      import javax.servlet.ServletResponse;
      import javax.servlet.http.HttpServletRequest;
      
      import org.apache.commons.logging.Log;
      import org.apache.commons.logging.LogFactory;
      
      public class ExposeHttpServletRequestFilter implements Filter
      {
      
       private static final Log logger = LogFactory.getLog(ExposeHttpServletRequestFilter.class);
      
       @SuppressWarnings("unused")
       private ServletContext servletContext;
      
       public void destroy()
       {
       }
      
       public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
       {
      
       HttpServletRequest httpServletRequest = (HttpServletRequest) request;
      
       addRemoteUser(httpServletRequest);
       addBasePath(httpServletRequest);
      
       try
       {
       chain.doFilter(request, response);
       } catch (Exception e)
       {
       logger.error("ended request due to exception", e);
       throw new ServletException(e);
       } finally
       {
       logger.debug("ended request");
       }
       }
      
       public void init(FilterConfig config) throws ServletException
       {
       servletContext = config.getServletContext();
       }
      
       private void addRemoteUser(HttpServletRequest httpServletRequest)
       {
       logger.info(httpServletRequest.getRemoteUser());
       httpServletRequest.setAttribute("remoteUsr", httpServletRequest.getRemoteUser());
       }
      
       private void addBasePath(HttpServletRequest httpServletRequest)
       {
       StringBuffer basePath = new StringBuffer();
       basePath.append(httpServletRequest.getScheme() + "://").append(httpServletRequest.getServerName() + ":" + httpServletRequest.getServerPort()).append(httpServletRequest.getContextPath() + "/");
       httpServletRequest.setAttribute("basePath", basePath.toString());
       }
      
      }
      


        • 1. Re: HttpServletRequest from Seam
          eekboom Newbie

          FacesContext facesContext = FacesContext.getCurrentInstance();
          ExternalContext externalContext = facesContext.getExternalContext();
          HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();

          • 2. Re: HttpServletRequest from Seam
            Jason Long Master

            I did away with the filter. I am posting an example here in case anyone finds it useful. I mainly use this to set the href value is the base tag in xhtml.

            People have helped me on this so I am going to start posting general examples here.

            @Stateless
            @Name("exposeHttpServletRequest")
            @Scope(ScopeType.EVENT)
            @Interceptors(SeamInterceptor.class)
            public class ExposeHttpServletRequestBean implements Serializable, ExposeHttpServletRequest
            {
             private static final long serialVersionUID = 2675604092563757800L;
            
             @SuppressWarnings("unused")
             @Out(scope = ScopeType.EVENT, required = false)
             private String userJAAS;
            
             @SuppressWarnings("unused")
             @Out(scope = ScopeType.EVENT, required = false)
             private String basePath;
            
             @In
             private FacesContext facesContext;
            
             private HttpServletRequest request;
            
             @Create
             public void setup() {
             request = (HttpServletRequest) facesContext.getExternalContext().getRequest();
             }
            
             @Factory("userJAAS")
             public void loadUserJAAS()
             {
             userJAAS = request.getRemoteUser();
             }
            
             @Factory("basePath")
             public void loadBasePath()
             {
             basePath = request.getScheme() + "://" +
             request.getServerName() + ":" +
             request.getServerPort() + request.getContextPath() + "/";
             }
            


            
            @Local
            public interface ExposeHttpServletRequest {
             public void loadUserJAAS();
             public void loadBasePath();
             public void setup();
            }
            


            • 3. Re: HttpServletRequest from Seam
              js8523 Novice

              Hi

              Probably the best place to put examples is the wiki.

              Thanks,

              James