forked from blackberry/WebWorks-Community-APIs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UploadRunnable.java
290 lines (237 loc) · 8.81 KB
/
UploadRunnable.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
* Copyright 2010-2011 Research In Motion Limited.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package webworks.io;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Hashtable;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.io.HttpsConnection;
import javax.microedition.io.file.FileConnection;
import net.rim.device.api.io.IOUtilities;
import net.rim.device.api.io.MIMETypeAssociations;
import net.rim.device.api.io.http.HttpProtocolConstants;
import net.rim.device.api.io.transport.ConnectionDescriptor;
import net.rim.device.api.io.transport.ConnectionFactory;
import net.rim.device.api.io.transport.TransportInfo;
import net.rim.device.api.io.transport.options.BisBOptions;
import net.rim.device.api.script.ScriptableFunction;
import net.rim.device.api.system.DeviceInfo;
public class UploadRunnable implements Runnable
{
private static final String BOUNDARY = "----0x6d6356418a05040fL";
private static final String TD = "--";
private static final String DCRLF = HttpProtocolConstants.HTTP_HEADER_DOUBLE_CRLF;
private static final String CRLF = HttpProtocolConstants.HTTP_HEADER_SINGLE_CRLF;
private String _url;
private Hashtable _params;
private String _fileKey;
private String _filePath;
private String _mimeType;
private Hashtable _headers;
private ScriptableFunction _success;
private ScriptableFunction _error;
private ConnectionFactory _factory;
private int[] _transports = new int[] {
TransportInfo.TRANSPORT_TCP_WIFI,
TransportInfo.TRANSPORT_BIS_B,
TransportInfo.TRANSPORT_MDS,
TransportInfo.TRANSPORT_TCP_CELLULAR,
TransportInfo.TRANSPORT_WAP2,
TransportInfo.TRANSPORT_WAP
};
public UploadRunnable(String url, String fileKey, Hashtable params, String filePath, String mimeType, long timeout, Hashtable headers, int[] transports, ScriptableFunction success, ScriptableFunction error)
{
_url = url;
_fileKey = fileKey;
_filePath = filePath;
_params = params;
_headers = headers;
_mimeType = mimeType;
if (transports != null) {
_transports = transports;
}
_success = success;
_error = error;
_factory = new ConnectionFactory();
if (DeviceInfo.isSimulator()) {
_factory.setPreferredTransportTypes(new int[] { TransportInfo.TRANSPORT_TCP_WIFI, TransportInfo.TRANSPORT_TCP_CELLULAR });
}
_factory.setPreferredTransportTypes(_transports);
_factory.setAttemptsLimit(100);
_factory.setTimeoutSupported(true);
_factory.setConnectionTimeout(timeout);
new Thread(this).start();
}
public void run() {
FileConnection fc = null;
HttpConnection hc = null;
InputStream is = null;
OutputStream os = null;
boolean isHTTPS = false;
try {
try {
fc = (FileConnection)Connector.open(_filePath, Connector.READ);
} catch (Exception e) {
Logger.error("Invalid file path");
callErrorCallback(new String[] {"Invalid file path"});
return;
}
Logger.log("Setting mime type...");
if (_mimeType == null) {
_mimeType = MIMETypeAssociations.getMIMEType(_filePath);
if (_mimeType == null) {
_mimeType = HttpProtocolConstants.CONTENT_TYPE_IMAGE_JPEG;
}
}
if (!fc.exists()) {
Logger.error("File not found");
callErrorCallback(new String[] { _filePath + " not found" });
}
if (_url.indexOf("https") == 0) {
isHTTPS = true;
Logger.error("Setting End to End");
_factory.setEndToEndDesired(isHTTPS);
}
ConnectionDescriptor connDesc = _factory.getConnection(_url);
if (connDesc != null) {
Logger.info("URL: " + connDesc.getUrl());
try {
if (isHTTPS) {
hc = (HttpsConnection) connDesc.getConnection();
} else {
hc = (HttpConnection) connDesc.getConnection();
}
String startBoundary = getStartBoundary(_fileKey, fc.getName(), _mimeType);
String endBoundary = getEndBoundary();
String params = (_params != null) ? getParameters(_params) : "";
long fileSize = fc.fileSize();
long contentLength = fileSize +
(long)startBoundary.length() +
(long)endBoundary.length() +
(long)params.length();
hc.setRequestMethod(HttpConnection.POST);
if (_headers != null) {
String hKey;
String hVal;
for (Enumeration e = _headers.keys(); e.hasMoreElements();) {
hKey = e.nextElement().toString();
hVal = (String) _headers.get(hKey);
Logger.error(hKey +": " + hVal);
hc.setRequestProperty(hKey, hVal);
}
}
hc.setRequestProperty(
HttpProtocolConstants.HEADER_USER_AGENT,
System.getProperty("browser.useragent"));
hc.setRequestProperty(
HttpProtocolConstants.HEADER_KEEP_ALIVE, "300");
hc.setRequestProperty(
HttpProtocolConstants.HEADER_CONNECTION, "keep-alive");
hc.setRequestProperty(
HttpProtocolConstants.HEADER_CONTENT_TYPE,
HttpProtocolConstants.CONTENT_TYPE_MULTIPART_FORM_DATA + "; boundary=" + BOUNDARY);
hc.setRequestProperty(
HttpProtocolConstants.HEADER_CONTENT_LENGTH,
Long.toString(contentLength));
os = hc.openDataOutputStream();
os.write(params.getBytes());
os.write(startBoundary.getBytes());
is = fc.openInputStream();
byte[] data = IOUtilities.streamToBytes(is);
os.write(data);
is.close();
os.write(endBoundary.getBytes());
os.flush();
os.close();
is = hc.openDataInputStream();
int responseCode = hc.getResponseCode();
if (responseCode != HttpConnection.HTTP_OK) {
Logger.error("Response code: " +responseCode);
callErrorCallback(new Object[] { "Server Error", new Integer(responseCode) });
} else {
callSuccessCallback(new Object[]{ new String(IOUtilities.streamToBytes(is)) });
}
} catch (Throwable e) {
callErrorCallback(new String[] { e.getMessage() });
e.printStackTrace();
}
} else {
Logger.error("Error creating HTTP connection");
callErrorCallback(new String[] { "Error creating HTTP connection." });
}
} finally {
try {
if (fc != null) fc.close();
if (os != null) os.close();
if (is != null) is.close();
if (hc != null) hc.close();
} catch (Exception e) {
}
}
}
protected String getStartBoundary(String fileKey, String fileName, String mimeType) {
return new StringBuffer()
.append(CRLF)
.append(TD).append(BOUNDARY)
.append(CRLF)
.append("Content-Disposition: form-data; name=\"").append(fileKey)
.append("\"; filename=\"").append(fileName).append("\"")
.append(CRLF)
.append(HttpProtocolConstants.HEADER_CONTENT_TYPE).append(mimeType)
.append(DCRLF)
.toString();
}
protected String getEndBoundary() {
return CRLF + TD + BOUNDARY + TD + CRLF;
}
protected String getParameters(Hashtable params) {
StringBuffer buf = new StringBuffer();
Enumeration e = params.keys();
String key;
String value;
while (e.hasMoreElements()) {
key = e.nextElement().toString();
value = (String) params.get(key);
buf.append(TD).append(BOUNDARY).append(CRLF)
.append("Content-Disposition: form-data; name=\"").append(key).append("\"")
.append(CRLF).append(CRLF)
.append(value).append(CRLF);
}
return buf.toString();
}
private ScriptableFunction callSuccessCallback(Object[] args)
{
if (_success != null) {
try {
_success.invoke(null, args);
} catch(Exception e) {
}
}
return null;
}
private ScriptableFunction callErrorCallback(Object[] args)
{
if (_error != null) {
try {
_error.invoke(null, args);
} catch(Exception e) {
}
}
return null;
}
}