Commit db71a9b9 authored by Constantin Jucovschi's avatar Constantin Jucovschi
Browse files

multiline data works now

parent ffd4ae98
......@@ -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
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();
}
}
}
}
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);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment