--- /dev/null
+package tim.prune.function.gpsies;\r
+\r
+import java.net.URLConnection;\r
+import java.net.URL;\r
+import java.io.IOException;\r
+import java.io.File;\r
+import java.io.InputStream;\r
+import java.util.Random;\r
+import java.io.OutputStream;\r
+import java.io.FileInputStream;\r
+\r
+/**\r
+ * Taken from Client HTTP Request class\r
+ * @author Vlad Patryshev\r
+ */\r
+public class FormPoster\r
+{\r
+ private URLConnection _connection;\r
+ private OutputStream _os = null;\r
+ private static final Random random = new Random();\r
+ private static final String boundary = "---------------------------"\r
+ + randomString() + randomString() + randomString();\r
+\r
+\r
+ protected void connect() throws IOException {\r
+ if (_os == null) _os = _connection.getOutputStream();\r
+ }\r
+\r
+ protected void write(char c) throws IOException {\r
+ connect();\r
+ _os.write(c);\r
+ }\r
+\r
+ protected void write(String s) throws IOException {\r
+ connect();\r
+ _os.write(s.getBytes());\r
+ }\r
+\r
+ protected void newline() throws IOException {\r
+ connect();\r
+ write("\r\n");\r
+ }\r
+\r
+ protected void writeln(String s) throws IOException {\r
+ connect();\r
+ write(s);\r
+ newline();\r
+ }\r
+\r
+ protected static String randomString() {\r
+ return Long.toString(random.nextLong(), 36);\r
+ }\r
+\r
+ private void boundary() throws IOException {\r
+ write("--");\r
+ write(boundary);\r
+ }\r
+\r
+ /**\r
+ * Creates a new multipart POST HTTP request on a freshly opened URLConnection\r
+ *\r
+ * @param inConnection an already open URL connection\r
+ * @throws IOException\r
+ */\r
+ public FormPoster(URLConnection inConnection) throws IOException\r
+ {\r
+ _connection = inConnection;\r
+ _connection.setDoOutput(true);\r
+ _connection.setRequestProperty("Content-Type",\r
+ "multipart/form-data; boundary=" + boundary);\r
+ }\r
+\r
+ /**\r
+ * Creates a new multipart POST HTTP request for a specified URL\r
+ *\r
+ * @param url the URL to send request to\r
+ * @throws IOException\r
+ */\r
+ public FormPoster(URL url) throws IOException {\r
+ this(url.openConnection());\r
+ }\r
+\r
+ /**\r
+ * Creates a new multipart POST HTTP request for a specified URL string\r
+ *\r
+ * @param urlString the string representation of the URL to send request to\r
+ * @throws IOException\r
+ */\r
+ public FormPoster(String urlString) throws IOException {\r
+ this(new URL(urlString));\r
+ }\r
+\r
+\r
+ private void writeName(String name) throws IOException\r
+ {\r
+ newline();\r
+ write("Content-Disposition: form-data; name=\"");\r
+ write(name);\r
+ write('"');\r
+ }\r
+\r
+ /**\r
+ * adds a string parameter to the request\r
+ * @param name parameter name\r
+ * @param value parameter value\r
+ * @throws IOException\r
+ */\r
+ public void setParameter(String name, String value) throws IOException\r
+ {\r
+ boundary();\r
+ writeName(name);\r
+ newline(); newline();\r
+ writeln(value);\r
+ }\r
+\r
+ private static void pipe(InputStream in, OutputStream out) throws IOException\r
+ {\r
+ byte[] buf = new byte[500000];\r
+ int nread;\r
+ int total = 0;\r
+ synchronized (in) {\r
+ while((nread = in.read(buf, 0, buf.length)) >= 0) {\r
+ out.write(buf, 0, nread);\r
+ total += nread;\r
+ }\r
+ }\r
+ out.flush();\r
+ buf = null;\r
+ }\r
+\r
+ /**\r
+ * adds a file parameter to the request\r
+ * @param name parameter name\r
+ * @param filename the name of the file\r
+ * @param is input stream to read the contents of the file from\r
+ * @throws IOException\r
+ */\r
+ public void setParameter(String name, String filename, InputStream is) throws IOException\r
+ {\r
+ boundary();\r
+ writeName(name);\r
+ write("; filename=\"");\r
+ write(filename);\r
+ write('"');\r
+ newline();\r
+ write("Content-Type: ");\r
+ String type = URLConnection.guessContentTypeFromName(filename);\r
+ if (type == null) type = "application/octet-stream";\r
+ writeln(type);\r
+ newline();\r
+ pipe(is, _os);\r
+ newline();\r
+ }\r
+\r
+ /**\r
+ * adds a file parameter to the request\r
+ * @param name parameter name\r
+ * @param file the file to upload\r
+ * @throws IOException\r
+ */\r
+ public void setParameter(String name, File file) throws IOException {\r
+ setParameter(name, file.getPath(), new FileInputStream(file));\r
+ }\r
+\r
+ /**\r
+ * adds a parameter to the request; if the parameter is a File, the file is uploaded,\r
+ * otherwise the string value of the parameter is passed in the request\r
+ * @param name parameter name\r
+ * @param object parameter value, a File or anything else that can be stringified\r
+ * @throws IOException\r
+ */\r
+ public void setParameter(String name, Object object) throws IOException\r
+ {\r
+ if (object instanceof File) {\r
+ setParameter(name, (File) object);\r
+ } else {\r
+ setParameter(name, object.toString());\r
+ }\r
+ }\r
+\r
+ /**\r
+ * posts the requests to the server\r
+ * @return input stream with the server response\r
+ * @throws IOException\r
+ */\r
+ public InputStream post() throws IOException {\r
+ boundary();\r
+ writeln("--");\r
+ _os.close();\r
+ return _connection.getInputStream();\r
+ }\r
+\r
+ /**\r
+ * post the POST request to the server, with the specified parameter\r
+ * @param name parameter name\r
+ * @param value parameter value\r
+ * @return input stream with the server response\r
+ * @throws IOException\r
+ * @see setParameter\r
+ */\r
+ public InputStream post(String name, Object value) throws IOException {\r
+ setParameter(name, value);\r
+ return post();\r
+ }\r
+\r
+ /**\r
+ * post the POST request to the server, with the specified parameters\r
+ * @param name1 first parameter name\r
+ * @param value1 first parameter value\r
+ * @param name2 second parameter name\r
+ * @param value2 second parameter value\r
+ * @return input stream with the server response\r
+ * @throws IOException\r
+ * @see setParameter\r
+ */\r
+ public InputStream post(String name1, Object value1, String name2, Object value2) throws IOException {\r
+ setParameter(name1, value1);\r
+ return post(name2, value2);\r
+ }\r
+\r
+ /**\r
+ * post the POST request to the server, with the specified parameters\r
+ * @param name1 first parameter name\r
+ * @param value1 first parameter value\r
+ * @param name2 second parameter name\r
+ * @param value2 second parameter value\r
+ * @param name3 third parameter name\r
+ * @param value3 third parameter value\r
+ * @return input stream with the server response\r
+ * @throws IOException\r
+ * @see setParameter\r
+ */\r
+ public InputStream post(String name1, Object value1, String name2, Object value2,\r
+ String name3, Object value3) throws IOException\r
+ {\r
+ setParameter(name1, value1);\r
+ return post(name2, value2, name3, value3);\r
+ }\r
+\r
+ /**\r
+ * post the POST request to the server, with the specified parameters\r
+ * @param name1 first parameter name\r
+ * @param value1 first parameter value\r
+ * @param name2 second parameter name\r
+ * @param value2 second parameter value\r
+ * @param name3 third parameter name\r
+ * @param value3 third parameter value\r
+ * @param name4 fourth parameter name\r
+ * @param value4 fourth parameter value\r
+ * @return input stream with the server response\r
+ * @throws IOException\r
+ * @see setParameter\r
+ */\r
+ public InputStream post(String name1, Object value1, String name2, Object value2,\r
+ String name3, Object value3, String name4, Object value4) throws IOException\r
+ {\r
+ setParameter(name1, value1);\r
+ return post(name2, value2, name3, value3, name4, value4);\r
+ }\r
+\r
+ /**\r
+ * post the POST request specified URL, with the specified parameter\r
+ * @param name parameter name\r
+ * @param value parameter value\r
+ * @return input stream with the server response\r
+ * @throws IOException\r
+ * @see setParameter\r
+ */\r
+ public static InputStream post(URL url, String name1, Object value1) throws IOException {\r
+ return new FormPoster(url).post(name1, value1);\r
+ }\r
+\r
+ /**\r
+ * post the POST request to specified URL, with the specified parameters\r
+ * @param name1 first parameter name\r
+ * @param value1 first parameter value\r
+ * @param name2 second parameter name\r
+ * @param value2 second parameter value\r
+ * @return input stream with the server response\r
+ * @throws IOException\r
+ * @see setParameter\r
+ */\r
+ public static InputStream post(URL url, String name1, Object value1,\r
+ String name2, Object value2) throws IOException\r
+ {\r
+ return new FormPoster(url).post(name1, value1, name2, value2);\r
+ }\r
+\r
+ /**\r
+ * post the POST request to specified URL, with the specified parameters\r
+ * @param name1 first parameter name\r
+ * @param value1 first parameter value\r
+ * @param name2 second parameter name\r
+ * @param value2 second parameter value\r
+ * @param name3 third parameter name\r
+ * @param value3 third parameter value\r
+ * @return input stream with the server response\r
+ * @throws IOException\r
+ * @see setParameter\r
+ */\r
+ public static InputStream post(URL url, String name1, Object value1, String name2, Object value2,\r
+ String name3, Object value3) throws IOException\r
+ {\r
+ return new FormPoster(url).post(name1, value1, name2, value2, name3, value3);\r
+ }\r
+\r
+ /**\r
+ * post the POST request to specified URL, with the specified parameters\r
+ * @param name1 first parameter name\r
+ * @param value1 first parameter value\r
+ * @param name2 second parameter name\r
+ * @param value2 second parameter value\r
+ * @param name3 third parameter name\r
+ * @param value3 third parameter value\r
+ * @param name4 fourth parameter name\r
+ * @param value4 fourth parameter value\r
+ * @return input stream with the server response\r
+ * @throws IOException\r
+ * @see setParameter\r
+ */\r
+ public static InputStream post(URL url, String name1, Object value1, String name2, Object value2,\r
+ String name3, Object value3, String name4, Object value4) throws IOException\r
+ {\r
+ return new FormPoster(url).post(name1, value1, name2, value2, name3, value3, name4, value4);\r
+ }\r
+}\r