com.squareup.okhttp.OkResponseCache Java Examples

The following examples show how to use com.squareup.okhttp.OkResponseCache. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HttpEngine.java    From reader with MIT License 6 votes vote down vote up
private void maybeCache() throws IOException {
  // Are we caching at all?
  if (!policy.getUseCaches()) return;
  OkResponseCache responseCache = client.getOkResponseCache();
  if (responseCache == null) return;

  HttpURLConnection connectionToCache = policy.getHttpConnectionToCache();

  // Should we cache this response for this request?
  if (!responseHeaders.isCacheable(requestHeaders)) {
    responseCache.maybeRemove(connectionToCache.getRequestMethod(), uri);
    return;
  }

  // Offer this request to the cache.
  cacheRequest = responseCache.put(uri, connectionToCache);
}
 
Example #2
Source File: MapFragmentTest.java    From open with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void shouldUse10MegResponseCache() throws Exception {
    Map map = mapFragment.getMap();
    TileLayer baseLayer = field("mBaseLayer").ofType(TileLayer.class).in(map).get();
    UrlTileSource tileSource =
            (UrlTileSource) field("mTileSource").
                    ofType(TileSource.class).in(baseLayer).get();
    HttpEngine.Factory engine = field("mHttpFactory").
            ofType(HttpEngine.Factory.class).in(tileSource).get();
    OkHttpClient client = field("mClient").ofType(OkHttpClient.class).in(engine).get();

    HttpResponseCache cache =
            (HttpResponseCache) field("responseCache").
                    ofType(OkResponseCache.class).in(client).get();
    assertThat(cache.getMaxSize()).isEqualTo(MapFragment.CACHE_SIZE);
}
 
Example #3
Source File: MapFragmentTest.java    From open with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void shouldUseResponseCacheStoredOnFile() throws Exception {
    Map map = mapFragment.getMap();
    TileLayer baseLayer = field("mBaseLayer").ofType(TileLayer.class).in(map).get();
    UrlTileSource tileSource =
            (UrlTileSource) field("mTileSource").
                    ofType(TileSource.class).in(baseLayer).get();
    HttpEngine.Factory engine = field("mHttpFactory").
            ofType(HttpEngine.Factory.class).in(tileSource).get();
    OkHttpClient client = field("mClient").ofType(OkHttpClient.class).in(engine).get();

    HttpResponseCache cache =
            (HttpResponseCache) field("responseCache").
                    ofType(OkResponseCache.class).in(client).get();
    assertThat(cache.getDirectory().getAbsolutePath()).
            isEqualTo(activity.getExternalCacheDir().getAbsolutePath() + "/tile-cache");
}
 
Example #4
Source File: HttpEngine.java    From CordovaYoutubeVideoPlayer with MIT License 6 votes vote down vote up
private void maybeCache() throws IOException {
  // Are we caching at all?
  if (!policy.getUseCaches()) return;
  OkResponseCache responseCache = client.getOkResponseCache();
  if (responseCache == null) return;

  HttpURLConnection connectionToCache = policy.getHttpConnectionToCache();

  // Should we cache this response for this request?
  if (!responseHeaders.isCacheable(requestHeaders)) {
    responseCache.maybeRemove(connectionToCache.getRequestMethod(), uri);
    return;
  }

  // Offer this request to the cache.
  cacheRequest = responseCache.put(uri, connectionToCache);
}
 
Example #5
Source File: HttpEngine.java    From phonegapbootcampsite with MIT License 6 votes vote down vote up
private void maybeCache() throws IOException {
  // Are we caching at all?
  if (!policy.getUseCaches()) return;
  OkResponseCache responseCache = client.getOkResponseCache();
  if (responseCache == null) return;

  HttpURLConnection connectionToCache = policy.getHttpConnectionToCache();

  // Should we cache this response for this request?
  if (!responseHeaders.isCacheable(requestHeaders)) {
    responseCache.maybeRemove(connectionToCache.getRequestMethod(), uri);
    return;
  }

  // Offer this request to the cache.
  cacheRequest = responseCache.put(uri, connectionToCache);
}
 
