Search in sources :

Example 1 with ContentResponse

use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.

the class HttpClientTransportOverHTTP2Test method testExternalServer.

@Ignore
@Test
public void testExternalServer() throws Exception {
    HTTP2Client http2Client = new HTTP2Client();
    SslContextFactory sslContextFactory = new SslContextFactory();
    HttpClient httpClient = new HttpClient(new HttpClientTransportOverHTTP2(http2Client), sslContextFactory);
    Executor executor = new QueuedThreadPool();
    httpClient.setExecutor(executor);
    httpClient.start();
    //        ContentResponse response = httpClient.GET("https://http2.akamai.com/");
    ContentResponse response = httpClient.GET("https://webtide.com/");
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
    httpClient.stop();
}
Also used : SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) Executor(java.util.concurrent.Executor) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpClient(org.eclipse.jetty.client.HttpClient) HTTP2Client(org.eclipse.jetty.http2.client.HTTP2Client) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with ContentResponse

use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.

the class HttpClientTransportOverHTTP2Test method testRequestHasHTTP2Version.

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

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            HttpVersion version = HttpVersion.fromString(request.getProtocol());
            response.setStatus(version == HttpVersion.HTTP_2 ? HttpStatus.OK_200 : HttpStatus.INTERNAL_SERVER_ERROR_500);
        }
    });
    ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).onRequestBegin(request -> {
        if (request.getVersion() != HttpVersion.HTTP_2)
            request.abort(new Exception("Not a HTTP/2 request"));
    }).send();
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Request(org.eclipse.jetty.server.Request) ServletException(javax.servlet.ServletException) HTTP2Client(org.eclipse.jetty.http2.client.HTTP2Client) ByteBuffer(java.nio.ByteBuffer) Stream(org.eclipse.jetty.http2.api.Stream) ServerSocket(java.net.ServerSocket) HttpClient(org.eclipse.jetty.client.HttpClient) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MetaData(org.eclipse.jetty.http.MetaData) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) Map(java.util.Map) HttpProxy(org.eclipse.jetty.client.HttpProxy) HttpStatus(org.eclipse.jetty.http.HttpStatus) ResetFrame(org.eclipse.jetty.http2.frames.ResetFrame) Callback(org.eclipse.jetty.util.Callback) HttpDestination(org.eclipse.jetty.client.HttpDestination) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) HTTP2Session(org.eclipse.jetty.http2.HTTP2Session) ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Session(org.eclipse.jetty.http2.api.Session) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) BufferUtil(org.eclipse.jetty.util.BufferUtil) Socket(java.net.Socket) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) HttpVersion(org.eclipse.jetty.http.HttpVersion) ErrorCode(org.eclipse.jetty.http2.ErrorCode) SettingsFrame(org.eclipse.jetty.http2.frames.SettingsFrame) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) SocketTimeoutException(java.net.SocketTimeoutException) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) HttpFields(org.eclipse.jetty.http.HttpFields) OutputStream(java.io.OutputStream) GoAwayFrame(org.eclipse.jetty.http2.frames.GoAwayFrame) Executor(java.util.concurrent.Executor) RawHTTP2ServerConnectionFactory(org.eclipse.jetty.http2.server.RawHTTP2ServerConnectionFactory) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) Test(org.junit.Test) ServerParser(org.eclipse.jetty.http2.parser.ServerParser) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Generator(org.eclipse.jetty.http2.generator.Generator) HttpMethod(org.eclipse.jetty.http.HttpMethod) Ignore(org.junit.Ignore) Assert(org.junit.Assert) InputStream(java.io.InputStream) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) HttpVersion(org.eclipse.jetty.http.HttpVersion) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) ServletException(javax.servlet.ServletException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 3 with ContentResponse

use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.

the class HttpClientTransportOverHTTP2Test method testRequestViaForwardHttpProxy.

@Test
public void testRequestViaForwardHttpProxy() throws Exception {
    String path = "/path";
    String query = "a=b";
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            Assert.assertEquals(path, request.getRequestURI());
            Assert.assertEquals(query, request.getQueryString());
        }
    });
    int proxyPort = connector.getLocalPort();
    client.getProxyConfiguration().getProxies().add(new HttpProxy("localhost", proxyPort));
    // Any port will do, just not the same as the proxy.
    int serverPort = proxyPort + 1;
    ContentResponse response = client.newRequest("localhost", serverPort).path(path + "?" + query).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) HttpProxy(org.eclipse.jetty.client.HttpProxy) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Test(org.junit.Test)

Example 4 with ContentResponse

use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.

the class PushedResourcesTest method testPushedResourceCancelled.

