use of java.net.CacheResponse in project android_frameworks_base by ParanoidAndroid.
the class HttpResponseCacheTest method testGetInstalledWithWrongTypeInstalled.
public void testGetInstalledWithWrongTypeInstalled() {
ResponseCache.setDefault(new ResponseCache() {
@Override
public CacheResponse get(URI uri, String requestMethod, Map<String, List<String>> requestHeaders) {
return null;
}
@Override
public CacheRequest put(URI uri, URLConnection connection) {
return null;
}
});
assertNull(HttpResponseCache.getInstalled());
}
use of java.net.CacheResponse in project phonegap-facebook-plugin by Wizcorp.
the class HttpEngine method initResponseSource.
/**
* 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() || policy.responseCache == null) {
return;
}
CacheResponse candidate = policy.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();
}
}
use of java.net.CacheResponse in project robovm by robovm.
the class URLConnectionTest method backdoorUrlToUri.
/**
* Exercises HttpURLConnection to convert URL to a URI. Unlike URL#toURI,
* HttpURLConnection recovers from URLs with unescaped but unsupported URI
* characters like '{' and '|' by escaping these characters.
*/
private URI backdoorUrlToUri(URL url) throws Exception {
final AtomicReference<URI> uriReference = new AtomicReference<URI>();
ResponseCache.setDefault(new ResponseCache() {
@Override
public CacheRequest put(URI uri, URLConnection connection) throws IOException {
return null;
}
@Override
public CacheResponse get(URI uri, String requestMethod, Map<String, List<String>> requestHeaders) throws IOException {
uriReference.set(uri);
throw new UnsupportedOperationException();
}
});
try {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.getResponseCode();
} catch (Exception expected) {
}
return uriReference.get();
}
use of java.net.CacheResponse in project robovm by robovm.
the class HttpEngine method initResponseSource.
/**
* 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();
}
}
use of java.net.CacheResponse in project okhttp by square.
the class ResponseCacheTest method assertCached.
private void assertCached(boolean shouldPut, int responseCode) throws Exception {
int expectedResponseCode = responseCode;
server = new MockWebServer();
MockResponse mockResponse = new MockResponse().addHeader("Last-Modified: " + formatDate(-1, TimeUnit.HOURS)).addHeader("Expires: " + formatDate(1, TimeUnit.HOURS)).setResponseCode(responseCode).setBody("ABCDE").addHeader("WWW-Authenticate: challenge");
if (responseCode == HttpURLConnection.HTTP_PROXY_AUTH) {
mockResponse.addHeader("Proxy-Authenticate: Basic realm=\"protected area\"");
} else if (responseCode == HttpURLConnection.HTTP_UNAUTHORIZED) {
mockResponse.addHeader("WWW-Authenticate: Basic realm=\"protected area\"");
} else if (responseCode == HttpURLConnection.HTTP_NO_CONTENT || responseCode == HttpURLConnection.HTTP_RESET) {
// We forbid bodies for 204 and 205.
mockResponse.setBody("");
}
server.enqueue(mockResponse);
if (responseCode == HttpURLConnection.HTTP_CLIENT_TIMEOUT) {
// 408's are a bit of an outlier because we may repeat the request if we encounter this
// response code. In this scenario, there are 2 responses: the initial 408 and then the 200
// because of the retry. We just want to ensure the initial 408 isn't cached.
expectedResponseCode = 200;
server.enqueue(new MockResponse().setHeader("Cache-Control", "no-store").setBody("FGHIJ"));
}
server.start();
URL url = server.url("/").url();
HttpURLConnection connection = openConnection(url);
assertEquals(expectedResponseCode, connection.getResponseCode());
// Exhaust the content stream.
readAscii(connection);
CacheResponse cached = cache.get(url.toURI(), "GET", null);
if (shouldPut) {
assertNotNull(Integer.toString(responseCode), cached);
} else {
assertNull(Integer.toString(responseCode), cached);
}
// tearDown() isn't sufficient; this test starts multiple servers
server.shutdown();
}
Aggregations