Search in sources :

Example 66 with Headers

use of com.amazonaws.services.s3.Headers in project scribejava by scribejava.

the class OkHttpHttpClient method convertResponse.

static Response convertResponse(okhttp3.Response okHttpResponse) {
    final Headers headers = okHttpResponse.headers();
    final Map<String, String> headersMap = new HashMap<>();
    for (String headerName : headers.names()) {
        headersMap.put(headerName, headers.get(headerName));
    }
    final ResponseBody body = okHttpResponse.body();
    return new Response(okHttpResponse.code(), okHttpResponse.message(), headersMap, body == null ? null : body.byteStream());
}
Also used : Response(com.github.scribejava.core.model.Response) HashMap(java.util.HashMap) Headers(okhttp3.Headers) ResponseBody(okhttp3.ResponseBody)

Example 67 with Headers

use of com.amazonaws.services.s3.Headers in project apollo by spotify.

the class HttpClient method send.

@Override
public CompletionStage<com.spotify.apollo.Response<ByteString>> send(com.spotify.apollo.Request apolloRequest, Optional<com.spotify.apollo.Request> apolloIncomingRequest) {
    final Optional<RequestBody> requestBody = apolloRequest.payload().map(payload -> {
        final MediaType contentType = apolloRequest.header("Content-Type").map(MediaType::parse).orElse(DEFAULT_CONTENT_TYPE);
        return RequestBody.create(contentType, payload);
    });
    final Headers.Builder headersBuilder = new Headers.Builder();
    apolloRequest.headers().asMap().forEach(headersBuilder::add);
    apolloIncomingRequest.flatMap(req -> req.header(AUTHORIZATION_HEADER)).ifPresent(header -> {
        if (headersBuilder.get(AUTHORIZATION_HEADER) == null) {
            headersBuilder.add(AUTHORIZATION_HEADER, header);
        }
    });
    final Request request = new Request.Builder().method(apolloRequest.method(), requestBody.orElse(null)).url(apolloRequest.uri()).headers(headersBuilder.build()).build();
    final CompletableFuture<com.spotify.apollo.Response<ByteString>> result = new CompletableFuture<>();
    // https://github.com/square/okhttp/wiki/Recipes#per-call-configuration
    final OkHttpClient finalClient;
    if (apolloRequest.ttl().isPresent() && client.readTimeoutMillis() != apolloRequest.ttl().get().toMillis()) {
        finalClient = client.newBuilder().readTimeout(apolloRequest.ttl().get().toMillis(), TimeUnit.MILLISECONDS).build();
    } else {
        finalClient = client;
    }
    finalClient.newCall(request).enqueue(TransformingCallback.create(result));
    return result;
}
Also used : IncomingRequestAwareClient(com.spotify.apollo.environment.IncomingRequestAwareClient) TimeUnit(java.util.concurrent.TimeUnit) Inject(javax.inject.Inject) Request(okhttp3.Request) CompletionStage(java.util.concurrent.CompletionStage) OkHttpClient(okhttp3.OkHttpClient) ByteString(okio.ByteString) Optional(java.util.Optional) CompletableFuture(java.util.concurrent.CompletableFuture) Headers(okhttp3.Headers) MediaType(okhttp3.MediaType) RequestBody(okhttp3.RequestBody) OkHttpClient(okhttp3.OkHttpClient) Headers(okhttp3.Headers) Request(okhttp3.Request) CompletableFuture(java.util.concurrent.CompletableFuture) MediaType(okhttp3.MediaType) RequestBody(okhttp3.RequestBody)

Example 68 with Headers

use of com.amazonaws.services.s3.Headers in project incubator-skywalking by apache.

the class RealCallInterceptor method beforeMethod.

