Search in sources :

Example 6 with HttpContext

use of org.osgi.service.http.HttpContext in project fabric8 by jboss-fuse.

the class ServletRegistrationHandler method activate.

@Activate
void activate(ComponentContext context, Map<String, ?> properties) {
    try {
        FabricDiscoveryServlet discoveryServlet = new FabricDiscoveryServlet();
        discoveryServlet.setCurator(curator.get());
        HttpContext base = httpService.get().createDefaultHttpContext();
        httpService.get().registerServlet("/mq-discovery", discoveryServlet, createParams("mq-discovery"), base);
    } catch (Throwable t) {
        LOGGER.warn("Failed to register fabric maven proxy servlets, due to:" + t.getMessage());
    }
    activateComponent();
}
Also used : HttpContext(org.osgi.service.http.HttpContext) Activate(org.apache.felix.scr.annotations.Activate)

Example 7 with HttpContext

use of org.osgi.service.http.HttpContext in project tesb-rt-se by Talend.

the class OsgiServletRegisterer method afterPropertiesSet.

@Override
public void afterPropertiesSet() throws Exception {
    HttpContext actualHttpContext = (httpContext == null) ? httpService.createDefaultHttpContext() : httpContext;
    final Dictionary<String, String> initParams = new Hashtable<String, String>();
    // The servlet will always have to match on uri prefix as some endpoints may do so
    initParams.put("matchOnUriPrefix", "true");
    try {
        String servletName = servlet.getServletName();
        initParams.put("servlet-name", servletName);
    } catch (Exception e) {
    // If getServletName is not implemented the default is to throw an exception
    // In this case we simply do not set a servlet name
    }
    httpService.registerServlet(alias, servlet, initParams, actualHttpContext);
    alreadyRegistered = true;
}
Also used : Hashtable(java.util.Hashtable) HttpContext(org.osgi.service.http.HttpContext)

Example 8 with HttpContext

use of org.osgi.service.http.HttpContext in project motech by motech.

the class HttpServiceTracker method register.

private void register(HttpService httpService) {
    if (contextPath == null && httpService != null) {
        DispatcherServlet dispatcherServlet = new OSGiDispatcherServlet(context, (ConfigurableWebApplicationContext) bundleContextWrapper.getBundleApplicationContext());
        contextPath = WebBundleUtil.getContextPath(context.getBundle());
        dispatcherServlet.setContextClass(MotechOSGiWebApplicationContext.class);
        dispatcherServlet.setContextConfigLocation(WebBundleUtil.getContextLocation(context.getBundle()));
        ClassLoader old = Thread.currentThread().getContextClassLoader();
        try {
            Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
            HttpContext httpContext = HttpContextFactory.getHttpContext(httpService.createDefaultHttpContext(), context.getBundle());
            httpService.unregister(contextPath);
            httpService.registerServlet(contextPath, dispatcherServlet, null, httpContext);
            if (resourceMapping != null) {
                for (String key : resourceMapping.keySet()) {
                    LOGGER.debug(String.format("Registering %s = %s for bundle %s ", key, resourceMapping.keySet(), bundleContextWrapper.getCurrentBundleSymbolicName()));
                    httpService.registerResources(key, resourceMapping.get(key), httpContext);
                }
            }
            LOGGER.info(String.format("servlet registered with context path %s for bundle %s", contextPath, OsgiStringUtils.nullSafeSymbolicName(context.getBundle())));
        } catch (ServletException e) {
            LOGGER.error("Unable to register dispatcher servlet for {}", bundleContextWrapper.getCurrentBundleSymbolicName(), e);
        } catch (NamespaceException e) {
            LOGGER.error("Unable to register dispatcher servlet for {}, namespace already taken", bundleContextWrapper.getCurrentBundleSymbolicName(), e);
        } finally {
            Thread.currentThread().setContextClassLoader(old);
        }
    }
}
Also used : ServletException(javax.servlet.ServletException) OSGiDispatcherServlet(org.motechproject.osgi.web.http.OSGiDispatcherServlet) OSGiDispatcherServlet(org.motechproject.osgi.web.http.OSGiDispatcherServlet) DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) HttpContext(org.osgi.service.http.HttpContext) NamespaceException(org.osgi.service.http.NamespaceException)

