Search in sources :

Example 16 with HttpContext

use of org.osgi.service.http.HttpContext 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)

Example 17 with HttpContext

use of org.osgi.service.http.HttpContext in project smarthome by eclipse.

the class HttpContextFactoryServiceImplTest method shouldCreateHttpContext.

@Test
public void shouldCreateHttpContext() {
    HttpContext context = httpContextFactoryService.createDefaultHttpContext(bundle);
    assertThat(context, is(notNullValue()));
}
Also used : HttpContext(org.osgi.service.http.HttpContext) Test(org.junit.Test)

Example 18 with HttpContext

use of org.osgi.service.http.HttpContext in project cxf by apache.

the class ServletExporter method updated.

@SuppressWarnings("rawtypes")
@Override
public void updated(Dictionary properties) throws ConfigurationException {
    if (alias != null) {
        try {
            LOG.log(Level.INFO, "Unregistering previous instance of \"" + alias + "\" servlet");
            httpService.unregister(alias);
        } catch (IllegalArgumentException e) {
            // NOTE: pax-web specific...
            if (e.getMessage() != null && e.getMessage().contains("was never registered")) {
                LOG.log(Level.INFO, "CXF OSGi servlet was not unregistered: " + e.getMessage());
            } else {
                LOG.log(Level.SEVERE, e.getMessage(), e);
            }
        }
        if (properties == null) {
            // otherwise, we'll try to register the servlet
            return;
        }
        alias = null;
    }
    if (properties == null) {
        properties = new Properties();
    }
    Properties sprops = new Properties();
    sprops.put("init-prefix", getProp(properties, CXF_SERVLET_PREFIX + "init-prefix", ""));
    sprops.put("servlet-name", getProp(properties, CXF_SERVLET_PREFIX + "name", "cxf-osgi-transport-servlet"));
    sprops.put("hide-service-list-page", getProp(properties, CXF_SERVLET_PREFIX + "hide-service-list-page", "false"));
    sprops.put("disable-address-updates", getProp(properties, CXF_SERVLET_PREFIX + "disable-address-updates", "true"));
    sprops.put("base-address", getProp(properties, CXF_SERVLET_PREFIX + "base-address", ""));
    sprops.put("service-list-path", getProp(properties, CXF_SERVLET_PREFIX + "service-list-path", ""));
    sprops.put("static-resources-list", getProp(properties, CXF_SERVLET_PREFIX + "static-resources-list", ""));
    sprops.put("redirects-list", getProp(properties, CXF_SERVLET_PREFIX + "redirects-list", ""));
    sprops.put("redirect-servlet-name", getProp(properties, CXF_SERVLET_PREFIX + "redirect-servlet-name", ""));
    sprops.put("redirect-servlet-path", getProp(properties, CXF_SERVLET_PREFIX + "redirect-servlet-path", ""));
    sprops.put("service-list-all-contexts", getProp(properties, CXF_SERVLET_PREFIX + "service-list-all-contexts", ""));
    sprops.put("service-list-page-authenticate", getProp(properties, CXF_SERVLET_PREFIX + "service-list-page-authenticate", "false"));
    sprops.put("service-list-page-authenticate-realm", getProp(properties, CXF_SERVLET_PREFIX + "service-list-page-authenticate-realm", "karaf"));
    sprops.put("use-x-forwarded-headers", getProp(properties, CXF_SERVLET_PREFIX + "use-x-forwarded-headers", "false"));
    sprops.put("async-supported", getProp(properties, CXF_SERVLET_PREFIX + "async-supported", "true"));
    // Accept extra properties by default, can be disabled if it is really needed
    if (Boolean.valueOf(getProp(properties, CXF_SERVLET_PREFIX + "support.extra.properties", "true").toString())) {
        Enumeration keys = properties.keys();
        while (keys.hasMoreElements()) {
            String nextKey = keys.nextElement().toString();
            if (!nextKey.startsWith(CXF_SERVLET_PREFIX)) {
                sprops.put(nextKey, properties.get(nextKey));
            }
        }
    }
    if (serviceRegistration != null) {
        serviceRegistration.unregister();
    }
    alias = (String) getProp(properties, CXF_SERVLET_PREFIX + "context", "/cxf");
    HttpContext context = httpService.createDefaultHttpContext();
    try {
        LOG.log(Level.INFO, "Registering new instance of \"" + alias + "\" servlet");
        httpService.registerServlet(alias, servlet, sprops, context);
    } catch (Exception e) {
        LOG.log(Level.WARNING, "Error registering CXF OSGi servlet " + e.getMessage(), e);
    }
}
Also used : Enumeration(java.util.Enumeration) HttpContext(org.osgi.service.http.HttpContext) Properties(java.util.Properties) ConfigurationException(org.osgi.service.cm.ConfigurationException)

Aggregations

HttpContext (org.osgi.service.http.HttpContext)18 Test (org.junit.Test)9 File (java.io.File)4 URL (java.net.URL)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 ServletException (javax.servlet.ServletException)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 MalformedURLException (java.net.MalformedURLException)3 Hashtable (java.util.Hashtable)3 IOException (java.io.IOException)2 ServletContext (javax.servlet.ServletContext)2 Activate (org.apache.felix.scr.annotations.Activate)2 NamespaceException (org.osgi.service.http.NamespaceException)2 DispatcherServlet (org.springframework.web.servlet.DispatcherServlet)2 ZkPath (io.fabric8.zookeeper.ZkPath)1 ConnectException (java.net.ConnectException)1 Path (java.nio.file.Path)1 PrivilegedActionException (java.security.PrivilegedActionException)1 Dictionary (java.util.Dictionary)1