Search in sources :

Example 1 with ResponseCache

use of java.net.ResponseCache in project android_frameworks_base by ParanoidAndroid.

the class HttpResponseCacheTest method testGetInstalledWithWrongTypeInstalled.

public void testGetInstalledWithWrongTypeInstalled() {
    ResponseCache.setDefault(new ResponseCache() {

        @Override
        public CacheResponse get(URI uri, String requestMethod, Map<String, List<String>> requestHeaders) {
            return null;
        }

        @Override
        public CacheRequest put(URI uri, URLConnection connection) {
            return null;
        }
    });
    assertNull(HttpResponseCache.getInstalled());
}
Also used : CacheResponse(java.net.CacheResponse) CacheRequest(java.net.CacheRequest) List(java.util.List) ResponseCache(java.net.ResponseCache) URI(java.net.URI) URLConnection(java.net.URLConnection)

Example 2 with ResponseCache

use of java.net.ResponseCache in project android_frameworks_base by ResurrectionRemix.

the class HttpResponseCache method install.

/**
     * Creates a new HTTP response cache and sets it as the system default cache.
     *
     * @param directory the directory to hold cache data.
     * @param maxSize the maximum size of the cache in bytes.
     * @return the newly-installed cache
     * @throws IOException if {@code directory} cannot be used for this cache.
     *     Most applications should respond to this exception by logging a
     *     warning.
     */
public static synchronized HttpResponseCache install(File directory, long maxSize) throws IOException {
    ResponseCache installed = ResponseCache.getDefault();
    if (installed instanceof HttpResponseCache) {
        HttpResponseCache installedResponseCache = (HttpResponseCache) installed;
        // don't close and reopen if an equivalent cache is already installed
        AndroidShimResponseCache trueResponseCache = installedResponseCache.delegate;
        if (trueResponseCache.isEquivalent(directory, maxSize)) {
            return installedResponseCache;
        } else {
            // The HttpResponseCache that owns this object is about to be replaced.
            trueResponseCache.close();
        }
    }
    AndroidShimResponseCache trueResponseCache = AndroidShimResponseCache.create(directory, maxSize);
    HttpResponseCache newResponseCache = new HttpResponseCache(trueResponseCache);
    ResponseCache.setDefault(newResponseCache);
    return newResponseCache;
}
Also used : AndroidShimResponseCache(com.android.okhttp.AndroidShimResponseCache) ResponseCache(java.net.ResponseCache) AndroidShimResponseCache(com.android.okhttp.AndroidShimResponseCache)

Example 3 with ResponseCache

use of java.net.ResponseCache in project okhttp by square.

the class CacheAdapterTest method put_httpsGet.

@Test
public void put_httpsGet() throws Exception {
    final URL serverUrl = configureHttpsServer(new MockResponse());
    assertEquals("https", serverUrl.getProtocol());
    ResponseCache responseCache = new AbstractResponseCache() {

        @Override
        public CacheRequest put(URI uri, URLConnection connection) throws IOException {
            try {
                assertTrue(connection instanceof HttpsURLConnection);
                assertEquals(toUri(serverUrl), uri);
                assertEquals(serverUrl, connection.getURL());
                HttpsURLConnection cacheHttpsUrlConnection = (HttpsURLConnection) connection;
                HttpsURLConnection realHttpsUrlConnection = (HttpsURLConnection) CacheAdapterTest.this.connection;
                assertEquals(realHttpsUrlConnection.getCipherSuite(), cacheHttpsUrlConnection.getCipherSuite());
                assertEquals(realHttpsUrlConnection.getPeerPrincipal(), cacheHttpsUrlConnection.getPeerPrincipal());
                assertArrayEquals(realHttpsUrlConnection.getLocalCertificates(), cacheHttpsUrlConnection.getLocalCertificates());
                assertArrayEquals(realHttpsUrlConnection.getServerCertificates(), cacheHttpsUrlConnection.getServerCertificates());
                assertEquals(realHttpsUrlConnection.getLocalPrincipal(), cacheHttpsUrlConnection.getLocalPrincipal());
                return null;
            } catch (Throwable t) {
                throw new IOException("unexpected cache failure", t);
            }
        }
    };
    setInternalCache(new CacheAdapter(responseCache));
    client = client.newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).hostnameVerifier(hostnameVerifier).build();
    connection = new OkUrlFactory(client).open(serverUrl);
    executeGet(connection);
}
Also used : AbstractResponseCache(okhttp3.AbstractResponseCache) OkUrlFactory(okhttp3.OkUrlFactory) MockResponse(okhttp3.mockwebserver.MockResponse) IOException(java.io.IOException) ResponseCache(java.net.ResponseCache) AbstractResponseCache(okhttp3.AbstractResponseCache) URI(java.net.URI) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Test(org.junit.Test)