Example #6
Source File: HttpEngine.java    From cordova-amazon-fireos with Apache License 2.0 6 votes vote down vote up
private void maybeCache() throws IOException {
  // Are we caching at all?
  if (!policy.getUseCaches()) return;
  OkResponseCache responseCache = client.getOkResponseCache();
  if (responseCache == null) return;

  HttpURLConnection connectionToCache = policy.getHttpConnectionToCache();

  // Should we cache this response for this request?
  if (!responseHeaders.isCacheable(requestHeaders)) {
    responseCache.maybeRemove(connectionToCache.getRequestMethod(), uri);
    return;
  }

  // Offer this request to the cache.
  cacheRequest = responseCache.put(uri, connectionToCache);
}
 
Example #7
Source File: HttpEngine.java    From wildfly-samples with MIT License 6 votes vote down vote up
private void maybeCache() throws IOException {
  // Are we caching at all?
  if (!policy.getUseCaches()) return;
  OkResponseCache responseCache = client.getOkResponseCache();
  if (responseCache == null) return;

  HttpURLConnection connectionToCache = policy.getHttpConnectionToCache();

  // Should we cache this response for this request?
  if (!responseHeaders.isCacheable(requestHeaders)) {
    responseCache.maybeRemove(connectionToCache.getRequestMethod(), uri);
    return;
  }

  // Offer this request to the cache.
  cacheRequest = responseCache.put(uri, connectionToCache);
}
 
Example #8
Source File: HttpEngine.java    From reader with MIT License 6 votes vote down vote up
private void maybeCache() throws IOException {
  // Are we caching at all?
  if (!policy.getUseCaches()) return;
  OkResponseCache responseCache = client.getOkResponseCache();
  if (responseCache == null) return;

  HttpURLConnection connectionToCache = policy.getHttpConnectionToCache();

  // Should we cache this response for this request?
  if (!responseHeaders.isCacheable(requestHeaders)) {
    responseCache.maybeRemove(connectionToCache.getRequestMethod(), uri);
    return;
  }

  // Offer this request to the cache.
  cacheRequest = responseCache.put(uri, connectionToCache);
}
 
Example #9
Source File: HttpEngine.java    From bluemix-parking-meter with MIT License 6 votes vote down vote up
private void maybeCache() throws IOException {
  // Are we caching at all?
  if (!policy.getUseCaches()) return;
  OkResponseCache responseCache = client.getOkResponseCache();
  if (responseCache == null) return;

  HttpURLConnection connectionToCache = policy.getHttpConnectionToCache();

  // Should we cache this response for this request?
  if (!responseHeaders.isCacheable(requestHeaders)) {
    responseCache.maybeRemove(connectionToCache.getRequestMethod(), uri);
    return;
  }

  // Offer this request to the cache.
  cacheRequest = responseCache.put(uri, connectionToCache);
}
 
Example #10
Source File: HttpEngine.java    From android-discourse with Apache License 2.0 6 votes vote down vote up
private void maybeCache() throws IOException {
    // Are we caching at all?
    if (!policy.getUseCaches())
        return;
    OkResponseCache responseCache = client.getOkResponseCache();
    if (responseCache == null)
        return;

    HttpURLConnection connectionToCache = policy.getHttpConnectionToCache();

    // Should we cache this response for this request?
    if (!responseHeaders.isCacheable(requestHeaders)) {
        responseCache.maybeRemove(connectionToCache.getRequestMethod(), uri);
        return;
    }

    // Offer this request to the cache.
    cacheRequest = responseCache.put(uri, connectionToCache);
}
 
