Skip to content

Commit

Permalink
Merge pull request #2736 from mstepanov/timob-9590-21x
Browse files Browse the repository at this point in the history
(2_1_X)[TIMOB-9590] Android: Addd support to send TiFile/TiBlob in HTTPClient
  • Loading branch information
srahim committed Aug 15, 2012
2 parents d32209a + d0e5c25 commit f5052fb
Showing 1 changed file with 49 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
package ti.modules.titanium.network;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
Expand Down Expand Up @@ -55,6 +56,8 @@
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.cookie.Cookie;
import org.apache.http.entity.AbstractHttpEntity;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.FileEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ContentBody;
Expand Down Expand Up @@ -131,7 +134,7 @@ public class TiHTTPClient
private long maxBufferSize;
private ArrayList<NameValuePair> nvPairs;
private HashMap<String, ContentBody> parts;
private String data;
private Object data;
private boolean needMultipart;
private Thread clientThread;
private boolean aborted;
Expand Down Expand Up @@ -841,7 +844,7 @@ public void open(String method, String url)
}
}

public void addStringData(String data)
public void setRawData(Object data)
{
this.data = data;
}
Expand Down Expand Up @@ -869,7 +872,7 @@ public void addPostData(String name, String value)
}
}

public int addTitaniumFileAsPostData(String name, Object value)
private int addTitaniumFileAsPostData(String name, Object value)
{
try {
// TiResourceFile cannot use the FileBody approach directly, because it requires
Expand Down Expand Up @@ -915,6 +918,34 @@ public int addTitaniumFileAsPostData(String name, Object value)
}
return 0;
}

private Object titaniumFileAsPutData(Object value)
{
if (value instanceof TiBaseFile && !(value instanceof TiResourceFile)) {
TiBaseFile baseFile = (TiBaseFile) value;
return new FileEntity(baseFile.getNativeFile(), TiMimeTypeHelper.getMimeType(baseFile.nativePath()));
} else if (value instanceof TiBlob || value instanceof TiResourceFile) {
try {
TiBlob blob;
if (value instanceof TiBlob) {
blob = (TiBlob) value;
} else {
blob = ((TiResourceFile) value).read();
}
String mimeType = blob.getMimeType();
File tmpFile = File.createTempFile("tixhr", "." + TiMimeTypeHelper.getFileExtensionFromMimeType(mimeType, "txt"));
FileOutputStream fos = new FileOutputStream(tmpFile);
fos.write(blob.getBytes());
fos.close();

tmpFiles.add(tmpFile);
return new FileEntity(tmpFile, mimeType);
} catch (IOException e) {
Log.e(LCAT, "Error adding put data: " + e.getMessage());
}
}
return value;
}

protected DefaultHttpClient createClient()
{
Expand Down Expand Up @@ -1014,9 +1045,18 @@ public void send(Object userData) throws MethodNotSupportedException
if (queryStringAltered) {
this.url = uri.toString();
}

} else if (userData instanceof TiFileProxy || userData instanceof TiBaseFile || userData instanceof TiBlob) {
Object value = userData;
if (value instanceof TiFileProxy) {
value = ((TiFileProxy) value).getBaseFile();
}
if (value instanceof TiBaseFile || value instanceof TiBlob) {
setRawData(titaniumFileAsPutData(value));
} else {
setRawData(TiConvert.toString(value));
}
} else {
addStringData(TiConvert.toString(userData));
setRawData(TiConvert.toString(userData));
}
}

Expand Down Expand Up @@ -1202,15 +1242,16 @@ private void deleteTmpFiles()
private void handleURLEncodedData(UrlEncodedFormEntity form)
{
AbstractHttpEntity entity = null;
if (data != null) {
if (data instanceof String) {
try {
entity = new StringEntity(data, "UTF-8");
entity = new StringEntity((String) data, "UTF-8");

} catch(Exception ex) {
//FIXME
Log.e(LCAT, "Exception, implement recovery: ", ex);
}

} else if (data instanceof AbstractHttpEntity) {
entity = (AbstractHttpEntity) data;
} else {
entity = form;
}
Expand Down

0 comments on commit f5052fb

Please sign in to comment.