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");
}
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;
}
}
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);
}
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;
}
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));
}
}
Aggregations