Example #11
Source File: HttpEngine.java    From L.TileLayer.Cordova with MIT License 6 votes vote down vote up
private void maybeCache() throws IOException {
  // Are we caching at all?
  if (!policy.getUseCaches()) return;
  OkResponseCache responseCache = client.getOkResponseCache();
  if (responseCache == null) return;

  HttpURLConnection connectionToCache = policy.getHttpConnectionToCache();

  // Should we cache this response for this request?
  if (!responseHeaders.isCacheable(requestHeaders)) {
    responseCache.maybeRemove(connectionToCache.getRequestMethod(), uri);
    return;
  }

  // Offer this request to the cache.
  cacheRequest = responseCache.put(uri, connectionToCache);
}
 
Example #12
Source File: HttpEngine.java    From crosswalk-cordova-android with Apache License 2.0 6 votes vote down vote up
private void maybeCache() throws IOException {
  // Are we caching at all?
  if (!policy.getUseCaches()) return;
  OkResponseCache responseCache = client.getOkResponseCache();
  if (responseCache == null) return;

  HttpURLConnection connectionToCache = policy.getHttpConnectionToCache();

  // Should we cache this response for this request?
  if (!responseHeaders.isCacheable(requestHeaders)) {
    responseCache.maybeRemove(connectionToCache.getRequestMethod(), uri);
    return;
  }

  // Offer this request to the cache.
  cacheRequest = responseCache.put(uri, connectionToCache);
}
 
Example #13
Source File: HttpEngine.java    From IoTgo_Android_App with MIT License 6 votes vote down vote up
private void maybeCache() throws IOException {
  // Are we caching at all?
  if (!policy.getUseCaches()) return;
  OkResponseCache responseCache = client.getOkResponseCache();
  if (responseCache == null) return;

  HttpURLConnection connectionToCache = policy.getHttpConnectionToCache();

  // Should we cache this response for this request?
  if (!responseHeaders.isCacheable(requestHeaders)) {
    responseCache.maybeRemove(connectionToCache.getRequestMethod(), uri);
    return;
  }

  // Offer this request to the cache.
  cacheRequest = responseCache.put(uri, connectionToCache);
}
 
Example #14
Source File: HttpEngine.java    From L.TileLayer.Cordova with MIT License 5 votes vote down vote up
/**
 * 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();
  }
}
 
Example #15
Source File: HttpEngine.java    From android-discourse with Apache License 2.0 5 votes vote down vote up
/**
 * 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();
    }
}
 
Example #16
Source File: HttpEngine.java    From L.TileLayer.Cordova with MIT License 5 votes vote down vote up
/**
 * Figures out what the response source will be, and opens a socket to that
 * source if necessary. Prepares the request headers and gets ready to start
 * writing the request body if it exists.
 */
public final void sendRequest() throws IOException {
  if (responseSource != null) {
    return;
  }

  prepareRawRequestHeaders();
  initResponseSource();
  OkResponseCache responseCache = client.getOkResponseCache();
  if (responseCache != null) {
    responseCache.trackResponse(responseSource);
  }

  // The raw response source may require the network, but the request
  // headers may forbid network use. In that case, dispose of the network
  // response and use a GATEWAY_TIMEOUT response instead, as specified
  // by http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.4.
  if (requestHeaders.isOnlyIfCached() && responseSource.requiresConnection()) {
    if (responseSource == ResponseSource.CONDITIONAL_CACHE) {
      Util.closeQuietly(cachedResponseBody);
    }
    this.responseSource = ResponseSource.CACHE;
    this.cacheResponse = GATEWAY_TIMEOUT_RESPONSE;
    RawHeaders rawResponseHeaders = RawHeaders.fromMultimap(cacheResponse.getHeaders(), true);
    setResponse(new ResponseHeaders(uri, rawResponseHeaders), cacheResponse.getBody());
  }

  if (responseSource.requiresConnection()) {
    sendSocketRequest();
  } else if (connection != null) {
    client.getConnectionPool().recycle(connection);
    connection = null;
  }
}
 
