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.service.portal;
35  
36  import fr.paris.lutece.portal.service.content.ContentService;
37  import fr.paris.lutece.portal.service.content.PageData;
38  import fr.paris.lutece.portal.service.content.XPageAppService;
39  import fr.paris.lutece.portal.service.includes.PageInclude;
40  import fr.paris.lutece.portal.service.includes.PageIncludeService;
41  import fr.paris.lutece.portal.service.message.SiteMessage;
42  import fr.paris.lutece.portal.service.message.SiteMessageException;
43  import fr.paris.lutece.portal.service.message.SiteMessageService;
44  import fr.paris.lutece.portal.service.security.UserNotSignedException;
45  import fr.paris.lutece.portal.service.template.AppTemplateService;
46  import fr.paris.lutece.portal.service.util.AppLogService;
47  import fr.paris.lutece.portal.service.util.AppPathService;
48  import fr.paris.lutece.portal.web.constants.Markers;
49  import fr.paris.lutece.portal.web.xpages.XPage;
50  import fr.paris.lutece.portal.web.xpages.XPageApplication;
51  import fr.paris.lutece.portal.web.xpages.XPageApplicationEntry;
52  import fr.paris.lutece.util.html.HtmlTemplate;
53  
54  import java.io.File;
55  
56  import java.util.Collection;
57  import java.util.HashMap;
58  
59  import javax.servlet.http.HttpServletRequest;
60  
61  
62  /**
63   * This class delivers Extra pages (xpages) to web components. An XPage is a page where the content is provided by a
64   * specific class, but should be integrated into the portal struture and design. XPageApps are identified by a key
65   * name. To display an XPage into the portal just call the following url :<br><code>
66   * RunStandaloneApp.jsp?page=<i>keyname</i>&amp;param1=value1&amp; ...&amp;paramN=valueN </code>
67   *
68   * @see fr.paris.lutece.portal.web.xpages.XPage
69   */
70  public class StandaloneAppService extends ContentService
71  {
72      public static final String PARAM_STANDALONE_APP = "page";
73      private static final String PROPERTY_PATH_LUTECE_PLUGINS = "path.lutece.plugins";
74      private static final String TEMPLATE_PAGE_FRAMESET = "page_frameset.html";
75      private static final String TEMPLATE_STANDALONE_PAGE_FRAMESET = "skin/site/standalone_app_frameset.html";
76      private static final String CONTENT_SERVICE_NAME = "StandaloneAppService";
77      private static final String MESSAGE_ERROR_APP_BODY = "portal.util.message.errorXpageApp";
78  
79      /**
80       * Returns the Content Service name
81       *
82       * @return The name as a String
83       */
84      public String getName(  )
85      {
86          return CONTENT_SERVICE_NAME;
87      }
88  
89      /**
90       * Analyzes request parameters to see if the request should be handled by the current Content Service
91       *
92       * @param request The HTTP request
93       * @return true if this ContentService should handle this request
94       */
95      public boolean isInvoked( HttpServletRequest request )
96      {
97          String strStandaloneApp = request.getParameter( PARAM_STANDALONE_APP );
98  
99          if ( ( strStandaloneApp != null ) && ( strStandaloneApp.length(  ) > 0 ) )
100         {
101             return true;
102         }
103 
104         return false;
105     }
106 
107     /**
108      * Enable or disable the cache feature.
109      *
110      * @param bCache true to enable the cache, false to disable
111      */
112     public void setCache( boolean bCache )
113     {
114     }
115 
116     /**
117      * Gets the current cache status.
118      *
119      * @return true if enable, otherwise false
120      */
121     public boolean isCacheEnable(  )
122     {
123         return false;
124     }
125 
126     /**
127      * Reset the cache.
128      */
129     public void resetCache(  )
130     {
131     }
132 
133     /**
134      * Gets the number of item currently in the cache.
135      *
136      * @return the number of item currently in the cache.
137      */
138     public int getCacheSize(  )
139     {
140         return 0;
141     }
142 
143     /**
144      * Build the XPage content.
145      *
146      * @param request The HTTP request.
147      * @param nMode The current mode.
148      * @return The HTML code of the page.
149      * @throws UserNotSignedException a userNotSignedException
150      * @throws SiteMessageException occurs when a site message need to be displayed
151      */
152     public String getPage( HttpServletRequest request, int nMode )
153         throws UserNotSignedException, SiteMessageException
154     {
155         // Gets XPage info from the lutece.properties
156         String strName = request.getParameter( PARAM_STANDALONE_APP );
157 
158         if ( ( strName == null ) || ( strName.length(  ) <= 0 ) )
159         {
160             // Return the welcome page
161             return null;
162         }
163 
164         PageData data = new PageData(  );
165         XPageApplicationEntry entry = XPageAppService.getApplicationEntry( strName );
166 
167         if ( ( entry != null ) && ( entry.isEnable(  ) ) )
168         {
169             XPageApplication app = XPageAppService.getApplicationInstance( entry );
170             XPage page = app.getPage( request, nMode, entry.getPlugin(  ) );
171             data.setContent( page.getContent(  ) );
172             data.setName( page.getTitle(  ) );
173         }
174         else
175         {
176             AppLogService.error( "The specified Xpage '" + strName +
177                 "' cannot be retrieved. Check installation of your Xpage application." );
178             SiteMessageService.setMessage( request, MESSAGE_ERROR_APP_BODY, SiteMessage.TYPE_ERROR );
179         }
180 
181         return buildPageContent( data, nMode, strName, request );
182     }
183 
184     /**
185      * Returns the html code which represents the page content
186      *
187      * @param data The structure which contains the informations about the page
188      * @param nMode The mode in which displaying the page : normal or administration
189      * @param strPluginName The name of the plugin
190      * @param request TheHttpServletRequest
191      * @return The html code of a page
192      */
193     private static String buildPageContent( PageData data, int nMode, String strPluginName, HttpServletRequest request )
194     {
195         HtmlTemplate template;
196         String strFileName = strPluginName + "/" + TEMPLATE_PAGE_FRAMESET;
197         String strFilePath = AppPathService.getPath( PROPERTY_PATH_LUTECE_PLUGINS, strFileName );
198         File file = new File( strFilePath );
199         String strPluginPath = "skin/plugins/";
200 
201         // Load of the templates
202         HashMap<String, Object> model = new HashMap<String, Object>(  );
203         model.put( Markers.BASE_URL, AppPathService.getBaseUrl( request ) );
204         model.put( Markers.PAGE_NAME, data.getName(  ) );
205         model.put( Markers.PAGE_CONTENT, data.getContent(  ) );
206 
207         Collection<PageInclude> colIncludes = PageIncludeService.getIncludes(  );
208 
209         for ( PageInclude pic : colIncludes )
210         {
211             pic.fillTemplate( model, data, nMode, request );
212         }
213 
214         if ( file.exists(  ) )
215         {
216             template = AppTemplateService.getTemplate( strPluginPath + strFileName, request.getLocale(  ), model );
217         }
218         else
219         {
220             template = AppTemplateService.getTemplate( TEMPLATE_STANDALONE_PAGE_FRAMESET, request.getLocale(  ), model );
221         }
222 
223         return template.getHtml(  );
224     }
225 }