View Javadoc
1   /*
2    * Copyright (c) 2002-2017, 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.plugins.xmlpage.web;
35  
36  import fr.paris.lutece.plugins.xmlpage.service.XmlPageElement;
37  import fr.paris.lutece.plugins.xmlpage.service.XmlPageService;
38  import fr.paris.lutece.plugins.xmlpage.util.XmlPageContentUtils;
39  import fr.paris.lutece.portal.service.plugin.Plugin;
40  import fr.paris.lutece.portal.service.template.AppTemplateService;
41  import fr.paris.lutece.portal.service.util.AppPropertiesService;
42  import fr.paris.lutece.portal.web.xpages.XPage;
43  import fr.paris.lutece.portal.web.xpages.XPageApplication;
44  import fr.paris.lutece.util.html.HtmlTemplate;
45  
46  import java.util.HashMap;
47  import java.util.Map;
48  
49  import javax.servlet.http.HttpServletRequest;
50  
51  
52  /**
53   * This class manages Xml Page.
54   */
55  public class XmlPageApp implements XPageApplication
56  {
57      // Templates
58      private static final String TEMPLATE_PAGE_XMLPAGE = "skin/plugins/xmlpage/page_xmlpage.html";
59  
60      //parameters
61      private static final String PARAMETER_XMLPAGE_NAME = "xmlpage";
62      private static final String PARAMETER_XMLPAGE_STYLE = "style";
63  
64      //properties
65      private static final String PROPERTY_ERROR_PAGE_TITLE = "xmlpage.error.pageTitle";
66      private static final String PROPERTY_ERROR_PAGE_PATH = "xmlpage.error.pagePathLabel";
67      private static final String PROPERTY_ERROR_MISSING_PARAMETERS = "xmlpage.error.message.missing.parameters";
68      private static final String PROPERTY_ERROR_PAGE_NOT_FOUND = "xmlpage.error.message.page.not.found";
69      private static final String PROPERTY_ERROR_UNKNOWN = "xmlpage.error.message.unknown";
70      private static final String PROPERTY_TRANSFORM_CACHE = "xmlpage.transform.cache";
71      private static final String PROPERTY_PARAMETERS_LIST = "xmlpage.parameters.list";
72      private static final String PROPERTY_PARAMETERS_LIST_SEPARATOR = "xmlpage.parameters.list.separator";
73  
74      //bookmarks
75      private static final String BOOKMARK_PAGE_CONTENT = "@page_content@";
76  
77      //utils
78      private static final String EMPTY_STRING = "";
79  
80      /**
81       * Returns the page.
82       * It is composed of the title, page path and content.
83       * The content is the result of the xml/xsl transformation.
84       * Xml data is retrieved from the "xmlpage" parameter.
85       * Xsl data is retrieved from the "style" parameter.
86       *
87       * @param request The http request
88       * @param nMode The current mode
89       * @param plugin The plugin object
90       * @return the Content of the page Contact
91       */
92      public XPage getPage( HttpServletRequest request, int nMode, Plugin plugin )
93      {
94          XPage page = new XPage(  );
95  
96          String strTransformCache = AppPropertiesService.getProperty( PROPERTY_TRANSFORM_CACHE );
97          String strXmlPageName = request.getParameter( PARAMETER_XMLPAGE_NAME );
98          String strStyle = request.getParameter( PARAMETER_XMLPAGE_STYLE );
99          String strPageContent;
100         String strPageTitle;
101         String strPagePath;
102 
103         boolean bTransformCacheActive = Boolean.parseBoolean( strTransformCache );
104         Map<String, String> mapParameters = null;
105 
106         if ( !bTransformCacheActive )
107         {
108             String strParametersList = AppPropertiesService.getProperty( PROPERTY_PARAMETERS_LIST );
109             String strParametersListSeparator = AppPropertiesService.getProperty( PROPERTY_PARAMETERS_LIST_SEPARATOR );
110             String[] listParameters = strParametersList.split( strParametersListSeparator );
111 
112             mapParameters = new HashMap<String, String>(  );
113 
114             for ( String currentParameter : listParameters )
115             {
116                 if ( request.getParameter( currentParameter ) != null )
117                 {
118                     mapParameters.put( currentParameter, request.getParameter( currentParameter ) );
119                 }
120             }
121         }
122 
123         if ( ( strXmlPageName != null ) && !EMPTY_STRING.equals( strXmlPageName ) && ( strStyle != null ) &&
124                 !EMPTY_STRING.equals( strStyle ) )
125         {
126             XmlPageElement xmlPageElement = XmlPageService.getInstance(  ).getXmlPageResource( strXmlPageName );
127 
128             if ( xmlPageElement != null )
129             {
130                 strPageContent = XmlPageContentUtils.getContent( xmlPageElement, strStyle, bTransformCacheActive,
131                         mapParameters );
132 
133                 // if the strPageContent is null, this means that there has been a problem when getting the content
134                 if ( strPageContent == null )
135                 {
136                     strPageContent = AppPropertiesService.getProperty( PROPERTY_ERROR_UNKNOWN );
137                     strPageTitle = AppPropertiesService.getProperty( PROPERTY_ERROR_PAGE_TITLE );
138                     strPagePath = AppPropertiesService.getProperty( PROPERTY_ERROR_PAGE_PATH );
139                 }
140                 else
141                 {
142                     strPageTitle = xmlPageElement.getTitle(  );
143                     strPagePath = xmlPageElement.getTitle(  );
144                 }
145             }
146             else
147             {
148                 strPageContent = AppPropertiesService.getProperty( PROPERTY_ERROR_PAGE_NOT_FOUND );
149                 strPageTitle = AppPropertiesService.getProperty( PROPERTY_ERROR_PAGE_TITLE );
150                 strPagePath = AppPropertiesService.getProperty( PROPERTY_ERROR_PAGE_PATH );
151             }
152         }
153         else
154         {
155             strPageContent = AppPropertiesService.getProperty( PROPERTY_ERROR_MISSING_PARAMETERS );
156             strPageTitle = AppPropertiesService.getProperty( PROPERTY_ERROR_PAGE_TITLE );
157             strPagePath = AppPropertiesService.getProperty( PROPERTY_ERROR_PAGE_PATH );
158         }
159 
160         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_PAGE_XMLPAGE );
161         template.substitute( BOOKMARK_PAGE_CONTENT, strPageContent );
162         page.setContent( template.getHtml(  ) );
163         page.setTitle( strPageTitle );
164         page.setPathLabel( strPagePath );
165 
166         return page;
167     }
168 }