Example #17
Source File: HttpEngine.java    From CordovaYoutubeVideoPlayer with MIT License 5 votes vote down vote up
/**
 * 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();
  }
}
 
Example #18
Source File: HttpEngine.java    From CordovaYoutubeVideoPlayer with MIT License 5 votes vote down vote up
/**
 * Figures out what the response source will be, and opens a socket to that
 * source if necessary. Prepares the request headers and gets ready to start
 * writing the request body if it exists.
 */
public final void sendRequest() throws IOException {
  if (responseSource != null) {
    return;
  }

  prepareRawRequestHeaders();
  initResponseSource();
  OkResponseCache responseCache = client.getOkResponseCache();
  if (responseCache != null) {
    responseCache.trackResponse(responseSource);
  }

  // The raw response source may require the network, but the request
  // headers may forbid network use. In that case, dispose of the network
  // response and use a GATEWAY_TIMEOUT response instead, as specified
  // by http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.4.
  if (requestHeaders.isOnlyIfCached() && responseSource.requiresConnection()) {
    if (responseSource == ResponseSource.CONDITIONAL_CACHE) {
      Util.closeQuietly(cachedResponseBody);
    }
    this.responseSource = ResponseSource.CACHE;
    this.cacheResponse = GATEWAY_TIMEOUT_RESPONSE;
    RawHeaders rawResponseHeaders = RawHeaders.fromMultimap(cacheResponse.getHeaders(), true);
    setResponse(new ResponseHeaders(uri, rawResponseHeaders), cacheResponse.getBody());
  }

  if (responseSource.requiresConnection()) {
    sendSocketRequest();
  } else if (connection != null) {
    client.getConnectionPool().recycle(connection);
    connection = null;
  }
}
 
Example #19
Source File: HttpEngine.java    From wildfly-samples with MIT License 5 votes vote down vote up
/**
 * Figures out what the response source will be, and opens a socket to that
 * source if necessary. Prepares the request headers and gets ready to start
 * writing the request body if it exists.
 */
public final void sendRequest() throws IOException {
  if (responseSource != null) {
    return;
  }

  prepareRawRequestHeaders();
  initResponseSource();
  OkResponseCache responseCache = client.getOkResponseCache();
  if (responseCache != null) {
    responseCache.trackResponse(responseSource);
  }

  // The raw response source may require the network, but the request
  // headers may forbid network use. In that case, dispose of the network
  // response and use a GATEWAY_TIMEOUT response instead, as specified
  // by http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.4.
  if (requestHeaders.isOnlyIfCached() && responseSource.requiresConnection()) {
    if (responseSource == ResponseSource.CONDITIONAL_CACHE) {
      Util.closeQuietly(cachedResponseBody);
    }
    this.responseSource = ResponseSource.CACHE;
    this.cacheResponse = GATEWAY_TIMEOUT_RESPONSE;
    RawHeaders rawResponseHeaders = RawHeaders.fromMultimap(cacheResponse.getHeaders(), true);
    setResponse(new ResponseHeaders(uri, rawResponseHeaders), cacheResponse.getBody());
  }

  if (responseSource.requiresConnection()) {
    sendSocketRequest();
  } else if (connection != null) {
    client.getConnectionPool().recycle(connection);
    connection = null;
  }
}
 
Example #20
Source File: HttpEngine.java    From IoTgo_Android_App with MIT License 5 votes vote down vote up
/**
 * Figures out what the response source will be, and opens a socket to that
 * source if necessary. Prepares the request headers and gets ready to start
 * writing the request body if it exists.
 */
