package page.uk.co.mydisk;

import org.wikiwebserver.core.WareHouse;
import org.wikiwebserver.handler.http.HTTPException;
import org.wikiwebserver.handler.http.interfaces.HTTPResponder;

import page.config.SiteTemplatedPage;

import static org.wikiwebserver.html.HTMLHelper.*;

public class Options extends SiteTemplatedPage implements HTTPResponder {

    public void generate() throws HTTPException {
        
        this.addCSSLink("/templates/uk/co/mydisk/mydisk.css");
        
        setTitle("MyDisk.co.uk - MyDiskNode application options");
        

        append(h(1, "MyDiskNode application options"));
        append(p("Please ensure you have " + a("http://www.java.com", "Java Runtime 6 or above") + " to launch MyDiskNode."));        
        
        append(h(2, "Java Web Start application"));
        append(p("Simple, one click launch using Java Web Start."));
        String ws = WareHouse.getUrlPathForClass(JNLP.class);
        append(ul(new String[] { a(ws, "Web Start MyDiskNode") + "." }));
        
        append(h(2, "Standalone application"));
        append(p("Launch without Java Web Start as a standalone Java Application."));    
        String jar = "/MyDiskNode.jar";
        append(ol(new String[] { 
        		"Download " + a(jar, "MyDiskNode.jar") + ".",
        		"Double click MyDiskNode.jar to start the Java application.", }));
        
        
        append(h(2, "Command line application"));
        append(p("Start from the command line:"));         
        append(codeBox("java -jar MyDiskNode.jar"));
        
        append(h(2, "Command line options"));
        append(p("If no graphics mode is available, configuration settings can be specified on the command line."));
        append(p("For example:"));
        append(p("Share drive <b>C:\\</b> with the name <b>mikes_drive</b>."));
        append(codeBox("java -jar MyDiskNode.jar nodePath=C:\\ nodeName=mikes_drive"));
        append(p("Share Mike's user directory (on Linux or Unix) with the name <b>mikes_files</b> and protect with the password <b>secret</b>."));
        append(codeBox("java -jar MyDiskNode.jar nodePath=/home/mike nodeName=mikes_files nodePassword=secret"));        
        
        append("<a name='#policy'></a>");
        append(h(2, "Advanced security policy"));
        append(p(strong("Only required for security conscious and paranoid users!")));
        append(p("The following Java security policy can be installed to provide peace of mind and guarantee that MyDiskNode" +
        		" does not (and can not) perform any malicious actions."));      
        append(p("The security policy is enforced by the Java Virtual Machine and not by MyDiskNode, essentially running" +
        		" MyDiskNode in a protected, sandboxed environment."));     
        append(p("To apply the security policy, follow the steps below:"));
        
        append(p("1. Copy the text below and paste into a new file called policy.txt"));
        
        StringBuilder policy = new StringBuilder();
        policy.append("grant {" + LF);
        
        policy.append("    // Required environment variables for initialisation and data formatting" + LF);
        policy.append(getPolicyPermission("java.util.PropertyPermission", "java.version", "read"));
        policy.append(getPolicyPermission("java.util.PropertyPermission", "os.name", "read"));
        policy.append(getPolicyPermission("java.util.PropertyPermission", "os.version", "read"));
        policy.append(getPolicyPermission("java.util.PropertyPermission", "line.separator", "read"));
        policy.append(getPolicyPermission("java.util.PropertyPermission", "user.language", "read, write"));
        policy.append(getPolicyPermission("java.util.PropertyPermission", "user.home", "read"));
        policy.append(getPolicyPermission("java.util.PropertyPermission", "user.dir", "read"));
        policy.append(LF);
        
        policy.append("    // Access to the configuration file used by MyDiskNode to store settings" + LF);
        policy.append(getPolicyPermission("java.io.FilePermission", "c:\\\\Documents and Settings\\\\Mike\\\\.node_config.xml.tmp", "read, write, delete"));
        policy.append(getPolicyPermission("java.io.FilePermission", "c:\\\\Documents and Settings\\\\Mike\\\\.node_config.xml", "read, write, delete"));
        policy.append(LF);
        
        policy.append("    // The ability to load Java classes from WikiWebServer if they can not be found in the JAR" + LF);
        policy.append(getPolicyPermission("java.lang.RuntimePermission", "setContextClassLoader"));
        policy.append(LF);

        policy.append("    // The ability to connect to the WikiWebServer hosting the service" + LF);
        policy.append(getPolicyPermission("java.net.SocketPermission", "www.mydisk.co.uk:80", "connect, resolve"));
        policy.append(getPolicyPermission("java.net.SocketPermission", "www.wikiwebserver.org:80", "connect, resolve"));
        policy.append(LF);
        
        policy.append("    // The ability to access files you wish to share" + LF);
        policy.append(getPolicyPermission("java.io.FilePermission", "c:\\\\Documents and Settings\\\\Mike\\\\My Documents\\\\", "read, execute"));
        policy.append(getPolicyPermission("java.io.FilePermission", "c:\\\\Documents and Settings\\\\Mike\\\\My Documents\\\\Shared on MyDisk.co.uk\\\\", "read, execute"));       
        policy.append(LF);
        
        policy.append("    // The ability to add an icon to the system tray" + LF);
        policy.append(getPolicyPermission("java.awt.AWTPermission", "accessSystemTray"));
        policy.append(LF);
        
        policy.append("    // The following two permissions are required to use the file chooser" + LF);
        policy.append(getPolicyPermission("java.lang.RuntimePermission", "shutdownHooks"));
        policy.append(getPolicyPermission("java.lang.RuntimePermission", "modifyThread"));
        
        policy.append("};");
        
        append(codeBox(policy.toString()));
        
        append(p("2. Modify the policy file as required. You will need to change the FilePermissions to the paths you wish to share."));
        
        append(p("3. Launch MyDiskNode with the following command:"));
        append(codeBox("java -Djava.security.manager -Djava.security.policy==policy.txt -jar MyDiskNode.jar"));
        
        append(p("When MyDiskNode is running with a strict security policy, be aware some operations may fail." +
        		" For example, links to web pages, copying to clipboard and accessing folders and files not specified in the policy."));
    }

	private Object getPolicyPermission(String permObj, String value) {
		return "    permission " + permObj + " \"" + value + "\";" + LF;
	}
	
	private Object getPolicyPermission(String permObj, String key, String value) {
		return "    permission " + permObj + " \"" + key + "\", " + "\"" + value + "\";" + LF;
	}
}
