@Override public int available() throws IOException { synchronized (lock) { poll(0); return readableBytes(); } }
@Override public int read() throws IOException { byte[] b = new byte[1]; int n = read(b, 0, 1); return n != -1 ? b[0] : -1; }
@Override protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception { invokeCallbackOnFirstRead(); stream.write(msg.copy()); }
FramedResponseStreamHandler streamHandler = new FramedResponseStreamHandler(resultCallback); HttpResponseHandler responseHandler = new HttpResponseHandler(requestProvider, resultCallback); HttpConnectionHijackHandler hijackHandler = new HttpConnectionHijackHandler(responseHandler); hijackHandler.awaitUpgrade();
public <T> void post(final Object entity, TypeReference<T> typeReference, final ResultCallback<T> resultCallback) { HttpRequestProvider requestProvider = httpPostRequestProvider(entity); Channel channel = getChannel(); JsonResponseCallbackHandler<T> jsonResponseHandler = new JsonResponseCallbackHandler<T>(typeReference, resultCallback); HttpResponseHandler responseHandler = new HttpResponseHandler(requestProvider, resultCallback); channel.pipeline().addLast(responseHandler); channel.pipeline().addLast(new JsonObjectDecoder()); channel.pipeline().addLast(jsonResponseHandler); sendRequest(requestProvider, channel); return; }
public InputStream post(final Object entity) { HttpRequestProvider requestProvider = httpPostRequestProvider(entity); Channel channel = getChannel(); AsyncResultCallback<InputStream> callback = new AsyncResultCallback<>(); HttpResponseHandler responseHandler = new HttpResponseHandler(requestProvider, callback); HttpResponseStreamHandler streamHandler = new HttpResponseStreamHandler(callback); channel.pipeline().addLast(responseHandler); channel.pipeline().addLast(streamHandler); sendRequest(requestProvider, channel); return callback.awaitResult(); }
public void get(ResultCallback<Frame> resultCallback) { HttpRequestProvider requestProvider = httpGetRequestProvider(); HttpResponseHandler responseHandler = new HttpResponseHandler(requestProvider, resultCallback); FramedResponseStreamHandler streamHandler = new FramedResponseStreamHandler(resultCallback); Channel channel = getChannel(); channel.pipeline().addLast(responseHandler); channel.pipeline().addLast(streamHandler); sendRequest(requestProvider, channel); }
public void delete() { HttpRequestProvider requestProvider = httpDeleteRequestProvider(); ResponseCallback<Void> callback = new ResponseCallback<Void>(); HttpResponseHandler responseHandler = new HttpResponseHandler(requestProvider, callback); Channel channel = getChannel(); channel.pipeline().addLast(responseHandler); sendRequest(requestProvider, channel); callback.awaitResult(); }
private int poll(int off) throws IOException { synchronized (lock) { while (readableBytes() <= off) { try { if (closed) { throw new IOException("Stream closed"); } off -= releaseCurrent(); if (writeCompleted) { return off; } while (current == null) { lock.wait(); if (closed) { throw new IOException("Stream closed"); } if (writeCompleted && current == null) { return off; } } } catch (InterruptedException e) { throw new RuntimeException(e); } } return off; } }
private void sendRequest(HttpRequestProvider requestProvider, Channel channel) { ChannelFuture channelFuture = channel.writeAndFlush(requestProvider.getHttpRequest(resource)); channelFuture.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { } }); }
@Override public void close() throws IOException { synchronized (lock) { closed = true; releaseCurrent(); lock.notifyAll(); } }
@Override public int read(byte[] b, int off, int len) throws IOException { synchronized (lock) { off = poll(off); if (current == null) { return -1; } else { int availableBytes = Math.min(len, current.readableBytes() - off); current.readBytes(b, off, availableBytes); return availableBytes; } } }
@Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { stream.writeComplete(); super.channelInactive(ctx); }
@Override public void upgradeTo(ChannelHandlerContext ctx, FullHttpResponse upgradeResponse) throws Exception { httpResponseHandler.channelRead(ctx, upgradeResponse); ctx.pipeline().addLast(httpResponseHandler); latch.countDown(); }
@Override protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception { rawBuffer.writeBytes(msg, 0, msg.readableBytes()); Frame frame = null; do { frame = decode(); if (frame != null) { resultCallback.onNext(frame); } } while (frame != null); }
public <T> void get(TypeReference<T> typeReference, ResultCallback<T> resultCallback) { HttpRequestProvider requestProvider = httpGetRequestProvider(); Channel channel = getChannel(); JsonResponseCallbackHandler<T> jsonResponseHandler = new JsonResponseCallbackHandler<T>(typeReference, resultCallback); HttpResponseHandler responseHandler = new HttpResponseHandler(requestProvider, resultCallback); channel.pipeline().addLast(responseHandler); channel.pipeline().addLast(new JsonObjectDecoder()); channel.pipeline().addLast(jsonResponseHandler); sendRequest(requestProvider, channel); return; }
public InputStream get() { HttpRequestProvider requestProvider = httpGetRequestProvider(); Channel channel = getChannel(); AsyncResultCallback<InputStream> resultCallback = new AsyncResultCallback<>(); HttpResponseHandler responseHandler = new HttpResponseHandler(requestProvider, resultCallback); HttpResponseStreamHandler streamHandler = new HttpResponseStreamHandler(resultCallback); channel.pipeline().addLast(responseHandler); channel.pipeline().addLast(streamHandler); sendRequest(requestProvider, channel); return resultCallback.awaitResult(); }
public void postStream(InputStream body) { SkipResultCallback resultCallback = new SkipResultCallback(); HttpRequestProvider requestProvider = httpPostRequestProvider(null); Channel channel = getChannel(); HttpResponseHandler responseHandler = new HttpResponseHandler(requestProvider, resultCallback); channel.pipeline().addLast(new ChunkedWriteHandler()); channel.pipeline().addLast(responseHandler); postChunkedStreamRequest(requestProvider, channel, body); try { resultCallback.awaitCompletion(); } catch (InterruptedException e) { throw new RuntimeException(e); } }
private void postChunkedStreamRequest(HttpRequestProvider requestProvider, Channel channel, InputStream body) { HttpRequest request = requestProvider.getHttpRequest(resource); // don't accept FullHttpRequest here if (request instanceof FullHttpRequest) { throw new DockerClientException("fatal: request is instance of FullHttpRequest"); } request.headers().set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED); request.headers().remove(HttpHeaderNames.CONTENT_LENGTH); channel.write(request); channel.write(new ChunkedStream(new BufferedInputStream(body, 1024 * 1024), 1024 * 1024)); channel.write(LastHttpContent.EMPTY_LAST_CONTENT); channel.flush(); }
public <T> void post(TypeReference<T> typeReference, ResultCallback<T> resultCallback, InputStream body) { HttpRequestProvider requestProvider = httpPostRequestProvider(null); Channel channel = getChannel(); JsonResponseCallbackHandler<T> jsonResponseHandler = new JsonResponseCallbackHandler<T>(typeReference, resultCallback); HttpResponseHandler responseHandler = new HttpResponseHandler(requestProvider, resultCallback); channel.pipeline().addLast(new ChunkedWriteHandler()); channel.pipeline().addLast(responseHandler); channel.pipeline().addLast(new JsonObjectDecoder()); channel.pipeline().addLast(jsonResponseHandler); postChunkedStreamRequest(requestProvider, channel, body); }