當前位置: 首頁>>代碼示例>>Java>>正文


Java CacheResponse類代碼示例

本文整理匯總了Java中java.net.CacheResponse的典型用法代碼示例。如果您正苦於以下問題:Java CacheResponse類的具體用法?Java CacheResponse怎麽用?Java CacheResponse使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


CacheResponse類屬於java.net包,在下文中一共展示了CacheResponse類的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: createOkBody

import java.net.CacheResponse; //導入依賴的package包/類
/**
 * 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;
    }
  };
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:24,代碼來源:JavaApiConverter.java

示例2: get_httpGet

import java.net.CacheResponse; //導入依賴的package包/類
@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);
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:26,代碼來源:CacheAdapterTest.java

示例3: get

import java.net.CacheResponse; //導入依賴的package包/類
@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;
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:17,代碼來源:ResponseCacheTest.java

示例4: responseCacheRequestHeaders

import java.net.CacheResponse; //導入依賴的package包/類
@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"));
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:20,代碼來源:ResponseCacheTest.java

示例5: createOkResponseForCacheGet_withMissingStatusLine

import java.net.CacheResponse; //導入依賴的package包/類
/** 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) {
  }
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:24,代碼來源:JavaApiConverterTest.java

示例6: createJavaCacheResponse_httpGet

import java.net.CacheResponse; //導入依賴的package包/類
@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());
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:23,代碼來源:JavaApiConverterTest.java

示例7: createJavaCacheResponse_httpPost

import java.net.CacheResponse; //導入依賴的package包/類
@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()));
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:24,代碼來源:JavaApiConverterTest.java

示例8: get

import java.net.CacheResponse; //導入依賴的package包/類
@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);
}
 
開發者ID:aabognah,項目名稱:LoRaWAN-Smart-Parking,代碼行數:25,代碼來源:HttpResponseCache.java

示例9: update

import java.net.CacheResponse; //導入依賴的package包/類
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);
  }
}
 
開發者ID:aabognah,項目名稱:LoRaWAN-Smart-Parking,代碼行數:23,代碼來源:HttpResponseCache.java

示例10: createOkBody

import java.net.CacheResponse; //導入依賴的package包/類
/**
 * 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;
    }
  };
}
 
開發者ID:lizhangqu,項目名稱:PriorityOkHttp,代碼行數:24,代碼來源:JavaApiConverter.java

示例11: get_httpGet

import java.net.CacheResponse; //導入依賴的package包/類
@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);
}
 
開發者ID:lizhangqu,項目名稱:PriorityOkHttp,代碼行數:22,代碼來源:CacheAdapterTest.java

示例12: get_httpsGet

import java.net.CacheResponse; //導入依賴的package包/類
@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);
}
 
開發者ID:lizhangqu,項目名稱:PriorityOkHttp,代碼行數:27,代碼來源:CacheAdapterTest.java

示例13: createOkResponseForCacheGet_withMissingStatusLine

import java.net.CacheResponse; //導入依賴的package包/類
/** 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) {
  }
}
 
開發者ID:lizhangqu,項目名稱:PriorityOkHttp,代碼行數:24,代碼來源:JavaApiConverterTest.java

示例14: fetchFromHTTPUrlConnectionCache

import java.net.CacheResponse; //導入依賴的package包/類
/**
 * 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;

}
 
開發者ID:nico-gonzalez,項目名稱:volley-it,代碼行數:28,代碼來源:MainActivity.java


注:本文中的java.net.CacheResponse類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。