private static void testPerformOpeningHandshake0(boolean subProtocol) { EmbeddedChannel ch = new EmbeddedChannel( new HttpObjectAggregator(42), new HttpRequestDecoder(), new HttpResponseEncoder()); FullHttpRequest req = ReferenceCountUtil.releaseLater( new DefaultFullHttpRequest( HTTP_1_1, HttpMethod.GET, "/chat", Unpooled.copiedBuffer("^n:ds[4U", CharsetUtil.US_ASCII))); req.headers().set(HttpHeaderNames.HOST, "server.example.com"); req.headers().set(HttpHeaderNames.UPGRADE, HttpHeaderValues.WEBSOCKET); req.headers().set(HttpHeaderNames.CONNECTION, "Upgrade"); req.headers().set(HttpHeaderNames.ORIGIN, "http://example.com"); req.headers().set(HttpHeaderNames.SEC_WEBSOCKET_KEY1, "4 @1 46546xW%0l 1 5"); req.headers().set(HttpHeaderNames.SEC_WEBSOCKET_KEY2, "12998 5 Y3 1 .P00"); req.headers().set(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL, "chat, superchat"); if (subProtocol) { new WebSocketServerHandshaker00("ws://example.com/chat", "chat", Integer.MAX_VALUE) .handshake(ch, req); } else { new WebSocketServerHandshaker00("ws://example.com/chat", null, Integer.MAX_VALUE) .handshake(ch, req); } EmbeddedChannel ch2 = new EmbeddedChannel(new HttpResponseDecoder()); ch2.writeInbound(ch.readOutbound()); HttpResponse res = ch2.readInbound(); Assert.assertEquals( "ws://example.com/chat", res.headers().get(HttpHeaderNames.SEC_WEBSOCKET_LOCATION)); if (subProtocol) { Assert.assertEquals("chat", res.headers().get(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL)); } else { Assert.assertNull(res.headers().get(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL)); } LastHttpContent content = ch2.readInbound(); Assert.assertEquals("8jKS'y:G*Co,Wxa-", content.content().toString(CharsetUtil.US_ASCII)); content.release(); }
public void messageReceived(ChannelHandlerContext ctx, Object o) throws Exception { if (o instanceof HttpRequest) { // DefaultHttpRequest ) { queue.add((HttpRequest) o); } else if (o instanceof LastHttpContent) { HttpRequest req = queue.remove(); req.getMethod(); req.getUri(); req.headers(); LastHttpContent content = (LastHttpContent) o; ByteBuf buf = content.content(); if (buf.readableBytes() > 0) { Gson gson = GsonFactory.createBuilder().create(); Reader in = new InputStreamReader(new ByteBufInputStream(buf), "utf-8"); Object v = gson.fromJson(in, Class.forName("com.logbook.logbook.resources.logs.LogEntryDTO")); System.out.println("v = " + v); } System.out.println( req.getMethod() + " " + req.getUri() + " -- " + buf.readableBytes() + " bytes"); HttpResponse r = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); r.headers().add("Content-Length", "0"); ctx.write(r); } else { System.out.println("o = " + o + " : " + o.getClass()); } }