Search in sources :

Example 6 with ResponseCache

use of java.net.ResponseCache in project robovm by robovm.

the class URLConnectionTest method backdoorUrlToUri.

/**
     * Exercises HttpURLConnection to convert URL to a URI. Unlike URL#toURI,
     * HttpURLConnection recovers from URLs with unescaped but unsupported URI
     * characters like '{' and '|' by escaping these characters.
     */
private URI backdoorUrlToUri(URL url) throws Exception {
    final AtomicReference<URI> uriReference = new AtomicReference<URI>();
    ResponseCache.setDefault(new ResponseCache() {

        @Override
        public CacheRequest put(URI uri, URLConnection connection) throws IOException {
            return null;
        }

        @Override
        public CacheResponse get(URI uri, String requestMethod, Map<String, List<String>> requestHeaders) throws IOException {
            uriReference.set(uri);
            throw new UnsupportedOperationException();
        }
    });
    try {
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.getResponseCode();
    } catch (Exception expected) {
    }
    return uriReference.get();
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) URI(java.net.URI) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) HttpRetryException(java.net.HttpRetryException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) SSLException(javax.net.ssl.SSLException) SocketTimeoutException(java.net.SocketTimeoutException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) UnknownHostException(java.net.UnknownHostException) ProtocolException(java.net.ProtocolException) CacheResponse(java.net.CacheResponse) HttpURLConnection(java.net.HttpURLConnection) CacheRequest(java.net.CacheRequest) List(java.util.List) ArrayList(java.util.ArrayList) HttpResponseCache(com.android.okhttp.HttpResponseCache) ResponseCache(java.net.ResponseCache)

Example 7 with ResponseCache

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

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 8 with ResponseCache

use of java.net.ResponseCache in project enroscar by stanfy.

the class ResponseCacheSwitcher method restoreUrlConnection.

public static void restoreUrlConnection(final URLConnection connection) {
    final ResponseCache cache = ResponseCache.getDefault();
    if (!(cache instanceof ResponseCacheSwitcher)) {
        return;
    }
    final ResponseCacheSwitcher hub = (ResponseCacheSwitcher) cache;
    final LinkedList<URLConnection> stack = hub.currentUrlConnection.get();
    if (!stack.isEmpty() && stack.peek() == connection) {
        stack.removeFirst();
    } else {
        final String message = "Bad call to restoreUrlConnection(): " + (stack.isEmpty() ? "stack is empty" : "connection does not match");
        Log.w(TAG, message);
        if (DebugFlags.STRICT_MODE) {
            throw new RuntimeException(message);
        }
    }
}
Also used : ResponseCache(java.net.ResponseCache) URLConnection(java.net.URLConnection)

Example 9 with ResponseCache

use of java.net.ResponseCache in project j2objc by google.

the class URLConnectionTest method testResponseCacheReturnsNullOutputStream.

// TODO(tball): b/28067294
// public void testHostWithNul() throws Exception {
// URL url = new URL("http://host\u0000/");
// try {
// url.openStream();
// fail();
// } catch (UnknownHostException expected) {}
// }
/**
 * Don't explode if the cache returns a null body. http://b/3373699
 */
public void testResponseCacheReturnsNullOutputStream() throws Exception {
    final AtomicBoolean aborted = new AtomicBoolean();
    ResponseCache.setDefault(new ResponseCache() {

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

        @Override
        public CacheRequest put(URI uri, URLConnection connection) throws IOException {
            return new CacheRequest() {

                @Override
                public void abort() {
                    aborted.set(true);
                }

                @Override
                public OutputStream getBody() throws IOException {
                    return null;
                }
            };
        }
    });
    server.enqueue(new MockResponse().setBody("abcdef"));
    server.play();
    HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
    InputStream in = connection.getInputStream();
    assertEquals("abc", readAscii(in, 3));
    in.close();
    // The best behavior is ambiguous, but RI 6 doesn't abort here
    assertFalse(aborted.get());
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) URI(java.net.URI) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CacheResponse(java.net.CacheResponse) HttpURLConnection(java.net.HttpURLConnection) CacheRequest(java.net.CacheRequest) List(java.util.List) ArrayList(java.util.ArrayList) ResponseCache(java.net.ResponseCache)

Example 10 with ResponseCache

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

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 java.io.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.shimResponseCache;
        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(okhttp3.AndroidShimResponseCache) ResponseCache(java.net.ResponseCache) AndroidShimResponseCache(okhttp3.AndroidShimResponseCache)

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