use of org.jboss.netty.handler.codec.http.HttpResponse in project camel by apache.
the class HttpServerMultiplexChannelHandler method messageReceived.
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent messageEvent) throws Exception {
// store request, as this channel handler is created per pipeline
HttpRequest request = (HttpRequest) messageEvent.getMessage();
LOG.debug("Message received: {}", request);
HttpServerChannelHandler handler = getHandler(request);
if (handler != null) {
// store handler as attachment
ctx.setAttachment(handler);
handler.messageReceived(ctx, messageEvent);
} else {
// this resource is not found, so send empty response back
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, NOT_FOUND);
response.headers().set(Exchange.CONTENT_TYPE, "text/plain");
response.headers().set(Exchange.CONTENT_LENGTH, 0);
response.setContent(ChannelBuffers.copiedBuffer(new byte[] {}));
messageEvent.getChannel().write(response).syncUninterruptibly();
// close the channel after send error message
messageEvent.getChannel().close();
}
}
use of org.jboss.netty.handler.codec.http.HttpResponse in project camel by apache.
the class NettyHttpAccessHttpRequestAndResponseBeanTest method myTransformer.
/**
* We can use both a netty http request and response type for transformation
*/
public static HttpResponse myTransformer(HttpRequest request) {
String in = request.getContent().toString(Charset.forName("UTF-8"));
String reply = "Bye " + in;
HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
response.setContent(ChannelBuffers.copiedBuffer(reply.getBytes()));
response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, reply.length());
return response;
}
use of org.jboss.netty.handler.codec.http.HttpResponse in project cdap by caskdata.
the class HttpStatusRequestHandler method messageReceived.
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent event) throws Exception {
Object msg = event.getMessage();
if (msg instanceof HttpRequest) {
HttpRequest request = (HttpRequest) msg;
// be served by the router itself without it talking to any downstream services
if (request.getUri().equals(Constants.EndPoints.STATUS)) {
String statusString = Constants.Monitor.STATUS_OK;
ChannelBuffer responseContent = ChannelBuffers.wrappedBuffer(Charsets.UTF_8.encode(statusString));
HttpResponse httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
httpResponse.setHeader(HttpHeaders.Names.CONTENT_TYPE, "text/plain");
httpResponse.setHeader(HttpHeaders.Names.CONTENT_LENGTH, responseContent.readableBytes());
httpResponse.setContent(responseContent);
ChannelFuture writeFuture = Channels.future(event.getChannel());
Channels.write(ctx, writeFuture, httpResponse);
writeFuture.addListener(ChannelFutureListener.CLOSE);
return;
}
}
super.messageReceived(ctx, event);
}
use of org.jboss.netty.handler.codec.http.HttpResponse in project cdap by caskdata.
the class IdleEventProcessor method messageReceived.
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
Object message = e.getMessage();
if (message instanceof HttpResponse) {
HttpResponse response = (HttpResponse) message;
if (!response.isChunked()) {
requestInProgress = false;
}
} else if (message instanceof HttpChunk) {
HttpChunk chunk = (HttpChunk) message;
if (chunk.isLast()) {
requestInProgress = false;
}
}
ctx.sendUpstream(e);
}
use of org.jboss.netty.handler.codec.http.HttpResponse in project NabAlive by jcheype.
the class Response method write.
public void write(ChannelBuffer buffer) {
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
response.setHeader(HttpHeaders.Names.SERVER, "nuvoos server");
setContentLength(response, buffer.readableBytes());
response.setContent(buffer);
if (isKeepAlive(request)) {
response.setHeader(HttpHeaders.Names.CONNECTION, "Keep-Alive");
}
final ChannelFuture future = ctx.getChannel().write(response);
if (!isKeepAlive(request)) {
future.addListener(ChannelFutureListener.CLOSE);
}
}
Aggregations