]> gitweb.fperrin.net Git - GpsPrune.git/blobdiff - tim/prune/function/gpsies/FormPoster.java
Version 11, August 2010
[GpsPrune.git] / tim / prune / function / gpsies / FormPoster.java
index 9f9edc4b7057bf052a8379fe7d59557f137b2478..5939e63ef54656102f74f16d6d1f75139843571c 100644 (file)
 package tim.prune.function.gpsies;\r
 \r
+import java.net.HttpURLConnection;\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
+ * Taken from the Client HTTP Request class at com.myjavatools.web\r
+ * and subsequently simplified and modified\r
  * @author Vlad Patryshev\r
  */\r
 public class FormPoster\r
 {\r
-       private URLConnection _connection;\r
+       private URLConnection _connection = null;\r
        private OutputStream _os = null;\r
-       private static final Random random = new Random();\r
-       private static final String boundary = "---------------------------"\r
+       private static final Random RANDOM_GEN = new Random();\r
+       private static final String BOUNDARY = "---------------------------"\r
                + randomString() + randomString() + randomString();\r
 \r
 \r
+       /** Connect (if not already connected) */\r
        protected void connect() throws IOException {\r
                if (_os == null) _os = _connection.getOutputStream();\r
        }\r
 \r
+       /** Write a single character */\r
        protected void write(char c) throws IOException {\r
                connect();\r
                _os.write(c);\r
        }\r
 \r
+       /** Write a string */\r
        protected void write(String s) throws IOException {\r
                connect();\r
                _os.write(s.getBytes());\r
        }\r
 \r
+       /** Write a -r-n newline sequence */\r
        protected void newline() throws IOException {\r
-               connect();\r
                write("\r\n");\r
        }\r
 \r
+       /** Write a string followed by a newline */\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
+       /** Generate a random alphanumeric string */\r
+       private static String randomString() {\r
+               return Long.toString(RANDOM_GEN.nextLong(), 36);\r
        }\r
 \r
+       /** Write a boundary marker */\r
        private void boundary() throws IOException {\r
                write("--");\r
-               write(boundary);\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
+       public FormPoster(URL inUrl) throws IOException\r
+       {\r
+               _connection = inUrl.openConnection();\r
+               _connection.setDoOutput(true);\r
+               _connection.setRequestProperty("Content-Type",\r
+                       "multipart/form-data; boundary=" + BOUNDARY);\r
        }\r
 \r
-\r
-       private void writeName(String name) throws IOException\r
+       /** Write a header with the given name */\r
+       private void writeName(String inName) throws IOException\r
        {\r
                newline();\r
                write("Content-Disposition: form-data; name=\"");\r
-               write(name);\r
+               write(inName);\r
                write('"');\r
        }\r
 \r
@@ -105,23 +90,22 @@ public class FormPoster
         * @param value parameter value\r
         * @throws IOException\r
         */\r
-       public void setParameter(String name, String value) throws IOException\r
+       public void setParameter(String inName, String inValue) throws IOException\r
        {\r
                boundary();\r
-               writeName(name);\r
+               writeName(inName);\r
                newline(); newline();\r
-               writeln(value);\r
+               writeln(inValue);\r
        }\r
 \r
+       /** Pipe the contents of the input stream to the output stream */\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
@@ -130,60 +114,35 @@ public class FormPoster
 \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
+        * @param inName parameter name\r
+        * @param inFilename the name of the file\r
+        * @param inStream 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
+       public void setParameter(String inName, String inFilename, InputStream inStream) throws IOException\r
        {\r
                boundary();\r
-               writeName(name);\r
+               writeName(inName);\r
                write("; filename=\"");\r
-               write(filename);\r
+               write(inFilename);\r
                write('"');\r
                newline();\r
                write("Content-Type: ");\r
-               String type = URLConnection.guessContentTypeFromName(filename);\r
-               if (type == null) type = "application/octet-stream";\r
+               String type = URLConnection.guessContentTypeFromName(inFilename);\r
+               if (type == null) {type = "application/octet-stream";}\r
                writeln(type);\r
                newline();\r
-               pipe(is, _os);\r
+               pipe(inStream, _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
+       public InputStream post() throws IOException\r
+       {\r
                boundary();\r
                writeln("--");\r
                _os.close();\r
@@ -191,136 +150,13 @@ public class FormPoster
        }\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
+        * @return the HTTP response code, 200 for success or -1 if not available\r
         */\r
-       public static InputStream post(URL url, String name1, Object value1, String name2, Object value2,\r
-               String name3, Object value3) throws IOException\r
+       public int getResponseCode() 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
+               if (_connection != null && _connection instanceof HttpURLConnection) {\r
+                       return ((HttpURLConnection) _connection).getResponseCode();\r
+               }\r
+               return -1;\r
        }\r
 }\r