Java Code Examples for io.undertow.servlet.api.DeploymentInfo#addServlet()

The following examples show how to use io.undertow.servlet.api.DeploymentInfo#addServlet() . 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: GetResourceTestCase.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(GetResourceTestCase.class.getClassLoader())
            .setContextPath("/servletContext")
            .setDeploymentName("servletContext.war")
            .setResourceManager(new TestResourceLoader(GetResourceTestCase.class));

    builder.addServlet(new ServletInfo("ReadFileServlet", ReadFileServlet.class)
            .addMapping("/file"));

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

    DefaultServer.setRootHandler(root);
}
 
Example 2
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 3
Source File: DefaultServletTestCase.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();

    DeploymentInfo builder = new DeploymentInfo()
            .setClassIntrospecter(TestClassIntrospector.INSTANCE)
            .setClassLoader(ServletPathMappingTestCase.class.getClassLoader())
            .setContextPath("/servletContext")
            .setDeploymentName("servletContext.war")
            .setResourceManager(new TestResourceLoader(DefaultServletTestCase.class));

    builder.addServlet(new ServletInfo("DefaultTestServlet", PathTestServlet.class)
            .addMapping("/path/default"));

    builder.addServlet(new ServletInfo("default", DefaultServlet.class)
            .addInitParam("directory-listing", "true")
            .addMapping("/*"));

    //see UNDERTOW-458
    builder.addFilter(new FilterInfo("date-header", GetDateFilter.class));
    builder.addFilterUrlMapping("date-header", "/*", DispatcherType.REQUEST);


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

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

    DefaultServer.setRootHandler(root);
}
 
Example 4
Source File: AbstractResponseWrapperTestCase.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() throws ServletException {
    DeploymentInfo builder = new DeploymentInfo();
    builder.setExceptionHandler(LoggingExceptionHandler.builder().add(IllegalArgumentException.class, "io.undertow", Logger.Level.DEBUG).build());

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

    builder.addServlet(new ServletInfo("wrapperServlet", WrapperServlet.class)
            .addMapping("/*"));


    builder.addFilter(new FilterInfo("standard", StandardRequestWrappingFilter.class));
    builder.addFilterUrlMapping("standard", "/standard", DispatcherType.REQUEST);

    builder.addFilter(new FilterInfo("nonstandard", NonStandardRequestWrappingFilter.class));
    builder.addFilterUrlMapping("nonstandard", "/nonstandard", DispatcherType.REQUEST);

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

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

    DefaultServer.setRootHandler(root);
}
 
Example 5
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 6
Source File: FilterPathMappingTestCase.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
@Test
public void testExtensionMatchServletWithGlobalFilter() throws IOException, ServletException {

    DeploymentInfo builder = new DeploymentInfo();

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

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

    builder.addFilter(new FilterInfo("/*", PathFilter.class));
    builder.addFilterUrlMapping("/*", "/*", 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, "aa.jsp", "*.jsp - /aa.jsp - null", "/*");

    } finally {
        client.getConnectionManager().shutdown();
    }
}
 
Example 7
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 8
Source File: WebServer.java    From metamodel-membrane with Apache License 2.0 5 votes vote down vote up
public static void startServer(int port, boolean enableCors) throws Exception {
    final DeploymentInfo deployment = Servlets.deployment().setClassLoader(WebServer.class.getClassLoader());
    deployment.setContextPath("");
    deployment.setDeploymentName("membrane");
    deployment.addInitParameter("contextConfigLocation", "classpath:context/application-context.xml");
    deployment.setResourceManager(new FileResourceManager(new File("."), 0));
    deployment.addListener(Servlets.listener(ContextLoaderListener.class));
    deployment.addListener(Servlets.listener(RequestContextListener.class));
    deployment.addServlet(Servlets.servlet("dispatcher", DispatcherServlet.class).addMapping("/*")
            .addInitParam("contextConfigLocation", "classpath:context/dispatcher-servlet.xml"));
    deployment.addFilter(Servlets.filter(CharacterEncodingFilter.class).addInitParam("forceEncoding", "true")
            .addInitParam("encoding", "UTF-8"));

    final DeploymentManager manager = Servlets.defaultContainer().addDeployment(deployment);
    manager.deploy();

    final HttpHandler handler;
    if (enableCors) {
        CorsHandlers corsHandlers = new CorsHandlers();
        handler = corsHandlers.allowOrigin(manager.start());
    } else {
        handler = manager.start();
    }

    final Undertow server = Undertow.builder().addHttpListener(port, "0.0.0.0").setHandler(handler).build();
    server.start();

    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
            // graceful shutdown of everything
            server.stop();
            try {
                manager.stop();
            } catch (ServletException e) {
            }
            manager.undeploy();
        }
    });
}
 
