2 Replies Latest reply on Mar 13, 2008 10:59 PM by pmuir

    Accessing HttpServletRequest

    alringer

      I am tying to integrate simpleCaptcha vs. jCaptcha (which looks horrible) and I need to access a key the capthca servlet puts in the user session.


      Being new to Seam I am not sure how to get at the HttpServletRequest.




        • 1. Re: Accessing HttpServletRequest
          junkie

          I had written this here, gives you what you need (getRequest()) and some more useful stuff:


          public class JSFUtil {    
            
            private static ResourceBundle getBundle() {
              FacesContext ctx = FacesContext.getCurrentInstance();
              UIViewRoot uiRoot = ctx.getViewRoot();
              Locale locale = uiRoot.getLocale();
              ClassLoader ldr = Thread.currentThread().getContextClassLoader();
              return ResourceBundle.getBundle(ctx.getApplication().getMessageBundle(), locale, ldr);
            }
            
            public static String getStringFromBundle(String key) {
              ResourceBundle bundle = getBundle();
              return bundle.getString(key);
            }
            
            public static void setViewRootLocale(Locale locale){
              FacesContext ctx = FacesContext.getCurrentInstance();
              UIViewRoot uiRoot = ctx.getViewRoot();
              uiRoot.setLocale(locale);
            }
            
            public static HttpServletRequest getRequest() {    
              return (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
            }
            
            public static String getRequestServerName(){    
              return getRequest().getServerName();
            } 
            
            public static String getRequestURI(){
              return getRequest().getRequestURI();
            }
            
            public static String getRequestQueryString(){
              return getRequest().getQueryString();
            }
            
            public static Locale getRequestLocale(){
              return getRequest().getLocale();
            }
            
            public static String getRequestUserAgent(){
              return getRequest().getHeader("User-Agent");
            }
            
            /** switches from HTTP to HTTPS or back
             *  if used to go from HTTPS to HTTP older browsers (like IE 6) could complain
             *  so use carefully
             */
            public static void switchHTTPSecurity() throws IOException{
              HttpServletRequest req = getRequest();
              StringBuffer sb = new StringBuffer();
              if (req.isSecure())
                sb.append("http://");
              else
                sb.append("https://");
              sb.append(req.getServerName()).append(req.getRequestURI());
              if (req.getQueryString() != null)
                sb.append("?").append(req.getQueryString());
              JSFUtil.sendRedirect(sb.toString());
            }
              
            public static void sendRedirect(String URL) throws IOException {      
              HttpServletResponse response = (HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
              FacesContext.getCurrentInstance().responseComplete();
              response.sendRedirect(URL);    
            }
            
            public static void setPageNotFound() throws IOException {  
              HttpServletResponse response = (HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
              response.setStatus(response.SC_NOT_FOUND);
            }
              
          }


          • 2. Re: Accessing HttpServletRequest
            pmuir

            Please don't post the same topic twice.