Java Code Examples for io.undertow.server.handlers.PathHandler#addPrefixPath()

The following examples show how to use io.undertow.server.handlers.PathHandler#addPrefixPath() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ServletAndResourceWelcomeFileTestCase.java    From quarkus-http with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setup() throws ServletException {

    final PathHandler root = new PathHandler();
    final ServletContainer container = ServletContainer.Factory.newInstance();

    DeploymentInfo builder = new DeploymentInfo()
            .setClassIntrospecter(TestClassIntrospector.INSTANCE)
            .setClassLoader(ServletPathMappingTestCase.class.getClassLoader())
            .setContextPath("/servletContext")
            .setDeploymentName("servletContext.war")
            .setResourceManager(new TestResourceLoader(ServletAndResourceWelcomeFileTestCase.class))
            .addWelcomePages("doesnotexist.html", "index.html", "default");

    builder.addServlet(new ServletInfo("DefaultTestServlet", PathTestServlet.class)
            .addMapping("*.html"));

    DeploymentManager manager = container.addDeployment(builder);
    manager.deploy();
    root.addPrefixPath(builder.getContextPath(), manager.start());

    DefaultServer.setRootHandler(root);
}
 
Example 2
Source File: HttpProtocolReceiver.java    From jesos with Apache License 2.0 6 votes vote down vote up
public HttpProtocolReceiver(final UPID localAddress,
                            final Class<?> messageBaseClass,
                            final ManagedEventBus eventBus)
{
    this.localAddress = localAddress;
    this.messageBaseClass = messageBaseClass;
    this.eventBus = eventBus;

    final PathHandler pathHandler = new PathHandler();
    pathHandler.addPrefixPath(localAddress.getId(), new CanonicalPathHandler(new BlockingHandler(this)));

    this.shutdownHandler = new GracefulShutdownHandler(pathHandler);

    this.httpServer = Undertow.builder()
        .setIoThreads(2)
        .setWorkerThreads(16)
        .addHttpListener(localAddress.getPort(), localAddress.getHost())
        .setHandler(shutdownHandler)
        .build();
}
 
Example 3
Source File: RedirectTestCase.java    From quarkus-http with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setup() throws ServletException {


    final PathHandler pathHandler = new PathHandler();
    final ServletContainer container = ServletContainer.Factory.newInstance();
    DeploymentInfo builder = new DeploymentInfo()
            .setClassLoader(SimpleServletTestCase.class.getClassLoader())
            .setContextPath("/servletContext")
            .setClassIntrospecter(TestClassIntrospector.INSTANCE)
            .setDeploymentName("servletContext.war")
            .addServlets(
                    servlet("request", RequestPathServlet.class)
                            .addMapping("/"),
                    servlet("redirect", RedirectServlet.class)
                            .addMapping("/redirect/*"));
    DeploymentManager manager = container.addDeployment(builder);
    manager.deploy();
    try {
        pathHandler.addPrefixPath(builder.getContextPath(), manager.start());
    } catch (ServletException e) {
        throw new RuntimeException(e);
    }
    DefaultServer.setRootHandler(pathHandler);

}
 
Example 4
Source File: NestedListenerInvocationTestCase.java    From quarkus-http with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setup() throws ServletException {

    final PathHandler root = new PathHandler();
    final ServletContainer container = ServletContainer.Factory.newInstance();

    ServletInfo a = new ServletInfo("asyncServlet", AsyncServlet.class)
            .setAsyncSupported(true)
            .addMapping("/async");

    DeploymentInfo builder = new DeploymentInfo()
            .setClassLoader(NestedListenerInvocationTestCase.class.getClassLoader())
            .setContextPath("/servletContext")
            .setClassIntrospecter(TestClassIntrospector.INSTANCE)
            .setDeploymentName("servletContext.war")
            .addServlets(a)
            .addListener(new ListenerInfo(SimpleRequestListener.class));

    DeploymentManager manager = container.addDeployment(builder);
    manager.deploy();
    root.addPrefixPath(builder.getContextPath(), manager.start());

    DefaultServer.setRootHandler(root);
}
 
