-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCBHelperRequest.java
More file actions
204 lines (170 loc) · 6.61 KB
/
Copy pathCBHelperRequest.java
File metadata and controls
204 lines (170 loc) · 6.61 KB
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
/*
Copyright (c) 2013 Cloudbase.io 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 com.cloudbase;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Map;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
/**
* A Runnable object used by the CBHelper class to execute HTTP calls asynchronously. This should not be called
* directly.
* @author Stefano Buliani
*
*/
public class CBHelperRequest implements Runnable {
//private String url;
//private String function;
//private String fileId;
//private Hashtable<String, String> postData;
//private ArrayList<File> files;
private CBQueuedRequest request;
private String temporaryFilePath;
private String queueFileName;
private CBHelperResponder responder;
private CBHelperResponse resp;
private CBHelper helperObject;
public CBHelperRequest(CBQueuedRequest req, CBHelper helper) {
this.request = req;
this.helperObject = helper;
}
@SuppressWarnings("unchecked")
public void run() {
//HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
HttpClient httpclient = HttpClientBuilder.create().build();
HttpPost httppost = new HttpPost(this.request.getUrl());
try {
// Add your data
Enumeration<String> params = this.request.getParameters().keys();
// prepare the request adding all of the parameters.
CBMultipartEntity entity = new CBMultipartEntity();
while (params.hasMoreElements())
{
String curKey = params.nextElement();
entity.addPart(new CBStringPart(curKey, this.request.getParameters().get(curKey), HTTP.UTF_8));
}
// if we have file attachments then add each file to the multipart request
if (this.request.getFiles() != null && this.request.getFiles().size() > 0) {
int fileCounter = 0;
for (File curFile : this.request.getFiles()) {
String name = curFile.getName();
int pos = name.lastIndexOf('.');
String ext = name.substring(pos+1);
entity.addPart(new CBFilePart("file" + fileCounter, curFile, null,
(pos > -1 ? MimeTypes.getMimeType(ext) : null)));
fileCounter++;
}
}
// add the multipart request to the http connection
httppost.setEntity(entity);
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
// if we have a responder then parse the response data into the global CBHelperResponse object
if (this.responder != null) {
resp = new CBHelperResponse();
resp.setFunction(this.request.getCloudbaseFunction());
resp.setHttpStatus(response.getStatusLine().getStatusCode());
// if it's a download then we need to save the file content into a temporary file in
// application cache folder. Then return that file to the responder
if (this.request.getCloudbaseFunction().equals("download")) {
InputStream input = response.getEntity().getContent();
File outputFile = File.createTempFile(this.request.getFileId(), null, new File(this.getTemporaryFilePath()));
OutputStream fos = new BufferedOutputStream(new FileOutputStream(outputFile));
try {
byte[] buffer = new byte[(int) 4096];
int readBytes;
while (((readBytes = input.read(buffer, 0, buffer.length)) != -1)) {
fos.write(buffer, 0, readBytes);
}
} catch (IOException e) {
e.printStackTrace();
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
}
}
if (input != null) {
try {
input.close();
} catch (IOException e) {
}
}
resp.setDownloadedFile(outputFile);
} else {
// if it's not a download parse the JSON response and set all
// the variables in the CBHelperResponse object
String responseString = EntityUtils.toString(response.getEntity());
resp.setResponseDataString(responseString);
//if (this.helperObject.isDebugMode())
//Log.d("test", resp.getResponseDataString());
// Use the cloudbase.io deserializer to get the data in a Map<String, Object>
// format.
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Object.class, new CBNaturalDeserializer());
Gson gson = gsonBuilder.create();
Map<String, Object> responseData = gson.fromJson(responseString,Map.class);
if (responseData == null) {
resp.setErrorMessage("Empty response data");
resp.setSuccess(false);
} else {
Map<String, Object> outputData = (Map<String, Object>)responseData.get(this.request.getCloudbaseFunction());
resp.setData(outputData.get("message"));
resp.setErrorMessage((String)outputData.get("error"));
resp.setSuccess(((String)outputData.get("status")).equals("OK"));
}
}
// now that the response object is ready use the Handler we have been handed from the
// CBHelper class on the main thread to call the responder object. This way the data
// is available to the UI thread
responder.handleResponse(request, resp);
}
if ((resp == null || resp.getHttpStatus() == 200) && this.queueFileName != null) {
this.helperObject.removeQueuedRequest(this.queueFileName);
}
} catch (Exception e) {
//Log.e("REQUEST", "Error " + e.getMessage(), e);
if (this.responder != null) {
responder.handleResponse(request, resp);
}
}
}
public CBHelperResponder getResponder() {
return responder;
}
public void setResponder(CBHelperResponder responder) {
this.responder = responder;
}
public String getTemporaryFilePath() {
return temporaryFilePath;
}
public void setTemporaryFilePath(String temporaryFilePath) {
this.temporaryFilePath = temporaryFilePath;
}
public String getQueueFileName() {
return queueFileName;
}
public void setQueueFileName(String queueFileName) {
this.queueFileName = queueFileName;
}
}