View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.portal.web.includes;
35  
36  import fr.paris.lutece.portal.business.style.Theme;
37  import fr.paris.lutece.portal.service.content.PageData;
38  import fr.paris.lutece.portal.service.includes.PageInclude;
39  import fr.paris.lutece.portal.service.plugin.Plugin;
40  import fr.paris.lutece.portal.service.plugin.PluginService;
41  import fr.paris.lutece.portal.service.portal.PortalService;
42  import fr.paris.lutece.portal.service.template.AppTemplateService;
43  import fr.paris.lutece.portal.web.xpages.XPageApplicationEntry;
44  import fr.paris.lutece.util.html.HtmlTemplate;
45  
46  import java.util.Collection;
47  import java.util.HashMap;
48  import java.util.Locale;
49  import java.util.Map;
50  
51  import javax.servlet.http.HttpServletRequest;
52  
53  
54  /**
55   * Page include that insert links into the head part of HTML pages
56   */
57  public class LinksInclude implements PageInclude
58  {
59      // Parameters
60      private static final String PARAMETER_PAGE = "page";
61  
62      // Markers
63      private static final String MARK_FAVOURITE = "favourite";
64      private static final String MARK_PORTAL_NAME = "lutece_name";
65      private static final String MARK_PLUGIN_THEME_CSS = "plugin_theme";
66      private static final String MARK_PLUGINS_CSS_LINKS = "plugins_css_links";
67      private static final String MARK_PLUGINS_JAVASCRIPT_LINKS = "plugins_javascript_links";
68      private static final String MARK_PLUGIN_CSS_STYLESHEET = "plugin_css_stylesheet";
69      private static final String MARK_PLUGIN_JAVASCRIPT_FILE = "plugin_javascript_file";
70      private static final String MARK_CSS_PREFIX = "css_prefix";
71  
72      // Templates
73      private static final String TEMPLATE_PLUGIN_CSS_LINK = "skin/site/plugin_css_link.html";
74      private static final String TEMPLATE_PLUGIN_JAVASCRIPT_LINK = "skin/site/plugin_javascript_link.html";
75      private static final String PREFIX_PLUGINS_CSS = "css/plugins/";
76      private static final String ABSOLUTE_URL = "http://";
77  
78      /**
79       * Substitue specific bookmarks in the page template.
80       * @param rootModel The data model
81       * @param data A PageData object containing applications data
82       * @param nMode The current mode
83       * @param request The HTTP request
84       */
85      public void fillTemplate( Map<String, Object> rootModel, PageData data, int nMode, HttpServletRequest request )
86      {
87          if ( request != null )
88          {
89              // Add links coming from the data object
90              String strFavourite = ( data.getFavourite(  ) != null ) ? data.getFavourite(  )
91                                                                      : PortalService.getSiteName(  );
92              String strPortalName = PortalService.getSiteName(  );
93              rootModel.put( MARK_FAVOURITE, strFavourite );
94              rootModel.put( MARK_PORTAL_NAME, strPortalName );
95  
96              Locale locale = request.getLocale(  );
97  
98              // Add CSS links coming from plugins
99              Collection<Plugin> listPlugins = PluginService.getPluginList(  );
100             listPlugins.add( PluginService.getCore(  ) );
101 
102             StringBuilder sbCssLinks = new StringBuilder(  );
103             StringBuilder sbJsLinks = new StringBuilder(  );
104 
105             for ( Plugin plugin : listPlugins )
106             {
107                 if ( plugin.isInstalled(  ) )
108                 {
109                     String strPage = request.getParameter( PARAMETER_PAGE );
110                     Theme xpageTheme = plugin.getXPageTheme( request );
111 
112                     if ( ( strPage != null ) && ( xpageTheme != null ) )
113                     {
114                         for ( XPageApplicationEntry entry : plugin.getApplications(  ) )
115                         {
116                             if ( strPage.equals( entry.getId(  ) ) )
117                             {
118                                 rootModel.put( MARK_PLUGIN_THEME_CSS, xpageTheme );
119                             }
120                         }
121                     }
122 
123                     boolean bXPage = isPluginXPage( strPage, plugin );
124 
125                     if ( plugin.isCssStylesheetsScopePortal(  ) || ( bXPage && plugin.isCssStylesheetsScopeXPage(  ) ) )
126                     {
127                         for ( String strCssStyleSheet : plugin.getCssStyleSheets(  ) )
128                         {
129                             appendStyleSheet( sbCssLinks, strCssStyleSheet, locale );
130                         }
131 
132                         for ( String strCssStyleSheet : plugin.getCssStyleSheets( nMode ) )
133                         {
134                             appendStyleSheet( sbCssLinks, strCssStyleSheet, locale );
135                         }
136                     }
137 
138                     if ( plugin.isJavascriptFilesScopePortal(  ) || ( bXPage && plugin.isJavascriptFilesScopeXPage(  ) ) )
139                     {
140                         for ( String strJavascriptFile : plugin.getJavascriptFiles(  ) )
141                         {
142                             appendJavascriptFile( sbJsLinks, strJavascriptFile, locale );
143                         }
144 
145                         for ( String strJavascriptFile : plugin.getJavascriptFiles( nMode ) )
146                         {
147                             appendJavascriptFile( sbJsLinks, strJavascriptFile, locale );
148                         }
149                     }
150                 }
151             }
152 
153             rootModel.put( MARK_PLUGINS_CSS_LINKS, sbCssLinks.toString(  ) );
154             rootModel.put( MARK_PLUGINS_JAVASCRIPT_LINKS, sbJsLinks.toString(  ) );
155         }
156     }
157 
158     /**
159      * Append a script to the links
160      * @param sbJsLinks links in construction
161      * @param strJavascriptFile the script to append
162      * @param locale the locale
163      */
164     private void appendJavascriptFile( StringBuilder sbJsLinks, String strJavascriptFile, Locale locale )
165     {
166         Map<String, String> model = new HashMap<String, String>( 1 );
167         model.put( MARK_PLUGIN_JAVASCRIPT_FILE, strJavascriptFile );
168 
169         HtmlTemplate tJs = AppTemplateService.getTemplate( TEMPLATE_PLUGIN_JAVASCRIPT_LINK, locale, model );
170         sbJsLinks.append( tJs.getHtml(  ) );
171     }
172 
173     /**
174      * Append a css to the stylesheets
175      * @param sbCssLinks stylesheets in construction
176      * @param strCssStyleSheet the stylesheet to append
177      * @param locale the locale
178      */
179     private void appendStyleSheet( StringBuilder sbCssLinks, String strCssStyleSheet, Locale locale )
180     {
181         String strPrefix = ( strCssStyleSheet.startsWith( ABSOLUTE_URL ) ) ? "" : PREFIX_PLUGINS_CSS;
182 
183         Map<String, String> model = new HashMap<String, String>( 2 );
184         model.put( MARK_PLUGIN_CSS_STYLESHEET, strCssStyleSheet );
185         model.put( MARK_CSS_PREFIX, strPrefix );
186 
187         HtmlTemplate tCss = AppTemplateService.getTemplate( TEMPLATE_PLUGIN_CSS_LINK, locale, model );
188         sbCssLinks.append( tCss.getHtml(  ) );
189     }
190 
191     /**
192      * Check if the page is a valid plugin's page
193      * @param strPage The page
194      * @param plugin The plugin
195      * @return true if valid otherwise false
196      */
197     private boolean isPluginXPage( String strPage, Plugin plugin )
198     {
199         if ( ( strPage != null ) )
200         {
201             for ( XPageApplicationEntry app : plugin.getApplications(  ) )
202             {
203                 if ( strPage.equals( app.getId(  ) ) )
204                 {
205                     return true;
206                 }
207             }
208         }
209 
210         return false;
211     }
212 }