List of usage examples for io.netty.channel ChannelHandlerContext alloc
ByteBufAllocator alloc();
From source file:com.flysoloing.learning.network.netty.binary.MemcacheClientHandler.java
License:Apache License
/** * Transforms basic string requests to binary memcache requests *//* w w w . j a va 2s .co m*/ @Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) { String command = (String) msg; if (command.startsWith("get ")) { String keyString = command.substring("get ".length()); ByteBuf key = Unpooled.wrappedBuffer(keyString.getBytes(CharsetUtil.UTF_8)); BinaryMemcacheRequest req = new DefaultBinaryMemcacheRequest(key); req.setOpcode(BinaryMemcacheOpcodes.GET); ctx.write(req, promise); } else if (command.startsWith("set ")) { String[] parts = command.split(" ", 3); if (parts.length < 3) { throw new IllegalArgumentException("Malformed Command: " + command); } String keyString = parts[1]; String value = parts[2]; ByteBuf key = Unpooled.wrappedBuffer(keyString.getBytes(CharsetUtil.UTF_8)); ByteBuf content = Unpooled.wrappedBuffer(value.getBytes(CharsetUtil.UTF_8)); ByteBuf extras = ctx.alloc().buffer(8); extras.writeZero(8); BinaryMemcacheRequest req = new DefaultFullBinaryMemcacheRequest(key, extras, content); req.setOpcode(BinaryMemcacheOpcodes.SET); ctx.write(req, promise); } else { throw new IllegalStateException("Unknown Message: " + msg); } }
From source file:com.flysoloing.learning.network.netty.http2.helloworld.multiplex.server.HelloWorldHttp2Handler.java
License:Apache License
/** * If receive a frame with end-of-stream set, send a pre-canned response. *//*from ww w . ja va 2 s . c om*/ public void onHeadersRead(ChannelHandlerContext ctx, Http2HeadersFrame headers) throws Exception { if (headers.isEndStream()) { ByteBuf content = ctx.alloc().buffer(); content.writeBytes(RESPONSE_BYTES.duplicate()); ByteBufUtil.writeAscii(content, " - via HTTP/2"); sendResponse(ctx, content); } }
From source file:com.flysoloing.learning.network.netty.http2.helloworld.server.HelloWorldHttp1Handler.java
License:Apache License
@Override public void channelRead0(ChannelHandlerContext ctx, FullHttpRequest req) throws Exception { if (HttpUtil.is100ContinueExpected(req)) { ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE)); }/*from w ww . j a va 2 s . c om*/ boolean keepAlive = HttpUtil.isKeepAlive(req); ByteBuf content = ctx.alloc().buffer(); content.writeBytes(HelloWorldHttp2Handler.RESPONSE_BYTES.duplicate()); ByteBufUtil.writeAscii(content, " - via " + req.protocolVersion() + " (" + establishApproach + ")"); FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, content); response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8"); response.headers().setInt(CONTENT_LENGTH, response.content().readableBytes()); if (!keepAlive) { ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); } else { response.headers().set(CONNECTION, HttpHeaderValues.KEEP_ALIVE); ctx.writeAndFlush(response); } }
From source file:com.flysoloing.learning.network.netty.http2.helloworld.server.HelloWorldHttp2Handler.java
License:Apache License
public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int padding, boolean endOfStream) { if (endOfStream) { ByteBuf content = ctx.alloc().buffer(); content.writeBytes(RESPONSE_BYTES.duplicate()); ByteBufUtil.writeAscii(content, " - via HTTP/2"); sendResponse(ctx, streamId, content); }/*from w ww . jav a2 s .c o m*/ }
From source file:com.flysoloing.learning.network.netty.http2.tiles.FallbackRequestHandler.java
License:Apache License
@Override protected void channelRead0(ChannelHandlerContext ctx, HttpRequest req) throws Exception { if (HttpUtil.is100ContinueExpected(req)) { ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE)); }/*from www .j a va2s .c o m*/ ByteBuf content = ctx.alloc().buffer(); content.writeBytes(response.duplicate()); FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, content); response.headers().set(CONTENT_TYPE, "text/html; charset=UTF-8"); response.headers().setInt(CONTENT_LENGTH, response.content().readableBytes()); ctx.write(response).addListener(ChannelFutureListener.CLOSE); }
From source file:com.flysoloing.learning.network.netty.http2.tiles.Http2RequestHandler.java
License:Apache License
private void handlePage(ChannelHandlerContext ctx, String streamId, int latency, FullHttpRequest request) { byte[] body = Html.body(latency); ByteBuf content = ctx.alloc().buffer(Html.HEADER.length + body.length + Html.FOOTER.length); content.writeBytes(Html.HEADER);/*from w ww .jav a2 s . c o m*/ content.writeBytes(body); content.writeBytes(Html.FOOTER); FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, content); response.headers().set(CONTENT_TYPE, "text/html; charset=UTF-8"); sendResponse(ctx, streamId, latency, response, request); }
From source file:com.flysoloing.learning.network.netty.portunification.PortUnificationServerHandler.java
License:Apache License
private void enableSsl(ChannelHandlerContext ctx) { ChannelPipeline p = ctx.pipeline();/* w w w. ja v a 2 s . c o m*/ p.addLast("ssl", sslCtx.newHandler(ctx.alloc())); p.addLast("unificationA", new PortUnificationServerHandler(sslCtx, false, detectGzip)); p.remove(this); }
From source file:com.flysoloing.learning.network.netty.redis.RedisClientHandler.java
License:Apache License
@Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) { String[] commands = ((String) msg).split("\\s+"); List<RedisMessage> children = new ArrayList<RedisMessage>(commands.length); for (String cmdString : commands) { children.add(new FullBulkStringRedisMessage(ByteBufUtil.writeUtf8(ctx.alloc(), cmdString))); }//from ww w. jav a 2 s .com RedisMessage request = new ArrayRedisMessage(children); ctx.write(request, promise); }
From source file:com.github.milenkovicm.kafka.handler.CompositeProducerHandler.java
License:Apache License
@Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { final ByteBuf message = (ByteBuf) msg; final ByteBuf kafkaMessage = creteProduceRequest(ctx.alloc(), message, topicName); super.write(ctx, kafkaMessage, promise); }
From source file:com.github.milenkovicm.kafka.handler.CopyProducerHandler.java
License:Apache License
@Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { final ByteBuf message = (ByteBuf) msg; final ByteBuf kafkaMessage = creteProduceRequest(ctx.alloc(), message, topicName); message.release();/*from ww w .ja v a 2 s. co m*/ super.write(ctx, kafkaMessage, promise); }