List of usage examples for io.netty.channel ChannelHandlerContext channel
Channel channel();
From source file:com.barchart.http.server.HttpRequestChannelHandler.java
License:BSD License
@Override public void channelRead0(final ChannelHandlerContext ctx, final FullHttpRequest msg) throws Exception { final RequestHandlerMapping mapping = config.getRequestMapping(msg.getUri()); String relativePath = msg.getUri(); if (mapping != null) { relativePath = relativePath.substring(mapping.path().length()); }//from w w w . j a va2 s .com // Create request/response final PooledServerRequest request = messagePool.getRequest(); // Handle 503 - sanity check, should be caught in acceptor if (request == null) { sendServerError(ctx, new ServerTooBusyException("Maximum concurrent connections reached")); return; } request.init(ctx.channel(), msg, relativePath); final RequestHandler handler = mapping == null ? null : mapping.handler(request); final PooledServerResponse response = messagePool.getResponse(); response.init(ctx, this, handler, request, config.logger()); if (mapping == null) { // No handler found, 404 response.setStatus(HttpResponseStatus.NOT_FOUND); } // Store in ChannelHandlerContext for future reference ctx.attr(ATTR_RESPONSE).set(response); try { // MJS: Dispatch an error if not found or authorized if (response.getStatus() == HttpResponseStatus.UNAUTHORIZED || response.getStatus() == HttpResponseStatus.NOT_FOUND) { config.errorHandler().onError(request, response, null); } else { handler.onRequest(request, response); } } catch (final Throwable t) { // Catch server errors response.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR); try { config.errorHandler().onError(request, response, t); } catch (final Throwable t2) { response.write(t.getClass() + " was thrown while processing this request. Additionally, " + t2.getClass() + " was thrown while handling this exception."); } config.logger().error(request, response, t); // Force request to end on exception, async handlers cannot allow // unchecked exceptions and still expect to return data if (!response.isFinished()) { response.finish(); } } finally { // If handler did not request async response, finish request if (!response.isFinished() && !response.isSuspended()) { response.finish(); } } }
From source file:com.barchart.http.server.HttpRequestChannelHandler.java
License:BSD License
private void sendServerError(final ChannelHandlerContext ctx, final ServerException cause) throws Exception { if (ctx.channel().isActive()) { final ByteBuf content = Unpooled.buffer(); content.writeBytes(/* w w w . j a v a 2 s. c o m*/ (cause.getStatus().code() + " " + cause.getStatus().reasonPhrase() + " - " + cause.getMessage()) .getBytes()); final FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, cause.getStatus()); response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, content.readableBytes()); response.content().writeBytes(content); ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); } }
From source file:com.barchart.netty.common.pipeline.PingHandler.java
License:BSD License
private void startPing(final ChannelHandlerContext ctx) { synchronized (this) { if (pingFuture != null && !pingFuture.isDone()) { pingFuture.cancel(false);/*w w w . ja va2s . c o m*/ } if (interval > 0) { pingFuture = ctx.channel().eventLoop().schedule(new Runnable() { @Override public void run() { sendPing(ctx); } }, interval, unit); } } }
From source file:com.barchart.netty.common.pipeline.PingHandler.java
License:BSD License
public void sendPing(final ChannelHandlerContext ctx) { try {/*from w w w .j ava 2 s . co m*/ if (pingFuture != null && !pingFuture.isDone()) { pingFuture.cancel(false); } ctx.write(new Ping() { @Override public long timestamp() { return System.currentTimeMillis(); } }); ctx.flush(); } finally { if (interval > 0) { pingFuture = ctx.channel().eventLoop().schedule(new Runnable() { @Override public void run() { sendPing(ctx); } }, interval, unit); } } }
From source file:com.basho.riak.client.core.netty.HealthCheckDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext chc, ByteBuf in, List<Object> list) throws Exception { // Make sure we have 4 bytes if (in.readableBytes() >= 4) { in.markReaderIndex();/*from w w w . j a va2s . c om*/ int length = in.readInt(); // See if we have the full frame. if (in.readableBytes() < length) { in.resetReaderIndex(); } else { byte code = in.readByte(); byte[] protobuf = new byte[length - 1]; in.readBytes(protobuf); chc.channel().pipeline().remove(this); if (code == RiakMessageCodes.MSG_ErrorResp) { logger.debug("Received MSG_ErrorResp reply to healthcheck"); future.setException((riakErrorToException(protobuf))); } else { logger.debug("Healthcheck op successful; returned code {}", code); future.setMessage(new RiakMessage(code, protobuf)); } } } }
From source file:com.basho.riak.client.core.netty.HealthCheckDecoder.java
License:Apache License
private void init(ChannelHandlerContext ctx) throws InterruptedException { ChannelFuture writeAndFlush = ctx.channel().writeAndFlush(buildOperation().channelMessage()); }
From source file:com.basho.riak.client.core.netty.HealthCheckDecoder.java
License:Apache License
@Override public void handlerAdded(ChannelHandlerContext ctx) throws Exception { logger.debug("HealthCheckDecoder Handler Added"); if (ctx.channel().isActive()) { init(ctx);/*from ww w.j av a2s. c o m*/ } else { future.setException(new IOException("HealthCheckDecoder added to inactive channel")); } }
From source file:com.basho.riak.client.core.netty.RiakHttpMessageHandler.java
License:Apache License
@Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { if (cause instanceof ReadTimeoutException) { timedOut = true;/* ww w . j av a 2s.co m*/ listener.onException(ctx.channel(), cause); } else { if (!timedOut) { listener.onException(ctx.channel(), cause); } ctx.channel().pipeline().remove(this); } }
From source file:com.basho.riak.client.core.netty.RiakPbMessageHandler.java
License:Apache License
@Override protected void channelRead0(ChannelHandlerContext ctx, RiakPbMessage msg) throws Exception { if (msg.getCode() == RiakMessageCodes.MSG_ErrorResp) { RiakPB.RpbErrorResp error = RiakPB.RpbErrorResp.newBuilder().setErrcode(msg.getCode()) .setErrmsg(ByteString.copyFrom(msg.getData())).build(); listener.onException(ctx.channel(), new RiakResponseException(RiakMessageCodes.MSG_ErrorResp, error.getErrmsg().toStringUtf8())); } else {//from w w w. j a v a2 s. c om listener.onSuccess(ctx.channel(), msg); } ctx.channel().pipeline().remove(this); }
From source file:com.basho.riak.client.core.netty.RiakResponseHandler.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext chc, Object message) throws Exception { RiakMessage riakMessage = (RiakMessage) message; if (riakMessage.getCode() == RiakMessageCodes.MSG_ErrorResp) { RiakPB.RpbErrorResp error = RiakPB.RpbErrorResp.parseFrom(riakMessage.getData()); listener.onRiakErrorResponse(chc.channel(), new RiakResponseException(error.getErrcode(), error.getErrmsg().toStringUtf8())); } else {/*from w ww . j av a 2s .com*/ listener.onSuccess(chc.channel(), riakMessage); } }