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.service.portal;
35
36 import java.util.ArrayList;
37 import java.util.Collection;
38 import java.util.HashMap;
39 import java.util.List;
40 import java.util.Locale;
41 import java.util.Map;
42 import java.util.Properties;
43
44 import javax.servlet.http.HttpServletRequest;
45
46 import org.apache.commons.lang3.StringUtils;
47
48 import fr.paris.lutece.portal.business.XmlContent;
49 import fr.paris.lutece.portal.business.page.Page;
50 import fr.paris.lutece.portal.business.page.PageHome;
51 import fr.paris.lutece.portal.business.portalcomponent.PortalComponentHome;
52 import fr.paris.lutece.portal.business.portlet.Portlet;
53 import fr.paris.lutece.portal.business.portlet.PortletHome;
54 import fr.paris.lutece.portal.business.style.ModeHome;
55 import fr.paris.lutece.portal.business.stylesheet.StyleSheet;
56 import fr.paris.lutece.portal.service.cache.IPathCacheService;
57 import fr.paris.lutece.portal.service.content.ContentService;
58 import fr.paris.lutece.portal.service.content.PageData;
59 import fr.paris.lutece.portal.service.datastore.DatastoreService;
60 import fr.paris.lutece.portal.service.html.XmlTransformerService;
61 import fr.paris.lutece.portal.service.includes.PageInclude;
62 import fr.paris.lutece.portal.service.includes.PageIncludeService;
63 import fr.paris.lutece.portal.service.message.SiteMessageException;
64 import fr.paris.lutece.portal.service.page.IPageService;
65 import fr.paris.lutece.portal.service.plugin.PluginService;
66 import fr.paris.lutece.portal.service.security.LuteceUser;
67 import fr.paris.lutece.portal.service.security.SecurityService;
68 import fr.paris.lutece.portal.service.spring.SpringContextService;
69 import fr.paris.lutece.portal.service.template.AppTemplateService;
70 import fr.paris.lutece.portal.service.util.AppPathService;
71 import fr.paris.lutece.portal.service.util.AppPropertiesService;
72 import fr.paris.lutece.portal.web.constants.Markers;
73 import fr.paris.lutece.portal.web.constants.Parameters;
74 import fr.paris.lutece.portal.web.l10n.LocaleService;
75 import fr.paris.lutece.util.date.DateUtil;
76 import fr.paris.lutece.util.html.HtmlTemplate;
77 import fr.paris.lutece.util.xml.XmlUtil;
78
79
80
81
82 public final class PortalService
83 {
84
85
86 private static final int PORTAL_COMPONENT_PAGE_PATH_ID = 5;
87
88
89 private static final String PROPERTY_HOME_PAGE_HEADER = "home.page.header.mode";
90 private static final String PROPERTY_INTERNAL_PAGE_HEADER = "internal.page.header.mode";
91 private static final String PROPERTY_PAGE_TOOLS_MENU = "page.tools.menu.mode";
92 private static final String PROPERTY_PORTAL_FOOTER = "page.portal.footer.mode";
93 private static final String PROPERTY_PATH_ON_ROOT = "lutece.root.path";
94 private static final String PROPERTY_ENCODING = "lutece.encoding";
95 private static final String PROPERTY_ENCODING_DEFAULT = "UTF-8";
96
97
98 private static final String KEY_SITE_NAME = "portal.site.site_property.name";
99 private static final String KEY_WEBMASTER_EMAIL = "portal.site.site_property.email";
100
101
102 private static final String TEMPLATE_PAGE_FRAMESET = "skin/site/page_frameset.html";
103 private static final String TEMPLATE_HOME_PAGE_HEADER = "skin/site/page_header_home.html";
104 private static final String TEMPLATE_INTERNAL_PAGE_HEADER = "skin/site/page_header_internal.html";
105 private static final String TEMPLATE_PAGE_TOOLS_MENU = "skin/site/page_menu_tools.html";
106 private static final String TEMPLATE_PAGE_PATH = "skin/site/page_path.html";
107 private static final String TEMPLATE_PORTAL_FOOTER = "skin/site/portal_footer.html";
108 private static final String TEMPLATE_ADMIN_CSS_LINKS = "admin/stylesheet_link.html";
109
110
111 private static final String MARKER_TARGET = "target";
112 private static final String MARKER_PAGE_DATA = "data";
113 private static final String PLUGIN_EXTEND_NAME = "extend";
114 private static final String PLUGIN_CONTACT_NAME = "contact";
115 private static final String MARK_IS_EXTEND_INSTALLED = "isExtendInstalled";
116 private static final String MARK_IS_CONTACT_INSTALLED = "isContactInstalled";
117 private static final String MARK_LUTECE_USER = "lutece_user";
118 private static final String TARGET_TOP = "target='_top'";
119 private static final String BOOKMARK_BASE_URL = "@base_url@";
120 private static final String MARK_LAST_MODIFIED = "last_modified";
121 private static final String MARK_DISPLAY_LAST_MODIFIED = "display_last_modified";
122
123
124 private static final int MODE_NORMAL = 0;
125 private static final int MODE_ADMIN = 1;
126 private static final String PARAMETER_SITE_PATH = "site-path";
127
128
129 private static Map<String, ContentService> _mapContentServicesRegistry = new HashMap<>( );
130 private static IPageService _pageService = SpringContextService.getBean( "pageService" );
131
132
133
134
135 private PortalService( )
136 {
137 }
138
139
140
141
142
143
144
145
146 public static ContentService getInvokedContentService( HttpServletRequest request )
147 {
148 for ( ContentService cs : getContentServicesList( ) )
149 {
150 if ( cs.isInvoked( request ) )
151 {
152 return cs;
153 }
154 }
155
156 return null;
157 }
158
159
160
161
162
163
164
165
166
167 public static void registerContentService( String strName, ContentService cs )
168 {
169 _mapContentServicesRegistry.put( strName, cs );
170 }
171
172
173
174
175
176
177 public static Collection<ContentService> getContentServicesList( )
178 {
179 return _mapContentServicesRegistry.values( );
180 }
181
182
183
184
185
186
187 public static int getRootPageId( )
188 {
189 return AppPropertiesService.getPropertyInt( "lutece.page.root", 1 );
190 }
191
192
193
194
195
196
197
198
199
200
201
202
203 public static String getDefaultPage( HttpServletRequest request, int nMode ) throws SiteMessageException
204 {
205 return _pageService.getPage( String.valueOf( getRootPageId( ) ), nMode, request );
206 }
207
208
209
210
211
212
213
214
215 public static String getXmlPagesList( Collection<Page> listPages )
216 {
217 StringBuffer strXml = new StringBuffer( );
218 strXml.append( XmlUtil.getXmlHeader( ) );
219 XmlUtil.beginElement( strXml, XmlContent.TAG_CHILD_PAGES_LIST );
220
221 for ( Page page : listPages )
222 {
223 XmlUtil.beginElement( strXml, XmlContent.TAG_PAGE );
224 XmlUtil.addElement( strXml, XmlContent.TAG_PAGE_ID, page.getId( ) );
225 XmlUtil.addElementHtml( strXml, XmlContent.TAG_PAGE_NAME, page.getName( ) );
226 XmlUtil.endElement( strXml, XmlContent.TAG_PAGE );
227 }
228
229 XmlUtil.endElement( strXml, XmlContent.TAG_CHILD_PAGES_LIST );
230
231 return strXml.toString( );
232 }
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248 public static String buildPageContent( PageData data, int nMode, HttpServletRequest request )
249 {
250 return buildPageContent( getRootPageId( ), data, nMode, request );
251 }
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266 public static String buildPageContent( int nCurrentPageId, PageData data, int nMode, HttpServletRequest request )
267 {
268 Locale locale = null;
269 HashMap<String, Object> model = new HashMap<>( );
270 LuteceUser user = null;
271 String strWebmasterEmail = DatastoreService.getDataValue( KEY_WEBMASTER_EMAIL, "" );
272 model.put( Markers.WEBMASTER_EMAIL, strWebmasterEmail );
273
274 locale = LocaleService.getContextUserLocale( request );
275
276 if ( request != null )
277 {
278 user = SecurityService.getInstance( ).getRegisteredUser( request );
279 if ( nMode != MODE_ADMIN )
280 {
281 model.put( MARK_LUTECE_USER, user );
282 }
283 }
284
285 List<PageInclude> listIncludes = PageIncludeService.getIncludes( );
286
287 for ( PageInclude pic : listIncludes )
288 {
289 pic.fillTemplate( model, data, nMode, request );
290 }
291
292 String strHeader = data.isHomePage( ) ? AppPropertiesService.getProperty( PROPERTY_HOME_PAGE_HEADER + nMode, TEMPLATE_HOME_PAGE_HEADER )
293 : AppPropertiesService.getProperty( PROPERTY_INTERNAL_PAGE_HEADER + nMode, TEMPLATE_INTERNAL_PAGE_HEADER );
294 HtmlTemplate tHeader = AppTemplateService.getTemplate( strHeader, locale, model );
295
296 String strFooter = AppPropertiesService.getProperty( PROPERTY_PORTAL_FOOTER + nMode, TEMPLATE_PORTAL_FOOTER );
297 String strToolsMenu = AppPropertiesService.getProperty( PROPERTY_PAGE_TOOLS_MENU + nMode, TEMPLATE_PAGE_TOOLS_MENU );
298 model.put( MARK_IS_CONTACT_INSTALLED, isContactActivated( ) );
299
300 boolean bDisplayLastModified = data.getDisplayDateUpdate( );
301 model.put( MARK_DISPLAY_LAST_MODIFIED, bDisplayLastModified );
302
303 if ( request != null )
304 {
305 String strLastModified = DateUtil.getDateString( data.getDateUpdate( ), locale );
306 model.put( MARK_LAST_MODIFIED, strLastModified );
307 }
308 HtmlTemplate tFooter = AppTemplateService.getTemplate( strFooter, locale, model );
309
310 HtmlTemplate tToolsMenu = AppTemplateService.getTemplate( strToolsMenu, locale, model );
311 model.put( Markers.PAGE_HEADER, tHeader.getHtml( ) );
312 model.put( MARKER_PAGE_DATA, data );
313 model.put( Markers.PAGE_NAME, data.getName( ) == null ? "" : data.getName( ) );
314 model.put( Markers.PAGE_CONTENT, data.getContent( ) == null ? "" : data.getContent( ) );
315 model.put( Markers.PAGE_PATH, data.getPagePath( ) == null ? "" : data.getPagePath( ) );
316 model.put( Markers.PAGE_TOOLS_MENU, tToolsMenu.getHtml( ) );
317 model.put( Markers.PAGE_ID, nCurrentPageId );
318
319 model.put( Markers.PAGE_FOOTER, tFooter.getHtml( ) );
320
321 String strBaseUrl = request != null ? AppPathService.getBaseUrl( request ) : "";
322
323
324
325
326 model.put( Markers.WEBAPP_PATH_FOR_LINKSERVICE, strBaseUrl );
327 model.put( Markers.BASE_URL, strBaseUrl );
328
329 String strEncoding = AppPropertiesService.getProperty( PROPERTY_ENCODING, PROPERTY_ENCODING_DEFAULT );
330
331 if ( strEncoding == null || strEncoding.equals( "" ) )
332 {
333 strEncoding = PROPERTY_ENCODING_DEFAULT;
334 }
335
336 model.put( Markers.ENCODING, strEncoding );
337
338 model.put( Markers.USER_CONTEXT_LANGUAGE, locale );
339
340 model.put( MARK_IS_EXTEND_INSTALLED, isExtendActivated( ) );
341
342 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_PAGE_FRAMESET, locale, model );
343
344 template.substitute( BOOKMARK_BASE_URL, request != null ? AppPathService.getBaseUrl( request ) : "" );
345
346
347
348
349
350
351
352
353
354
355 return template.getHtml( );
356 }
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372 public static String getPagePathContent( int nPageId, int nMode, HttpServletRequest request )
373 {
374 String strPathOnRoot = AppPropertiesService.getProperty( PROPERTY_PATH_ON_ROOT );
375
376
377
378 if ( nPageId == getRootPageId( ) && ( strPathOnRoot == null || strPathOnRoot.equalsIgnoreCase( "false" ) ) )
379 {
380 return "";
381 }
382
383
384
385
386 StyleSheet xslSource;
387
388 switch( nMode )
389 {
390 case MODE_NORMAL:
391 case MODE_ADMIN:
392 xslSource = PortalComponentHome.getXsl( PORTAL_COMPONENT_PAGE_PATH_ID, MODE_NORMAL );
393
394 break;
395
396 default:
397 xslSource = PortalComponentHome.getXsl( PORTAL_COMPONENT_PAGE_PATH_ID, nMode );
398
399 break;
400 }
401
402 String strXml = getXmlPagesList( getPagePath( nPageId ) );
403
404 Properties outputProperties = ModeHome.getOuputXslProperties( nMode );
405
406
407
408 Map<String, String> mapParamRequest = new HashMap<>( );
409 setXslPortalPath( mapParamRequest, nMode );
410
411 XmlTransformerServicervice.html#XmlTransformerService">XmlTransformerService xmlTransformerService = new XmlTransformerService( );
412 String strPath = xmlTransformerService.transformBySourceWithXslCache( strXml, xslSource, mapParamRequest, outputProperties );
413
414 return formatPath( strPath, nMode, request );
415 }
416
417
418
419
420
421
422
423
424
425
426
427
428 public static String getXPagePathContent( String strXPageName, int nMode, HttpServletRequest request )
429 {
430 final IPathCacheService pathCacheService = SpringContextService.getBean( IPathCacheService.BEAN_NAME );
431
432 final String strKey = pathCacheService.getKey( strXPageName, nMode, request );
433
434 String strRes = pathCacheService.getFromCache( strKey );
435
436 if ( strRes != null )
437 {
438 return strRes;
439 }
440
441
442 StyleSheet xslSource;
443
444
445 switch( nMode )
446 {
447 case MODE_NORMAL:
448 case MODE_ADMIN:
449 xslSource = PortalComponentHome.getXsl( PORTAL_COMPONENT_PAGE_PATH_ID, MODE_NORMAL );
450
451 break;
452
453 default:
454 xslSource = PortalComponentHome.getXsl( PORTAL_COMPONENT_PAGE_PATH_ID, nMode );
455
456 break;
457 }
458
459 String strXml = StringUtils.EMPTY;
460 String strPageId = request.getParameter( Parameters.PAGE_ID );
461
462 if ( StringUtils.isNotBlank( strPageId ) && StringUtils.isNumeric( strPageId ) )
463 {
464 int nPageId = Integer.parseInt( strPageId );
465 strXml = getXmlPagesList( getXPagePath( strXPageName, nPageId ) );
466 }
467 else
468 {
469 String strPortletId = request.getParameter( Parameters.PORTLET_ID );
470
471 if ( StringUtils.isNotBlank( strPortletId ) && StringUtils.isNumeric( strPortletId ) )
472 {
473 int nPortletId = Integer.parseInt( strPortletId );
474 Portlet portlet = PortletHome.findByPrimaryKey( nPortletId );
475
476 if ( portlet != null )
477 {
478 int nPageId = portlet.getPageId( );
479 strXml = getXmlPagesList( getXPagePath( strXPageName, nPageId ) );
480 }
481 }
482 }
483
484 if ( StringUtils.isBlank( strXml ) )
485 {
486 strXml = getXmlPagesList( getXPagePath( strXPageName ) );
487 }
488
489 Properties outputProperties = ModeHome.getOuputXslProperties( nMode );
490
491
492
493 Map<String, String> mapXslParams = new HashMap<>( );
494 setXslPortalPath( mapXslParams, nMode );
495
496 XmlTransformerServicervice.html#XmlTransformerService">XmlTransformerService xmlTransformerService = new XmlTransformerService( );
497 String strPath = xmlTransformerService.transformBySourceWithXslCache( strXml, xslSource, mapXslParams, outputProperties );
498
499 strRes = formatPath( strPath, nMode, request );
500
501 pathCacheService.putInCache( strKey, strRes );
502
503 return strRes;
504 }
505
506
507
508
509
510
511
512
513 public static Collection<Page> getPagePath( int nPageId )
514 {
515 ArrayList<Page> list = new ArrayList<>( );
516 Page page = PageHome.getPage( nPageId );
517 int nParentPageId = page.getParentPageId( );
518 list.add( page );
519
520 while ( nParentPageId != 0 )
521 {
522 Page parentPage = PageHome.getPage( nParentPageId );
523
524
525 list.add( 0, parentPage );
526 nParentPageId = parentPage.getParentPageId( );
527 }
528
529 return list;
530 }
531
532
533
534
535
536
537
538
539 private static Collection<Page> getXPagePath( String strXPageName )
540 {
541 ArrayList<Page> list = new ArrayList<>( );
542 Page homePage = PageHome.getPage( getRootPageId( ) );
543 list.add( homePage );
544
545 Pagetal/business/page/Page.html#Page">Page xPage = new Page( );
546 xPage.setName( strXPageName );
547 list.add( xPage );
548
549 return list;
550 }
551
552
553
554
555
556
557
558
559
560
561 private static Collection<Page> getXPagePath( String strXPageName, int nPageId )
562 {
563 List<Page> list = new ArrayList<>( );
564 Page page = PageHome.getPage( nPageId );
565
566 if ( page != null )
567 {
568 int nParentPageId = page.getParentPageId( );
569
570 while ( ( nParentPageId > 0 ) && ( nParentPageId != getRootPageId( ) ) )
571 {
572 Page parentPage = PageHome.getPage( nParentPageId );
573
574 if ( parentPage != null )
575 {
576
577 list.add( 0, parentPage );
578 nParentPageId = parentPage.getParentPageId( );
579 }
580 }
581
582 if ( nPageId != getRootPageId( ) )
583 {
584 list.add( page );
585 }
586 }
587
588
589 Page homePage = PageHome.getPage( getRootPageId( ) );
590 list.add( 0, homePage );
591
592
593 Pagetal/business/page/Page.html#Page">Page xPage = new Page( );
594 xPage.setName( strXPageName );
595 xPage.setId( nPageId );
596 list.add( xPage );
597
598 return list;
599 }
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614 public static String formatPath( String strPath, int nMode, HttpServletRequest request )
615 {
616 HashMap<String, Object> model = new HashMap<>( );
617 model.put( Markers.PAGE_PATH, strPath );
618
619 List<PageInclude> listIncludes = PageIncludeService.getIncludes( );
620 PageData/service/content/PageData.html#PageData">PageData data = new PageData( );
621
622 for ( PageInclude pic : listIncludes )
623 {
624 pic.fillTemplate( model, data, nMode, request );
625 }
626
627 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_PAGE_PATH, ( request == null ) ? null : request.getLocale( ), model );
628
629 return template.getHtml( );
630 }
631
632
633
634
635
636
637
638
639 private static String getXmlPagesListExtended( String strXmlExtend )
640 {
641 StringBuffer strXml = new StringBuffer( );
642 strXml.append( XmlUtil.getXmlHeader( ) );
643 XmlUtil.beginElement( strXml, XmlContent.TAG_CHILD_PAGES_LIST );
644
645 Page homePage = PageHome.getPage( getRootPageId( ) );
646
647 XmlUtil.beginElement( strXml, XmlContent.TAG_PAGE );
648 XmlUtil.addElement( strXml, XmlContent.TAG_PAGE_ID, homePage.getId( ) );
649 XmlUtil.addElementHtml( strXml, XmlContent.TAG_PAGE_NAME, homePage.getName( ) );
650 XmlUtil.endElement( strXml, XmlContent.TAG_PAGE );
651
652 strXml.append( strXmlExtend );
653
654 XmlUtil.endElement( strXml, XmlContent.TAG_CHILD_PAGES_LIST );
655
656 return strXml.toString( );
657 }
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673 public static String getXPagePathContent( String strXPageName, int nMode, String strTitlesUrls, HttpServletRequest request )
674 {
675 final IPathCacheService pathCacheService = SpringContextService.getBean( IPathCacheService.BEAN_NAME );
676
677 final String strKey = pathCacheService.getKey( strXPageName, nMode, strTitlesUrls, request );
678
679 String strRes = pathCacheService.getFromCache( strKey );
680
681 if ( strRes != null )
682 {
683 return strRes;
684 }
685
686
687 StyleSheet xslSource;
688
689
690 switch( nMode )
691 {
692 case MODE_NORMAL:
693 case MODE_ADMIN:
694 xslSource = PortalComponentHome.getXsl( PORTAL_COMPONENT_PAGE_PATH_ID, MODE_NORMAL );
695
696 break;
697
698 default:
699 xslSource = PortalComponentHome.getXsl( PORTAL_COMPONENT_PAGE_PATH_ID, nMode );
700
701 break;
702 }
703
704 String strXml = getXmlPagesListExtended( strTitlesUrls );
705
706 Properties outputProperties = ModeHome.getOuputXslProperties( nMode );
707
708
709
710 Map<String, String> mapXslParams = new HashMap<>( );
711 setXslPortalPath( mapXslParams, nMode );
712
713 XmlTransformerServicervice.html#XmlTransformerService">XmlTransformerService xmlTransformerService = new XmlTransformerService( );
714 String strPath = xmlTransformerService.transformBySourceWithXslCache( strXml, xslSource, mapXslParams, outputProperties );
715
716 strRes = formatPath( strPath, nMode, request );
717
718 pathCacheService.putInCache( strKey, strRes );
719
720 return strRes;
721 }
722
723
724
725
726
727
728
729
730
731 public static void setXslPortalPath( Map<String, String> mapParameters, int nMode )
732 {
733 if ( nMode != MODE_ADMIN )
734 {
735 mapParameters.put( PARAMETER_SITE_PATH, AppPathService.getPortalUrl( ) );
736 }
737 else
738 {
739 mapParameters.put( PARAMETER_SITE_PATH, AppPathService.getAdminPortalUrl( ) );
740 mapParameters.put( MARKER_TARGET, TARGET_TOP );
741 }
742 }
743
744
745
746
747
748
749 public static String getSiteName( )
750 {
751 return DatastoreService.getDataValue( KEY_SITE_NAME, StringUtils.EMPTY );
752 }
753
754
755
756
757
758
759 public static boolean isExtendActivated( )
760 {
761 return PluginService.isPluginEnable( PLUGIN_EXTEND_NAME );
762 }
763
764
765
766
767
768
769 public static boolean isContactActivated( )
770 {
771 return PluginService.isPluginEnable( PLUGIN_CONTACT_NAME );
772 }
773
774
775
776
777
778
779 public static String getAdminCssLinks( )
780 {
781 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_ADMIN_CSS_LINKS );
782 return template.getHtml( );
783 }
784 }