Search in sources :

Example 1 with ParseNetworkInterceptor

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

the class ParseClientConfigurationTest method testSetNetworkInterceptors.

@Test
public void testSetNetworkInterceptors() {
    final ParseNetworkInterceptor interceptorA = mock(ParseNetworkInterceptor.class);
    final ParseNetworkInterceptor interceptorB = mock(ParseNetworkInterceptor.class);
    Collection<ParseNetworkInterceptor> collectionA = new ArrayList<ParseNetworkInterceptor>() {

        {
            add(interceptorA);
            add(interceptorB);
        }
    };
    Collection<ParseNetworkInterceptor> collectionB = new ArrayList<ParseNetworkInterceptor>() {

        {
            add(interceptorB);
            add(interceptorA);
        }
    };
    Parse.Configuration.Builder builder = new Parse.Configuration.Builder(null);
    builder.setNetworkInterceptors(collectionA);
    Parse.Configuration configurationA = builder.build();
    builder.setNetworkInterceptors(collectionB);
    Parse.Configuration configurationB = builder.build();
    assertTrue(collectionsEqual(configurationA.interceptors, collectionA));
    assertTrue(collectionsEqual(configurationB.interceptors, collectionB));
}
Also used : ParseNetworkInterceptor(com.parse.http.ParseNetworkInterceptor) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 2 with ParseNetworkInterceptor

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

the class ParseOkHttpClientTest method testParseOkHttpClientExecuteWithExternalInterceptorAndGZIPResponse.

@Test
public void testParseOkHttpClientExecuteWithExternalInterceptorAndGZIPResponse() throws Exception {
    // Make mock response
    Buffer buffer = new Buffer();
    final ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    GZIPOutputStream gzipOut = new GZIPOutputStream(byteOut);
    gzipOut.write("content".getBytes());
    gzipOut.close();
    buffer.write(byteOut.toByteArray());
    MockResponse mockResponse = new MockResponse().setStatus("HTTP/1.1 " + 201 + " " + "OK").setBody(buffer).setHeader("Content-Encoding", "gzip");
    // Start mock server
    server.enqueue(mockResponse);
    server.start();
    ParseHttpClient client = new ParseOkHttpClient(10000, null);
    final Semaphore done = new Semaphore(0);
    // Add plain interceptor to disable decompress response stream
    client.addExternalInterceptor(new ParseNetworkInterceptor() {

        @Override
        public ParseHttpResponse intercept(Chain chain) throws IOException {
            done.release();
            ParseHttpResponse parseResponse = chain.proceed(chain.getRequest());
            // Make sure the response we get from the interceptor is the raw gzip stream
            byte[] content = ParseIOUtils.toByteArray(parseResponse.getContent());
            assertArrayEquals(byteOut.toByteArray(), content);
            // We need to set a new stream since we have read it
            return new ParseHttpResponse.Builder().setContent(new ByteArrayInputStream(byteOut.toByteArray())).build();
        }
    });
    // We do not need to add Accept-Encoding header manually, httpClient library should do that.
    String requestUrl = server.url("/").toString();
    ParseHttpRequest parseRequest = new ParseHttpRequest.Builder().setUrl(requestUrl).setMethod(ParseHttpRequest.Method.GET).build();
    // Execute request
    ParseHttpResponse parseResponse = client.execute(parseRequest);
    // Make sure the response we get is ungziped by OkHttp library
    byte[] content = ParseIOUtils.toByteArray(parseResponse.getContent());
    assertArrayEquals("content".getBytes(), content);
    // Make sure interceptor is called
    assertTrue(done.tryAcquire(10, TimeUnit.SECONDS));
    server.shutdown();
}
Also used : Buffer(okio.Buffer) MockResponse(okhttp3.mockwebserver.MockResponse) ParseHttpRequest(com.parse.http.ParseHttpRequest) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Semaphore(java.util.concurrent.Semaphore) IOException(java.io.IOException) GZIPOutputStream(java.util.zip.GZIPOutputStream) ParseNetworkInterceptor(com.parse.http.ParseNetworkInterceptor) ByteArrayInputStream(java.io.ByteArrayInputStream) ParseHttpResponse(com.parse.http.ParseHttpResponse) Test(org.junit.Test)

Example 3 with ParseNetworkInterceptor

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

the class Parse method initializeParseHttpClientsWithParseNetworkInterceptors.

// Initialize all necessary http clients and add interceptors to these http clients
private static void initializeParseHttpClientsWithParseNetworkInterceptors(List<ParseNetworkInterceptor> interceptors) {
    // This means developers have not called addInterceptor method so we should do nothing.
    if (interceptors == null) {
        return;
    }
    List<ParseHttpClient> clients = new ArrayList<>();
    // Rest http client
    clients.add(ParsePlugins.get().restClient());
    // AWS http client
    clients.add(ParseCorePlugins.getInstance().getFileController().awsClient());
    // Add interceptors to http clients
    for (ParseHttpClient parseHttpClient : clients) {
        // We need to add the decompress interceptor before the external interceptors to return
        // a decompressed response to Parse.
        parseHttpClient.addInternalInterceptor(new ParseDecompressInterceptor());
        for (ParseNetworkInterceptor interceptor : interceptors) {
            parseHttpClient.addExternalInterceptor(interceptor);
        }
    }
}
Also used : ParseNetworkInterceptor(com.parse.http.ParseNetworkInterceptor) ArrayList(java.util.ArrayList)

Example 4 with ParseNetworkInterceptor

use of com.parse.http.ParseNetworkInterceptor 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 5 with ParseNetworkInterceptor

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

the class ParseClientConfigurationTest method testNetworkInterceptors.

@Test
public void testNetworkInterceptors() {
    ParseNetworkInterceptor interceptorA = mock(ParseNetworkInterceptor.class);
    ParseNetworkInterceptor interceptorB = mock(ParseNetworkInterceptor.class);
    Parse.Configuration.Builder builder = new Parse.Configuration.Builder(null);
    builder.addNetworkInterceptor(interceptorA);
    Parse.Configuration configurationA = builder.build();
    builder.addNetworkInterceptor(interceptorB);
    Parse.Configuration configurationB = builder.build();
    assertFalse(configurationA.interceptors.contains(interceptorB));
    assertTrue(configurationB.interceptors.contains(interceptorB));
    try {
        configurationA.interceptors.add(interceptorB);
        fail("Interceptors shouldn't be mutable.");
    } catch (UnsupportedOperationException ex) {
    // Expected
    }
}
Also used : ParseNetworkInterceptor(com.parse.http.ParseNetworkInterceptor) Test(org.junit.Test)

Aggregations

ParseNetworkInterceptor (com.parse.http.ParseNetworkInterceptor)6 ParseHttpRequest (com.parse.http.ParseHttpRequest)3 ParseHttpResponse (com.parse.http.ParseHttpResponse)3 Test (org.junit.Test)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Semaphore (java.util.concurrent.Semaphore)2 Capture (bolts.Capture)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1 Interceptor (okhttp3.Interceptor)1 OkHttpClient (okhttp3.OkHttpClient)1 Request (okhttp3.Request)1 Response (okhttp3.Response)1 ResponseBody (okhttp3.ResponseBody)1 MockResponse (okhttp3.mockwebserver.MockResponse)1 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)1 Buffer (okio.Buffer)1