package com.hamishrickerby.http_server;

/**
 * Created by rickerbh on 17/08/2016.
 */
public class ResponseFactory {
    String rootPath;

    public ResponseFactory(String rootPath) {
        this.rootPath = rootPath;
    }

    public Response makeResponse(Request request) {
        Response response = null;
        if (isFileResponse(request)) {
            FileContentsResponse fcr = new FileContentsResponse(request, rootPath);
            response = fcr;
        } else if (isDirectoryResponse(request)) {
            DirectoryListingResponse dlr = new DirectoryListingResponse(request, rootPath);
            response = dlr;
        } else {
            response = new FourOhFourResponse(request);
        }
        return response;
    }

    private boolean isFileResponse(Request request) {
        FileServer server = new FileServer(rootPath);
        return server.fileExists(request.getPath());
    }

    private boolean isDirectoryResponse(Request request) {
        FileDirectoryServer server = new FileDirectoryServer(rootPath);
        return server.directoryExists(request.getPath());
    }

}

@@ -301,15 +301,8 @@
 onse
- = null
 ;%0A  
@@ -354,29 +354,17 @@
     
-FileContentsR
+r
 esponse 
 fcr 
@@ -363,12 +363,8 @@
 nse 
-fcr 
 = ne
@@ -410,36 +410,8 @@
 h);%0A
-            response = fcr;%0A
     
@@ -473,33 +473,17 @@
     
-DirectoryListingR
+r
 esponse 
 dlr 
@@ -482,12 +482,8 @@
 nse 
-dlr 
 = ne
@@ -533,36 +533,8 @@
 h);%0A
-            response = dlr;%0A
     

Drop some unnecessary code from Response