All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.viper.core.server.StatusResponseHandler Maven / Gradle / Ivy

package io.viper.core.server;


import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.ChannelFutureListener;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
import org.jboss.netty.handler.codec.http.DefaultHttpResponse;
import org.jboss.netty.handler.codec.http.HttpResponse;
import org.jboss.netty.handler.codec.http.HttpResponseStatus;

import java.nio.charset.Charset;

import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.CONTENT_TYPE;
import static org.jboss.netty.handler.codec.http.HttpVersion.HTTP_1_1;


public class StatusResponseHandler extends SimpleChannelUpstreamHandler
{
  private String text;
  private HttpResponseStatus status = HttpResponseStatus.OK;

  public StatusResponseHandler(String text)
  {
    this.text = text;
  }

  public StatusResponseHandler(String text, HttpResponseStatus status)
  {
    this(text);
    this.status = status;
  }

  public StatusResponseHandler(String text, int status)
  {
    this(text);
    this.status = HttpResponseStatus.valueOf(status);
  }

  @Override
  public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
      throws Exception
  {
    HttpResponse response = new DefaultHttpResponse(HTTP_1_1, status);
    response.setHeader(CONTENT_TYPE, "text/plain; charset=UTF-8");
    response.setContent(ChannelBuffers.copiedBuffer(text, Charset.forName("UTF-8")));
    e.getChannel().write(response).addListener(ChannelFutureListener.CLOSE);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy