Search in sources :

Example 1 with ServletRegistrationException

use of org.motechproject.osgi.web.exception.ServletRegistrationException in project motech by motech.

the class Activator method serviceAdded.

/**
 * Initializes the security chain by fetching the proxy manager,
 * registers the security filter and spring dispatcher servlet.
 */
private void serviceAdded(ExtHttpService service) {
    DispatcherServlet dispatcherServlet = new DispatcherServlet();
    dispatcherServlet.setContextConfigLocation(CONTEXT_CONFIG_LOCATION);
    dispatcherServlet.setContextClass(WebSecurityApplicationContext.class);
    ClassLoader old = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
        HttpContext httpContext = HttpContextFactory.getHttpContext(service.createDefaultHttpContext(), bundleContext.getBundle());
        service.registerServlet(SERVLET_URL_MAPPING, dispatcherServlet, null, null);
        service.registerResources(RESOURCE_URL_MAPPING, "/webapp", httpContext);
        LOGGER.debug("Servlet registered");
        filter = new MotechDelegatingFilterProxy("springSecurityFilterChain", dispatcherServlet.getWebApplicationContext());
        MotechProxyManager proxyManager = dispatcherServlet.getWebApplicationContext().getBean(MotechProxyManager.class);
        LOGGER.debug("Creating initial proxy chain");
        proxyManager.initializeProxyChain();
        service.registerFilter(filter, "/.*", null, 0, httpContext);
        LOGGER.debug("Filter registered");
    } catch (NamespaceException e) {
        throw new ServletRegistrationException("Web-security servlet already registered", e);
    } catch (ServletException e) {
        throw new ServletRegistrationException("Unable to register servlet for web-security", e);
    } finally {
        Thread.currentThread().setContextClassLoader(old);
    }
}
Also used : ServletException(javax.servlet.ServletException) ServletRegistrationException(org.motechproject.osgi.web.exception.ServletRegistrationException) DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) MotechDelegatingFilterProxy(org.motechproject.security.filter.MotechDelegatingFilterProxy) HttpContext(org.osgi.service.http.HttpContext) NamespaceException(org.osgi.service.http.NamespaceException) MotechProxyManager(org.motechproject.security.service.MotechProxyManager)

Aggregations

ServletException (javax.servlet.ServletException)1 ServletRegistrationException (org.motechproject.osgi.web.exception.ServletRegistrationException)1 MotechDelegatingFilterProxy (org.motechproject.security.filter.MotechDelegatingFilterProxy)1 MotechProxyManager (org.motechproject.security.service.MotechProxyManager)1 HttpContext (org.osgi.service.http.HttpContext)1 NamespaceException (org.osgi.service.http.NamespaceException)1 DispatcherServlet (org.springframework.web.servlet.DispatcherServlet)1