/** Create a new instance for the specified URI. */ public HttpRequestBuilder(String clientName, URI uri) { this.uri = uri; this.entry = new HttpLogEntry() .withRequestUri(uri) .withClientName(clientName) .withMethod(method); }
@Override public void call(Throwable throwable) { update(entry, throwable); HttpLogEntry.logClientRequest(entry); } })
private void update(HttpLogEntry entry, Throwable t) { boolean canRetry = (t instanceof ConnectException || t instanceof ReadTimeoutException); entry.mark("received-error").withException(t).withCanRetry(canRetry); }
/** Set the attempt if redirect occurs, should only be used after the initial request. */ public HttpLogEntry withRedirect(URI loc) { reset(redirect + 1); return withRequestUri(loc); }
entry.withRequestContentLength(entity.length).mark("start"); if (entity.length > 0) { con.setDoOutput(true); entry.mark("complete").withStatusCode(status); for (Map.Entry<String, List<String>> h : headers.entrySet()) { for (String v : h.getValue()) { entry.withResponseHeader(h.getKey(), v); entry.withResponseContentLength(data.length); return new HttpResponse(status, headers, data); entry.mark("complete").withException(e); throw e; } finally { entry.withCanRetry(canRetry); HttpLogEntry.logClientRequest(entry);
/** Create a log entry for an rxnetty request. */ public static HttpLogEntry create(HttpClientRequest<ByteBuf> req) { HttpLogEntry entry = new HttpLogEntry() .withMethod(req.getMethod().name()) .withRequestUri(URI.create(req.getUri())) .withRequestContentLength(req.getHeaders().getContentLength(-1)); for (Map.Entry<String, String> h : req.getHeaders().entries()) { entry.withRequestHeader(h.getKey(), h.getValue()); } return entry; }
private static void update(HttpLogEntry entry, HttpClientResponse<ByteBuf> res) { int code = res.getStatus().code(); boolean canRetry = (code == 429 || code >= 500); entry.mark("received-response") .withStatusCode(code) .withStatusReason(res.getStatus().reasonPhrase()) .withResponseContentLength(res.getHeaders().getContentLength(-1)) .withCanRetry(canRetry); for (Map.Entry<String, String> h : res.getHeaders().entries()) { entry.withResponseHeader(h.getKey(), h.getValue()); } }
@Test public void npeIfMethodIsNotSet() { Assertions.assertThrows(NullPointerException.class, () -> { HttpLogEntry entry = new HttpLogEntry() .withClientName("test") .withRequestUri(URI.create("http://test.com/foo")); try { HttpLogEntry.logClientRequest(entry); } catch (NullPointerException e) { Assertions.assertEquals("parameter 'method' cannot be null", e.getMessage()); throw e; } }); } }
private static HttpLogEntry create(ClientConfig cfg, HttpClientRequest<ByteBuf> req) { return create(req) .withClientName(cfg.name()) .withOriginalUri(cfg.originalUri()) .withMaxAttempts(cfg.numRetries() + 1); }
HttpResponse response = null; for (int attempt = 1; attempt <= numAttempts; ++attempt) { entry.withAttempt(attempt); try { response = sendImpl();
/** Set the URI for the actual http request. */ public HttpLogEntry withRequestUri(URI uri) { return withRequestUri(uri.toString(), uri.getPath()); }
/** * Execute an HTTP request. * * @param context * Context associated with the request. * @return * Observable with the response of the request. */ Observable<HttpClientResponse<ByteBuf>> execute(final RequestContext context) { final HttpLogEntry entry = context.entry(); final HttpClient<ByteBuf, ByteBuf> client = getClient(context); entry.mark("start"); entry.withRemoteAddr(context.server().host()); entry.withRemotePort(context.server().port()); return client.submit(context.request()) .doOnNext(new Action1<HttpClientResponse<ByteBuf>>() { @Override public void call(HttpClientResponse<ByteBuf> res) { update(entry, res); HttpLogEntry.logClientRequest(entry); } }) .doOnError(new Action1<Throwable>() { @Override public void call(Throwable throwable) { update(entry, throwable); HttpLogEntry.logClientRequest(entry); } }) .doOnTerminate(Actions.empty()); }
/** Set the request method (GET, PUT, POST, DELETE). */ public HttpRequestBuilder withMethod(String m) { this.method = m; entry.withMethod(method); return this; }
/** How many times to retry if the intial attempt fails? */ public HttpRequestBuilder withRetries(int n) { Preconditions.checkArg(n >= 0, "number of retries must be >= 0"); this.numAttempts = n + 1; entry.withMaxAttempts(numAttempts); return this; }
entry.withRequestContentLength(entity.length).mark("start"); if (entity.length > 0) { con.setDoOutput(true); entry.mark("complete").withStatusCode(status); for (Map.Entry<String, List<String>> h : headers.entrySet()) { for (String v : h.getValue()) { entry.withResponseHeader(h.getKey(), v); entry.withResponseContentLength(data.length); return new HttpResponse(status, headers, data); entry.mark("complete").withException(e); throw e; } finally { entry.withCanRetry(canRetry); HttpLogEntry.logClientRequest(entry);
/** Set the attempt if redirect occurs, should only be used after the initial request. */ public HttpLogEntry withRedirect(URI loc) { reset(redirect + 1); return withRequestUri(loc); }
HttpResponse response = null; for (int attempt = 1; attempt <= numAttempts; ++attempt) { entry.withAttempt(attempt); try { response = sendImpl();
/** Set the URI for the actual http request. */ public HttpLogEntry withRequestUri(URI uri) { return withRequestUri(uri.toString(), uri.getPath()); }
/** Set the request method (GET, PUT, POST, DELETE). */ public HttpRequestBuilder withMethod(String m) { this.method = m; entry.withMethod(method); return this; }
/** How many times to retry if the intial attempt fails? */ public HttpRequestBuilder withRetries(int n) { Preconditions.checkArg(n >= 0, "number of retries must be >= 0"); this.numAttempts = n + 1; entry.withMaxAttempts(numAttempts); return this; }