|
17 | 17 | import com.loopj.android.http.AsyncHttpClient; |
18 | 18 | import com.loopj.android.http.AsyncHttpResponseHandler; |
19 | 19 | import com.loopj.android.http.Base64; |
| 20 | +import com.loopj.android.http.MySSLSocketFactory; |
20 | 21 | import com.loopj.android.http.RequestParams; |
21 | 22 |
|
22 | 23 | import java.io.ByteArrayOutputStream; |
23 | 24 | import java.io.File; |
| 25 | +import java.security.KeyStore; |
24 | 26 | import java.util.HashMap; |
25 | 27 | import java.util.Map; |
26 | 28 |
|
@@ -156,159 +158,12 @@ public void readStream(String path, String encoding, int bufferSize) { |
156 | 158 |
|
157 | 159 | @ReactMethod |
158 | 160 | public void fetchBlob(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, final Callback callback) { |
159 | | - |
160 | | - RNFetchBlobConfig config = new RNFetchBlobConfig(options); |
161 | | - |
162 | | - try { |
163 | | - AsyncHttpClient req = new AsyncHttpClient(); |
164 | | - |
165 | | - AbstractHttpEntity entity = null; |
166 | | - |
167 | | - // set headers |
168 | | - ReadableMapKeySetIterator it = headers.keySetIterator(); |
169 | | - while (it.hasNextKey()) { |
170 | | - String key = it.nextKey(); |
171 | | - req.addHeader(key, headers.getString(key)); |
172 | | - } |
173 | | - |
174 | | - // set body for POST and PUT |
175 | | - if(body != null && method.equalsIgnoreCase("post") || method.equalsIgnoreCase("put")) { |
176 | | - |
177 | | - byte [] blob; |
178 | | - // upload from storage |
179 | | - if(body.startsWith(filePathPrefix)) { |
180 | | - String filePath = body.substring(filePathPrefix.length()); |
181 | | - entity = new FileEntity(new File(filePath)); |
182 | | - } |
183 | | - else { |
184 | | - blob = Base64.decode(body, 0); |
185 | | - entity = new ByteArrayEntity(blob); |
186 | | - } |
187 | | - entity.setContentType(headers.getString("Content-Type")); |
188 | | - } |
189 | | - |
190 | | - AsyncHttpResponseHandler handler; |
191 | | - |
192 | | - // create handler |
193 | | - if(config.fileCache || config.path != null) { |
194 | | - handler = new RNFetchBlobFileHandler(this.getReactApplicationContext(), taskId, config, callback); |
195 | | - // if path format invalid, throw error |
196 | | - if (!((RNFetchBlobFileHandler)handler).isValid) { |
197 | | - callback.invoke("RNFetchBlob fetch error, configuration path `"+ config.path +"` is not a valid path."); |
198 | | - return; |
199 | | - } |
200 | | - } |
201 | | - else |
202 | | - handler = new RNFetchBlobBinaryHandler(this.getReactApplicationContext(), taskId, callback); |
203 | | - |
204 | | - // send request |
205 | | - switch(method.toLowerCase()) { |
206 | | - case "get" : |
207 | | - req.get(url, handler); |
208 | | - break; |
209 | | - case "post" : |
210 | | - req.post(this.getReactApplicationContext(), url, entity, "octet-stream", handler); |
211 | | - break; |
212 | | - case "put" : |
213 | | - req.put(this.getReactApplicationContext(), url, entity, "octet-stream",handler); |
214 | | - break; |
215 | | - case "delete" : |
216 | | - req.delete(url, handler); |
217 | | - break; |
218 | | - } |
219 | | - } catch(Exception error) { |
220 | | - callback.invoke( "RNFetchBlob serialize request data failed: " + error.getMessage() + error.getCause()); |
221 | | - } |
222 | | - |
| 161 | + new RNFetchBlobReq(this.getReactApplicationContext(), options, taskId, method, url, headers, body, callback).run(); |
223 | 162 | } |
224 | 163 |
|
225 | 164 | @ReactMethod |
226 | 165 | public void fetchBlobForm(ReadableMap options, String taskId, String method, String url, ReadableMap headers, ReadableArray body, final Callback callback) { |
227 | | - |
228 | | - RNFetchBlobConfig config = new RNFetchBlobConfig(options); |
229 | | - try { |
230 | | - |
231 | | - AsyncHttpClient req = new AsyncHttpClient(); |
232 | | - |
233 | | - HttpEntity entity = null; |
234 | | - |
235 | | - // set headers |
236 | | - if(headers != null) { |
237 | | - ReadableMapKeySetIterator it = headers.keySetIterator(); |
238 | | - while (it.hasNextKey()) { |
239 | | - String key = it.nextKey(); |
240 | | - req.addHeader(key, headers.getString(key)); |
241 | | - } |
242 | | - } |
243 | | - |
244 | | - // set body for POST and PUT |
245 | | - if(body != null && method.equalsIgnoreCase("post") || method.equalsIgnoreCase("put")) { |
246 | | - Long tsLong = System.currentTimeMillis()/1000; |
247 | | - String ts = tsLong.toString(); |
248 | | - String boundary = "RNFetchBlob".concat(ts); |
249 | | - MultipartEntityBuilder form = MultipartEntityBuilder.create(); |
250 | | - form.setBoundary(boundary); |
251 | | - for( int i = 0; i< body.size(); i++) { |
252 | | - ReadableMap map = body.getMap(i); |
253 | | - String name = map.getString("name"); |
254 | | - if(!map.hasKey("data")) |
255 | | - continue; |
256 | | - String data = map.getString("data"); |
257 | | - // file field |
258 | | - if(map.hasKey("filename")) { |
259 | | - String filename = map.getString("filename"); |
260 | | - // upload from storage |
261 | | - if(data.startsWith(filePathPrefix)) { |
262 | | - File file = new File(data.substring(filePathPrefix.length())); |
263 | | - form.addBinaryBody(name, file, ContentType.APPLICATION_OCTET_STREAM, filename); |
264 | | - } |
265 | | - // base64 embedded file content |
266 | | - else { |
267 | | - form.addBinaryBody(name, Base64.decode(data, 0), ContentType.APPLICATION_OCTET_STREAM, filename); |
268 | | - } |
269 | | - } |
270 | | - // data field |
271 | | - else { |
272 | | - form.addTextBody(name, map.getString("data")); |
273 | | - } |
274 | | - } |
275 | | - entity = form.build(); |
276 | | - req.addHeader("Content-Type", headers.getString("Content-Type") + "; charset=utf8; boundary=" + boundary); |
277 | | - } |
278 | | - |
279 | | - AsyncHttpResponseHandler handler; |
280 | | - |
281 | | - // create handler |
282 | | - if(config.fileCache || config.path != null) { |
283 | | - handler = new RNFetchBlobFileHandler(this.getReactApplicationContext(), taskId, config, callback); |
284 | | - // if path format invalid, throw error |
285 | | - if (!((RNFetchBlobFileHandler)handler).isValid) { |
286 | | - callback.invoke("RNFetchBlob fetch error, configuration path `"+ config.path +"` is not a valid path."); |
287 | | - return; |
288 | | - } |
289 | | - } |
290 | | - else |
291 | | - handler = new RNFetchBlobBinaryHandler(this.getReactApplicationContext(), taskId, callback); |
292 | | - |
293 | | - // send request |
294 | | - switch(method.toLowerCase()) { |
295 | | - case "get" : |
296 | | - req.get(url, handler); |
297 | | - break; |
298 | | - case "post" : |
299 | | - req.post(this.getReactApplicationContext(), url, entity, "multipart/form-data; charset=utf8", handler); |
300 | | - break; |
301 | | - case "put" : |
302 | | - req.put(this.getReactApplicationContext(), url, entity, "multipart/form-data",handler); |
303 | | - break; |
304 | | - case "delete" : |
305 | | - req.delete(url, handler); |
306 | | - break; |
307 | | - } |
308 | | - } catch(Exception error) { |
309 | | - callback.invoke( "RNFetchBlob serialize request data failed: " + error.getMessage() + error.getCause()); |
310 | | - } |
311 | | - |
| 166 | + new RNFetchBlobReq(this.getReactApplicationContext(), options, taskId, method, url, headers, body, callback).run(); |
312 | 167 | } |
313 | 168 |
|
314 | 169 | } |
|
0 commit comments