Example 5
Source File: SimpleServletTestCase.java    From quarkus-http with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setup() throws ServletException {

    final PathHandler root = new PathHandler();
    final ServletContainer container = ServletContainer.Factory.newInstance();

    ServletInfo s = new ServletInfo("servlet", MessageServlet.class)
            .addInitParam(MessageServlet.MESSAGE, HELLO_WORLD)
            .addMapping("/aa");

    DeploymentInfo builder = new DeploymentInfo()
            .setClassLoader(SimpleServletTestCase.class.getClassLoader())
            .setContextPath("/servletContext")
            .setClassIntrospecter(TestClassIntrospector.INSTANCE)
            .setDeploymentName("servletContext.war")
            .addServlet(s);

    DeploymentManager manager = container.addDeployment(builder);
    manager.deploy();
    root.addPrefixPath(builder.getContextPath(), manager.start());

    DefaultServer.setRootHandler(root);
}
 
Example 6
Source File: MockRequestTestCase.java    From quarkus-http with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setup() throws ServletException {

    final PathHandler root = new PathHandler();
    final ServletContainer container = ServletContainer.Factory.newInstance();

    ServletInfo s = new ServletInfo("servlet", HelloServlet.class).addMapping("/aa");

    DeploymentInfo builder = new DeploymentInfo()
            .setClassLoader(MockRequestTestCase.class.getClassLoader())
            .setContextPath("/servletContext")
            .setClassIntrospecter(TestClassIntrospector.INSTANCE)
            .setDeploymentName("servletContext.war")
            .addServlet(s);

    DeploymentManager manager = container.addDeployment(builder);
    manager.deploy();
    deployment = manager.getDeployment();
    root.addPrefixPath(builder.getContextPath(), manager.start());

    DefaultServer.setRootHandler(root);
}
 
Example 7
Source File: ContentTypeFilesTestCase.java    From quarkus-http with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setup() throws ServletException {
    final PathHandler root = new PathHandler();
    final ServletContainer container = ServletContainer.Factory.newInstance();

    DeploymentInfo builder = new DeploymentInfo()
            .setClassIntrospecter(TestClassIntrospector.INSTANCE)
            .setClassLoader(ContentTypeFilesTestCase.class.getClassLoader())
            .setContextPath("/app")
            .setDeploymentName("servletContext.war")
            .setResourceManager(new TestResourceLoader(ContentTypeServlet.class))
            .setDefaultServletConfig(new DefaultServletConfig(true))
            .addMimeMapping(new MimeMapping("jnlp", "application/x-java-jnlp-file"));

    DeploymentManager manager = container.addDeployment(builder);
    manager.deploy();
    root.addPrefixPath(builder.getContextPath(), manager.start());
    DefaultServer.setRootHandler(root);
}
 
Example 8
Source File: DefaultServletCachingTestCase.java    From quarkus-http with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setup() throws ServletException, IOException {

    tmpDir = Files.createTempDirectory(DIR_NAME);

    final PathHandler root = new PathHandler();
    final ServletContainer container = ServletContainer.Factory.newInstance();

    DeploymentInfo builder = new DeploymentInfo()
            .setClassIntrospecter(TestClassIntrospector.INSTANCE)
            .setClassLoader(ServletPathMappingTestCase.class.getClassLoader())
            .setContextPath("/servletContext")
            .setDeploymentName("servletContext.war")
            .setResourceManager(new CachingResourceManager(100, 10000, dataCache, new PathResourceManager(tmpDir, 10485760, false, false, false), METADATA_MAX_AGE));

    builder.addServlet(new ServletInfo("DefaultTestServlet", PathTestServlet.class)
            .addMapping("/path/default"))
            .addFilter(Servlets.filter("message", MessageFilter.class).addInitParam(MessageFilter.MESSAGE, "FILTER_TEXT "))
            .addFilterUrlMapping("message", "*.txt", DispatcherType.REQUEST);

    DeploymentManager manager = container.addDeployment(builder);
    manager.deploy();
    root.addPrefixPath(builder.getContextPath(), manager.start());

    DefaultServer.setRootHandler(root);
}
 
