Search in sources :

Example 11 with ParseHttpRequest

use of com.parse.http.ParseHttpRequest in project Parse-SDK-Android by ParsePlatform.

the class ParseDecompressInterceptor method intercept.

@Override
public ParseHttpResponse intercept(Chain chain) throws IOException {
    ParseHttpRequest request = chain.getRequest();
    ParseHttpResponse response = chain.proceed(request);
    // If the response is gziped, we need to decompress the stream and remove the gzip header.
    if (GZIP_ENCODING.equalsIgnoreCase(response.getHeader(CONTENT_ENCODING_HEADER))) {
        Map<String, String> newHeaders = new HashMap<>(response.getAllHeaders());
        newHeaders.remove(CONTENT_ENCODING_HEADER);
        // Since before we decompress the stream, we can not know the actual length of the stream.
        // In this situation, we follow the OkHttp library, set the content-length of the response
        // to -1
        newHeaders.put(CONTENT_LENGTH_HEADER, "-1");
        // TODO(mengyan): Add builder constructor based on an existing ParseHttpResponse
        response = new ParseHttpResponse.Builder(response).setTotalSize(-1).setHeaders(newHeaders).setContent(new GZIPInputStream(response.getContent())).build();
    }
    return response;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) ParseHttpRequest(com.parse.http.ParseHttpRequest) HashMap(java.util.HashMap) ParseHttpResponse(com.parse.http.ParseHttpResponse)

Example 12 with ParseHttpRequest

use of com.parse.http.ParseHttpRequest in project Parse-SDK-Android by ParsePlatform.

the class ParseOkHttpClient method getRequest.

@Override
/* package */
Request getRequest(ParseHttpRequest parseRequest) throws IOException {
    Request.Builder okHttpRequestBuilder = new Request.Builder();
    ParseHttpRequest.Method method = parseRequest.getMethod();
    // Set method
    switch(method) {
        case GET:
            okHttpRequestBuilder.get();
            break;
        case DELETE:
            okHttpRequestBuilder.delete();
            break;
        case POST:
        case PUT:
            // the following.
            break;
        default:
            // ParseRequest.newRequest().
            throw new IllegalStateException("Unsupported http method " + method.toString());
    }
    // Set url
    okHttpRequestBuilder.url(parseRequest.getUrl());
    // Set Header
    Headers.Builder okHttpHeadersBuilder = new Headers.Builder();
    for (Map.Entry<String, String> entry : parseRequest.getAllHeaders().entrySet()) {
        okHttpHeadersBuilder.add(entry.getKey(), entry.getValue());
    }
    // OkHttp automatically add gzip header so we do not need to deal with it
    Headers okHttpHeaders = okHttpHeadersBuilder.build();
    okHttpRequestBuilder.headers(okHttpHeaders);
    // Set Body
    ParseHttpBody parseBody = parseRequest.getBody();
    ParseOkHttpRequestBody okHttpRequestBody = null;
    if (parseBody != null) {
        okHttpRequestBody = new ParseOkHttpRequestBody(parseBody);
    }
    switch(method) {
        case PUT:
            okHttpRequestBuilder.put(okHttpRequestBody);
            break;
        case POST:
            okHttpRequestBuilder.post(okHttpRequestBody);
            break;
    }
    return okHttpRequestBuilder.build();
}
Also used : ParseHttpRequest(com.parse.http.ParseHttpRequest) Headers(okhttp3.Headers) ParseHttpBody(com.parse.http.ParseHttpBody) Request(okhttp3.Request) ParseHttpRequest(com.parse.http.ParseHttpRequest) HashMap(java.util.HashMap) Map(java.util.Map)

Example 13 with ParseHttpRequest

use of com.parse.http.ParseHttpRequest in project Parse-SDK-Android by ParsePlatform.

the class ParseOkHttpClient method addExternalInterceptor.

/**
   * For OKHttpClient, since it does not expose any interface for us to check the raw response
   * stream, we have to use OKHttp networkInterceptors. Instead of using our own interceptor list,
   * we use OKHttp inner interceptor list.
   * @param parseNetworkInterceptor
   */
