public void operationComplete(ChannelFuture cf) { if (!abortOnThrowable(cf.getCause(), cf.getChannel())) { future.touch(); /** * We need to make sure we aren't in the middle of an authorization process before publishing events as we will re-publish again the same event after the authorization, * causing unpredictable behavior. */ Realm realm = future.getRequest().getRealm() != null ? future.getRequest().getRealm() : config.getRealm(); boolean startPublishing = future.isInAuth() || realm == null || realm.getUsePreemptiveAuth(); if (startPublishing && asyncHandler instanceof ProgressAsyncHandler) { if (notifyHeaders) { ProgressAsyncHandler.class.cast(asyncHandler).onHeadersWritten(); } else { ProgressAsyncHandler.class.cast(asyncHandler).onContentWritten(); } } } }
public void operationComplete(ChannelFuture cf) { if (!abortOnThrowable(cf.getCause(), cf.getChannel())) { future.touch(); /** * We need to make sure we aren't in the middle of an authorization process before publishing events as we will re-publish again the same event after the authorization, * causing unpredictable behavior. */ Realm realm = future.getRequest().getRealm() != null ? future.getRequest().getRealm() : config.getRealm(); boolean startPublishing = future.isInAuth() || realm == null || realm.getUsePreemptiveAuth(); if (startPublishing && asyncHandler instanceof ProgressAsyncHandler) { if (notifyHeaders) { ProgressAsyncHandler.class.cast(asyncHandler).onHeaderWriteCompleted(); } else { ProgressAsyncHandler.class.cast(asyncHandler).onContentWriteCompleted(); } } } }
public boolean retry(NettyResponseFuture<?> future) { if (isClosed()) return false; // FIXME this was done in AHC2, is this a bug? // channelManager.removeAll(channel); if (future.canBeReplayed()) { future.setState(NettyResponseFuture.STATE.RECONNECTED); LOGGER.debug("Trying to recover request {}\n", future.getNettyRequest().getHttpRequest()); if (future.getAsyncHandler() instanceof AsyncHandlerExtensions) AsyncHandlerExtensions.class.cast(future.getAsyncHandler()).onRetry(); try { sendNextRequest(future.getRequest(), future); return true; } catch (IOException iox) { future.setState(NettyResponseFuture.STATE.CLOSED); future.abort(iox); LOGGER.error("Remotely closed, unable to recover", iox); return false; } } else { LOGGER.debug("Unable to recover future {}\n", future); return false; } }
private void onFutureSuccess(final Channel channel) throws Exception { Request request = future.getRequest(); Uri uri = request.getUri(); // in case of proxy tunneling, we'll add the SslHandler later, after the CONNECT request if (future.getProxyServer() == null && uri.isSecured()) { SslHandler sslHandler = channelManager.addSslHandler(channel.pipeline(), uri, request.getVirtualHost()); sslHandler.handshakeFuture().addListener(new GenericFutureListener<Future<Channel>>() { @Override public void operationComplete(Future<Channel> handshakeFuture) throws Exception { if (handshakeFuture.isSuccess()) { final AsyncHandler<T> asyncHandler = future.getAsyncHandler(); if (asyncHandler instanceof AsyncHandlerExtensions) AsyncHandlerExtensions.class.cast(asyncHandler).onSslHandshakeCompleted(); writeRequest(channel); } else { onFutureFailure(channel, handshakeFuture.cause()); } } }); } else { writeRequest(channel); } }
public boolean retry(NettyResponseFuture<?> future) { if (isClosed()) return false; if (future.canBeReplayed()) { future.setState(NettyResponseFuture.STATE.RECONNECTED); future.getAndSetStatusReceived(false); LOGGER.debug("Trying to recover request {}\n", future.getNettyRequest().getHttpRequest()); if (future.getAsyncHandler() instanceof AsyncHandlerExtensions) { AsyncHandlerExtensions.class.cast(future.getAsyncHandler()).onRetry(); } try { sendNextRequest(future.getRequest(), future); return true; } catch (Exception e) { abort(future.channel(), future, e); return false; } } else { LOGGER.debug("Unable to recover future {}\n", future); return false; } }
public boolean retry(NettyResponseFuture<?> future) { if (isClosed()) return false; if (future.canBeReplayed()) { future.setState(NettyResponseFuture.STATE.RECONNECTED); future.getAndSetStatusReceived(false); LOGGER.debug("Trying to recover request {}\n", future.getNettyRequest().getHttpRequest()); if (future.getAsyncHandler() instanceof AsyncHandlerExtensions) { AsyncHandlerExtensions.class.cast(future.getAsyncHandler()).onRetry(); } try { sendNextRequest(future.getRequest(), future); return true; } catch (Exception e) { abort(future.channel(), future, e); return false; } } else { LOGGER.debug("Unable to recover future {}\n", future); return false; } }
private boolean exitAfterHandlingConnect(// final Channel channel,// final NettyResponseFuture<?> future,// final Request request,// ProxyServer proxyServer,// int statusCode,// HttpRequest httpRequest) throws IOException { if (statusCode == OK.code() && httpRequest.getMethod() == HttpMethod.CONNECT) { if (future.isKeepAlive()) future.attachChannel(channel, true); Uri requestUri = request.getUri(); logger.debug("Connecting to proxy {} for scheme {}", proxyServer, requestUri.getScheme()); try { channelManager.upgradeProtocol(channel.pipeline(), requestUri); future.setReuseChannel(true); future.setConnectAllowed(false); requestSender.drainChannelAndExecuteNextRequest(channel, future, new RequestBuilder(future.getRequest()).build()); } catch (GeneralSecurityException ex) { requestSender.abort(channel, future, ex); } return true; } return false; }
private boolean exitAfterHandlingConnect(// final Channel channel,// final NettyResponseFuture<?> future,// final Request request,// ProxyServer proxyServer,// int statusCode,// HttpRequest httpRequest) throws IOException { if (statusCode == OK.code() && httpRequest.getMethod() == HttpMethod.CONNECT) { if (future.isKeepAlive()) future.attachChannel(channel, true); try { Uri requestUri = request.getUri(); String scheme = requestUri.getScheme(); String host = requestUri.getHost(); int port = getDefaultPort(requestUri); logger.debug("Connecting to proxy {} for scheme {}", proxyServer, scheme); channelManager.upgradeProtocol(channel.pipeline(), scheme, host, port); } catch (Throwable ex) { requestSender.abort(channel, future, ex); } future.setReuseChannel(true); future.setConnectAllowed(false); requestSender.drainChannelAndExecuteNextRequest(channel, future, new RequestBuilder(future.getRequest()).build()); return true; } return false; }
private void scheduleTimeouts(NettyResponseFuture<?> nettyResponseFuture) { nettyResponseFuture.touch(); int requestTimeoutInMs = requestTimeout(config, nettyResponseFuture.getRequest()); TimeoutsHolder timeoutsHolder = new TimeoutsHolder(); if (requestTimeoutInMs != -1) { Timeout requestTimeout = newTimeout(new RequestTimeoutTimerTask(nettyResponseFuture, this, timeoutsHolder, requestTimeoutInMs), requestTimeoutInMs); timeoutsHolder.requestTimeout = requestTimeout; } int readTimeoutValue = config.getReadTimeout(); if (readTimeoutValue != -1 && readTimeoutValue < requestTimeoutInMs) { // no need to schedule a readTimeout if the requestTimeout happens first Timeout readTimeout = newTimeout(new ReadTimeoutTimerTask(nettyResponseFuture, this, timeoutsHolder, requestTimeoutInMs, readTimeoutValue), readTimeoutValue); timeoutsHolder.readTimeout = readTimeout; } nettyResponseFuture.setTimeoutsHolder(timeoutsHolder); }
private void scheduleTimeouts(NettyResponseFuture<?> nettyResponseFuture) { nettyResponseFuture.touch(); int requestTimeoutInMs = requestTimeout(config, nettyResponseFuture.getRequest()); TimeoutsHolder timeoutsHolder = new TimeoutsHolder(); if (requestTimeoutInMs != -1) { Timeout requestTimeout = newTimeout(new RequestTimeoutTimerTask(nettyResponseFuture, this, timeoutsHolder, requestTimeoutInMs), requestTimeoutInMs); timeoutsHolder.requestTimeout = requestTimeout; } int readTimeoutValue = config.getReadTimeout(); if (readTimeoutValue != -1 && readTimeoutValue < requestTimeoutInMs) { // no need to schedule a readTimeout if the requestTimeout happens first Timeout readTimeout = newTimeout(new ReadTimeoutTimerTask(nettyResponseFuture, this, timeoutsHolder, requestTimeoutInMs, readTimeoutValue), readTimeoutValue); timeoutsHolder.readTimeout = readTimeout; } nettyResponseFuture.setTimeoutsHolder(timeoutsHolder); }
private void scheduleTimeouts(NettyResponseFuture<?> nettyResponseFuture) { nettyResponseFuture.touch(); int requestTimeoutInMs = requestTimeout(config, nettyResponseFuture.getRequest()); TimeoutsHolder timeoutsHolder = new TimeoutsHolder(); if (requestTimeoutInMs != -1) { Timeout requestTimeout = newTimeout(new RequestTimeoutTimerTask(nettyResponseFuture, this, timeoutsHolder, requestTimeoutInMs), requestTimeoutInMs); timeoutsHolder.requestTimeout = requestTimeout; } int readTimeoutValue = config.getReadTimeout(); if (readTimeoutValue != -1 && readTimeoutValue < requestTimeoutInMs) { // no need for a readTimeout that's less than the requestTimeoutInMs Timeout readTimeout = newTimeout(new ReadTimeoutTimerTask(nettyResponseFuture, this, timeoutsHolder, requestTimeoutInMs, readTimeoutValue), readTimeoutValue); timeoutsHolder.readTimeout = readTimeout; } nettyResponseFuture.setTimeoutsHolder(timeoutsHolder); }
public boolean applyIoExceptionFiltersAndReplayRequest(NettyResponseFuture<?> future, IOException e, Channel channel) { boolean replayed = false; @SuppressWarnings({ "unchecked", "rawtypes" }) FilterContext<?> fc = new FilterContext.FilterContextBuilder().asyncHandler(future.getAsyncHandler()).request(future.getRequest()).ioException(e).build(); for (IOExceptionFilter asyncFilter : config.getIOExceptionFilters()) { try { fc = asyncFilter.filter(fc); if (fc == null) { throw new NullPointerException("FilterContext is null"); } } catch (FilterException efe) { abort(channel, future, efe); } } if (fc.replayRequest() && future.canBeReplayed()) { replayRequest(future, fc, channel); replayed = true; } return replayed; }
public boolean applyIoExceptionFiltersAndReplayRequest(NettyResponseFuture<?> future, IOException e, Channel channel) { boolean replayed = false; @SuppressWarnings({ "unchecked", "rawtypes" }) FilterContext<?> fc = new FilterContext.FilterContextBuilder().asyncHandler(future.getAsyncHandler()).request(future.getRequest()).ioException(e).build(); for (IOExceptionFilter asyncFilter : config.getIOExceptionFilters()) { try { fc = asyncFilter.filter(fc); if (fc == null) { throw new NullPointerException("FilterContext is null"); } } catch (FilterException efe) { abort(channel, future, efe); } } if (fc.replayRequest() && future.canBeReplayed()) { replayRequest(future, fc, channel); replayed = true; } return replayed; }
public boolean applyIoExceptionFiltersAndReplayRequest(NettyResponseFuture<?> future, IOException e, Channel channel) throws IOException { boolean replayed = false; @SuppressWarnings({ "unchecked", "rawtypes" }) FilterContext<?> fc = new FilterContext.FilterContextBuilder().asyncHandler(future.getAsyncHandler()).request(future.getRequest()).ioException(e).build(); for (IOExceptionFilter asyncFilter : config.getIOExceptionFilters()) { try { fc = asyncFilter.filter(fc); if (fc == null) { throw new NullPointerException("FilterContext is null"); } } catch (FilterException efe) { abort(channel, future, efe); } } if (fc.replayRequest() && future.canBeReplayed()) { replayRequest(future, fc, channel); replayed = true; } return replayed; }
private void scheduleTimeouts(NettyResponseFuture<?> nettyResponseFuture) { nettyResponseFuture.touch(); int requestTimeoutInMs = requestTimeout(config, nettyResponseFuture.getRequest()); TimeoutsHolder timeoutsHolder = new TimeoutsHolder(); if (requestTimeoutInMs != -1) { Timeout requestTimeout = newTimeout(new RequestTimeoutTimerTask(nettyResponseFuture, this, timeoutsHolder, requestTimeoutInMs), requestTimeoutInMs); timeoutsHolder.requestTimeout = requestTimeout; } int readTimeoutValue = config.getReadTimeout(); if (readTimeoutValue != -1 && readTimeoutValue < requestTimeoutInMs) { // no need for a readTimeout that's less than the requestTimeout Timeout readTimeout = newTimeout(new ReadTimeoutTimerTask(nettyResponseFuture, this, timeoutsHolder, requestTimeoutInMs, readTimeoutValue), readTimeoutValue); timeoutsHolder.readTimeout = readTimeout; } nettyResponseFuture.setTimeoutsHolder(timeoutsHolder); }
public boolean applyIoExceptionFiltersAndReplayRequest(NettyResponseFuture<?> future, IOException e, Channel channel) throws IOException { boolean replayed = false; @SuppressWarnings({ "unchecked", "rawtypes" }) FilterContext<?> fc = new FilterContext.FilterContextBuilder().asyncHandler(future.getAsyncHandler()).request(future.getRequest()) .ioException(e).build(); for (IOExceptionFilter asyncFilter : config.getIOExceptionFilters()) { try { fc = asyncFilter.filter(fc); if (fc == null) { throw new NullPointerException("FilterContext is null"); } } catch (FilterException efe) { abort(channel, future, efe); } } if (fc.replayRequest() && future.canBeReplayed()) { replayRequest(future, fc, channel); replayed = true; } return replayed; }
private boolean exitAfterHandlingConnect(// final Channel channel,// final NettyResponseFuture<?> future,// final Request request,// ProxyServer proxyServer,// int statusCode,// HttpRequest httpRequest) { if (statusCode == OK.getCode() && httpRequest.getMethod() == HttpMethod.CONNECT) { if (future.isKeepAlive()) future.attachChannel(channel, true); Uri requestUri = request.getUri(); logger.debug("Connecting to proxy {} for scheme {}", proxyServer, requestUri.getScheme()); try { channelManager.upgradeProtocol(channel.getPipeline(), requestUri); future.setReuseChannel(true); future.setConnectAllowed(false); requestSender.sendNextRequest(new RequestBuilder(future.getRequest()).build(), future); } catch (GeneralSecurityException ex) { requestSender.abort(channel, future, ex); } return true; } return false; }
private void onFutureSuccess(final Channel channel) throws Exception { Request request = future.getRequest(); Uri uri = request.getUri(); // in case of proxy tunneling, we'll add the SslHandler later, after the CONNECT request if (future.getProxyServer() == null && uri.isSecured()) { SslHandler sslHandler = channelManager.addSslHandler(channel.getPipeline(), uri, request.getVirtualHost()); sslHandler.handshake().addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture handshakeFuture) throws Exception { if (handshakeFuture.isSuccess()) { final AsyncHandler<T> asyncHandler = future.getAsyncHandler(); if (asyncHandler instanceof AsyncHandlerExtensions) AsyncHandlerExtensions.class.cast(asyncHandler).onSslHandshakeCompleted(); writeRequest(channel); } else { onFutureFailure(channel, handshakeFuture.getCause()); } } }); } else { writeRequest(channel); } }
private boolean handleHttpResponse(final HttpResponse response, final Channel channel, final NettyResponseFuture<?> future, AsyncHandler<?> handler) throws Exception { HttpRequest httpRequest = future.getNettyRequest().getHttpRequest(); ProxyServer proxyServer = future.getProxyServer(); logger.debug("\n\nRequest {}\n\nResponse {}\n", httpRequest, response); // store the original headers so we can re-send all them to // the handler in case of trailing headers future.setHttpHeaders(response.headers()); future.setKeepAlive(connectionStrategy.keepAlive(httpRequest, response)); NettyResponseStatus status = new NettyResponseStatus(future.getUri(), config, response, channel); int statusCode = response.getStatus().code(); Request request = future.getRequest(); Realm realm = request.getRealm() != null ? request.getRealm() : config.getRealm(); NettyResponseHeaders responseHeaders = new NettyResponseHeaders(response.headers()); return exitAfterProcessingFilters(channel, future, handler, status, responseHeaders) || exitAfterHandling401(channel, future, response, request, statusCode, realm, proxyServer) || // exitAfterHandling407(channel, future, response, request, statusCode, realm, proxyServer) || // exitAfterHandling100(channel, future, statusCode) || // exitAfterHandlingRedirect(channel, future, response, request, statusCode, realm) || // exitAfterHandlingConnect(channel, future, request, proxyServer, statusCode, httpRequest) || // exitAfterHandlingStatus(channel, future, response, handler, status) || // exitAfterHandlingHeaders(channel, future, response, handler, responseHeaders); }
private boolean handleHttpResponse(final HttpResponse response, final Channel channel, final NettyResponseFuture<?> future, AsyncHandler<?> handler) throws Exception { HttpRequest httpRequest = future.getNettyRequest().getHttpRequest(); ProxyServer proxyServer = future.getProxyServer(); logger.debug("\n\nRequest {}\n\nResponse {}\n", httpRequest, response); // store the original headers so we can re-send all them to // the handler in case of trailing headers future.setHttpHeaders(response.headers()); future.setKeepAlive(connectionStrategy.keepAlive(httpRequest, response)); NettyResponseStatus status = new NettyResponseStatus(future.getUri(), config, response, channel); int statusCode = response.getStatus().code(); Request request = future.getRequest(); Realm realm = request.getRealm() != null ? request.getRealm() : config.getRealm(); NettyResponseHeaders responseHeaders = new NettyResponseHeaders(response.headers()); return exitAfterProcessingFilters(channel, future, handler, status, responseHeaders) || exitAfterHandling401(channel, future, response, request, statusCode, realm, proxyServer) || // exitAfterHandling407(channel, future, response, request, statusCode, realm, proxyServer) || // exitAfterHandling100(channel, future, statusCode) || // exitAfterHandlingRedirect(channel, future, response, request, statusCode, realm) || // exitAfterHandlingConnect(channel, future, request, proxyServer, statusCode, httpRequest) || // exitAfterHandlingStatus(channel, future, response, handler, status) || // exitAfterHandlingHeaders(channel, future, response, handler, responseHeaders) || exitAfterHandlingReactiveStreams(channel, future, response, handler); }