use of org.jboss.netty.handler.codec.http.DefaultHttpResponse in project load-balancer by RestComm.
the class HttpRequestHandler method writeResponse.
private void writeResponse(MessageEvent e, HttpResponseStatus status, String responseString) {
// Convert the response content to a ChannelBuffer.
ChannelBuffer buf = ChannelBuffers.copiedBuffer(responseString, Charset.forName("UTF-8"));
// Decide whether to close the connection or not.
boolean close = HttpHeaders.Values.CLOSE.equalsIgnoreCase(request.getHeader(HttpHeaders.Names.CONNECTION)) || request.getProtocolVersion().equals(HttpVersion.HTTP_1_0) && !HttpHeaders.Values.KEEP_ALIVE.equalsIgnoreCase(request.getHeader(HttpHeaders.Names.CONNECTION));
// Build the response object.
HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, status);
response.setContent(buf);
response.setHeader(HttpHeaders.Names.CONTENT_TYPE, "text/plain; charset=UTF-8");
if (!close) {
// There's no need to add 'Content-Length' header
// if this is the last response.
response.setHeader(HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(buf.readableBytes()));
}
String cookieString = request.getHeader(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);
}
response.addHeader(HttpHeaders.Names.SET_COOKIE, cookieEncoder.encode());
}
}
// Write the response.
ChannelFuture future = null;
if (status.equals(HttpResponseStatus.SERVICE_UNAVAILABLE) || status.equals(HttpResponseStatus.LOCKED))
future = e.getChannel().write(buf);
else
future = e.getChannel().write(response);
// Close the connection after the write operation is done if necessary.
if (close) {
future.addListener(ChannelFutureListener.CLOSE);
}
}
use of org.jboss.netty.handler.codec.http.DefaultHttpResponse in project load-balancer by RestComm.
the class HttpRequestHandler method sendRedirectResponse.
private void sendRedirectResponse(MessageEvent e, HttpRequest request) {
HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.FOUND);
String host = request.getHeader("Host");
host = host.replace(balancerRunner.balancerContext.lbConfig.getHttpConfiguration().getHttpPort().toString(), balancerRunner.balancerContext.lbConfig.getHttpConfiguration().getHttpsPort().toString());
response.setHeader("Location", "https://" + host + request.getUri());
ChannelFuture future = e.getChannel().write(response);
future.addListener(ChannelFutureListener.CLOSE);
}
use of org.jboss.netty.handler.codec.http.DefaultHttpResponse in project load-balancer by RestComm.
the class HttpRequestHandler method writeInfoResponse.
private void writeInfoResponse(MessageEvent e) {
GsonBuilder builder = new GsonBuilder();
Gson gson = builder.setPrettyPrinting().create();
JsonElement je = gson.toJsonTree(new NodesInfoObject(balancerRunner));
JsonObject jo = new JsonObject();
jo.add("Nodes info", je);
String output = jo.toString();
HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
ChannelBuffer buf = ChannelBuffers.copiedBuffer(output, Charset.forName("UTF-8"));
response.setContent(buf);
ChannelFuture future = e.getChannel().write(response);
future.addListener(ChannelFutureListener.CLOSE);
}
use of org.jboss.netty.handler.codec.http.DefaultHttpResponse in project traccar by tananaev.
the class Mta6ProtocolDecoder method sendContinue.
private void sendContinue(Channel channel) {
HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.CONTINUE);
channel.write(response);
}
use of org.jboss.netty.handler.codec.http.DefaultHttpResponse 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();
}
}
Aggregations