@Override public void close() throws IOException { available = false; super.close(); } }
@Override public InputStream getInputStream() { return delegate.getInputStream(); }
@Override public long length() { return delegate.length(); }
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()); }
private <A> Optional<A> lift(Payload p, ThrowableFunction<Payload, A, IOException> f) { try(InputStream is = p.getInputStream()) { InputStreamPayload isp = new InputStreamPayload(is, p.getMimeType(), p.length()); return Optional.ofNullable(f.apply(isp)); } catch (IOException e) { throw new HTTPException(e); } }
protected void writeBody(PrintStream writer, Optional<Payload> payload) { payload.ifPresent(p -> { writer.print("\r\n"); if (p.isAvailable()) { try (InputStream stream = p.getInputStream()) { IOUtils.copy(stream, writer); writer.print("\r\n"); } catch (IOException e) { throw new HTTPException("Unable to write the body of the response", e); } } }); } }
private Payload createRealPayload(Key key, FilePayload payload) throws IOException { File file = fileManager.moveFile(payload.getFile(), key); if (file != null && file.exists()) { return new FilePayload(file, payload.getMimeType()); } return null; }
@Override protected Payload createPayload(Key key, Payload payload, InputStream stream) throws IOException { File file = fileManager.createFile(tmpKey(key), stream); if (file != null && file.exists()) { return new FilePayload(file, payload.getMimeType()); } return null; }
public void consume() { payload.ifPresent(p -> { try(InputStream is = p.getInputStream()) { if (null != is) { is.close(); } } catch (IOException ignored){} }); }
public boolean isStale(LocalDateTime requestTime) { if (response.hasPayload() && !response.getPayload().get().isAvailable()) { return true; } return ttl - getAge(requestTime) <= 0; }
/** * Returns all headers with the headers from the Payload * * @return All the headers */ public Headers getAllHeaders() { Headers requestHeaders = getHeaders(); requestHeaders = hasPayload() ? requestHeaders.withContentType(getPayload().get().getMimeType()) : requestHeaders; //We don't want to add headers more than once. return requestHeaders; }
@Override public boolean isAvailable() { return delegate.isAvailable(); } }
@Override public MIMEType getMimeType() { return delegate.getMimeType(); }
public static StringPayload fromInputStream(InputStream is, MIMEType mime) throws IOException { return fromInputStream(is, mime, StandardCharsets.UTF_8); }
public StringPayload(String value, MIMEType mimeType, Charset charset) { this.delegate = new ByteArrayPayload(value.getBytes(charset), mimeType); this.value = value; this.charset = charset; }
public static StringPayload fromInputStream(InputStream is, MIMEType mime, Charset charset) throws IOException { try(InputStream i = is) { return new StringPayload(new String(IOUtils.toByteArray(i), charset), mime, charset); } }
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 default <A> Optional<A> transform(final Function<InputStream, A> f) { try(InputStream is = getInputStream()) { return Optional.ofNullable(f.apply(is)); } catch (IOException e) { throw new HTTPException(e); } } }
@Override public void close() throws IOException { try { super.close(); } finally { connection.disconnect(); } }
@Override public void close() throws IOException { super.close(); JdbcUtil.close(rs); JdbcUtil.close(connection); } }