List of usage examples for io.netty.handler.codec.json JsonObjectDecoder JsonObjectDecoder
public JsonObjectDecoder(boolean streamArrayElements)
From source file:com.heliosapm.tsdblite.handlers.http.HttpRequestManager.java
License:Apache License
/** * {@inheritDoc}/* w w w . j av a2 s. com*/ * @see io.netty.channel.SimpleChannelInboundHandler#channelRead0(io.netty.channel.ChannelHandlerContext, java.lang.Object) */ @Override protected void channelRead0(final ChannelHandlerContext ctx, final HttpRequest msg) throws Exception { try { final String uri = msg.uri(); final Channel channel = ctx.channel(); if (uri.endsWith("/favicon.ico")) { final DefaultFullHttpResponse resp = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, favicon); resp.headers().set(HttpHeaders.CONTENT_TYPE, "image/x-icon"); resp.headers().setInt(HttpHeaders.CONTENT_LENGTH, favSize); ctx.writeAndFlush(resp); return; } else if (uri.equals("/api/put") || uri.equals("/api/metadata")) { final ChannelPipeline p = ctx.pipeline(); // p.addLast(loggingHandler, jsonAdapter, new JsonObjectDecoder(true), traceHandler); p.addLast(jsonAdapter, new JsonObjectDecoder(true), traceHandler); // if(msg instanceof FullHttpRequest) { // ByteBuf b = ((FullHttpRequest)msg).content().copy(); // ctx.fireChannelRead(b); // } ctx.fireChannelRead(msg); p.remove("requestManager"); log.info("Switched to JSON Trace Processor"); return; } final TSDBHttpRequest r = new TSDBHttpRequest(msg, ctx.channel(), ctx); final HttpRequestHandler handler = requestHandlers.get(r.getRoute()); if (handler == null) { r.send404().addListener(new GenericFutureListener<Future<? super Void>>() { public void operationComplete(Future<? super Void> f) throws Exception { log.info("404 Not Found for {} Complete: success: {}", r.getRoute(), f.isSuccess()); if (!f.isSuccess()) { log.error("Error sending 404", f.cause()); } }; }); return; } handler.process(r); } catch (Exception ex) { log.error("HttpRequest Routing Error", ex); } }
From source file:com.heliosapm.tsdblite.handlers.http.HttpSwitch.java
License:Apache License
@Override protected void decode(final ChannelHandlerContext ctx, final HttpRequest msg, final List<Object> out) throws Exception { final String uri = msg.uri(); log.info("-----------------------> URI [{}]", uri); if (uri.endsWith("/favicon.ico")) { final DefaultFullHttpResponse resp = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, favicon); resp.headers().set(HttpHeaders.CONTENT_TYPE, "image/x-icon"); resp.headers().setInt(HttpHeaders.CONTENT_LENGTH, favSize); ctx.writeAndFlush(resp);/*ww w .jav a 2 s .co m*/ return; } ReferenceCountUtil.retain(msg); final ChannelPipeline p = ctx.pipeline(); final int index = uri.indexOf("/api/"); final String endpoint = index == -1 ? "" : uri.substring(5); if (index != -1 && pureJsonEndPoints.contains(endpoint)) { log.info("Switching to PureJSON handler"); p.addLast(eventExecutorGroup, "httpToJson", httpToJson); // p.addLast("jsonLogger", loggingHandler); p.addLast("jsonDecoder", new JsonObjectDecoder(true)); // p.addLast("jsonLogger", loggingHandler); p.addLast("traceHandler", traceHandler); p.remove(this); if (msg instanceof FullHttpMessage) { out.add(msg); } } else { log.info("Switching to Http Request Manager"); out.add(msg); p.addLast(eventExecutorGroup, "requestManager", HttpRequestManager.getInstance()); p.remove(this); } }
From source file:com.netflix.iep.http.ByteBufs.java
License:Apache License
/** * Process json data encoded as a sequence of ByteBufs so that each object within an array * will be a single ByteBuf in the output. *//* ww w . j ava 2 s. c o m*/ public static Observable.Transformer<ByteBuf, ByteBuf> json() { return input -> decode(input, new EmbeddedChannel(new JsonObjectDecoder(true))); }
From source file:org.cloudfoundry.reactor.util.JsonCodec.java
License:Apache License
public static <T> Function<Mono<HttpClientResponse>, Flux<T>> decode(ObjectMapper objectMapper, Class<T> responseType) { return inbound -> inbound.flatMapMany(response -> response .addHandler(new JsonObjectDecoder(MAX_PAYLOAD_SIZE)).receive().asByteArray().map(payload -> { try { return objectMapper.readValue(payload, responseType); } catch (Throwable t) { throw new JsonParsingException(t.getMessage(), t, new String(payload, Charset.defaultCharset())); }//from w w w. ja v a 2 s . c o m })); }
From source file:reactor.ipc.netty.http.HttpOperationsTest.java
License:Open Source License
@Test public void httpAndJsonDecoders() { EmbeddedChannel channel = new EmbeddedChannel(); NettyContext testContext = () -> channel; ChannelHandler handler = new JsonObjectDecoder(true); testContext.addHandlerLast("foo", handler); HttpOperations.autoAddHttpExtractor(testContext, "foo", handler); String json1 = "[{\"some\": 1} , {\"valu"; String json2 = "e\": true, \"test\": 1}]"; Object[] content = new Object[3]; content[0] = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); content[1] = new DefaultHttpContent(Unpooled.copiedBuffer(json1, CharsetUtil.UTF_8)); content[2] = new DefaultLastHttpContent(Unpooled.copiedBuffer(json2, CharsetUtil.UTF_8)); channel.writeInbound(content);//from w w w . j a va 2 s . c o m Object t = channel.readInbound(); assertThat(t, instanceOf(HttpResponse.class)); assertThat(t, not(instanceOf(HttpContent.class))); t = channel.readInbound(); assertThat(t, instanceOf(ByteBuf.class)); assertThat(((ByteBuf) t).toString(CharsetUtil.UTF_8), is("{\"some\": 1}")); t = channel.readInbound(); assertThat(t, instanceOf(ByteBuf.class)); assertThat(((ByteBuf) t).toString(CharsetUtil.UTF_8), is("{\"value\": true, \"test\": 1}")); t = channel.readInbound(); assertThat(t, is(LastHttpContent.EMPTY_LAST_CONTENT)); t = channel.readInbound(); assertThat(t, nullValue()); }