Example 9 with HttpContext

use of org.osgi.service.http.HttpContext in project motech by motech.

the class HttpContextFactoryTest method shouldCreateFileSystemAwareUiHttpContext.

@Test
public void shouldCreateFileSystemAwareUiHttpContext() {
    HttpContext httpContext = mock(HttpContext.class);
    PowerMockito.mockStatic(ApplicationEnvironment.class);
    MockBundle bundle = new MockBundle("org.motechproject.com-sms-api-bundle");
    when(ApplicationEnvironment.isInDevelopmentMode()).thenReturn(true);
    when(ApplicationEnvironment.getModulePath(new BundleName("org.motechproject.com-sms-api-bundle"))).thenReturn("/Users/s/project/motech/modules/sms/src/main/resources");
    FileSystemAwareUIHttpContext fileSystemAwareUIHttpContext = (FileSystemAwareUIHttpContext) HttpContextFactory.getHttpContext(httpContext, bundle);
    assertThat(fileSystemAwareUIHttpContext, IsNot.not(notNull()));
    assertThat(fileSystemAwareUIHttpContext.getResourceRootDirectoryPath(), Is.is("/Users/s/project/motech/modules/sms/src/main/resources"));
}
Also used : FileSystemAwareUIHttpContext(org.motechproject.osgi.web.http.FileSystemAwareUIHttpContext) HttpContext(org.osgi.service.http.HttpContext) FileSystemAwareUIHttpContext(org.motechproject.osgi.web.http.FileSystemAwareUIHttpContext) BundleName(org.motechproject.osgi.web.bundle.BundleName) MockBundle(org.eclipse.gemini.blueprint.mock.MockBundle) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 10 with HttpContext

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

the class HttpJettyTest method testUseServletContextOk.

@Test
public void testUseServletContextOk() throws Exception {
    CountDownLatch initLatch = new CountDownLatch(1);
    CountDownLatch destroyLatch = new CountDownLatch(1);
    HttpContext context = new HttpContext() {

        @Override
        public String getMimeType(String name) {
            return null;
        }

        @Override
        public URL getResource(String name) {
            try {
                File f = new File("src/test/resources/resource/" + name);
                if (f.exists()) {
                    return f.toURI().toURL();
                }
            } catch (MalformedURLException e) {
                fail();
            }
            return null;
        }

        @Override
        public boolean handleSecurity(HttpServletRequest request, HttpServletResponse response) throws IOException {
            return true;
        }
    };
    TestServlet servlet = new TestServlet(initLatch, destroyLatch) {

        private static final long serialVersionUID = 1L;

        @Override
        public void init(ServletConfig config) throws ServletException {
            ServletContext context = config.getServletContext();
            try {
                assertEquals("", context.getContextPath());
                assertNotNull(context.getResource("test.html"));
                assertNotNull(context.getRealPath("test.html"));
            } catch (MalformedURLException e) {
                fail();
            }
            super.init(config);
        }
    };
    register("/foo", servlet, context);
    URL testURL = createURL("/foo");
    assertTrue(initLatch.await(5, TimeUnit.SECONDS));
    assertResponseCode(SC_OK, testURL);
    unregister("/foo");
    assertTrue(destroyLatch.await(5, TimeUnit.SECONDS));
    assertResponseCode(SC_NOT_FOUND, testURL);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MalformedURLException(java.net.MalformedURLException) HttpContext(org.osgi.service.http.HttpContext) ServletConfig(javax.servlet.ServletConfig) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletContext(javax.servlet.ServletContext) CountDownLatch(java.util.concurrent.CountDownLatch) File(java.io.File) URL(java.net.URL) Test(org.junit.Test)

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