Java 类java.net.CacheResponse 实例源码

项目:GitHub    文件:JavaApiConverter.java   
/**
 * Creates an OkHttp Response.Body containing the supplied information.
 */
private static ResponseBody createOkBody(final Headers okHeaders,
    final CacheResponse cacheResponse) throws IOException {
  final BufferedSource body = Okio.buffer(Okio.source(cacheResponse.getBody()));
  return new ResponseBody() {
    @Override
    public MediaType contentType() {
      String contentTypeHeader = okHeaders.get("Content-Type");
      return contentTypeHeader == null ? null : MediaType.parse(contentTypeHeader);
    }

    @Override
    public long contentLength() {
      return HttpHeaders.contentLength(okHeaders);
    }

    @Override public BufferedSource source() {
      return body;
    }
  };
}
项目:GitHub    文件:CacheAdapterTest.java   
@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);
}
项目:GitHub    文件:ResponseCacheTest.java   
@Override public CacheResponse get(URI uri, String requestMethod,
    Map<String, List<String>> requestHeaders) throws IOException {
  final CacheResponse response = delegate.get(uri, requestMethod, requestHeaders);
  if (response instanceof SecureCacheResponse) {
    return new CacheResponse() {
      @Override public InputStream getBody() throws IOException {
        return response.getBody();
      }

      @Override public Map<String, List<String>> getHeaders() throws IOException {
        return response.getHeaders();
      }
    };
  }
  return response;
}
项目:GitHub    文件:ResponseCacheTest.java   
@Test public void responseCacheRequestHeaders() throws IOException, URISyntaxException {
  server.enqueue(new MockResponse()
      .setBody("ABC"));

  final AtomicReference<Map<String, List<String>>> requestHeadersRef = new AtomicReference<>();
  setInternalCache(new CacheAdapter(new AbstractResponseCache() {
    @Override public CacheResponse get(URI uri, String requestMethod,
        Map<String, List<String>> requestHeaders) throws IOException {
      requestHeadersRef.set(requestHeaders);
      return null;
    }
  }));

  URL url = server.url("/").url();
  URLConnection urlConnection = openConnection(url);
  urlConnection.addRequestProperty("A", "android");
  readAscii(urlConnection);
  assertEquals(Arrays.asList("android"), requestHeadersRef.get().get("A"));
}
项目:GitHub    文件:JavaApiConverterTest.java   
/** Test for https://code.google.com/p/android/issues/detail?id=160522 */
@Test public void createOkResponseForCacheGet_withMissingStatusLine() throws Exception {
  URI uri = new URI("http://foo/bar");
  Request request = new Request.Builder().url(uri.toURL()).build();
  CacheResponse cacheResponse = new CacheResponse() {
    @Override public Map<String, List<String>> getHeaders() throws IOException {
      Map<String, List<String>> headers = new LinkedHashMap<>();
      // Headers is deliberately missing an entry with a null key.
      headers.put("xyzzy", Arrays.asList("bar", "baz"));
      return headers;
    }

    @Override public InputStream getBody() throws IOException {
      return null; // Should never be called
    }
  };

  try {
    JavaApiConverter.createOkResponseForCacheGet(request, cacheResponse);
    fail();
  } catch (IOException expected) {
  }
}
项目:GitHub    文件:JavaApiConverterTest.java   
@Test public void createJavaCacheResponse_httpGet() throws Exception {
  Request okRequest =
      createArbitraryOkRequest().newBuilder()
          .url("http://insecure/request")
          .get()
          .build();
  Response okResponse = createArbitraryOkResponse(okRequest).newBuilder()
      .protocol(Protocol.HTTP_1_1)
      .code(200)
      .message("Fantastic")
      .addHeader("key1", "value1_1")
      .addHeader("key2", "value2")
      .addHeader("key1", "value1_2")
      .body(null)
      .build();
  CacheResponse javaCacheResponse = JavaApiConverter.createJavaCacheResponse(okResponse);
  assertFalse(javaCacheResponse instanceof SecureCacheResponse);
  Map<String, List<String>> javaHeaders = javaCacheResponse.getHeaders();
  assertEquals(Arrays.asList("value1_1", "value1_2"), javaHeaders.get("key1"));
  assertEquals(Arrays.asList("HTTP/1.1 200 Fantastic"), javaHeaders.get(null));
  assertNull(javaCacheResponse.getBody());
}
项目:GitHub    文件:JavaApiConverterTest.java   
@Test public void createJavaCacheResponse_httpPost() throws Exception {
  Request okRequest =
      createArbitraryOkRequest().newBuilder()
          .url("http://insecure/request")
          .post(createRequestBody("RequestBody"))
          .build();
  ResponseBody responseBody = createResponseBody("ResponseBody");
  Response okResponse = createArbitraryOkResponse(okRequest).newBuilder()
      .protocol(Protocol.HTTP_1_1)
      .code(200)
      .message("Fantastic")
      .addHeader("key1", "value1_1")
      .addHeader("key2", "value2")
      .addHeader("key1", "value1_2")
      .body(responseBody)
      .build();
  CacheResponse javaCacheResponse = JavaApiConverter.createJavaCacheResponse(okResponse);
  assertFalse(javaCacheResponse instanceof SecureCacheResponse);
  Map<String, List<String>> javaHeaders = javaCacheResponse.getHeaders();
  assertEquals(Arrays.asList("value1_1", "value1_2"), javaHeaders.get("key1"));
  assertEquals(Arrays.asList("HTTP/1.1 200 Fantastic"), javaHeaders.get(null));
  assertEquals("ResponseBody", readAll(javaCacheResponse.getBody()));
}
项目:GitHub    文件:JavaApiConverter.java   
/**
 * Creates an OkHttp Response.Body containing the supplied information.
 */
