Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Equinox » ExtendedHttpService.registerFilter - filter's init method called, doFilter is never called
ExtendedHttpService.registerFilter - filter's init method called, doFilter is never called [message #638670] Fri, 12 November 2010 04:47 Go to next message
Misha Koshelev is currently offline Misha KoshelevFriend
Messages: 31
Registered: May 2010
Member
Dear All:

I am trying to use filter support as documented here:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=128068
http://help.eclipse.org/helios/index.jsp?topic=/org.eclipse. platform.doc.isv/reference/api/org/eclipse/equinox/http/serv let/ExtendedHttpService.html

working on this ticket for OpenMRS, an enterprise medical record system:
http://tickets.openmrs.org/browse/TRUNK-1596

I have made a skeletal servletbridge implementation to track down a bug I was having, and have discovered that my simple filter's doFilter method is _never_ called Sad (init _is_ called though).

Am I missing something? Please find relevant code snippets below.

I am using ServletBridge pre-built WAR http://www.eclipse.org/equinox/server/downloads/bridge.war updated to reflect bundles here:
http://download.eclipse.org/equinox/drops/S-3.7M3-2010102814 41/index.php

Here is my ss:

osgi> ss

Framework is launched.

id State Bundle
0 ACTIVE org.eclipse.osgi_3.7.0.v20101022
Fragments=14
1 ACTIVE org.eclipse.equinox.common_3.6.0.v20100503
2 ACTIVE org.eclipse.update.configurator_3.3.100.v20100512
3 ACTIVE org.eclipse.equinox.http.servletbridge_1.0.200.v20100503
4 ACTIVE org.eclipse.equinox.http.registry_1.1.0.v20100503
6 ACTIVE examples.test.bundle_1.0.0.SNAPSHOT
7 ACTIVE org.eclipse.equinox.registry_3.5.100.v20101018
11 RESOLVED org.eclipse.osgi.services_3.3.0.v20101018
12 ACTIVE org.eclipse.equinox.http.servlet_1.1.0.v20100503
14 RESOLVED org.eclipse.equinox.servletbridge.extensionbundle_1.0.0
Master=0

My activator:
package org.example.pkg.internal;

import java.util.Dictionary;
import java.util.Properties;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.http.HttpContext;
import org.osgi.util.tracker.ServiceTracker;

import org.eclipse.equinox.http.servlet.ExtendedHttpService;
import org.example.pkg.ExampleService;

/**
 * Extension of the default OSGi bundle activator
 */
public final class ExampleActivator
    implements BundleActivator
{
    private ServiceTracker tracker;
    private ExampleFilter filter = new ExampleFilter();
    private ExampleServlet servlet = new ExampleServlet();

    /**
     * Called whenever the OSGi framework starts our bundle
     */
    public void start( BundleContext bc )
        throws Exception
    {
        System.out.println( "STARTING org.example.pkg" );

        Dictionary props = new Properties();
        // add specific service properties here...

        System.out.println( "REGISTER org.example.pkg.ExampleService" );

        // Register our example service implementation in the OSGi service registry
        bc.registerService( ExampleService.class.getName(), new ExampleServiceImpl(), props );

        tracker = new ServiceTracker(bc, ExtendedHttpService.class.getName(), null)
        {
            public Object addingService(ServiceReference ref)
            {
                Object service =  super.addingService(ref);
                serviceAdded((ExtendedHttpService)service);
                return service;
            }

            public void removedService(ServiceReference ref, Object service)
            {
                serviceRemoved((ExtendedHttpService)service);
                super.removedService(ref, service);
            }
        };

        tracker.open();
    }

    /**
     * Called whenever the OSGi framework stops our bundle
     */
    public void stop( BundleContext bc )
        throws Exception
    {
        System.out.println( "STOPPING org.example.pkg" );

        // no need to unregister our service - the OSGi framework handles it for us
    }

    
    private void serviceAdded(ExtendedHttpService service)
    {
        try {
	    HttpContext defaultContext = service.createDefaultHttpContext();
            service.registerFilter("/*", filter, null, defaultContext);
	    service.registerServlet("/", servlet, null, defaultContext);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void serviceRemoved(ExtendedHttpService service)
    {
	service.unregister("/");
	service.unregisterFilter(filter);
     }
}


My sample filter:

package org.example.pkg.internal;

import javax.servlet.*;

public class ExampleFilter implements Filter {
    
    public void init(FilterConfig filterConfig) {
	System.out.println("ExampleFilter.init");
    }
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)  { 
	System.out.println("ExampleFilter.doFilter");
    }
    public void destroy() {
	System.out.println("ExampleFilter.destroy");
    }
}


Note that my sample servlet _is_ called correctly. I get this output when installing the servlet:

osgi> start 6
STARTING org.example.pkg
REGISTER org.example.pkg.ExampleService
ExampleFilter.init

and then this when I request pages (my sample servlet is simply a Hello World servlet, but also notes when it has been called)

ExampleServlet.doGet

Note, however, the absence of calls to doFilter!

Help much appreciated.

Thank you!

Yours
Misha

p.s. Relevant sample servlet:


package org.example.pkg.internal;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ExampleServlet extends HttpServlet {
    public void doGet(HttpServletRequest request,
		      HttpServletResponse response)
	throws ServletException, IOException {
	System.out.println("ExampleServlet.doGet");
	response.setContentType("text/html");
	PrintWriter out = response.getWriter();
	out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
		    "Transitional//EN\">\n" +
		    "<HTML>\n" +
		    "<HEAD><TITLE>Hello WWW</TITLE></HEAD>\n" +
		    "<BODY>\n" +
		    "<H1>Hello WWW</H1>\n" +
		    "</BODY></HTML>");
    }
}


