Search in sources :

Example 51 with Response

use of org.eclipse.jetty.client.api.Response in project airlift by airlift.

the class TestHttpClientLogger method testClientLog.

@Test
public void testClientLog() throws Exception {
    String method = "GET";
    URI uri = new URI("http://www.google.com");
    int status = 200;
    long responseSize = 345;
    long requestTimestamp = System.currentTimeMillis();
    HttpFields headers = new HttpFields();
    headers.add(TRACETOKEN_HEADER, "test-token");
    Request request = new TestRequest(HTTP_2, method, uri, headers);
    Response response = new TestResponse(status);
    DefaultHttpClientLogger logger = new DefaultHttpClientLogger(file.getAbsolutePath(), 1, 256, new DataSize(1, MEGABYTE), Long.MAX_VALUE, false);
    RequestInfo requestInfo = RequestInfo.from(request, requestTimestamp);
    ResponseInfo responseInfo = ResponseInfo.from(Optional.of(response), responseSize);
    logger.log(requestInfo, responseInfo);
    logger.close();
    String actual = Files.toString(file, UTF_8);
    String[] columns = actual.trim().split("\\t");
    assertEquals(columns[0], ISO_FORMATTER.format(Instant.ofEpochMilli(requestTimestamp)));
    assertEquals(columns[1], HTTP_2.toString());
    assertEquals(columns[2], method);
    assertEquals(columns[3], uri.toString());
    assertEquals(columns[4], Integer.toString(status));
    assertEquals(columns[5], Long.toString(responseSize));
    assertEquals(columns[6], Long.toString(responseInfo.getResponseTimestampMillis() - requestTimestamp));
    assertEquals(columns[7], "test-token");
}
Also used : ResponseInfo(io.airlift.http.client.jetty.HttpClientLogger.ResponseInfo) Request(org.eclipse.jetty.client.api.Request) RequestInfo(io.airlift.http.client.jetty.HttpClientLogger.RequestInfo) URI(java.net.URI) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) HttpFields(org.eclipse.jetty.http.HttpFields) DataSize(io.airlift.units.DataSize) Test(org.testng.annotations.Test)

Example 52 with Response

use of org.eclipse.jetty.client.api.Response in project rpki-validator-3 by RIPE-NCC.

the class Http method readStream.

public static <T> T readStream(final Supplier<Request> requestF, Function<InputStream, T> reader) {
    InputStreamResponseListener listener = new InputStreamResponseListener();
    Request request = requestF.get();
    request.send(listener);
    Response response = null;
    try {
        response = listener.get(30, TimeUnit.SECONDS);
        if (response.getStatus() != 200) {
            if (response.getStatus() == 304) {
                final NotModified error = new NotModified();
                response.abort(error);
                throw error;
            } else {
                final Failure error = new Failure("unexpected response status " + response.getStatus() + " for " + request.getURI());
                response.abort(error);
                throw error;
            }
        }
        try (InputStream inputStream = listener.getInputStream()) {
            return reader.apply(inputStream);
        }
    } catch (IOException | InterruptedException | TimeoutException | ExecutionException e) {
        final Failure error = new Failure("failed reading response stream for " + request.getURI() + ": " + e, e);
        if (response != null) {
            response.abort(error);
        }
        log.error("Error ", e);
        throw error;
    }
}
Also used : Response(org.eclipse.jetty.client.api.Response) InputStreamResponseListener(org.eclipse.jetty.client.util.InputStreamResponseListener) InputStream(java.io.InputStream) Request(org.eclipse.jetty.client.api.Request) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 53 with Response

use of org.eclipse.jetty.client.api.Response in project instrumentation-java by census-instrumentation.

the class OcJettyHttpClientExtractorTest method testExtraction.

