|
1 |
| -package com.ReactNativeBlobUtil; |
2 |
| - |
3 |
| -import android.net.Uri; |
4 |
| -import android.util.Base64; |
5 |
| - |
6 |
| -import com.ReactNativeBlobUtil.Utils.PathResolver; |
7 |
| -import com.facebook.react.bridge.Arguments; |
8 |
| -import com.facebook.react.bridge.WritableMap; |
9 |
| -import com.facebook.react.modules.core.DeviceEventManagerModule; |
10 |
| - |
11 |
| -import java.nio.charset.Charset; |
12 |
| -import java.security.MessageDigest; |
13 |
| -import java.util.Locale; |
14 |
| - |
15 |
| -import javax.net.ssl.HostnameVerifier; |
16 |
| -import javax.net.ssl.SSLContext; |
17 |
| -import javax.net.ssl.SSLSession; |
18 |
| -import javax.net.ssl.SSLSocketFactory; |
19 |
| -import javax.net.ssl.TrustManager; |
20 |
| -import javax.net.ssl.X509TrustManager; |
21 |
| - |
22 |
| -import okhttp3.OkHttpClient; |
23 |
| - |
24 |
| -public class ReactNativeBlobUtilUtils { |
25 |
| - |
26 |
| - public static X509TrustManager sharedTrustManager; |
27 |
| - |
28 |
| - public static String getMD5(String input) { |
29 |
| - String result = null; |
30 |
| - |
31 |
| - try { |
32 |
| - MessageDigest md = MessageDigest.getInstance("MD5"); |
33 |
| - md.update(input.getBytes()); |
34 |
| - byte[] digest = md.digest(); |
35 |
| - |
36 |
| - StringBuilder sb = new StringBuilder(); |
37 |
| - |
38 |
| - for (byte b : digest) { |
39 |
| - sb.append(String.format(Locale.ROOT, "%02x", b & 0xff)); |
40 |
| - } |
41 |
| - |
42 |
| - result = sb.toString(); |
43 |
| - } catch (Exception ex) { |
44 |
| - ex.printStackTrace(); |
45 |
| - } finally { |
46 |
| - // TODO: Is discarding errors the intent? (https://www.owasp.org/index.php/Return_Inside_Finally_Block) |
47 |
| - return result; |
48 |
| - } |
49 |
| - |
50 |
| - } |
51 |
| - |
52 |
| - public static void emitWarningEvent(String data) { |
53 |
| - WritableMap args = Arguments.createMap(); |
54 |
| - args.putString("event", "warn"); |
55 |
| - args.putString("detail", data); |
56 |
| - |
57 |
| - // emit event to js context |
58 |
| - ReactNativeBlobUtil.RCTContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) |
59 |
| - .emit(ReactNativeBlobUtilConst.EVENT_MESSAGE, args); |
60 |
| - } |
61 |
| - |
62 |
| - public static OkHttpClient.Builder getUnsafeOkHttpClient(OkHttpClient client) { |
63 |
| - try { |
64 |
| - |
65 |
| - if (sharedTrustManager == null) throw new IllegalStateException("Use of own trust manager but none defined"); |
66 |
| - |
67 |
| - final TrustManager[] trustAllCerts = new TrustManager[]{sharedTrustManager}; |
68 |
| - |
69 |
| - // Install the all-trusting trust manager |
70 |
| - final SSLContext sslContext = SSLContext.getInstance("SSL"); |
71 |
| - sslContext.init(null, trustAllCerts, new java.security.SecureRandom()); |
72 |
| - // Create an ssl socLket factory with our all-trusting manager |
73 |
| - final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory(); |
74 |
| - |
75 |
| - OkHttpClient.Builder builder = client.newBuilder(); |
76 |
| - builder.sslSocketFactory(sslSocketFactory, sharedTrustManager); |
77 |
| - builder.hostnameVerifier(new HostnameVerifier() { |
78 |
| - @Override |
79 |
| - public boolean verify(String hostname, SSLSession session) { |
80 |
| - return true; |
81 |
| - } |
82 |
| - }); |
83 |
| - |
84 |
| - return builder; |
85 |
| - } catch (Exception e) { |
86 |
| - throw new RuntimeException(e); |
87 |
| - } |
88 |
| - } |
89 |
| - |
90 |
| - /** |
91 |
| - * String to byte converter method |
92 |
| - * |
93 |
| - * @param data Raw data in string format |
94 |
| - * @param encoding Decoder name |
95 |
| - * @return Converted data byte array |
96 |
| - */ |
97 |
| - public static byte[] stringToBytes(String data, String encoding) { |
98 |
| - if (encoding.equalsIgnoreCase("ascii")) { |
99 |
| - return data.getBytes(Charset.forName("US-ASCII")); |
100 |
| - } else if (encoding.toLowerCase(Locale.ROOT).contains("base64")) { |
101 |
| - return Base64.decode(data, Base64.NO_WRAP); |
102 |
| - |
103 |
| - } else if (encoding.equalsIgnoreCase("utf8")) { |
104 |
| - return data.getBytes(Charset.forName("UTF-8")); |
105 |
| - } |
106 |
| - return data.getBytes(Charset.forName("US-ASCII")); |
107 |
| - } |
108 |
| - |
109 |
| - /** |
110 |
| - * Normalize the path, remove URI scheme (xxx://) so that we can handle it. |
111 |
| - * |
112 |
| - * @param path URI string. |
113 |
| - * @return Normalized string |
114 |
| - */ |
115 |
| - public static String normalizePath(String path) { |
116 |
| - if (path == null) |
117 |
| - return null; |
118 |
| - if (!path.matches("\\w+\\:.*")) |
119 |
| - return path; |
120 |
| - if (path.startsWith("file://")) { |
121 |
| - return path.replace("file://", ""); |
122 |
| - } |
123 |
| - |
124 |
| - Uri uri = Uri.parse(path); |
125 |
| - if (path.startsWith(ReactNativeBlobUtilConst.FILE_PREFIX_BUNDLE_ASSET)) { |
126 |
| - return path; |
127 |
| - } else |
128 |
| - return PathResolver.getRealPathFromURI(ReactNativeBlobUtil.RCTContext, uri); |
129 |
| - } |
130 |
| - |
131 |
| - public static boolean isAsset(String path) { |
132 |
| - return path != null && path.startsWith(ReactNativeBlobUtilConst.FILE_PREFIX_BUNDLE_ASSET); |
133 |
| - } |
134 |
| - |
135 |
| - public static boolean isContentUri(String path) { |
136 |
| - return path != null && path.startsWith(ReactNativeBlobUtilConst.FILE_PREFIX_CONTENT); |
137 |
| - } |
138 |
| -} |
| 1 | +package com.ReactNativeBlobUtil; |
| 2 | + |
| 3 | +import android.net.Uri; |
| 4 | +import android.util.Base64; |
| 5 | + |
| 6 | +import com.ReactNativeBlobUtil.Utils.PathResolver; |
| 7 | +import com.facebook.react.bridge.Arguments; |
| 8 | +import com.facebook.react.bridge.WritableMap; |
| 9 | +import com.facebook.react.modules.core.DeviceEventManagerModule; |
| 10 | + |
| 11 | +import java.nio.charset.Charset; |
| 12 | +import java.security.MessageDigest; |
| 13 | +import java.util.Locale; |
| 14 | + |
| 15 | +import javax.net.ssl.HostnameVerifier; |
| 16 | +import javax.net.ssl.SSLContext; |
| 17 | +import javax.net.ssl.SSLSession; |
| 18 | +import javax.net.ssl.SSLSocketFactory; |
| 19 | +import javax.net.ssl.TrustManager; |
| 20 | +import javax.net.ssl.X509TrustManager; |
| 21 | + |
| 22 | +import okhttp3.OkHttpClient; |
| 23 | + |
| 24 | +public class ReactNativeBlobUtilUtils { |
| 25 | + |
| 26 | + public static X509TrustManager sharedTrustManager; |
| 27 | + |
| 28 | + public static String getMD5(String input) { |
| 29 | + String result = null; |
| 30 | + |
| 31 | + try { |
| 32 | + MessageDigest md = MessageDigest.getInstance("MD5"); |
| 33 | + md.update(input.getBytes()); |
| 34 | + byte[] digest = md.digest(); |
| 35 | + |
| 36 | + StringBuilder sb = new StringBuilder(); |
| 37 | + |
| 38 | + for (byte b : digest) { |
| 39 | + sb.append(String.format(Locale.ROOT, "%02x", b & 0xff)); |
| 40 | + } |
| 41 | + |
| 42 | + result = sb.toString(); |
| 43 | + } catch (Exception ex) { |
| 44 | + ex.printStackTrace(); |
| 45 | + } finally { |
| 46 | + // TODO: Is discarding errors the intent? (https://www.owasp.org/index.php/Return_Inside_Finally_Block) |
| 47 | + return result; |
| 48 | + } |
| 49 | + |
| 50 | + } |
| 51 | + |
| 52 | + public static void emitWarningEvent(String data) { |
| 53 | + WritableMap args = Arguments.createMap(); |
| 54 | + args.putString("event", "warn"); |
| 55 | + args.putString("detail", data); |
| 56 | + |
| 57 | + // emit event to js context |
| 58 | + ReactNativeBlobUtilImpl.RCTContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) |
| 59 | + .emit(ReactNativeBlobUtilConst.EVENT_MESSAGE, args); |
| 60 | + } |
| 61 | + |
| 62 | + public static OkHttpClient.Builder getUnsafeOkHttpClient(OkHttpClient client) { |
| 63 | + try { |
| 64 | + |
| 65 | + if (sharedTrustManager == null) throw new IllegalStateException("Use of own trust manager but none defined"); |
| 66 | + |
| 67 | + final TrustManager[] trustAllCerts = new TrustManager[]{sharedTrustManager}; |
| 68 | + |
| 69 | + // Install the all-trusting trust manager |
| 70 | + final SSLContext sslContext = SSLContext.getInstance("SSL"); |
| 71 | + sslContext.init(null, trustAllCerts, new java.security.SecureRandom()); |
| 72 | + // Create an ssl socLket factory with our all-trusting manager |
| 73 | + final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory(); |
| 74 | + |
| 75 | + OkHttpClient.Builder builder = client.newBuilder(); |
| 76 | + builder.sslSocketFactory(sslSocketFactory, sharedTrustManager); |
| 77 | + builder.hostnameVerifier(new HostnameVerifier() { |
| 78 | + @Override |
| 79 | + public boolean verify(String hostname, SSLSession session) { |
| 80 | + return true; |
| 81 | + } |
| 82 | + }); |
| 83 | + |
| 84 | + return builder; |
| 85 | + } catch (Exception e) { |
| 86 | + throw new RuntimeException(e); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * String to byte converter method |
| 92 | + * |
| 93 | + * @param data Raw data in string format |
| 94 | + * @param encoding Decoder name |
| 95 | + * @return Converted data byte array |
| 96 | + */ |
| 97 | + public static byte[] stringToBytes(String data, String encoding) { |
| 98 | + if (encoding.equalsIgnoreCase("ascii")) { |
| 99 | + return data.getBytes(Charset.forName("US-ASCII")); |
| 100 | + } else if (encoding.toLowerCase(Locale.ROOT).contains("base64")) { |
| 101 | + return Base64.decode(data, Base64.NO_WRAP); |
| 102 | + |
| 103 | + } else if (encoding.equalsIgnoreCase("utf8")) { |
| 104 | + return data.getBytes(Charset.forName("UTF-8")); |
| 105 | + } |
| 106 | + return data.getBytes(Charset.forName("US-ASCII")); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Normalize the path, remove URI scheme (xxx://) so that we can handle it. |
| 111 | + * |
| 112 | + * @param path URI string. |
| 113 | + * @return Normalized string |
| 114 | + */ |
| 115 | + public static String normalizePath(String path) { |
| 116 | + if (path == null) |
| 117 | + return null; |
| 118 | + if (!path.matches("\\w+\\:.*")) |
| 119 | + return path; |
| 120 | + if (path.startsWith("file://")) { |
| 121 | + return path.replace("file://", ""); |
| 122 | + } |
| 123 | + |
| 124 | + Uri uri = Uri.parse(path); |
| 125 | + if (path.startsWith(ReactNativeBlobUtilConst.FILE_PREFIX_BUNDLE_ASSET)) { |
| 126 | + return path; |
| 127 | + } else |
| 128 | + return PathResolver.getRealPathFromURI(ReactNativeBlobUtilImpl.RCTContext, uri); |
| 129 | + } |
| 130 | + |
| 131 | + public static boolean isAsset(String path) { |
| 132 | + return path != null && path.startsWith(ReactNativeBlobUtilConst.FILE_PREFIX_BUNDLE_ASSET); |
| 133 | + } |
| 134 | + |
| 135 | + public static boolean isContentUri(String path) { |
| 136 | + return path != null && path.startsWith(ReactNativeBlobUtilConst.FILE_PREFIX_CONTENT); |
| 137 | + } |
| 138 | +} |
0 commit comments