Thank you!
Re: ExtendedHttpService.registerFilter - filter's init method called, doFilter is never called [message #638720 is a reply to message #638670] Fri, 12 November 2010 10:42 Go to previous messageGo to next message
Gunnar Wagenknecht is currently offline Gunnar WagenknechtFriend
Messages: 486
Registered: July 2009
Location: San Francisco ✈ Germany
Senior Member

Am 12.11.2010 05:48, schrieb Misha Koshelev:
> service.registerFilter("/*", filter, null, defaultContext);
> service.registerServlet("/", servlet, null, defaultContext);

You shouldn't use "/*" as the alias is always a prefix matching and '*'
has no special meaning.

-Gunnar

--
Gunnar Wagenknecht
gunnar@wagenknecht.org
http://wagenknecht.org/
Re: ExtendedHttpService.registerFilter - filter's init method called, doFilter is never called [message #638758 is a reply to message #638720] Fri, 12 November 2010 13:42 Go to previous messageGo to next message
Misha Koshelev is currently offline Misha KoshelevFriend
Messages: 31
Registered: May 2010
Member
Ah I see thank you!

So if I want to convert a filter such as:

<filter>
        <filter-name>StartupErrorFilter</filter-name>
        <filter-class>org.openmrs.web.filter.startuperror.StartupErrorFilter</filter-class>
</filter>
<filter-mapping>
	<filter-name>StartupErrorFilter</filter-name> 
	<url-pattern>/*</url-pattern> 
</filter-mapping>


with a filter-mapping that has a url-pattern of /*, do I understand that I simply set my alias as an empty string, i.e.:

service.registerFilter("", filter, null, defaultContext);


???

Thank you so much for your help!

Yours
Misha
Re: ExtendedHttpService.registerFilter - filter's init method called, doFilter is never called [message #638865 is a reply to message #638758] Fri, 12 November 2010 19:53 Go to previous messageGo to next message
Misha Koshelev is currently offline Misha KoshelevFriend
Messages: 31
Registered: May 2010
Member
Update: I have tried
            service.registerFilter("", filter, null, defaultContext);

and gotten the following error:
osgi> start 6
STARTING org.example.pkg
REGISTER org.example.pkg.ExampleService
java.lang.IllegalArgumentException: Invalid alias ''
	at org.eclipse.equinox.http.servlet.internal.ProxyServlet.checkAlias(ProxyServlet.java:211)
	at org.eclipse.equinox.http.servlet.internal.ProxyServlet.registerFilter(ProxyServlet.java:227)
	at org.eclipse.equinox.http.servlet.internal.HttpServiceImpl.registerFilter(HttpServiceImpl.java:107)

The relevant function in ProxyServlet based on HEAD checked out prior to 201011091423 is:
	private void checkAlias(String alias) {
		if (alias == null)
			throw new IllegalArgumentException("Alias cannot be null"); //$NON-NLS-1$

		if (!alias.startsWith("/") || (alias.endsWith("/") && !alias.equals("/"))) //$NON-NLS-1$ //$NON-NLS-2$//$NON-NLS-3$
			throw new IllegalArgumentException("Invalid alias '" + alias + "'"); //$NON-NLS-1$//$NON-NLS-2$
	}

Thus, clearly the alias must start with a leading slash.

I next tried changing my example Activator to:
            service.registerFilter("/", filter, null, defaultContext);

and my filter to:
public class ExampleFilter implements Filter {
    
    public void init(FilterConfig filterConfig) {
	System.out.println("ExampleFilter.init");
    }
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)  { 
	System.out.println("ExampleFilter.doFilter");
	try {
	    chain.doFilter(request, response);
	} catch (java.io.IOException ioe) {
	    ioe.printStackTrace();
	} catch (javax.servlet.ServletException se) {
	    se.printStackTrace();
	}
    }
    public void destroy() {
	System.out.println("ExampleFilter.destroy");
    }
}


It is now successfully getting called!
osgi> start 6
STARTING org.example.pkg
REGISTER org.example.pkg.ExampleService
ExampleFilter.init

osgi> ExampleFilter.doFilter

Furthermore, the servlet is also being called because of chain.doFilter!

Thank you
Yours
Misha
Re: ExtendedHttpService.registerFilter - filter's init method called, doFilter is never called [message #638916 is a reply to message #638865] Sat, 13 November 2010 07:31 Go to previous message
Gunnar Wagenknecht is currently offline Gunnar WagenknechtFriend
Messages: 486
Registered: July 2009
Location: San Francisco ✈ Germany
Senior Member

Am 12.11.2010 20:53, schrieb Misha Koshelev:
> Thus, clearly the alias must start with a leading slash.

Correct. As the spec says, the alias must start with a leading slash but
must not end with a trailing slash except for the root alias (which is
just a single slash).

-Gunnar

--
Gunnar Wagenknecht
gunnar@wagenknecht.org
http://wagenknecht.org/
Previous Topic:multiple aliases for servlet - OSGi
Next Topic:How to use OSGI to prevent some bundle to load according to licence
Goto Forum:
  


Current Time: Fri Apr 19 08:14:53 GMT 2024

Powered by FUDForum. Page generated in 0.01777 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top