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

CordovaHttpPlugin.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package com.silkimen.cordovahttp;
  2. import java.security.KeyStore;
  3. import com.silkimen.http.TLSConfiguration;
  4. import org.apache.cordova.CallbackContext;
  5. import org.apache.cordova.CordovaInterface;
  6. import org.apache.cordova.CordovaPlugin;
  7. import org.apache.cordova.CordovaWebView;
  8. import org.json.JSONArray;
  9. import org.json.JSONException;
  10. import org.json.JSONObject;
  11. import android.util.Log;
  12. import android.util.Base64;
  13. import javax.net.ssl.TrustManagerFactory;
  14. public class CordovaHttpPlugin extends CordovaPlugin {
  15. private static final String TAG = "Cordova-Plugin-HTTP";
  16. private TLSConfiguration tlsConfiguration;
  17. @Override
  18. public void initialize(CordovaInterface cordova, CordovaWebView webView) {
  19. super.initialize(cordova, webView);
  20. this.tlsConfiguration = new TLSConfiguration();
  21. try {
  22. KeyStore store = KeyStore.getInstance("AndroidCAStore");
  23. String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
  24. TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
  25. store.load(null);
  26. tmf.init(store);
  27. this.tlsConfiguration.setHostnameVerifier(null);
  28. this.tlsConfiguration.setTrustManagers(tmf.getTrustManagers());
  29. } catch (Exception e) {
  30. Log.e(TAG, "An error occured while loading system's CA certificates", e);
  31. }
  32. }
  33. @Override
  34. public boolean execute(String action, final JSONArray args, final CallbackContext callbackContext)
  35. throws JSONException {
  36. if (action == null) {
  37. return false;
  38. }
  39. if ("get".equals(action)) {
  40. return this.executeHttpRequestWithoutData(action, args, callbackContext);
  41. } else if ("head".equals(action)) {
  42. return this.executeHttpRequestWithoutData(action, args, callbackContext);
  43. } else if ("delete".equals(action)) {
  44. return this.executeHttpRequestWithoutData(action, args, callbackContext);
  45. } else if ("options".equals(action)) {
  46. return this.executeHttpRequestWithoutData(action, args, callbackContext);
  47. } else if ("post".equals(action)) {
  48. return this.executeHttpRequestWithData(action, args, callbackContext);
  49. } else if ("put".equals(action)) {
  50. return this.executeHttpRequestWithData(action, args, callbackContext);
  51. } else if ("patch".equals(action)) {
  52. return this.executeHttpRequestWithData(action, args, callbackContext);
  53. } else if ("uploadFiles".equals(action)) {
  54. return this.uploadFiles(args, callbackContext);
  55. } else if ("downloadFile".equals(action)) {
  56. return this.downloadFile(args, callbackContext);
  57. } else if ("setServerTrustMode".equals(action)) {
  58. return this.setServerTrustMode(args, callbackContext);
  59. } else if ("setClientAuthMode".equals(action)) {
  60. return this.setClientAuthMode(args, callbackContext);
  61. } else {
  62. return false;
  63. }
  64. }
  65. private boolean executeHttpRequestWithoutData(final String method, final JSONArray args,
  66. final CallbackContext callbackContext) throws JSONException {
  67. String url = args.getString(0);
  68. JSONObject headers = args.getJSONObject(1);
  69. int timeout = args.getInt(2) * 1000;
  70. boolean followRedirect = args.getBoolean(3);
  71. String responseType = args.getString(4);
  72. CordovaHttpOperation request = new CordovaHttpOperation(method.toUpperCase(), url, headers, timeout, followRedirect,
  73. responseType, this.tlsConfiguration, callbackContext);
  74. cordova.getThreadPool().execute(request);
  75. return true;
  76. }
  77. private boolean executeHttpRequestWithData(final String method, final JSONArray args,
  78. final CallbackContext callbackContext) throws JSONException {
  79. String url = args.getString(0);
  80. Object data = args.get(1);
  81. String serializer = args.getString(2);
  82. JSONObject headers = args.getJSONObject(3);
  83. int timeout = args.getInt(4) * 1000;
  84. boolean followRedirect = args.getBoolean(5);
  85. String responseType = args.getString(6);
  86. CordovaHttpOperation request = new CordovaHttpOperation(method.toUpperCase(), url, serializer, data, headers,
  87. timeout, followRedirect, responseType, this.tlsConfiguration, callbackContext);
  88. cordova.getThreadPool().execute(request);
  89. return true;
  90. }
  91. private boolean uploadFiles(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
  92. String url = args.getString(0);
  93. JSONObject headers = args.getJSONObject(1);
  94. JSONArray filePaths = args.getJSONArray(2);
  95. JSONArray uploadNames = args.getJSONArray(3);
  96. int timeout = args.getInt(4) * 1000;
  97. boolean followRedirect = args.getBoolean(5);
  98. String responseType = args.getString(6);
  99. CordovaHttpUpload upload = new CordovaHttpUpload(url, headers, filePaths, uploadNames, timeout, followRedirect,
  100. responseType, this.tlsConfiguration, this.cordova.getActivity().getApplicationContext(), callbackContext);
  101. cordova.getThreadPool().execute(upload);
  102. return true;
  103. }
  104. private boolean downloadFile(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
  105. String url = args.getString(0);
  106. JSONObject headers = args.getJSONObject(1);
  107. String filePath = args.getString(2);
  108. int timeout = args.getInt(3) * 1000;
  109. boolean followRedirect = args.getBoolean(4);
  110. CordovaHttpDownload download = new CordovaHttpDownload(url, headers, filePath, timeout, followRedirect,
  111. this.tlsConfiguration, callbackContext);
  112. cordova.getThreadPool().execute(download);
  113. return true;
  114. }
  115. private boolean setServerTrustMode(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
  116. CordovaServerTrust runnable = new CordovaServerTrust(args.getString(0), this.cordova.getActivity(),
  117. this.tlsConfiguration, callbackContext);
  118. cordova.getThreadPool().execute(runnable);
  119. return true;
  120. }
  121. private boolean setClientAuthMode(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
  122. byte[] pkcs = args.isNull(2) ? null : Base64.decode(args.getString(2), Base64.DEFAULT);
  123. CordovaClientAuth runnable = new CordovaClientAuth(args.getString(0), args.isNull(1) ? null : args.getString(1),
  124. pkcs, args.getString(3), this.cordova.getActivity(), this.cordova.getActivity().getApplicationContext(),
  125. this.tlsConfiguration, callbackContext);
  126. cordova.getThreadPool().execute(runnable);
  127. return true;
  128. }
  129. }