本文整理汇总了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;
}
};
}
示例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);
}
示例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;
}
示例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"));
}
示例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) {
}
}
示例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());
}
示例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()));
}
示例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);
}
示例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);
}
}
示例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;
}
};
}
示例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);
}
示例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);
}
示例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) {
}
}
示例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;
}