Questions about working WebApplicationHandler and Filters..

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Questions about working WebApplicationHandler and Filters..

igo4ward@netzero.com
Hello everyone,
 
I am writing in reference to a post at GMANE written (By J Bartel) back in november of 2004, linked below:
 
http://article.gmane.org/gmane.comp.java.jetty.support/6245
 
I have been using Jetty for quite a few years now embedded as the servlet server of a distributed application framework I've designed. Recently I decided to add support for Filters, however the test code I've found for implementing Filters with Jetty (I am using version 5.2.24 currently) doesn't seem to work on my system. I am wondering if I am missing some obscure initialization call. Transcribed below (in green with WebApp lines in red) is a copy of the code in my threaded HttpServer class:
 
 
.
.
//Initialize the server object and iterate through a list of desired listener ip/port combinations and add them to the server.
.
.
 
  String home = System.getProperty("server.home",cono.getHomePath());
             String subdirz = cono.getSubDir();
             String resourcepath = new File(home + File.separator + subdirz).getPath();
             //make the resourcepath if it doesn't yet exist.
             Utilities.fileMakeDirs(resourcepath);
           
             System.out.println("just before defining authorization filter code block, httpS context.");
              WebApplicationContext context = new WebApplicationContext();
                 context.setIgnoreWebJetty(true);
                 context.setContextPath(cono.getVirtualPath());
                 context.setResourceBase(resourcepath);
                 server.addContext(null, context);
                 FilterHolder holder = context.getWebApplicationHandler().defineFilter ("authfilter",AuthorizationFilter.class.getName());
                 context.getWebApplicationHandler().addFilterPathMapping(cono.getVirtualPath(),"authfilter", Dispatcher.__REQUEST);
                 context.getWebApplicationHandler().addFilterPathMapping("*.jsp:*.jsP:*.jSp:*.jSP:*.Jsp:*.JsP:*.JSp:*.JSP", "authfilter", Dispatcher.__REQUEST);
                 context.addHandler(context.getWebApplicationHandler());

            /* old code, that also compiled but didn't call the filter!
             //Instantiate an authorizationFilter
             AuthorizationFilter afil = new AuthorizationFilter();
             //Instantiate a new webapplicationhandler
             WebApplicationHandler whandler = new WebApplicationHandler(); //context.getWebApplicationHandler();
             //Instantiate a filter holder
             FilterHolder fholder = whandler.defineFilter("authfilter",AuthorizationFilter.class.getName());
             //Define application scope of holder.
             //  fholder.
             //add the filterholder to the webapp handler after defining it for the authorization filter.
             whandler.addFilterHolder(fholder);
             //Map context path to the filter
             whandler.addFilterPathMapping(cono.getVirtualPath(),"authfilter", Dispatcher.__REQUEST);
             //add the webapphandler to the existing server context
             context.addHandler(whandler);
               */
              System.out.println("just after adding authorization filter handler to context, http context.");
           
                 
             //conditionally add a servlet handler to the context
              if(cono.getUseServlets() > 0) {
                 //  System.out.println("about to add jsp handlers..");
            // Create a servlet container to handle jsp's    
               ServletHandler shandler = new ServletHandler();              
            // Map a servlet onto the container
           
            shandler.addServlet ("Dump","/Dump", "org.mortbay.servlet.Dump");
            shandler.addServlet ("JSP", "*.jsp", "org.apache.jasper.servlet.JspServlet");
            shandler.addServlet ("JSP", "*.jSp", "org.apache.jasper.servlet.JspServlet");
            shandler.addServlet ("JSP", "*.jSP", "org.apache.jasper.servlet.JspServlet");
            shandler.addServlet ("JSP", "*.JSP", "org.apache.jasper.servlet.JspServlet");
            shandler.addServlet ("JSP", "*.Jsp", "org.apache.jasper.servlet.JspServlet");
            shandler.addServlet ("JSP", "*.JsP", "org.apache.jasper.servlet.JspServlet");
            shandler.addServlet ("JSP", "*.JSp", "org.apache.jasper.servlet.JspServlet");
            shandler.addServlet ("JSP", "*.jsP", "org.apache.jasper.servlet.JspServlet");
     
            context.addHandler(shandler);
                    }
            //Set the resource base.
            context.setResourceBase(resourcepath);

            //Create a default resource handler and set it's directory browsing attrib then add it to the context first!
             ResourceHandler handler = new ResourceHandler();
             boolean allowd = false;
             if(cono.getBrowsable() == 1) allowd = true;
             handler.setDirAllowed(allowd);
             //Add handler as next object to context.
//doesn't work in jetty 4.2.8              context.addHttpHandler(handler);
             context.addHandler(handler);
 
the code is what I was able to cobble together using the post as a guide. I first recieved an error during run time related to the java_home path that I found out had to do with tools.jar not being on the class path, after fixing this the run time error went away but the Filter failed to be called. Below  is the code in my test Filter class AuthorizationFilter.
 
public class AuthorizationFilter implements Filter {
    private FilterConfig filterConfig = null;
   
    /** Creates a new instance of AuthorizationFilter */
    public AuthorizationFilter() {
    }
   
    public void doFilter(ServletRequest request,ServletResponse response, FilterChain chain)  throws ServletException, IOException {
     System.out.println("inside doFilter..of authorizationFilter");
    //cast the servlet request to a standard http request and grab the associated session.
      HttpSession sess = ((HttpServletRequest)request).getSession(false);
      if(sess == null) {
          System.out.println("The intercepted request has a NULL session, deny access.");
      } else {
          System.out.println("The intercepted request has a NULL session.");
      }
      chain.doFilter(request, response);
    }
   
   public void init(FilterConfig filterConfig) throws ServletException {
      this.filterConfig = filterConfig;
   }
   public void destroy() {
      this.filterConfig = null;
   }
   
}
 
As you can see it is pretty straight forward, yet when I make any page requests, the trace lines(conditional statement in red above) don't report to the command line.
 
Am I missing a critical initialization call?
 
You'll note from the first code block that I add 3 handlers to the WebApplicationContext, a WebApplicationHandler (which I am not sure of the usage of) , a ServletHandler (which is loaded with various jsp servlets) and the ResourceHandler that was made to handle static content. I understand from what I've been reading that I may be able to do away with the ServletHandler and the ResourceHandler but is it a requirement to get the WebAppHandler to work? My system needs the ability to provide servlets and static or only static resources, it currently works fine using the ResourceHandler. I am hoping I won't have to get rid of it to make the WebAppHandler call the filter.
 
I've tried various things (for example the call to "setIgnoreWebJetty" I included since I am not using the web.xml file) to no avail, any assistance that can provide would be greatly appreciated.
 
 
Regards,
 
David Saintloth



______________________________________________________________________
Revolutionize the way you surf the Web!
Try NetZero HiSpeed 3G.
Visit http://www.netzero.com to sign up today!



-------------------------------------------------------
This SF.Net email is sponsored by:
Power Architecture Resource Center: Free content, downloads, discussions,
and more. http://solutions.newsforge.com/ibmarch.tmpl
_______________________________________________
jetty-discuss mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jetty-discuss