Example 9
Source File: TransferTestCase.java    From quarkus-http with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setup() throws ServletException {

    final PathHandler root = new PathHandler();
    final ServletContainer container = ServletContainer.Factory.newInstance();

    DeploymentInfo builder = new DeploymentInfo()
            .setClassLoader(SimpleServletTestCase.class.getClassLoader())
            .setContextPath("/servletContext")
            .setClassIntrospecter(TestClassIntrospector.INSTANCE)
            .setDeploymentName("servletContext.war")
            .addServlet(
                    new ServletInfo("servlet", TXServlet.class)
                            .addMapping("/")
            );

    DeploymentManager manager = container.addDeployment(builder);
    manager.deploy();
    root.addPrefixPath(builder.getContextPath(), manager.start());

    DefaultServer.setRootHandler(root);
}
 
Example 10
Source File: UndertowServer.java    From pippo with Apache License 2.0 5 votes vote down vote up
protected HttpHandler createContextHandler(HttpHandler pippoHandler) {
    String contextPath = getSettings().getContextPath();

    // create a handler than redirects non-contact requests to the context
    PathHandler contextHandler = Handlers.path(Handlers.redirect(contextPath));

    // add the handler with the context prefix
    contextHandler.addPrefixPath(contextPath, pippoHandler);

    return contextHandler;
}
 
Example 11
Source File: SecurityErrorPageTestCase.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void setup() throws IOException, ServletException {

    final ServletContainer container = ServletContainer.Factory.newInstance();
    final PathHandler root = new PathHandler();
    DefaultServer.setRootHandler(root);

    DeploymentInfo builder = new DeploymentInfo();

    builder.addServlet(new ServletInfo("secure", SecureServlet.class)
            .addMapping("/secure"))
            .addSecurityConstraint(Servlets.securityConstraint().addRoleAllowed("user").addWebResourceCollection(Servlets.webResourceCollection().addUrlPattern("/*")));

    builder.addServlet(new ServletInfo("path", PathServlet.class)
            .addMapping("/*"));

    builder.addErrorPage(new ErrorPage("/401", StatusCodes.UNAUTHORIZED));

    ServletIdentityManager identityManager = new ServletIdentityManager();
    identityManager.addUser("user1", "password1"); // Just one role less user.

    builder.setClassIntrospecter(TestClassIntrospector.INSTANCE)
            .setClassLoader(ErrorPageTestCase.class.getClassLoader())
            .setContextPath("/servletContext")
            .setServletStackTraces(ServletStackTraces.NONE)
            .setIdentityManager(identityManager)
            .setLoginConfig(Servlets.loginConfig("BASIC", "Test Realm"))
            .setDeploymentName("servletContext.war");

    final DeploymentManager manager1 = container.addDeployment(builder);
    manager1.deploy();
    root.addPrefixPath(builder.getContextPath(), manager1.start());

}
 
Example 12
Source File: ServletClientCertAuthTestCase.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void setup() throws ServletException, IOException {
    DefaultServer.startSSLServer();
    clientSSLContext = DefaultServer.getClientSSLContext();


    final PathHandler path = new PathHandler();

    final ServletContainer container = ServletContainer.Factory.newInstance();

    ServletInfo usernameServlet = new ServletInfo("Username Servlet", SendUsernameServlet.class)
            .addMapping("/secured/username");

    ServletInfo authTypeServlet = new ServletInfo("Auth Type Servlet", SendAuthTypeServlet.class)
            .addMapping("/secured/authType");

    LoginConfig loginConfig = new LoginConfig(REALM_NAME);
    loginConfig.addFirstAuthMethod(new AuthMethodConfig("CLIENT_CERT"));
    DeploymentInfo builder = new DeploymentInfo()
            .setClassLoader(SimpleServletTestCase.class.getClassLoader())
            .setContextPath("/servletContext")
            .setClassIntrospecter(TestClassIntrospector.INSTANCE)
            .setDeploymentName("servletContext.war")
            .setIdentityManager(identityManager)
            .setLoginConfig(loginConfig)
            .addServlets(usernameServlet, authTypeServlet);

    builder.addSecurityConstraint(new SecurityConstraint()
            .addWebResourceCollection(new WebResourceCollection()
                    .addUrlPattern("/secured/*"))
            .addRoleAllowed("role1")
            .setEmptyRoleSemantic(SecurityInfo.EmptyRoleSemantic.DENY));

    DeploymentManager manager = container.addDeployment(builder);
    manager.deploy();
    path.addPrefixPath(builder.getContextPath(), manager.start());

    DefaultServer.setRootHandler(path);
}
 
