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