public final void sendRequest() throws IOException {
  if (responseSource != null) {
    return;
  }

  prepareRawRequestHeaders();
  initResponseSource();
  OkResponseCache responseCache = client.getOkResponseCache();
  if (responseCache != null) {
    responseCache.trackResponse(responseSource);
  }

  // The raw response source may require the network, but the request
  // headers may forbid network use. In that case, dispose of the network
  // response and use a GATEWAY_TIMEOUT response instead, as specified
  // by http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.4.
  if (requestHeaders.isOnlyIfCached() && responseSource.requiresConnection()) {
    if (responseSource == ResponseSource.CONDITIONAL_CACHE) {
      Util.closeQuietly(cachedResponseBody);
    }
    this.responseSource = ResponseSource.CACHE;
    this.cacheResponse = GATEWAY_TIMEOUT_RESPONSE;
    RawHeaders rawResponseHeaders = RawHeaders.fromMultimap(cacheResponse.getHeaders(), true);
    setResponse(new ResponseHeaders(uri, rawResponseHeaders), cacheResponse.getBody());
  }

  if (responseSource.requiresConnection()) {
    sendSocketRequest();
  } else if (connection != null) {
    client.getConnectionPool().recycle(connection);
    connection = null;
  }
}
 
Example #21
Source File: HttpEngine.java    From phonegapbootcampsite with MIT License 5 votes vote down vote up
/**
 * 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();
  }
}
 
Example #22
Source File: HttpEngine.java    From phonegapbootcampsite with MIT License 5 votes vote down vote up
/**
 * Figures out what the response source will be, and opens a socket to that
 * source if necessary. Prepares the request headers and gets ready to start
 * writing the request body if it exists.
 */
public final void sendRequest() throws IOException {
  if (responseSource != null) {
    return;
  }

  prepareRawRequestHeaders();
  initResponseSource();
  OkResponseCache responseCache = client.getOkResponseCache();
  if (responseCache != null) {
    responseCache.trackResponse(responseSource);
  }

  // The raw response source may require the network, but the request
  // headers may forbid network use. In that case, dispose of the network
  // response and use a GATEWAY_TIMEOUT response instead, as specified
  // by http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.4.
  if (requestHeaders.isOnlyIfCached() && responseSource.requiresConnection()) {
    if (responseSource == ResponseSource.CONDITIONAL_CACHE) {
      Util.closeQuietly(cachedResponseBody);
    }
    this.responseSource = ResponseSource.CACHE;
    this.cacheResponse = GATEWAY_TIMEOUT_RESPONSE;
    RawHeaders rawResponseHeaders = RawHeaders.fromMultimap(cacheResponse.getHeaders(), true);
    setResponse(new ResponseHeaders(uri, rawResponseHeaders), cacheResponse.getBody());
  }

  if (responseSource.requiresConnection()) {
    sendSocketRequest();
  } else if (connection != null) {
    client.getConnectionPool().recycle(connection);
    connection = null;
  }
}
 
Example #23
Source File: HttpEngine.java    From wildfly-samples with MIT License 5 votes vote down vote up
/**
 * 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();
  }
}
 
Example #24
Source File: HttpEngine.java    From IoTgo_Android_App with MIT License 5 votes vote down vote up
/**
 * 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();
  }
}
 
Example #25
Source File: HttpEngine.java    From cordova-amazon-fireos with Apache License 2.0 5 votes vote down vote up
/**
 * 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();
  }
}
 
Example #26
Source File: HttpEngine.java    From cordova-amazon-fireos with Apache License 2.0 5 votes vote down vote up
/**
 * Figures out what the response source will be, and opens a socket to that
 * source if necessary. Prepares the request headers and gets ready to start
 * writing the request body if it exists.
 */
public final void sendRequest() throws IOException {
  if (responseSource != null) {
    return;
  }

  prepareRawRequestHeaders();
  initResponseSource();
  OkResponseCache responseCache = client.getOkResponseCache();
  if (responseCache != null) {
    responseCache.trackResponse(responseSource);
  }

  // The raw response source may require the network, but the request
  // headers may forbid network use. In that case, dispose of the network
  // response and use a GATEWAY_TIMEOUT response instead, as specified
  // by http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.4.
  if (requestHeaders.isOnlyIfCached() && responseSource.requiresConnection()) {
    if (responseSource == ResponseSource.CONDITIONAL_CACHE) {
      Util.closeQuietly(cachedResponseBody);
    }
    this.responseSource = ResponseSource.CACHE;
    this.cacheResponse = GATEWAY_TIMEOUT_RESPONSE;
    RawHeaders rawResponseHeaders = RawHeaders.fromMultimap(cacheResponse.getHeaders(), true);
    setResponse(new ResponseHeaders(uri, rawResponseHeaders), cacheResponse.getBody());
  }

  if (responseSource.requiresConnection()) {
    sendSocketRequest();
  } else if (connection != null) {
    client.getConnectionPool().recycle(connection);
    connection = null;
  }
}
 