Example 13
Source File: WSServer.java    From greycat with Apache License 2.0 5 votes vote down vote up
public void start() {
    final PathHandler pathHandler = Handlers.path();
    for (String name : handlers.keySet()) {
        pathHandler.addPrefixPath(name, handlers.get(name));
    }
    this.server = Undertow.builder().addHttpListener(port, "0.0.0.0", pathHandler).build();
    server.start();
    if (builder.storage != null) {
        builder.storage.listen(this);
    } else if (builder.storageFactory != null) {
        builder.storageFactory.listen(this);
    }
}
 
Example 14
Source File: RequestListenerAsyncRequestTestCase.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void setup() throws ServletException {

    final PathHandler root = new PathHandler();
    final ServletContainer container = ServletContainer.Factory.newInstance();

    ServletInfo m = new ServletInfo("messageServlet", MessageServlet.class)
            .addInitParam(MessageServlet.MESSAGE, HELLO_WORLD)
            .setAsyncSupported(true)
            .addMapping("/message");


    ServletInfo a = new ServletInfo("asyncServlet", AsyncServlet.class)
            .addInitParam(MessageServlet.MESSAGE, HELLO_WORLD)
            .setAsyncSupported(true)
            .addMapping("/async");
    ServletInfo comp = new ServletInfo("completeAsyncServlet", CompleteAsyncServlet.class)
            .addInitParam(MessageServlet.MESSAGE, HELLO_WORLD)
            .setAsyncSupported(true)
            .addMapping("/asynccomplete");

    ServletInfo a2 = new ServletInfo("asyncServlet2", AnotherAsyncServlet.class)
    .setAsyncSupported(true)
    .addMapping("/async2");

    DeploymentInfo builder = new DeploymentInfo()
            .setClassLoader(SimpleServletTestCase.class.getClassLoader())
            .setContextPath("/servletContext")
            .setClassIntrospecter(TestClassIntrospector.INSTANCE)
            .setDeploymentName("servletContext.war")
            .addServlets(m, a, a2, comp)
            .addListener(new ListenerInfo(TestListener.class));

    DeploymentManager manager = container.addDeployment(builder);
    manager.deploy();
    root.addPrefixPath(builder.getContextPath(), manager.start());

    DefaultServer.setRootHandler(root);
}
 
Example 15
Source File: FilterPathMappingTestCase.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
@Test
public void test_WFLY_1935() throws IOException, ServletException {

    DeploymentInfo builder = new DeploymentInfo();

    final PathHandler root = new PathHandler();
    final ServletContainer container = ServletContainer.Factory.newInstance();

    builder.addServlet(new ServletInfo("*.a", PathMappingServlet.class)
            .addMapping("*.a"));

    builder.addFilter(new FilterInfo("/*", PathFilter.class));
    builder.addFilterUrlMapping("/*", "/*", DispatcherType.REQUEST);

    //non standard, but we still support it
    builder.addFilter(new FilterInfo("/SimpleServlet.a", PathFilter.class));
    builder.addFilterUrlMapping("/SimpleServlet.a", "/SimpleServlet.a", DispatcherType.REQUEST);

    builder.setClassIntrospecter(TestClassIntrospector.INSTANCE)
            .setClassLoader(FilterPathMappingTestCase.class.getClassLoader())
            .setContextPath("/servletContext")
            .setDeploymentName("servletContext.war");

    final DeploymentManager manager = container.addDeployment(builder);
    manager.deploy();
    root.addPrefixPath(builder.getContextPath(), manager.start());

    DefaultServer.setRootHandler(root);


    TestHttpClient client = new TestHttpClient();
    try {
        runTest(client, "SimpleServlet.a", "*.a - /SimpleServlet.a - null", "/*", "/SimpleServlet.a");

    } finally {
        client.getConnectionManager().shutdown();
    }
}
 
