List of usage examples for io.netty.buffer Unpooled wrappedBuffer
public static ByteBuf wrappedBuffer(ByteBuffer... buffers)
From source file:com.weibo.api.motan.protocol.grpc.http.NettyHttpRequestHandler.java
License:Apache License
@SuppressWarnings({ "rawtypes", "unchecked" }) protected FullHttpResponse buildHttpResponse(Response response, boolean keepAlive) throws Exception { Object value = response.getValue(); byte[] responseBytes = null; if (value instanceof Message) { Marshaller marshaller = ProtoUtils.jsonMarshaller( (Message) value.getClass().getMethod("getDefaultInstance", null).invoke(null, null)); InputStream is = marshaller.stream(value); responseBytes = new byte[is.available()]; is.read(responseBytes);/*from w ww . j av a 2 s. c o m*/ } else { // TODO not pb } FullHttpResponse httpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(responseBytes)); httpResponse.headers().set(HttpHeaderNames.CONTENT_TYPE, "application/x-www-form-urlencoded"); httpResponse.headers().set(HttpHeaderNames.CONTENT_LENGTH, httpResponse.content().readableBytes()); if (keepAlive) { httpResponse.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE); } else { httpResponse.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE); } return httpResponse; }
From source file:com.weibo.api.motan.protocol.grpc.http.NettyHttpRequestHandler.java
License:Apache License
protected FullHttpResponse buildDefaultResponse(String msg, HttpResponseStatus status) { FullHttpResponse errorResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, Unpooled.wrappedBuffer(msg.getBytes())); return errorResponse; }
From source file:com.weibo.api.motan.transport.netty4.yar.YarMessageHandlerWarpper.java
License:Apache License
@Override public Object handle(Channel channel, Object message) { FullHttpRequest httpRequest = (FullHttpRequest) message; String uri = httpRequest.getUri(); int index = uri.indexOf("?");// should not be null String requestPath = uri;/* ww w.j a va2 s .c o m*/ Map<String, String> attachments = null; if (index > -1) { requestPath = uri.substring(0, index); if (index != uri.length() - 1) { attachments = getAttachMents(uri.substring(index + 1, uri.length())); } } YarResponse yarResponse = null; String packagerName = "JSON"; try { ByteBuf buf = httpRequest.content(); final byte[] contentBytes = new byte[buf.readableBytes()]; buf.getBytes(0, contentBytes); YarRequest yarRequest = new AttachmentRequest(YarProtocol.buildRequest(contentBytes), attachments); yarRequest.setRequestPath(requestPath); yarResponse = (YarResponse) orgHandler.handle(channel, yarRequest); } catch (Exception e) { LoggerUtil.error("YarMessageHandlerWarpper handle yar request fail.", e); yarResponse = YarProtocolUtil.buildDefaultErrorResponse(e.getMessage(), packagerName); } byte[] responseBytes; try { responseBytes = YarProtocol.toProtocolBytes(yarResponse); } catch (IOException e) { throw new MotanFrameworkException("convert yar response to bytes fail.", e); } FullHttpResponse httpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(responseBytes)); httpResponse.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/x-www-form-urlencoded"); httpResponse.headers().set(HttpHeaders.Names.CONTENT_LENGTH, httpResponse.content().readableBytes()); if (HttpHeaders.isKeepAlive(httpRequest)) { httpResponse.headers().set(HttpHeaders.Names.CONNECTION, Values.KEEP_ALIVE); } else { httpResponse.headers().set(HttpHeaders.Names.CONNECTION, Values.CLOSE); } return httpResponse; }
From source file:com.weibo.yar.yarserver.HttpServerHandler.java
License:Apache License
@Override protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest msg) throws Exception { ByteBuf buf = msg.content();/*from ww w.j a v a2 s .co m*/ byte[] bytes = new byte[buf.readableBytes()]; buf.getBytes(0, bytes); YarRequest yarRequest = YarProtocol.buildRequest(bytes); YarResponse yarResponse = process(yarRequest); FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(YarProtocol.toProtocolBytes(yarResponse))); response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/x-www-form-urlencoded"); response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes()); if (HttpHeaders.isKeepAlive(msg)) { response.headers().set(HttpHeaders.Names.CONNECTION, Values.KEEP_ALIVE); } ctx.write(response); ctx.flush(); ctx.close(); }
From source file:com.whizzosoftware.hobson.lifx.api.codec.LIFXFrameDecoderTest.java
License:Open Source License
@Test public void testLightState() throws Exception { byte[] expected = new byte[] { 0x58, 0x00, 0x00, 0x54, 0x42, 0x52, 0x4B, 0x52, (byte) 0xD0, 0x73, (byte) 0xD5, 0x12, 0x09, 0x34, 0x00, 0x00, 0x4C, 0x49, 0x46, 0x58, 0x56, 0x32, 0x00, 0x00, 0x58, (byte) 0x92, 0x17, (byte) 0xD9, 0x2E, 0x0F, 0x3C, 0x14, 0x6B, 0x00, 0x00, 0x00, (byte) 0xD3, (byte) 0xFD, 0x00, 0x00, (byte) 0xFF, (byte) 0xFF, (byte) 0xAC, 0x0D, 0x00, 0x00, (byte) 0xFF, (byte) 0xFF, 0x44, 0x61, 0x6E, 0x27, 0x73, 0x20, 0x4F, 0x66, 0x66, 0x69, 0x63, 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; LIFXFrameDecoder d = new LIFXFrameDecoder(); List<Object> list = new ArrayList<>(); d.decode(null, new DatagramPacket(Unpooled.wrappedBuffer(expected), new InetSocketAddress("255.255.255.255", 56700)), list); assertEquals(1, list.size());//from w ww.ja v a 2 s . c o m assertTrue(list.get(0) instanceof LightState); assertEquals("Dan's Office", ((LightState) list.get(0)).getLabel()); }
From source file:com.whizzosoftware.hobson.lifx.api.codec.LIFXFrameDecoderTest.java
License:Open Source License
@Test public void testReadShortLE() { LIFXFrameDecoder d = new LIFXFrameDecoder(); assertEquals(65535, d.read16BitLE(Unpooled.wrappedBuffer(new byte[] { -1, -1 }))); assertEquals(32767, d.read16BitLE(Unpooled.wrappedBuffer(new byte[] { -1, 127 }))); assertEquals(0, d.read16BitLE(Unpooled.wrappedBuffer(new byte[] { 0, 0 }))); }
From source file:com.whizzosoftware.wzwave.channel.TransactionInboundHandlerTest.java
License:Open Source License
@Test public void testRequestNodeInfoFailure() throws Exception { MockChannelHandlerContext ctx = new MockChannelHandlerContext(); TransactionInboundHandler h = new TransactionInboundHandler(); // initiate new RequestNodeInfo transaction RequestNodeInfo requestFrame = new RequestNodeInfo((byte) 0x2c); h.userEventTriggered(ctx, new DataFrameSentEvent(requestFrame, true)); assertEquals(0, ctx.getWriteQueue().size()); // receive ACK h.channelRead(ctx, new ACK()); assertEquals(0, ctx.getWriteQueue().size()); // receive unsuccessful send h.channelRead(ctx, new RequestNodeInfo( Unpooled.wrappedBuffer(new byte[] { 0x01, 0x04, 0x01, 0x60, 0x00, (byte) 0x9a }))); assertTrue(h.hasCurrentTransaction()); // confirm request was re-queued assertEquals(1, ctx.getWriteQueue().size()); assertTrue(ctx.getWriteQueue().get(0) instanceof OutboundDataFrame); assertTrue(((OutboundDataFrame) ctx.getWriteQueue().get(0)).getDataFrame() instanceof RequestNodeInfo); // simulate re-send requestFrame.incremenentSendCount(); h.userEventTriggered(ctx, new DataFrameSentEvent(requestFrame, true)); assertTrue(h.hasCurrentTransaction()); // receive ACK h.channelRead(ctx, new ACK()); assertEquals(1, ctx.getWriteQueue().size()); // receive unsuccessful send h.channelRead(ctx, new RequestNodeInfo( Unpooled.wrappedBuffer(new byte[] { 0x01, 0x04, 0x01, 0x60, 0x00, (byte) 0x9a }))); assertTrue(h.hasCurrentTransaction()); // confirm request was re-queued assertEquals(2, ctx.getWriteQueue().size()); assertTrue(ctx.getWriteQueue().get(1) instanceof OutboundDataFrame); assertTrue(((OutboundDataFrame) ctx.getWriteQueue().get(1)).getDataFrame() instanceof RequestNodeInfo); // simulate re-send requestFrame.incremenentSendCount(); h.userEventTriggered(ctx, new DataFrameSentEvent(requestFrame, true)); assertTrue(h.hasCurrentTransaction()); // receive ACK h.channelRead(ctx, new ACK()); assertEquals(2, ctx.getWriteQueue().size()); // receive unsuccessful send h.channelRead(ctx, new RequestNodeInfo( Unpooled.wrappedBuffer(new byte[] { 0x01, 0x04, 0x01, 0x60, 0x00, (byte) 0x9a }))); assertFalse(h.hasCurrentTransaction()); // confirm request was not requeued assertEquals(2, ctx.getWriteQueue().size()); }
From source file:com.whizzosoftware.wzwave.channel.TransactionInboundHandlerTest.java
License:Open Source License
@Test public void testAddNodeToNetworkTransaction() { MockChannelHandlerContext ctx = new MockChannelHandlerContext(); TransactionInboundHandler h = new TransactionInboundHandler(); h.channelRead(ctx, new AddNodeToNetwork(Unpooled .wrappedBuffer(new byte[] { 0x01, 0x07, 0x00, 0x4A, 0x01, 0x02, 0x00, 0x00, (byte) 0xB1 }))); assertTrue(h.hasCurrentTransaction()); h.channelRead(ctx,//from ww w .ja v a 2 s. c om new AddNodeToNetwork(Unpooled.wrappedBuffer(new byte[] { 0x01, 0x12, 0x00, 0x4A, 0x01, 0x03, 0x02, 0x0B, 0x04, 0x20, 0x01, 0x30, (byte) 0x80, (byte) 0x84, 0x71, 0x70, (byte) 0x85, (byte) 0x86, 0x72, (byte) 0xCD }))); assertTrue(h.hasCurrentTransaction()); h.channelRead(ctx, new AddNodeToNetwork(Unpooled.wrappedBuffer(new byte[] { 0x01, 0x12, 0x00, 0x4A, 0x01, 0x05, 0x02, 0x0B, 0x04, 0x20, 0x01, 0x30, (byte) 0x80, (byte) 0x84, 0x71, 0x70, (byte) 0x85, (byte) 0x86, 0x72, (byte) 0xCB, 0x18 }))); assertFalse(h.hasCurrentTransaction()); }
From source file:com.whizzosoftware.wzwave.channel.TransactionInboundHandlerTest.java
License:Open Source License
@Test public void testSendDataTransactionTimeout() throws Exception { MockChannelHandlerContext ctx = new MockChannelHandlerContext(); TransactionInboundHandler h = new TransactionInboundHandler(); h.userEventTriggered(ctx,// w ww. j ava 2 s .c o m new DataFrameSentEvent(new SendData(Unpooled.wrappedBuffer( new byte[] { 0x01, 0x09, 0x00, 0x13, 0x03, 0x02, 0x20, 0x02, 0x05, 0x31, (byte) 0xF2 })), true)); }
From source file:com.whizzosoftware.wzwave.channel.TransactionInboundHandlerTest.java
License:Open Source License
@Test public void testSendDataSleepingNodeTransactionNoACKFailure() throws Exception { MockChannelHandlerContext ctx = new MockChannelHandlerContext(); TransactionInboundHandler h = new TransactionInboundHandler(); h.userEventTriggered(ctx,//from ww w . j a va2s. com new DataFrameSentEvent(new SendData(Unpooled.wrappedBuffer( new byte[] { 0x01, 0x09, 0x00, 0x13, 0x06, 0x02, 0x00, 0x00, 0x25, 0x0a, (byte) 0xce })), false)); assertEquals(1, ctx.getUserEvents().size()); assertTrue(ctx.getUserEvents().get(0) instanceof TransactionStartedEvent); h.channelRead(ctx, new ACK()); assertEquals(1, ctx.getUserEvents().size()); h.channelRead(ctx, new SendData(Unpooled.wrappedBuffer(new byte[] { 0x01, 0x04, 0x01, 0x13, 0x01, (byte) 0xe8 }))); assertEquals(1, ctx.getUserEvents().size()); h.channelRead(ctx, new SendData( Unpooled.wrappedBuffer(new byte[] { 0x01, 0x05, 0x00, 0x13, 0x0a, 0x01, (byte) 0xe2 }))); assertEquals(2, ctx.getUserEvents().size()); assertTrue(ctx.getUserEvents().get(1) instanceof TransactionFailedEvent); }