|
| 1 | +/* |
| 2 | + * arcus-java-client : Arcus Java client |
| 3 | + * Copyright 2010-2014 NAVER Corp. |
| 4 | + * Copyright 2014-present JaM2in Co., Ltd. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package net.spy.memcached; |
| 19 | + |
| 20 | +import java.util.Collection; |
| 21 | + |
| 22 | +/** |
| 23 | + * Validator for memcached keys. |
| 24 | + */ |
| 25 | +public final class KeyValidator { |
| 26 | + |
| 27 | + public static final int MAX_KEY_LENGTH = 4000; |
| 28 | + public static final int MAX_MKEY_LENGTH = 250; |
| 29 | + private static final int MAX_BKEY_BYTE_ARRAY_LENGTH = 31; |
| 30 | + |
| 31 | + private final byte delimiter; |
| 32 | + |
| 33 | + public KeyValidator(byte delimiter) { |
| 34 | + this.delimiter = delimiter; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Validate cache key. |
| 39 | + * |
| 40 | + * @param key the cache key to validate |
| 41 | + * @throws IllegalArgumentException if the key is invalid |
| 42 | + */ |
| 43 | + public void validateKey(String key) { |
| 44 | + boolean hasPrefix = false; |
| 45 | + |
| 46 | + byte[] keyBytes = KeyUtil.getKeyBytes(key); |
| 47 | + if (keyBytes.length > MAX_KEY_LENGTH) { |
| 48 | + throw new IllegalArgumentException("Key is too long (maxlen = " |
| 49 | + + MAX_KEY_LENGTH + ")"); |
| 50 | + } else if (keyBytes.length == 0) { |
| 51 | + throw new IllegalArgumentException( |
| 52 | + "Key must contain at least one character."); |
| 53 | + } |
| 54 | + // Validate the key |
| 55 | + for (byte b : keyBytes) { |
| 56 | + if (b == ' ' || b == '\n' || b == '\r' || b == 0) { |
| 57 | + throw new IllegalArgumentException( |
| 58 | + "Key contains invalid characters: ``" + key + "''"); |
| 59 | + } |
| 60 | + if (b == delimiter) { |
| 61 | + hasPrefix = true; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + // Validate the prefix |
| 66 | + if (hasPrefix) { |
| 67 | + if (keyBytes[0] == '-') { |
| 68 | + throw new IllegalArgumentException( |
| 69 | + "Key contains invalid prefix: ``" + key + "''"); |
| 70 | + } |
| 71 | + for (byte b : keyBytes) { |
| 72 | + if (b == delimiter) { |
| 73 | + break; |
| 74 | + } |
| 75 | + if (!(('a' <= b && b <= 'z') || ('A' <= b && b <= 'Z') || |
| 76 | + ('0' <= b && b <= '9') || |
| 77 | + (b == '_') || (b == '-') || (b == '+') || (b == '.'))) { |
| 78 | + throw new IllegalArgumentException( |
| 79 | + "Key contains invalid prefix: ``" + key + "''"); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Validate cache keys. |
| 87 | + * |
| 88 | + * @param keyList the cache keys to validate |
| 89 | + * @throws IllegalArgumentException if the key list is null, empty, or contains invalid keys |
| 90 | + */ |
| 91 | + public void validateKey(Collection<String> keyList) { |
| 92 | + if (keyList == null) { |
| 93 | + throw new IllegalArgumentException("Key list is null."); |
| 94 | + } else if (keyList.isEmpty()) { |
| 95 | + throw new IllegalArgumentException("Key list is empty."); |
| 96 | + } |
| 97 | + |
| 98 | + for (String key : keyList) { |
| 99 | + validateKey(key); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Validate map key. |
| 105 | + * |
| 106 | + * @param mkey the mkey to validate |
| 107 | + * @throws IllegalArgumentException if the mkey is invalid |
| 108 | + */ |
| 109 | + public static void validateMKey(String mkey) { |
| 110 | + if (mkey == null) { |
| 111 | + throw new IllegalArgumentException("mkey is null"); |
| 112 | + } |
| 113 | + |
| 114 | + byte[] keyBytes = KeyUtil.getKeyBytes(mkey); |
| 115 | + if (keyBytes.length > MAX_MKEY_LENGTH) { |
| 116 | + throw new IllegalArgumentException("MKey is too long (maxlen = " |
| 117 | + + MAX_MKEY_LENGTH + ")"); |
| 118 | + } |
| 119 | + if (keyBytes.length == 0) { |
| 120 | + throw new IllegalArgumentException("MKey must contain at least one character."); |
| 121 | + } |
| 122 | + // Validate the mkey |
| 123 | + for (byte b : keyBytes) { |
| 124 | + if (b == ' ' || b == '\n' || b == '\r' || b == 0) { |
| 125 | + throw new IllegalArgumentException("MKey contains invalid characters: ``" |
| 126 | + + mkey + "''"); |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Validate map keys. |
| 133 | + * |
| 134 | + * @param mkeyList the mkeys to validate |
| 135 | + * @throws IllegalArgumentException if the mkey list is null, empty, or contains invalid mkeys |
| 136 | + */ |
| 137 | + public static void validateMKey(Collection<String> mkeyList) { |
| 138 | + if (mkeyList == null) { |
| 139 | + throw new IllegalArgumentException("mkeyList is null."); |
| 140 | + } else if (mkeyList.isEmpty()) { |
| 141 | + throw new IllegalArgumentException("mkeyList is empty."); |
| 142 | + } |
| 143 | + |
| 144 | + for (String mkey : mkeyList) { |
| 145 | + validateMKey(mkey); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Validate byte type bkeys. |
| 151 | + * |
| 152 | + * @param bkeys the bkeys to validate |
| 153 | + * @throws IllegalArgumentException if the bkey is invalid |
| 154 | + */ |
| 155 | + public static void validateBKey(byte[]... bkeys) { |
| 156 | + for (byte[] bkey : bkeys) { |
| 157 | + if (bkey == null) { |
| 158 | + throw new IllegalArgumentException("bkey is null"); |
| 159 | + } |
| 160 | + if (bkey.length > MAX_BKEY_BYTE_ARRAY_LENGTH) { |
| 161 | + throw new IllegalArgumentException("bkey size exceeded 31"); |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Validate long type bkeys. |
| 168 | + * |
| 169 | + * @param bkeys the bkeys to validate |
| 170 | + * @throws IllegalArgumentException if the bkey is invalid |
| 171 | + */ |
| 172 | + public static void validateBKey(long... bkeys) { |
| 173 | + for (long bkey : bkeys) { |
| 174 | + if (bkey < 0) { |
| 175 | + throw new IllegalArgumentException( |
| 176 | + String.format("not supported unsigned long bkey : %s, use byte array bkey", bkey)); |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | +} |
0 commit comments