Example #27
Source File: HttpEngine.java    From reader with MIT License 5 votes vote down vote up
/**
 * 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();
  }
}
 
Example #28
Source File: HttpEngine.java    From reader with MIT License 5 votes vote down vote up
/**
 * Figures out what the response source will be, and opens a socket to that
 * source if necessary. Prepares the request headers and gets ready to start
 * writing the request body if it exists.
 */
public final void sendRequest() throws IOException {
  if (responseSource != null) {
    return;
  }

  prepareRawRequestHeaders();
  initResponseSource();
  OkResponseCache responseCache = client.getOkResponseCache();
  if (responseCache != null) {
    responseCache.trackResponse(responseSource);
  }

  // The raw response source may require the network, but the request
  // headers may forbid network use. In that case, dispose of the network
  // response and use a GATEWAY_TIMEOUT response instead, as specified
  // by http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.4.
  if (requestHeaders.isOnlyIfCached() && responseSource.requiresConnection()) {
    if (responseSource == ResponseSource.CONDITIONAL_CACHE) {
      Util.closeQuietly(cachedResponseBody);
    }
    this.responseSource = ResponseSource.CACHE;
    this.cacheResponse = GATEWAY_TIMEOUT_RESPONSE;
    RawHeaders rawResponseHeaders = RawHeaders.fromMultimap(cacheResponse.getHeaders(), true);
    setResponse(new ResponseHeaders(uri, rawResponseHeaders), cacheResponse.getBody());
  }

  if (responseSource.requiresConnection()) {
    sendSocketRequest();
  } else if (connection != null) {
    client.getConnectionPool().recycle(connection);
    connection = null;
  }
}
 
Example #29
Source File: HttpEngine.java    From reader with MIT License 5 votes vote down vote up
/**
 * 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();
  }
}
 
Example #30
Source File: HttpEngine.java    From reader with MIT License 5 votes vote down vote up
/**
 * Figures out what the response source will be, and opens a socket to that
 * source if necessary. Prepares the request headers and gets ready to start
 * writing the request body if it exists.
 */
public final void sendRequest() throws IOException {
  if (responseSource != null) {
    return;
  }

  prepareRawRequestHeaders();
  initResponseSource();
  OkResponseCache responseCache = client.getOkResponseCache();
  if (responseCache != null) {
    responseCache.trackResponse(responseSource);
  }

  // The raw response source may require the network, but the request
  // headers may forbid network use. In that case, dispose of the network
  // response and use a GATEWAY_TIMEOUT response instead, as specified
  // by http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.4.
  if (requestHeaders.isOnlyIfCached() && responseSource.requiresConnection()) {
    if (responseSource == ResponseSource.CONDITIONAL_CACHE) {
      Util.closeQuietly(cachedResponseBody);
    }
    this.responseSource = ResponseSource.CACHE;
    this.cacheResponse = GATEWAY_TIMEOUT_RESPONSE;
    RawHeaders rawResponseHeaders = RawHeaders.fromMultimap(cacheResponse.getHeaders(), true);
    setResponse(new ResponseHeaders(uri, rawResponseHeaders), cacheResponse.getBody());
  }

  if (responseSource.requiresConnection()) {
    sendSocketRequest();
  } else if (connection != null) {
    client.getConnectionPool().recycle(connection);
    connection = null;
  }
}