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) {
}
}
}