]> gitweb.fperrin.net Git - GpsPrune.git/blobdiff - src/tim/prune/function/gpsies/FormPoster.java
Version 20, March 2020
[GpsPrune.git] / src / tim / prune / function / gpsies / FormPoster.java
diff --git a/src/tim/prune/function/gpsies/FormPoster.java b/src/tim/prune/function/gpsies/FormPoster.java
deleted file mode 100644 (file)
index 5939e63..0000000
+++ /dev/null
@@ -1,162 +0,0 @@
-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.InputStream;\r
-import java.util.Random;\r
-import java.io.OutputStream;\r
-\r
-/**\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 = null;\r
-       private OutputStream _os = null;\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
-               write("\r\n");\r
-       }\r
-\r
-       /** Write a string followed by a newline */\r
-       protected void writeln(String s) throws IOException {\r
-               write(s);\r
-               newline();\r
-       }\r
-\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
-       }\r
-\r
-\r
-       /**\r
-        * Creates a new multipart POST HTTP request for a specified URL\r
-        * @param url the URL to send request to\r
-        * @throws IOException\r
-        */\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
-       /** 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(inName);\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 inName, String inValue) throws IOException\r
-       {\r
-               boundary();\r
-               writeName(inName);\r
-               newline(); newline();\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
-               synchronized (in) {\r
-                       while((nread = in.read(buf, 0, buf.length)) >= 0) {\r
-                               out.write(buf, 0, nread);\r
-                       }\r
-               }\r
-               out.flush();\r
-               buf = null;\r
-       }\r
-\r
-       /**\r
-        * adds a file parameter to the request\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 inName, String inFilename, InputStream inStream) throws IOException\r
-       {\r
-               boundary();\r
-               writeName(inName);\r
-               write("; filename=\"");\r
-               write(inFilename);\r
-               write('"');\r
-               newline();\r
-               write("Content-Type: ");\r
-               String type = URLConnection.guessContentTypeFromName(inFilename);\r
-               if (type == null) {type = "application/octet-stream";}\r
-               writeln(type);\r
-               newline();\r
-               pipe(inStream, _os);\r
-               newline();\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
-       {\r
-               boundary();\r
-               writeln("--");\r
-               _os.close();\r
-               return _connection.getInputStream();\r
-       }\r
-\r
-       /**\r
-        * @return the HTTP response code, 200 for success or -1 if not available\r
-        */\r
-       public int getResponseCode() throws IOException\r
-       {\r
-               if (_connection != null && _connection instanceof HttpURLConnection) {\r
-                       return ((HttpURLConnection) _connection).getResponseCode();\r
-               }\r
-               return -1;\r
-       }\r
-}\r