|
| 1 | +package com.deploygate.sdk; |
| 2 | + |
| 3 | +import com.deploygate.sdk.internal.Logger; |
| 4 | + |
| 5 | +import org.json.JSONException; |
| 6 | +import org.json.JSONObject; |
| 7 | + |
| 8 | +import java.util.regex.Pattern; |
| 9 | + |
| 10 | +/** |
| 11 | + * This class provides store key-value pairs. |
| 12 | + * These methods are thread-safe. |
| 13 | + */ |
| 14 | +public final class CustomAttributes { |
| 15 | + |
| 16 | + private static final String TAG = "CustomAttributes"; |
| 17 | + |
| 18 | + private static final int MAX_ATTRIBUTES_SIZE = 8; |
| 19 | + private static final Pattern VALID_KEY_PATTERN = Pattern.compile("^[a-z][_a-z0-9]{2,31}$"); |
| 20 | + private static final int MAX_VALUE_LENGTH = 64; |
| 21 | + |
| 22 | + private final Object mLock; |
| 23 | + private JSONObject attributes; |
| 24 | + |
| 25 | + CustomAttributes() { |
| 26 | + mLock = new Object(); |
| 27 | + attributes = new JSONObject(); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Put a string value with the key. |
| 32 | + * If the key already exists, the value will be overwritten. |
| 33 | + * @param key key must be non-null and match the valid pattern. |
| 34 | + * @param value value must be non-null and its length must be less than 64. |
| 35 | + * @return true if the value is put successfully, otherwise false. |
| 36 | + * @see CustomAttributes#VALID_KEY_PATTERN |
| 37 | + */ |
| 38 | + public boolean putString(String key, String value) { |
| 39 | + return putInternal(key, value); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Put an int value with the key. |
| 44 | + * If the key already exists, the value will be overwritten. |
| 45 | + * @param key key must be non-null and match the valid pattern. |
| 46 | + * @param value int value |
| 47 | + * @return true if the value is put successfully, otherwise false. |
| 48 | + * @see CustomAttributes#VALID_KEY_PATTERN |
| 49 | + */ |
| 50 | + public boolean putInt(String key, int value) { |
| 51 | + return putInternal(key, value); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Put a long value with the key. |
| 56 | + * If the key already exists, the value will be overwritten. |
| 57 | + * @param key key must be non-null and match the valid pattern. |
| 58 | + * @param value long value |
| 59 | + * @return true if the value is put successfully, otherwise false. |
| 60 | + * @see CustomAttributes#VALID_KEY_PATTERN |
| 61 | + */ |
| 62 | + public boolean putLong(String key, long value) { |
| 63 | + return putInternal(key, value); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Put a float value with the key. |
| 68 | + * If the key already exists, the value will be overwritten. |
| 69 | + * @param key key must be non-null and match the valid pattern. |
| 70 | + * @param value float value |
| 71 | + * @return true if the value is put successfully, otherwise false. |
| 72 | + * @see CustomAttributes#VALID_KEY_PATTERN |
| 73 | + */ |
| 74 | + public boolean putFloat(String key, float value) { |
| 75 | + return putInternal(key, value); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Put a double value with the key. |
| 80 | + * If the key already exists, the value will be overwritten. |
| 81 | + * @param key key must be non-null and match the valid pattern. |
| 82 | + * @param value double value |
| 83 | + * @return true if the value is put successfully, otherwise false. |
| 84 | + * @see CustomAttributes#VALID_KEY_PATTERN |
| 85 | + */ |
| 86 | + public boolean putDouble(String key, double value) { |
| 87 | + return putInternal(key, value); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Put a boolean value with the key. |
| 92 | + * If the key already exists, the value will be overwritten. |
| 93 | + * @param key key must be non-null and match the valid pattern. |
| 94 | + * @param value boolean value |
| 95 | + * @return true if the value is put successfully, otherwise false. |
| 96 | + * @see CustomAttributes#VALID_KEY_PATTERN |
| 97 | + */ |
| 98 | + public boolean putBoolean(String key, boolean value) { |
| 99 | + return putInternal(key, value); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Remove the value with the key. |
| 104 | + * @param key name of the key to be removed. |
| 105 | + */ |
| 106 | + public void remove(String key) { |
| 107 | + synchronized (mLock) { |
| 108 | + attributes.remove(key); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Remove all key-value pairs. |
| 114 | + */ |
| 115 | + public void removeAll() { |
| 116 | + synchronized (mLock) { |
| 117 | + // recreate new object instead of removing all keys |
| 118 | + attributes = new JSONObject(); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + int size() { |
| 123 | + synchronized (mLock) { |
| 124 | + return attributes.length(); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + String getJSONString() { |
| 129 | + synchronized (mLock) { |
| 130 | + return attributes.toString(); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + private boolean putInternal(String key, Object value) { |
| 135 | + if (!isValidKey(key)) { |
| 136 | + return false; |
| 137 | + } |
| 138 | + |
| 139 | + if (!isValidValue(value)) { |
| 140 | + return false; |
| 141 | + } |
| 142 | + |
| 143 | + synchronized (mLock) { |
| 144 | + try { |
| 145 | + attributes.put(key, value); |
| 146 | + |
| 147 | + if (attributes.length() > MAX_ATTRIBUTES_SIZE) { |
| 148 | + // rollback put operation |
| 149 | + attributes.remove(key); |
| 150 | + Logger.w(TAG, "Attributes already reached max size. Ignored: " + key); |
| 151 | + return false; |
| 152 | + } |
| 153 | + } catch (JSONException e) { |
| 154 | + Logger.w(TAG, "Failed to put attribute: " + key, e); |
| 155 | + return false; |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + return true; |
| 160 | + } |
| 161 | + |
| 162 | + private boolean isValidKey(String key) { |
| 163 | + if (key == null || key.equals("true") || key.equals("false") || key.equals("null")) { |
| 164 | + Logger.w(TAG, "Not allowed key: " + key); |
| 165 | + return false; |
| 166 | + } |
| 167 | + |
| 168 | + if (!VALID_KEY_PATTERN.matcher(key).matches()) { |
| 169 | + Logger.w(TAG, "Invalid key: " + key); |
| 170 | + return false; |
| 171 | + } |
| 172 | + |
| 173 | + return true; |
| 174 | + } |
| 175 | + |
| 176 | + private boolean isValidValue(Object value) { |
| 177 | + if (value == null) { |
| 178 | + Logger.w(TAG, "Value is null"); |
| 179 | + return false; |
| 180 | + } |
| 181 | + |
| 182 | + if (value instanceof String && ((String) value).length() > MAX_VALUE_LENGTH) { |
| 183 | + Logger.w(TAG, "Value too long: " + value); |
| 184 | + return false; |
| 185 | + } else if (value instanceof String || value instanceof Number || value instanceof Boolean) { |
| 186 | + return true; |
| 187 | + } else { |
| 188 | + // dead code |
| 189 | + Logger.w(TAG, "Invalid value: " + value); |
| 190 | + return false; |
| 191 | + } |
| 192 | + } |
| 193 | +} |
0 commit comments