Search in sources :

Example 41 with HttpHeaders

use of com.google.api.client.http.HttpHeaders in project feign by OpenFeign.

the class GoogleHttpClient method convertResponse.

private final Response convertResponse(final Request inputRequest, final HttpResponse inputResponse) throws IOException {
    final HttpHeaders headers = inputResponse.getHeaders();
    Integer contentLength = null;
    if (headers.getContentLength() != null && headers.getContentLength() <= Integer.MAX_VALUE) {
        contentLength = inputResponse.getHeaders().getContentLength().intValue();
    }
    return Response.builder().body(inputResponse.getContent(), contentLength).status(inputResponse.getStatusCode()).reason(inputResponse.getStatusMessage()).headers(toMap(inputResponse.getHeaders())).request(inputRequest).build();
}
Also used : HttpHeaders(com.google.api.client.http.HttpHeaders)

Example 42 with HttpHeaders

use of com.google.api.client.http.HttpHeaders in project feign by OpenFeign.

the class GoogleHttpClient method convertRequest.

private final HttpRequest convertRequest(final Request inputRequest, final Request.Options options) throws IOException {
    // Setup the request body
    HttpContent content = null;
    if (inputRequest.length() > 0) {
        final Collection<String> contentTypeValues = inputRequest.headers().get("Content-Type");
        String contentType = null;
        if (contentTypeValues != null && contentTypeValues.size() > 0) {
            contentType = contentTypeValues.iterator().next();
        } else {
            contentType = "application/octet-stream";
        }
        content = new ByteArrayContent(contentType, inputRequest.body());
    }
    // Build the request
    final HttpRequest request = requestFactory.buildRequest(inputRequest.httpMethod().name(), new GenericUrl(inputRequest.url()), content);
    // Setup headers
    final HttpHeaders headers = new HttpHeaders();
    for (final Map.Entry<String, Collection<String>> header : inputRequest.headers().entrySet()) {
        headers.set(header.getKey(), header.getValue());
    }
    // Some servers don't do well with no Accept header
    if (inputRequest.headers().get("Accept") == null) {
        headers.setAccept("*/*");
    }
    request.setHeaders(headers);
    // Setup request options
    request.setReadTimeout(options.readTimeoutMillis()).setConnectTimeout(options.connectTimeoutMillis()).setFollowRedirects(options.isFollowRedirects()).setThrowExceptionOnExecuteError(false);
    return request;
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) HttpHeaders(com.google.api.client.http.HttpHeaders) Collection(java.util.Collection) GenericUrl(com.google.api.client.http.GenericUrl) ByteArrayContent(com.google.api.client.http.ByteArrayContent) HashMap(java.util.HashMap) Map(java.util.Map) HttpContent(com.google.api.client.http.HttpContent)

Example 43 with HttpHeaders

use of com.google.api.client.http.HttpHeaders in project scout.rt by eclipse.

the class HttpServiceTunnel method executeRequest.

/**
 * Execute a {@link ServiceTunnelRequest}, returns the plain {@link HttpResponse} - (executed and) ready to be
 * processed to create a {@link ServiceTunnelResponse}.
 *
 * @param call
 *          the original call
 * @param callData
 *          the data created by the {@link IServiceTunnelContentHandler} used by this tunnel Create url connection and
 *          write post data (if required)
 * @throws IOException
 *           override this method to customize the creation of the {@link HttpResponse} see
 *           {@link #addCustomHeaders(HttpRequest, ServiceTunnelRequest, byte[])}
 */
protected HttpResponse executeRequest(ServiceTunnelRequest call, byte[] callData) throws IOException {
    // fast check of wrong URL's for this tunnel
    if (!"http".equalsIgnoreCase(getServerUrl().getProtocol()) && !"https".equalsIgnoreCase(getServerUrl().getProtocol())) {
        throw new IOException("URL '" + getServerUrl().toString() + "' is not supported by this tunnel ('" + getClass().getName() + "').");
    }
    if (!isActive()) {
        String key = BEANS.get(ServiceTunnelTargetUrlProperty.class).getKey();
        throw new IllegalArgumentException("No target URL configured. Please specify a target URL in the config.properties using property '" + key + "'.");
    }
    HttpRequestFactory requestFactory = getHttpTransportManager().getHttpRequestFactory();
    HttpRequest request = requestFactory.buildPostRequest(getGenericUrl(), new ByteArrayContent(null, callData));
    HttpHeaders headers = request.getHeaders();
    headers.setCacheControl("no-cache");
    headers.setContentType("text/xml");
    headers.put("Pragma", "no-cache");
    addCustomHeaders(request, call, callData);
    return request.execute();
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) HttpHeaders(com.google.api.client.http.HttpHeaders) HttpRequestFactory(com.google.api.client.http.HttpRequestFactory) ServiceTunnelTargetUrlProperty(org.eclipse.scout.rt.shared.SharedConfigProperties.ServiceTunnelTargetUrlProperty) IOException(java.io.IOException) ByteArrayContent(com.google.api.client.http.ByteArrayContent)

