Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

CordovaHttpDownload.java 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package com.silkimen.cordovahttp;
  2. import java.io.File;
  3. import java.net.URI;
  4. import javax.net.ssl.HostnameVerifier;
  5. import javax.net.ssl.SSLSocketFactory;
  6. import com.silkimen.http.HttpRequest;
  7. import com.silkimen.http.TLSConfiguration;
  8. import org.apache.cordova.CallbackContext;
  9. import org.apache.cordova.file.FileUtils;
  10. import org.json.JSONObject;
  11. class CordovaHttpDownload extends CordovaHttpBase {
  12. private String filePath;
  13. public CordovaHttpDownload(String url, JSONObject headers, String filePath, int timeout, boolean followRedirects,
  14. TLSConfiguration tlsConfiguration, CallbackContext callbackContext) {
  15. super("GET", url, headers, timeout, followRedirects, "text", tlsConfiguration, callbackContext);
  16. this.filePath = filePath;
  17. }
  18. @Override
  19. protected void processResponse(HttpRequest request, CordovaHttpResponse response) throws Exception {
  20. response.setStatus(request.code());
  21. response.setUrl(request.url().toString());
  22. response.setHeaders(request.headers());
  23. if (request.code() >= 200 && request.code() < 300) {
  24. File file = new File(new URI(this.filePath));
  25. JSONObject fileEntry = FileUtils.getFilePlugin().getEntryForFile(file);
  26. request.receive(file);
  27. response.setFileEntry(fileEntry);
  28. } else {
  29. response.setErrorMessage("There was an error downloading the file");
  30. }
  31. }
  32. }