Search in sources :

Example 11 with CacheResponse

use of java.net.CacheResponse 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 12 with CacheResponse

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

the class HttpEngine method initResponseSource.

/**
   * Initialize the source for this response. It may be corrected later if the
   * request headers forbids network use.
   */
private void initResponseSource() throws IOException {
    responseSource = ResponseSource.NETWORK;
    if (!policy.getUseCaches())
        return;
    OkResponseCache responseCache = client.getOkResponseCache();
    if (responseCache == null)
        return;
    CacheResponse candidate = responseCache.get(uri, method, requestHeaders.getHeaders().toMultimap(false));
    if (candidate == null)
        return;
    Map<String, List<String>> responseHeadersMap = candidate.getHeaders();
    cachedResponseBody = candidate.getBody();
    if (!acceptCacheResponseType(candidate) || responseHeadersMap == null || cachedResponseBody == null) {
        Util.closeQuietly(cachedResponseBody);
        return;
    }
    RawHeaders rawResponseHeaders = RawHeaders.fromMultimap(responseHeadersMap, true);
    cachedResponseHeaders = new ResponseHeaders(uri, rawResponseHeaders);
    long now = System.currentTimeMillis();
    this.responseSource = cachedResponseHeaders.chooseResponseSource(now, requestHeaders);
    if (responseSource == ResponseSource.CACHE) {
        this.cacheResponse = candidate;
        setResponse(cachedResponseHeaders, cachedResponseBody);
    } else if (responseSource == ResponseSource.CONDITIONAL_CACHE) {
        this.cacheResponse = candidate;
    } else if (responseSource == ResponseSource.NETWORK) {
        Util.closeQuietly(cachedResponseBody);
    } else {
        throw new AssertionError();
    }
}
Also used : CacheResponse(java.net.CacheResponse) OkResponseCache(com.squareup.okhttp.OkResponseCache) List(java.util.List)

Example 13 with CacheResponse

use of java.net.CacheResponse in project phonegap-facebook-plugin by Wizcorp.

the class HttpEngine method initResponseSource.

/**
   * Initialize the source for this response. It may be corrected later if the
   * request headers forbids network use.
   */
private void initResponseSource() throws IOException {
    responseSource = ResponseSource.NETWORK;
    if (!policy.getUseCaches() || policy.responseCache == null) {
        return;
    }
    CacheResponse candidate = policy.responseCache.get(uri, method, requestHeaders.getHeaders().toMultimap(false));
    if (candidate == null) {
        return;
    }
    Map<String, List<String>> responseHeadersMap = candidate.getHeaders();
    cachedResponseBody = candidate.getBody();
    if (!acceptCacheResponseType(candidate) || responseHeadersMap == null || cachedResponseBody == null) {
        Util.closeQuietly(cachedResponseBody);
        return;
    }
    RawHeaders rawResponseHeaders = RawHeaders.fromMultimap(responseHeadersMap, true);
    cachedResponseHeaders = new ResponseHeaders(uri, rawResponseHeaders);
    long now = System.currentTimeMillis();
    this.responseSource = cachedResponseHeaders.chooseResponseSource(now, requestHeaders);
    if (responseSource == ResponseSource.CACHE) {
        this.cacheResponse = candidate;
        setResponse(cachedResponseHeaders, cachedResponseBody);
    } else if (responseSource == ResponseSource.CONDITIONAL_CACHE) {
        this.cacheResponse = candidate;
    } else if (responseSource == ResponseSource.NETWORK) {
        Util.closeQuietly(cachedResponseBody);
    } else {
        throw new AssertionError();
    }
}
Also used : CacheResponse(java.net.CacheResponse) List(java.util.List)

Example 14 with CacheResponse

use of java.net.CacheResponse 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 15 with CacheResponse

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

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) URISyntaxException(java.net.URISyntaxException) SocketTimeoutException(java.net.SocketTimeoutException) HttpRetryException(java.net.HttpRetryException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) ProtocolException(java.net.ProtocolException) CacheResponse(java.net.CacheResponse) HttpURLConnection(java.net.HttpURLConnection) CacheRequest(java.net.CacheRequest) ArrayList(java.util.ArrayList) List(java.util.List) ResponseCache(java.net.ResponseCache)

Aggregations

CacheResponse (java.net.CacheResponse)20 List (java.util.List)17 URI (java.net.URI)10 SecureCacheResponse (java.net.SecureCacheResponse)9 CacheRequest (java.net.CacheRequest)7 Test (org.junit.Test)7 HttpURLConnection (java.net.HttpURLConnection)6 ResponseCache (java.net.ResponseCache)6 URLConnection (java.net.URLConnection)6 Request (okhttp3.Request)6 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 Response (okhttp3.Response)5 LinkedHashMap (java.util.LinkedHashMap)4 Headers (okhttp3.Headers)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ProtocolException (java.net.ProtocolException)3 Certificate (java.security.cert.Certificate)3 Handshake (okhttp3.Handshake)3 ResponseBody (okhttp3.ResponseBody)3