Search in sources :

Example 81 with HttpServletResponse

use of javax.servlet.http.HttpServletResponse 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 82 with HttpServletResponse

use of javax.servlet.http.HttpServletResponse 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 83 with HttpServletResponse

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

the class MultiPartContentProviderTest method testFieldWithOverridenContentType.

@Test
public void testFieldWithOverridenContentType() throws Exception {
    String name = "field";
    String value = "è";
    Charset encoding = StandardCharsets.ISO_8859_1;
    start(new AbstractMultiPartHandler() {

        @Override
        protected void handle(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            Collection<Part> parts = request.getParts();
            Assert.assertEquals(1, parts.size());
            Part part = parts.iterator().next();
            Assert.assertEquals(name, part.getName());
            String contentType = part.getContentType();
            Assert.assertNotNull(contentType);
            int equal = contentType.lastIndexOf('=');
            Charset charset = Charset.forName(contentType.substring(equal + 1));
            Assert.assertEquals(encoding, charset);
            Assert.assertEquals(value, IO.toString(part.getInputStream(), charset));
        }
    });
    MultiPartContentProvider multiPart = new MultiPartContentProvider();
    HttpFields fields = new HttpFields();
    fields.put(HttpHeader.CONTENT_TYPE, "text/plain;charset=" + encoding.name());
    BytesContentProvider content = new BytesContentProvider(value.getBytes(encoding));
    multiPart.addFieldPart(name, content, fields);
    multiPart.close();
    ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).method(HttpMethod.POST).content(multiPart).send();
    Assert.assertEquals(200, response.getStatus());
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Charset(java.nio.charset.Charset) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Part(javax.servlet.http.Part) HttpFields(org.eclipse.jetty.http.HttpFields) Collection(java.util.Collection) AbstractHttpClientServerTest(org.eclipse.jetty.client.AbstractHttpClientServerTest) Test(org.junit.Test)

Example 84 with HttpServletResponse

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

the class MultiPartContentProviderTest method testFieldWithFile.

@Test
public void testFieldWithFile() throws Exception {
    // Prepare a file to upload.
    byte[] data = new byte[1024];
    new Random().nextBytes(data);
    Path tmpDir = MavenTestingUtils.getTargetTestingPath();
    Path tmpPath = Files.createTempFile(tmpDir, "multipart_", ".txt");
    try (OutputStream output = Files.newOutputStream(tmpPath, StandardOpenOption.CREATE)) {
        output.write(data);
    }
    String field = "field";
    String value = "€";
    String fileField = "file";
    Charset encoding = StandardCharsets.UTF_8;
    String contentType = "text/plain;charset=" + encoding.name();
    String headerName = "foo";
    String headerValue = "bar";
    start(new AbstractMultiPartHandler() {

        @Override
        protected void handle(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            List<Part> parts = new ArrayList<>(request.getParts());
            Assert.assertEquals(2, parts.size());
            Part fieldPart = parts.get(0);
            Part filePart = parts.get(1);
            if (!field.equals(fieldPart.getName())) {
                Part swap = filePart;
                filePart = fieldPart;
                fieldPart = swap;
            }
            Assert.assertEquals(field, fieldPart.getName());
            Assert.assertEquals(contentType, fieldPart.getContentType());
            Assert.assertEquals(value, IO.toString(fieldPart.getInputStream(), encoding));
            Assert.assertEquals(headerValue, fieldPart.getHeader(headerName));
            Assert.assertEquals(fileField, filePart.getName());
            Assert.assertEquals("application/octet-stream", filePart.getContentType());
            Assert.assertEquals(tmpPath.getFileName().toString(), filePart.getSubmittedFileName());
            Assert.assertEquals(Files.size(tmpPath), filePart.getSize());
            Assert.assertArrayEquals(data, IO.readBytes(filePart.getInputStream()));
        }
    });
    MultiPartContentProvider multiPart = new MultiPartContentProvider();
    HttpFields fields = new HttpFields();
    fields.put(headerName, headerValue);
    multiPart.addFieldPart(field, new StringContentProvider(value, encoding), fields);
    multiPart.addFilePart(fileField, tmpPath.getFileName().toString(), new PathContentProvider(tmpPath), null);
    multiPart.close();
    ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).method(HttpMethod.POST).content(multiPart).send();
    Assert.assertEquals(200, response.getStatus());
    Files.delete(tmpPath);
}
Also used : Path(java.nio.file.Path) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) OutputStream(java.io.OutputStream) Charset(java.nio.charset.Charset) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Random(java.util.Random) Part(javax.servlet.http.Part) HttpFields(org.eclipse.jetty.http.HttpFields) ArrayList(java.util.ArrayList) List(java.util.List) AbstractHttpClientServerTest(org.eclipse.jetty.client.AbstractHttpClientServerTest) Test(org.junit.Test)

Example 85 with HttpServletResponse

use of javax.servlet.http.HttpServletResponse 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)

Aggregations

HttpServletResponse (javax.servlet.http.HttpServletResponse)1635 HttpServletRequest (javax.servlet.http.HttpServletRequest)1312 Test (org.junit.Test)705 IOException (java.io.IOException)576 ServletException (javax.servlet.ServletException)491 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)223 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)195 Request (org.eclipse.jetty.server.Request)186 HttpServlet (javax.servlet.http.HttpServlet)157 CountDownLatch (java.util.concurrent.CountDownLatch)156 FilterChain (javax.servlet.FilterChain)148 PrintWriter (java.io.PrintWriter)138 Test (org.testng.annotations.Test)127 HashMap (java.util.HashMap)106 ServletOutputStream (javax.servlet.ServletOutputStream)105 InterruptedIOException (java.io.InterruptedIOException)97 InputStream (java.io.InputStream)85 OutputStream (java.io.OutputStream)81 HttpSession (javax.servlet.http.HttpSession)75 ServletResponse (javax.servlet.ServletResponse)74