private HTTPResponse createCacheableResponse(Key key, HTTPResponse response) throws IOException { Optional<Payload> payload = Optional.empty(); if (response.hasPayload()) { Payload p = response.getPayload().get(); File file = fileManager.createFile(key, p.getInputStream()); if (file != null && file.exists()) { payload = Optional.of(new FilePayload(file, p.getMimeType())); } } return new HTTPResponse(payload, response.getStatusLine(), response.getHeaders()); }
public Properties toProperties() { Properties object = new Properties(); object.setProperty("cache-time", HeaderUtils.toHttpDate("cache-time", item.getCachedTime()).getValue()); HTTPResponse response = item.getResponse(); object.setProperty("status", String.valueOf(response.getStatus().getCode())); if (response.hasPayload()) { FilePayload payload = (FilePayload) response.getPayload().get(); object.setProperty("file", payload.getFile().getAbsolutePath()); } object.setProperty("headers", response.getHeaders().toString()); return object; }
public <A> Optional<A> transform(final ThrowableFunction<Payload, A, IOException> f) { if (hasPayload()) { return payload.flatMap(p -> lift(p, f)); } return Optional.empty(); }
public void write(HTTPResponse response, PrintStream stream) { writeStatus(stream, response.getStatusLine()); writeHeaders(stream, response.getHeaders()); if (response.hasPayload()) { writeBody(stream, response.getPayload()); } stream.flush(); }
public boolean isStale(LocalDateTime requestTime) { if (response.hasPayload() && !response.getPayload().get().isAvailable()) { return true; } return ttl - getAge(requestTime) <= 0; }
return new HTTPResponse(payload.get(), response.getStatus(), response.getHeaders());
protected HTTPResponse resolveProxy(final HTTPRequest request, final HTTPResponse response) throws IOException { HTTPRequest req = getProxyAuthenticator().prepareAuthentication(request, response); if (req != request) { response.consume(); HTTPResponse newResponse = null; try { newResponse = resolveImpl(req); return newResponse; } finally { if (newResponse != null) { if (newResponse.getStatus() == Status.PROXY_AUTHENTICATION_REQUIRED) { //We failed getProxyAuthenticator().afterFailedAuthentication(newResponse.getHeaders()); } else { getProxyAuthenticator().afterSuccessfulAuthentication(newResponse.getHeaders()); } } } } return response; }
@Internal public HTTPResponse withHeaders(Headers headers) { return new HTTPResponse(payload, statusLine, headers); }
protected List<AuthScheme> toAuthSchemes(HTTPResponse response, String name) { List<Header> authenticateHeader = response.getHeaders().getHeaders(name); return authenticateHeader.stream(). flatMap( h -> h.getDirectives().stream()). map(AuthScheme::new). collect(Collectors.toList()); } }
@Override protected HTTPResponse putImpl(Key key, HTTPResponse response) { HTTPResponse resolvedResponse = response.getPayload().flatMap(p -> { if (persistablePayload(p)) { try { return Optional.of(response.withPayload(createRealPayload(key, (FilePayload)p))); } catch (IOException ignore) {} } return Optional.empty(); }).orElse(response); HTTPResponse res = super.putImpl(key, resolvedResponse); if (serializationPolicy.shouldWePersist(modCount++, lastSerialization)) { lastSerialization = System.currentTimeMillis(); saveCacheToDisk(); } return res; }
private HTTPResponse resolveAuthenticated(final HTTPRequest request) throws IOException { HTTPResponse convertedResponse; HTTPRequest req = request; if (getAuthenticator().canAuthenticatePreemptively(request)) { req = getAuthenticator().preparePreemptiveAuthentication(request); } if (getProxyAuthenticator().canAuthenticatePreemptively()) { req = getProxyAuthenticator().preparePreemptiveAuthentication(req); } convertedResponse = resolveImpl(req); if (convertedResponse.getStatus() == Status.PROXY_AUTHENTICATION_REQUIRED) { return resolveProxy(req, convertedResponse); } if (convertedResponse.getStatus() == Status.UNAUTHORIZED) { return resolveUnauthorized(req, convertedResponse); } return convertedResponse; }
protected HTTPResponse resolveUnauthorized(final HTTPRequest request, final HTTPResponse response) throws IOException { HTTPRequest req = getAuthenticator().prepareAuthentication(request, response); if (req != request) { response.consume(); HTTPResponse newResponse = null; try { newResponse = resolveImpl(req); return newResponse; } finally { if (newResponse != null) { if (newResponse.getStatus() == Status.UNAUTHORIZED) { //We failed getAuthenticator().afterFailedAuthentication(req, newResponse.getHeaders()); } else { getAuthenticator().afterSuccessfulAuthentication(req, newResponse.getHeaders()); } } } } return response; }
@Internal public HTTPResponse withPayload(Payload payload) { return new HTTPResponse(Optional.ofNullable(payload), statusLine, headers); }
@Override public void doWith(Connection connection) throws SQLException { PreparedStatement statement = null; try { statement = connection.prepareStatement("update response set headers = ?, cachetime = ? where uri = ? and vary = ?"); statement.setString(1, response.getHeaders().toJSON()); statement.setTimestamp(2, new Timestamp(DateTimeUtils.currentTimeMillis())); statement.setString(3, key.getURI().toString()); statement.setString(4, key.getVary().toJSON()); statement.executeUpdate(); } finally { JdbcUtil.close(statement); } } });
public final HTTPRequest prepareAuthentication(final HTTPRequest request, final HTTPResponse response) { if (configuration.getHost() != null) { if (response == null && registry.matches(configuration.getHost())) { //preemptive auth. AuthScheme authScheme = registry.get(configuration.getHost()); return doAuth(request, Arrays.asList(authScheme)); } if (response != null && response.getStatus() == Status.PROXY_AUTHENTICATION_REQUIRED) { if (proxyChallenge == null) { proxyChallenge = configuration.getProvider().getChallenge(); } if (proxyChallenge != null) { List<AuthScheme> schemes = toAuthSchemes(response, HeaderConstants.PROXY_AUTHENTICATE); return doAuth(request, schemes); } } } return request; }
statement.setString(1, key.getURI().toString()); statement.setString(2, key.getVary().toJSON()); statement.setInt(3, response.getStatus().getCode()); statement.setString(4, response.getHeaders().toJSON()); if (response.hasPayload() && response.getPayload().isAvailable()) { statement.setString(5, response.getPayload().getMimeType().toString()); try { File file = manager.createFile(key, response.getPayload().getInputStream()); if (file != null && file.exists()) { payload.set(new FilePayload(file, response.getPayload().getMimeType()));
public static CacheItem parse(Properties object) { Optional<LocalDateTime> time = HeaderUtils.fromHttpDate(new Header("cache-time", object.getProperty("cache-time"))); Status status = Status.valueOf(NumberUtils.toInt(object.getProperty("status"), 200)); Headers headers = Headers.parse(object.getProperty("headers")); Optional<Payload> p = Optional.empty(); if (object.containsKey("file")) { p = Optional.of(new FilePayload(new File(object.getProperty("file")), headers.getContentType().get())); } return new DefaultCacheItem(new HTTPResponse(p, status, headers), time.get()); }
public static long getTTL(HTTPResponse response, int defaultTTLinSeconds) { final Optional<CacheControl> cc = response.getHeaders().getCacheControl(); if (cc.isPresent()) { int maxAge = cc.get().getMaxAge(); if (maxAge > 0) { return maxAge; } } /** * HTTP/1.1 clients and caches MUST treat other invalid date formats, especially including the value "0", as in the past (i.e., "already expired"). * To mark a response as "already expired," an origin server sends an Expires date that is equal to the Date header value. * (See the rules for expiration calculations in section 13.2.4.) * To mark a response as "never expires," an origin server sends an Expires date approximately one year from the time the response is sent. * HTTP/1.1 servers SHOULD NOT send Expires dates more than one year in the future. */ Optional<LocalDateTime> expires = response.getHeaders().getExpires(); if (expires.isPresent()) { LocalDateTime expiryDate = expires.get(); Optional<LocalDateTime> date = response.getHeaders().getDate(); if (OptionalUtils.exists(date, dt -> dt.isBefore(expiryDate))) { return Duration.between(date.get(), expiryDate).getSeconds(); } } return defaultTTLinSeconds; }
public final HTTPRequest prepareAuthentication(final HTTPRequest request, final HTTPResponse response) { HTTPRequest req = request; HTTPHost host = new HTTPHost(request.getRequestURI()); if (response == null && registry.matches(host)) { //preemptive auth. AuthScheme authScheme = registry.get(host); Pair<AuthenticatorStrategy, AuthScheme> selected = select(Arrays.asList(authScheme)); return selected.getKey().prepare(request, authScheme); } if (response != null && response.getStatus() == Status.UNAUTHORIZED) { List<AuthScheme> schemes = toAuthSchemes(response, HeaderConstants.WWW_AUTHENTICATE); if (!schemes.isEmpty() && request.getChallenge() != null) { Pair<AuthenticatorStrategy, AuthScheme> selected = select(schemes); if (selected.getValue() != null) { req = selected.getKey().prepare(request, selected.getValue()); if (req != request) { //If authentication header was added registry.register(host, selected.getValue()); } } } } return req; }
public static HTTPResponse createResponse(final StatusLine line, final Headers responseHeaders, final Optional<InputStream> stream) { Optional<String> contentLengthHeader = responseHeaders.getFirstHeaderValue(HeaderConstants.CONTENT_LENGTH); MIMEType type = responseHeaders.getContentType().orElse(MIMEType.APPLICATION_OCTET_STREAM); Optional<Long> length = responseHeaders.getContentLength(); Optional<Payload> payload = stream. filter(is -> line.getStatus().isBodyContentAllowed()). map(is -> new InputStreamPayload(is, type, length.orElse(-1L))); return new HTTPResponse(payload, line, responseHeaders); } }