-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathStringUtils.java
More file actions
176 lines (148 loc) · 5.82 KB
/
StringUtils.java
File metadata and controls
176 lines (148 loc) · 5.82 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
// 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
// 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 java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;
import org.owasp.esapi.StringUtilities;
public class StringUtils {
private static final char[] hexChar = {
'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
};
public static String join(Iterable<? extends Object> iterable, String delim) {
StringBuilder sb = new StringBuilder();
if (iterable != null) {
Iterator<? extends Object> iter = iterable.iterator();
if (iter.hasNext()) {
Object next = iter.next();
sb.append(next.toString());
}
while (iter.hasNext()) {
Object next = iter.next();
sb.append(delim + next.toString());
}
}
return sb.toString();
}
public static String join(final String delimiter,
final Object... components) {
return org.apache.commons.lang.StringUtils.join(components, delimiter);
}
public static boolean isNotBlank(String str) {
if (str != null && str.trim().length() > 0) {
return true;
}
return false;
}
/**
* @param tags
* @return List of tags
*/
public static List<String> csvTagsToList(String tags) {
List<String> tagsList = new ArrayList<String>();
if (tags != null) {
String[] tokens = tags.split(",");
for (int i = 0; i < tokens.length; i++) {
tagsList.add(tokens[i].trim());
}
}
return tagsList;
}
/**
* Converts a List of tags to a comma separated list
* @param tags
* @return String containing a comma separated list of tags
*/
public static String listToCsvTags(List<String> tagsList) {
String tags = "";
if (tagsList.size() > 0) {
for (int i = 0; i < tagsList.size(); i++) {
tags += tagsList.get(i);
if (i != tagsList.size() - 1) {
tags += ",";
}
}
}
return tags;
}
public static String getExceptionStackInfo(Throwable e) {
StringBuffer sb = new StringBuffer();
sb.append(e.toString()).append("\n");
StackTraceElement[] elemnents = e.getStackTrace();
for(StackTraceElement element : elemnents) {
sb.append(element.getClassName()).append(".");
sb.append(element.getMethodName()).append("(");
sb.append(element.getFileName()).append(":");
sb.append(element.getLineNumber()).append(")");
sb.append("\n");
}
return sb.toString();
}
public static String unicodeEscape(String s) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
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(String password) {
if(password == null || password.isEmpty())
return "*";
StringBuffer sb = new StringBuffer();
sb.append(password.charAt(0));
for(int i = 1; i < password.length(); i++)
sb.append("*");
return sb.toString();
}
// removes a password request param and it's value
private static final Pattern REGEX_PASSWORD_QUERYSTRING = Pattern.compile("&?password=.*?(?=[&'\"])");
// removes a password property from a response json object
private static final Pattern REGEX_PASSWORD_JSON = Pattern.compile("\"password\":\".*?\",?");
// Responsible for stripping sensitive content from request and response strings
public static String cleanString(String stringToClean){
String cleanResult = "";
cleanResult = REGEX_PASSWORD_QUERYSTRING.matcher(stringToClean).replaceAll("");
cleanResult = REGEX_PASSWORD_JSON.matcher(cleanResult).replaceAll("");
return cleanResult;
}
public static String stripControlCharacters(String s) {
return StringUtilities.stripControls(s);
}
public static int formatForOutput(String text, int start, int columns, char separator) {
if (start >= text.length()) {
return -1;
}
int end = start + columns;
if (end > text.length()) {
end = text.length();
}
String searchable = text.substring(start, end);
int found = searchable.lastIndexOf(separator);
return found > 0 ? found : end - start;
}
}