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