|
20 | 20 | package com.aliyun.oss.model; |
21 | 21 |
|
22 | 22 | import com.aliyun.oss.common.utils.StringUtils; |
| 23 | + |
23 | 24 | import java.util.ArrayList; |
24 | 25 | import java.util.HashMap; |
25 | 26 | import java.util.List; |
@@ -77,24 +78,24 @@ public String jsonize() { |
77 | 78 | String jsonizedCond = null; |
78 | 79 | switch (tupleType) { |
79 | 80 | case Two: |
80 | | - jsonizedCond = String.format("{\"%s\":\"%s\"},", name, value); |
| 81 | + jsonizedCond = String.format("{\"%s\":\"%s\"},", jsonEscape(name), jsonEscape(value)); |
81 | 82 | break; |
82 | 83 | case Three: |
83 | 84 | switch (matchMode) { |
84 | 85 | case Exact: |
85 | | - jsonizedCond = String.format("[\"eq\",\"$%s\",\"%s\"],", name, value); |
| 86 | + jsonizedCond = String.format("[\"eq\",\"$%s\",\"%s\"],", jsonEscape(name), jsonEscape(value)); |
86 | 87 | break; |
87 | 88 | case StartWith: |
88 | | - jsonizedCond = String.format("[\"starts-with\",\"$%s\",\"%s\"],", name, value); |
| 89 | + jsonizedCond = String.format("[\"starts-with\",\"$%s\",\"%s\"],", jsonEscape(name), jsonEscape(value)); |
89 | 90 | break; |
90 | 91 | case Range: |
91 | 92 | jsonizedCond = String.format("[\"content-length-range\",%d,%d],", minimum, maximum); |
92 | 93 | break; |
93 | 94 | case In: |
94 | | - jsonizedCond = String.format("[\"in\",\"$%s\",[\"%s\"]],", name, StringUtils.join("\",\"",contain)); |
| 95 | + jsonizedCond = String.format("[\"in\",\"$%s\",[\"%s\"]],", jsonEscape(name), StringUtils.join("\",\"",jsonEscape(contain))); |
95 | 96 | break; |
96 | 97 | case NotIn: |
97 | | - jsonizedCond = String.format("[\"not-in\",\"$%s\",[\"%s\"]],", name, StringUtils.join("\",\"",contain)); |
| 98 | + jsonizedCond = String.format("[\"not-in\",\"$%s\",[\"%s\"]],", jsonEscape(name), StringUtils.join("\",\"",jsonEscape(contain))); |
98 | 99 | break; |
99 | 100 | default: |
100 | 101 | throw new IllegalArgumentException(String.format("Unsupported match mode %s", matchMode.toString())); |
@@ -152,6 +153,67 @@ public long getMaximum() { |
152 | 153 | public void setMaximum(long maximum) { |
153 | 154 | this.maximum = maximum; |
154 | 155 | } |
| 156 | + |
| 157 | + private static String[] jsonEscape(String[] ss) { |
| 158 | + if (ss == null) { |
| 159 | + return null; |
| 160 | + } |
| 161 | + String[] result = new String[ss.length]; |
| 162 | + for (int i = 0; i < ss.length; i++) { |
| 163 | + result[i] = jsonEscape(ss[i]); |
| 164 | + } |
| 165 | + return result; |
| 166 | + } |
| 167 | + |
| 168 | + private static String jsonEscape(String s) { |
| 169 | + if (s == null) { |
| 170 | + return null; |
| 171 | + } |
| 172 | + StringBuffer sb = new StringBuffer(); |
| 173 | + for (int i = 0; i < s.length(); i++) { |
| 174 | + char ch = s.charAt(i); |
| 175 | + switch (ch) { |
| 176 | + case '"': |
| 177 | + sb.append("\\\""); |
| 178 | + break; |
| 179 | + case '\\': |
| 180 | + sb.append("\\\\"); |
| 181 | + break; |
| 182 | + case '\b': |
| 183 | + sb.append("\\b"); |
| 184 | + break; |
| 185 | + case '\f': |
| 186 | + sb.append("\\f"); |
| 187 | + break; |
| 188 | + case '\n': |
| 189 | + sb.append("\\n"); |
| 190 | + break; |
| 191 | + case '\r': |
| 192 | + sb.append("\\r"); |
| 193 | + break; |
| 194 | + case '\t': |
| 195 | + sb.append("\\t"); |
| 196 | + break; |
| 197 | + case '/': |
| 198 | + sb.append("\\/"); |
| 199 | + break; |
| 200 | + default: |
| 201 | + if ((ch >= '\u0000' && ch <= '\u001F') || |
| 202 | + (ch >= '\u007F' && ch <= '\u009F') || |
| 203 | + (ch >= '\u2000' && ch <= '\u20FF')) { |
| 204 | + String ss = Integer.toHexString(ch); |
| 205 | + sb.append("\\u"); |
| 206 | + for (int k = 0; k < 4 - ss.length(); k++) { |
| 207 | + sb.append('0'); |
| 208 | + } |
| 209 | + sb.append(ss.toUpperCase()); |
| 210 | + } else { |
| 211 | + sb.append(ch); |
| 212 | + } |
| 213 | + } |
| 214 | + } |
| 215 | + return sb.toString(); |
| 216 | + } |
155 | 217 | } |
156 | 218 |
|
157 | 219 | /** |
|
0 commit comments