package indexnode;

import java.io.IOException;
import java.net.URL;
import java.util.Map;

import common.httpserver.HttpExchange;
import common.httpserver.HttpHandler;
import common.HttpUtil;
import common.Logger;
import common.Util;

public class IndexBrowser implements HttpHandler {
	private Filesystem fs;
	
	public IndexBrowser(Filesystem fs) {
		this.fs = fs;
	}
	
	public void handle(HttpExchange exchange) throws IOException {
		try {
			//Thread.sleep(1000);//simulate internet latency
			
			FilesystemEntry requestedEntry = null;
			//A message to give to the user if they do something stupid or to tell them the status
			IndexTemplate template = new IndexTemplate(exchange);
			template.setTitle("Browse the filesystem...");
			
			String path = HttpUtil.getPathAfterContext(exchange);
			requestedEntry = fs.lookupFromPath(path);
			if (path.length() > 0 && (requestedEntry == null)) {
				//Then they supplied an invalid path:
				HttpUtil.simple404(exchange);
				return;
			}
			
			//Logger.log("Serving browse request: "+path);
			
			if (requestedEntry.isDirectory()) {
				template.setFileHeader(requestedEntry);
				Map<String, ? extends FilesystemEntry> children = requestedEntry.getChildren();
				synchronized (children) {
					template.generateFilelist(children.values(), false, false);
				}
			} else {
				//This interface to the indexnode is really for browsing structure. (directories)
				//However, it will also understand paths to files
				// in the case of a file path, redirect to /download/{hash}
				
				HttpUtil.redirectToURL(exchange, new URL(HttpUtil.getClientURLToServerRoot(exchange)+"download/"+Util.bytesToHexString(requestedEntry.getHash().get())));
				return;
			}
			
			template.sendToClient(exchange);
		} catch (IOException e) {
			if (e.getMessage().equals("Broken pipe")) Logger.warn("Ungrateful bastard client ('"+exchange.getRequestHeaders().getFirst("fs2-alias")+"') broke the pipe.");
		} catch (Exception e) {
			Logger.severe("Exception handling a browse request: " +e.toString());
			Logger.log(e);
			//If we're here then things have gone so wrong we shouldn't bother with nice output.
			HttpUtil.simpleResponse(exchange, "Your request couldn't be handled due to an internal exception.", 500);
		}
	}
}

@@ -280,16 +280,18 @@
 ndler %7B%0A
+%09%0A
 %09private
@@ -443,307 +443,8 @@
 y %7B%0A
-%09%09%09//Thread.sleep(1000);//simulate internet latency%0A%09%09%09%0A%09%09%09FilesystemEntry requestedEntry = null;%0A%09%09%09//A message to give to the user if they do something stupid or to tell them the status%0A%09%09%09IndexTemplate template = new IndexTemplate(exchange);%0A%09%09%09template.setTitle(%22Browse the filesystem...%22);%0A%09%09%09%0A
 %09%09%09S
@@ -499,16 +499,32 @@
 ge);%0A%09%09%09
+FilesystemEntry 
 requeste
@@ -557,16 +557,20 @@
 (path);%0A
+%09%09%09%0A
 %09%09%09if (p
@@ -589,17 +589,16 @@
  %3E 0 && 
-(
 requeste
@@ -612,17 +612,16 @@
 == null)
-)
  %7B%0A%09%09%09%09/
@@ -621,16 +621,17 @@
 %7B%0A%09%09%09%09//
+ 
 Then the
@@ -717,69 +717,16 @@
 %09%09%09%0A
-%09%09%09//Logger.log(%22Serving browse request: %22+path);%0A%09%09%09%0A
 %09%09%09if (
+!
 requ
@@ -757,255 +757,15 @@
 ) %7B%0A
-%09%09%09%09template.setFileHeader(requestedEntry);%0A%09%09%09%09Map%3CString, ? extends FilesystemEntry%3E children = requestedEntry.getChildren();%0A%09%09%09%09synchronized (children) %7B%0A%09%09%09%09%09template.generateFilelist(children.values(), false, false);%0A%09%09%09%09%7D%0A%09%09%09%7D else %7B%0A
 %09%09%09%09//
+ 
 This
@@ -846,16 +846,17 @@
 )%0A%09%09%09%09//
+ 
 However,
@@ -894,24 +894,25 @@
 to files
+.
 %0A%09%09%09%09// 
 in the c
@@ -903,17 +903,17 @@
 %0A%09%09%09%09// 
-i
+I
 n the ca
@@ -964,13 +964,8 @@
 sh%7D%0A
-%09%09%09%09%0A
 %09%09%09%09
@@ -1139,16 +1139,355 @@
 %09%09%7D%0A%09%09%09%0A
+%09%09%09IndexTemplate template = new IndexTemplate(exchange);%0A%09%09%09template.setTitle(%22Browse the filesystem...%22);%0A%09%09%09template.setFileHeader(requestedEntry);%0A%09%09%09%0A%09%09%09Map%3CString, ? extends FilesystemEntry%3E children = requestedEntry.getChildren();%0A%09%09%09synchronized (children) %7B%0A%09%09%09%09template.generateFilelist(children.values(), false, false);%0A%09%09%09%7D%0A%09%09%09%0A
 %09%09%09templ
@@ -1514,16 +1514,20 @@
 hange);%0A
+%09%09%09%0A
 %09%09%7D catc
@@ -1708,16 +1708,20 @@
 ipe.%22);%0A
+%09%09%09%0A
 %09%09%7D catc
@@ -1800,20 +1800,10 @@
  %22 +
+ 
 e
-.toString()
 );%0A%09
@@ -1824,16 +1824,17 @@
 );%0A%09%09%09//
+ 
 If we're
@@ -2024,10 +2024,12 @@
 %0A%09%09%7D%0A%09%7D%0A
+%09%0A
 %7D%0A

Clean up code before making further changes