Search in sources :

Example 71 with HttpServlet

use of javax.servlet.http.HttpServlet in project jetty.project by eclipse.

the class FastCGIProxyServletTest method testURIRewrite.

@Test
public void testURIRewrite() throws Exception {
    String originalPath = "/original/index.php";
    String originalQuery = "foo=bar";
    String remotePath = "/remote/index.php";
    prepare(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            Assert.assertThat((String) request.getAttribute(FCGI.Headers.REQUEST_URI), Matchers.startsWith(originalPath));
            Assert.assertEquals(originalQuery, request.getAttribute(FCGI.Headers.QUERY_STRING));
            Assert.assertThat(request.getRequestURI(), Matchers.endsWith(remotePath));
        }
    });
    context.stop();
    String pathAttribute = "_path_attribute";
    String queryAttribute = "_query_attribute";
    ServletHolder fcgi = context.getServletHandler().getServlet("fcgi");
    fcgi.setInitParameter(FastCGIProxyServlet.ORIGINAL_URI_ATTRIBUTE_INIT_PARAM, pathAttribute);
    fcgi.setInitParameter(FastCGIProxyServlet.ORIGINAL_QUERY_ATTRIBUTE_INIT_PARAM, queryAttribute);
    context.insertHandler(new HandlerWrapper() {

        @Override
        public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            if (target.startsWith("/remote/")) {
                request.setAttribute(pathAttribute, originalPath);
                request.setAttribute(queryAttribute, originalQuery);
            }
            super.handle(target, baseRequest, request, response);
        }
    });
    context.start();
    ContentResponse response = client.newRequest("localhost", httpConnector.getLocalPort()).path(remotePath).send();
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServlet(javax.servlet.http.HttpServlet) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) HandlerWrapper(org.eclipse.jetty.server.handler.HandlerWrapper) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Test(org.junit.Test)

Example 72 with HttpServlet

use of javax.servlet.http.HttpServlet in project jetty.project by eclipse.

the class TryFilesFilterTest method testHTTPSRequestIsForwarded.

@Test
public void testHTTPSRequestIsForwarded() throws Exception {
    final String path = "/one/";
    prepare(new HttpServlet() {

        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            Assert.assertTrue("https".equalsIgnoreCase(req.getScheme()));
            Assert.assertTrue(req.isSecure());
            Assert.assertEquals(forwardPath, req.getRequestURI());
            Assert.assertTrue(req.getQueryString().endsWith(path));
        }
    });
    ContentResponse response = client.newRequest("localhost", sslConnector.getLocalPort()).scheme("https").path(path).send();
    Assert.assertEquals(200, response.getStatus());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) Test(org.junit.Test)

Example 73 with HttpServlet

use of javax.servlet.http.HttpServlet in project jetty.project by eclipse.

the class TestOSGiUtil method testHttpServiceGreetings.

protected static void testHttpServiceGreetings(BundleContext bundleContext, String protocol, int port) throws Exception {
    assertActiveBundle(bundleContext, "org.eclipse.jetty.osgi.boot");
    assertActiveBundle(bundleContext, "org.eclipse.jetty.osgi.httpservice");
    assertActiveBundle(bundleContext, "org.eclipse.equinox.http.servlet");
    // in the OSGi world this would be bad code and we should use a bundle
    // tracker.
    // here we purposely want to make sure that the httpService is actually
    // ready.
    ServiceReference sr = bundleContext.getServiceReference(HttpService.class.getName());
    Assert.assertNotNull("The httpServiceOSGiBundle is started and should " + "have deployed a service reference for HttpService", sr);
    HttpService http = (HttpService) bundleContext.getService(sr);
    http.registerServlet("/greetings", new HttpServlet() {

        private static final long serialVersionUID = 1L;

        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.getWriter().write("Hello");
        }
    }, null, null);
    // now test the servlet
    HttpClient client = protocol.equals("https") ? new HttpClient(newSslContextFactory()) : new HttpClient();
    try {
        client.start();
        ContentResponse response = client.GET(protocol + "://127.0.0.1:" + port + "/greetings");
        Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
        String content = new String(response.getContent());
        Assert.assertEquals("Hello", content);
    } finally {
        client.stop();
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpService(org.osgi.service.http.HttpService) HttpServlet(javax.servlet.http.HttpServlet) HttpClient(org.eclipse.jetty.client.HttpClient) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) ServiceReference(org.osgi.framework.ServiceReference)

Example 74 with HttpServlet

use of javax.servlet.http.HttpServlet in project jetty.project by eclipse.

the class DispatcherForwardTest method testQueryAggregatesWithFormBeforeAndAfterForward.

