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); }
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); } }
@Override public ChannelFuture sendPushMessage(ChannelHandlerContext ctx, ByteBuf mesg) { final TextWebSocketFrame wsf = new TextWebSocketFrame(mesg); return ctx.channel().writeAndFlush(wsf); }
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); }
@Override public void handle(Channel channel, Command command, String... args) { channel.writeAndFlush("Bye bye!" + JConstants.NEWLINE) .addListener(ChannelFutureListener.CLOSE); } }
@Override public ChannelFuture sendPing(ChannelHandlerContext ctx) { return ctx.channel().writeAndFlush(new PingWebSocketFrame()); }
private void sendError(ChannelHandlerContext ctx) { HttpResponse res = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR); ctx.channel().writeAndFlush(res).addListener(ChannelFutureListener.CLOSE); }
@Override public void handle(Channel channel, Command command, String... args) { channel.writeAndFlush("Bye bye!" + JConstants.NEWLINE) .addListener(ChannelFutureListener.CLOSE); } }
private ChannelFuture sendResponse(ChannelHandlerContext ctx, Response response) { byte[] msg = CodecUtil.encodeObjectToBytes(channel, codec, response); if (ctx.channel().isActive()) { return ctx.channel().writeAndFlush(msg); } return null; }
@Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { if (cause instanceof WebSocketHandshakeException) { FullHttpResponse response = new DefaultFullHttpResponse( HTTP_1_1, HttpResponseStatus.BAD_REQUEST, Unpooled.wrappedBuffer(cause.getMessage().getBytes())); ctx.channel().writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); } else { ctx.fireExceptionCaught(cause); ctx.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); } }
@Override public ChannelFuture sendPing(ChannelHandlerContext ctx) { final ByteBuf newBuff = ctx.alloc().buffer(); newBuff.ensureWritable(SSE_PING.length()); newBuff.writeCharSequence(SSE_PING, Charsets.UTF_8); return ctx.channel().writeAndFlush(newBuff); }
private void failure(String message, Throwable cause, ChannelHandlerContext ctx, Object response) { if (shouldNotIgnoreException(cause)) { mockServerLogger.error(message, cause); } Channel channel = ctx.channel(); channel.writeAndFlush(response); if (channel.isActive()) { channel.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE); } }
@Override public void channelRead0(final ChannelHandlerContext ctx, final FullHttpResponse response) { upstreamChannel.writeAndFlush(response).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) { if (future.isSuccess()) { ctx.channel().read(); } else { if (isNotSocketClosedException(future.cause())) { mockServerLogger.error("Exception while returning writing " + response, future.cause()); } future.channel().close(); } } }); }
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { if (msg instanceof FullHttpRequest) { ((FullHttpRequest) msg).release(); FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.FORBIDDEN); ctx.channel().writeAndFlush(response); } else { ctx.fireChannelRead(msg); } } };
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); } }
public void close() { channel.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE); }
private void sendMessage(ChannelHandlerContext ctx, ImmutableMap<String, Object> message) throws JsonProcessingException { ctx.channel().writeAndFlush(new TextWebSocketFrame( objectMapper.writeValueAsString(message) )); }
private static void sendHttpResponse(ChannelHandlerContext ctx, HttpRequest req, HttpResponse res) { ChannelFuture f = ctx.channel().writeAndFlush(res); if (!isKeepAlive(req) || res.status().code() != 200) { f.addListener(ChannelFutureListener.CLOSE); } }
/** * Performs the closing handshake * * @param channel * Channel * @param frame * Closing Frame that was received * @param promise * the {@link ChannelPromise} to be notified when the closing handshake is done */ public ChannelFuture close(Channel channel, CloseWebSocketFrame frame, ChannelPromise promise) { if (channel == null) { throw new NullPointerException("channel"); } return channel.writeAndFlush(frame, promise).addListener(ChannelFutureListener.CLOSE); }