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.test;
35
36 import static org.junit.Assert.fail;
37
38 import java.io.BufferedReader;
39 import java.io.ByteArrayInputStream;
40 import java.io.IOException;
41 import java.io.InputStream;
42 import java.io.InputStreamReader;
43 import java.io.StringWriter;
44 import java.io.Writer;
45 import java.math.BigInteger;
46 import java.nio.charset.StandardCharsets;
47 import java.security.SecureRandom;
48 import java.util.HashMap;
49 import java.util.Locale;
50 import java.util.Map;
51 import java.util.Random;
52
53 import javax.servlet.http.HttpServletRequest;
54
55 import com.fasterxml.jackson.databind.JsonNode;
56 import com.fasterxml.jackson.databind.ObjectMapper;
57 import org.xml.sax.SAXException;
58
59 import fr.paris.lutece.portal.business.right.Right;
60 import fr.paris.lutece.portal.business.user.AdminUser;
61 import nu.validator.client.EmbeddedValidator;
62
63
64
65
66 public class Utils
67 {
68 private static Utils#Utils">Utils _singleton = new Utils( );
69
70 private static final String ATTRIBUTE_ADMIN_USER = "lutece_admin_user";
71
72 private static final Random _rand = new SecureRandom( );
73
74
75
76
77
78
79
80 public static void registerAdminUserWithRigth( HttpServletRequest request, AdminUser user, String strRight )
81 {
82 Map<String, Right> mapRights = new HashMap<String, Right>( );
83 Right right = new Right( );
84 right.setId( strRight );
85 mapRights.put( strRight, right );
86 user.setRights( mapRights );
87
88
89 user.setLocale( new Locale( "fr", "FR", "" ) );
90 registerAdminUser( request, user );
91 }
92
93
94
95
96
97
98 public static void registerAdminUser( HttpServletRequest request, AdminUser user )
99 {
100 request.getSession( true ).setAttribute( ATTRIBUTE_ADMIN_USER, user );
101 }
102
103
104
105
106
107
108
109
110
111
112 public static String getFileContent( String strFilename ) throws IOException
113 {
114 InputStream is = _singleton.getClass( ).getResourceAsStream( "/" + strFilename );
115 InputStreamReader isr = new InputStreamReader( is );
116 BufferedReader in = new BufferedReader( isr );
117 Writer writer = new StringWriter( );
118
119 if ( in != null )
120 {
121 char [ ] buffer = new char [ 1024];
122
123 try
124 {
125 int n;
126
127 while ( ( n = in.read( buffer ) ) != -1 )
128 {
129 writer.write( buffer, 0, n );
130 }
131 }
132 finally
133 {
134 isr.close( );
135 }
136
137 return writer.toString( );
138 }
139 else
140 {
141 return "";
142 }
143 }
144
145
146
147
148
149
150
151
152
153
154
155
156
157 public static void validateHtmlFragment( String html ) throws IOException, SAXException
158 {
159 validateHtmlFragment( html, true );
160 }
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176 public static void validateHtmlFragment( String html, boolean failOnWarning ) throws IOException, SAXException
177 {
178 validateHtml( "<!DOCTYPE html><html lang=fr><title>junit</title>" + html, failOnWarning );
179 }
180
181
182
183
184
185
186
187
188
189
190
191
192
193 public static void validateHtml( String html ) throws IOException, SAXException
194 {
195 validateHtml( html, true );
196 }
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212 public static void validateHtml( String html, boolean failOnWarning ) throws IOException, SAXException
213 {
214 EmbeddedValidator validator = new EmbeddedValidator( );
215 String res = validator.validate( new ByteArrayInputStream( html.getBytes( StandardCharsets.UTF_8 ) ) );
216 ObjectMapper mapper = new ObjectMapper( );
217 JsonNode node = mapper.readTree( res );
218 JsonNode messages = node.get( "messages" );
219 if ( messages != null )
220 {
221 messages.forEach( message -> {
222 JsonNode type = message.get( "type" );
223 if ( type != null )
224 {
225 switch ( type.asText( ) )
226 {
227 case "error":
228 fail( "Invalid HTML : " + message.toString( ) );
229 break;
230 case "info":
231 JsonNode subtype = message.get( "subType" );
232 if ( subtype != null && subtype.asText( ).equals( "warning" ) )
233 {
234 if ( failOnWarning )
235 {
236 fail( "HTML with warning : " + message.toString( ) );
237 }
238 }
239 default:
240 break;
241 }
242
243 }
244 } );
245 }
246 }
247
248
249
250
251
252
253 public static String getRandomName( )
254 {
255 return getRandomName( "junit", 128 );
256 }
257
258
259
260
261
262
263
264
265
266 public static String getRandomName( String strPrefix )
267 {
268 return getRandomName( strPrefix, 128 );
269 }
270
271
272
273
274
275
276
277
278
279
280
281 public static String getRandomName( String strPrefix, int nRandomBits )
282 {
283 BigInteger bigInt = new BigInteger( nRandomBits, _rand );
284 return strPrefix + bigInt.toString( 36 );
285 }
286 }