Example 16
Source File: ServletBasicAuthTestCase.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
@BeforeClass
public static void setup() throws ServletException {

    final PathHandler path = new PathHandler();

    final ServletContainer container = ServletContainer.Factory.newInstance();

    ServletInfo usernameServlet = new ServletInfo("Username Servlet", SendUsernameServlet.class)
            .addMapping("/secured/username");

    ServletInfo authTypeServlet = new ServletInfo("Auth Type Servlet", SendAuthTypeServlet.class)
            .addMapping("/secured/authType");

    ServletIdentityManager identityManager = new ServletIdentityManager();
    identityManager.addUser("user1", "password1", "role1");
    identityManager.addUser("charsetUser", "password-ΓΌ", "role1");

    LoginConfig loginConfig = new LoginConfig(REALM_NAME);
    Map<String, String> props = new HashMap<>();
    props.put("charset", "ISO_8859_1");
    props.put("user-agent-charsets", "Chrome,UTF-8,OPR,UTF-8");
    loginConfig.addFirstAuthMethod(new AuthMethodConfig("BASIC", props));
    DeploymentInfo builder = new DeploymentInfo()
            .setClassLoader(SimpleServletTestCase.class.getClassLoader())
            .setContextPath("/servletContext")
            .setClassIntrospecter(TestClassIntrospector.INSTANCE)
            .setDeploymentName("servletContext.war")
            .setIdentityManager(identityManager)
            .setLoginConfig(loginConfig)
            .addServlets(usernameServlet, authTypeServlet);

    builder.addSecurityConstraint(new SecurityConstraint()
            .addWebResourceCollection(new WebResourceCollection()
                    .addUrlPattern("/secured/*"))
            .addRoleAllowed("role1")
            .setEmptyRoleSemantic(SecurityInfo.EmptyRoleSemantic.DENY));

    DeploymentManager manager = container.addDeployment(builder);
    manager.deploy();
    path.addPrefixPath(builder.getContextPath(), manager.start());

    DefaultServer.setRootHandler(path);
}
 
Example 17
Source File: AsyncListenerOnErrorTest.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
@BeforeClass
public static void setup() throws ServletException {

    final PathHandler root = new PathHandler();
    final ServletContainer container = ServletContainer.Factory.newInstance();

    ServletInfo f = new ServletInfo("faultyServlet", FaultyServlet.class)
            .addMapping("/faulty");


    ServletInfo a1 = new ServletInfo("asyncServlet1", AsyncServlet1.class)
            .setAsyncSupported(true)
            .addMapping("/async1");

    ServletInfo a2 = new ServletInfo("asyncServlet2", AsyncServlet2.class)
            .setAsyncSupported(true)
            .addMapping("/async2");


    ServletInfo a3 = new ServletInfo("asyncServlet3", AsyncServlet3.class)
            .setAsyncSupported(true)
            .addMapping("/async3");

    DeploymentInfo builder = new DeploymentInfo()
            .setClassLoader(AsyncListenerOnErrorTest.class.getClassLoader())
            .setContextPath("/servletContext")
            .setClassIntrospecter(TestClassIntrospector.INSTANCE)
            .setDeploymentName("servletContext.war")
            .addServlets(f, a1, a2, a3);

    builder.setExceptionHandler(LoggingExceptionHandler.builder()
            .add(IllegalStateException.class, "io.undertow", Logger.Level.DEBUG)
            .build());


    DeploymentManager manager = container.addDeployment(builder);
    manager.deploy();
    root.addPrefixPath(builder.getContextPath(), manager.start());

    DefaultServer.setRootHandler(root);
}
 