/**
 * Get the {@link okhttp3.Request} from {@link EnhancedInstance}, then create {@link AbstractSpan} and set host,
 * port, kind, component, url from {@link okhttp3.Request}.
 * Through the reflection of the way, set the http header of context data into {@link okhttp3.Request#headers}.
 *
 * @param method
 * @param result change this result, if you want to truncate the method.
 * @throws Throwable
 */
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
    Request request = (Request) objInst.getSkyWalkingDynamicField();
    ContextCarrier contextCarrier = new ContextCarrier();
    HttpUrl requestUrl = request.url();
    AbstractSpan span = ContextManager.createExitSpan(requestUrl.uri().getPath(), contextCarrier, requestUrl.host() + ":" + requestUrl.port());
    span.setComponent(ComponentsDefine.OKHTTP);
    Tags.HTTP.METHOD.set(span, request.method());
    Tags.URL.set(span, requestUrl.uri().toString());
    SpanLayer.asHttp(span);
    Field headersField = Request.class.getDeclaredField("headers");
    Field modifiersField = Field.class.getDeclaredField("modifiers");
    modifiersField.setAccessible(true);
    modifiersField.setInt(headersField, headersField.getModifiers() & ~Modifier.FINAL);
    headersField.setAccessible(true);
    Headers.Builder headerBuilder = request.headers().newBuilder();
    CarrierItem next = contextCarrier.items();
    while (next.hasNext()) {
        next = next.next();
        headerBuilder.add(next.getHeadKey(), next.getHeadValue());
    }
    headersField.set(request, headerBuilder.build());
}
Also used : Field(java.lang.reflect.Field) ContextCarrier(org.apache.skywalking.apm.agent.core.context.ContextCarrier) CarrierItem(org.apache.skywalking.apm.agent.core.context.CarrierItem) Headers(okhttp3.Headers) Request(okhttp3.Request) HttpUrl(okhttp3.HttpUrl) AbstractSpan(org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan)

Example 69 with Headers

use of com.amazonaws.services.s3.Headers in project spring-cloud-netflix by spring-cloud.

the class OkHttpStatusCodeExceptionTest method getResponse.

@Test
public void getResponse() throws Exception {
    Headers headers = new Headers.Builder().add("foo", "bar").build();
    Response response = new Response.Builder().code(200).headers(headers).code(200).message("Success").body(ResponseBody.create(MediaType.parse("text/plain"), "foo")).protocol(Protocol.HTTP_1_1).request(new Request.Builder().url("http://service.com").build()).build();
    ResponseBody body = response.peekBody(Integer.MAX_VALUE);
    OkHttpStatusCodeException ex = new OkHttpStatusCodeException("service", response, body, new URI("http://service.com"));
    assertEquals(headers, ex.getResponse().headers());
    assertEquals(200, ex.getResponse().code());
    assertEquals("Success", ex.getResponse().message());
    assertEquals("foo", ex.getResponse().body().string());
    assertEquals(Protocol.HTTP_1_1, ex.getResponse().protocol());
}
Also used : Response(okhttp3.Response) Headers(okhttp3.Headers) URI(java.net.URI) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Example 70 with Headers

use of com.amazonaws.services.s3.Headers in project edx-app-android by edx.

the class NoCacheHeaderStrippingInterceptor method intercept.