@Test
public void testPushedResourceCancelled() throws Exception {
    String pushPath = "/secondary";
    CountDownLatch latch = new CountDownLatch(1);
    start(new ServerSessionListener.Adapter() {

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            HttpURI pushURI = new HttpURI("http://localhost:" + connector.getLocalPort() + pushPath);
            MetaData.Request pushRequest = new MetaData.Request(HttpMethod.GET.asString(), pushURI, HttpVersion.HTTP_2, new HttpFields());
            stream.push(new PushPromiseFrame(stream.getId(), 0, pushRequest), new Promise.Adapter<Stream>() {

                @Override
                public void succeeded(Stream pushStream) {
                    // Just send the normal response and wait for the reset.
                    MetaData.Response response = new MetaData.Response(HttpVersion.HTTP_2, HttpStatus.OK_200, new HttpFields());
                    stream.headers(new HeadersFrame(stream.getId(), response, null, true), Callback.NOOP);
                }
            }, new Stream.Listener.Adapter() {

                @Override
                public void onReset(Stream stream, ResetFrame frame) {
                    latch.countDown();
                }
            });
            return null;
        }
    });
    HttpRequest request = (HttpRequest) client.newRequest("localhost", connector.getLocalPort());
    ContentResponse response = request.pushListener((mainRequest, pushedRequest) -> null).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : HttpRequest(org.eclipse.jetty.client.HttpRequest) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpRequest(org.eclipse.jetty.client.HttpRequest) CountDownLatch(java.util.concurrent.CountDownLatch) PushPromiseFrame(org.eclipse.jetty.http2.frames.PushPromiseFrame) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) HttpURI(org.eclipse.jetty.http.HttpURI) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) ResetFrame(org.eclipse.jetty.http2.frames.ResetFrame) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Test(org.junit.Test)

Example 5 with ContentResponse

use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.

the class AsyncMiddleManServletTest method testAfterContentTransformerDoNoTransform.

private void testAfterContentTransformerDoNoTransform(final boolean readSource, final boolean useDisk) throws Exception {
    final String key0 = "id";
    long value0 = 1;
    final String key1 = "channel";
    String value1 = "foo";
    final String json = "{ \"" + key0 + "\":" + value0 + ", \"" + key1 + "\":\"" + value1 + "\" }";
    startServer(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.getOutputStream().write(json.getBytes(StandardCharsets.UTF_8));
        }
    });
    startProxy(new AsyncMiddleManServlet() {

        @Override
        protected ContentTransformer newServerResponseContentTransformer(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Response serverResponse) {
            return new AfterContentTransformer() {

                {
                    if (useDisk)
                        setMaxInputBufferSize(0);
                }

                @Override
                public boolean transform(Source source, Sink sink) throws IOException {
                    if (readSource) {
                        InputStream input = source.getInputStream();
                        JSON.parse(new InputStreamReader(input, "UTF-8"));
                    }
                    // No transformation.
                    return false;
                }
            };
        }
    });
    startClient();
    ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(200, response.getStatus());
    @SuppressWarnings("unchecked") Map<String, Object> obj = (Map<String, Object>) JSON.parse(response.getContentAsString());
    Assert.assertNotNull(obj);
    Assert.assertEquals(2, obj.size());
    Assert.assertEquals(value0, obj.get(key0));
    Assert.assertEquals(value1, obj.get(key1));
}
Also used : InputStreamReader(java.io.InputStreamReader) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServlet(javax.servlet.http.HttpServlet) GZIPInputStream(java.util.zip.GZIPInputStream) ServletInputStream(javax.servlet.ServletInputStream) InputStream(java.io.InputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) IOException(java.io.IOException) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

ContentResponse (org.eclipse.jetty.client.api.ContentResponse)436 Test (org.junit.Test)343 HttpServletRequest (javax.servlet.http.HttpServletRequest)204 IOException (java.io.IOException)166 HttpServletResponse (javax.servlet.http.HttpServletResponse)159 ServletException (javax.servlet.ServletException)150 Request (org.eclipse.jetty.client.api.Request)117 HttpClient (org.eclipse.jetty.client.HttpClient)109 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)92 HttpServlet (javax.servlet.http.HttpServlet)48 Properties (java.util.Properties)45 ModelMetaData (org.teiid.adminapi.impl.ModelMetaData)45 InterruptedIOException (java.io.InterruptedIOException)42 Request (org.eclipse.jetty.server.Request)39 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)38 CountDownLatch (java.util.concurrent.CountDownLatch)37 Test (org.testng.annotations.Test)30 StringContentProvider (org.eclipse.jetty.client.util.StringContentProvider)29 BytesContentProvider (org.eclipse.jetty.client.util.BytesContentProvider)28 HardCodedExecutionFactory (org.teiid.runtime.HardCodedExecutionFactory)27