@Override public void operationComplete(ChannelFuture future) throws Exception { try { Channel channel = future.channel(); HttpRequest request = RequestAdapter.adapt(marshalledRequest); // Send request channel.writeAndFlush(request); // Send chunked content channel.writeAndFlush(new HttpChunkedInput(new ChunkedStream(marshalledRequest.getContent()))); } catch (Exception e) { responseHandler.onFailure(e); } } });
@Override public void write(HttpRequest<?> request, MutableHttpResponse<?> response, ChannelHandlerContext context) { if (response instanceof NettyMutableHttpResponse) { FullHttpResponse nettyResponse = ((NettyMutableHttpResponse) response).getNativeResponse(); // Write the request data context.write(new DefaultHttpResponse(nettyResponse.protocolVersion(), nettyResponse.status(), nettyResponse.headers()), context.voidPromise()); context.writeAndFlush(new HttpChunkedInput(new ChunkedStream(getInputStream()))); } else { throw new IllegalArgumentException("Unsupported response type. Not a Netty response: " + response); } } }
ctx.write(new HttpChunkedInput(jerseyChunkedInput)).addListener(FLUSH_FUTURE); } else { ctx.write(new HttpChunkedInput(jerseyChunkedInput)).addListener(FLUSH_FUTURE);
sendFileFuture = context.writeAndFlush(new HttpChunkedInput(new ChunkedFile(raf, 0, getLength(), LENGTH_8K)), context.newProgressivePromise()); } catch (IOException e) {
HttpChunkedInput chunkedInput = new HttpChunkedInput( new ChunkedNioFile(channel, offset, count, bufferSize));
ch.write(new HttpChunkedInput(jerseyChunkedInput)); } else { ch.write(jerseyChunkedInput);
} else { sendFileFuture = ctx.writeAndFlush(new HttpChunkedInput(new ChunkedFile(raf, 128 * 1024)), ctx.newProgressivePromise());
new HttpChunkedInput( new ChunkedFile(raf, 0, fileLength, 8192) ),
new HttpChunkedInput( new ChunkedFile(raf, 0, fileLength, 8192) ),
} else { sendFileFuture = ctx.writeAndFlush(new HttpChunkedInput(new ChunkedFile(raf, 0, fileLength, 8192)), ctx.newProgressivePromise());
// Write the content. ChannelFuture sendFileFuture; ChannelFuture lastContentFuture; if (ctx.pipeline().get(SslHandler.class) == null) { sendFileFuture = ctx.write(new DefaultFileRegion(raf.getChannel(), 0, fileLength), ctx.newProgressivePromise()); // Write the end marker. lastContentFuture = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT); // <= last writeAndFlush } else { sendFileFuture = ctx.writeAndFlush(new HttpChunkedInput(new ChunkedFile(raf, 0, fileLength, 8192)), ctx.newProgressivePromise()); // <= last writeAndFlush // HttpChunkedInput will write the end marker (LastHttpContent) for us. lastContentFuture = sendFileFuture; }
@Override public void operationComplete(ChannelFuture future) throws Exception { if (!future.isSuccess()) { callBodyProducerHandleError(bodyProducer, future.cause()); channel.close(); return; } channel.writeAndFlush(new HttpChunkedInput(new BodyProducerChunkedInput(bodyProducer, contentLength))) .addListener(createBodyProducerCompletionListener(bodyProducer)); } });
protected boolean writeResponse(ChannelHandlerContext ctx, FullHttpRequest request, ChunkedInput<ByteBuf> content, int length) { // Build the response object. HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK); return internalWriteResponse(ctx, request, response, length, () -> { HttpChunkedInput chunked = new HttpChunkedInput(content, LastHttpContent.EMPTY_LAST_CONTENT); ctx.write(response); return ctx.writeAndFlush(chunked); }); }
private ChannelFuture writeStaticFile(ChannelHandlerContext ctx, String resourceFullPath) { InputStream inputStream = getClass().getResourceAsStream(resourceFullPath); if (inputStream == null) { return writeJsonResponse(ctx, NOT_FOUND, NOT_FOUND_RESPONSE); } DefaultHttpResponse resp = new DefaultHttpResponse(HTTP_1_1, OK); resp.headers().set(CONNECTION, isKeepAlive ? KEEP_ALIVE : CLOSE); resp.headers().set(CONTENT_TYPE, mimeTypesMap.getContentType(resourceFullPath)); HttpUtil.setTransferEncodingChunked(resp, true); ctx.write(resp); return ctx.writeAndFlush(new HttpChunkedInput(new ChunkedStream(inputStream))); }
@Override public void operationComplete(ChannelFuture future) throws Exception { try { Channel channel = future.channel(); HttpRequest request = RequestAdapter.adapt(marshalledRequest); // Send request channel.writeAndFlush(request); // Send chunked content channel.writeAndFlush(new HttpChunkedInput(new ChunkedStream(marshalledRequest.getContent()))); } catch (Exception e) { responseHandler.onFailure(e); } } });
@Override public ChunkedInput<HttpContent> chunkFile(FileChannel fileChannel, long offset, long length, int chunkSize) { try { return new HttpChunkedInput(new ChunkedNioFile(fileChannel, offset, length, chunkSize)); } catch (IOException e) { throw Exceptions.propagate(e); } }
private ChannelFuture sendFile(ChannelHandlerContext ctx, FileChunk file) throws IOException { RandomAccessFile raf; try { raf = new RandomAccessFile(file.getFile(), "r"); } catch (FileNotFoundException fnfe) { return null; } ChannelFuture writeFuture; ChannelFuture lastContentFuture; if (ctx.pipeline().get(SslHandler.class) != null) { // Cannot use zero-copy with HTTPS. lastContentFuture = ctx.write(new HttpChunkedInput(new ChunkedFile(raf, file.startOffset(), file.length(), 8192))); } else { // No encryption - use zero-copy. final FileRegion region = new DefaultFileRegion(raf.getChannel(), file.startOffset(), file.length()); writeFuture = ctx.write(region); lastContentFuture = ctx.write(LastHttpContent.EMPTY_LAST_CONTENT); writeFuture.addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) { if (region.refCnt() > 0) { region.release(); } } }); } return lastContentFuture; }
@Override public void sendFile(File file, String contentType, @Nullable Multimap<String, String> headers) throws IOException { Preconditions.checkArgument(responded.compareAndSet(false, true), "Response has been already sent"); HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); setCustomHeaders(response, headers); response.headers().set(HttpHeaders.Names.CONTENT_TYPE, contentType); if (!response.headers().contains(HttpHeaders.Names.CONTENT_LENGTH)) { response.headers().set(HttpHeaders.Names.TRANSFER_ENCODING, HttpHeaders.Values.CHUNKED); } channel.writeAndFlush(response); HttpChunkedInput httpChunkWriter = new HttpChunkedInput(new ChunkedFile(file)); ChannelFuture sendFileFuture = channel.write(httpChunkWriter); final boolean responseKeepAlive = setResponseKeepAlive(response); sendFileFuture.addListener(future -> { if (!responseKeepAlive) { channel.close(); } }); }
@Override public void transmit(HttpResponseStatus status, Path file) { String sizeString = responseHeaders.getAsString(HttpHeaderConstants.CONTENT_LENGTH); long size = sizeString == null ? 0 : Long.parseLong(sizeString); boolean compress = !responseHeaders.contains(HttpHeaderConstants.CONTENT_ENCODING, HttpHeaderConstants.IDENTITY, true); if (!isSsl && !compress && file.getFileSystem().equals(FileSystems.getDefault())) { FileChannel fileChannel; try { fileChannel = FileChannel.open(file, OPEN_OPTIONS); } catch (IOException e) { throw new UncheckedIOException(e); } FileRegion defaultFileRegion = new DefaultFileRegion(fileChannel, 0, size); transmit(status, defaultFileRegion, true); } else { Blocking.get(() -> Files.newByteChannel(file) ).then(fileChannel -> transmit(status, new HttpChunkedInput(new ChunkedNioStream(fileChannel)), false) ); } }
manageOsCache, readaheadLength, readaheadPool, file.getFile().getAbsolutePath()); writeFuture = ctx.write(new HttpChunkedInput(chunk));