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.portal.util.mvc.admin;
35
36 import fr.paris.lutece.portal.service.security.AccessLogService;
37 import java.io.IOException;
38 import java.io.OutputStream;
39 import java.io.PrintWriter;
40 import java.lang.reflect.InvocationTargetException;
41 import java.lang.reflect.Method;
42 import java.util.ArrayList;
43 import java.util.HashMap;
44 import java.util.List;
45 import java.util.Locale;
46 import java.util.Map;
47 import java.util.Map.Entry;
48
49 import javax.servlet.http.HttpServletRequest;
50 import javax.servlet.http.HttpServletResponse;
51
52 import org.apache.logging.log4j.Logger;
53 import org.springframework.util.ReflectionUtils;
54
55 import fr.paris.lutece.portal.business.user.AdminUser;
56 import fr.paris.lutece.portal.service.admin.AccessDeniedException;
57 import fr.paris.lutece.portal.service.admin.AdminAuthenticationService;
58 import fr.paris.lutece.portal.service.i18n.I18nService;
59 import fr.paris.lutece.portal.service.security.AccessLoggerConstants;
60 import fr.paris.lutece.portal.service.template.AppTemplateService;
61 import fr.paris.lutece.portal.service.util.AppException;
62 import fr.paris.lutece.portal.service.util.AppLogService;
63 import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
64 import fr.paris.lutece.portal.util.mvc.utils.MVCMessage;
65 import fr.paris.lutece.portal.util.mvc.utils.MVCUtils;
66 import fr.paris.lutece.portal.web.admin.PluginAdminPageJspBean;
67 import fr.paris.lutece.util.ErrorMessage;
68 import fr.paris.lutece.util.beanvalidation.ValidationError;
69 import fr.paris.lutece.util.html.HtmlTemplate;
70 import fr.paris.lutece.util.url.UrlItem;
71
72
73
74
75 public abstract class MVCAdminJspBean extends PluginAdminPageJspBean
76 {
77 private static final long serialVersionUID = 278165302545398831L;
78
79
80 private static final String MARK_ERRORS = "errors";
81 private static final String MARK_INFOS = "infos";
82 private static final String MARK_WARNINGS = "warnings";
83
84
85 private static final String PROPERTY_SITE_CODE = "lutece.code";
86
87
88 private static Logger _logger = MVCUtils.getLogger( );
89 private List<ErrorMessage> _listErrors = new ArrayList<>( );
90 private List<ErrorMessage> _listInfos = new ArrayList<>( );
91 private List<ErrorMessage> _listWarnings = new ArrayList<>( );
92 private Controller _controller = getClass( ).getAnnotation( Controller.class );
93 private HttpServletResponse _response;
94
95
96
97
98
99
100
101
102
103
104
105
106 public String processController( HttpServletRequest request, HttpServletResponse response ) throws AccessDeniedException
107 {
108 _response = response;
109 init( request, _controller.right( ) );
110
111 Method [ ] methods = ReflectionUtils.getAllDeclaredMethods( getClass( ) );
112
113 try
114 {
115
116 Method m = MVCUtils.findViewAnnotedMethod( request, methods );
117
118 AdminUser adminUser = AdminAuthenticationService.getInstance( ).getRegisteredUser( request );
119
120 if ( m != null )
121 {
122 AccessLogService.getInstance( ).trace( AccessLoggerConstants.EVENT_TYPE_VIEW, m.getName( ), adminUser,
123 request.getRequestURL( ) + "?" + request.getQueryString( ), AccessLogService.ACCESS_LOG_BO );
124 return (String) m.invoke( this, request );
125 }
126
127
128 m = MVCUtils.findActionAnnotedMethod( request, methods );
129
130 if ( m != null )
131 {
132 AccessLogService.getInstance( ).debug( AccessLoggerConstants.EVENT_TYPE_ACTION, m.getName( ), adminUser,
133 request.getRequestURL( ) + "?" + request.getQueryString( ), AccessLogService.ACCESS_LOG_BO );
134 return (String) m.invoke( this, request );
135 }
136
137
138 m = MVCUtils.findDefaultViewMethod( methods );
139
140 AccessLogService.getInstance( ).trace( AccessLoggerConstants.EVENT_TYPE_VIEW, m.getName( ), adminUser,
141 request.getRequestURL( ) + "?" + request.getQueryString( ), AccessLogService.ACCESS_LOG_BO );
142 return (String) m.invoke( this, request );
143 }
144 catch( InvocationTargetException e )
145 {
146 if ( e.getTargetException( ) instanceof AccessDeniedException )
147 {
148 throw (AccessDeniedException) e.getTargetException( );
149 }
150
151 throw new AppException( "MVC Error dispaching view and action ", e );
152 }
153 catch( IllegalAccessException e )
154 {
155 throw new AppException( "MVC Error dispaching view and action ", e );
156 }
157 }
158
159
160
161
162
163
164
165
166
167
168 protected void addError( String strMessage )
169 {
170 _listErrors.add( new MVCMessage( strMessage ) );
171 }
172
173
174
175
176
177
178
179
180
181 protected void addError( String strMessageKey, Locale locale )
182 {
183 _listErrors.add( new MVCMessage( I18nService.getLocalizedString( strMessageKey, locale ) ) );
184 }
185
186
187
188
189
190
191
192 protected void addWarning( String strMessage )
193 {
194 _listWarnings.add( new MVCMessage( strMessage ) );
195 }
196
197
198
199
200
201
202
203
204
205 protected void addWarning( String strMessageKey, Locale locale )
206 {
207 _listWarnings.add( new MVCMessage( I18nService.getLocalizedString( strMessageKey, locale ) ) );
208 }
209
210
211
212
213
214
215
216 protected void addInfo( String strMessage )
217 {
218 _listInfos.add( new MVCMessage( strMessage ) );
219 }
220
221
222
223
224
225
226
227
228
229 protected void addInfo( String strMessageKey, Locale locale )
230 {
231 _listInfos.add( new MVCMessage( I18nService.getLocalizedString( strMessageKey, locale ) ) );
232 }
233
234
235
236
237
238
239
240 protected void fillCommons( Map<String, Object> model )
241 {
242 List<ErrorMessage> listErrors = new ArrayList<>( _listErrors );
243 List<ErrorMessage> listInfos = new ArrayList<>( _listInfos );
244 List<ErrorMessage> listWarnings = new ArrayList<>( _listWarnings );
245 model.put( MARK_ERRORS, listErrors );
246 model.put( MARK_INFOS, listInfos );
247 model.put( MARK_WARNINGS, listWarnings );
248 _listErrors.clear( );
249 _listInfos.clear( );
250 _listWarnings.clear( );
251 }
252
253
254
255
256
257
258 protected Map<String, Object> getModel( )
259 {
260 Map<String, Object> model = new HashMap<>( );
261 fillCommons( model );
262
263 return model;
264 }
265
266
267
268
269
270
271
272
273 protected String getPage( String strTemplate )
274 {
275 String strPageTitleProperty = _controller.pageTitleProperty( );
276
277 return getPage( strPageTitleProperty, strTemplate, getModel( ) );
278 }
279
280
281
282
283
284
285
286
287
288
289 protected String getPage( String strPageTitleProperty, String strTemplate )
290 {
291 return getPage( strPageTitleProperty, strTemplate, getModel( ) );
292 }
293
294
295
296
297
298
299
300
301
302
303
304
305 protected String getPage( String strPageTitleProperty, String strTemplate, Map<String, Object> model )
306 {
307 setPageTitleProperty( strPageTitleProperty );
308
309 HtmlTemplate template = AppTemplateService.getTemplate( strTemplate, getLocale( ), model );
310
311 return getAdminPage( template.getHtml( ) );
312 }
313
314
315
316
317
318
319
320
321
322
323
324
325 protected <T> boolean validateBean( T bean, String strPrefix )
326 {
327 List<ValidationError> errors = validate( bean, strPrefix );
328
329 if ( errors.isEmpty( ) )
330 {
331 return true;
332 }
333
334 for ( ValidationError errorValidation : errors )
335 {
336 MVCMessage/util/mvc/utils/MVCMessage.html#MVCMessage">MVCMessage error = new MVCMessage( );
337 error.setMessage( errorValidation.getMessage( ) );
338 error.setFieldName( errorValidation.getFieldName( ) );
339 _listErrors.add( error );
340 }
341
342 return false;
343 }
344
345
346
347
348
349
350
351
352
353 protected String getControllerJsp( )
354 {
355 return _controller.controllerJsp( );
356 }
357
358
359
360
361
362
363 protected String getControllerPath( )
364 {
365 return _controller.controllerPath( );
366 }
367
368
369
370
371
372
373
374
375
376
377 protected String redirect( HttpServletRequest request, String strTarget )
378 {
379 try
380 {
381 _logger.debug( "Redirect : {}", strTarget );
382 _response.sendRedirect( strTarget );
383 }
384 catch( IOException e )
385 {
386 _logger.error( "Unable to redirect : {} : {}", strTarget, e.getMessage( ), e );
387 }
388
389 return null;
390 }
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405 protected String redirect( HttpServletRequest request, String strView, String strParameter, int nValue )
406 {
407 UrlItemtil/url/UrlItem.html#UrlItem">UrlItem url = new UrlItem( getViewUrl( strView ) );
408 url.addParameter( strParameter, nValue );
409
410 return redirect( request, url.getUrl( ) );
411 }
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430 protected String redirect( HttpServletRequest request, String strView, String strParameter1, int nValue1, String strParameter2, int nValue2 )
431 {
432 UrlItemtil/url/UrlItem.html#UrlItem">UrlItem url = new UrlItem( getViewUrl( strView ) );
433 url.addParameter( strParameter1, nValue1 );
434 url.addParameter( strParameter2, nValue2 );
435
436 return redirect( request, url.getUrl( ) );
437 }
438
439
440
441
442
443
444
445
446
447
448
449
450 protected String redirect( HttpServletRequest request, String strView, Map<String, String> additionalParameters )
451 {
452 UrlItemtil/url/UrlItem.html#UrlItem">UrlItem url = new UrlItem( getViewUrl( strView ) );
453
454 if ( additionalParameters != null )
455 {
456 for ( Entry<String, String> entry : additionalParameters.entrySet( ) )
457 {
458 url.addParameter( entry.getKey( ), entry.getValue( ) );
459 }
460 }
461
462 return redirect( request, url.getUrl( ) );
463 }
464
465
466
467
468
469
470
471
472
473
474 protected String redirectView( HttpServletRequest request, String strView )
475 {
476 return redirect( request, getViewUrl( strView ) );
477 }
478
479
480
481
482
483
484
485
486 protected String getViewUrl( String strView )
487 {
488 UrlItemtil/url/UrlItem.html#UrlItem">UrlItem url = new UrlItem( getControllerJsp( ) );
489 url.addParameter( MVCUtils.PARAMETER_VIEW, strView );
490
491 return url.getUrl( );
492 }
493
494
495
496
497
498
499
500
501 protected String getViewFullUrl( String strView )
502 {
503 return getControllerPath( ) + getViewUrl( strView );
504 }
505
506
507
508
509
510
511
512
513 protected String getActionUrl( String strAction )
514 {
515 UrlItemtil/url/UrlItem.html#UrlItem">UrlItem url = new UrlItem( getControllerPath( ) + getControllerJsp( ) );
516 url.addParameter( MVCUtils.PARAMETER_ACTION, strAction );
517
518 return url.getUrl( );
519 }
520
521
522
523
524
525
526
527
528
529
530
531 protected void download( String strData, String strFilename, String strContentType )
532 {
533 HttpServletResponse response = _response;
534 PrintWriter out = null;
535 response.setHeader( "Content-Disposition", "attachment; filename=\"" + strFilename + "\";" );
536 MVCUtils.addDownloadHeaderToResponse( response, strFilename, strContentType );
537
538 try
539 {
540 out = response.getWriter( );
541 out.print( strData );
542 }
543 catch( IOException e )
544 {
545 AppLogService.error( e.getStackTrace( ), e );
546 }
547 finally
548 {
549 if ( out != null )
550 {
551 out.close( );
552 }
553 }
554 }
555
556
557
558
559
560
561
562
563
564
565
566 protected void download( byte [ ] data, String strFilename, String strContentType )
567 {
568 HttpServletResponse response = _response;
569 OutputStream os;
570 MVCUtils.addDownloadHeaderToResponse( response, strFilename, strContentType );
571
572 try
573 {
574 os = response.getOutputStream( );
575 os.write( data );
576 os.close( );
577 }
578 catch( IOException e )
579 {
580 AppLogService.error( e.getStackTrace( ), e );
581 }
582 }
583 }