Search in sources :

Example 1 with ACCEPT_ENCODING

use of org.projectnessie.client.http.HttpUtils.ACCEPT_ENCODING 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)

Aggregations

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 URI (java.net.URI)1 StandardCharsets (java.nio.charset.StandardCharsets)1 List (java.util.List)1 BiConsumer (java.util.function.BiConsumer)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)1 Method (org.projectnessie.client.http.HttpClient.Method)1 ACCEPT_ENCODING (org.projectnessie.client.http.HttpUtils.ACCEPT_ENCODING)1 GZIP (org.projectnessie.client.http.HttpUtils.GZIP)1 HEADER_ACCEPT (org.projectnessie.client.http.HttpUtils.HEADER_ACCEPT)1 HEADER_ACCEPT_ENCODING (org.projectnessie.client.http.HttpUtils.HEADER_ACCEPT_ENCODING)1 HEADER_CONTENT_ENCODING (org.projectnessie.client.http.HttpUtils.HEADER_CONTENT_ENCODING)1