View Javadoc
1   /*
2    * Copyright (c) 2002-2022, City of 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;
35  
36  import java.util.ArrayList;
37  import java.util.Collection;
38  import java.util.Collections;
39  import java.util.HashMap;
40  import java.util.List;
41  import java.util.Locale;
42  
43  import javax.servlet.http.HttpServletRequest;
44  
45  import fr.paris.lutece.portal.service.content.ContentService;
46  import fr.paris.lutece.portal.service.content.XPageAppService;
47  import fr.paris.lutece.portal.service.message.ISiteMessageHandler;
48  import fr.paris.lutece.portal.service.message.SiteMessageException;
49  import fr.paris.lutece.portal.service.plugin.Plugin;
50  import fr.paris.lutece.portal.service.plugin.PluginService;
51  import fr.paris.lutece.portal.service.portal.StandaloneAppService;
52  import fr.paris.lutece.portal.service.security.UserNotSignedException;
53  import fr.paris.lutece.portal.service.spring.SpringContextService;
54  import fr.paris.lutece.portal.service.template.AppTemplateService;
55  import fr.paris.lutece.portal.service.util.AppPathService;
56  import fr.paris.lutece.portal.web.xpages.XPageApplicationEntry;
57  import fr.paris.lutece.util.html.HtmlTemplate;
58  
59  /**
60   * Class of the StandaloneAppJspBean object.
61   */
62  public class StandaloneAppJspBean
63  {
64      // //////////////////////////////////////////////////////////////////////////
65      // Constants
66      private static final int MODE_HTML = 0;
67      private static final String TEMPLATE_STANDALONE = "skin/site/standalone_app.html";
68      private static final String MARK_ENTRY_LIST = "entry_list";
69      private static final String MARK_BASE_URL = "base_url";
70      private static final String MARK_CORE_PLUGIN = "core_plugin";
71      private static final String BEAN_SITE_MESSAGE_HANDLER = "siteMessageHandler";
72  
73      /**
74       * Returns the content of a page according to the parameters found in the http request. One distinguishes article, page and xpage and the mode.
75       *
76       * @param request
77       *            The http request
78       * @return the html code for the display of a page of a site
79       * @throws UserNotSignedException
80       *             The UserNotSignedException
81       * @throws SiteMessageException
82       *             occurs when a site message need to be displayed
83       */
84      public String getContent( HttpServletRequest request ) throws UserNotSignedException, SiteMessageException
85      {
86          return getContent( request, MODE_HTML );
87      }
88  
89      /**
90       * Returns the content of a page according to the parameters found in the http request. One distinguishes article, page and xpage and the mode.
91       *
92       * @param request
93       *            The http request
94       * @param nMode
95       *            The mode (normal or administration)
96       * @return the html code for the display of a page of a site
97       * @throws UserNotSignedException
98       *             The UserNotSignedException
99       * @throws SiteMessageException
100      *             occurs when a site message need to be displayed
101      */
102     public String getContent( HttpServletRequest request, int nMode ) throws UserNotSignedException, SiteMessageException
103     {
104         // Handle site messages first
105         ISiteMessageHandler handlerSiteMessage = SpringContextService.getBean( BEAN_SITE_MESSAGE_HANDLER );
106 
107         if ( handlerSiteMessage.hasMessage( request ) )
108         {
109             return handlerSiteMessage.getPage( request, nMode );
110         }
111 
112         ContentService csStandalone = new StandaloneAppService( );
113         String htmlPage = csStandalone.getPage( request, nMode );
114 
115         if ( htmlPage == null )
116         {
117             // Return the welcome page
118             return getPluginList( request );
119         }
120 
121         return htmlPage;
122     }
123 
124     /**
125      * Display the list of plugins app installed on the instance of lutece
126      *
127      * @param request
128      *            The HTTP request
129      * @return the list
130      */
131     public String getPluginList( HttpServletRequest request )
132     {
133         HashMap<String, Object> modelList = new HashMap<>( );
134         Collection<XPageApplicationEntry> entryList = new ArrayList<>( );
135         Locale locale = ( request == null ) ? null : request.getLocale( );
136 
137         Collection<XPageApplicationEntry> applications = XPageAppService.getXPageApplicationsList( );
138         List<XPageApplicationEntry> applicationsSorted = new ArrayList<>( applications );
139 
140         Collections.sort( applicationsSorted, ( XPageApplicationEntry../../fr/paris/lutece/portal/web/xpages/XPageApplicationEntry.html#XPageApplicationEntry">XPageApplicationEntry c1, XPageApplicationEntry c2 ) -> {
141             Plugin p1 = ( c1.getPlugin( ) == null ) ? PluginService.getCore( ) : c1.getPlugin( );
142             Plugin p2 = ( c2.getPlugin( ) == null ) ? PluginService.getCore( ) : c2.getPlugin( );
143 
144             return p1.getName( ).compareTo( p2.getName( ) );
145         } );
146 
147         // Scan of the list
148         for ( XPageApplicationEntry entry : applicationsSorted )
149         {
150             if ( entry.isEnable( ) )
151             {
152                 entryList.add( entry );
153             }
154         }
155 
156         // Insert the rows in the list
157         modelList.put( MARK_ENTRY_LIST, entryList );
158         modelList.put( MARK_BASE_URL, AppPathService.getBaseUrl( request ) );
159         modelList.put( MARK_CORE_PLUGIN, PluginService.getCore( ) );
160 
161         HtmlTemplate templateList = AppTemplateService.getTemplate( TEMPLATE_STANDALONE, locale, modelList );
162 
163         return templateList.getHtml( );
164     }
165 }