Java Code Examples for io.undertow.servlet.api.ServletContainer#addDeployment()

The following examples show how to use io.undertow.servlet.api.ServletContainer#addDeployment() . 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: TestMessagesReceivedInOrder.java    From quarkus-http with Apache License 2.0 9 votes vote down vote up
@BeforeClass
public static void setup() throws ServletException {

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

    DeploymentInfo builder = new DeploymentInfo()
            .setClassLoader(TestMessagesReceivedInOrder.class.getClassLoader())
            .setContextPath("/")
            .setResourceManager(new TestResourceLoader(TestMessagesReceivedInOrder.class))
            .setClassIntrospecter(TestClassIntrospector.INSTANCE)
            .addServletContextAttribute(WebSocketDeploymentInfo.ATTRIBUTE_NAME,
                    new WebSocketDeploymentInfo()
                            .setDispatchToWorkerThread(true)
                            .addEndpoint(EchoSocket.class)
            )
            .setDeploymentName("servletContext.war");


    DeploymentManager manager = container.addDeployment(builder);
    manager.deploy();


    DefaultServer.setRootHandler(Handlers.path().addPrefixPath("/", manager.start()));
}
 
Example 2
Source File: ChangeSessionIdTestCase.java    From quarkus-http with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setup() throws ServletException {

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

    ServletInfo s = new ServletInfo("servlet", ChangeSessionIdServlet.class)
            .addMapping("/aa");
    DeploymentInfo builder = new DeploymentInfo()
            .setClassLoader(SimpleServletTestCase.class.getClassLoader())
            .setContextPath("/servletContext")
            .setClassIntrospecter(TestClassIntrospector.INSTANCE)
            .setDeploymentName("servletContext.war")
            .addListener(new ListenerInfo(ChangeSessionIdListener.class))
            .addServlet(s);

    DeploymentManager manager = container.addDeployment(builder);
    manager.deploy();
    path.addPrefixPath(builder.getContextPath(), manager.start());
    DefaultServer.setRootHandler(path);
}
 
Example 3
Source File: JsrWebSocketServerTest.java    From quarkus-http with Apache License 2.0 6 votes vote down vote up
private ServletContext deployServlet(final ServerWebSocketContainer deployment) throws ServletException {

        final DeploymentInfo builder;
        builder = new DeploymentInfo()
                .setClassLoader(getClass().getClassLoader())
                .setContextPath("/")
                .setClassIntrospecter(TestClassIntrospector.INSTANCE)
                .setDeploymentName("websocket.war")
                .addFilter(new FilterInfo("filter", JsrWebSocketFilter.class))
                .addFilterUrlMapping("filter", "/*", DispatcherType.REQUEST)
                .addServletContextAttribute(javax.websocket.server.ServerContainer.class.getName(), deployment);

        final PathHandler root = new PathHandler();
        final ServletContainer container = ServletContainer.Factory.newInstance();
        DeploymentManager manager = container.addDeployment(builder);
        manager.deploy();
        root.addPrefixPath(builder.getContextPath(), manager.start());

        DefaultServer.setRootHandler(root);
        return manager.getDeployment().getServletContext();
    }
 
Example 4
Source File: ParameterEchoTestCase.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", ParameterEchoServlet.class)
            .addMapping("/aaa");

    DeploymentInfo builder = new DeploymentInfo()
            .setClassLoader(ParameterEchoTestCase.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 5
Source File: SessionIdHandlingTestCase.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(new ServletInfo("servlet", RequestedSessionIdServlet.class)
                    .addMapping("/session"));
    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 6
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 7
Source File: ResponseWriterTestCase.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(ResponseWriterTestCase.class.getClassLoader())
            .setContextPath("/servletContext")
            .setDeploymentName("servletContext.war")
            .addServlet(Servlets.servlet("resp", ResponseWriterServlet.class)
                    .addMapping("/resp"))
            .addServlet(Servlets.servlet("respLArget", LargeResponseWriterServlet.class)
                    .addMapping("/large"));

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


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

    FilterInfo f = new FilterInfo("filter", LifecycleFilter.class);

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

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

    DefaultServer.setRootHandler(root);

    Assert.assertTrue(LifecycleFilter.initCalled);
}
 
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: 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 11
Source File: SuspendResumeTestCase.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void setup() throws ServletException {

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

    DeploymentInfo builder = new DeploymentInfo()
            .setClassLoader(TestMessagesReceivedInOrder.class.getClassLoader())
            .setContextPath("/")
            .setResourceManager(new TestResourceLoader(TestMessagesReceivedInOrder.class))
            .setClassIntrospecter(TestClassIntrospector.INSTANCE)
            .addServletContextAttribute(WebSocketDeploymentInfo.ATTRIBUTE_NAME,
                    new WebSocketDeploymentInfo()
                            .addListener(new WebSocketDeploymentInfo.ContainerReadyListener() {
                                @Override
                                public void ready(ServerWebSocketContainer c) {
                                    serverContainer = c;
                                }
                            })
                            .addEndpoint(SuspendResumeEndpoint.class)
            )
            .setDeploymentName("servletContext.war");


    DeploymentManager manager = container.addDeployment(builder);
    manager.deploy();


    DefaultServer.setRootHandler(Handlers.path().addPrefixPath("/", manager.start()));
}
 
