Search in sources :

Example 11 with HttpContext

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);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MalformedURLException(java.net.MalformedURLException) HttpContext(org.osgi.service.http.HttpContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) CountDownLatch(java.util.concurrent.CountDownLatch) File(java.io.File) URL(java.net.URL) Test(org.junit.Test)

Example 12 with HttpContext

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);
}
Also used : HttpContext(org.osgi.service.http.HttpContext) ServletContext(javax.servlet.ServletContext) Test(org.junit.Test)

Example 13 with HttpContext

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;
}
Also used : Dictionary(java.util.Dictionary) HttpContext(org.osgi.service.http.HttpContext) Map(java.util.Map) HashMap(java.util.HashMap) ServletException(javax.servlet.ServletException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) PrivilegedActionException(java.security.PrivilegedActionException) IOException(java.io.IOException)

Example 14 with HttpContext

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);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpContext(org.osgi.service.http.HttpContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) CountDownLatch(java.util.concurrent.CountDownLatch) URL(java.net.URL) Test(org.junit.Test)

Example 15 with HttpContext

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();
    }
}
Also used : ZkPath(io.fabric8.zookeeper.ZkPath) Path(java.nio.file.Path) Hashtable(java.util.Hashtable) HttpContext(org.osgi.service.http.HttpContext) File(java.io.File)

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