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

WhitelistAPI.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. package org.apache.cordova.test;
  18. import org.apache.cordova.Whitelist;
  19. import org.apache.cordova.Config;
  20. import org.apache.cordova.CallbackContext;
  21. import org.apache.cordova.CordovaPlugin;
  22. import org.apache.cordova.PluginResult;
  23. import org.json.JSONArray;
  24. import org.json.JSONException;
  25. import org.apache.cordova.PluginManager;
  26. import java.lang.reflect.InvocationTargetException;
  27. import java.lang.reflect.Method;
  28. public class WhitelistAPI extends CordovaPlugin {
  29. /**
  30. * Executes the request and returns PluginResult.
  31. *
  32. * @param action The action to execute.
  33. * @param args JSONArry of arguments for the plugin.
  34. * @param callbackContext The callback id used when calling back into JavaScript.
  35. * @return True if the action was valid, false if not.
  36. */
  37. public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
  38. if (action.equals("URLMatchesPatterns")) {
  39. String url = args.getString(0);
  40. JSONArray patterns = args.getJSONArray(1);
  41. Whitelist whitelist = new Whitelist();
  42. for (int i=0; i < patterns.length(); i++) {
  43. String pattern = patterns.getString(i);
  44. whitelist.addWhiteListEntry(pattern, false);
  45. }
  46. boolean isAllowed = whitelist.isUrlWhiteListed(url);
  47. callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, isAllowed));
  48. return true;
  49. } else if (action.equals("URLIsAllowed")) {
  50. String url = args.getString(0);
  51. /* This code exists for compatibility between 3.x and 4.x versions of Cordova.
  52. * Previously the CordovaWebView class had a method, getWhitelist, which would
  53. * return a Whitelist object. Since the fixed whitelist is removed in Cordova 4.x,
  54. * the correct call now is to shouldAllowRequest from the plugin manager.
  55. */
  56. Boolean isAllowed = null;
  57. try {
  58. Method isUrlWhiteListed = Config.class.getDeclaredMethod("isUrlWhitelisted", String.class);
  59. isAllowed = (Boolean)isUrlWhiteListed.invoke(url);
  60. } catch (NoSuchMethodException e) {
  61. } catch (IllegalAccessException e) {
  62. } catch (InvocationTargetException e) {
  63. }
  64. if (isAllowed == null) {
  65. try {
  66. Method gpm = webView.getClass().getMethod("getPluginManager");
  67. PluginManager pm = (PluginManager)gpm.invoke(webView);
  68. Method isAllowedMethod = pm.getClass().getMethod("shouldAllowRequest", String.class);
  69. isAllowed = (Boolean)isAllowedMethod.invoke(pm, url);
  70. if (isAllowed == null) {
  71. isAllowed = false;
  72. }
  73. } catch (NoSuchMethodException e) {
  74. } catch (IllegalAccessException e) {
  75. } catch (InvocationTargetException e) {
  76. }
  77. }
  78. callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, isAllowed));
  79. return true;
  80. }
  81. return false;
  82. }
  83. }