Example 12
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 13
Source File: DigestAuthTestCase.java    From quarkus-http with Apache License 2.0 5 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");

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

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

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

    DefaultServer.setRootHandler(path);
}
 
Example 14
Source File: UndertowHTTPServerEngine.java    From cxf with Apache License 2.0 5 votes vote down vote up
private ServletContext buildServletContext(String contextName)
    throws ServletException {
    ServletContainer servletContainer = new ServletContainerImpl();
    DeploymentInfo deploymentInfo = new DeploymentInfo();
    deploymentInfo.setClassLoader(Thread.currentThread().getContextClassLoader());
    deploymentInfo.setDeploymentName("cxf-undertow");
    deploymentInfo.setContextPath(contextName);
    ServletInfo asyncServlet = new ServletInfo(ServletPathMatches.DEFAULT_SERVLET_NAME, CxfUndertowServlet.class);
    deploymentInfo.addServlet(asyncServlet);
    servletContainer.addDeployment(deploymentInfo);
    DeploymentManager deploymentManager = servletContainer.getDeployment(deploymentInfo.getDeploymentName());
    deploymentManager.deploy();
    deploymentManager.start();
    return deploymentManager.getDeployment().getServletContext();
}
 
Example 15
Source File: ServletSessionCrawlerTestCase.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
@Test
public void testCrawlerSessionUsage() throws IOException, InterruptedException {
    final PathHandler pathHandler = new PathHandler();
    final ServletContainer container = ServletContainer.Factory.newInstance();
    DeploymentInfo builder = new DeploymentInfo()
            .setCrawlerSessionManagerConfig(new CrawlerSessionManagerConfig())
            .setClassLoader(SimpleServletTestCase.class.getClassLoader())
            .setContextPath("/servletContext")
            .setClassIntrospecter(TestClassIntrospector.INSTANCE)
            .setDeploymentName("servletContext.war")
            .addListener(new ListenerInfo(SessionCookieConfigListener.class))
            .addServlets(new ServletInfo("servlet", SessionServlet.class)
                    .addMapping("/aa/b"));
    DeploymentManager manager = container.addDeployment(builder);
    manager.deploy();
    try {
        pathHandler.addPrefixPath(builder.getContextPath(), manager.start());
    } catch (ServletException e) {
        throw new RuntimeException(e);
    }
    DefaultServer.setRootHandler(pathHandler);
    TestHttpClient client = new TestHttpClient();

    client.setCookieStore(new CookieStore() {
        @Override
        public void addCookie(Cookie cookie) {

        }

        @Override
        public List<Cookie> getCookies() {
            return Collections.EMPTY_LIST;
        }

        @Override
        public boolean clearExpired(Date date) {
            return false;
        }

        @Override
        public void clear() {

        }
    });
    try {
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext/aa/b");
        get.addHeader(HttpHeaderNames.USER_AGENT, "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        String response = HttpClientUtils.readResponse(result);
        Assert.assertEquals("1", response);

        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        response = HttpClientUtils.readResponse(result);
        Assert.assertEquals("2", response);

        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        response = HttpClientUtils.readResponse(result);
        Assert.assertEquals("3", response);

    } finally {
        client.getConnectionManager().shutdown();
    }
}
 
Example 16
Source File: ServletSessionPersistenceTestCase.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
@Test
public void testSimpleSessionUsage() throws IOException, 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")
            .setSessionPersistenceManager(new InMemorySessionPersistence())
            .setServletSessionConfig(new ServletSessionConfig().setPath("/servletContext/aa"))
            .addServlets(new ServletInfo("servlet", SessionServlet.class)
                    .addMapping("/aa/b"));
    DeploymentManager manager = container.addDeployment(builder);
    manager.deploy();
    try {
        pathHandler.addPrefixPath(builder.getContextPath(), manager.start());
    } catch (ServletException e) {
        throw new RuntimeException(e);
    }
    DefaultServer.setRootHandler(pathHandler);
    TestHttpClient client = new TestHttpClient();
    try {
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext/aa/b");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        String response = HttpClientUtils.readResponse(result);
        Assert.assertEquals("1", response);

        String cookieValue = result.getHeaders("Set-Cookie")[0].getValue();
        Assert.assertTrue(cookieValue, cookieValue.contains("JSESSIONID"));
        Assert.assertTrue(cookieValue, cookieValue.contains("/servletContext/aa"));

        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        response = HttpClientUtils.readResponse(result);
        Assert.assertEquals("2", response);

        manager.stop();
        manager.undeploy();
        manager.deploy();
        pathHandler.addPrefixPath(builder.getContextPath(), manager.start());

        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        response = HttpClientUtils.readResponse(result);
        Assert.assertEquals("3", response);


    } finally {
        client.getConnectionManager().shutdown();
    }
}
 
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: 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 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: 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);
}