diff --git a/pom.xml b/pom.xml index e26b33017a9102f1f0a2ced8c81629fe7097513b..30a3fb3d5983ae5d05097e0a921f3727616c4d78 100644 --- a/pom.xml +++ b/pom.xml @@ -39,5 +39,11 @@ <artifactId>jcommander</artifactId> <version>1.30</version> </dependency> + + <dependency> + <groupId>org.apache.commons</groupId> + <artifactId>commons-lang3</artifactId> + <version>3.2.1</version> + </dependency> </dependencies> </project> \ No newline at end of file diff --git a/src/main/java/info/kwarc/theo/Browser.java b/src/main/java/info/kwarc/theo/Browser.java deleted file mode 100644 index 7a2326d2d71524b8f61e88db3a6ff5f41492409d..0000000000000000000000000000000000000000 --- a/src/main/java/info/kwarc/theo/Browser.java +++ /dev/null @@ -1,53 +0,0 @@ -package info.kwarc.theo; - -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; -import java.util.List; - -public class Browser { - final String script = "<HTML><BODY><script>var myForm = document.createElement(\"form\");\r\nmyForm.method = \"post\";\r\nmyForm.action = \"%s\";\r\n%s\r\ndocument.body.appendChild(myForm);\r\nmyForm.submit();\r\ndocument.body.removeChild(myForm);</script></BODY></HTML>"; - final String paramScript = "var myInput = document.createElement(\"input\");\r\nmyInput.setAttribute(\"name\", \"%s\");\r\nmyInput.setAttribute(\"value\", \"%s\");\r\nmyForm.appendChild(myInput);\n"; - - void execFirefox(String filePath) { - Process p; - try { - p = Runtime.getRuntime().exec("firefox -P theo --newinstance -new-window "+filePath); - p.waitFor(); - } catch (IOException e) { - e.printStackTrace(); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - - public Browser(String url, List<String> data) { - if (data.size() == 0) { - execFirefox(url); - } else { - StringBuilder paramSettings = new StringBuilder(); - for (String param : data) { - int eq = param.indexOf('='); - if (eq == -1) { - continue; - } - String key = param.substring(0, eq); - String val = param.substring(eq+1); - paramSettings.append(String.format(paramScript, key, val)); - } - final String allScript = String.format(script, url, paramSettings.toString()); - try { - File temp = File.createTempFile("temp",".html"); - FileWriter fileoutput = new FileWriter(temp); - BufferedWriter buffout = new BufferedWriter(fileoutput); - buffout.write(allScript); - buffout.close(); - execFirefox(temp.getAbsolutePath()); - temp.deleteOnExit(); - } catch (IOException e) { - e.printStackTrace(); - } - } - } -} diff --git a/src/main/java/info/kwarc/theo/TheoExec.java b/src/main/java/info/kwarc/theo/TheoExec.java index 70e0eb73368945b19c9f13dc53efdf62a54781a7..4296d1855a5951d38cb3d82ab85db7f3da4619a4 100644 --- a/src/main/java/info/kwarc/theo/TheoExec.java +++ b/src/main/java/info/kwarc/theo/TheoExec.java @@ -1,8 +1,14 @@ package info.kwarc.theo; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; import java.util.ArrayList; import java.util.List; +import org.apache.commons.lang3.StringEscapeUtils; + import com.beust.jcommander.JCommander; import com.beust.jcommander.Parameter; import com.beust.jcommander.ParameterException; @@ -24,6 +30,52 @@ public class TheoExec { @Parameter(names = "--data", required=false, description="POST data to be sent", variableArity=true) public List<String> data = new ArrayList<String>(); + final String script = "<HTML><BODY><script>var myForm = document.createElement(\"form\");\r\nmyForm.method = \"post\";\r\nmyForm.action = \"%s\";\r\n%s\r\ndocument.body.appendChild(myForm);\r\nmyForm.submit();\r\ndocument.body.removeChild(myForm);</script></BODY></HTML>"; + final String paramScript = "var myInput = document.createElement(\"input\");\r\nmyInput.setAttribute(\"name\", \"%s\");\r\nmyInput.setAttribute(\"value\", \"%s\");\r\nmyForm.appendChild(myInput);\n"; + + void execFirefox(String filePath) { + Process p; + try { + String cmd = "firefox -P theo --newinstance -new-window "+filePath+" -width "+width+" -height "+height; + System.out.println(cmd); + p = Runtime.getRuntime().exec(cmd); + p.waitFor(); + } catch (IOException e) { + e.printStackTrace(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + public void process( ) { + if (data.size() == 0) { + execFirefox(url); + } else { + StringBuilder paramSettings = new StringBuilder(); + for (String param : data) { + int eq = param.indexOf('='); + if (eq == -1) { + continue; + } + String key = param.substring(0, eq); + String val = param.substring(eq+1); + val = StringEscapeUtils.escapeEcmaScript(val); + paramSettings.append(String.format(paramScript, key, val)); + } + final String allScript = String.format(script, url, paramSettings.toString()); + try { + File temp = File.createTempFile("temp",".html"); + FileWriter fileoutput = new FileWriter(temp); + BufferedWriter buffout = new BufferedWriter(fileoutput); + buffout.write(allScript); + buffout.close(); + execFirefox(temp.getAbsolutePath()); + temp.deleteOnExit(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + public void run(String []args) { JCommander cmd = new JCommander(this); try { @@ -32,12 +84,14 @@ public class TheoExec { cmd.usage(); return; } - - new Browser(url, data); + + process(); } public static void main(String[] args){ //new TheoExec().run(new String[] {"--title", "hello", "--url", "http://localhost:8080/sider-nnexus/app/link", "--data", "forward_destination=123", "forward_correlation=123", "body=function"}); + + //new TheoExec().run(new String[] {"--url" , "http://mathhub.info:8983/defindexer/app/search?forward_destination=/queue/sublime_client_f0ae6362-42c8-4faf-bd1b-83d53c2f8644&forward_correlation=69401.0"}); new TheoExec().run(args); } }