@Override
/* package */
void addExternalInterceptor(final ParseNetworkInterceptor parseNetworkInterceptor) {
    OkHttpClient.Builder builder = okHttpClient.newBuilder();
    builder.networkInterceptors().add(new Interceptor() {

        @Override
        public Response intercept(final Chain okHttpChain) throws IOException {
            Request okHttpRequest = okHttpChain.request();
            // Transfer OkHttpRequest to ParseHttpRequest
            final ParseHttpRequest parseRequest = getParseHttpRequest(okHttpRequest);
            // Capture OkHttpResponse
            final Capture<Response> okHttpResponseCapture = new Capture<>();
            final ParseHttpResponse parseResponse = parseNetworkInterceptor.intercept(new ParseNetworkInterceptor.Chain() {

                @Override
                public ParseHttpRequest getRequest() {
                    return parseRequest;
                }

                @Override
                public ParseHttpResponse proceed(ParseHttpRequest parseRequest) throws IOException {
                    // Use OKHttpClient to send request
                    Request okHttpRequest = ParseOkHttpClient.this.getRequest(parseRequest);
                    Response okHttpResponse = okHttpChain.proceed(okHttpRequest);
                    okHttpResponseCapture.set(okHttpResponse);
                    return getResponse(okHttpResponse);
                }
            });
            final Response okHttpResponse = okHttpResponseCapture.get();
            // Ideally we should build newOkHttpResponse only based on parseResponse, however
            // ParseHttpResponse does not have all the info we need to build the newOkHttpResponse, so
            // we rely on the okHttpResponse to generate the builder and change the necessary info
            // inside
            Response.Builder newOkHttpResponseBuilder = okHttpResponse.newBuilder();
            // Set status
            newOkHttpResponseBuilder.code(parseResponse.getStatusCode()).message(parseResponse.getReasonPhrase());
            // Set headers
            if (parseResponse.getAllHeaders() != null) {
                for (Map.Entry<String, String> entry : parseResponse.getAllHeaders().entrySet()) {
                    newOkHttpResponseBuilder.header(entry.getKey(), entry.getValue());
                }
            }
            // Set body
            newOkHttpResponseBuilder.body(new ResponseBody() {

                @Override
                public MediaType contentType() {
                    if (parseResponse.getContentType() == null) {
                        return null;
                    }
                    return MediaType.parse(parseResponse.getContentType());
                }

                @Override
                public long contentLength() {
                    return parseResponse.getTotalSize();
                }

                @Override
                public BufferedSource source() {
                    // interceptor.
                    if (parseResponse.getContent() == null) {
                        return null;
                    }
                    return Okio.buffer(Okio.source(parseResponse.getContent()));
                }
            });
            return newOkHttpResponseBuilder.build();
        }
    });
    okHttpClient = builder.build();
}
Also used : ParseHttpRequest(com.parse.http.ParseHttpRequest) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) ParseHttpRequest(com.parse.http.ParseHttpRequest) IOException(java.io.IOException) Capture(bolts.Capture) ResponseBody(okhttp3.ResponseBody) Response(okhttp3.Response) ParseHttpResponse(com.parse.http.ParseHttpResponse) Interceptor(okhttp3.Interceptor) ParseNetworkInterceptor(com.parse.http.ParseNetworkInterceptor) ParseHttpResponse(com.parse.http.ParseHttpResponse)

Example 14 with ParseHttpRequest

use of com.parse.http.ParseHttpRequest in project Parse-SDK-Android by ParsePlatform.

the class ParseRESTCommand method newRequest.

@Override
protected ParseHttpRequest newRequest(ParseHttpRequest.Method method, String url, ProgressCallback uploadProgressCallback) {
    ParseHttpRequest request;
    if (jsonParameters != null && method != ParseHttpRequest.Method.POST && method != ParseHttpRequest.Method.PUT) {
        // The request URI may be too long to include parameters in the URI.
        // To avoid this problem we send the parameters in a POST request json-encoded body
        // and add a http method override parameter in newBody.
        request = super.newRequest(ParseHttpRequest.Method.POST, url, uploadProgressCallback);
    } else {
        request = super.newRequest(method, url, uploadProgressCallback);
    }
    ParseHttpRequest.Builder requestBuilder = new ParseHttpRequest.Builder(request);
    addAdditionalHeaders(requestBuilder);
    return requestBuilder.build();
}
Also used : ParseHttpRequest(com.parse.http.ParseHttpRequest)

Example 15 with ParseHttpRequest

use of com.parse.http.ParseHttpRequest in project Parse-SDK-Android by ParsePlatform.

the class ParseDecompressInterceptorTest method testDecompressInterceptorWithNotGZIPResponse.

@Test
public void testDecompressInterceptorWithNotGZIPResponse() throws Exception {
    ParseDecompressInterceptor interceptor = new ParseDecompressInterceptor();
    final String responseContent = "content";
    ParseHttpResponse interceptedResponse = interceptor.intercept(new ParseNetworkInterceptor.Chain() {

        @Override
        public ParseHttpRequest getRequest() {
            // Generate test request
            return new ParseHttpRequest.Builder().setUrl("www.parse.com").setMethod(ParseHttpRequest.Method.GET).build();
        }

        @Override
        public ParseHttpResponse proceed(ParseHttpRequest request) throws IOException {
            // Generate test response
            return new ParseHttpResponse.Builder().setStatusCode(200).setTotalSize(responseContent.length()).setReasonPhrase("Success").setContentType("text/plain").setContent(new ByteArrayInputStream(responseContent.getBytes())).build();
        }
    });
    // Verify response is correct
    assertEquals(200, interceptedResponse.getStatusCode());
    assertEquals(responseContent.length(), interceptedResponse.getTotalSize());
    assertEquals("Success", interceptedResponse.getReasonPhrase());
    assertEquals("text/plain", interceptedResponse.getContentType());
    byte[] content = ParseIOUtils.toByteArray(interceptedResponse.getContent());
    assertArrayEquals(responseContent.getBytes(), content);
}
Also used : ParseHttpRequest(com.parse.http.ParseHttpRequest) ParseNetworkInterceptor(com.parse.http.ParseNetworkInterceptor) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) ParseHttpResponse(com.parse.http.ParseHttpResponse) Test(org.junit.Test)

Aggregations

ParseHttpRequest (com.parse.http.ParseHttpRequest)19 ParseHttpResponse (com.parse.http.ParseHttpResponse)10 HashMap (java.util.HashMap)9 Test (org.junit.Test)9 ParseNetworkInterceptor (com.parse.http.ParseNetworkInterceptor)6 Request (okhttp3.Request)6 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)6 IOException (java.io.IOException)5 ParseHttpBody (com.parse.http.ParseHttpBody)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Map (java.util.Map)3 GZIPOutputStream (java.util.zip.GZIPOutputStream)3 Headers (okhttp3.Headers)3 MockResponse (okhttp3.mockwebserver.MockResponse)3 Buffer (okio.Buffer)3 Semaphore (java.util.concurrent.Semaphore)2 Response (okhttp3.Response)2 MockWebServer (okhttp3.mockwebserver.MockWebServer)2 JSONObject (org.json.JSONObject)2