package com.hamishrickerby.http_server;

import java.nio.channels.AsynchronousServerSocketChannel;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;

/**
 * Created by rickerbh on 15/08/2016.
 */
public class HTTPCompletionHandler implements CompletionHandler<AsynchronousSocketChannel, Void> {
    ResponseFactory responseFactory;
    AsynchronousServerSocketChannel listeningChannel;

    public HTTPCompletionHandler(String rootDirectory, AsynchronousServerSocketChannel listeningChannel) {
        responseFactory = new ResponseFactory(rootDirectory);
        this.listeningChannel = listeningChannel;
    }

    @Override
    public void completed(AsynchronousSocketChannel ch, Void attachment) {
        listeningChannel.accept(null, this);
        String requestText = extractRequestText(ch);

        Request request = new Request(requestText);
        Response response = responseFactory.makeResponse(request);

        sendResponse(ch, response);

        closeChannel(ch);
    }

    @Override
    public void failed(Throwable exc, Void attachment) {

    }

    private String extractRequestText(AsynchronousSocketChannel ch) {
        ByteReader reader = new AsynchronousSocketChannelReader(ch);
        return new String(reader.read());
    }

    private void sendResponse(AsynchronousSocketChannel ch, Response response) {
        ByteWriter writer = new AsynchronousSocketChannelWriter(ch);
        writer.write(response.getBytes());
    }

    private void closeChannel(AsynchronousSocketChannel ch) {
        if (ch.isOpen()) {
            try {
                ch.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

@@ -338,45 +338,8 @@
 %3E %7B%0A
-    ResponseFactory responseFactory;%0A
     
@@ -387,16 +387,41 @@
 Channel;
+%0A    HTTPHandler handler;
 %0A%0A    pu
@@ -533,45 +533,33 @@
     
-responseFactory = new ResponseFactory
+handler = new HTTPHandler
 (roo
@@ -729,406 +729,74 @@
     
-listeningChannel.accept(null, this);%0A        String requestText = extractRequestText(ch);%0A%0A        Request request = new Request(requestText);%0A        Response response = responseFactory.makeResponse(request);%0A%0A        sendResponse(ch, response);%0A%0A        closeChannel(ch);%0A    %7D%0A%0A    @Override%0A    public void failed(Throwable exc, Void attachment) %7B%0A%0A    %7D%0A%0A    private String extractRequestText(
+setupHandlerForNextConnection();%0A%0A        ByteReader reader = new 
 Asyn
@@ -808,38 +808,43 @@
 ousSocketChannel
- ch) %7B
+Reader(ch);
 %0A        ByteRea
@@ -836,35 +836,35 @@
         Byte
-Reader read
+Writer writ
 er = new Asy
@@ -877,36 +877,36 @@
 ousSocketChannel
-Read
+Writ
 er(ch);%0A        
@@ -909,241 +909,245 @@
     
-return new String(reader.read());%0A    %7D%0A%0A    private void sendResponse(AsynchronousSocketChannel ch, Response response) %7B%0A        ByteWriter writer = new AsynchronousSocketChannelWriter(ch
+handler.run(reader, writer);%0A%0A        closeChannel(ch);%0A    %7D%0A%0A    private void setupHandlerForNextConnection() %7B%0A        listeningChannel.accept(null, this
 );%0A    
+%7D%0A%0A
     
-writer.write(response.getBytes());
+@Override%0A    public void failed(Throwable exc, Void attachment) %7B%0A
 %0A   

Replace CompletionHandler internals with Handler