Example 4 with ResponseCache

use of java.net.ResponseCache in project okhttp by square.

the class CacheAdapterTest method get_httpGet.

@Test
public void get_httpGet() throws Exception {
    final URL serverUrl = configureServer(new MockResponse());
    assertEquals("http", serverUrl.getProtocol());
    ResponseCache responseCache = new AbstractResponseCache() {

        @Override
        public CacheResponse get(URI uri, String method, Map<String, List<String>> headers) throws IOException {
            try {
                assertEquals(toUri(serverUrl), uri);
                assertEquals("GET", method);
                assertTrue("Arbitrary standard header not present", headers.containsKey("User-Agent"));
                assertEquals(Collections.singletonList("value1"), headers.get("key1"));
                return null;
            } catch (Throwable t) {
                throw new IOException("unexpected cache failure", t);
            }
        }
    };
    setInternalCache(new CacheAdapter(responseCache));
    connection = new OkUrlFactory(client).open(serverUrl);
    connection.setRequestProperty("key1", "value1");
    executeGet(connection);
}
Also used : AbstractResponseCache(okhttp3.AbstractResponseCache) OkUrlFactory(okhttp3.OkUrlFactory) MockResponse(okhttp3.mockwebserver.MockResponse) IOException(java.io.IOException) ResponseCache(java.net.ResponseCache) AbstractResponseCache(okhttp3.AbstractResponseCache) URI(java.net.URI) Map(java.util.Map) URL(java.net.URL) Test(org.junit.Test)

Example 5 with ResponseCache

use of java.net.ResponseCache in project okhttp by square.

the class CacheAdapterTest method get_httpsGet.

@Test
public void get_httpsGet() throws Exception {
    final URL serverUrl = configureHttpsServer(new MockResponse());
    assertEquals("https", serverUrl.getProtocol());
    ResponseCache responseCache = new AbstractResponseCache() {

        @Override
        public CacheResponse get(URI uri, String method, Map<String, List<String>> headers) throws IOException {
            try {
                assertEquals("https", uri.getScheme());
                assertEquals(toUri(serverUrl), uri);
                assertEquals("GET", method);
                assertTrue("Arbitrary standard header not present", headers.containsKey("User-Agent"));
                assertEquals(Collections.singletonList("value1"), headers.get("key1"));
                return null;
            } catch (Throwable t) {
                throw new IOException("unexpected cache failure", t);
            }
        }
    };
    setInternalCache(new CacheAdapter(responseCache));
    client = client.newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).hostnameVerifier(hostnameVerifier).build();
    connection = new OkUrlFactory(client).open(serverUrl);
    connection.setRequestProperty("key1", "value1");
    executeGet(connection);
}
Also used : AbstractResponseCache(okhttp3.AbstractResponseCache) OkUrlFactory(okhttp3.OkUrlFactory) MockResponse(okhttp3.mockwebserver.MockResponse) IOException(java.io.IOException) ResponseCache(java.net.ResponseCache) AbstractResponseCache(okhttp3.AbstractResponseCache) URI(java.net.URI) Map(java.util.Map) URL(java.net.URL) Test(org.junit.Test)

Aggregations

ResponseCache (java.net.ResponseCache)22 URI (java.net.URI)11 URLConnection (java.net.URLConnection)11 IOException (java.io.IOException)9 HttpURLConnection (java.net.HttpURLConnection)7 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)7 CacheRequest (java.net.CacheRequest)6 CacheResponse (java.net.CacheResponse)6 List (java.util.List)6 Test (org.junit.Test)6 AndroidShimResponseCache (com.android.okhttp.AndroidShimResponseCache)5 URL (java.net.URL)5 AbstractResponseCache (okhttp3.AbstractResponseCache)5 OkUrlFactory (okhttp3.OkUrlFactory)5 MockResponse (okhttp3.mockwebserver.MockResponse)5 ArrayList (java.util.ArrayList)4 HttpResponseCache (com.android.okhttp.HttpResponseCache)2 MockResponse (com.google.mockwebserver.MockResponse)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 InputStream (java.io.InputStream)2