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 package fr.paris.lutece.util.string;
35
36 import fr.paris.lutece.portal.service.util.AppLogService;
37 import fr.paris.lutece.portal.service.util.AppPropertiesService;
38
39 import java.io.BufferedReader;
40 import java.io.ByteArrayInputStream;
41 import java.io.ByteArrayOutputStream;
42 import java.io.IOException;
43 import java.io.InputStreamReader;
44 import java.text.Normalizer;
45 import java.util.zip.GZIPInputStream;
46 import java.util.zip.GZIPOutputStream;
47
48 import org.apache.commons.lang3.StringUtils;
49
50
51
52
53 public final class StringUtil
54 {
55 private static final String PROPERTY_XSS_CHARACTERS = "input.xss.characters";
56 private static final String PROPERTY_MAIL_PATTERN = "mail.accepted.pattern";
57 private static final String STRING_CODE_PATTERN = "^[\\w]+$";
58 private static final String CONSTANT_AT = "@";
59 private static final String CONSTANT_UTF8 = "UTF-8";
60 private static final String EMAIL_PATTERN = "^[\\w_.\\-]+@[\\w_.\\-]+\\.[\\w]+$";
61
62
63 private static char [ ] _aXssCharacters;
64 private static String _xssCharactersAsString;
65
66
67
68
69 private StringUtil( )
70 {
71 }
72
73
74
75
76
77
78
79
80
81
82
83
84 public static String substitute( String strSource, String strValue, String strBookmark )
85 {
86 StringBuilder strResult = new StringBuilder( );
87 int nPos = strSource.indexOf( strBookmark );
88 String strModifySource = strSource;
89
90 while ( nPos != -1 )
91 {
92 strResult.append( strModifySource.substring( 0, nPos ) );
93 strResult.append( strValue );
94 strModifySource = strModifySource.substring( nPos + strBookmark.length( ) );
95 nPos = strModifySource.indexOf( strBookmark );
96 }
97
98 strResult.append( strModifySource );
99
100 return strResult.toString( );
101 }
102
103
104
105
106
107
108
109
110 public static String replaceAccent( String strSource )
111 {
112 String strNormalized = Normalizer.normalize( strSource, Normalizer.Form.NFKD );
113 strNormalized = strNormalized.replaceAll( "\\p{M}", "" );
114
115 return strNormalized;
116 }
117
118
119
120
121
122
123
124
125 public static boolean containsHtmlSpecialCharacters( String strValue )
126 {
127 return ( ( strValue.indexOf( '"' ) > -1 ) || ( strValue.indexOf( '&' ) > -1 ) || ( strValue.indexOf( '<' ) > -1 ) || ( strValue.indexOf( '>' ) > -1 ) );
128 }
129
130
131
132
133
134
135
136
137 public static synchronized boolean containsXssCharacters( String strValue )
138 {
139
140 if ( _aXssCharacters == null )
141 {
142 _aXssCharacters = AppPropertiesService.getProperty( PROPERTY_XSS_CHARACTERS ).toCharArray( );
143 }
144
145 return containsXssCharacters( strValue, _aXssCharacters );
146 }
147
148
149
150
151
152
153
154
155
156
157 public static synchronized boolean containsXssCharacters( String strValue, char [ ] aXssCharacters )
158 {
159
160 boolean bContains = false;
161
162 if ( aXssCharacters != null )
163 {
164 for ( int nIndex = 0; !bContains && ( nIndex < aXssCharacters.length ); nIndex++ )
165 {
166 bContains = strValue.lastIndexOf( aXssCharacters [nIndex] ) >= 0;
167 }
168 }
169
170 return bContains;
171 }
172
173
174
175
176
177
178
179
180
181
182 public static synchronized boolean containsXssCharacters( String strValue, String strXssCharacters )
183 {
184
185 if ( strXssCharacters != null )
186 {
187 return containsXssCharacters( strValue, strXssCharacters.toCharArray( ) );
188 }
189
190 return false;
191 }
192
193
194
195
196
197
198 public static synchronized String getXssCharactersAsString( )
199 {
200
201 if ( _aXssCharacters == null )
202 {
203 _aXssCharacters = AppPropertiesService.getProperty( PROPERTY_XSS_CHARACTERS ).toCharArray( );
204 }
205
206 if ( _xssCharactersAsString == null )
207 {
208 StringBuilder sbfCharList = new StringBuilder( );
209
210 int iIndex;
211
212 for ( iIndex = 0; iIndex < ( _aXssCharacters.length - 1 ); iIndex++ )
213 {
214 sbfCharList.append( _aXssCharacters [iIndex] );
215 sbfCharList.append( ", " );
216 }
217
218
219 sbfCharList.append( _aXssCharacters [iIndex] );
220 _xssCharactersAsString = sbfCharList.toString( );
221 }
222
223 return _xssCharactersAsString;
224 }
225
226
227
228
229
230
231
232
233 public static synchronized boolean checkEmail( String strEmail )
234 {
235 return strEmail.matches( AppPropertiesService.getProperty( PROPERTY_MAIL_PATTERN, EMAIL_PATTERN ) );
236 }
237
238
239
240
241
242
243
244
245
246
247 public static synchronized boolean checkEmailAndDomainName( String strEmail, String [ ] strBannedDomainNames )
248 {
249 boolean bIsValid = strEmail.matches( AppPropertiesService.getProperty( PROPERTY_MAIL_PATTERN, EMAIL_PATTERN ) );
250
251 return bIsValid && checkEmailDomainName( strEmail, strBannedDomainNames );
252 }
253
254
255
256
257
258
259
260
261
262
263 public static synchronized boolean checkEmailDomainName( String strEmail, String [ ] strBannedDomainNames )
264 {
265 if ( ( strBannedDomainNames != null ) && ( strBannedDomainNames.length > 0 ) )
266 {
267 int nOffset;
268
269 if ( strBannedDomainNames [0].contains( CONSTANT_AT ) )
270 {
271 nOffset = 0;
272 }
273 else
274 {
275 nOffset = 1;
276 }
277
278 int nIndex = strEmail.indexOf( CONSTANT_AT );
279
280 if ( ( nIndex >= 0 ) && ( ( nIndex + nOffset ) < strEmail.length( ) ) )
281 {
282 String strDomainName = strEmail.substring( nIndex + nOffset );
283
284 for ( String strDomain : strBannedDomainNames )
285 {
286 if ( strDomainName.equals( strDomain ) )
287 {
288 return false;
289 }
290 }
291 }
292 }
293
294 return true;
295 }
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310 public static synchronized boolean checkCodeKey( String strCodeKey )
311 {
312 return strCodeKey != null && strCodeKey.matches( STRING_CODE_PATTERN );
313 }
314
315
316
317
318
319
320
321
322
323
324 public static int getIntValue( String strValue, int nDefaultValue )
325 {
326 try
327 {
328 return Integer.parseInt( strValue );
329 }
330 catch( NumberFormatException nfe )
331 {
332 AppLogService.error( nfe.getMessage( ), nfe );
333 }
334
335 return nDefaultValue;
336 }
337
338
339
340
341
342
343
344
345 public static boolean isAnyEmpty( String... strings )
346 {
347 for ( String string : strings )
348 {
349 if ( StringUtils.isEmpty( string ) )
350 {
351 return true;
352 }
353 }
354 return false;
355 }
356
357
358
359
360
361
362
363
364 public static byte[] compress(String str) throws IOException {
365
366 if (str == null || str.length() == 0) {
367 return "".getBytes( CONSTANT_UTF8 );
368 }
369
370 ByteArrayOutputStream out = new ByteArrayOutputStream();
371 GZIPOutputStream gzip = new GZIPOutputStream(out);
372 gzip.write( str.getBytes( CONSTANT_UTF8 ) );
373 gzip.close( );
374
375 return out.toByteArray();
376 }
377
378
379
380
381
382
383
384
385 public static String decompress(byte[] bytes) throws IOException {
386 return decompress( bytes, CONSTANT_UTF8);
387 }
388
389
390
391
392
393
394
395
396
397 public static String decompress(byte[] bytes, String encoding) throws IOException {
398
399 if (bytes == null || bytes.length == 0) {
400 return "";
401 }
402
403 GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(bytes));
404
405 ByteArrayOutputStream out = new ByteArrayOutputStream();
406
407 byte[] b = new byte[4096];
408 int len;
409 while ( (len = gis.read( b ) ) >= 0 )
410 {
411 out.write(b, 0, len);
412 }
413
414 return out.toString(encoding);
415 }
416 }