Search in sources :

Example 76 with HttpServletResponse

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

the class JDK9ALPNTest method testClientNotSupportingALPNServerSpeaksDefaultProtocol.

@Test
public void testClientNotSupportingALPNServerSpeaksDefaultProtocol() throws Exception {
    startServer(new AbstractHandler.ErrorDispatchHandler() {

        @Override
        protected void doNonErrorHandle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
        }
    });
    SslContextFactory sslContextFactory = new SslContextFactory(true);
    sslContextFactory.start();
    SSLContext sslContext = sslContextFactory.getSslContext();
    try (SSLSocket client = (SSLSocket) sslContext.getSocketFactory().createSocket("localhost", connector.getLocalPort())) {
        client.setUseClientMode(true);
        client.setSoTimeout(5000);
        client.startHandshake();
        OutputStream output = client.getOutputStream();
        output.write(("" + "GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: close\r\n" + "\r\n" + "").getBytes(StandardCharsets.UTF_8));
        output.flush();
        InputStream input = client.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8));
        String line = reader.readLine();
        Assert.assertTrue(line.contains(" 200 "));
        while (true) {
            if (reader.readLine() == null)
                break;
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) SSLSocket(javax.net.ssl.SSLSocket) OutputStream(java.io.OutputStream) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) SSLContext(javax.net.ssl.SSLContext) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) BufferedReader(java.io.BufferedReader) Test(org.junit.Test)

Example 77 with HttpServletResponse

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

the class TryFilesFilter method doFilter.

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    HttpServletRequest httpRequest = (HttpServletRequest) request;
    HttpServletResponse httpResponse = (HttpServletResponse) response;
    for (int i = 0; i < files.length - 1; ++i) {
        String file = files[i];
        String resolved = resolve(httpRequest, file);
        URL url = request.getServletContext().getResource(resolved);
        if (url == null)
            continue;
        if (Files.isReadable(toPath(url))) {
            chain.doFilter(httpRequest, httpResponse);
            return;
        }
    }
    // The last one is the fallback
    fallback(httpRequest, httpResponse, chain, files[files.length - 1]);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) URL(java.net.URL)

Example 78 with HttpServletResponse

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

the class HttpClientTest method testPOSTWithParameters.

@Test
public void testPOSTWithParameters() throws Exception {
    final String paramName = "a";
    final String paramValue = "€";
    start(new AbstractHandler() {

        @Override
        public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            String value = request.getParameter(paramName);
            if (paramValue.equals(value)) {
                response.setCharacterEncoding("UTF-8");
                response.setContentType("text/plain");
                response.getOutputStream().print(value);
            }
        }
    });
    ContentResponse response = client.POST(scheme + "://localhost:" + connector.getLocalPort()).param(paramName, paramValue).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertNotNull(response);
    Assert.assertEquals(200, response.getStatus());
    Assert.assertEquals(paramValue, new String(response.getContent(), "UTF-8"));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Test(org.junit.Test)

Example 79 with HttpServletResponse

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

the class HttpClientTest method testEarlyEOF.

@Test
public void testEarlyEOF() throws Exception {
    start(new AbstractHandler() {

        @Override
        public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            // Promise some content, then flush the headers, then fail to send the content.
            response.setContentLength(16);
            response.flushBuffer();
            throw new NullPointerException("Explicitly thrown by test");
        }
    });
    try (StacklessLogging stackless = new StacklessLogging(org.eclipse.jetty.server.HttpChannel.class)) {
        client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).timeout(60, TimeUnit.SECONDS).send();
        Assert.fail();
    } catch (ExecutionException x) {
    // Expected.
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 80 with HttpServletResponse

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

the class HttpClientTest method testPOSTWithParametersWithContent.

@Test
public void testPOSTWithParametersWithContent() throws Exception {
    final byte[] content = { 0, 1, 2, 3 };
    final String paramName = "a";
    final String paramValue = "€";
    start(new AbstractHandler() {

        @Override
        public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            String value = request.getParameter(paramName);
            if (paramValue.equals(value)) {
                response.setCharacterEncoding("UTF-8");
                response.setContentType("application/octet-stream");
                IO.copy(request.getInputStream(), response.getOutputStream());
            }
        }
    });
    for (int i = 0; i < 256; ++i) {
        ContentResponse response = client.POST(scheme + "://localhost:" + connector.getLocalPort() + "/?b=1").param(paramName, paramValue).content(new BytesContentProvider(content)).timeout(5, TimeUnit.SECONDS).send();
        Assert.assertNotNull(response);
        Assert.assertEquals(200, response.getStatus());
        Assert.assertArrayEquals(content, response.getContent());
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Test(org.junit.Test)

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