Example 9
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 10
Source File: UndertowDeployerHelper.java    From keycloak with Apache License 2.0 5 votes vote down vote up
private void addAnnotatedServlets(DeploymentInfo di, Archive<?> archive) {
    Map<ArchivePath, Node> classNodes = archive.getContent((ArchivePath path) -> {

        String stringPath = path.get();
        return (stringPath.startsWith("/WEB-INF/classes") && stringPath.endsWith("class"));

    });

    for (Map.Entry<ArchivePath, Node> entry : classNodes.entrySet()) {
        Node n = entry.getValue();
        if (n.getAsset() instanceof ClassAsset) {
            ClassAsset classAsset = (ClassAsset) n.getAsset();
            Class<?> clazz = classAsset.getSource();

            WebServlet annotation = clazz.getAnnotation(WebServlet.class);
            if (annotation != null) {
                ServletInfo undertowServlet = new ServletInfo(clazz.getSimpleName(), (Class<? extends Servlet>) clazz);

                String[] mappings = annotation.value();
                if (mappings != null) {
                    for (String urlPattern : mappings) {
                        undertowServlet.addMapping(urlPattern);
                    }
                }

                di.addServlet(undertowServlet);
            }
        }
    }

}
 
Example 11
Source File: WebMvcBenchmarks.java    From brave with Apache License 2.0 5 votes vote down vote up
@Override protected void init(DeploymentInfo servletBuilder) {
  addFilterMappings(servletBuilder);
  AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
  appContext.register(HelloController.class);
  appContext.register(SpringConfig.class);
  servletBuilder.addServlet(new ServletInfo("DispatcherServlet", DispatcherServlet.class,
    () -> new ImmediateInstanceHandle<>(new DispatcherServlet(appContext))).addMapping("/*"));
}
 
Example 12
Source File: ServletMetricsHandlerTestCase.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
@Test
public void testMetrics() throws Exception {


    final TestMetricsCollector metricsCollector = new TestMetricsCollector();

    CompletionLatchHandler completionLatchHandler;
    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(DefaultServletTestCase.class));

    builder.addServlet(new ServletInfo("MetricTestServlet", MetricTestServlet.class)
            .addMapping("/path/default"));

    builder.addFilter(new FilterInfo("Filter", HelloFilter.class));
    builder.addFilterUrlMapping("Filter", "/filterpath/*", DispatcherType.REQUEST);
    builder.setMetricsCollector(metricsCollector);

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

    DefaultServer.setRootHandler(completionLatchHandler = new CompletionLatchHandler(root));

    HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext/path/default");
    TestHttpClient client = new TestHttpClient();
    try {
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Assert.assertTrue(HttpClientUtils.readResponse(result).contains("metric"));
        completionLatchHandler.await();
        completionLatchHandler.reset();

        MetricsHandler.MetricResult metrics = metricsCollector.getMetrics("MetricTestServlet");
        Assert.assertEquals(1, metrics.getTotalRequests());
        Assert.assertTrue(metrics.getMaxRequestTime() > 0);
        Assert.assertEquals(metrics.getMinRequestTime(), metrics.getMaxRequestTime());
        Assert.assertEquals(metrics.getMaxRequestTime(), metrics.getTotalRequestTime());


        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Assert.assertTrue(HttpClientUtils.readResponse(result).contains("metric"));
        completionLatchHandler.await();
        completionLatchHandler.reset();


        metrics = metricsCollector.getMetrics("MetricTestServlet");
        Assert.assertEquals(2, metrics.getTotalRequests());

    } finally {

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