Search in sources :

Example 1 with HEADER_ACCEPT

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);
    }
}
Also used : OutputStream(java.io.OutputStream) HttpURLConnection(java.net.HttpURLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) MalformedURLException(java.net.MalformedURLException) GZIP(org.projectnessie.client.http.HttpUtils.GZIP) IOException(java.io.IOException) Method(org.projectnessie.client.http.HttpClient.Method) HEADER_CONTENT_TYPE(org.projectnessie.client.http.HttpUtils.HEADER_CONTENT_TYPE) StandardCharsets(java.nio.charset.StandardCharsets) ProtocolException(java.net.ProtocolException) List(java.util.List) ACCEPT_ENCODING(org.projectnessie.client.http.HttpUtils.ACCEPT_ENCODING) HEADER_ACCEPT(org.projectnessie.client.http.HttpUtils.HEADER_ACCEPT) HEADER_CONTENT_ENCODING(org.projectnessie.client.http.HttpUtils.HEADER_CONTENT_ENCODING) SocketTimeoutException(java.net.SocketTimeoutException) BiConsumer(java.util.function.BiConsumer) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) HEADER_ACCEPT_ENCODING(org.projectnessie.client.http.HttpUtils.HEADER_ACCEPT_ENCODING) GZIPOutputStream(java.util.zip.GZIPOutputStream) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) URI(java.net.URI) MalformedURLException(java.net.MalformedURLException) OutputStream(java.io.OutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) URI(java.net.URI) HttpURLConnection(java.net.HttpURLConnection) GZIPOutputStream(java.util.zip.GZIPOutputStream) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) List(java.util.List) ProtocolException(java.net.ProtocolException) IOException(java.io.IOException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ProtocolException(java.net.ProtocolException) SocketTimeoutException(java.net.SocketTimeoutException) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) SocketTimeoutException(java.net.SocketTimeoutException) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) BiConsumer(java.util.function.BiConsumer)

Example 2 with HEADER_ACCEPT

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);
}
Also used : HttpClientBuilder(org.projectnessie.client.http.HttpClientBuilder) Assumptions.assumeThat(org.assertj.core.api.Assumptions.assumeThat) HttpAuthentication(org.projectnessie.client.http.HttpAuthentication) InmemoryDatabaseAdapterFactory(org.projectnessie.versioned.persist.inmem.InmemoryDatabaseAdapterFactory) NessieDbAdapterName(org.projectnessie.versioned.persist.tests.extension.NessieDbAdapterName) InmemoryTestConnectionProviderSource(org.projectnessie.versioned.persist.inmem.InmemoryTestConnectionProviderSource) NessieExternalDatabase(org.projectnessie.versioned.persist.tests.extension.NessieExternalDatabase) NessieApiV1(org.projectnessie.client.api.NessieApiV1) RequestFilter(org.projectnessie.client.http.RequestFilter) HEADER_ACCEPT(org.projectnessie.client.http.HttpUtils.HEADER_ACCEPT) URI(java.net.URI) Nullable(javax.annotation.Nullable) HttpClient(org.projectnessie.client.http.HttpClient) HttpAuthentication(org.projectnessie.client.http.HttpAuthentication) RequestFilter(org.projectnessie.client.http.RequestFilter) NessieApiV1(org.projectnessie.client.api.NessieApiV1)

Aggregations

URI (java.net.URI)2 HEADER_ACCEPT (org.projectnessie.client.http.HttpUtils.HEADER_ACCEPT)2 JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)1 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 MalformedURLException (java.net.MalformedURLException)1 ProtocolException (java.net.ProtocolException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 StandardCharsets (java.nio.charset.StandardCharsets)1 List (java.util.List)1 BiConsumer (java.util.function.BiConsumer)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1 Nullable (javax.annotation.Nullable)1 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)1 Assumptions.assumeThat (org.assertj.core.api.Assumptions.assumeThat)1 NessieApiV1 (org.projectnessie.client.api.NessieApiV1)1 HttpAuthentication (org.projectnessie.client.http.HttpAuthentication)1 HttpClient (org.projectnessie.client.http.HttpClient)1