@Internal public HTTPResponse withHeaders(Headers headers) { return new HTTPResponse(payload, statusLine, headers); }
@Internal public HTTPResponse withPayload(Payload payload) { return new HTTPResponse(Optional.ofNullable(payload), statusLine, headers); }
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 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); } }
public CacheItemHolder mapRow(ResultSet rs) throws SQLException { URI uri = URI.create(rs.getString("uri")); Vary vary = convertToVary(rs.getString("vary")); Key key = Key.create(uri, vary); File file = manager.resolve(key); Payload payload = null; if (file.exists()) { payload = new FilePayload(file, MIMEType.valueOf(rs.getString("mimetype"))); } Status status = Status.valueOf(rs.getInt("status")); Headers headers = convertToHeaders(rs.getString("headers")); DateTime cacheTime = new DateTime(rs.getTimestamp("cachetime").getTime()); HTTPResponse response = new HTTPResponse(payload, status, headers); return new CacheItemHolder(uri, vary, new DefaultCacheItem(response, cacheTime)); } }
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()); }
return new HTTPResponse(payload.get(), response.getStatus(), response.getHeaders());