package net.lshift.diffa.auth;

import org.jboss.netty.channel.*;
import org.jboss.netty.handler.codec.http.DefaultHttpResponse;
import org.jboss.netty.handler.codec.http.HttpRequest;
import org.jboss.netty.handler.codec.http.HttpResponse;
import org.jboss.netty.handler.codec.http.HttpResponseEncoder;

import static org.jboss.netty.handler.codec.http.HttpResponseStatus.FORBIDDEN;
import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.*;
import static org.jboss.netty.handler.codec.http.HttpVersion.HTTP_1_1;

public class AuthenticatingTranscoder extends SimpleChannelUpstreamHandler {

  @Override
  public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {

    HttpRequest request = (HttpRequest) e.getMessage();
    String space = extractSpace(request);

    if (isAuthenticated(space)) {

      rewriteUri(request, System.currentTimeMillis());
      ctx.sendUpstream(e);

    }
    else {

      ctx.getPipeline().addLast("encoder", new HttpResponseEncoder());
      HttpResponse response = new DefaultHttpResponse(HTTP_1_1, FORBIDDEN);
      response.setHeader(CONTENT_LENGTH, 0);
      ChannelFuture future = ctx.getChannel().write(response);
      future.addListener(ChannelFutureListener.CLOSE);

    }

  }

  private void rewriteUri(HttpRequest request, Long endpoint) {
    String url = String.format("/%s/interview", endpoint);
    request.setUri(url);
  }

  private String extractSpace(HttpRequest request) {
    return "space";
  }

  private boolean isAuthenticated(String space) {
    return true;
  }
}

@@ -105,201 +105,66 @@
 ttp.
-DefaultHttpResponse;%0Aimport org.jboss.netty.handler.codec.http.HttpRequest;%0Aimport org.jboss.netty.handler.codec.http.HttpResponse;%0Aimport org.jboss.netty.handler.codec.http.HttpResponseEncoder
+*;%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory
 ;%0A%0Ai
@@ -461,16 +461,96 @@
 dler %7B%0A%0A
+  static Logger log = LoggerFactory.getLogger(AuthenticatingTranscoder.class);%0A%0A
   @Overr
@@ -642,24 +642,76 @@
 xception %7B%0A%0A
+    if (e.getMessage() instanceof HttpRequest) %7B%0A%0A  
     HttpRequ
@@ -754,16 +754,18 @@
 sage();%0A
+  
     Stri
@@ -799,24 +799,26 @@
 uest);%0A%0A    
+  
 if (isAuthen
@@ -832,24 +832,26 @@
 (space)) %7B%0A%0A
+  
       rewrit
@@ -895,24 +895,26 @@
 s());%0A      
+ 
+ 
 ctx.sendUpst
@@ -927,18 +927,22 @@
 );%0A%0A    
-%7D%0A
+  %7D%0A  
     else
@@ -947,24 +947,26 @@
 se %7B%0A%0A      
+  
 ctx.getPipel
@@ -1014,24 +1014,26 @@
 Encoder());%0A
+  
       HttpRe
@@ -1102,16 +1102,18 @@
 ;%0A      
+ 
+ 
 response
@@ -1143,16 +1143,18 @@
 TH, 0);%0A
+  
       Ch
@@ -1208,16 +1208,18 @@
 ponse);%0A
+  
       fu
@@ -1274,90 +1274,629 @@
     
+  
 %7D%0A%0A  
-%7D%0A%0A  private void rewriteUri(HttpRequest request, Long endpoint) %7B%0A    String
+  %7D%0A    else if (e.getMessage() instanceof HttpChunk) %7B%0A%0A      // TODO This is very dodgy - basically we're assuming that the input stream was pre-validated .... fix this%0A%0A      ctx.sendUpstream(e);%0A%0A    %7D%0A    else %7B%0A      log.error(%22Forgot to implement some code: %22 + e.getMessage());%0A    %7D%0A%0A%0A%0A  %7D%0A%0A  private void rewriteUri(HttpRequest request, Long endpoint) %7B%0A    String url;%0A    String original = request.getUri();%0A    int index = original.indexOf(%22?%22);%0A%0A    if (index %3E 0) %7B%0A      String query = original.substring(index);%0A      url = String.format(%22/%25s/interview%25s%22, endpoint, query);%0A    %7D%0A    else %7B%0A     
  url
@@ -1940,16 +1940,23 @@
 point);%0A
+    %7D%0A%0A
     requ

Made the query re-writing a bit less dodgy