List of usage examples for io.netty.buffer Unpooled copiedBuffer
private static ByteBuf copiedBuffer(CharBuffer buffer, Charset charset)
From source file:cn.dennishucd.nettyhttpserver.HttpServerHandler.java
License:Apache License
private static void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) { FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, status, Unpooled.copiedBuffer("Failure: " + status + "\r\n", CharsetUtil.UTF_8)); response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8"); // Close the connection as soon as the error message is sent. ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); }
From source file:cn.pengj.udpdemo.EchoSeverHandler.java
License:Open Source License
@Override protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception { // ??//from w w w . ja v a 2 s .co m ByteBuf buf = (ByteBuf) packet.copy().content(); byte[] req = new byte[buf.readableBytes()]; buf.readBytes(req); String body = new String(req, CharsetUtil.UTF_8); System.out.println("?NOTE>>>>>> ?" + body); // ??? ctx.writeAndFlush(new DatagramPacket( Unpooled.copiedBuffer("HelloServer" + System.currentTimeMillis(), CharsetUtil.UTF_8), packet.sender())).sync(); }
From source file:com.addthis.hydra.query.loadbalance.NextQueryTask.java
License:Apache License
private static void sendDetailedError(ChannelHandlerContext ctx, Throwable cause) { if (cause == null) { cause = new RuntimeException("query failed for unknown reasons"); }/*from w w w.java2 s. co m*/ String reasonPhrase = cause.getMessage(); HttpResponseStatus responseStatus; try { responseStatus = new HttpResponseStatus(500, reasonPhrase); } catch (NullPointerException | IllegalArgumentException ignored) { reasonPhrase = cause.getClass().getSimpleName(); responseStatus = new HttpResponseStatus(500, reasonPhrase); } String detailPhrase = logAndFormatErrorDetail(cause); FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, responseStatus, Unpooled.copiedBuffer(detailPhrase + "\r\n", CharsetUtil.UTF_8)); response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8"); log.trace("issuing error of {}", responseStatus); // Close the connection as soon as the error message is sent. ctx.channel().writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); }
From source file:com.addthis.hydra.query.web.HttpUtils.java
License:Apache License
public static void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) { FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, status, Unpooled.copiedBuffer("Failure: " + status.toString() + "\r\n", CharsetUtil.UTF_8)); response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8"); log.trace("issuing error of {}", status); // Close the connection as soon as the error message is sent. ctx.channel().writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); }
From source file:com.adobe.acs.livereload.impl.WebSocketServerHandler.java
License:Apache License
private static void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) { // Generate an error page if response getStatus code is not OK (200). if (res.getStatus().code() != HttpServletResponse.SC_OK) { ByteBuf buf = Unpooled.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8); res.content().writeBytes(buf);// w ww .j a v a 2s . c o m buf.release(); setContentLength(res, res.content().readableBytes()); } // Send the response and close the connection if necessary. ChannelFuture f = ctx.channel().writeAndFlush(res); if (!isKeepAlive(req) || res.getStatus().code() != HttpServletResponse.SC_OK) { f.addListener(ChannelFutureListener.CLOSE); } }
From source file:com.athena.dolly.websocket.server.test.WebSocketServerHandler.java
License:Apache License
private static void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) { // Generate an error page if response getStatus code is not OK (200). if (res.getStatus().code() != 200) { ByteBuf buf = Unpooled.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8); res.content().writeBytes(buf);/*from w w w. j av a2 s . c o m*/ buf.release(); setContentLength(res, res.content().readableBytes()); } // Send the response and close the connection if necessary. ChannelFuture f = ctx.channel().writeAndFlush(res); if (!isKeepAlive(req) || res.getStatus().code() != 200) { f.addListener(ChannelFutureListener.CLOSE); } }
From source file:com.athena.dolly.websocket.server.test.WebSocketSslServerHandler.java
License:Apache License
private static void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) { // Generate an error page if response getStatus code is not OK (200). if (res.getStatus().code() != 200) { ByteBuf buf = Unpooled.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8); res.content().writeBytes(buf);/*from w ww. j a v a 2 s. co m*/ buf.release(); setContentLength(res, res.content().readableBytes()); } // Send the response and close the connection if necessary. ChannelFuture f = ctx.channel().write(res); if (!isKeepAlive(req) || res.getStatus().code() != 200) { f.addListener(ChannelFutureListener.CLOSE); } }
From source file:com.bala.learning.learning.netty.HttpServerHandler.java
License:Apache License
private boolean writeResponse(HttpObject currentObj, ChannelHandlerContext ctx) { // Decide whether to close the connection or not. boolean keepAlive = HttpHeaders.isKeepAlive(request); // Build the response object. FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, currentObj.getDecoderResult().isSuccess() ? OK : BAD_REQUEST, Unpooled.copiedBuffer(buf.toString(), CharsetUtil.UTF_8)); response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8"); if (keepAlive) { // Add 'Content-Length' header only for a keep-alive connection. response.headers().set(CONTENT_LENGTH, response.content().readableBytes()); // Add keep alive header as per: // - http://www.w3.org/Protocols/HTTP/1.1/draft-ietf-http-v11-spec-01.html#Connection response.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE); }/*w ww . j a v a 2s . co m*/ // Encode the cookie. String cookieString = request.headers().get(COOKIE); if (cookieString != null) { Set<Cookie> cookies = CookieDecoder.decode(cookieString); if (!cookies.isEmpty()) { // Reset the cookies if necessary. for (Cookie cookie : cookies) { response.headers().add(SET_COOKIE, ServerCookieEncoder.encode(cookie)); } } } else { // Browser sent no cookie. Add some. response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key1", "value1")); response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key2", "value2")); } // Write the response. ctx.write(response); return keepAlive; }
From source file:com.brainlounge.zooterrain.netty.WebSocketServerIndexPage.java
License:Apache License
public ByteBuf getContent(String webSocketLocation) { String contentReplaced = content.replace("${wsLoc}", webSocketLocation); return Unpooled.copiedBuffer(contentReplaced, CharsetUtil.US_ASCII); }
From source file:com.caricah.iotracah.server.httpserver.netty.HttpServerHandler.java
License:Apache License
@Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { try {/*from w w w .j av a2 s . c o m*/ log.info(" exceptionCaught : Unhandled exception: ", cause); JSONObject error = new JSONObject(); error.put("message", cause.getMessage()); error.put("status", "failure"); ByteBuf buffer = Unpooled.copiedBuffer(error.toString(), CharsetUtil.UTF_8); // Build the response object. FullHttpResponse httpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR, buffer); ctx.channel().writeAndFlush(httpResponse).addListener(ChannelFutureListener.CLOSE); } catch (Exception ex) { log.debug(" exceptionCaught : trying to close socket because we got an unhandled exception", ex); } }