Example usage for javax.servlet DispatcherType REQUEST

List of usage examples for javax.servlet DispatcherType REQUEST

Introduction

In this page you can find the example usage for javax.servlet DispatcherType REQUEST.

Prototype

DispatcherType REQUEST

To view the source code for javax.servlet DispatcherType REQUEST.

Click Source Link

Usage

From source file:se.inera.certificate.proxy.mappings.remote.RemoteDispatcherTest.java

@Test
public void testPut() throws Exception {
    ServletTester tester = new ServletTester();
    tester.setContextPath("/webapp1");

    tester.addFilter(filterWithMapper("/app1/path", "/app1", "/module.webapp"), "/*",
            EnumSet.of(DispatcherType.REQUEST));
    tester.addServlet(DefaultServlet.class, "/*");
    tester.start();/*ww w .j  a  v a  2 s. com*/

    HttpTester.Request req = HttpTester.newRequest();
    req.setMethod("PUT");
    req.setContent("This is some content!!");
    req.setHeader("HOST", "proxy.host");
    req.setURI("/webapp1/app1/path");

    HttpTester.Response resp = HttpTester.parseResponse(tester.getResponses(req.generate()));

    ResponseHelper rh = new ResponseHelper(resp.getContent());

    assertEquals("Unexpected X-Proxy-Original-Url", "http://proxy.host/webapp1/app1/path",
            rh.requestHeaders.get("X-Proxy-Original-Url"));
    assertEquals("Unexpected X-Proxy-Original-Servlet-ContextPath", "/webapp1",
            rh.requestHeaders.get("X-Proxy-Original-Servlet-ContextPath"));
    assertEquals("Unexpected Request URL", moduleHostUri + "/module.webapp/path", rh.requestUrl);
    assertEquals("This is some content!!", rh.requestContent);
}

From source file:se.inera.certificate.proxy.mappings.remote.RemoteDispatcherTest.java

@Test
public void testOptions() throws Exception {
    ServletTester tester = new ServletTester();
    tester.setContextPath("/webapp1");

    tester.addFilter(filterWithMapper("/app1/path", "/app1", "/module.webapp"), "/*",
            EnumSet.of(DispatcherType.REQUEST));
    tester.addServlet(DefaultServlet.class, "/*");
    tester.start();//from  w  w  w .  j  a  v a  2 s . c om

    HttpTester.Request req = HttpTester.newRequest();
    req.setMethod("OPTIONS");
    req.setHeader("HOST", "proxy.host");
    req.setURI("/webapp1/app1/path");

    HttpTester.Response resp = HttpTester.parseResponse(tester.getResponses(req.generate()));

    ResponseHelper rh = new ResponseHelper(resp.getContent());

    assertEquals("Unexpected X-Proxy-Original-Url", "http://proxy.host/webapp1/app1/path",
            rh.requestHeaders.get("X-Proxy-Original-Url"));
    assertEquals("Unexpected X-Proxy-Original-Servlet-ContextPath", "/webapp1",
            rh.requestHeaders.get("X-Proxy-Original-Servlet-ContextPath"));
    assertEquals("Unexpected Request URL", moduleHostUri + "/module.webapp/path", rh.requestUrl);
}

From source file:se.inera.certificate.proxy.mappings.remote.RemoteDispatcherTest.java

@Test
public void testHeaderOverride() throws Exception {
    ServletTester tester = new ServletTester();
    tester.setContextPath("/webapp1");

    tester.addFilter(filterWithMapper("/app1/path", "/app1", "/module.webapp"), "/*",
            EnumSet.of(DispatcherType.REQUEST));
    tester.addServlet(DefaultServlet.class, "/*");
    tester.start();//from  w  w w  .j  av a2  s .com

    HttpTester.Request req = HttpTester.newRequest();
    req.setHeader("X-Proxy-Original-Url", "BlaBla");
    req.setHeader("X-Proxy-Original-Servlet-ContextPath", "BlaBla");
    req.setMethod("GET");
    req.setHeader("HOST", "proxy.host");
    req.setURI("/webapp1/app1/path");

    HttpTester.Response resp = HttpTester.parseResponse(tester.getResponses(req.generate()));
    assertEquals("Status code not 200!", 200, resp.getStatus());

    ResponseHelper rh = new ResponseHelper(resp.getContent());

    assertEquals("http://proxy.host/webapp1/app1/path", rh.requestHeaders.get("X-Proxy-Original-Url"));
    assertEquals("/webapp1", rh.requestHeaders.get("X-Proxy-Original-Servlet-ContextPath"));
    assertEquals(moduleHostUri + "/module.webapp/path", rh.requestUrl);
}

From source file:ws.ament.hammock.web.jetty.JettyBootTest.java

@Test
public void shouldBootWebServerWithOnlyFilter() throws Exception {
    try (WeldContainer weldContainer = new Weld().disableDiscovery().extensions(new ConfigurationExtension())
            .beanClasses(JettyWebServer.class, DefaultServlet.class, MessageProvider.class,
                    WebServerConfiguration.class, DefaultConfigPropertyProducer.class)
            .initialize()) {//from   w w w  .ja  va  2s .c  o  m
        JettyWebServer webServer = weldContainer.select(JettyWebServer.class).get();
        webServer.addServletContextAttribute(WeldServletLifecycle.BEAN_MANAGER_ATTRIBUTE_NAME,
                weldContainer.getBeanManager());
        webServer.addFilter(new FilterDescriptor("Default", null, new String[] { "/*" },
                new DispatcherType[] { DispatcherType.REQUEST }, null, true, null, DefaultFilter.class));
        webServer.start();
        try (InputStream stream = new URL("http://localhost:8080/").openStream()) {
            String data = IOUtils.toString(stream).trim();
            assertThat(data).isEqualTo("Hello, world!");
        }

        try (InputStream stream = new URL("http://localhost:8080/rest").openStream()) {
            String data = IOUtils.toString(stream).trim();
            assertThat(data).isEqualTo("Hello, world!");
        }
        webServer.stop();
    }
}

From source file:ws.ament.hammock.web.tomcat.TomcatWebServerTest.java

@Test
public void shouldBootWebServer() throws Exception {
    try (WeldContainer weldContainer = new Weld().disableDiscovery().extensions(new ConfigurationExtension())
            .beanClasses(TomcatWebServer.class, DefaultServlet.class, MessageProvider.class,
                    WebServerConfiguration.class, DefaultConfigPropertyProducer.class)
            .initialize()) {//w w  w.j a v a  2s .  c  o  m
        TomcatWebServer tomcat = weldContainer.select(TomcatWebServer.class).get();
        tomcat.addServlet(new ServletDescriptor("Default", new String[] { "/*" }, new String[] { "/*" }, 1,
                new WebInitParam[] {}, true, DefaultServlet.class));
        tomcat.addFilter(new FilterDescriptor("Default", null, new String[] { "/rest" },
                new DispatcherType[] { DispatcherType.REQUEST }, null, true, null, DefaultFilter.class));
        tomcat.start();
        try (InputStream stream = new URL("http://localhost:8080/").openStream()) {
            String data = IOUtils.toString(stream).trim();
            assertThat(data).isEqualTo(MessageProvider.DATA);
        }

        try (InputStream stream = new URL("http://localhost:8080/rest").openStream()) {
            String data = IOUtils.toString(stream).trim();
            assertThat(data).isEqualTo("Hello, world!");
        }
        tomcat.stop();
    }
}