]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/function/gpsies/FormPoster.java
Update build scripts
[GpsPrune.git] / src / tim / prune / function / gpsies / FormPoster.java
1 package tim.prune.function.gpsies;\r
2 \r
3 import java.net.HttpURLConnection;\r
4 import java.net.URLConnection;\r
5 import java.net.URL;\r
6 import java.io.IOException;\r
7 import java.io.InputStream;\r
8 import java.util.Random;\r
9 import java.io.OutputStream;\r
10 \r
11 /**\r
12  * Taken from the Client HTTP Request class at com.myjavatools.web\r
13  * and subsequently simplified and modified\r
14  * @author Vlad Patryshev\r
15  */\r
16 public class FormPoster\r
17 {\r
18         private URLConnection _connection = null;\r
19         private OutputStream _os = null;\r
20         private static final Random RANDOM_GEN = new Random();\r
21         private static final String BOUNDARY = "---------------------------"\r
22                 + randomString() + randomString() + randomString();\r
23 \r
24 \r
25         /** Connect (if not already connected) */\r
26         protected void connect() throws IOException {\r
27                 if (_os == null) _os = _connection.getOutputStream();\r
28         }\r
29 \r
30         /** Write a single character */\r
31         protected void write(char c) throws IOException {\r
32                 connect();\r
33                 _os.write(c);\r
34         }\r
35 \r
36         /** Write a string */\r
37         protected void write(String s) throws IOException {\r
38                 connect();\r
39                 _os.write(s.getBytes());\r
40         }\r
41 \r
42         /** Write a -r-n newline sequence */\r
43         protected void newline() throws IOException {\r
44                 write("\r\n");\r
45         }\r
46 \r
47         /** Write a string followed by a newline */\r
48         protected void writeln(String s) throws IOException {\r
49                 write(s);\r
50                 newline();\r
51         }\r
52 \r
53         /** Generate a random alphanumeric string */\r
54         private static String randomString() {\r
55                 return Long.toString(RANDOM_GEN.nextLong(), 36);\r
56         }\r
57 \r
58         /** Write a boundary marker */\r
59         private void boundary() throws IOException {\r
60                 write("--");\r
61                 write(BOUNDARY);\r
62         }\r
63 \r
64 \r
65         /**\r
66          * Creates a new multipart POST HTTP request for a specified URL\r
67          * @param url the URL to send request to\r
68          * @throws IOException\r
69          */\r
70         public FormPoster(URL inUrl) throws IOException\r
71         {\r
72                 _connection = inUrl.openConnection();\r
73                 _connection.setDoOutput(true);\r
74                 _connection.setRequestProperty("Content-Type",\r
75                         "multipart/form-data; boundary=" + BOUNDARY);\r
76         }\r
77 \r
78         /** Write a header with the given name */\r
79         private void writeName(String inName) throws IOException\r
80         {\r
81                 newline();\r
82                 write("Content-Disposition: form-data; name=\"");\r
83                 write(inName);\r
84                 write('"');\r
85         }\r
86 \r
87         /**\r
88          * adds a string parameter to the request\r
89          * @param name parameter name\r
90          * @param value parameter value\r
91          * @throws IOException\r
92          */\r
93         public void setParameter(String inName, String inValue) throws IOException\r
94         {\r
95                 boundary();\r
96                 writeName(inName);\r
97                 newline(); newline();\r
98                 writeln(inValue);\r
99         }\r
100 \r
101         /** Pipe the contents of the input stream to the output stream */\r
102         private static void pipe(InputStream in, OutputStream out) throws IOException\r
103         {\r
104                 byte[] buf = new byte[500000];\r
105                 int nread;\r
106                 synchronized (in) {\r
107                         while((nread = in.read(buf, 0, buf.length)) >= 0) {\r
108                                 out.write(buf, 0, nread);\r
109                         }\r
110                 }\r
111                 out.flush();\r
112                 buf = null;\r
113         }\r
114 \r
115         /**\r
116          * adds a file parameter to the request\r
117          * @param inName parameter name\r
118          * @param inFilename the name of the file\r
119          * @param inStream input stream to read the contents of the file from\r
120          * @throws IOException\r
121          */\r
122         public void setParameter(String inName, String inFilename, InputStream inStream) throws IOException\r
123         {\r
124                 boundary();\r
125                 writeName(inName);\r
126                 write("; filename=\"");\r
127                 write(inFilename);\r
128                 write('"');\r
129                 newline();\r
130                 write("Content-Type: ");\r
131                 String type = URLConnection.guessContentTypeFromName(inFilename);\r
132                 if (type == null) {type = "application/octet-stream";}\r
133                 writeln(type);\r
134                 newline();\r
135                 pipe(inStream, _os);\r
136                 newline();\r
137         }\r
138 \r
139         /**\r
140          * posts the requests to the server\r
141          * @return input stream with the server response\r
142          * @throws IOException\r
143          */\r
144         public InputStream post() throws IOException\r
145         {\r
146                 boundary();\r
147                 writeln("--");\r
148                 _os.close();\r
149                 return _connection.getInputStream();\r
150         }\r
151 \r
152         /**\r
153          * @return the HTTP response code, 200 for success or -1 if not available\r
154          */\r
155         public int getResponseCode() throws IOException\r
156         {\r
157                 if (_connection != null && _connection instanceof HttpURLConnection) {\r
158                         return ((HttpURLConnection) _connection).getResponseCode();\r
159                 }\r
160                 return -1;\r
161         }\r
162 }\r