@Override public void handlerAdded(ChannelHandlerContext ctx) throws Exception { if (!acceptForeignIp) { if (!((InetSocketAddress) ctx.channel().remoteAddress()).getAddress().isLoopbackAddress()) { ByteBuf cb = Unpooled.wrappedBuffer((QosConstants.BR_STR + "Foreign Ip Not Permitted." + QosConstants.BR_STR).getBytes()); ctx.writeAndFlush(cb).addListener(ChannelFutureListener.CLOSE); } } } }
private void sendServiceUnavailable(Channel ch) { ch.writeAndFlush( Unpooled.copiedBuffer("HTTP/1.1 503 Service Unavailable\r\n" + "Content-Length:0\r\n" + "\r\n", StandardCharsets.ISO_8859_1)) .addListener(ChannelFutureListener.CLOSE); }
@Override protected boolean accept(ChannelHandlerContext ctx, InetSocketAddress remoteAddress) throws Exception { final InetAddress remoteIp = remoteAddress.getAddress(); if (!connected.add(remoteIp)) { return false; } else { ctx.channel().closeFuture().addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { connected.remove(remoteIp); } }); return true; } } }
private static void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) { var response = new DefaultFullHttpResponse(HTTP_1_1, status, Unpooled.copiedBuffer("Failure: " + status + "\r\n", CharsetUtil.UTF_8)); response.headers().set(HttpConst.CONTENT_TYPE, Const.CONTENT_TYPE_TEXT); // Close the connection as soon as the error message is sent. ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); }
public final void sendHttpResponse(HttpRequest req, ChannelHandlerContext ctx, HttpResponseStatus status) { FullHttpResponse resp = new DefaultFullHttpResponse(HTTP_1_1, status); resp.headers().add("Content-Length", "0"); final boolean closeConn = ((status != OK) || (! HttpUtil.isKeepAlive(req))); if (closeConn) { resp.headers().add(HttpHeaderNames.CONNECTION, "Close"); } final ChannelFuture cf = ctx.channel().writeAndFlush(resp); if (closeConn) { cf.addListener(ChannelFutureListener.CLOSE); } }
private void sendError(ChannelHandlerContext ctx) { HttpResponse res = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR); ctx.channel().writeAndFlush(res).addListener(ChannelFutureListener.CLOSE); }
private void shutdownOutputDone(final ChannelFuture shutdownOutputFuture, final ChannelPromise promise) { ChannelFuture shutdownInputFuture = shutdownInput(); if (shutdownInputFuture.isDone()) { shutdownDone(shutdownOutputFuture, shutdownInputFuture, promise); } else { shutdownInputFuture.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture shutdownInputFuture) throws Exception { shutdownDone(shutdownOutputFuture, shutdownInputFuture, promise); } }); } }
public final void sendErrorAndClose(ChannelHandlerContext ctx, int statusCode, String reasonText) { final Object mesg = serverClosingConnectionMessage(statusCode, reasonText); ctx.writeAndFlush(mesg).addListener(ChannelFutureListener.CLOSE); }
@Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { int status = 500; final String errorMsg = "ClientResponseWriter caught exception in client connection pipeline: " + ChannelUtils.channelInfoForLogging(ctx.channel()); if (cause instanceof ZuulException) { final ZuulException ze = (ZuulException) cause; status = ze.getStatusCode(); LOG.error(errorMsg, cause); } else if (cause instanceof ReadTimeoutException) { LOG.error(errorMsg + ", Read timeout fired"); status = 504; } else { LOG.error(errorMsg, cause); } if (isHandlingRequest && !startedSendingResponseToClient && ctx.channel().isActive()) { final HttpResponse httpResponse = new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.valueOf(status)); ctx.writeAndFlush(httpResponse).addListener(ChannelFutureListener.CLOSE); startedSendingResponseToClient = true; } else { ctx.close(); } }
private void writeQuery(final DnsQuery query, final boolean flush, final ChannelPromise writePromise) { final ChannelFuture writeFuture = flush ? parent.ch.writeAndFlush(query, writePromise) : parent.ch.write(query, writePromise); if (writeFuture.isDone()) { onQueryWriteCompletion(writeFuture); } else { writeFuture.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) { onQueryWriteCompletion(writeFuture); } }); } }
@Override public void handlerAdded(ChannelHandlerContext ctx) throws Exception { if (!acceptForeignIp) { if (!((InetSocketAddress) ctx.channel().remoteAddress()).getAddress().isLoopbackAddress()) { ByteBuf cb = Unpooled.wrappedBuffer((QosConstants.BR_STR + "Foreign Ip Not Permitted." + QosConstants.BR_STR).getBytes()); ctx.writeAndFlush(cb).addListener(ChannelFutureListener.CLOSE); } } } }
private static void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) { var response = new DefaultFullHttpResponse(HTTP_1_1, status, Unpooled.copiedBuffer("Failure: " + status + "\r\n", CharsetUtil.UTF_8)); response.headers().set(HttpConst.CONTENT_TYPE, Const.CONTENT_TYPE_TEXT); // Close the connection as soon as the error message is sent. ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); }
private void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest request, HttpResponseStatus status, PushUserAuth userAuth) { final FullHttpResponse resp = new DefaultFullHttpResponse(HTTP_1_1, status); resp.headers().add("Content-Length", "0"); final ChannelFuture cf = ctx.channel().writeAndFlush(resp); if (!HttpUtil.isKeepAlive(request)) { cf.addListener(ChannelFutureListener.CLOSE); } logPushEvent(request, status, userAuth); }
private void handleDisconnectMessage(ChannelHandlerContext ctx, ProxyMessage proxyMessage) { Channel realServerChannel = ctx.channel().attr(Constants.NEXT_CHANNEL).get(); logger.debug("handleDisconnectMessage, {}", realServerChannel); if (realServerChannel != null) { ctx.channel().attr(Constants.NEXT_CHANNEL).remove(); ClientChannelMannager.returnProxyChanel(ctx.channel()); realServerChannel.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE); } }
/** * Closes the specified channel after all queued write requests are flushed. */ public static void closeOnFlush(Channel ch) { if (ch != null && ch.isActive()) { ch.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE); } }
private void shutdownOutputDone(final ChannelFuture shutdownOutputFuture, final ChannelPromise promise) { ChannelFuture shutdownInputFuture = shutdownInput(); if (shutdownInputFuture.isDone()) { shutdownDone(shutdownOutputFuture, shutdownInputFuture, promise); } else { shutdownInputFuture.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture shutdownInputFuture) throws Exception { shutdownDone(shutdownOutputFuture, shutdownInputFuture, promise); } }); } }
public final void sendErrorAndClose(ChannelHandlerContext ctx, int statusCode, String reasonText) { ctx.writeAndFlush(serverClosingConnectionMessage(statusCode, reasonText)) .addListener(ChannelFutureListener.CLOSE); }
@Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { int status = 500; final String errorMsg = "ClientResponseWriter caught exception in client connection pipeline: " + ChannelUtils.channelInfoForLogging(ctx.channel()); if (cause instanceof ZuulException) { final ZuulException ze = (ZuulException) cause; status = ze.getStatusCode(); LOG.error(errorMsg, cause); } else if (cause instanceof ReadTimeoutException) { LOG.error(errorMsg + ", Read timeout fired"); status = 504; } else { LOG.error(errorMsg, cause); } if (isHandlingRequest && !startedSendingResponseToClient && ctx.channel().isActive()) { final HttpResponse httpResponse = new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.valueOf(status)); ctx.writeAndFlush(httpResponse).addListener(ChannelFutureListener.CLOSE); startedSendingResponseToClient = true; } else { ctx.close(); } }
@Override public void channelActive(ChannelHandlerContext ctx) throws Exception { connections.incrementAndGet(); totalConnections.incrementAndGet(); ctx.channel().closeFuture().addListener(f -> connections.decrementAndGet()); super.channelActive(ctx); }
public CompletableFuture<MessageIdData> sendGetLastMessageId(ByteBuf request, long requestId) { CompletableFuture<MessageIdData> future = new CompletableFuture<>(); pendingGetLastMessageIdRequests.put(requestId, future); ctx.writeAndFlush(request).addListener(writeFuture -> { if (!writeFuture.isSuccess()) { log.warn("{} Failed to send GetLastMessageId request to broker: {}", ctx.channel(), writeFuture.cause().getMessage()); pendingGetLastMessageIdRequests.remove(requestId); future.completeExceptionally(writeFuture.cause()); } }); return future; }