Example 44 with HttpHeaders

use of com.google.api.client.http.HttpHeaders in project scout.rt by eclipse.

the class HttpProxy method writeRequestHeaders.

protected void writeRequestHeaders(HttpServletRequest req, HttpRequest httpReq) {
    Enumeration<String> headerNames = req.getHeaderNames();
    while (headerNames.hasMoreElements()) {
        String name = headerNames.nextElement();
        String value = req.getHeader(name);
        for (IHttpHeaderFilter filter : getRequestHeaderFilters()) {
            value = filter.filter(name, value);
        }
        if (value != null) {
            HttpHeaders headers = httpReq.getHeaders();
            headers.set(name, Collections.singletonList(value));
            LOG.trace("Wrote request header: {}: {}", name, value);
        } else {
            LOG.trace("Removed request header: {} (original value: {})", name, req.getHeader(name));
        }
    }
}
Also used : HttpHeaders(com.google.api.client.http.HttpHeaders)

Example 45 with HttpHeaders

use of com.google.api.client.http.HttpHeaders in project providence by morimekta.

the class SetHeadersInitializer method initialize.

@Override
public void initialize(HttpRequest request) {
    request.setConnectTimeout(connect_timeout);
    request.setReadTimeout(read_timeout);
    // With the interceptor will overwrite headers set by the Http client.
    request.setInterceptor(rq -> {
        HttpHeaders http = rq.getHeaders();
        for (Map.Entry<String, String> entry : headers.entrySet()) {
            String value = entry.getValue();
            switch(entry.getKey().toLowerCase()) {
                case "accept":
                    http.setAccept(value);
                    break;
                case "accept-encoding":
                    http.setAcceptEncoding(value);
                    break;
                case "authorization":
                    http.setAuthorization(value);
                    break;
                case "content-encoding":
                    http.setContentEncoding(value);
                    break;
                case "content-type":
                    http.setContentType(value);
                    break;
                case "user-agent":
                    http.setUserAgent(value);
                    break;
                default:
                    try {
                        http.set(entry.getKey(), value);
                    } catch (Exception e) {
                        // special method.
                        throw new IOException("Unable to set header " + entry.getKey() + ": " + e.getMessage(), e);
                    }
                    break;
            }
        }
    });
}
Also used : HttpHeaders(com.google.api.client.http.HttpHeaders) IOException(java.io.IOException) Map(java.util.Map) IOException(java.io.IOException)

Aggregations

HttpHeaders (com.google.api.client.http.HttpHeaders)46 HttpRequest (com.google.api.client.http.HttpRequest)22 IOException (java.io.IOException)21 GenericUrl (com.google.api.client.http.GenericUrl)13 HttpResponse (com.google.api.client.http.HttpResponse)13 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)10 GoogleJsonError (com.google.api.client.googleapis.json.GoogleJsonError)7 HttpRequestInitializer (com.google.api.client.http.HttpRequestInitializer)6 Test (org.junit.Test)6 HttpContent (com.google.api.client.http.HttpContent)5 Objects (com.google.api.services.storage.model.Objects)5 HttpResponseException (com.google.api.client.http.HttpResponseException)4 HttpTransport (com.google.api.client.http.HttpTransport)4 JsonObjectParser (com.google.api.client.json.JsonObjectParser)4 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)4 MockHttpTransport (com.google.api.client.testing.http.MockHttpTransport)4 SocketTimeoutException (java.net.SocketTimeoutException)4 Map (java.util.Map)4 GoogleCredential (com.google.api.client.googleapis.auth.oauth2.GoogleCredential)3 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)3