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.wrapper.web;
35  
36  import fr.paris.lutece.plugins.wrapper.business.Wrapper;
37  import fr.paris.lutece.plugins.wrapper.business.WrapperHome;
38  import fr.paris.lutece.portal.service.message.SiteMessage;
39  import fr.paris.lutece.portal.service.message.SiteMessageException;
40  import fr.paris.lutece.portal.service.message.SiteMessageService;
41  import fr.paris.lutece.portal.service.plugin.Plugin;
42  import fr.paris.lutece.portal.service.plugin.PluginService;
43  import fr.paris.lutece.portal.service.security.SecurityService;
44  import fr.paris.lutece.portal.service.template.AppTemplateService;
45  import fr.paris.lutece.portal.service.util.AppPropertiesService;
46  import fr.paris.lutece.portal.web.xpages.XPage;
47  import fr.paris.lutece.portal.web.xpages.XPageApplication;
48  import fr.paris.lutece.util.html.HtmlTemplate;
49  
50  import java.util.ArrayList;
51  import java.util.Collection;
52  import java.util.HashMap;
53  
54  import javax.servlet.http.HttpServletRequest;
55  
56  
57  /**
58   * This class manages Wrapper page.
59   */
60  public class WrapperApp implements XPageApplication
61  {
62      //////////////////////////////////////////////////////////////////////////////////////////////
63      // Constants
64  
65      // Parameters
66      private static final String PARAMETER_PAGE = "page";
67      private static final String PARAMETER_WRAPPER = "wrapper";
68      private static final String PARAMETER_WRAPPER_ID = "wrapper_id";
69  
70      // Properties
71      private static final String PROPERTY_PAGE_TITLE = "wrapper.pageTitle";
72      private static final String PROPERTY_PAGE_PATH = "wrapper.pagePathLabel";
73  
74      // Messages
75      private static final String PROPERTY_MESSAGE_ERROR_WRAPPER = "wrapper.message.pageInvalid";
76      private static final String PROPERTY_MESSAGE_NOT_AUTHORIZED = "wrapper.message.notAuthorized";
77  
78      // Markers
79      private static final String MARK_WRAPPER_LIST = "wrappers_list";
80      private static final String MARK_WRAPPER = "wrapper";
81      private static final String MARK_PAGE = "page";
82  
83      // Templates
84      private static final String TEMPLATE_XPAGE_WRAPPER = "skin/plugins/wrapper/page_wrapper.html";
85      private static final String TEMPLATE_XPAGE_WRAPPER_LISTS = "skin/plugins/wrapper/wrappers_list.html";
86  
87      // Constants
88      private static final String EMPTY_STRING = "";
89  
90      // private fields
91      private Plugin _plugin;
92  
93      /** Creates a new instance of WrapperApp */
94      public WrapperApp(  )
95      {
96      }
97  
98      /**
99       * Returns the content of the page Wrapper. It is composed by a form which to capture the data to send a message to
100      * a contact of the portal.
101      * @return the Content of the page Contact
102      * @param request The http request
103      * @param nMode The current mode
104      * @param plugin The plugin object
105      * @throws fr.paris.lutece.portal.service.message.SiteMessageException Message displayed if an exception occures
106      */
107     public XPage getPage( HttpServletRequest request, int nMode, Plugin plugin )
108         throws SiteMessageException
109     {
110         XPage page = new XPage(  );
111 
112         String strPluginName = request.getParameter( PARAMETER_PAGE );
113         _plugin = PluginService.getPlugin( strPluginName );
114 
115         page.setTitle( AppPropertiesService.getProperty( PROPERTY_PAGE_TITLE ) );
116         page.setPathLabel( AppPropertiesService.getProperty( PROPERTY_PAGE_PATH ) );
117 
118         String strWrapperId = request.getParameter( PARAMETER_WRAPPER_ID );
119 
120         if ( strWrapperId == null )
121         {
122             page.setContent( getWrappersLists( request ) );
123         }
124 
125         if ( ( strWrapperId != null ) )
126         {
127             page.setContent( getWrapper( request, strWrapperId ) );
128         }
129 
130         return page;
131     }
132 
133     private String getWrappersLists( HttpServletRequest request )
134         throws SiteMessageException
135     {
136         HashMap model = new HashMap(  );
137 
138         Collection<Wrapper> wrapperList = WrapperHome.findEnabledWrapperList( _plugin );
139         Collection<Wrapper> visibleWrapperList = new ArrayList(  ); // filter the list of lists by role
140 
141         for ( Wrapper wrapper : wrapperList )
142         {
143             if ( isVisible( request, wrapper.getRole(  ) ) )
144             {
145                 visibleWrapperList.add( wrapper );
146             }
147         }
148 
149         model.put( MARK_WRAPPER_LIST, visibleWrapperList );
150 
151         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_XPAGE_WRAPPER_LISTS, request.getLocale(  ),
152                 model );
153 
154         return template.getHtml(  );
155     }
156 
157     /**
158      * Returns the wrapper page
159      * @param request The Html request
160      * @param plugin The plugin
161      * @return The Html template
162      */
163     private String getWrapper( HttpServletRequest request, String strWrapperId )
164         throws SiteMessageException
165     {
166         HashMap model = new HashMap(  );
167 
168         int nWrapperId = Integer.parseInt( strWrapperId );
169         Wrapper wrapper = WrapperHome.findByPrimaryKey( nWrapperId, _plugin );
170 
171         if ( wrapper != null )
172         {
173             int nStatus = wrapper.getStatus(  );
174 
175             if ( ( nStatus == 0 ) && ( isVisible( request, wrapper.getRole(  ) ) ) )
176             {
177                 model.put( MARK_WRAPPER, wrapper );
178                 model.put( MARK_PAGE, _plugin.getName(  ) );
179             }
180             else
181             {
182                 SiteMessageService.setMessage( request, PROPERTY_MESSAGE_NOT_AUTHORIZED, SiteMessage.TYPE_ERROR );
183             }
184         }
185         else
186         {
187             SiteMessageService.setMessage( request, PROPERTY_MESSAGE_ERROR_WRAPPER, SiteMessage.TYPE_ERROR );
188         }
189 
190         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_XPAGE_WRAPPER, request.getLocale(  ), model );
191 
192         return template.getHtml(  );
193     }
194 
195     /**
196      * Checks if the page is visible for the current user
197      * @param request The HTTP request
198      * @return true if the page could be shown to the user
199      * @since v1.3.1
200      */
201     private boolean isVisible( HttpServletRequest request, String strRole )
202     {
203         if ( ( strRole == null ) || ( strRole.trim(  ).equals( EMPTY_STRING ) ) )
204         {
205             return true;
206         }
207 
208         if ( !strRole.equals( Wrapper.ROLE_NONE ) && SecurityService.isAuthenticationEnable(  ) )
209         {
210             return SecurityService.getInstance(  ).isUserInRole( request, strRole );
211         }
212 
213         return true;
214     }
215 }