use of org.projectnessie.client.http.HttpUtils.HEADER_ACCEPT in project nessie by projectnessie.
the class HttpRequest method executeRequest.
private HttpResponse executeRequest(Method method, Object body) throws HttpClientException {
URI uri = uriBuilder.build();
try {
HttpURLConnection con = (HttpURLConnection) uri.toURL().openConnection();
con.setReadTimeout(config.getReadTimeoutMillis());
con.setConnectTimeout(config.getConnectionTimeoutMillis());
if (con instanceof HttpsURLConnection) {
((HttpsURLConnection) con).setSSLSocketFactory(config.getSslContext().getSocketFactory());
}
RequestContext context = new RequestContext(headers, uri, method, body);
ResponseContext responseContext = new ResponseContextImpl(con);
try {
headers.put(HEADER_ACCEPT, accept);
boolean postOrPut = method.equals(Method.PUT) || method.equals(Method.POST);
if (postOrPut) {
// Need to set the Content-Type even if body==null, otherwise the server responds with
// RESTEASY003065: Cannot consume content type
headers.put(HEADER_CONTENT_TYPE, contentsType);
}
boolean doesOutput = postOrPut && body != null;
if (!config.isDisableCompression()) {
headers.put(HEADER_ACCEPT_ENCODING, ACCEPT_ENCODING);
if (doesOutput) {
headers.put(HEADER_CONTENT_ENCODING, GZIP);
}
}
config.getRequestFilters().forEach(a -> a.filter(context));
headers.applyTo(con);
con.setRequestMethod(method.name());
if (doesOutput) {
con.setDoOutput(true);
OutputStream out = con.getOutputStream();
try {
if (!config.isDisableCompression()) {
out = new GZIPOutputStream(out);
}
Class<?> bodyType = body.getClass();
if (bodyType != String.class) {
config.getMapper().writerFor(bodyType).writeValue(out, body);
} else {
// This is mostly used for testing bad/broken JSON
out.write(((String) body).getBytes(StandardCharsets.UTF_8));
}
} finally {
out.close();
}
}
con.connect();
// call to ensure http request is complete
con.getResponseCode();
List<BiConsumer<ResponseContext, Exception>> callbacks = context.getResponseCallbacks();
if (callbacks != null) {
callbacks.forEach(callback -> callback.accept(responseContext, null));
}
} catch (IOException e) {
List<BiConsumer<ResponseContext, Exception>> callbacks = context.getResponseCallbacks();
if (callbacks != null) {
callbacks.forEach(callback -> callback.accept(null, e));
}
throw e;
}
config.getResponseFilters().forEach(responseFilter -> responseFilter.filter(responseContext));
return new HttpResponse(responseContext, config.getMapper());
} catch (ProtocolException e) {
throw new HttpClientException(String.format("Cannot perform request against '%s'. Invalid protocol %s", uri, method), e);
} catch (JsonGenerationException | JsonMappingException e) {
throw new HttpClientException(String.format("Cannot serialize body of %s request against '%s'. Unable to serialize %s", method, uri, body.getClass()), e);
} catch (MalformedURLException e) {
throw new HttpClientException(String.format("Cannot perform %s request. Malformed Url for %s", method, uriBuilder.build()), e);
} catch (SocketTimeoutException e) {
throw new HttpClientReadTimeoutException(String.format("Cannot finish %s request against '%s'. Timeout while waiting for response with a timeout of %ds", method, uri, config.getReadTimeoutMillis() / 1000), e);
} catch (IOException e) {
throw new HttpClientException(String.format("Failed to execute %s request against '%s'.", method, uri), e);
}
}
use of org.projectnessie.client.http.HttpUtils.HEADER_ACCEPT in project nessie by projectnessie.
the class TestJerseyRestNaiveClientInMemory method init.
@Override
protected void init(NessieApiV1 api, @Nullable HttpClient.Builder httpClient, URI uri) {
assumeThat(httpClient).isNotNull();
// Intentionally remove the `Accept` header from requests.
// Service endpoints should declare the content type for their return values,
// which should allow the Web Container to properly format output even in the absence
// of `Accept` HTTP headers.
RequestFilter noAcceptFilter = context -> context.removeHeader(HEADER_ACCEPT);
httpClient.addRequestFilter(noAcceptFilter);
api = HttpClientBuilder.builder().withAuthentication((HttpAuthentication) client -> client.addRequestFilter(noAcceptFilter)).withUri(httpClient.getBaseUri()).build(NessieApiV1.class);
super.init(api, httpClient, uri);
}
Aggregations