private static ResponseBody createOkBody(final Headers okHeaders,
    final CacheResponse cacheResponse) throws IOException {
  final BufferedSource body = Okio.buffer(Okio.source(cacheResponse.getBody()));
  return new ResponseBody() {
    @Override
    public MediaType contentType() {
      String contentTypeHeader = okHeaders.get("Content-Type");
      return contentTypeHeader == null ? null : MediaType.parse(contentTypeHeader);
    }

    @Override
    public long contentLength() {
      return HttpHeaders.contentLength(okHeaders);
    }

    @Override public BufferedSource source() {
      return body;
    }
  };
}
项目:GitHub    文件:CacheAdapterTest.java   
@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);
}
项目:GitHub    文件:ResponseCacheTest.java   
@Override public CacheResponse get(URI uri, String requestMethod,
    Map<String, List<String>> requestHeaders) throws IOException {
  final CacheResponse response = delegate.get(uri, requestMethod, requestHeaders);
  if (response instanceof SecureCacheResponse) {
    return new CacheResponse() {
      @Override public InputStream getBody() throws IOException {
        return response.getBody();
      }

      @Override public Map<String, List<String>> getHeaders() throws IOException {
        return response.getHeaders();
      }
    };
  }
  return response;
}
项目:GitHub    文件:ResponseCacheTest.java   
@Test public void responseCacheRequestHeaders() throws IOException, URISyntaxException {
  server.enqueue(new MockResponse()
      .setBody("ABC"));

  final AtomicReference<Map<String, List<String>>> requestHeadersRef = new AtomicReference<>();
  setInternalCache(new CacheAdapter(new AbstractResponseCache() {
    @Override public CacheResponse get(URI uri, String requestMethod,
        Map<String, List<String>> requestHeaders) throws IOException {
      requestHeadersRef.set(requestHeaders);
      return null;
    }
  }));

  URL url = server.url("/").url();
  URLConnection urlConnection = openConnection(url);
  urlConnection.addRequestProperty("A", "android");
  readAscii(urlConnection);
  assertEquals(Arrays.asList("android"), requestHeadersRef.get().get("A"));
}
项目:GitHub    文件:JavaApiConverterTest.java   
/** Test for https://code.google.com/p/android/issues/detail?id=160522 */
@Test public void createOkResponseForCacheGet_withMissingStatusLine() throws Exception {
  URI uri = new URI("http://foo/bar");
  Request request = new Request.Builder().url(uri.toURL()).build();
  CacheResponse cacheResponse = new CacheResponse() {
    @Override public Map<String, List<String>> getHeaders() throws IOException {
      Map<String, List<String>> headers = new LinkedHashMap<>();
      // Headers is deliberately missing an entry with a null key.
      headers.put("xyzzy", Arrays.asList("bar", "baz"));
      return headers;
    }

    @Override public InputStream getBody() throws IOException {
      return null; // Should never be called
    }
  };

  try {
    JavaApiConverter.createOkResponseForCacheGet(request, cacheResponse);
    fail();
  } catch (IOException expected) {
  }
}
项目:GitHub    文件:JavaApiConverterTest.java   
@Test public void createJavaCacheResponse_httpGet() throws Exception {
  Request okRequest =
      createArbitraryOkRequest().newBuilder()
          .url("http://insecure/request")
          .get()
          .build();
  Response okResponse = createArbitraryOkResponse(okRequest).newBuilder()
      .protocol(Protocol.HTTP_1_1)
      .code(200)
      .message("Fantastic")
      .addHeader("key1", "value1_1")
      .addHeader("key2", "value2")
      .addHeader("key1", "value1_2")
      .body(null)
      .build();
  CacheResponse javaCacheResponse = JavaApiConverter.createJavaCacheResponse(okResponse);
  assertFalse(javaCacheResponse instanceof SecureCacheResponse);
  Map<String, List<String>> javaHeaders = javaCacheResponse.getHeaders();
  assertEquals(Arrays.asList("value1_1", "value1_2"), javaHeaders.get("key1"));
  assertEquals(Arrays.asList("HTTP/1.1 200 Fantastic"), javaHeaders.get(null));
  assertNull(javaCacheResponse.getBody());
}
项目:GitHub    文件:JavaApiConverterTest.java   
@Test public void createJavaCacheResponse_httpPost() throws Exception {
  Request okRequest =
      createArbitraryOkRequest().newBuilder()
          .url("http://insecure/request")
          .post(createRequestBody("RequestBody"))
          .build();
  ResponseBody responseBody = createResponseBody("ResponseBody");
  Response okResponse = createArbitraryOkResponse(okRequest).newBuilder()
      .protocol(Protocol.HTTP_1_1)
      .code(200)
      .message("Fantastic")
      .addHeader("key1", "value1_1")
      .addHeader("key2", "value2")
      .addHeader("key1", "value1_2")
      .body(responseBody)
      .build();
  CacheResponse javaCacheResponse = JavaApiConverter.createJavaCacheResponse(okResponse);
  assertFalse(javaCacheResponse instanceof SecureCacheResponse);
  Map<String, List<String>> javaHeaders = javaCacheResponse.getHeaders();
  assertEquals(Arrays.asList("value1_1", "value1_2"), javaHeaders.get("key1"));
  assertEquals(Arrays.asList("HTTP/1.1 200 Fantastic"), javaHeaders.get(null));
  assertEquals("ResponseBody", readAll(javaCacheResponse.getBody()));
}
项目:LoRaWAN-Smart-Parking    文件:HttpResponseCache.java   
@Override public CacheResponse get(URI uri, String requestMethod,
    Map<String, List<String>> requestHeaders) {
  String key = uriToKey(uri);
  DiskLruCache.Snapshot snapshot;
  Entry entry;
  try {
    snapshot = cache.get(key);
    if (snapshot == null) {
      return null;
    }
    entry = new Entry(snapshot.getInputStream(ENTRY_METADATA));
  } catch (IOException e) {
    // Give up because the cache cannot be read.
    return null;
  }

  if (!entry.matches(uri, requestMethod, requestHeaders)) {
    snapshot.close();
    return null;
  }

  return entry.isHttps() ? new EntrySecureCacheResponse(entry, snapshot)
      : new EntryCacheResponse(entry, snapshot);
}
项目:LoRaWAN-Smart-Parking    文件:HttpResponseCache.java   
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 {
    editor = snapshot.edit(); // returns null if snapshot is not current
    if (editor != null) {
      entry.writeTo(editor);
      editor.commit();
    }
  } catch (IOException e) {
    abortQuietly(editor);
  }
}
项目:LoRaWAN-Smart-Parking    文件:HttpResponseCache.java   
@Override public CacheResponse get(URI uri, String requestMethod,
    Map<String, List<String>> requestHeaders) {
  String key = uriToKey(uri);
  DiskLruCache.Snapshot snapshot;
  Entry entry;
  try {
    snapshot = cache.get(key);
    if (snapshot == null) {
      return null;
    }
    entry = new Entry(snapshot.getInputStream(ENTRY_METADATA));
  } catch (IOException e) {
    // Give up because the cache cannot be read.
    return null;
  }

  if (!entry.matches(uri, requestMethod, requestHeaders)) {
    snapshot.close();
    return null;
  }

  return entry.isHttps() ? new EntrySecureCacheResponse(entry, snapshot)
      : new EntryCacheResponse(entry, snapshot);
}
项目:LoRaWAN-Smart-Parking    文件:HttpResponseCache.java   
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 {
    editor = snapshot.edit(); // returns null if snapshot is not current
    if (editor != null) {
      entry.writeTo(editor);
      editor.commit();
    }
  } catch (IOException e) {
    abortQuietly(editor);
  }
}
项目:PriorityOkHttp    文件:JavaApiConverter.java   
/**
 * Creates an OkHttp Response.Body containing the supplied information.
 */
