
import java.io.*;
import java.net.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;

public class customerManager extends Thread {

	private Socket customerManagerSocket;
	private BufferedReader out;
	private DataOutputStream input;
	private String method, filename, fileType, errorFile = "error.html";
	private long responseSize;
	private byte[] content;
	private File file;

	public customerManager(Socket clientSocket) {
		customerManagerSocket = clientSocket;
	}

	public void run() {
		try {
			// Receive and send data to the customer
			out = new BufferedReader(new InputStreamReader(customerManagerSocket.getInputStream()));
			input = new DataOutputStream(customerManagerSocket.getOutputStream());

			// Fragments the information of the header
			managementHeader();
			// Show the information of the request to the server
			while (out.ready()) {
				System.out.println(out.readLine());
			}
			System.out.println(" ");
			if (method.equals("GET")) {
				file = new File(filename);

				if (server.filesCache.containsKey(filename)) {
					managementResponse("HTTP/1.1 200 OK", false);
				} else {
					// Check if the file exists
					managementResponse((file.exists()) ? "HTTP/1.1 200 OK" : "HTTP/1.1 404 Not Found", true);
				}
			}
		} catch (Exception e) {
			System.out.println("Communication problems with the client");
		}

	}

	/**
	 * Function: Send the content of the header and the content of the file
	 * -Parameters: String code:Let us know if the file exists. Boolean readFile
	 * will help us to know if we have to read the file or not.
	 */
	private void managementResponse(String code, boolean readFile) throws Exception {
		// If you dont need to read the file
		if (!readFile) {
			fileType = server.filesCache.get(filename).getFileType();
			server.filesCache.get(filename).setAppearances(server.filesCache.get(filename).getAppearances() + 1);
			responseSize = server.filesCache.get(filename).getResponseSize();
			content = server.filesCache.get(filename).getContent();
		} else {
			readFiles((code.indexOf("200") > 0) ? filename : errorFile);
			file file = new file(responseSize, code, content);
			server.filesCache.put(filename, file);
		}

		// Sending data header
		input.writeBytes(code + "Content-Type: " + fileType + "\r\n" + "Content-Length: " + String.valueOf(responseSize)
				+ "\r\n" + "Connection: close\r\n" + "\r\n");
		// Sending data content
		input.write(content);
		input.close();
	}

	/**
	 * Function: Read files -Parameters: String name:filename.
	 */
	private void readFiles(String name) {
		Path path = Paths.get(name);
		try {
			fileType = Files.probeContentType(path);
			// If there have been problems with the file
			if (name.equals(errorFile))
				file = new File(name);
			// Content file
			content = Files.readAllBytes(path);
			// Length file
			responseSize = file.length();
		} catch (IOException e) {
			System.out.println("Problems reading file");
		}
	}

	/**
	 * Function: Manage the header of the request.
	 */
	private void managementHeader() {
		try {
			// Stores the header information of the request
			String informationRequest = out.readLine();
			StringTokenizer tokenizer = new StringTokenizer(informationRequest);
			method = tokenizer.nextToken();
			// Removed the first position
			filename = tokenizer.nextToken().replaceFirst("/", "");
			System.out.println(informationRequest);
		} catch (IOException e) {
			System.out.println("Problems receiving the request");
		}

	}

}
@@ -1,20 +1,21 @@
+%0A
 %0Aimport java.io.*;%0Ai
@@ -136,16 +136,50 @@
 til.*;%0A%0A
+/**%0A * Author: Carlos C%C3%A1mara%0A */%0A%0A
 public c
@@ -280,17 +280,19 @@
 dReader 
-o
+inp
 ut;%0A%09pri
@@ -313,19 +313,17 @@
 tStream 
-inp
+o
 ut;%0A%09pri
@@ -629,17 +629,19 @@
 omer%0A%09%09%09
-o
+inp
 ut = new
@@ -719,27 +719,25 @@
 eam()));%0A%09%09%09
-inp
+o
 ut = new Dat
@@ -928,17 +928,19 @@
 %09while (
-o
+inp
 ut.ready
@@ -968,17 +968,19 @@
 println(
-o
+inp
 ut.readL
@@ -1082,17 +1082,16 @@
 ename);%0A
-%0A
 %09%09%09%09if (
@@ -1357,32 +1357,35 @@
 ception e) %7B%0A%09%09%09
+// 
 System.out.print
@@ -1447,26 +1447,16 @@
 %09/**%0A%09 *
- Function:
  Send th
@@ -1515,32 +1515,40 @@
 %09 * 
--Parameters: String code
+%0A%09 * @param code%0A%09 *            
 :Let
@@ -1579,16 +1579,19 @@
 sts.
- Boolean
+%0A%09 * @param
  rea
@@ -1600,16 +1600,28 @@
 ile%0A%09 * 
+           :
 will hel
@@ -1772,16 +1772,17 @@
  you don
+%C2%B4
 t need t
@@ -2313,19 +2313,17 @@
 eader%0A%09%09
-inp
+o
 ut.write
@@ -2502,19 +2502,17 @@
 ntent%0A%09%09
-inp
+o
 ut.write
@@ -2524,19 +2524,17 @@
 ent);%0A%09%09
-inp
+o
 ut.close
@@ -2554,62 +2554,84 @@
 %09 * 
-Function: Read files -Parameters: String name:filename
+Read files%0A%09 * %0A%09 * @param name%0A%09 *            : Name of the file to be read
 .%0A%09 
@@ -3066,18 +3066,8 @@
 %0A%09 *
- Function:
  Man
@@ -3227,17 +3227,19 @@
 quest = 
-o
+inp
 ut.readL
@@ -3582,8 +3582,9 @@
 %7D%0A%0A%09%7D%0A%0A%7D
+%0A

Update customerManager.java