Example 18
Source File: WelcomeFileTestCase.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
@BeforeClass
public static void setup() throws ServletException {

    final PathHandler root = new PathHandler();
    final ServletContainer container = ServletContainer.Factory.newInstance();

    DeploymentInfo builder = new DeploymentInfo()
            .setClassIntrospecter(TestClassIntrospector.INSTANCE)
            .setClassLoader(ServletPathMappingTestCase.class.getClassLoader())
            .setContextPath("/servletContext")
            .setDeploymentName("servletContext.war")
            .setResourceManager(new TestResourceLoader(WelcomeFileTestCase.class))
            .addWelcomePages("doesnotexist.html", "index.html", "default", "servletPath/servletFile.xhtml")
            .addServlet(new ServletInfo("DefaultTestServlet", PathTestServlet.class)
                    .addMapping("/path/default"))

            .addServlet(new ServletInfo("ServletPath", PathTestServlet.class)
                    .addMapping("/foo/servletPath/*"))

            .addFilter(new FilterInfo("Filter", NoOpFilter.class))
            .addFilterUrlMapping("Filter", "/*", DispatcherType.REQUEST)
            .addFilterUrlMapping("Filter", "/", DispatcherType.REQUEST);

    DeploymentManager manager = container.addDeployment(builder);
    manager.deploy();
    root.addPrefixPath(builder.getContextPath(), manager.start());


    builder = new DeploymentInfo()
            .setClassIntrospecter(TestClassIntrospector.INSTANCE)
            .setClassLoader(ServletPathMappingTestCase.class.getClassLoader())
            .setContextPath("/servletContext2")
            .setDeploymentName("servletContext2.war")
            .setResourceManager(new TestResourceLoader(WelcomeFileTestCase.class))
            .addWelcomePages("doesnotexist.html", "index.do")
            .addServlet(new ServletInfo("*.do", PathTestServlet.class)
                    .addMapping("*.do"));

    manager = container.addDeployment(builder);
    manager.deploy();
    root.addPrefixPath(builder.getContextPath(), manager.start());
    DefaultServer.setRootHandler(root);
}
 
