Java Code Examples for org.jboss.netty.handler.codec.http.HttpResponse#setContent()
The following examples show how to use
org.jboss.netty.handler.codec.http.HttpResponse#setContent() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WelcomeHandler.java From feeyo-hlsserver with Apache License 2.0 | 6 votes |
@Override public void execute(ChannelHandlerContext ctx, MessageEvent e) { VelocityBuilder velocityBuilder = new VelocityBuilder(); String htmlText = velocityBuilder.generate("index.vm", "UTF8", null); HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); response.headers().add(HttpHeaders.Names.CONTENT_LENGTH, htmlText.length()); response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/html; charset=UTF-8"); ChannelBuffer buffer = ChannelBuffers.copiedBuffer(htmlText, CharsetUtil.UTF_8); response.setContent(buffer); ChannelFuture channelFuture = ctx.getChannel().write(response); if (channelFuture.isSuccess()) { channelFuture.getChannel().close(); } }
Example 2
Source File: HttpServerHandler.java From netty-servlet with Apache License 2.0 | 6 votes |
@Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent event) throws Exception { if (event.getMessage() instanceof HttpRequest) { try { HttpServletRequest httpServletRequest = new NettyHttpServletRequestAdaptor((HttpRequest) event.getMessage(), ctx.getChannel()); HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK); response.setContent(new DynamicChannelBuffer(200)); HttpServletResponse httpServletResponse = new NettyHttpServletResponseAdaptor(response, ctx.getChannel()); dispatcher.dispatch(httpServletRequest,httpServletResponse); response.headers().set(HttpHeaders.Names.CONTENT_LENGTH,response.getContent().writerIndex()); ChannelFuture future = ctx.getChannel().write(response); future.addListener(ChannelFutureListener.CLOSE); } catch (Exception e) { e.printStackTrace(); } } }
Example 3
Source File: TestDelegationTokenRemoteFetcher.java From big-c with Apache License 2.0 | 6 votes |
@Override public void handle(Channel channel, Token<DelegationTokenIdentifier> token, String serviceUrl) throws IOException { Assert.assertEquals(testToken, token); Credentials creds = new Credentials(); creds.addToken(new Text(serviceUrl), token); DataOutputBuffer out = new DataOutputBuffer(); creds.write(out); int fileLength = out.getData().length; ChannelBuffer cbuffer = ChannelBuffers.buffer(fileLength); cbuffer.writeBytes(out.getData()); HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK); response.setHeader(HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(fileLength)); response.setContent(cbuffer); channel.write(response).addListener(ChannelFutureListener.CLOSE); }
Example 4
Source File: TestDelegationTokenRemoteFetcher.java From hadoop with Apache License 2.0 | 6 votes |
@Override public void handle(Channel channel, Token<DelegationTokenIdentifier> token, String serviceUrl) throws IOException { Assert.assertEquals(testToken, token); Credentials creds = new Credentials(); creds.addToken(new Text(serviceUrl), token); DataOutputBuffer out = new DataOutputBuffer(); creds.write(out); int fileLength = out.getData().length; ChannelBuffer cbuffer = ChannelBuffers.buffer(fileLength); cbuffer.writeBytes(out.getData()); HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK); response.setHeader(HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(fileLength)); response.setContent(cbuffer); channel.write(response).addListener(ChannelFutureListener.CLOSE); }
Example 5
Source File: FileServerHandler.java From netty-file-parent with Apache License 2.0 | 6 votes |
private void writeResponse(Channel channel) { ChannelBuffer buf = ChannelBuffers.copiedBuffer( this.responseContent.toString(), CharsetUtil.UTF_8); this.responseContent.setLength(0); boolean close = ("close".equalsIgnoreCase(this.request .getHeader("Connection"))) || ((this.request.getProtocolVersion() .equals(HttpVersion.HTTP_1_0)) && (!"keep-alive" .equalsIgnoreCase(this.request.getHeader("Connection")))); HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); response.setContent(buf); response.setHeader("Content-Type", "text/plain; charset=UTF-8"); if (!close) { response.setHeader("Content-Length", String.valueOf(buf.readableBytes())); } ChannelFuture future = channel.write(response); if (close) future.addListener(ChannelFutureListener.CLOSE); }
Example 6
Source File: HlsStreamsHandler.java From feeyo-hlsserver with Apache License 2.0 | 6 votes |
@Override public void execute(ChannelHandlerContext ctx, MessageEvent e) throws Exception { VelocityBuilder velocityBuilder = new VelocityBuilder(); Map<String,Object> model = new HashMap<String, Object>(); model.put("streams", HlsLiveStreamMagr.INSTANCE().getAllLiveStream()); String htmlText = velocityBuilder.generate("streams.vm", "UTF8", model); HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); response.headers().add(HttpHeaders.Names.CONTENT_LENGTH, htmlText.length()); response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/html; charset=UTF-8"); ChannelBuffer buffer = ChannelBuffers.copiedBuffer(htmlText, Charset.defaultCharset());// CharsetUtil.UTF_8); response.setContent(buffer); ChannelFuture channelFuture = ctx.getChannel().write(response); if (channelFuture.isSuccess()) { channelFuture.getChannel().close(); } }
Example 7
Source File: ShuffleHandler.java From hadoop with Apache License 2.0 | 5 votes |
protected void sendError(ChannelHandlerContext ctx, String message, HttpResponseStatus status) { HttpResponse response = new DefaultHttpResponse(HTTP_1_1, status); response.setHeader(CONTENT_TYPE, "text/plain; charset=UTF-8"); // Put shuffle version into http header response.setHeader(ShuffleHeader.HTTP_HEADER_NAME, ShuffleHeader.DEFAULT_HTTP_HEADER_NAME); response.setHeader(ShuffleHeader.HTTP_HEADER_VERSION, ShuffleHeader.DEFAULT_HTTP_HEADER_VERSION); response.setContent( ChannelBuffers.copiedBuffer(message, CharsetUtil.UTF_8)); // Close the connection as soon as the error message is sent. ctx.getChannel().write(response).addListener(ChannelFutureListener.CLOSE); }
Example 8
Source File: TestDelegationTokenRemoteFetcher.java From hadoop with Apache License 2.0 | 5 votes |
@Override public void handle(Channel channel, Token<DelegationTokenIdentifier> token, String serviceUrl) throws IOException { Assert.assertEquals(testToken, token); byte[] bytes = EXP_DATE.getBytes(); ChannelBuffer cbuffer = ChannelBuffers.buffer(bytes.length); cbuffer.writeBytes(bytes); HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK); response.setHeader(HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(bytes.length)); response.setContent(cbuffer); channel.write(response).addListener(ChannelFutureListener.CLOSE); }
Example 9
Source File: ShuffleHandler.java From big-c with Apache License 2.0 | 5 votes |
protected void sendError(ChannelHandlerContext ctx, String message, HttpResponseStatus status) { HttpResponse response = new DefaultHttpResponse(HTTP_1_1, status); response.setHeader(CONTENT_TYPE, "text/plain; charset=UTF-8"); // Put shuffle version into http header response.setHeader(ShuffleHeader.HTTP_HEADER_NAME, ShuffleHeader.DEFAULT_HTTP_HEADER_NAME); response.setHeader(ShuffleHeader.HTTP_HEADER_VERSION, ShuffleHeader.DEFAULT_HTTP_HEADER_VERSION); response.setContent( ChannelBuffers.copiedBuffer(message, CharsetUtil.UTF_8)); // Close the connection as soon as the error message is sent. ctx.getChannel().write(response).addListener(ChannelFutureListener.CLOSE); }
Example 10
Source File: TestDelegationTokenRemoteFetcher.java From big-c with Apache License 2.0 | 5 votes |
@Override public void handle(Channel channel, Token<DelegationTokenIdentifier> token, String serviceUrl) throws IOException { Assert.assertEquals(testToken, token); byte[] bytes = EXP_DATE.getBytes(); ChannelBuffer cbuffer = ChannelBuffers.buffer(bytes.length); cbuffer.writeBytes(bytes); HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK); response.setHeader(HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(bytes.length)); response.setContent(cbuffer); channel.write(response).addListener(ChannelFutureListener.CLOSE); }
Example 11
Source File: RaopAudioHandler.java From Android-Airplay-Server with MIT License | 5 votes |
/** * Handle GET_PARAMETER request. Currently only {@code volume} is supported */ public synchronized void getParameterReceived(final ChannelHandlerContext ctx, final HttpRequest req) throws ProtocolException { final StringBuilder body = new StringBuilder(); if (audioOutputQueue != null) { /* Report output gain */ body.append("volume: "); body.append(audioOutputQueue.getRequestedVolume()); body.append("\r\n"); } final HttpResponse response = new DefaultHttpResponse(RtspVersions.RTSP_1_0, RtspResponseStatuses.OK); response.setContent(ChannelBuffers.wrappedBuffer(body.toString().getBytes(Charset.forName("ASCII")))); ctx.getChannel().write(response); }
Example 12
Source File: HttpDataServerHandler.java From incubator-tajo with Apache License 2.0 | 5 votes |
private void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) { HttpResponse response = new DefaultHttpResponse(HTTP_1_1, status); response.setHeader(CONTENT_TYPE, "text/plain; charset=UTF-8"); response.setContent(ChannelBuffers.copiedBuffer( "Failure: " + status.toString() + "\r\n", CharsetUtil.UTF_8)); // Close the connection as soon as the error message is sent. ctx.getChannel().write(response).addListener(ChannelFutureListener.CLOSE); }
Example 13
Source File: HttpServerHandler.java From glowroot with Apache License 2.0 | 5 votes |
private void writeResponse(MessageEvent e) throws Exception { HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); response.setContent(ChannelBuffers.copiedBuffer("Hello world", CharsetUtil.UTF_8)); addHeader(response, HttpHeaders.Names.CONTENT_TYPE, "text/plain; charset=UTF-8"); ChannelFuture future = e.getChannel().write(response); future.addListener(ChannelFutureListener.CLOSE); }
Example 14
Source File: ShuffleHandler.java From RDFS with Apache License 2.0 | 5 votes |
private void sendError(ChannelHandlerContext ctx, String message, HttpResponseStatus status) { HttpResponse response = new DefaultHttpResponse(HTTP_1_1, status); response.setHeader(CONTENT_TYPE, "text/plain; charset=UTF-8"); response.setContent( ChannelBuffers.copiedBuffer(message, CharsetUtil.UTF_8)); // Close the connection as soon as the error message is sent. ctx.getChannel().write(response).addListener(ChannelFutureListener.CLOSE); }
Example 15
Source File: WebSocketChannelHandler.java From usergrid with Apache License 2.0 | 5 votes |
private void sendHttpResponse( ChannelHandlerContext ctx, HttpRequest req, HttpResponse res ) { // Generate an error page if response status code is not OK (200). if ( res.getStatus().getCode() != 200 ) { res.setContent( ChannelBuffers.copiedBuffer( res.getStatus().toString(), CharsetUtil.UTF_8 ) ); setContentLength( res, res.getContent().readableBytes() ); } // Send the response and close the connection if necessary. ChannelFuture f = ctx.getChannel().write( res ); if ( !isKeepAlive( req ) || ( res.getStatus().getCode() != 200 ) ) { f.addListener( ChannelFutureListener.CLOSE ); } }
Example 16
Source File: ShuffleHandler.java From tez with Apache License 2.0 | 5 votes |
protected void sendError(ChannelHandlerContext ctx, String message, HttpResponseStatus status) { HttpResponse response = new DefaultHttpResponse(HTTP_1_1, status); response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8"); // Put shuffle version into http header response.headers().set(ShuffleHeader.HTTP_HEADER_NAME, ShuffleHeader.DEFAULT_HTTP_HEADER_NAME); response.headers().set(ShuffleHeader.HTTP_HEADER_VERSION, ShuffleHeader.DEFAULT_HTTP_HEADER_VERSION); response.setContent( ChannelBuffers.copiedBuffer(message, CharsetUtil.UTF_8)); // Close the connection as soon as the error message is sent. ctx.getChannel().write(response).addListener(ChannelFutureListener.CLOSE); }
Example 17
Source File: ShuffleHandler.java From tez with Apache License 2.0 | 5 votes |
protected void sendError(ChannelHandlerContext ctx, String message, HttpResponseStatus status) { HttpResponse response = new DefaultHttpResponse(HTTP_1_1, status); response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8"); // Put shuffle version into http header response.headers().set(ShuffleHeader.HTTP_HEADER_NAME, ShuffleHeader.DEFAULT_HTTP_HEADER_NAME); response.headers().set(ShuffleHeader.HTTP_HEADER_VERSION, ShuffleHeader.DEFAULT_HTTP_HEADER_VERSION); response.setContent( ChannelBuffers.copiedBuffer(message, CharsetUtil.UTF_8)); // Close the connection as soon as the error message is sent. ctx.getChannel().write(response).addListener(ChannelFutureListener.CLOSE); }
Example 18
Source File: NettyHttpChannel.java From Elasticsearch with Apache License 2.0 | 4 votes |
@Override public void doSendResponse(RestResponse response) { // if the response object was created upstream, then use it; // otherwise, create a new one HttpResponse resp = newResponse(); resp.setStatus(getStatus(response.status())); CorsHandler.setCorsResponseHeaders(nettyRequest, resp, transport.getCorsConfig()); String opaque = nettyRequest.headers().get("X-Opaque-Id"); if (opaque != null) { resp.headers().add("X-Opaque-Id", opaque); } // Add all custom headers Map<String, List<String>> customHeaders = response.getHeaders(); if (customHeaders != null) { for (Map.Entry<String, List<String>> headerEntry : customHeaders.entrySet()) { for (String headerValue : headerEntry.getValue()) { resp.headers().add(headerEntry.getKey(), headerValue); } } } BytesReference content = response.content(); ChannelBuffer buffer; boolean addedReleaseListener = false; try { buffer = content.toChannelBuffer(); resp.setContent(buffer); // If our response doesn't specify a content-type header, set one if (!resp.headers().contains(HttpHeaders.Names.CONTENT_TYPE)) { resp.headers().add(HttpHeaders.Names.CONTENT_TYPE, response.contentType()); } // If our response has no content-length, calculate and set one if (!resp.headers().contains(HttpHeaders.Names.CONTENT_LENGTH)) { resp.headers().add(HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(buffer.readableBytes())); } if (transport.resetCookies) { String cookieString = nettyRequest.headers().get(HttpHeaders.Names.COOKIE); if (cookieString != null) { CookieDecoder cookieDecoder = new CookieDecoder(); Set<Cookie> cookies = cookieDecoder.decode(cookieString); if (!cookies.isEmpty()) { // Reset the cookies if necessary. CookieEncoder cookieEncoder = new CookieEncoder(true); for (Cookie cookie : cookies) { cookieEncoder.addCookie(cookie); } resp.headers().add(HttpHeaders.Names.SET_COOKIE, cookieEncoder.encode()); } } } ChannelFuture future; if (orderedUpstreamMessageEvent != null) { OrderedDownstreamChannelEvent downstreamChannelEvent = new OrderedDownstreamChannelEvent(orderedUpstreamMessageEvent, 0, true, resp); future = downstreamChannelEvent.getFuture(); channel.getPipeline().sendDownstream(downstreamChannelEvent); } else { future = channel.write(resp); } if (content instanceof Releasable) { future.addListener(new ReleaseChannelFutureListener((Releasable) content)); addedReleaseListener = true; } if (isCloseConnection()) { future.addListener(ChannelFutureListener.CLOSE); } } finally { if (!addedReleaseListener && content instanceof Releasable) { ((Releasable) content).close(); } } }
Example 19
Source File: HttpServerRequestHandler.java From feeyo-hlsserver with Apache License 2.0 | 4 votes |
private HttpResponse buildDefaultResponse(String msg, HttpResponseStatus status){ HttpResponse errorResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, status); errorResponse.setContent( ChannelBuffers.copiedBuffer(msg, Charset.defaultCharset()) ); return errorResponse; }