private void handleGet(ChannelHandlerContext ctx, MappingHttpRequest request) { MappingHttpResponse response = new MappingHttpResponse(request.getProtocolVersion(), HttpResponseStatus.OK); String resp = ""; for (Component c : Component.values()) { resp += String.format( "name=%-20.20s enabled=%-10.10s local=%-10.10s initialized=%-10.10s\n", c.name(), c.isEnabled(), c.isLocal(), c.isInitialized()); } ChannelBuffer buf = ChannelBuffers.copiedBuffer(resp.getBytes()); response.setContent(buf); response.addHeader(HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(buf.readableBytes())); response.addHeader(HttpHeaders.Names.CONTENT_TYPE, "text/plain; charset=UTF-8"); ChannelFuture writeFuture = ctx.getChannel().write(response); writeFuture.addListener(ChannelFutureListener.CLOSE); }
@Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { if (e.getMessage() instanceof HttpRequest && HttpMethod.GET.equals(((HttpRequest) e.getMessage()).getMethod())) { HttpRequest request = (HttpRequest) e.getMessage(); HttpResponse response = new DefaultHttpResponse(request.getProtocolVersion(), HttpResponseStatus.OK); String resp = ""; for (Component c : Component.values()) { resp += String.format( "name=%-20.20s enabled=%-10.10s local=%-10.10s initialized=%-10.10s\n", c.name(), c.isEnabled(), c.isLocal(), c.isInitialized()); } ChannelBuffer buf = ChannelBuffers.copiedBuffer(resp.getBytes()); response.setContent(buf); response.addHeader(HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(buf.readableBytes())); response.addHeader(HttpHeaders.Names.CONTENT_TYPE, "text/plain; charset=UTF-8"); ChannelFuture writeFuture = ctx.getChannel().write(response); writeFuture.addListener(ChannelFutureListener.CLOSE); } else { ctx.sendUpstream(e); } }