@Override public Body createBody() throws IOException { return new PushBody(); }
public void feed(final ByteBuffer buffer, final boolean isLast) throws IOException { queue.offer(new BodyPart(buffer, isLast)); if (listener != null) { listener.onContentAdded(); } }
public long transferTo(WritableByteChannel target, long position) throws IOException { long count = this.count - position; if (count < 0 || position < 0) { throw new IllegalArgumentException("position out of range: " + position + " (expected: 0 - " + (this.count - 1) + ")"); } if (count == 0) { return 0L; } long bw = file.transferTo(this.position + position, count, target); byteWritten += bw; if (byteWritten == raf.length()) { releaseExternalResources(); } return bw; }
nettyBody = new NettyByteArrayBody(request.getByteData()); nettyBody = new NettyCompositeByteArrayBody(request.getCompositeByteData()); nettyBody = new NettyByteBufferBody(StringUtils.charSequence2ByteBuffer(request.getStringData(), bodyCharset)); nettyBody = new NettyInputStreamBody(request.getStreamData()); contentType = HttpHeaders.Values.APPLICATION_X_WWW_FORM_URLENCODED; nettyBody = new NettyByteBufferBody(urlEncodeFormParams(request.getFormParams(), bodyCharset), contentType); nettyBody = new NettyMultipartBody(request.getParts(), request.getHeaders(), nettyConfig); nettyBody = new NettyFileBody(request.getFile(), nettyConfig); nettyBody = new NettyFileBody(fileBodyGenerator.getFile(), fileBodyGenerator.getRegionSeek(), fileBodyGenerator.getRegionLength(), nettyConfig); nettyBody = new NettyInputStreamBody(InputStreamBodyGenerator.class.cast(request.getBodyGenerator()).getInputStream()); nettyBody = new NettyBodyBody(request.getBodyGenerator().createBody(), nettyConfig);
private NettyBody body(Request request, HttpMethod method) throws IOException { NettyBody nettyBody = null; if (method != HttpMethod.CONNECT) { Charset bodyCharset = request.getBodyEncoding() == null ? DEFAULT_CHARSET : Charset.forName(request.getBodyEncoding()); if (request.getByteData() != null) nettyBody = new NettyByteArrayBody(request.getByteData()); else if (request.getStringData() != null) nettyBody = new NettyByteArrayBody(request.getStringData().getBytes(bodyCharset)); else if (request.getStreamData() != null) nettyBody = new NettyInputStreamBody(request.getStreamData()); else if (isNonEmpty(request.getFormParams())) { String contentType = null; if (!request.getHeaders().containsKey(HttpHeaders.Names.CONTENT_TYPE)) contentType = HttpHeaders.Values.APPLICATION_X_WWW_FORM_URLENCODED; nettyBody = new NettyByteArrayBody(computeBodyFromParams(request.getFormParams(), bodyCharset), contentType); } else if (isNonEmpty(request.getParts())) nettyBody = new NettyMultipartBody(request.getParts(), request.getHeaders(), nettyConfig); else if (request.getFile() != null) nettyBody = new NettyFileBody(request.getFile(), nettyConfig); else if (request.getBodyGenerator() instanceof FileBodyGenerator) { FileBodyGenerator fileBodyGenerator = (FileBodyGenerator) request.getBodyGenerator(); nettyBody = new NettyFileBody(fileBodyGenerator.getFile(), fileBodyGenerator.getRegionSeek(), fileBodyGenerator.getRegionLength(), nettyConfig); } else if (request.getBodyGenerator() instanceof InputStreamBodyGenerator) nettyBody = new NettyInputStreamBody(InputStreamBodyGenerator.class.cast(request.getBodyGenerator()).getInputStream()); else if (request.getBodyGenerator() != null) nettyBody = new NettyBodyBody(request.getBodyGenerator().createBody(), nettyConfig); } return nettyBody; }
msg = new BodyFileRegion((RandomAccessBody) body); msg = new BodyChunkedInput(body); feedableBodyGenerator.writeChunkBoundaries(); feedableBodyGenerator.setListener(new FeedListener() { @Override public void onContentAdded() {
NettyRequest nettyRequest; if (body instanceof NettyDirectBody) { ChannelBuffer buffer = NettyDirectBody.class.cast(body).channelBuffer(); httpRequest = new DefaultHttpRequest(httpVersion, method, requestUri); if (body.getContentLength() < 0) headers.set(HttpHeaders.Names.TRANSFER_ENCODING, HttpHeaders.Values.CHUNKED); else headers.set(HttpHeaders.Names.CONTENT_LENGTH, body.getContentLength()); if (body.getContentType() != null) headers.set(HttpHeaders.Names.CONTENT_TYPE, body.getContentType());
NettyRequest nettyRequest; if (body instanceof NettyByteArrayBody) { byte[] bytes = NettyByteArrayBody.class.cast(body).getBytes(); httpRequest = new DefaultHttpRequest(httpVersion, method, requestUri); if (body.getContentLength() > 0) httpRequest.headers().set(HttpHeaders.Names.CONTENT_LENGTH, body.getContentLength()); else httpRequest.headers().set(HttpHeaders.Names.TRANSFER_ENCODING, HttpHeaders.Values.CHUNKED); if (body.getContentType() != null) httpRequest.headers().set(HttpHeaders.Names.CONTENT_TYPE, body.getContentType());
@Override public void write(final Channel channel, NettyResponseFuture<?> future, AsyncHttpClientConfig config) throws IOException { Object msg; if (body instanceof RandomAccessBody && !ChannelManager.isSslHandlerConfigured(channel.getPipeline()) && !nettyConfig.isDisableZeroCopy()) { msg = new BodyFileRegion((RandomAccessBody) body); } else { msg = new BodyChunkedInput(body); BodyGenerator bg = future.getRequest().getBodyGenerator(); if (bg instanceof FeedableBodyGenerator) { FeedableBodyGenerator.class.cast(bg).setListener(new FeedListener() { @Override public void onContentAdded() { channel.getPipeline().get(ChunkedWriteHandler.class).resumeTransfer(); } }); } } channel.write(msg).addListener(new ProgressListener(config, future.getAsyncHandler(), future, false) { public void operationComplete(ChannelFuture cf) { closeSilently(body); super.operationComplete(cf); } }); } }
@Override public void write(Channel channel, NettyResponseFuture<?> future, AsyncHttpClientConfig config) throws IOException { final RandomAccessFile raf = new RandomAccessFile(file, "r"); try { ChannelFuture writeFuture; if (ChannelManager.isSslHandlerConfigured(channel.getPipeline()) || nettyConfig.isDisableZeroCopy()) { writeFuture = channel.write(new ChunkedFile(raf, offset, raf.length(), nettyConfig.getChunkedFileChunkSize())); } else { final FileRegion region = new OptimizedFileRegion(raf, offset, raf.length()); writeFuture = channel.write(region); } writeFuture.addListener(new ProgressListener(config, future.getAsyncHandler(), future, false) { public void operationComplete(ChannelFuture cf) { closeSilently(raf); super.operationComplete(cf); } }); } catch (IOException ex) { closeSilently(raf); throw ex; } } }
channel.write(new BodyChunkedInput(body)).addListener(new ProgressListener(config, future.getAsyncHandler(), future, false) { public void operationComplete(ChannelFuture cf) { closeSilently(body);
public <T> void writeRequest(NettyResponseFuture<T> future, Channel channel) { NettyRequest nettyRequest = future.getNettyRequest(); HttpRequest httpRequest = nettyRequest.getHttpRequest(); AsyncHandler<T> handler = future.getAsyncHandler(); // if the channel is dead because it was pooled and the remote // server decided to close it, // we just let it go and the channelInactive do its work if (!Channels.isChannelValid(channel)) return; try { if (handler instanceof TransferCompletionHandler) configureTransferAdapter(handler, httpRequest); if (!future.isHeadersAlreadyWrittenOnContinue()) { if (future.getAsyncHandler() instanceof AsyncHandlerExtensions) AsyncHandlerExtensions.class.cast(future.getAsyncHandler()).onSendRequest(nettyRequest); channel.write(httpRequest).addListener(new ProgressListener(config, future.getAsyncHandler(), future, true)); } if (nettyRequest.getBody() != null && !future.isDontWriteBodyBecauseExpectContinue() && httpRequest.getMethod() != HttpMethod.CONNECT) nettyRequest.getBody().write(channel, future, config); // don't bother scheduling timeouts if channel became invalid if (Channels.isChannelValid(channel)) scheduleTimeouts(future); } catch (Throwable t) { LOGGER.error("Can't write request", t); Channels.silentlyCloseChannel(channel); } }
writeRequest(f.getChannel(), poolKey); abortChannelPreemption(poolKey); Throwable cause = f.getCause();
state = writeChunkBoundaries ? PushBodyState.CLOSING : PushBodyState.FINISHED; return read(buffer);
private void writeRequest(Channel channel, String poolKey) { LOGGER.debug("Request using non cached Channel '{}':\n{}\n", channel, future.getNettyRequest().getHttpRequest()); if (future.isDone()) { abortChannelPreemption(poolKey); return; } channelManager.registerOpenChannel(channel); future.attachChannel(channel, false); requestSender.writeRequest(future, channel); }
channelFuture.addListener(new NettyConnectListener<T>(config, future, this, channelManager, channelPreempted, poolKey));
public long transferTo(WritableByteChannel target, long position) throws IOException { long count = this.count - position; if (count < 0 || position < 0) { throw new IllegalArgumentException("position out of range: " + position + " (expected: 0 - " + (this.count - 1) + ")"); } if (count == 0) { return 0L; } long bw = file.transferTo(this.position + position, count, target); byteWritten += bw; if (byteWritten == raf.length()) { releaseExternalResources(); } return bw; }
@Override public void write(Channel channel, NettyResponseFuture<?> future, AsyncHttpClientConfig config) throws IOException { final RandomAccessFile raf = new RandomAccessFile(file, "r"); try { ChannelFuture writeFuture; if (ChannelManager.isSslHandlerConfigured(channel.getPipeline()) || nettyConfig.isDisableZeroCopy()) { writeFuture = channel.write(new ChunkedFile(raf, offset, raf.length(), nettyConfig.getChunkedFileChunkSize())); } else { final FileRegion region = new OptimizedFileRegion(raf, offset, raf.length()); writeFuture = channel.write(region); } writeFuture.addListener(new ProgressListener(config, future.getAsyncHandler(), future, false) { public void operationComplete(ChannelFuture cf) { closeSilently(raf); super.operationComplete(cf); } }); } catch (IOException ex) { closeSilently(raf); throw ex; } } }
channel.write(new BodyChunkedInput(body)).addListener(new ProgressListener(config, future.getAsyncHandler(), future, false) { public void operationComplete(ChannelFuture cf) { closeSilently(body);
nettyRequest.getBody().write(channel, future, config);