Search in sources :

Example 1 with GzipSource

use of okio.GzipSource in project zipkin by openzipkin.

the class ZipkinRuleTest method readSpans_gzippedResponse.

@Test
public void readSpans_gzippedResponse() throws Exception {
    char[] annotation2K = new char[2048];
    Arrays.fill(annotation2K, 'a');
    List<Span> trace = asList(TRACE.get(0).toBuilder().addAnnotation(Annotation.create(System.currentTimeMillis(), new String(annotation2K), null)).build());
    zipkin.storeSpans(trace);
    Response response = client.newCall(new Request.Builder().url(format("%s/api/v1/trace/%016x", zipkin.httpUrl(), traceId)).addHeader("Accept-Encoding", "gzip").build()).execute();
    assertThat(response.code()).isEqualTo(200);
    assertThat(response.body().contentLength()).isLessThan(annotation2K.length);
    Buffer result = new Buffer();
    GzipSource source = new GzipSource(response.body().source());
    while (source.read(result, Integer.MAX_VALUE) != -1) ;
    byte[] unzipped = result.readByteArray();
    assertThat(Codec.JSON.readSpans(unzipped)).isEqualTo(trace);
}
Also used : Response(okhttp3.Response) Buffer(okio.Buffer) GzipSource(okio.GzipSource) Request(okhttp3.Request) ByteString(okio.ByteString) Span(zipkin.Span) Test(org.junit.Test)

Example 2 with GzipSource

use of okio.GzipSource in project okhttp by square.

the class BridgeInterceptor method intercept.

@Override
public Response intercept(Chain chain) throws IOException {
    Request userRequest = chain.request();
    Request.Builder requestBuilder = userRequest.newBuilder();
    RequestBody body = userRequest.body();
    if (body != null) {
        MediaType contentType = body.contentType();
        if (contentType != null) {
            requestBuilder.header("Content-Type", contentType.toString());
        }
        long contentLength = body.contentLength();
        if (contentLength != -1) {
            requestBuilder.header("Content-Length", Long.toString(contentLength));
            requestBuilder.removeHeader("Transfer-Encoding");
        } else {
            requestBuilder.header("Transfer-Encoding", "chunked");
            requestBuilder.removeHeader("Content-Length");
        }
    }
    if (userRequest.header("Host") == null) {
        requestBuilder.header("Host", hostHeader(userRequest.url(), false));
    }
    if (userRequest.header("Connection") == null) {
        requestBuilder.header("Connection", "Keep-Alive");
    }
    // If we add an "Accept-Encoding: gzip" header field we're responsible for also decompressing
    // the transfer stream.
    boolean transparentGzip = false;
    if (userRequest.header("Accept-Encoding") == null && userRequest.header("Range") == null) {
        transparentGzip = true;
        requestBuilder.header("Accept-Encoding", "gzip");
    }
    List<Cookie> cookies = cookieJar.loadForRequest(userRequest.url());
    if (!cookies.isEmpty()) {
        requestBuilder.header("Cookie", cookieHeader(cookies));
    }
    if (userRequest.header("User-Agent") == null) {
        requestBuilder.header("User-Agent", Version.userAgent());
    }
    Response networkResponse = chain.proceed(requestBuilder.build());
    HttpHeaders.receiveHeaders(cookieJar, userRequest.url(), networkResponse.headers());
    Response.Builder responseBuilder = networkResponse.newBuilder().request(userRequest);
    if (transparentGzip && "gzip".equalsIgnoreCase(networkResponse.header("Content-Encoding")) && HttpHeaders.hasBody(networkResponse)) {
        GzipSource responseBody = new GzipSource(networkResponse.body().source());
        Headers strippedHeaders = networkResponse.headers().newBuilder().removeAll("Content-Encoding").removeAll("Content-Length").build();
        responseBuilder.headers(strippedHeaders);
        responseBuilder.body(new RealResponseBody(strippedHeaders, Okio.buffer(responseBody)));
    }
    return responseBuilder.build();
}
Also used : Cookie(okhttp3.Cookie) Headers(okhttp3.Headers) Request(okhttp3.Request) Response(okhttp3.Response) GzipSource(okio.GzipSource) MediaType(okhttp3.MediaType) RequestBody(okhttp3.RequestBody)

Example 3 with GzipSource

use of okio.GzipSource in project okhttp by square.

the class PublicSuffixDatabaseTest method publicSuffixExceptions.

