Search in sources :

Example 51 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 52 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)

Example 53 with HttpHeaders

use of com.google.api.client.http.HttpHeaders in project beam by apache.

the class GcsUtil method enqueueDelete.

private void enqueueDelete(final GcsPath file, BatchInterface batch) throws IOException {
    Storage.Objects.Delete deleteRequest = storageClient.objects().delete(file.getBucket(), file.getObject());
    batch.queue(deleteRequest, new JsonBatchCallback<Void>() {

        @Override
        public void onSuccess(Void obj, HttpHeaders responseHeaders) {
            LOG.debug("Successfully deleted {}", file);
        }

        @Override
        public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders) throws IOException {
            if (e.getCode() == 404) {
                LOG.info("Ignoring failed deletion of file {} which already does not exist: {}", file, e);
            } else {
                throw new IOException(String.format("Error trying to delete %s: %s", file, e));
            }
        }
    });
}
Also used : HttpHeaders(com.google.api.client.http.HttpHeaders) Objects(com.google.api.services.storage.model.Objects) GoogleJsonError(com.google.api.client.googleapis.json.GoogleJsonError) IOException(java.io.IOException)

Example 54 with HttpHeaders

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

the class GerritApiTransportImpl method getHttpRequestFactory.

/**
 * TODO(malcon): Consolidate GitHub and this one in one class
 */
private HttpRequestFactory getHttpRequestFactory(@Nullable UserPassword userPassword) throws RepoException, ValidationException {
    return httpTransport.createRequestFactory(request -> {
        request.setConnectTimeout((int) Duration.ofMinutes(1).toMillis());
        request.setReadTimeout((int) Duration.ofMinutes(1).toMillis());
        HttpHeaders httpHeaders = new HttpHeaders();
        if (userPassword != null) {
            httpHeaders.setBasicAuthentication(userPassword.getUsername(), userPassword.getPassword_BeCareful());
        }
        request.setHeaders(httpHeaders);
        request.setParser(new JsonObjectParser(JSON_FACTORY));
    });
}
Also used : HttpHeaders(com.google.api.client.http.HttpHeaders) JsonObjectParser(com.google.api.client.json.JsonObjectParser)

Example 55 with HttpHeaders

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

the class GitHubApiTransportImpl method maybeGetLinkHeader.

@SuppressWarnings("unchecked")
@Nullable
private static String maybeGetLinkHeader(HttpResponse response) {
    HttpHeaders headers = response.getHeaders();
    List<String> link = (List<String>) headers.get("Link");
    if (link == null) {
        return null;
    }
    return Iterables.getOnlyElement(link);
}
Also used : HttpHeaders(com.google.api.client.http.HttpHeaders) List(java.util.List) Nullable(javax.annotation.Nullable)

Aggregations

HttpHeaders (com.google.api.client.http.HttpHeaders)63 HttpRequest (com.google.api.client.http.HttpRequest)32 IOException (java.io.IOException)26 GenericUrl (com.google.api.client.http.GenericUrl)22 HttpResponse (com.google.api.client.http.HttpResponse)21 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)15 Test (org.junit.Test)8 GoogleJsonError (com.google.api.client.googleapis.json.GoogleJsonError)7 ByteArrayContent (com.google.api.client.http.ByteArrayContent)6 HttpContent (com.google.api.client.http.HttpContent)6 HttpRequestInitializer (com.google.api.client.http.HttpRequestInitializer)6 HttpResponseException (com.google.api.client.http.HttpResponseException)6 JsonObjectParser (com.google.api.client.json.JsonObjectParser)5 Map (java.util.Map)5 HttpTransport (com.google.api.client.http.HttpTransport)4 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)4 MockHttpTransport (com.google.api.client.testing.http.MockHttpTransport)4 Objects (com.google.api.services.storage.model.Objects)4 SocketTimeoutException (java.net.SocketTimeoutException)4 URI (java.net.URI)4