View Javadoc
1   /*
2    * Copyright (c) 2002-2018, 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.html.web.portlet;
35  
36  import fr.paris.lutece.plugins.html.business.portlet.HtmlPortlet;
37  import fr.paris.lutece.plugins.html.business.portlet.HtmlPortletHome;
38  import fr.paris.lutece.plugins.html.business.portlet.IHtmlPortlet;
39  import fr.paris.lutece.plugins.html.business.portlet.UntransformedHtmlPortlet;
40  import fr.paris.lutece.plugins.html.business.portlet.UntransformedHtmlPortletHome;
41  import fr.paris.lutece.portal.business.portlet.Portlet;
42  import fr.paris.lutece.portal.business.portlet.PortletHome;
43  import fr.paris.lutece.portal.service.admin.AdminUserService;
44  import fr.paris.lutece.portal.service.html.HtmlCleanerException;
45  import fr.paris.lutece.portal.service.html.HtmlCleanerService;
46  import fr.paris.lutece.portal.service.message.AdminMessage;
47  import fr.paris.lutece.portal.service.message.AdminMessageService;
48  import fr.paris.lutece.portal.service.util.AppPathService;
49  import fr.paris.lutece.portal.web.constants.Messages;
50  import fr.paris.lutece.portal.web.portlet.PortletJspBean;
51  import fr.paris.lutece.util.html.HtmlTemplate;
52  
53  import java.util.HashMap;
54  import java.util.Map;
55  
56  import javax.servlet.http.HttpServletRequest;
57  
58  /**
59   * This class provides the user interface to manage Html Portlet features
60   */
61  public class HtmlPortletJspBean extends PortletJspBean
62  {
63      // //////////////////////////////////////////////////////////////////////////
64      // Constants
65  
66      // Parameter
67      private static final String PARAMETER_CONTENT_HTML = "html_content";
68      private static final String MARK_WEBAPP_URL = "webapp_url";
69      private static final String MARK_LOCALE = "locale";
70      private static final String MARK_HTML_CONTENT = "html_content";
71      private static final String MARK_EDITOR = "editor";
72  
73      private static final String PORTLET_TYPE_LEGACY_HTML = "HTML_PORTLET";
74      private static final String PORTLET_TYPE_UNTRANSFORMED_HTML = "HTML_UNTRANSFORMED_PORTLET";
75  
76      private static final String MESSAGE_INVALID_PORTLET_TYPE_ERROR = "html.message.invalidPortletType";
77  
78      private static final long serialVersionUID = 1L;
79      private String _strPortletTypeId;
80  
81      /**
82       * Returns the properties prefix used for html portlet and defined in lutece.properties file
83       *
84       * @return the value of the property prefix
85       */
86      public String getPropertiesPrefix( )
87      {
88          return "portlet.html";
89      }
90  
91      /**
92       * Returns the Html Portlet form of creation
93       *
94       * @param request
95       *            The Http rquest
96       * @return the html code of the html portlet form
97       */
98      @Override
99      public String getCreate( HttpServletRequest request )
100     {
101         String strPageId = request.getParameter( PARAMETER_PAGE_ID );
102         _strPortletTypeId = request.getParameter( PARAMETER_PORTLET_TYPE_ID );
103 
104         Map<String, Object> model = new HashMap<String, Object>( );
105         model.put( MARK_WEBAPP_URL, AppPathService.getBaseUrl( request ) );
106         model.put( MARK_LOCALE, AdminUserService.getLocale( request ).getLanguage( ) );
107         model.put( MARK_HTML_CONTENT, "" );
108         model.put( MARK_EDITOR, PORTLET_TYPE_LEGACY_HTML.equals( _strPortletTypeId ) );
109 
110         HtmlTemplate template = getCreateTemplate( strPageId, _strPortletTypeId, model );
111 
112         return template.getHtml( );
113     }
114 
115     /**
116      * Returns the Html Portlet form for update
117      *
118      * @param request
119      *            The Http request
120      * @return the html code of the html portlet form
121      */
122     @Override
123     public String getModify( HttpServletRequest request )
124     {
125         String strPortletId = request.getParameter( PARAMETER_PORTLET_ID );
126         int nPortletId = Integer.parseInt( strPortletId );
127         Portlet portlet = PortletHome.findByPrimaryKey( nPortletId );
128         _strPortletTypeId = portlet.getPortletTypeId( );
129         IHtmlPortlet/../../../fr/paris/lutece/plugins/html/business/portlet/IHtmlPortlet.html#IHtmlPortlet">IHtmlPortlet htmlPortlet = (IHtmlPortlet) portlet;
130         Map<String, Object> model = new HashMap<String, Object>( );
131         model.put( MARK_WEBAPP_URL, AppPathService.getBaseUrl( request ) );
132         model.put( MARK_LOCALE, AdminUserService.getLocale( request ).getLanguage( ) );
133         model.put( MARK_HTML_CONTENT, htmlPortlet.getHtml( ) );
134         model.put( MARK_EDITOR, PORTLET_TYPE_LEGACY_HTML.equals( _strPortletTypeId ) );
135 
136         HtmlTemplate template = getModifyTemplate( portlet, model );
137 
138         return template.getHtml( );
139     }
140 
141     /**
142      * Processes the creation form of a new html portlet
143      *
144      * @param request
145      *            The Http request
146      * @return The jsp URL which displays the view of the created html portlet
147      */
148     @Override
149     public String doCreate( HttpServletRequest request )
150     {
151         Portlet portlet;
152         PortletHome portletHome;
153         boolean bClean;
154 
155         if ( _strPortletTypeId.equals( PORTLET_TYPE_LEGACY_HTML ) )
156         {
157             portlet = new HtmlPortlet( );
158             bClean = true;
159             portletHome = HtmlPortletHome.getInstance( );
160         }
161         else
162             if ( _strPortletTypeId.equals( PORTLET_TYPE_UNTRANSFORMED_HTML ) )
163             {
164                 portlet = new UntransformedHtmlPortlet( );
165                 bClean = false;
166                 portletHome = UntransformedHtmlPortletHome.getInstance( );
167             }
168             else
169             {
170                 String strErrorUrl = AdminMessageService.getMessageUrl( request, MESSAGE_INVALID_PORTLET_TYPE_ERROR, AdminMessage.TYPE_STOP );
171                 return strErrorUrl;
172             }
173 
174         // get portlet common attributes
175         String strErrorUrl = setPortletCommonData( request, portlet );
176 
177         if ( strErrorUrl != null )
178         {
179             return strErrorUrl;
180         }
181 
182         // get portlet specific attributes
183         String strPageId = request.getParameter( PARAMETER_PAGE_ID );
184         int nPageId = Integer.parseInt( strPageId );
185         portlet.setPageId( nPageId );
186 
187         // Clean and Insert the content
188         strErrorUrl = doInsertContent( request, (IHtmlPortlet) portlet, bClean );
189 
190         if ( strErrorUrl != null )
191         {
192             return strErrorUrl;
193         }
194 
195         // creates the portlet
196         portletHome.create( portlet );
197 
198         // displays the page with the new portlet
199         return getPageUrl( portlet.getPageId( ) );
200     }
201 
202     /**
203      * Processes the update form of the html portlet whose identifier is in the http request
204      *
205      * @param request
206      *            The Http request
207      * @return The jsp URL which displays the view of the updated portlet
208      */
209     @Override
210     public String doModify( HttpServletRequest request )
211     {
212         // recovers portlet attributes
213         String strPortletId = request.getParameter( PARAMETER_PORTLET_ID );
214         int nPortletId = Integer.parseInt( strPortletId );
215         Portlet portlet = PortletHome.findByPrimaryKey( nPortletId );
216 
217         // get portlet common attributes
218         String strErrorUrl = setPortletCommonData( request, portlet );
219 
220         if ( strErrorUrl != null )
221         {
222             return strErrorUrl;
223         }
224 
225         boolean bClean;
226         if ( _strPortletTypeId.equals( PORTLET_TYPE_LEGACY_HTML ) )
227         {
228             bClean = true;
229         }
230         else
231             if ( _strPortletTypeId.equals( PORTLET_TYPE_UNTRANSFORMED_HTML ) )
232             {
233                 bClean = false;
234             }
235             else
236             {
237                 strErrorUrl = AdminMessageService.getMessageUrl( request, MESSAGE_INVALID_PORTLET_TYPE_ERROR, AdminMessage.TYPE_STOP );
238                 return strErrorUrl;
239             }
240 
241         IHtmlPortlet/../../../fr/paris/lutece/plugins/html/business/portlet/IHtmlPortlet.html#IHtmlPortlet">IHtmlPortlet htmlPortlet = (IHtmlPortlet) portlet;
242 
243         // Clean and Insert the content
244         strErrorUrl = doInsertContent( request, htmlPortlet, bClean );
245 
246         if ( strErrorUrl != null )
247         {
248             return strErrorUrl;
249         }
250 
251         // updates the portlet
252         htmlPortlet.update( );
253 
254         // displays the page with the portlet updated
255         return getPageUrl( portlet.getPageId( ) );
256     }
257 
258     // //////////////////////////////////////////////////////////////////////////////////
259     // Private Implementation
260 
261     /**
262      * Do commons insert
263      * 
264      * @param request
265      *            The HttpServletRequest
266      * @param htmlPortlet
267      *            the HtmlPortlet
268      */
269     private String doInsertContent( HttpServletRequest request, IHtmlPortlet htmlPortlet, boolean bClean )
270     {
271         String strErrorUrl = null;
272 
273         // html code cleaning
274         String strContent = request.getParameter( PARAMETER_CONTENT_HTML );
275 
276         try
277         {
278             if ( bClean )
279             {
280                 strContent = HtmlCleanerService.clean( strContent );
281             }
282             htmlPortlet.setHtml( strContent );
283         }
284         catch( Exception e )
285         {
286             strErrorUrl = AdminMessageService.getMessageUrl( request, Messages.HTML_CLEANER_ERROR, AdminMessage.TYPE_STOP );
287         }
288 
289         return strErrorUrl;
290     }
291 }