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.xpage;
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.nio.charset.StandardCharsets;
43 import java.util.ArrayList;
44 import java.util.HashMap;
45 import java.util.List;
46 import java.util.Locale;
47 import java.util.Map;
48 import java.util.Map.Entry;
49 import java.util.Set;
50
51 import javax.servlet.http.HttpServletRequest;
52 import javax.servlet.http.HttpServletResponse;
53 import javax.validation.ConstraintViolation;
54
55 import org.apache.logging.log4j.Logger;
56 import org.springframework.util.ReflectionUtils;
57
58 import fr.paris.lutece.portal.service.i18n.I18nService;
59 import fr.paris.lutece.portal.service.message.SiteMessageException;
60 import fr.paris.lutece.portal.service.plugin.Plugin;
61 import fr.paris.lutece.portal.service.security.AccessLoggerConstants;
62 import fr.paris.lutece.portal.service.security.LuteceUser;
63 import fr.paris.lutece.portal.service.security.SecurityService;
64 import fr.paris.lutece.portal.service.security.UserNotSignedException;
65 import fr.paris.lutece.portal.service.template.AppTemplateService;
66 import fr.paris.lutece.portal.service.util.AppException;
67 import fr.paris.lutece.portal.service.util.AppLogService;
68 import fr.paris.lutece.portal.service.util.AppPropertiesService;
69 import fr.paris.lutece.portal.util.mvc.utils.MVCMessage;
70 import fr.paris.lutece.portal.util.mvc.utils.MVCMessageBox;
71 import fr.paris.lutece.portal.util.mvc.utils.MVCUtils;
72 import fr.paris.lutece.portal.util.mvc.xpage.annotations.Controller;
73 import fr.paris.lutece.portal.web.LocalVariables;
74 import fr.paris.lutece.portal.web.l10n.LocaleService;
75 import fr.paris.lutece.portal.web.xpages.XPage;
76 import fr.paris.lutece.portal.web.xpages.XPageApplication;
77 import fr.paris.lutece.util.ErrorMessage;
78 import fr.paris.lutece.util.bean.BeanUtil;
79 import fr.paris.lutece.util.beanvalidation.BeanValidationUtil;
80 import fr.paris.lutece.util.html.HtmlTemplate;
81 import fr.paris.lutece.util.url.UrlItem;
82
83
84
85
86 public abstract class MVCApplication implements XPageApplication
87 {
88 private static final long serialVersionUID = 6093635383465830355L;
89
90
91 private static final String MARK_ERRORS = "errors";
92 private static final String MARK_INFOS = "infos";
93 private static final String MARK_WARNINGS = "warnings";
94 private static final String MARK_MESSAGE_BOX = "messageBox";
95
96
97 private static final String URL_PORTAL = "Portal.jsp";
98 private static final String PATH_PORTAL = "jsp/site/";
99 private static final String VIEW_MESSAGEBOX = "messageBox";
100 private static final String CONTENT_TYPE_JSON = "application/json";
101 private static final String CONTENT_TYPE_XML = "application/xml";
102
103
104 private static Logger _logger = MVCUtils.getLogger( );
105 private List<ErrorMessage> _listErrors = new ArrayList<>( );
106 private List<ErrorMessage> _listInfos = new ArrayList<>( );
107 private List<ErrorMessage> _listWarnings = new ArrayList<>( );
108 private MVCMessageBox _messageBox;
109 private Controller _controller = getClass( ).getAnnotation( Controller.class );
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126 @Override
127 public XPage getPage( HttpServletRequest request, int nMode, Plugin plugin ) throws SiteMessageException, UserNotSignedException
128 {
129 return processController( request );
130 }
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146 private XPage processController( HttpServletRequest request ) throws UserNotSignedException, SiteMessageException
147 {
148 Method [ ] methods = ReflectionUtils.getAllDeclaredMethods( getClass( ) );
149
150 try
151 {
152 if ( isMessageBox( request ) )
153 {
154 return messageBox( request );
155 }
156
157 LuteceUser registredUser = getRegistredUser( request );
158
159
160 Method m = MVCUtils.findViewAnnotedMethod( request, methods );
161
162 if ( m != null )
163 {
164 AccessLogService.getInstance( ).trace( AccessLoggerConstants.EVENT_TYPE_READ, m.getName( ), registredUser,
165 request.getRequestURL( ) + "?" + request.getQueryString( ), AccessLogService.ACCESS_LOG_FO );
166 return (XPage) m.invoke( this, request );
167 }
168
169
170 m = MVCUtils.findActionAnnotedMethod( request, methods );
171
172 if ( m != null )
173 {
174 AccessLogService.getInstance( ).debug( AccessLoggerConstants.EVENT_TYPE_ACTION, m.getName( ), registredUser,
175 request.getRequestURL( ) + "?" + request.getQueryString( ), AccessLogService.ACCESS_LOG_FO );
176 return (XPage) m.invoke( this, request );
177 }
178
179
180 m = MVCUtils.findDefaultViewMethod( methods );
181
182 AccessLogService.getInstance( ).trace( AccessLoggerConstants.EVENT_TYPE_ACTION, m.getName( ), registredUser,
183 request.getRequestURL( ) + "?" + request.getQueryString( ), AccessLogService.ACCESS_LOG_FO );
184 return (XPage) m.invoke( this, request );
185 }
186 catch( InvocationTargetException e )
187 {
188 if ( e.getTargetException( ) instanceof UserNotSignedException )
189 {
190 throw (UserNotSignedException) e.getTargetException( );
191 }
192
193 if ( e.getTargetException( ) instanceof SiteMessageException )
194 {
195 throw (SiteMessageException) e.getTargetException( );
196 }
197
198 if ( e.getTargetException( ) instanceof RuntimeException )
199 {
200 throw new AppException( "MVC Error dispaching view and action ", (RuntimeException) e.getTargetException( ) );
201 }
202
203 throw new AppException( "MVC Error dispaching view and action ", e );
204 }
205 catch( IllegalAccessException e )
206 {
207 throw new AppException( "MVC Error dispaching view and action ", e );
208 }
209 }
210
211
212
213
214
215
216 protected String getXPageName( )
217 {
218 return _controller.xpageName( );
219 }
220
221
222
223
224
225
226
227
228 protected String getDefaultPageTitle( Locale locale )
229 {
230 if ( !_controller.pageTitleProperty( ).equals( "" ) )
231 {
232 return AppPropertiesService.getProperty( _controller.pageTitleProperty( ) );
233 }
234 else
235 if ( !_controller.pageTitleI18nKey( ).equals( "" ) )
236 {
237 return I18nService.getLocalizedString( _controller.pageTitleI18nKey( ), locale );
238 }
239
240 return _controller.xpageName( );
241 }
242
243
244
245
246
247
248
249
250 protected String getDefaultPagePath( Locale locale )
251 {
252 if ( !_controller.pagePathProperty( ).equals( "" ) )
253 {
254 return AppPropertiesService.getProperty( _controller.pagePathProperty( ) );
255 }
256 else
257 if ( !_controller.pagePathI18nKey( ).equals( "" ) )
258 {
259 return I18nService.getLocalizedString( _controller.pagePathI18nKey( ), locale );
260 }
261
262 return _controller.xpageName( );
263 }
264
265
266
267
268
269
270
271
272
273 protected XPage getXPage( )
274 {
275 XPageportal/web/xpages/XPage.html#XPage">XPage page = new XPage( );
276
277 page.setTitle( getDefaultPageTitle( LocaleService.getDefault( ) ) );
278 page.setPathLabel( getDefaultPagePath( LocaleService.getDefault( ) ) );
279
280 return page;
281 }
282
283
284
285
286
287
288
289
290 protected XPage getXPage( String strTemplate )
291 {
292 XPage page = getXPage( );
293
294 HtmlTemplate t = AppTemplateService.getTemplate( strTemplate );
295 page.setContent( t.getHtml( ) );
296
297 return page;
298 }
299
300
301
302
303
304
305
306
307
308
309 protected XPage getXPage( String strTemplate, Locale locale )
310 {
311 return getXPage( strTemplate, locale, getModel( ) );
312 }
313
314
315
316
317
318
319
320
321
322
323
324
325
326 protected XPage getXPage( String strTemplate, Locale locale, Map<String, Object> model )
327 {
328 XPage page = getXPage( );
329
330 HtmlTemplate t = AppTemplateService.getTemplate( strTemplate, locale, model );
331 page.setContent( t.getHtml( ) );
332 page.setTitle( getDefaultPageTitle( locale ) );
333 page.setPathLabel( getDefaultPagePath( locale ) );
334
335 return page;
336 }
337
338
339
340
341
342
343 protected Map<String, Object> getModel( )
344 {
345 Map<String, Object> model = new HashMap<>( );
346 fillCommons( model );
347
348 return model;
349 }
350
351
352
353
354
355
356
357
358
359
360
361
362 protected void populate( Object bean, HttpServletRequest request )
363 {
364 BeanUtil.populate( bean, request, null );
365 }
366
367
368
369
370
371
372
373
374
375
376
377 protected void populate( Object bean, HttpServletRequest request, Locale locale )
378 {
379 BeanUtil.populate( bean, request, locale );
380 }
381
382
383
384
385
386
387
388
389
390
391
392
393 protected <T> boolean validateBean( T bean )
394 {
395 Set<ConstraintViolation<T>> errors = BeanValidationUtil.validate( bean );
396
397 if ( errors.isEmpty( ) )
398 {
399 return true;
400 }
401
402 for ( ConstraintViolation<T> constraint : errors )
403 {
404 MVCMessage/util/mvc/utils/MVCMessage.html#MVCMessage">MVCMessage error = new MVCMessage( );
405 error.setMessage( constraint.getMessage( ) );
406 _listErrors.add( error );
407 }
408
409 return false;
410 }
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425 protected <T> boolean validateBean( T bean, Locale locale )
426 {
427 Set<ConstraintViolation<T>> errors = BeanValidationUtil.validate( bean );
428
429 if ( errors.isEmpty( ) )
430 {
431 return true;
432 }
433
434 for ( ConstraintViolation<T> constraint : errors )
435 {
436 MVCMessage/util/mvc/utils/MVCMessage.html#MVCMessage">MVCMessage error = new MVCMessage( );
437 error.setMessage( I18nService.getLocalizedString( constraint.getMessage( ), locale ) );
438 _listErrors.add( error );
439 }
440
441 return false;
442 }
443
444
445
446
447
448
449
450 protected void addError( String strMessage )
451 {
452 _listErrors.add( new MVCMessage( strMessage ) );
453 }
454
455
456
457
458
459
460
461
462 protected void addError( String strMessage, String strFieldName )
463 {
464 _listErrors.add( new MVCMessage( strMessage,strFieldName ) );
465 }
466
467
468
469
470
471
472
473
474
475 protected void addError( String strMessageKey, Locale locale )
476 {
477 _listErrors.add( new MVCMessage( I18nService.getLocalizedString( strMessageKey, locale ) ) );
478 }
479
480
481
482
483
484
485
486
487 protected void addWarning( String strMessage )
488 {
489 _listWarnings.add( new MVCMessage( strMessage ) );
490 }
491
492
493
494
495
496
497
498
499
500
501 protected void addWarning( String strMessage, String strFieldName )
502 {
503 _listWarnings.add( new MVCMessage( strMessage,strFieldName ) );
504 }
505
506
507
508
509
510
511
512
513
514
515
516 protected void addWarning( String strMessageKey, Locale locale )
517 {
518 _listWarnings.add( new MVCMessage( I18nService.getLocalizedString( strMessageKey, locale ) ) );
519 }
520
521
522
523
524
525
526
527 protected void addInfo( String strMessage )
528 {
529 _listInfos.add( new MVCMessage( strMessage ) );
530 }
531
532
533
534
535
536
537
538
539 protected void addInfo( String strMessage, String strFieldName )
540 {
541 _listInfos.add( new MVCMessage( strMessage,strFieldName ) );
542 }
543
544
545
546
547
548
549
550
551
552 protected void addInfo( String strMessageKey, Locale locale )
553 {
554 _listInfos.add( new MVCMessage( I18nService.getLocalizedString( strMessageKey, locale ) ) );
555 }
556
557
558
559
560
561
562
563 protected void fillCommons( Map<String, Object> model )
564 {
565 List<ErrorMessage> listErrors = new ArrayList<>( _listErrors );
566 List<ErrorMessage> listInfos = new ArrayList<>( _listInfos );
567 List<ErrorMessage> listWarnings = new ArrayList<>( _listWarnings );
568 model.put( MARK_ERRORS, listErrors );
569 model.put( MARK_INFOS, listInfos );
570 model.put( MARK_WARNINGS, listWarnings );
571 _listErrors.clear( );
572 _listInfos.clear( );
573 _listWarnings.clear( );
574 }
575
576
577
578
579
580
581
582
583
584
585
586
587
588 protected XPage redirect( HttpServletRequest request, String strTarget )
589 {
590 HttpServletResponse response = LocalVariables.getResponse( );
591
592 try
593 {
594 _logger.debug( "Redirect :{}", strTarget );
595 response.sendRedirect( strTarget );
596 }
597 catch( IOException e )
598 {
599 _logger.error( "Unable to redirect : {} : {}", strTarget, e.getMessage( ), e );
600 }
601 XPage/portal/web/xpages/XPage.html#XPage">XPage xpage=new XPage();
602 xpage.setSendRedirect(true);
603 return xpage;
604 }
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619 protected XPage redirect( HttpServletRequest request, String strView, String strParameter, int nValue )
620 {
621 UrlItemtil/url/UrlItem.html#UrlItem">UrlItem url = new UrlItem( getViewUrl( strView ) );
622 url.addParameter( strParameter, nValue );
623
624 return redirect( request, url.getUrl( ) );
625 }
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644 protected XPage redirect( HttpServletRequest request, String strView, String strParameter1, int nValue1, String strParameter2, int nValue2 )
645 {
646 UrlItemtil/url/UrlItem.html#UrlItem">UrlItem url = new UrlItem( getViewUrl( strView ) );
647 url.addParameter( strParameter1, nValue1 );
648 url.addParameter( strParameter2, nValue2 );
649
650 return redirect( request, url.getUrl( ) );
651 }
652
653
654
655
656
657
658
659
660
661
662
663
664 protected XPage redirect( HttpServletRequest request, String strView, Map<String, String> additionalParameters )
665 {
666 UrlItemtil/url/UrlItem.html#UrlItem">UrlItem url = new UrlItem( getViewUrl( strView ) );
667
668 if ( additionalParameters != null )
669 {
670 for ( Entry<String, String> entry : additionalParameters.entrySet( ) )
671 {
672 url.addParameter( entry.getKey( ), entry.getValue( ) );
673 }
674 }
675
676 return redirect( request, url.getUrl( ) );
677 }
678
679
680
681
682
683
684
685
686
687
688 protected XPage redirectView( HttpServletRequest request, String strView )
689 {
690 return redirect( request, getViewUrl( strView ) );
691 }
692
693
694
695
696
697
698
699
700 protected String getViewUrl( String strView )
701 {
702 UrlItemtil/url/UrlItem.html#UrlItem">UrlItem url = new UrlItem( URL_PORTAL );
703 url.addParameter( MVCUtils.PARAMETER_PAGE, getXPageName( ) );
704 url.addParameter( MVCUtils.PARAMETER_VIEW, strView );
705
706 return url.getUrl( );
707 }
708
709
710
711
712
713
714
715
716 protected String getViewFullUrl( String strView )
717 {
718 return PATH_PORTAL + getViewUrl( strView );
719 }
720
721
722
723
724
725
726
727
728 protected String getActionUrl( String strAction )
729 {
730 UrlItemtil/url/UrlItem.html#UrlItem">UrlItem url = new UrlItem( URL_PORTAL );
731 url.addParameter( MVCUtils.PARAMETER_PAGE, getXPageName( ) );
732 url.addParameter( MVCUtils.PARAMETER_ACTION, strAction );
733
734 return url.getUrl( );
735 }
736
737
738
739
740
741
742
743
744 protected String getActionFullUrl( String strAction )
745 {
746 return PATH_PORTAL + getActionUrl( strAction );
747 }
748
749
750
751
752
753
754
755
756
757
758
759
760 protected XPage download( String strData, String strFilename, String strContentType )
761 {
762 return download( strData.getBytes( StandardCharsets.UTF_8 ), strFilename, strContentType );
763 }
764
765
766
767
768
769
770
771
772
773
774
775
776 protected XPage download( byte [ ] data, String strFilename, String strContentType )
777 {
778 HttpServletResponse response = LocalVariables.getResponse( );
779 MVCUtils.addDownloadHeaderToResponse( response, strFilename, strContentType );
780
781 try ( OutputStream os = response.getOutputStream( ) )
782 {
783 os.write( data );
784 }
785 catch( IOException e )
786 {
787 AppLogService.error( "An error occured while writing the downloaded data", e );
788 }
789
790 XPageortal/web/xpages/XPage.html#XPage">XPage xPage = new XPage( );
791 xPage.setStandalone( true );
792 return xPage;
793 }
794
795
796
797
798
799
800
801
802 protected XPage responseJSON( String strJSON )
803 {
804 HttpServletResponse response = LocalVariables.getResponse( );
805 response.setContentType( CONTENT_TYPE_JSON );
806
807 try
808 {
809 PrintWriter out = response.getWriter( );
810 out.print( strJSON );
811 out.flush( );
812 out.close( );
813 }
814 catch( IOException e )
815 {
816 AppLogService.error( e.getStackTrace( ), e );
817 }
818
819 return new XPage( );
820 }
821
822
823
824
825
826
827
828
829 protected XPage responseXML( String strXML )
830 {
831 HttpServletResponse response = LocalVariables.getResponse( );
832 response.setContentType( CONTENT_TYPE_XML );
833
834 try
835 {
836 PrintWriter out = response.getWriter( );
837 out.print( strXML );
838 out.flush( );
839 out.close( );
840 }
841 catch( IOException e )
842 {
843 AppLogService.error( e.getStackTrace( ), e );
844 }
845
846 return new XPage( );
847 }
848
849
850
851
852
853
854
855
856
857
858
859
860
861 protected XPage redirectMessageBox( HttpServletRequest request, MVCMessageBox messageBox )
862 {
863 _messageBox = messageBox;
864
865 return redirectView( request, VIEW_MESSAGEBOX );
866 }
867
868
869
870
871
872
873
874
875 private boolean isMessageBox( HttpServletRequest request )
876 {
877 String strView = request.getParameter( MVCUtils.PARAMETER_VIEW );
878
879 return ( ( strView != null ) && ( strView.equals( VIEW_MESSAGEBOX ) ) );
880 }
881
882
883
884
885
886
887
888
889 protected Locale getLocale( HttpServletRequest request )
890 {
891 return LocaleService.getContextUserLocale( request );
892 }
893
894
895
896
897
898
899
900
901 private XPage messageBox( HttpServletRequest request )
902 {
903 _messageBox.localize( getLocale( request ) );
904
905 Map<String, Object> model = getModel( );
906 model.put( MARK_MESSAGE_BOX, _messageBox );
907
908 return getXPage( _messageBox.getTemplate( ), getLocale( request ), model );
909 }
910
911
912
913
914
915
916
917 protected LuteceUser getRegistredUser( HttpServletRequest request )
918 {
919
920 if ( SecurityService.isAuthenticationEnable( ) )
921 {
922 LuteceUser luteceUser = SecurityService.getInstance( ).getRegisteredUser( request );
923 if ( luteceUser != null )
924 {
925 return luteceUser;
926 }
927 }
928
929 return null;
930 }
931 }