Example 19
Source File: SecurityConstraintUrlMappingTestCase.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
@BeforeClass
public static void setup() throws ServletException {

    final PathHandler root = new PathHandler();
    final ServletContainer container = ServletContainer.Factory.newInstance();

    ServletInfo s = new ServletInfo("servlet", AuthenticationMessageServlet.class)
            .addInitParam(MessageServlet.MESSAGE, HELLO_WORLD)
            .addMapping("/role1")
            .addMapping("/role2")
            .addMapping("/starstar")
            .addMapping("/secured/role2/*")
            .addMapping("/secured/1/2/*")
            .addMapping("/public/*")
            .addMapping("/extension/*");

    ServletIdentityManager identityManager = new ServletIdentityManager();
    identityManager.addUser("user1", "password1", "role1");
    identityManager.addUser("user2", "password2", "role2", "**");
    identityManager.addUser("user3", "password3", "role1", "role2");
    identityManager.addUser("user4", "password4", "badRole");

    DeploymentInfo builder = new DeploymentInfo()
            .setClassLoader(SimpleServletTestCase.class.getClassLoader())
            .setContextPath("/servletContext")
            .setClassIntrospecter(TestClassIntrospector.INSTANCE)
            .setDeploymentName("servletContext.war")
            .setIdentityManager(identityManager)
            .setLoginConfig(new LoginConfig("BASIC", "Test Realm"))
            .addServlet(s);

    builder.addSecurityConstraint(new SecurityConstraint()
            .addWebResourceCollection(new WebResourceCollection()
                    .addUrlPattern("/role1"))
            .addRoleAllowed("role1"));

    builder.addSecurityConstraint(new SecurityConstraint()
            .addWebResourceCollection(new WebResourceCollection()
                    .addUrlPattern("/starstar"))
            .addRoleAllowed("**"));
    builder.addSecurityConstraint(new SecurityConstraint()
            .addWebResourceCollection(new WebResourceCollection()
                    .addUrlPattern("/secured/*"))
            .addRoleAllowed("role2"));
    builder.addSecurityConstraint(new SecurityConstraint()
            .addWebResourceCollection(new WebResourceCollection()
                    .addUrlPattern("/secured/*"))
            .addRoleAllowed("role2"));
    builder.addSecurityConstraint(new SecurityConstraint()
            .addWebResourceCollection(new WebResourceCollection()
                    .addUrlPattern("/secured/1/*"))
            .addRoleAllowed("role1"));
    builder.addSecurityConstraint(new SecurityConstraint()
            .addWebResourceCollection(new WebResourceCollection()
                    .addUrlPattern("/secured/1/2/*"))
            .addRoleAllowed("role2"));
    builder.addSecurityConstraint(new SecurityConstraint()
            .addWebResourceCollection(new WebResourceCollection()
                    .addUrlPattern("*.html"))
            .addRoleAllowed("role2"));
    builder.addSecurityConstraint(new SecurityConstraint()
            .addWebResourceCollection(new WebResourceCollection()
                    .addUrlPattern("/public/*")).setEmptyRoleSemantic(SecurityInfo.EmptyRoleSemantic.PERMIT));
    builder.addSecurityConstraint(new SecurityConstraint()
            .addWebResourceCollection(new WebResourceCollection()
                    .addUrlPattern("/public/postSecured/*")
                    .addHttpMethod("POST"))
            .addRoleAllowed("role1"));

    DeploymentManager manager = container.addDeployment(builder);
    manager.deploy();
    root.addPrefixPath(builder.getContextPath(), manager.start());

    builder = new DeploymentInfo()
            .setClassLoader(SimpleServletTestCase.class.getClassLoader())
            .setContextPath("/star")
            .setClassIntrospecter(TestClassIntrospector.INSTANCE)
            .setDeploymentName("servletContext.war")
            .setIdentityManager(identityManager)
            .setLoginConfig(new LoginConfig("BASIC", "Test Realm"))
            .addSecurityRole("**")
            .addServlet(s);

    builder.addSecurityConstraint(new SecurityConstraint()
            .addWebResourceCollection(new WebResourceCollection()
                    .addUrlPattern("/starstar"))
            .addRoleAllowed("**"));

    manager = container.addDeployment(builder);
    manager.deploy();
    root.addPrefixPath(builder.getContextPath(), manager.start());
    DefaultServer.setRootHandler(root);
}
 
Example 20
Source File: ServletLifecycleTestCase.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
@Test
public void testServletLifecycle() throws Exception {


    final PathHandler root = new PathHandler();
    final ServletContainer container = ServletContainer.Factory.newInstance();

    ServletInfo s = new ServletInfo("servlet", LifeCycleServlet.class)
            .addMapping("/aa");
    FilterInfo f = new FilterInfo("filter", LifecycleFilter.class);

    DeploymentInfo builder = new DeploymentInfo()
            .setClassLoader(ServletLifecycleTestCase.class.getClassLoader())
            .setContextPath("/servletContext")
            .setClassIntrospecter(TestClassIntrospector.INSTANCE)
            .setDeploymentName("servletContext.war")
            .addServlet(s)
            .addFilter(f)
            .addFilterUrlMapping("filter", "/aa", DispatcherType.REQUEST);

    DeploymentManager manager = container.addDeployment(builder);
    manager.deploy();
    root.addPrefixPath(builder.getContextPath(), manager.start());

    DefaultServer.setRootHandler(root);

    TestHttpClient client = new TestHttpClient();
    try {
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext/aa");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        final String response = HttpClientUtils.readResponse(result);

        manager.stop();
        manager.undeploy();

        Assert.assertTrue(LifeCycleServlet.initCalled);
        Assert.assertTrue(LifeCycleServlet.destroyCalled);
        Assert.assertTrue(LifecycleFilter.initCalled);
        Assert.assertTrue(LifecycleFilter.destroyCalled);

    } finally {
        client.getConnectionManager().shutdown();
    }
}