/** * @return true if the target server returned an error at some point during the request */ public boolean sawErrorResponse() { for (Pair<HttpRequest, HttpResponse> event : requestTrace) { if (event.two == null || event.two.isError()) { return true; } } return false; }
/** * @return true if the target server returned an error at some point during the request */ public boolean sawErrorResponse() { for (Pair<HttpRequest, HttpResponse> event : requestTrace) { if (event.two == null || event.two.isError()) { return true; } } return false; }
/** * @return true if the target server returned an error at some point during the request */ public boolean sawErrorResponse() { for (Pair<HttpRequest, HttpResponse> event : requestTrace) { if (event.two == null || event.two.isError()) { return true; } } return false; }
/** * @return true if a strict no-cache header is set in Cache-Control or Pragma */ public boolean isStrictNoCache() { if (isError() && !NEGATIVE_CACHING_EXEMPT_STATUS.contains(httpStatusCode)) { return false; } String cacheControl = getHeader(HttpHeaders.CACHE_CONTROL); if (cacheControl != null) { String[] directives = StringUtils.split(cacheControl, ','); for (String directive : directives) { directive = directive.trim(); if (directive.equalsIgnoreCase("no-cache") || directive.equalsIgnoreCase("no-store") || directive.equalsIgnoreCase("private")) { return true; } } } for (String pragma : getHeaders(HttpHeaders.PRAGMA)) { if ("no-cache".equalsIgnoreCase(pragma)) { return true; } } return false; }
/** * @return true if a strict no-cache header is set in Cache-Control or Pragma */ public boolean isStrictNoCache() { if (isError() && !NEGATIVE_CACHING_EXEMPT_STATUS.contains(httpStatusCode)) { return false; } String cacheControl = getHeader("Cache-Control"); if (cacheControl != null) { String[] directives = StringUtils.split(cacheControl, ','); for (String directive : directives) { directive = directive.trim(); if (directive.equalsIgnoreCase("no-cache") || directive.equalsIgnoreCase("no-store") || directive.equalsIgnoreCase("private")) { return true; } } } for (String pragma : getHeaders("Pragma")) { if ("no-cache".equalsIgnoreCase(pragma)) { return true; } } return false; }
/** * @return true if a strict no-cache header is set in Cache-Control or Pragma */ public boolean isStrictNoCache() { if (isError() && !NEGATIVE_CACHING_EXEMPT_STATUS.contains(httpStatusCode)) { return false; } String cacheControl = getHeader("Cache-Control"); if (cacheControl != null) { String[] directives = StringUtils.split(cacheControl, ','); for (String directive : directives) { directive = directive.trim(); if (directive.equalsIgnoreCase("no-cache") || directive.equalsIgnoreCase("no-store") || directive.equalsIgnoreCase("private")) { return true; } } } for (String pragma : getHeaders("Pragma")) { if ("no-cache".equalsIgnoreCase(pragma)) { return true; } } return false; }
/** * Process errors when fetching uri using request pipeline and return the * error response to be returned to the user if any. * @param results The http response returned by request pipeline. * @return An HttpResponse instance encapsulating error message and status * code to be returned to the user in case of errors, null otherwise. */ protected HttpResponse handleErrors(HttpResponse results) { if (results == null) { return new HttpResponseBuilder() .setHttpStatusCode(HttpResponse.SC_NOT_FOUND) .setResponse(ERROR_FETCHING_DATA.getBytes()) .create(); } if (results.isError()) { return results; } return null; } }
/** * Process errors when fetching uri using request pipeline and return the * error response to be returned to the user if any. * @param results The http response returned by request pipeline. * @return An HttpResponse instance encapsulating error message and status * code to be returned to the user in case of errors, null otherwise. */ protected HttpResponse handleErrors(HttpResponse results) { if (results == null) { return new HttpResponseBuilder() .setHttpStatusCode(HttpResponse.SC_NOT_FOUND) .setResponse(ERROR_FETCHING_DATA.getBytes()) .create(); } if (results.isError()) { return results; } return null; } }
/** * Process errors when fetching uri using request pipeline and return the * error response to be returned to the user if any. * @param results The http response returned by request pipeline. * @return An HttpResponse instance encapsulating error message and status * code to be returned to the user in case of errors, null otherwise. */ protected HttpResponse handleErrors(HttpResponse results) { if (results == null) { return new HttpResponseBuilder() .setHttpStatusCode(HttpResponse.SC_NOT_FOUND) .setResponse(ERROR_FETCHING_DATA.getBytes()) .create(); } if (results.isError()) { return results; } return null; } }
if (isError() && !NEGATIVE_CACHING_EXEMPT_STATUS.contains(httpStatusCode)) {
if (isError() && !NEGATIVE_CACHING_EXEMPT_STATUS.contains(httpStatusCode)) {
response.isError()) {
@Test public void testIsError() { // These aren't all valid status codes, but they're reserved in these blocks. Changes // would be required to the HTTP standard anyway before this test would be invalid. for (int i = 100; i < 400; i += 100) { for (int j = 0; j < 10; ++j) { HttpResponse response = new HttpResponseBuilder().setHttpStatusCode(i).create(); assertFalse("Status below 400 considered to be an error", response.isError()); } } for (int i = 400; i < 600; i += 100) { for (int j = 0; j < 10; ++j) { HttpResponse response = new HttpResponseBuilder().setHttpStatusCode(i).create(); assertTrue("Status above 400 considered to be an error", response.isError()); } } }
public HttpResponse addResponse(HttpRequest request, HttpResponse response) { HttpResponseBuilder responseBuilder; boolean storeStrictNoCacheResources = (refetchStrictNoCacheAfterMs >= 0); if (isCacheable(request, response, storeStrictNoCacheResources)) { if (storeStrictNoCacheResources && response.isStrictNoCache()) { responseBuilder = buildStrictNoCacheHttpResponse(response); } else { responseBuilder = new HttpResponseBuilder(response); } } else { return null; } int forcedTtl = request.getCacheTtl(); if (forcedTtl != -1 && !response.isError()) { responseBuilder.setCacheTtl(forcedTtl); } response = responseBuilder.create(); String keyString = createKey(request); addResponseImpl(keyString, response); return response; // cached and possibly modified }
@Test public void testIsError() { // These aren't all valid status codes, but they're reserved in these blocks. Changes // would be required to the HTTP standard anyway before this test would be invalid. for (int i = 100; i < 400; i += 100) { for (int j = 0; j < 10; ++j) { HttpResponse response = new HttpResponseBuilder().setHttpStatusCode(i).create(); assertFalse("Status below 400 considered to be an error", response.isError()); } } for (int i = 400; i < 600; i += 100) { for (int j = 0; j < 10; ++j) { HttpResponse response = new HttpResponseBuilder().setHttpStatusCode(i).create(); assertTrue("Status above 400 considered to be an error", response.isError()); } } }
if (!cachedResponse.isError()) {
@Test public void testIsError() { // These aren't all valid status codes, but they're reserved in these blocks. Changes // would be required to the HTTP standard anyway before this test would be invalid. for (int i = 100; i < 400; i += 100) { for (int j = 0; j < 10; ++j) { HttpResponse response = new HttpResponseBuilder().setHttpStatusCode(i).create(); assertFalse("Status below 400 considered to be an error", response.isError()); } } for (int i = 400; i < 600; i += 100) { for (int j = 0; j < 10; ++j) { HttpResponse response = new HttpResponseBuilder().setHttpStatusCode(i).create(); assertTrue("Status above 400 considered to be an error", response.isError()); } } }
if (results.isError()) {
if (response.isError()) { throw new RenderingException("Unable to reach remote host. HTTP status " + response.getHttpStatusCode(), HttpServletResponse.SC_NOT_FOUND);
if (response.isError()) { throw new RenderingException("Unable to reach remote host. HTTP status " + response.getHttpStatusCode(), HttpServletResponse.SC_NOT_FOUND);