1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.mail2.core;
19
20 import java.nio.charset.StandardCharsets;
21 import java.util.BitSet;
22 import java.util.Collection;
23 import java.util.Locale;
24 import java.util.Map;
25 import java.util.Random;
26
27
28
29
30
31
32
33
34
35
36
37
38 public final class EmailUtils {
39
40
41
42
43 private static final Random RANDOM = new Random();
44
45
46
47
48 private static final int RADIX = 16;
49
50
51
52
53 private static final char ESCAPE_CHAR = '%';
54
55
56
57
58 private static final BitSet SAFE_URL = new BitSet(256);
59
60
61 static {
62
63 for (int i = 'a'; i <= 'z'; i++) {
64 SAFE_URL.set(i);
65 }
66 for (int i = 'A'; i <= 'Z'; i++) {
67 SAFE_URL.set(i);
68 }
69
70 for (int i = '0'; i <= '9'; i++) {
71 SAFE_URL.set(i);
72 }
73
74
75 SAFE_URL.set('-');
76 SAFE_URL.set('_');
77 SAFE_URL.set('.');
78 SAFE_URL.set('*');
79 SAFE_URL.set('+');
80 SAFE_URL.set('$');
81 SAFE_URL.set('!');
82 SAFE_URL.set('\'');
83 SAFE_URL.set('(');
84 SAFE_URL.set(')');
85 SAFE_URL.set(',');
86 SAFE_URL.set('@');
87 }
88
89
90
91
92
93
94
95
96 public static String encodeUrl(final String input) {
97 if (input == null) {
98 return null;
99 }
100 final StringBuilder builder = new StringBuilder();
101 for (final byte c : input.getBytes(StandardCharsets.US_ASCII)) {
102 final int b = c & 0xff;
103 if (SAFE_URL.get(b)) {
104 builder.append((char) b);
105 } else {
106 builder.append(ESCAPE_CHAR);
107 final char hex1 = Character.toUpperCase(Character.forDigit(b >> 4 & 0xF, RADIX));
108 final char hex2 = Character.toUpperCase(Character.forDigit(b & 0xF, RADIX));
109 builder.append(hex1);
110 builder.append(hex2);
111 }
112 }
113 return builder.toString();
114 }
115
116 public static boolean isEmpty(final Collection<?> collection) {
117 return collection == null || collection.isEmpty();
118 }
119
120 public static boolean isEmpty(final Map<?, ?> map) {
121 return map == null || map.isEmpty();
122 }
123
124 public static boolean isEmpty(final Object[] array) {
125 return array == null || array.length == 0;
126 }
127
128
129
130
131
132
133
134
135
136
137 public static boolean isEmpty(final String str) {
138 return str == null || str.isEmpty();
139 }
140
141
142
143
144
145
146
147
148
149
150 public static boolean isNotEmpty(final String str) {
151 return str != null && !str.isEmpty();
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 private static String random(int count, int start, int end, final boolean letters, final boolean numbers, final char[] chars, final Random random) {
182 if (count == 0) {
183 return "";
184 }
185 if (count < 0) {
186 throw new IllegalArgumentException("Requested random string length " + count + " is less than 0.");
187 }
188
189 if (start == 0 && end == 0) {
190 end = 'z' + 1;
191 start = ' ';
192
193 if (!letters && !numbers) {
194 start = 0;
195 end = Integer.MAX_VALUE;
196 }
197 }
198
199 final StringBuilder buffer = new StringBuilder();
200 final int gap = end - start;
201
202 while (count-- != 0) {
203 char ch;
204
205 if (chars == null) {
206 ch = (char) (random.nextInt(gap) + start);
207 } else {
208 ch = chars[random.nextInt(gap) + start];
209 }
210
211 if (letters && numbers && Character.isLetterOrDigit(ch) || letters && Character.isLetter(ch) || numbers && Character.isDigit(ch)
212 || !letters && !numbers) {
213 buffer.append(ch);
214 } else {
215 count++;
216 }
217 }
218
219 return buffer.toString();
220 }
221
222
223
224
225
226
227
228
229
230
231
232
233
234 public static String randomAlphabetic(final int count) {
235 return random(count, 0, 0, true, false, null, RANDOM);
236 }
237
238
239
240
241
242
243
244 public static String replaceEndOfLineCharactersWithSpaces(final String input) {
245 return input == null ? null : input.replace('\n', ' ').replace('\r', ' ');
246 }
247
248 public static String toLower(final String value) {
249 return value.toLowerCase(Locale.ROOT);
250 }
251
252
253
254
255 private EmailUtils() {
256 }
257 }