Repository navigation
Expand file tree
/
Copy pathStringUtils.java
More file actions
464 lines (399 loc) · 17.8 KB
/
Copy pathStringUtils.java
File metadata and controls
464 lines (399 loc) · 17.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//
package com.cloud.utils;
import com.cloud.utils.exception.CloudRuntimeException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class StringUtils extends org.apache.commons.lang3.StringUtils {
private static final char[] hexChar = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
private static final Pattern PASSWORD_FIELD_PATTERN = Pattern.compile("\\\"password\\\":\\\"([^\\\"]*)\\\"+");
private static final Charset preferredACSCharset;
private static final String UTF8 = "UTF-8";
static {
if (isUtf8Supported()) {
preferredACSCharset = StandardCharsets.UTF_8;
} else {
preferredACSCharset = Charset.defaultCharset();
}
}
public static Charset getPreferredCharset() {
return preferredACSCharset;
}
public static boolean isUtf8Supported() {
return Charset.isSupported(UTF8);
}
protected static Charset getDefaultCharset() {
return Charset.defaultCharset();
}
public static String cleanupTags(String tags) {
if (tags != null) {
final String[] tokens = tags.split(",");
final StringBuilder t = new StringBuilder();
for (String token : tokens) {
t.append(token.trim()).append(",");
}
t.delete(t.length() - 1, t.length());
tags = t.toString();
}
return tags;
}
/**
* @param tags a {code}String{code} containing a list of comma separated tags
* @return List of tags
*/
public static List<String> csvTagsToList(final String tags) {
final List<String> tagsList = new ArrayList<>();
if (tags != null) {
final String[] tokens = tags.split(",");
for (String token : tokens) {
tagsList.add(token.trim());
}
}
return tagsList;
}
/**
* Converts a List of tags to a comma separated list
* @param tagsList List of tags to convert to a comma separated list in a {code}String{code}
* @return String containing a comma separated list of tags
*/
public static String listToCsvTags(final List<String> tagsList) {
final StringBuilder tags = new StringBuilder();
if (!tagsList.isEmpty()) {
for (int i = 0; i < tagsList.size(); i++) {
tags.append(tagsList.get(i));
if (i != tagsList.size() - 1) {
tags.append(',');
}
}
}
return tags.toString();
}
public static String unicodeEscape(final String s) {
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
final char c = s.charAt(i);
if (c >> 7 > 0) {
sb.append("\\u");
sb.append(hexChar[c >> 12 & 0xF]); // append the hex character for the left-most 4-bits
sb.append(hexChar[c >> 8 & 0xF]); // hex for the second group of 4-bits from the left
sb.append(hexChar[c >> 4 & 0xF]); // hex for the third group
sb.append(hexChar[c & 0xF]); // hex for the last group, e.g., the right most 4-bits
} else {
sb.append(c);
}
}
return sb.toString();
}
public static String getMaskedPasswordForDisplay(final String password) {
if (password == null || password.isEmpty()) {
return "*";
}
return password.charAt(0) +
"*".repeat(password.length() - 1);
}
// removes a password request param and it's value, also considering password is in query parameter value which has been url encoded
private static final Pattern REGEX_PASSWORD_QUERYSTRING = Pattern.compile("(&|%26)?[^(&|%26)]*(([pP])assword|accesskey|secretkey)(=|%3D).*?(?=(%26|[&'\"]|$))");
// removes a password/accesskey/ property from a response json object
private static final Pattern REGEX_PASSWORD_JSON = Pattern.compile("\"(([pP])assword|privatekey|accesskey|secretkey)\":\\s?\".*?\",?");
private static final Pattern REGEX_PASSWORD_DETAILS = Pattern.compile("(&|%26)?details(\\[|%5B)\\d*(\\]|%5D)\\.key(=|%3D)(([pP])assword|accesskey|secretkey)(?=(%26|[&'\"]))");
private static final Pattern REGEX_PASSWORD_DETAILS_INDEX = Pattern.compile("details(\\[|%5B)\\d*(\\]|%5D)");
private static final Pattern REGEX_SESSION_KEY = Pattern.compile("sessionkey=[A-Za-z0-9_-]+");
private static final Pattern REGEX_REDUNDANT_AND = Pattern.compile("(&|%26)(&|%26)+");
// Responsible for stripping sensitive content from request and response strings
public static String cleanString(final String stringToClean) {
String cleanResult = "";
if (stringToClean != null) {
cleanResult = REGEX_PASSWORD_QUERYSTRING.matcher(stringToClean).replaceAll("");
cleanResult = REGEX_PASSWORD_JSON.matcher(cleanResult).replaceAll("");
cleanResult = REGEX_SESSION_KEY.matcher(cleanResult).replaceAll("");
final Matcher detailsMatcher = REGEX_PASSWORD_DETAILS.matcher(cleanResult);
while (detailsMatcher.find()) {
final Matcher detailsIndexMatcher = REGEX_PASSWORD_DETAILS_INDEX.matcher(detailsMatcher.group());
if (detailsIndexMatcher.find()) {
cleanResult = cleanDetails(cleanResult, detailsIndexMatcher.group());
}
}
}
return cleanResult;
}
public static String cleanDetails(final String stringToClean, final String detailsIndexString) {
String cleanResult = stringToClean;
for (final String log : stringToClean.split("&|%26")) {
if (log.contains(detailsIndexString)) {
cleanResult = cleanResult.replace(log, "");
}
}
cleanResult = REGEX_REDUNDANT_AND.matcher(cleanResult).replaceAll("&");
return cleanResult;
}
public static boolean areTagsEqual(final String tags1, final String tags2) {
if (tags1 == null && tags2 == null) {
return true;
}
if (tags1 == null ^ tags2 == null) {
return false;
}
final String delimiter = ",";
final List<String> lstTags1 = new ArrayList<>();
final String[] aTags1 = tags1.split(delimiter);
for (final String tag1 : aTags1) {
lstTags1.add(tag1.toLowerCase());
}
final List<String> lstTags2 = new ArrayList<>();
final String[] aTags2 = tags2.split(delimiter);
for (final String tag2 : aTags2) {
lstTags2.add(tag2.toLowerCase());
}
return lstTags1.containsAll(lstTags2) && lstTags2.containsAll(lstTags1);
}
public static Map<String, String> stringToMap(final String s) {
final Map<String, String> map = new HashMap<>();
final String[] elements = s.split(";");
for (final String parts : elements) {
final String[] keyValue = parts.split(":");
map.put(keyValue[0], keyValue[1]);
}
return map;
}
public static String mapToString(final Map<String, String> map) {
StringBuilder s = new StringBuilder();
for (final Map.Entry<String, String> entry : map.entrySet()) {
s.append(entry.getKey()).append(":").append(entry.getValue()).append(";");
}
if (s.length() > 0) {
s = new StringBuilder(s.substring(0, s.length() - 1));
}
return s.toString();
}
public static <T> List<T> applyPagination(final List<T> originalList, final Long startIndex, final Long pageSizeVal) {
// Most likely pageSize will never exceed int value, and we need integer to partition the listToReturn
final boolean applyPagination = startIndex != null && pageSizeVal != null
&& startIndex <= Integer.MAX_VALUE && startIndex >= 0 && pageSizeVal <= Integer.MAX_VALUE
&& pageSizeVal > 0;
List<T> listWPagination = null;
if (applyPagination) {
listWPagination = new ArrayList<>();
final int index = startIndex.intValue() == 0 ? 0 : startIndex.intValue() / pageSizeVal.intValue();
final List<List<T>> partitions = StringUtils.partitionList(originalList, pageSizeVal.intValue());
if (index < partitions.size()) {
listWPagination = partitions.get(index);
}
}
return listWPagination;
}
private static <T> List<List<T>> partitionList(final List<T> originalList, final int chunkSize) {
final List<List<T>> listOfChunks = new ArrayList<>();
for (int i = 0; i < originalList.size() / chunkSize; i++) {
listOfChunks.add(originalList.subList(i * chunkSize, i * chunkSize + chunkSize));
}
if (originalList.size() % chunkSize != 0) {
listOfChunks.add(originalList.subList(originalList.size() - originalList.size() % chunkSize, originalList.size()));
}
return listOfChunks;
}
public static String toCSVList(final List<String> csvList) {
return org.apache.commons.lang3.StringUtils.defaultString(org.apache.commons.lang3.StringUtils.join(csvList, ","));
}
public static Pair<String, String> getKeyValuePairWithSeparator(String keyValuePair, String separator) {
final int index = keyValuePair.indexOf(separator);
final String key = keyValuePair.substring(0, index);
final String value = keyValuePair.substring(index + 1);
return new Pair<>(key.trim(), value.trim());
}
public static Map<String, String> parseJsonToMap(String jsonString) {
ObjectMapper objectMapper = new ObjectMapper();
Map<String, String> mapResult = new HashMap<>();
if (org.apache.commons.lang3.StringUtils.isNotBlank(jsonString)) {
try {
JsonNode jsonNode = objectMapper.readTree(jsonString);
jsonNode.fields().forEachRemaining(entry -> mapResult.put(entry.getKey(), entry.getValue().asText()));
} catch (Exception e) {
throw new CloudRuntimeException("Error while parsing json to convert it to map " + e.getMessage());
}
}
return mapResult;
}
/**
* Converts the comma separated numbers to ranges for any consecutive numbers in the input with numbers (and ranges)
* Eg: "198,200-203,299,300,301,303,304,305,306,307,308,311,197" to "197-198,200-203,299-301,303-308,311"
* @param inputNumbersAndRanges
* @return String containing a converted ranges for any consecutive numbers
*/
public static String numbersToRange(String inputNumbersAndRanges) {
Set<Integer> numberSet = new TreeSet<>();
for (String inputNumber : inputNumbersAndRanges.split(",")) {
inputNumber = inputNumber.trim();
if (inputNumber.contains("-")) {
String[] range = inputNumber.split("-");
if (range.length == 2 && range[0] != null && range[1] != null) {
int start = NumbersUtil.parseInt(range[0], 0);
int end = NumbersUtil.parseInt(range[1], 0);
for (int i = start; i <= end; i++) {
numberSet.add(i);
}
}
} else {
numberSet.add(NumbersUtil.parseInt(inputNumber, 0));
}
}
StringBuilder result = new StringBuilder();
if (!numberSet.isEmpty()) {
List<Integer> numbers = new ArrayList<>(numberSet);
int startNumber = numbers.get(0);
int endNumber = startNumber;
for (int i = 1; i < numbers.size(); i++) {
if (numbers.get(i) == endNumber + 1) {
endNumber = numbers.get(i);
} else {
appendRange(result, startNumber, endNumber);
startNumber = endNumber = numbers.get(i);
}
}
appendRange(result, startNumber, endNumber);
}
return result.toString();
}
private static void appendRange(StringBuilder sb, int startNumber, int endNumber) {
if (sb.length() > 0) {
sb.append(",");
}
if (startNumber == endNumber) {
sb.append(startNumber);
} else {
sb.append(startNumber).append("-").append(endNumber);
}
}
/**
* Converts the comma separated numbers and ranges to numbers
* Eg: "197-198,200-203,299-301,303-308,311" to "197,198,200,201,202,203,299,300,301,303,304,305,306,307,308,311"
* @param inputNumbersAndRanges
* @return String containing a converted numbers
*/
public static String rangeToNumbers(String inputNumbersAndRanges) {
Set<Integer> numberSet = new TreeSet<>();
for (String inputNumber : inputNumbersAndRanges.split(",")) {
inputNumber = inputNumber.trim();
if (inputNumber.contains("-")) {
String[] range = inputNumber.split("-");
int startNumber = Integer.parseInt(range[0]);
int endNumber = Integer.parseInt(range[1]);
for (int i = startNumber; i <= endNumber; i++) {
numberSet.add(i);
}
} else {
numberSet.add(Integer.parseInt(inputNumber));
}
}
StringBuilder result = new StringBuilder();
for (int number : numberSet) {
if (result.length() > 0) {
result.append(",");
}
result.append(number);
}
return result.toString();
}
public static String[] splitCommaSeparatedStrings(String... tags) {
StringBuilder sb = new StringBuilder();
for (String tag : tags) {
if (tag != null && !tag.isEmpty()) {
if (sb.length() > 0) {
sb.append(",");
}
sb.append(tag);
}
}
String appendedTags = sb.toString();
String[] finalMergedTagsArray = appendedTags.split(",");
return finalMergedTagsArray;
}
/**
* Converts the comma separated numbers and ranges to numbers
* @param originalString the original string (can be null or empty) containing list of comma separated values that has to be updated
* @param value the value to add to, or remove from the original string
* @param add if true, adds the input value; if false, removes it
* @return String containing the modified original string (or null if empty)
*/
public static String updateCommaSeparatedStringWithValue(String originalString, String value, boolean add) {
if (org.apache.commons.lang3.StringUtils.isEmpty(value)) {
return originalString;
}
Set<String> values = new LinkedHashSet<>();
if (org.apache.commons.lang3.StringUtils.isNotEmpty(originalString)) {
values.addAll(Arrays.stream(originalString.split(","))
.map(String::trim)
.filter(s -> !s.isEmpty())
.collect(Collectors.toList()));
}
if (add) {
values.add(value);
} else {
values.remove(value);
}
return values.isEmpty() ? null : String.join(",", values);
}
/**
* Returns the first value from a comma-separated string.
* @param inputString the input string (can be null or empty) containing list of comma separated values
* @return the first value, or null if none found
*/
public static String getFirstValueFromCommaSeparatedString(String inputString) {
if (org.apache.commons.lang3.StringUtils.isEmpty(inputString)) {
return inputString;
}
String[] values = inputString.split(",");
if (values.length > 0) {
return values[0].trim();
}
return null;
}
public static String obfuscatePasswordInJsonLikeString(String result) {
if (isEmpty(result)) {
return result;
}
Matcher matcher = PASSWORD_FIELD_PATTERN.matcher(result);
StringBuilder obfuscatedResult = new StringBuilder();
while (matcher.find()) {
String password = matcher.group(1);
String replacement = "\"password\":\"" + obfuscatePasswordValue(password) + "\"";
matcher.appendReplacement(obfuscatedResult, Matcher.quoteReplacement(replacement));
}
matcher.appendTail(obfuscatedResult);
return obfuscatedResult.toString();
}
private static String obfuscatePasswordValue(String password) {
if (isEmpty(password)) {
return password;
}
return password.charAt(0) + "*****";
}
}