Search in sources :

Example 1 with HttpEngine

use of com.squareup.okhttp.internal.http.HttpEngine in project cordova-android-chromeview by thedracle.

the class HttpResponseCache method put.

@Override
public CacheRequest put(URI uri, URLConnection urlConnection) throws IOException {
    if (!(urlConnection instanceof HttpURLConnection)) {
        return null;
    }
    HttpURLConnection httpConnection = (HttpURLConnection) urlConnection;
    String requestMethod = httpConnection.getRequestMethod();
    String key = uriToKey(uri);
    if (requestMethod.equals("POST") || requestMethod.equals("PUT") || requestMethod.equals("DELETE")) {
        try {
            cache.remove(key);
        } catch (IOException ignored) {
        // The cache cannot be written.
        }
        return null;
    } else if (!requestMethod.equals("GET")) {
        // so is high and the benefit is low.
        return null;
    }
    HttpEngine httpEngine = getHttpEngine(httpConnection);
    if (httpEngine == null) {
        // Don't cache unless the HTTP implementation is ours.
        return null;
    }
    ResponseHeaders response = httpEngine.getResponseHeaders();
    if (response.hasVaryAll()) {
        return null;
    }
    RawHeaders varyHeaders = httpEngine.getRequestHeaders().getHeaders().getAll(response.getVaryFields());
    Entry entry = new Entry(uri, varyHeaders, httpConnection);
    DiskLruCache.Editor editor = null;
    try {
        editor = cache.edit(key);
        if (editor == null) {
            return null;
        }
        entry.writeTo(editor);
        return new CacheRequestImpl(editor);
    } catch (IOException e) {
        abortQuietly(editor);
        return null;
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) HttpEngine(com.squareup.okhttp.internal.http.HttpEngine) DiskLruCache(com.squareup.okhttp.internal.DiskLruCache) IOException(java.io.IOException) ResponseHeaders(com.squareup.okhttp.internal.http.ResponseHeaders) RawHeaders(com.squareup.okhttp.internal.http.RawHeaders)

Example 2 with HttpEngine

use of com.squareup.okhttp.internal.http.HttpEngine in project cordova-android-chromeview by thedracle.

the class HttpResponseCache method update.

private void update(CacheResponse conditionalCacheHit, HttpURLConnection httpConnection) throws IOException {
    HttpEngine httpEngine = getHttpEngine(httpConnection);
    URI uri = httpEngine.getUri();
    ResponseHeaders response = httpEngine.getResponseHeaders();
    RawHeaders varyHeaders = httpEngine.getRequestHeaders().getHeaders().getAll(response.getVaryFields());
    Entry entry = new Entry(uri, varyHeaders, httpConnection);
    DiskLruCache.Snapshot snapshot = (conditionalCacheHit instanceof EntryCacheResponse) ? ((EntryCacheResponse) conditionalCacheHit).snapshot : ((EntrySecureCacheResponse) conditionalCacheHit).snapshot;
    DiskLruCache.Editor editor = null;
    try {
        // returns null if snapshot is not current
        editor = snapshot.edit();
        if (editor != null) {
            entry.writeTo(editor);
            editor.commit();
        }
    } catch (IOException e) {
        abortQuietly(editor);
    }
}
Also used : HttpEngine(com.squareup.okhttp.internal.http.HttpEngine) DiskLruCache(com.squareup.okhttp.internal.DiskLruCache) IOException(java.io.IOException) ResponseHeaders(com.squareup.okhttp.internal.http.ResponseHeaders) URI(java.net.URI) RawHeaders(com.squareup.okhttp.internal.http.RawHeaders)

Example 3 with HttpEngine

use of com.squareup.okhttp.internal.http.HttpEngine in project phonegap-facebook-plugin by Wizcorp.

the class HttpResponseCache method put.

@Override
public CacheRequest put(URI uri, URLConnection urlConnection) throws IOException {
    if (!(urlConnection instanceof HttpURLConnection)) {
        return null;
    }
    HttpURLConnection httpConnection = (HttpURLConnection) urlConnection;
    String requestMethod = httpConnection.getRequestMethod();
    String key = uriToKey(uri);
    if (requestMethod.equals("POST") || requestMethod.equals("PUT") || requestMethod.equals("DELETE")) {
        try {
            cache.remove(key);
        } catch (IOException ignored) {
        // The cache cannot be written.
        }
        return null;
    } else if (!requestMethod.equals("GET")) {
        // so is high and the benefit is low.
        return null;
    }
    HttpEngine httpEngine = getHttpEngine(httpConnection);
    if (httpEngine == null) {
        // Don't cache unless the HTTP implementation is ours.
        return null;
    }
    ResponseHeaders response = httpEngine.getResponseHeaders();
    if (response.hasVaryAll()) {
        return null;
    }
    RawHeaders varyHeaders = httpEngine.getRequestHeaders().getHeaders().getAll(response.getVaryFields());
    Entry entry = new Entry(uri, varyHeaders, httpConnection);
    DiskLruCache.Editor editor = null;
    try {
        editor = cache.edit(key);
        if (editor == null) {
            return null;
        }
        entry.writeTo(editor);
        return new CacheRequestImpl(editor);
    } catch (IOException e) {
        abortQuietly(editor);
        return null;
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) HttpEngine(com.squareup.okhttp.internal.http.HttpEngine) DiskLruCache(com.squareup.okhttp.internal.DiskLruCache) IOException(java.io.IOException) ResponseHeaders(com.squareup.okhttp.internal.http.ResponseHeaders) RawHeaders(com.squareup.okhttp.internal.http.RawHeaders)

Example 4 with HttpEngine

use of com.squareup.okhttp.internal.http.HttpEngine in project phonegap-facebook-plugin by Wizcorp.

the class HttpResponseCache method update.

private void update(CacheResponse conditionalCacheHit, HttpURLConnection httpConnection) throws IOException {
    HttpEngine httpEngine = getHttpEngine(httpConnection);
    URI uri = httpEngine.getUri();
    ResponseHeaders response = httpEngine.getResponseHeaders();
    RawHeaders varyHeaders = httpEngine.getRequestHeaders().getHeaders().getAll(response.getVaryFields());
    Entry entry = new Entry(uri, varyHeaders, httpConnection);
    DiskLruCache.Snapshot snapshot = (conditionalCacheHit instanceof EntryCacheResponse) ? ((EntryCacheResponse) conditionalCacheHit).snapshot : ((EntrySecureCacheResponse) conditionalCacheHit).snapshot;
    DiskLruCache.Editor editor = null;
    try {
        // returns null if snapshot is not current
        editor = snapshot.edit();
        if (editor != null) {
            entry.writeTo(editor);
            editor.commit();
        }
    } catch (IOException e) {
        abortQuietly(editor);
    }
}
Also used : HttpEngine(com.squareup.okhttp.internal.http.HttpEngine) DiskLruCache(com.squareup.okhttp.internal.DiskLruCache) IOException(java.io.IOException) ResponseHeaders(com.squareup.okhttp.internal.http.ResponseHeaders) URI(java.net.URI) RawHeaders(com.squareup.okhttp.internal.http.RawHeaders)

Example 5 with HttpEngine

use of com.squareup.okhttp.internal.http.HttpEngine in project robovm by robovm.

the class HttpResponseCache method update.

private void update(CacheResponse conditionalCacheHit, HttpURLConnection httpConnection) throws IOException {
    HttpEngine httpEngine = getHttpEngine(httpConnection);
    URI uri = httpEngine.getUri();
    ResponseHeaders response = httpEngine.getResponseHeaders();
    RawHeaders varyHeaders = httpEngine.getRequestHeaders().getHeaders().getAll(response.getVaryFields());
    Entry entry = new Entry(uri, varyHeaders, httpConnection);
    DiskLruCache.Snapshot snapshot = (conditionalCacheHit instanceof EntryCacheResponse) ? ((EntryCacheResponse) conditionalCacheHit).snapshot : ((EntrySecureCacheResponse) conditionalCacheHit).snapshot;
    DiskLruCache.Editor editor = null;
    try {
        // returns null if snapshot is not current
        editor = snapshot.edit();
        if (editor != null) {
            entry.writeTo(editor);
            editor.commit();
        }
    } catch (IOException e) {
        abortQuietly(editor);
    }
}
Also used : HttpEngine(com.squareup.okhttp.internal.http.HttpEngine) DiskLruCache(com.squareup.okhttp.internal.DiskLruCache) IOException(java.io.IOException) ResponseHeaders(com.squareup.okhttp.internal.http.ResponseHeaders) URI(java.net.URI) RawHeaders(com.squareup.okhttp.internal.http.RawHeaders)

Aggregations

HttpEngine (com.squareup.okhttp.internal.http.HttpEngine)7 IOException (java.io.IOException)7 DiskLruCache (com.squareup.okhttp.internal.DiskLruCache)6 RawHeaders (com.squareup.okhttp.internal.http.RawHeaders)6 ResponseHeaders (com.squareup.okhttp.internal.http.ResponseHeaders)6 HttpURLConnection (java.net.HttpURLConnection)3 URI (java.net.URI)3 Connection (com.squareup.okhttp.Connection)1 MediaType (com.squareup.okhttp.MediaType)1 Request (com.squareup.okhttp.Request)1 RequestBody (com.squareup.okhttp.RequestBody)1 Response (com.squareup.okhttp.Response)1 RequestException (com.squareup.okhttp.internal.http.RequestException)1 RouteException (com.squareup.okhttp.internal.http.RouteException)1 ProtocolException (java.net.ProtocolException)1