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();
}
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;
}
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);
}
}
}
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"));
}
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);
}
Aggregations