@Test
public void publicSuffixExceptions() throws IOException {
    InputStream resource = PublicSuffixDatabaseTest.class.getClassLoader().getResourceAsStream(PUBLIC_SUFFIX_RESOURCE);
    BufferedSource source = Okio.buffer(new GzipSource(Okio.source(resource)));
    int length = source.readInt();
    source.skip(length);
    length = source.readInt();
    Buffer buffer = new Buffer();
    buffer.write(source, length);
    resource.close();
    while (!buffer.exhausted()) {
        String exception = buffer.readUtf8LineStrict();
        assertEquals(exception, publicSuffixDatabase.getEffectiveTldPlusOne(exception));
        String test = "foobar." + exception;
        assertEquals(exception, publicSuffixDatabase.getEffectiveTldPlusOne(test));
    }
}
Also used : GzipSource(okio.GzipSource) Buffer(okio.Buffer) InputStream(java.io.InputStream) BufferedSource(okio.BufferedSource) Test(org.junit.Test)

Example 4 with GzipSource

use of okio.GzipSource in project OkVolley by googolmo.

the class OkNetwork method performRequest.

@Override
public NetworkResponse performRequest(Request<?> request) throws VolleyError {
    long requestStart = SystemClock.elapsedRealtime();
    while (true) {
        Response httpResponse = null;
        byte[] responseContents = null;
        Map<String, String> responseHeaders = Collections.emptyMap();
        try {
            // Gather headers.
            Map<String, String> headers = new HashMap<String, String>();
            addCacheHeaders(headers, request.getCacheEntry());
            httpResponse = mHttpStack.performRequest(request, headers);
            int statusCode = httpResponse.code();
            responseHeaders = new TreeMap<String, String>();
            for (String field : httpResponse.headers().names()) {
                responseHeaders.put(field, httpResponse.headers().get(field));
            }
            // Handle cache validation.
            if (statusCode == 304) {
                return new NetworkResponse(304, request.getCacheEntry().data, responseHeaders, true);
            }
            if (httpResponse.body() != null) {
                if (responseGzip(responseHeaders)) {
                    Buffer buffer = new Buffer();
                    GzipSource gzipSource = new GzipSource(httpResponse.body().source());
                    while (gzipSource.read(buffer, Integer.MAX_VALUE) != -1) {
                    }
                    responseContents = buffer.readByteArray();
                } else {
                    responseContents = httpResponse.body().bytes();
                }
            } else {
                responseContents = new byte[0];
            }
            //                // Some responses such as 204s do not have content.  We must check.
            //                if (httpResponse.getEntity() != null) {
            //                    responseContents = entityToBytes(httpResponse.getEntity()
            //                            , responseGzip(responseHeaders));
            //                } else {
            //                    // Add 0 byte response as a way of honestly representing a
            //                    // no-content request.
            //                    responseContents = new byte[0];
            //                }
            // if the request is slow, log it.
            long requestLifetime = SystemClock.elapsedRealtime() - requestStart;
            logSlowRequests(requestLifetime, request, responseContents, httpResponse);
            if (statusCode < 200 || statusCode > 299) {
                throw new IOException();
            }
            return new NetworkResponse(statusCode, responseContents, responseHeaders, false);
        } catch (SocketTimeoutException e) {
            attemptRetryOnException("socket", request, new TimeoutError());
        } catch (ConnectTimeoutException e) {
            attemptRetryOnException("connection", request, new TimeoutError());
        } catch (MalformedURLException e) {
            throw new RuntimeException("Bad URL " + request.getUrl(), e);
        } catch (IOException e) {
            int statusCode;
            NetworkResponse networkResponse = null;
            if (httpResponse != null) {
                statusCode = httpResponse.code();
            } else {
                throw new NoConnectionError(e);
            }
            VolleyLog.e("Unexpected response code %d for %s", statusCode, request.getUrl());
            if (responseContents != null) {
                networkResponse = new NetworkResponse(statusCode, responseContents, responseHeaders, false);
                if (statusCode == 401 || statusCode == 403) {
                    attemptRetryOnException("auth", request, new AuthFailureError(networkResponse));
                } else {
                    // TODO: Only throw ServerError for 5xx status codes.
                    throw new ServerError(networkResponse);
                }
            } else {
                throw new NetworkError(networkResponse);
            }
        }
    }
}
Also used : Buffer(okio.Buffer) MalformedURLException(java.net.MalformedURLException) HashMap(java.util.HashMap) ServerError(com.android.volley.ServerError) AuthFailureError(com.android.volley.AuthFailureError) NetworkError(com.android.volley.NetworkError) NoConnectionError(com.android.volley.NoConnectionError) IOException(java.io.IOException) TimeoutError(com.android.volley.TimeoutError) Response(com.squareup.okhttp.Response) NetworkResponse(com.android.volley.NetworkResponse) GzipSource(okio.GzipSource) SocketTimeoutException(java.net.SocketTimeoutException) NetworkResponse(com.android.volley.NetworkResponse) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException)

