use of org.osgi.service.http.HttpContext in project felix by apache.
the class ResourceTest method testHandleResourceRegistrationOk.
@Test
public void testHandleResourceRegistrationOk() throws Exception {
CountDownLatch initLatch = new CountDownLatch(1);
CountDownLatch destroyLatch = new CountDownLatch(1);
HttpContext context = new HttpContext() {
@Override
public boolean handleSecurity(HttpServletRequest request, HttpServletResponse response) throws IOException {
return true;
}
@Override
public URL getResource(String name) {
try {
File f = new File("src/test/resources/" + name);
if (f.exists()) {
return f.toURI().toURL();
}
} catch (MalformedURLException e) {
fail();
}
return null;
}
@Override
public String getMimeType(String name) {
return null;
}
};
TestServlet servlet = new TestServlet(initLatch, destroyLatch);
register("/", "/resource", context);
register("/test", servlet, context);
URL testHtmlURL = createURL("/test.html");
URL testURL = createURL("/test");
assertTrue(initLatch.await(5, TimeUnit.SECONDS));
assertResponseCode(SC_OK, testHtmlURL);
assertResponseCode(SC_OK, testURL);
unregister("/test");
assertTrue(destroyLatch.await(5, TimeUnit.SECONDS));
assertResponseCode(SC_OK, testHtmlURL);
assertResponseCode(SC_OK, testURL);
unregister("/");
assertResponseCode(SC_NOT_FOUND, testHtmlURL);
assertResponseCode(SC_NOT_FOUND, testURL);
}
use of org.osgi.service.http.HttpContext in project felix by apache.
the class ServletContextManagerTest method testGetServletContext.
@Test
public void testGetServletContext() {
HttpContext httpCtx = Mockito.mock(HttpContext.class);
ServletContext result1 = this.manager.getServletContext(httpCtx);
ServletContext result2 = this.manager.getServletContext(httpCtx);
Assert.assertNotNull(result1);
Assert.assertNotNull(result2);
Assert.assertSame(result1, result2);
httpCtx = Mockito.mock(HttpContext.class);
result2 = this.manager.getServletContext(httpCtx);
Assert.assertNotNull(result2);
Assert.assertNotSame(result1, result2);
}
use of org.osgi.service.http.HttpContext in project felix by apache.
the class OsgiManager method bindHttpService.
protected synchronized void bindHttpService(HttpService httpService) {
// do not bind service, when we are already bound
if (this.httpService != null) {
log(LogService.LOG_DEBUG, "bindHttpService: Already bound to an HTTP Service, ignoring further services");
return;
}
Map config = getConfiguration();
// get authentication details
String realm = ConfigurationUtil.getProperty(config, PROP_REALM, DEFAULT_REALM);
String userId = ConfigurationUtil.getProperty(config, PROP_USER_NAME, DEFAULT_USER_NAME);
String password = ConfigurationUtil.getProperty(config, PROP_PASSWORD, DEFAULT_PASSWORD);
// register the servlet and resources
try {
HttpContext httpContext = new OsgiManagerHttpContext(httpService, securityProviderTracker, userId, password, realm);
Dictionary servletConfig = toStringConfig(config);
// register this servlet and take note of this
httpService.registerServlet(this.webManagerRoot, this, servletConfig, httpContext);
httpServletRegistered = true;
// register resources and take of this
httpService.registerResources(this.webManagerRoot + "/res", "/res", httpContext);
httpResourcesRegistered = true;
} catch (Exception e) {
log(LogService.LOG_ERROR, "bindHttpService: Problem setting up", e);
}
this.httpService = httpService;
}
use of org.osgi.service.http.HttpContext in project felix by apache.
the class HttpJettyTest method testHandleSecurityInFilterOk.
/**
* Test case for FELIX-3988, handling security constraints in {@link Filter}s.
*/
@Test
public void testHandleSecurityInFilterOk() throws Exception {
CountDownLatch initLatch = new CountDownLatch(2);
CountDownLatch destroyLatch = new CountDownLatch(2);
HttpContext context = new HttpContext() {
@Override
public String getMimeType(String name) {
return null;
}
@Override
public URL getResource(String name) {
return null;
}
@Override
public boolean handleSecurity(HttpServletRequest request, HttpServletResponse response) throws IOException {
if (request.getParameter("setStatus") != null) {
response.setStatus(SC_PAYMENT_REQUIRED);
} else if (request.getParameter("sendError") != null) {
response.sendError(SC_PAYMENT_REQUIRED);
} else if (request.getParameter("commit") != null) {
if (!response.isCommitted()) {
response.getWriter().append("Not allowed!");
response.flushBuffer();
}
}
return false;
}
};
TestFilter filter = new TestFilter(initLatch, destroyLatch);
TestServlet servlet = new TestServlet(initLatch, destroyLatch);
register("/foo", servlet, context);
register("/.*", filter, context);
URL url1 = createURL("/foo");
URL url2 = createURL("/foo?sendError=true");
URL url3 = createURL("/foo?setStatus=true");
URL url4 = createURL("/foo?commit=true");
assertTrue(initLatch.await(5, TimeUnit.SECONDS));
assertResponseCode(SC_FORBIDDEN, url1);
assertResponseCode(SC_PAYMENT_REQUIRED, url2);
assertResponseCode(SC_PAYMENT_REQUIRED, url3);
assertContent(SC_OK, "Not allowed!", url4);
unregister(filter);
unregister(servlet);
assertTrue(destroyLatch.await(5, TimeUnit.SECONDS));
assertResponseCode(SC_NOT_FOUND, url1);
}
use of org.osgi.service.http.HttpContext in project fabric8 by jboss-fuse.
the class GitHttpServerRegistrationHandler method registerServlet.
private void registerServlet(Path dataPath, String realm, String role) throws Exception {
synchronized (gitRemoteUrl) {
basePath = dataPath.resolve(Paths.get("git", "servlet"));
Path fabricRepoPath = basePath.resolve("fabric");
String servletBase = basePath.toFile().getAbsolutePath();
// Init and clone the local repo.
File fabricRoot = fabricRepoPath.toFile();
if (!fabricRoot.exists()) {
LOGGER.info("Cloning master root repo into {}", fabricRoot);
File localRepo = gitDataStore.get().getGit().getRepository().getDirectory();
git = Git.cloneRepository().setTimeout(10).setBare(true).setNoCheckout(true).setCloneAllBranches(true).setDirectory(fabricRoot).setURI(localRepo.toURI().toString()).call();
} else {
LOGGER.info("{} already exists", fabricRoot);
git = Git.open(fabricRoot);
}
HttpContext base = httpService.get().createDefaultHttpContext();
HttpContext secure = new GitSecureHttpContext(base, curator.get(), realm, role);
Dictionary<String, Object> initParams = new Hashtable<String, Object>();
initParams.put("base-path", servletBase);
initParams.put("repository-root", servletBase);
initParams.put("export-all", "true");
httpService.get().registerServlet("/git", new FabricGitServlet(git, curator.get()), initParams, secure);
registerGitHttpEndpoint();
}
}
Aggregations