Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(2_1_X)[TIMOB-9590] Android: Addd support to send TiFile/TiBlob in HTTPClient #2736

Merged
merged 1 commit into from
Aug 15, 2012
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -839,7 +842,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 @@ -867,7 +870,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 @@ -911,6 +914,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 @@ -1010,9 +1041,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 @@ -1184,15 +1224,16 @@ public void progress(int progress) {
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