public ClientResponse cacheIfPossible(ClientRequest request, BaseClientResponse response) throws Exception { String cc = (String) response.getResponseHeaders().getFirst(HttpHeaders.CACHE_CONTROL); String exp = (String) response.getResponseHeaders().getFirst(HttpHeaders.EXPIRES); int expires = -1; if (cc != null) { CacheControl cacheControl = CacheControl.valueOf(cc); if (cacheControl.isNoCache()) return response; expires = cacheControl.getMaxAge(); } else if (exp != null) { Date date = DateUtil.parseDate(exp); expires = (int) ((date.getTime() - System.currentTimeMillis()) / 1000); } String lastModified = (String) response.getResponseHeaders().getFirst(HttpHeaders.LAST_MODIFIED); String etag = (String) response.getResponseHeaders().getFirst(HttpHeaders.ETAG); String contentType = (String) response.getResponseHeaders().getFirst(HttpHeaders.CONTENT_TYPE); byte[] cached = ReadFromStream.readFromStream(1024, response.getStreamFactory().getInputStream()); response.getStreamFactory().performReleaseConnection(); MediaType mediaType = MediaType.valueOf(contentType); final BrowserCache.Entry entry = cache.put( request.getUri(), mediaType, response.getResponseHeaders(), cached, expires, etag, lastModified); response.setStreamFactory(new CachedStreamFactory(entry)); return response; }
@SuppressWarnings("rawtypes") public void handle(ClientResponse response) throws RuntimeException { try { BaseClientResponse r = (BaseClientResponse) response; String message = "SomeApplikationException. Status Code: " + response.getStatus(); InputStream stream = r.getStreamFactory().getInputStream(); stream.reset(); if (response.getStatus() == 523) { System.out.println( "ClientExceptionmapper: Mapped Stauscode 523 to SomeApplicationException"); throw new SomeApplicationException(message); } } catch (IOException e) { e.printStackTrace(); } }