private static ResponseBody createOkBody(final Headers okHeaders,
    final CacheResponse cacheResponse) throws IOException {
  final BufferedSource body = Okio.buffer(Okio.source(cacheResponse.getBody()));
  return new ResponseBody() {
    @Override
    public MediaType contentType() {
      String contentTypeHeader = okHeaders.get("Content-Type");
      return contentTypeHeader == null ? null : MediaType.parse(contentTypeHeader);
    }

    @Override
    public long contentLength() {
      return OkHeaders.contentLength(okHeaders);
    }

    @Override public BufferedSource source() {
      return body;
    }
  };
}
项目:PriorityOkHttp    文件:CacheAdapterTest.java   
@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 {
      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;
    }
  };
  setInternalCache(new CacheAdapter(responseCache));

  connection = new OkUrlFactory(client).open(serverUrl);
  connection.setRequestProperty("key1", "value1");

  executeGet(connection);
}
项目:PriorityOkHttp    文件:CacheAdapterTest.java   
@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 {
      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;
    }
  };
  setInternalCache(new CacheAdapter(responseCache));
  client = client.newBuilder()
      .sslSocketFactory(sslContext.getSocketFactory())
      .hostnameVerifier(hostnameVerifier)
      .build();

  connection = new OkUrlFactory(client).open(serverUrl);
  connection.setRequestProperty("key1", "value1");

  executeGet(connection);
}
项目:PriorityOkHttp    文件:ResponseCacheTest.java   
@Override public CacheResponse get(URI uri, String requestMethod,
    Map<String, List<String>> requestHeaders) throws IOException {
  final CacheResponse response = delegate.get(uri, requestMethod, requestHeaders);
  if (response instanceof SecureCacheResponse) {
    return new CacheResponse() {
      @Override public InputStream getBody() throws IOException {
        return response.getBody();
      }

      @Override public Map<String, List<String>> getHeaders() throws IOException {
        return response.getHeaders();
      }
    };
  }
  return response;
}
项目:PriorityOkHttp    文件:ResponseCacheTest.java   
@Test public void responseCacheRequestHeaders() throws IOException, URISyntaxException {
  server.enqueue(new MockResponse()
      .setBody("ABC"));

  final AtomicReference<Map<String, List<String>>> requestHeadersRef = new AtomicReference<>();
  setInternalCache(new CacheAdapter(new AbstractResponseCache() {
    @Override public CacheResponse get(URI uri, String requestMethod,
        Map<String, List<String>> requestHeaders) throws IOException {
      requestHeadersRef.set(requestHeaders);
      return null;
    }
  }));

  URL url = server.url("/").url();
  URLConnection urlConnection = openConnection(url);
  urlConnection.addRequestProperty("A", "android");
  readAscii(urlConnection);
  assertEquals(Arrays.asList("android"), requestHeadersRef.get().get("A"));
}
项目:PriorityOkHttp    文件:JavaApiConverterTest.java   
/** Test for https://code.google.com/p/android/issues/detail?id=160522 */
@Test public void createOkResponseForCacheGet_withMissingStatusLine() throws Exception {
  URI uri = new URI("http://foo/bar");
  Request request = new Request.Builder().url(uri.toURL()).build();
  CacheResponse cacheResponse = new CacheResponse() {
    @Override public Map<String, List<String>> getHeaders() throws IOException {
      Map<String, List<String>> headers = new HashMap<>();
      // Headers is deliberately missing an entry with a null key.
      headers.put("xyzzy", Arrays.asList("bar", "baz"));
      return headers;
    }

    @Override public InputStream getBody() throws IOException {
      return null; // Should never be called
    }
  };

  try {
    JavaApiConverter.createOkResponseForCacheGet(request, cacheResponse);
    fail();
  } catch (IOException expected) {
  }
}
项目:PriorityOkHttp    文件:JavaApiConverterTest.java   
@Test public void createJavaCacheResponse_httpGet() throws Exception {
  Request okRequest =
      createArbitraryOkRequest().newBuilder()
          .url("http://insecure/request")
          .get()
          .build();
  Response okResponse = createArbitraryOkResponse(okRequest).newBuilder()
      .protocol(Protocol.HTTP_1_1)
      .code(200)
      .message("Fantastic")
      .addHeader("key1", "value1_1")
      .addHeader("key2", "value2")
      .addHeader("key1", "value1_2")
      .body(null)
      .build();
  CacheResponse javaCacheResponse = JavaApiConverter.createJavaCacheResponse(okResponse);
  assertFalse(javaCacheResponse instanceof SecureCacheResponse);
  Map<String, List<String>> javaHeaders = javaCacheResponse.getHeaders();
  assertEquals(Arrays.asList("value1_1", "value1_2"), javaHeaders.get("key1"));
  assertEquals(Arrays.asList("HTTP/1.1 200 Fantastic"), javaHeaders.get(null));
  assertNull(javaCacheResponse.getBody());
}
项目:PriorityOkHttp    文件:JavaApiConverterTest.java   
@Test public void createJavaCacheResponse_httpPost() throws Exception {
  Request okRequest =
      createArbitraryOkRequest().newBuilder()
          .url("http://insecure/request")
          .post(createRequestBody("RequestBody"))
          .build();
  ResponseBody responseBody = createResponseBody("ResponseBody");
  Response okResponse = createArbitraryOkResponse(okRequest).newBuilder()
      .protocol(Protocol.HTTP_1_1)
      .code(200)
      .message("Fantastic")
      .addHeader("key1", "value1_1")
      .addHeader("key2", "value2")
      .addHeader("key1", "value1_2")
      .body(responseBody)
      .build();
  CacheResponse javaCacheResponse = JavaApiConverter.createJavaCacheResponse(okResponse);
  assertFalse(javaCacheResponse instanceof SecureCacheResponse);
  Map<String, List<String>> javaHeaders = javaCacheResponse.getHeaders();
  assertEquals(Arrays.asList("value1_1", "value1_2"), javaHeaders.get("key1"));
  assertEquals(Arrays.asList("HTTP/1.1 200 Fantastic"), javaHeaders.get(null));
  assertEquals("ResponseBody", readAll(javaCacheResponse.getBody()));
}
项目:Okhttp    文件:JavaApiConverter.java   
/**
 * Creates an OkHttp Response.Body containing the supplied information.
 */