@NonNull
@Override
public Response intercept(@NonNull final Chain chain) throws IOException {
    final Request request = chain.request();
    final Response response = chain.proceed(request);
    final Headers headers = response.headers();
    Headers.Builder strippedHeadersBuilder = null;
    List<String> headersToStrip = null;
    for (int i = 0, headersCount = headers.size(); i < headersCount; i++) {
        final String headerName = headers.name(i);
        final String headerValue = headers.value(i);
        if (headerName.equalsIgnoreCase("Cache-Control")) {
            Matcher directiveMatcher = PATTERN_NO_CACHE_HEADER.matcher(headerValue);
            if (directiveMatcher.find()) {
                if (strippedHeadersBuilder == null) {
                    strippedHeadersBuilder = new Headers.Builder();
                    for (int j = 0; j < i; j++) {
                        strippedHeadersBuilder.add(headers.name(j), headers.value(j));
                    }
                    headersToStrip = new ArrayList<>();
                }
                String newHeaderValue = headerValue;
                while (true) {
                    Collections.addAll(headersToStrip, directiveMatcher.group(GROUP_NO_CACHE_HEADERS).trim().split("\\s*,\\s*"));
                    final StringBuffer newHeaderValueBuffer = new StringBuffer();
                    directiveMatcher.appendReplacement(newHeaderValueBuffer, "$" + (directiveMatcher.group(GROUP_SEPARATOR_START).isEmpty() ? GROUP_SEPARATOR_END : GROUP_SEPARATOR_START));
                    directiveMatcher.appendTail(newHeaderValueBuffer);
                    newHeaderValue = newHeaderValueBuffer.toString();
                    directiveMatcher = PATTERN_NO_CACHE_HEADER.matcher(newHeaderValue);
                    if (!directiveMatcher.find())
                        break;
                }
                if (!newHeaderValue.isEmpty()) {
                    strippedHeadersBuilder.add(headerName, newHeaderValue);
                }
                continue;
            }
        }
        if (strippedHeadersBuilder != null) {
            strippedHeadersBuilder.add(headerName, headerValue);
        }
    }
    if (strippedHeadersBuilder == null) {
        return response;
    }
    final HttpUrl url = request.url();
    List<Cookie> cookies = null;
    for (final String headerToStrip : headersToStrip) {
        strippedHeadersBuilder.removeAll(headerToStrip);
        if (headerToStrip.equalsIgnoreCase("Set-Cookie")) {
            if (cookieJar != CookieJar.NO_COOKIES) {
                for (final String cookieString : headers.values(headerToStrip)) {
                    Cookie cookie = Cookie.parse(url, cookieString);
                    if (cookie != null) {
                        if (cookies == null) {
                            cookies = new ArrayList<>();
                        }
                        cookies.add(cookie);
                    }
                }
            }
        }
    }
    if (cookies != null) {
        cookieJar.saveFromResponse(url, cookies);
    }
    return response.newBuilder().headers(strippedHeadersBuilder.build()).build();
}
Also used : Cookie(okhttp3.Cookie) Matcher(java.util.regex.Matcher) Headers(okhttp3.Headers) Request(okhttp3.Request) HttpUrl(okhttp3.HttpUrl) Response(okhttp3.Response) NonNull(android.support.annotation.NonNull)

Aggregations

Headers (okhttp3.Headers)128 Request (okhttp3.Request)61 Response (okhttp3.Response)54 Test (org.junit.Test)40 IOException (java.io.IOException)32 Call (okhttp3.Call)30 RequestBody (okhttp3.RequestBody)25 CancelledException (com.hippo.ehviewer.client.exception.CancelledException)20 EhException (com.hippo.ehviewer.client.exception.EhException)20 NoHAtHClientException (com.hippo.ehviewer.client.exception.NoHAtHClientException)20 ParseException (com.hippo.ehviewer.client.exception.ParseException)20 StatusCodeException (com.hippo.network.StatusCodeException)20 ResponseBody (okhttp3.ResponseBody)18 HttpHeaders (okhttp3.internal.http.HttpHeaders)18 MediaType (okhttp3.MediaType)15 ServiceResponseWithHeaders (com.microsoft.rest.ServiceResponseWithHeaders)14 HeaderResponseBoolHeaders (fixtures.header.models.HeaderResponseBoolHeaders)14 HeaderResponseByteHeaders (fixtures.header.models.HeaderResponseByteHeaders)14 HeaderResponseDateHeaders (fixtures.header.models.HeaderResponseDateHeaders)14 HeaderResponseDatetimeHeaders (fixtures.header.models.HeaderResponseDatetimeHeaders)14