List of usage examples for io.netty.channel ChannelHandlerContext fireChannelRead
@Override ChannelHandlerContext fireChannelRead(Object msg);
From source file:com.soho.framework.server.netty.http.AsyncHttpServletHandler.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object e) throws Exception { if (e instanceof ServletResponse) { logger.info("Handler async task..."); HttpServletResponse response = (HttpServletResponse) e; Runnable task = ThreadLocalAsyncExecutor.pollTask(response); task.run();// w ww .j a v a 2 s. c om // write response... ChannelFuture future = ctx.channel().writeAndFlush(response); String keepAlive = response.getHeader(CONNECTION); if (null != keepAlive && HttpHeaders.Values.KEEP_ALIVE.equalsIgnoreCase(keepAlive)) { future.addListener(ChannelFutureListener.CLOSE); } } else { ctx.fireChannelRead(e); } }
From source file:com.soho.framework.server.netty.http.HttpServletHandler.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object e) throws Exception { if (e instanceof HttpRequest) { HttpRequest request = (HttpRequest) e; String uri = request.uri(); log.debug("The current request uri:" + uri); if (uri.startsWith(uriPrefix)) { /* if (HttpHeaders.is100ContinueExpected(request)) { ctx.channel().write(new DefaultHttpResponse(HTTP_1_1, CONTINUE)); }*//*from w w w . ja va2 s . com*/ FilterChainImpl chain = ServletWebApp.get().initializeChain(uri); if (chain.isValid()) { handleHttpServletRequest(ctx, request, chain); } else if (ServletWebApp.get().getStaticResourcesFolder() != null) { handleStaticResourceRequest(ctx, request); } else { throw new NettyServletRuntimeException("No handler found for uri: " + request.uri()); } } else { ctx.fireChannelRead(e); } } else { ctx.fireChannelRead(e); } }
From source file:com.soho.framework.server.netty.http.HttpServletHandler.java
License:Apache License
protected void handleHttpServletRequest(ChannelHandlerContext ctx, HttpRequest request, FilterChainImpl chain) throws Exception { // ?/*from w w w. jav a 2 s .co m*/ interceptOnRequestReceived(ctx, request); // Netty HTTP?Servlet HttpServletRequestImpl req = buildHttpServletRequest(request, chain); // Netty HTTP??Servlet? DefaultFullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK); HttpServletResponseImpl resp = buildHttpServletResponse(response); // chain.doFilter(req, resp); // ? interceptOnRequestSuccessed(ctx, request, response); resp.getWriter().flush(); boolean keepAlive = HttpHeaders.isKeepAlive(request); if (keepAlive) { // Add 'Content-Length' header only for a keep-alive connection. response.headers().set(CONTENT_LENGTH, response.content().readableBytes()); // Add keep alive header as per: // - // http://www.w3.org/Protocols/HTTP/1.1/draft-ietf-http-v11-spec-01.html#Connection response.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE); } // ? if (req.isAsyncSupported() && req.isAsyncStarted()) { ctx.fireChannelRead(resp); } else { // write response... ChannelFuture future = ctx.channel().writeAndFlush(response); if (!keepAlive) { future.addListener(ChannelFutureListener.CLOSE); } } }
From source file:com.spotify.ffwd.riemann.RiemannResponder.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ctx.channel().writeAndFlush(ack(true)); ctx.fireChannelRead(msg); }
From source file:com.spotify.heroic.rpc.nativerpc.NativeRpcClientSession.java
License:Apache License
@Override protected void initChannel(final Channel ch) throws Exception { final ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast(new ChannelInboundHandlerAdapter() { @Override/* w w w . j a va2 s. c o m*/ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { bumpTimeout(ctx); ctx.fireChannelRead(msg); } }); // first four bytes are length prefix of message, strip first four bytes. pipeline.addLast(new LengthFieldBasedFrameDecoder(maxFrameSize, 0, 4, 0, 4)); pipeline.addLast(new NativeRpcDecoder()); pipeline.addLast(new SimpleChannelInboundHandler<Object>() { @Override protected void channelRead0(final ChannelHandlerContext ctx, final Object msg) throws Exception { if (msg instanceof NativeRpcError) { final NativeRpcError error = (NativeRpcError) msg; if (log.isTraceEnabled()) { log.trace("[{}] remote error: {}", ctx.channel(), error.getMessage()); } future.fail(new NativeRpcRemoteException(address, error.getMessage())); ctx.channel().close(); return; } if (msg instanceof NativeRpcResponse) { if (log.isTraceEnabled()) { log.trace("[{}] response: cancelling heartbeat", ctx.channel()); } try { handleResponse((NativeRpcResponse) msg); } catch (Exception e) { future.fail(new Exception("Failed to handle response", e)); } return; } if (msg instanceof NativeRpcHeartBeat) { if (log.isTraceEnabled()) { log.trace("[{}] heartbeat: delaying timeout by {}ms", ctx.channel(), heartbeatInterval); } bumpTimeout(ctx); return; } throw new IllegalArgumentException("unable to handle type: " + msg); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { future.fail(cause); ctx.channel().close(); } }); pipeline.addLast(new LengthFieldPrepender(4)); pipeline.addLast(new NativeRpcEncoder()); }
From source file:com.spotify.heroic.rpc.nativerpc.NativeRpcClientSessionInitializer.java
License:Apache License
@Override protected void initChannel(final Channel ch) throws Exception { final ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast(new ChannelInboundHandlerAdapter() { @Override//from w w w.j av a2s. c o m public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { bumpTimeout(ctx); ctx.fireChannelRead(msg); } }); // first four bytes are length prefix of message, strip first four bytes. pipeline.addLast(new LengthFieldBasedFrameDecoder(maxFrameSize, 0, 4, 0, 4)); pipeline.addLast(new NativeRpcDecoder()); pipeline.addLast(new SimpleChannelInboundHandler<Object>() { @Override protected void channelRead0(final ChannelHandlerContext ctx, final Object msg) throws Exception { if (msg instanceof NativeRpcError) { final NativeRpcError error = (NativeRpcError) msg; if (log.isTraceEnabled()) { log.trace("[{}] remote error: {}", ctx.channel(), error.getMessage()); } future.fail(new NativeRpcRemoteException(address, error.getMessage())); ctx.channel().close(); return; } if (msg instanceof NativeRpcResponse) { if (log.isTraceEnabled()) { log.trace("[{}] response: cancelling heartbeat", ctx.channel()); } final Timeout old = heartbeatTimeout.getAndSet(null); if (old != null) { old.cancel(); } final NativeRpcResponse response = (NativeRpcResponse) msg; final R responseBody = mapper.readValue(response.getBody(), expected); future.resolve(responseBody); return; } if (msg instanceof NativeRpcHeartBeat) { if (log.isTraceEnabled()) { log.trace("[{}] heartbeat: delaying timeout by {}ms", ctx.channel(), heartbeatInterval); } bumpTimeout(ctx); return; } throw new IllegalArgumentException("unable to handle type: " + msg); } }); pipeline.addLast(new SimpleChannelInboundHandler<Object>() { @Override protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception { } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { future.fail(cause); } }); pipeline.addLast(new LengthFieldPrepender(4)); pipeline.addLast(new NativeRpcEncoder()); }
From source file:com.study.hc.net.netty.EchoServerHandler.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) { System.out.println("?" + ((ByteBuf) msg).toString(Charset.defaultCharset())); ctx.write(Unpooled.wrappedBuffer("98877".getBytes())); // ((ByteBuf) msg).release(); ctx.fireChannelRead(msg); }
From source file:com.tc.websocket.server.handler.ProxyFrontendHandler.java
License:Apache License
@Override public void channelRead(final ChannelHandlerContext ctx, final Object msg) { ByteBuf buf = (ByteBuf) msg;/*from w w w. j a v a 2 s . com*/ String data = new String(ByteBufUtil.getBytes(buf)); if (data.contains(Const.UPGRADE_WEBSOCKET) || data.contains(Const.GET_WEBSOCKET)) { proxy.set(false); } else if (data.contains(StringCache.HTTP_1_1)) { proxy.set(true); } if (proxy.get() == false) { writeToFile("frontend." + ctx.channel().id(), ByteBufUtil.getBytes(buf)); } if (Config.getInstance().isCertAuth()) { SslHandler sslhandler = (SslHandler) ctx.channel().pipeline().get("ssl"); try { X509Certificate cert = sslhandler.engine().getSession().getPeerCertificateChain()[0]; Principal p = cert.getSubjectDN(); /* Added by Miguel */ LdapName ldapDN = new LdapName(p.getName()); String username = ""; String thumbprint = getThumbPrint(cert.getEncoded()); for (Rdn rdn : ldapDN.getRdns()) { //System.out.println(rdn.getType() + " -> " + rdn.getValue()); if (rdn.getType().equals("CN")) { username = rdn.getValue().toString(); break; } } /* End Added by Miguel*/ String sessionId = parseSessionID(data); if (sessionId != null) { String current = System.getProperty("user.dir"); //System.out.println("Current working directory in Java : " + current); //File sessionFile = new File("c:/sessions/" + sessionId + ".txt"); File sessionFile = new File(current + "/data/sessions/" + sessionId + ".txt"); //only write the file if it hasn't been written yet. if (sessionFile.createNewFile()) { FileUtils.write(sessionFile, p.getName().replaceAll("\"", "").replaceAll("\\+", ",") + "\n" + username + "\n" + thumbprint); } } } catch (Exception e) { LOG.log(Level.SEVERE, null, e); } } if (proxy.get()) { ctx.channel().config().setAutoRead(false); if (outboundChannel.isActive()) { outboundChannel.writeAndFlush(buf).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) { if (future.isSuccess()) { // was able to flush out data, start to read the next chunk ctx.channel().read(); } else { future.channel().close(); } } }); } } else { //make sure the backend handler knows its a websocket connection. this.handler.setWebsocket(true); //get handle on the pipeline. ChannelPipeline pipeline = ctx.pipeline(); //apply the websocket handlers builder.apply(pipeline); //remove this handler. pipeline.remove(this); //fire the event to move on to the next handler. ctx.fireChannelRead(msg); } }
From source file:com.tc.websocket.server.handler.WebSocketValidationHandler.java
License:Apache License
@Override protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest req) throws Exception { if (this.isValidRequest(ctx, req)) { ReferenceCountUtil.retain(req);//from www . j a v a2s .c o m ctx.fireChannelRead(req); } }
From source file:com.tesora.dve.db.mysql.MysqlCommandSenderHandler.java
License:Open Source License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { if (msg instanceof MyMessage) { MyMessage message = (MyMessage) msg; dispatchRead(ctx, message);/* w w w.j a va 2 s . c o m*/ } else { if (logger.isDebugEnabled()) { logger.debug(String.format("unexpected message type, %s", (msg == null ? "null" : msg.getClass().getName()))); } ctx.fireChannelRead(msg); } }