private static ResponseBody createOkBody(final Headers okHeaders,
    final CacheResponse cacheResponse) throws IOException {
  final BufferedSource body = Okio.buffer(Okio.source(cacheResponse.getBody()));
  return new ResponseBody() {
    @Override
    public MediaType contentType() {
      String contentTypeHeader = okHeaders.get("Content-Type");
      return contentTypeHeader == null ? null : MediaType.parse(contentTypeHeader);
    }

    @Override
    public long contentLength() {
      return HttpHeaders.contentLength(okHeaders);
    }

    @Override public BufferedSource source() {
      return body;
    }
  };
}
项目:Okhttp    文件:CacheAdapterTest.java   
@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);
}
项目:Okhttp    文件:ResponseCacheTest.java   
@Override public CacheResponse get(URI uri, String requestMethod,
    Map<String, List<String>> requestHeaders) throws IOException {
  final CacheResponse response = delegate.get(uri, requestMethod, requestHeaders);
  if (response instanceof SecureCacheResponse) {
    return new CacheResponse() {
      @Override public InputStream getBody() throws IOException {
        return response.getBody();
      }

      @Override public Map<String, List<String>> getHeaders() throws IOException {
        return response.getHeaders();
      }
    };
  }
  return response;
}
项目:Okhttp    文件:ResponseCacheTest.java   
@Test public void responseCacheRequestHeaders() throws IOException, URISyntaxException {
  server.enqueue(new MockResponse()
      .setBody("ABC"));

  final AtomicReference<Map<String, List<String>>> requestHeadersRef = new AtomicReference<>();
  setInternalCache(new CacheAdapter(new AbstractResponseCache() {
    @Override public CacheResponse get(URI uri, String requestMethod,
        Map<String, List<String>> requestHeaders) throws IOException {
      requestHeadersRef.set(requestHeaders);
      return null;
    }
  }));

  URL url = server.url("/").url();
  URLConnection urlConnection = openConnection(url);
  urlConnection.addRequestProperty("A", "android");
  readAscii(urlConnection);
  assertEquals(Arrays.asList("android"), requestHeadersRef.get().get("A"));
}
项目:Okhttp    文件:JavaApiConverterTest.java   
/** Test for https://code.google.com/p/android/issues/detail?id=160522 */
@Test public void createOkResponseForCacheGet_withMissingStatusLine() throws Exception {
  URI uri = new URI("http://foo/bar");
  Request request = new Request.Builder().url(uri.toURL()).build();
  CacheResponse cacheResponse = new CacheResponse() {
    @Override public Map<String, List<String>> getHeaders() throws IOException {
      Map<String, List<String>> headers = new HashMap<>();
      // Headers is deliberately missing an entry with a null key.
      headers.put("xyzzy", Arrays.asList("bar", "baz"));
      return headers;
    }

    @Override public InputStream getBody() throws IOException {
      return null; // Should never be called
    }
  };

  try {
    JavaApiConverter.createOkResponseForCacheGet(request, cacheResponse);
    fail();
  } catch (IOException expected) {
  }
}
项目:Okhttp    文件:JavaApiConverterTest.java   
@Test public void createJavaCacheResponse_httpGet() throws Exception {
  Request okRequest =
      createArbitraryOkRequest().newBuilder()
          .url("http://insecure/request")
          .get()
          .build();
  Response okResponse = createArbitraryOkResponse(okRequest).newBuilder()
      .protocol(Protocol.HTTP_1_1)
      .code(200)
      .message("Fantastic")
      .addHeader("key1", "value1_1")
      .addHeader("key2", "value2")
      .addHeader("key1", "value1_2")
      .body(null)
      .build();
  CacheResponse javaCacheResponse = JavaApiConverter.createJavaCacheResponse(okResponse);
  assertFalse(javaCacheResponse instanceof SecureCacheResponse);
  Map<String, List<String>> javaHeaders = javaCacheResponse.getHeaders();
  assertEquals(Arrays.asList("value1_1", "value1_2"), javaHeaders.get("key1"));
  assertEquals(Arrays.asList("HTTP/1.1 200 Fantastic"), javaHeaders.get(null));
  assertNull(javaCacheResponse.getBody());
}
项目:Okhttp    文件:JavaApiConverterTest.java   
@Test public void createJavaCacheResponse_httpPost() throws Exception {
  Request okRequest =
      createArbitraryOkRequest().newBuilder()
          .url("http://insecure/request")
          .post(createRequestBody("RequestBody"))
          .build();
  ResponseBody responseBody = createResponseBody("ResponseBody");
  Response okResponse = createArbitraryOkResponse(okRequest).newBuilder()
      .protocol(Protocol.HTTP_1_1)
      .code(200)
      .message("Fantastic")
      .addHeader("key1", "value1_1")
      .addHeader("key2", "value2")
      .addHeader("key1", "value1_2")
      .body(responseBody)
      .build();
  CacheResponse javaCacheResponse = JavaApiConverter.createJavaCacheResponse(okResponse);
  assertFalse(javaCacheResponse instanceof SecureCacheResponse);
  Map<String, List<String>> javaHeaders = javaCacheResponse.getHeaders();
  assertEquals(Arrays.asList("value1_1", "value1_2"), javaHeaders.get("key1"));
  assertEquals(Arrays.asList("HTTP/1.1 200 Fantastic"), javaHeaders.get(null));
  assertEquals("ResponseBody", readAll(javaCacheResponse.getBody()));
}
项目:smart-mirror-app    文件:HttpResponseCache.java   
@Override public CacheResponse get(URI uri, String requestMethod,
    Map<String, List<String>> requestHeaders) {
  String key = uriToKey(uri);
  DiskLruCache.Snapshot snapshot;
  Entry entry;
  try {
    snapshot = cache.get(key);
    if (snapshot == null) {
      return null;
    }
    entry = new Entry(snapshot.getInputStream(ENTRY_METADATA));
  } catch (IOException e) {
    // Give up because the cache cannot be read.
    return null;
  }

  if (!entry.matches(uri, requestMethod, requestHeaders)) {
    snapshot.close();
    return null;
  }

  return entry.isHttps() ? new EntrySecureCacheResponse(entry, snapshot)
      : new EntryCacheResponse(entry, snapshot);
}
项目:smart-mirror-app    文件:HttpResponseCache.java   
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 {
    editor = snapshot.edit(); // returns null if snapshot is not current
    if (editor != null) {
      entry.writeTo(editor);
      editor.commit();
    }
  } catch (IOException e) {
    abortQuietly(editor);
  }
}
项目:cordova-plugin-background-mode    文件:HttpResponseCache.java   
@Override public CacheResponse get(URI uri, String requestMethod,
    Map<String, List<String>> requestHeaders) {
  String key = uriToKey(uri);
  DiskLruCache.Snapshot snapshot;
  Entry entry;
  try {
    snapshot = cache.get(key);
    if (snapshot == null) {
      return null;
    }
    entry = new Entry(snapshot.getInputStream(ENTRY_METADATA));
  } catch (IOException e) {
    // Give up because the cache cannot be read.
    return null;
  }

  if (!entry.matches(uri, requestMethod, requestHeaders)) {
    snapshot.close();
    return null;
  }

  return entry.isHttps() ? new EntrySecureCacheResponse(entry, snapshot)
      : new EntryCacheResponse(entry, snapshot);
}
项目:cordova-plugin-background-mode    文件:HttpResponseCache.java   
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 {
    editor = snapshot.edit(); // returns null if snapshot is not current
    if (editor != null) {
      entry.writeTo(editor);
      editor.commit();
    }
  } catch (IOException e) {
    abortQuietly(editor);
  }
}
项目:krakn    文件:HttpResponseCache.java   
@Override public CacheResponse get(URI uri, String requestMethod,
    Map<String, List<String>> requestHeaders) {
  String key = uriToKey(uri);
  DiskLruCache.Snapshot snapshot;
  Entry entry;
  try {
    snapshot = cache.get(key);
    if (snapshot == null) {
      return null;
    }
    entry = new Entry(snapshot.getInputStream(ENTRY_METADATA));
  } catch (IOException e) {
    // Give up because the cache cannot be read.
    return null;
  }

  if (!entry.matches(uri, requestMethod, requestHeaders)) {
    snapshot.close();
    return null;
  }

  return entry.isHttps() ? new EntrySecureCacheResponse(entry, snapshot)
      : new EntryCacheResponse(entry, snapshot);
}
项目:multirotorstuff-vtx-calc    文件:HttpResponseCache.java   
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 {
    editor = snapshot.edit(); // returns null if snapshot is not current
    if (editor != null) {
      entry.writeTo(editor);
      editor.commit();
    }
  } catch (IOException e) {
    abortQuietly(editor);
  }
}
项目:volley-it    文件:MainActivity.java   
/**
 * Fetches an entry value from the HttpResponseCache cache
 * @param connection connection from which we need the cache
 * @param uri uri to use to get the cache entry
 * @return cache entry value as String
 */
private String fetchFromHTTPUrlConnectionCache(HttpURLConnection connection, URI uri) {

    try {
        HttpResponseCache responseCache = HttpResponseCache.getInstalled();
        if(responseCache != null){
            CacheResponse cacheResponse = responseCache.get(uri, "GET", connection.getRequestProperties());
            Scanner scanner = new Scanner(cacheResponse.getBody(), "UTF-8");
            StringBuilder sb = new StringBuilder();
            while (scanner.hasNextLine()){
                sb.append(scanner.nextLine());
            }

            return sb.toString();
        }

    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;

}