package io.viper.net.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);
    }
}

@@ -23,16 +23,50 @@
 erver;%0A%0A
+%0Aimport java.nio.charset.Charset;%0A
 import o
@@ -503,42 +503,8 @@
 s;%0A%0A
-import java.nio.charset.Charset;%0A%0A
 impo
@@ -652,16 +652,17 @@
 P_1_1;%0A%0A
+%0A
 public c
@@ -724,21 +724,19 @@
 mHandler
- 
+%0A
 %7B%0A
-  
   privat
@@ -750,18 +750,16 @@
 g text;%0A
-  
   privat
@@ -812,18 +812,16 @@
 us.OK;%0A%0A
-  
   public
@@ -855,23 +855,21 @@
 ng text)
+%0A 
  %7B%0A
-    
     this
@@ -882,25 +882,21 @@
 = text;%0A
-  
   %7D%0A%0A
-  
   public
@@ -949,39 +949,37 @@
 seStatus status)
+%0A 
  %7B%0A
-    
     this(text);%0A
@@ -970,36 +970,32 @@
     this(text);%0A
-    
     this.status 
@@ -1004,25 +1004,21 @@
 status;%0A
-  
   %7D%0A%0A
-  
   public
@@ -1064,27 +1064,25 @@
  status)
- %7B
 %0A  
- 
+%7B%0A
     
- 
 this(tex
@@ -1085,20 +1085,16 @@
 (text);%0A
-    
     this
@@ -1143,17 +1143,13 @@
 s);%0A
-  
   %7D%0A%0A
-  
   @O
@@ -1158,18 +1158,16 @@
 rride%0A  
- 
- 
 public v
@@ -1228,16 +1228,22 @@
 Event e)
+%0A     
  throws 
@@ -1251,23 +1251,21 @@
 xception
+%0A 
  %7B%0A
-    
     Http
@@ -1327,28 +1327,24 @@
 tatus);%0A    
- 
-   
 response.set
@@ -1398,20 +1398,16 @@
 %22);%0A    
- 
-   
 response
@@ -1480,20 +1480,16 @@
 -8%22)));%0A
-    
     e.ge
@@ -1557,16 +1557,14 @@
 CLOSE);%0A
-  
   %7D%0A%7D%0A

clean up