Example 5 with GzipSource

use of okio.GzipSource in project zipkin by openzipkin.

the class ZipkinDispatcher method dispatch.

@Override
public MockResponse dispatch(RecordedRequest request) {
    HttpUrl url = server.url(request.getPath());
    if (request.getMethod().equals("GET")) {
        if (url.encodedPath().equals("/health")) {
            return new MockResponse().setBody("OK\n");
        } else if (url.encodedPath().equals("/api/v1/services")) {
            return jsonResponse(Codec.JSON.writeStrings(store.getServiceNames()));
        } else if (url.encodedPath().equals("/api/v1/spans")) {
            String serviceName = url.queryParameter("serviceName");
            return jsonResponse(Codec.JSON.writeStrings(store.getSpanNames(serviceName)));
        } else if (url.encodedPath().equals("/api/v1/dependencies")) {
            Long endTs = maybeLong(url.queryParameter("endTs"));
            Long lookback = maybeLong(url.queryParameter("lookback"));
            List<DependencyLink> result = store.getDependencies(endTs, lookback);
            return jsonResponse(Codec.JSON.writeDependencyLinks(result));
        } else if (url.encodedPath().equals("/api/v1/traces")) {
            QueryRequest queryRequest = toQueryRequest(url);
            return jsonResponse(Codec.JSON.writeTraces(store.getTraces(queryRequest)));
        } else if (url.encodedPath().startsWith("/api/v1/trace/")) {
            String traceIdHex = url.encodedPath().replace("/api/v1/trace/", "");
            long traceIdHigh = traceIdHex.length() == 32 ? lowerHexToUnsignedLong(traceIdHex, 0) : 0L;
            long traceIdLow = lowerHexToUnsignedLong(traceIdHex);
            List<Span> trace = url.queryParameterNames().contains("raw") ? store.getRawTrace(traceIdHigh, traceIdLow) : store.getTrace(traceIdHigh, traceIdLow);
            if (trace != null)
                return jsonResponse(Codec.JSON.writeSpans(trace));
        }
    } else if (request.getMethod().equals("POST")) {
        if (url.encodedPath().equals("/api/v1/spans")) {
            metrics.incrementMessages();
            byte[] body = request.getBody().readByteArray();
            String encoding = request.getHeader("Content-Encoding");
            if (encoding != null && encoding.contains("gzip")) {
                try {
                    Buffer result = new Buffer();
                    GzipSource source = new GzipSource(new Buffer().write(body));
                    while (source.read(result, Integer.MAX_VALUE) != -1) ;
                    body = result.readByteArray();
                } catch (IOException e) {
                    metrics.incrementMessagesDropped();
                    return new MockResponse().setResponseCode(400).setBody("Cannot gunzip spans");
                }
            }
            String type = request.getHeader("Content-Type");
            Codec codec = type != null && type.contains("/x-thrift") ? Codec.THRIFT : Codec.JSON;
            final MockResponse result = new MockResponse();
            consumer.acceptSpans(body, codec, new Callback<Void>() {

                @Override
                public void onSuccess(Void value) {
                    result.setResponseCode(202);
                }

                @Override
                public void onError(Throwable t) {
                    String message = t.getMessage();
                    result.setBody(message).setResponseCode(message.startsWith("Cannot store") ? 500 : 400);
                }
            });
            return result;
        }
    } else {
        // unsupported method
        return new MockResponse().setResponseCode(405);
    }
    return new MockResponse().setResponseCode(404);
}
Also used : Buffer(okio.Buffer) MockResponse(okhttp3.mockwebserver.MockResponse) QueryRequest(zipkin.storage.QueryRequest) IOException(java.io.IOException) HttpUrl(okhttp3.HttpUrl) GzipSource(okio.GzipSource) Codec(zipkin.Codec) Callback(zipkin.storage.Callback) Util.lowerHexToUnsignedLong(zipkin.internal.Util.lowerHexToUnsignedLong) List(java.util.List)

Aggregations

GzipSource (okio.GzipSource)7 Buffer (okio.Buffer)5 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 BufferedSource (okio.BufferedSource)3 Test (org.junit.Test)3 Request (okhttp3.Request)2 Response (okhttp3.Response)2 AuthFailureError (com.android.volley.AuthFailureError)1 NetworkError (com.android.volley.NetworkError)1 NetworkResponse (com.android.volley.NetworkResponse)1 NoConnectionError (com.android.volley.NoConnectionError)1 ServerError (com.android.volley.ServerError)1 TimeoutError (com.android.volley.TimeoutError)1 Response (com.squareup.okhttp.Response)1 MalformedURLException (java.net.MalformedURLException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Cookie (okhttp3.Cookie)1