@Test
public void testExtraction() {
    HttpFields fields = new HttpFields();
    fields.add(new HttpField("User-Agent", "Test 1.0"));
    Request request = mock(Request.class);
    Response response = mock(Response.class);
    OcJettyHttpClientExtractor extractor = new OcJettyHttpClientExtractor();
    when(request.getHost()).thenReturn("localhost");
    when(request.getMethod()).thenReturn("GET");
    when(request.getHeaders()).thenReturn(fields);
    when(request.getPath()).thenReturn("/test");
    when(request.getURI()).thenReturn(uri);
    when(response.getStatus()).thenReturn(0);
    assertThat(extractor.getHost(request)).contains("localhost");
    assertThat(extractor.getMethod(request)).contains("GET");
    assertThat(extractor.getPath(request)).contains("/test");
    assertThat(extractor.getUrl(request)).contains(URI_STR);
    assertThat(extractor.getRoute(request)).contains("");
    assertThat(extractor.getUserAgent(request)).contains("Test 1.0");
    assertThat(extractor.getStatusCode(response)).isEqualTo(0);
}
Also used : Response(org.eclipse.jetty.client.api.Response) HttpField(org.eclipse.jetty.http.HttpField) HttpFields(org.eclipse.jetty.http.HttpFields) Request(org.eclipse.jetty.client.api.Request) Test(org.junit.Test)

Example 54 with Response

use of org.eclipse.jetty.client.api.Response in project SSM by Intel-bigdata.

the class ZeppelinhubRestApiHandler method sendToZeppelinHub.

private String sendToZeppelinHub(final Request request) throws IOException {
    InputStreamResponseListener listener = new InputStreamResponseListener();
    Response response;
    String data;
    request.send(listener);
    try {
        response = listener.get(30, TimeUnit.SECONDS);
    } catch (InterruptedException | TimeoutException | ExecutionException e) {
        String method = request.getMethod();
        LOG.error("Cannot perform {} request to ZeppelinHub", method, e);
        throw new IOException("Cannot perform " + method + " request to ZeppelinHub", e);
    }
    int code = response.getStatus();
    if (code == 200) {
        try (InputStream responseContent = listener.getInputStream()) {
            data = IOUtils.toString(responseContent, "UTF-8");
        }
    } else {
        String method = response.getRequest().getMethod();
        String url = response.getRequest().getURI().toString();
        LOG.error("ZeppelinHub {} {} returned with status {} ", method, url, code);
        throw new IOException("Cannot perform " + method + " request to ZeppelinHub");
    }
    return data;
}
Also used : Response(org.eclipse.jetty.client.api.Response) InputStreamResponseListener(org.eclipse.jetty.client.util.InputStreamResponseListener) InputStream(java.io.InputStream) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 55 with Response

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

the class ExternalSiteTest method testExternalSiteWrongProtocol.

@Test
public void testExternalSiteWrongProtocol() throws Exception {
    String host = "github.com";
    // SSH port
    int port = 22;
    // Verify that we have connectivity
    assumeCanConnectTo(host, port);
    for (int i = 0; i < 2; ++i) {
        final CountDownLatch latch = new CountDownLatch(3);
        client.newRequest(host, port).onResponseFailure(new Response.FailureListener() {

            @Override
            public void onFailure(Response response, Throwable failure) {
                latch.countDown();
            }
        }).send(new Response.Listener.Adapter() {

            @Override
            public void onFailure(Response response, Throwable failure) {
                latch.countDown();
            }

            @Override
            public void onComplete(Result result) {
                Assert.assertTrue(result.isFailed());
                latch.countDown();
            }
        });
        Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) CountDownLatch(java.util.concurrent.CountDownLatch) Result(org.eclipse.jetty.client.api.Result) Test(org.junit.Test)

Aggregations

Response (org.eclipse.jetty.client.api.Response)113 Test (org.junit.Test)90 HttpServletResponse (javax.servlet.http.HttpServletResponse)89 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)84 IOException (java.io.IOException)77 HttpServletRequest (javax.servlet.http.HttpServletRequest)70 ServletException (javax.servlet.ServletException)68 CountDownLatch (java.util.concurrent.CountDownLatch)54 Result (org.eclipse.jetty.client.api.Result)51 InputStream (java.io.InputStream)36 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)36 ByteBuffer (java.nio.ByteBuffer)34 ServletOutputStream (javax.servlet.ServletOutputStream)34 Request (org.eclipse.jetty.client.api.Request)31 InterruptedIOException (java.io.InterruptedIOException)29 HttpServlet (javax.servlet.http.HttpServlet)29 InputStreamResponseListener (org.eclipse.jetty.client.util.InputStreamResponseListener)29 Request (org.eclipse.jetty.server.Request)26 BufferingResponseListener (org.eclipse.jetty.client.util.BufferingResponseListener)23 Callback (org.eclipse.jetty.util.Callback)22