@Test
public void testQueryAggregatesWithFormBeforeAndAfterForward() throws Exception {
    // 1. request /one?a=1 + content b=2
    // 1. assert params => a=1&b=2
    // 1. forward /two?c=3
    // 2. assert query => a=1&c=3 + params => a=1&b=2&c=3
    // 1. assert query => a=1 + params => a=1&b=2
    CountDownLatch latch = new CountDownLatch(1);
    final String query1 = "a=1%20one";
    final String query2 = "c=3%20three";
    final String query3 = "c=3%20three&a=1%20one";
    final String form = "b=2%20two";
    servlet1 = new HttpServlet() {

        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            checkThat(req.getQueryString(), Matchers.equalTo(query1));
            checkThat(req.getParameter("a"), Matchers.equalTo("1 one"));
            checkThat(req.getParameter("b"), Matchers.equalTo("2 two"));
            req.getRequestDispatcher("/two?" + query2).forward(req, resp);
            checkThat(req.getQueryString(), Matchers.equalTo(query1));
            checkThat(req.getParameter("a"), Matchers.equalTo("1 one"));
            checkThat(req.getParameter("b"), Matchers.equalTo("2 two"));
            checkThat(req.getParameter("c"), Matchers.nullValue());
            latch.countDown();
        }
    };
    servlet2 = new HttpServlet() {

        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            checkThat(req.getQueryString(), Matchers.equalTo(query3));
            checkThat(req.getParameter("a"), Matchers.equalTo("1 one"));
            checkThat(req.getParameter("b"), Matchers.equalTo("2 two"));
            checkThat(req.getParameter("c"), Matchers.equalTo("3 three"));
        }
    };
    prepare();
    String request = "" + "POST /one?" + query1 + " HTTP/1.1\r\n" + "Host: localhost\r\n" + "Content-Type: application/x-www-form-urlencoded\r\n" + "Content-Length: " + form.length() + "\r\n" + "Connection: close\r\n" + "\r\n" + form;
    String response = connector.getResponses(request);
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(response, response.startsWith("HTTP/1.1 200"));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 75 with HttpServlet

use of javax.servlet.http.HttpServlet in project jetty.project by eclipse.

the class DispatcherForwardTest method testQueryRetainedByForwardWithoutQuery.

@Test
public void testQueryRetainedByForwardWithoutQuery() throws Exception {
    // 1. request /one?a=1%20one
    // 1. forward /two
    // 2. assert query => a=1 one
    // 1. assert query => a=1 one
    CountDownLatch latch = new CountDownLatch(1);
    final String query1 = "a=1%20one";
    servlet1 = new HttpServlet() {

        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            checkThat(req.getQueryString(), Matchers.equalTo(query1));
            req.getRequestDispatcher("/two").forward(req, resp);
            checkThat(req.getQueryString(), Matchers.equalTo(query1));
            checkThat(req.getParameter("a"), Matchers.equalTo("1 one"));
            latch.countDown();
        }
    };
    servlet2 = new HttpServlet() {

        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            checkThat(req.getQueryString(), Matchers.equalTo(query1));
            checkThat(req.getParameter("a"), Matchers.equalTo("1 one"));
        }
    };
    prepare();
    String request = "" + "GET /one?" + query1 + " HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: close\r\n" + "\r\n";
    String response = connector.getResponses(request);
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(response, response.startsWith("HTTP/1.1 200"));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

HttpServlet (javax.servlet.http.HttpServlet)173 HttpServletRequest (javax.servlet.http.HttpServletRequest)152 HttpServletResponse (javax.servlet.http.HttpServletResponse)152 IOException (java.io.IOException)130 ServletException (javax.servlet.ServletException)128 Test (org.junit.Test)117 CountDownLatch (java.util.concurrent.CountDownLatch)68 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)61 InterruptedIOException (java.io.InterruptedIOException)55 ServletOutputStream (javax.servlet.ServletOutputStream)36 AsyncContext (javax.servlet.AsyncContext)34 HttpFields (org.eclipse.jetty.http.HttpFields)32 MetaData (org.eclipse.jetty.http.MetaData)32 HeadersFrame (org.eclipse.jetty.http2.frames.HeadersFrame)32 ServletInputStream (javax.servlet.ServletInputStream)31 Session (org.eclipse.jetty.http2.api.Session)30 Stream (org.eclipse.jetty.http2.api.Stream)27 Response (org.eclipse.jetty.client.api.Response)26 HttpContentResponse (org.eclipse.jetty.client.HttpContentResponse)24 HashMap (java.util.HashMap)22