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.html.modules.rest.rs;
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.rest.service.RestConstants;
39  import fr.paris.lutece.plugins.rest.util.json.JSONUtil;
40  import fr.paris.lutece.plugins.rest.util.xml.XMLUtil;
41  import fr.paris.lutece.portal.business.portlet.PortletHome;
42  import fr.paris.lutece.portal.service.html.HtmlCleanerException;
43  import fr.paris.lutece.portal.service.html.HtmlCleanerService;
44  import fr.paris.lutece.portal.service.message.SiteMessageException;
45  import fr.paris.lutece.portal.service.template.AppTemplateService;
46  import fr.paris.lutece.portal.service.util.AppPathService;
47  import fr.paris.lutece.util.html.HtmlTemplate;
48  
49  import net.sf.json.JSONObject;
50  
51  import java.util.HashMap;
52  import java.util.Map;
53  
54  import javax.servlet.http.HttpServletRequest;
55  
56  import javax.ws.rs.Consumes;
57  import javax.ws.rs.FormParam;
58  import javax.ws.rs.GET;
59  import javax.ws.rs.POST;
60  import javax.ws.rs.Path;
61  import javax.ws.rs.PathParam;
62  import javax.ws.rs.Produces;
63  import javax.ws.rs.core.Context;
64  import javax.ws.rs.core.MediaType;
65  
66  
67  /**
68   * QuizRest
69   */
70  @Path( RestConstants.BASE_PATH + "html" )
71  /**
72   *
73   * @author pierre
74   */
75  public class HtmlPortletRest
76  {
77      private static final String TEMPLATE_WADL = "admin/plugins/html/modules/rest/wadl.xml";
78      private static final String MARK_BASE_URL = "base_url";
79      private static final String HTML = "html";
80  
81      @GET
82      @Path( "wadl" )
83      @Produces( MediaType.APPLICATION_XML )
84      public String getWADL( @Context
85      HttpServletRequest request )
86      {
87          String strBase = AppPathService.getBaseUrl( request ) + RestConstants.BASE_PATH + "html";
88          Map model = new HashMap(  );
89          model.put( MARK_BASE_URL, strBase );
90  
91          HtmlTemplate t = AppTemplateService.getTemplate( TEMPLATE_WADL, request.getLocale(  ), model );
92  
93          return t.getHtml(  );
94      }
95  
96      @GET
97      @Path( "/portlet/{id}" )
98      @Produces( MediaType.APPLICATION_XML )
99      public String getPortletXml( @PathParam( "id" )
100     String strId ) throws SiteMessageException
101     {
102         StringBuilder sbXML = new StringBuilder(  );
103 
104         try
105         {
106             int nId = Integer.parseInt( strId );
107             HtmlPortlet portlet = (HtmlPortlet) PortletHome.findByPrimaryKey( nId );
108 
109             if ( portlet != null )
110             {
111                 sbXML.append( "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" );
112                 addPortletXml( sbXML, portlet );
113             }
114         }
115         catch ( NumberFormatException e )
116         {
117             sbXML.append( XMLUtil.formatError( "Invalid portlet number", 3 ) );
118         }
119         catch ( Exception e )
120         {
121             sbXML.append( XMLUtil.formatError( "Portlet not found", 1 ) );
122         }
123 
124         return sbXML.toString(  );
125     }
126 
127     @GET
128     @Path( "/portlet/{id}" )
129     @Produces( MediaType.APPLICATION_JSON )
130     public String getPortletJson( @PathParam( "id" )
131     String strId ) throws SiteMessageException
132     {
133         String strJSON = "";
134 
135         try
136         {
137             int nId = Integer.parseInt( strId );
138             HtmlPortlet portlet = (HtmlPortlet) PortletHome.findByPrimaryKey( nId );
139 
140             if ( portlet != null )
141             {
142                 JSONObject json = new JSONObject(  );
143                 json.accumulate( RestPortletConstants.ID_PORTLET, portlet.getId(  ) );
144                 json.accumulate( RestPortletConstants.NAME, portlet.getName(  ) );
145                 json.accumulate( RestPortletConstants.TYPE, portlet.getPortletTypeId(  ) );
146                 json.accumulate( RestPortletConstants.ID_PAGE, portlet.getPageId(  ) );
147                 json.accumulate( RestPortletConstants.ID_STYLE, portlet.getStyleId(  ) );
148                 json.accumulate( RestPortletConstants.COLUMN, portlet.getColumn(  ) );
149                 json.accumulate( RestPortletConstants.ORDER, portlet.getOrder(  ) );
150                 json.accumulate( RestPortletConstants.ACCEPT_ALIAS, portlet.getAcceptAlias(  ) );
151                 json.accumulate( RestPortletConstants.DISPLAY_PORTLET_TITLE, portlet.getDisplayPortletTitle(  ) );
152                 json.accumulate( HTML, portlet.getHtml(  ) );
153 
154                 JSONObject jsonPortlet = new JSONObject(  );
155                 jsonPortlet.accumulate( "portlet", json );
156                 strJSON = jsonPortlet.toString( 4 );
157             }
158         }
159         catch ( NumberFormatException e )
160         {
161             strJSON = JSONUtil.formatError( "Invalid portlet number", 3 );
162         }
163         catch ( Exception e )
164         {
165             strJSON = JSONUtil.formatError( "Portlet not found", 1 );
166         }
167 
168         return strJSON;
169     }
170 
171     @POST
172     @Produces( MediaType.TEXT_HTML )
173     @Consumes( MediaType.APPLICATION_FORM_URLENCODED )
174     public String createPortlet( @FormParam( "id_portlet" )
175     String strId, @FormParam( RestPortletConstants.ID_PAGE )
176     String strPageId, @FormParam( RestPortletConstants.NAME )
177     String strName, @FormParam( RestPortletConstants.ID_STYLE )
178     String strStyleId, @FormParam( RestPortletConstants.COLUMN )
179     String strColumn, @FormParam( RestPortletConstants.ORDER )
180     String strOrder, @FormParam( RestPortletConstants.ACCEPT_ALIAS )
181     String strAcceptAlias, @FormParam( RestPortletConstants.DISPLAY_PORTLET_TITLE )
182     String strDisplayPortletTitle, @FormParam( HTML )
183     String strHtml ) throws HtmlCleanerException
184     {
185         boolean bUpdate = ( strId != null );
186         HtmlPortlet portlet = null;
187 
188         if ( bUpdate )
189         {
190             portlet = (HtmlPortlet) HtmlPortletHome.findByPrimaryKey( Integer.parseInt( strId ) );
191         }
192         else
193         {
194             portlet = new HtmlPortlet(  );
195         }
196 
197         strPageId = getNotNull( strPageId, "1" );
198         strStyleId = getNotNull( strStyleId, "100" );
199         strColumn = getNotNull( strColumn, "1" );
200         strOrder = getNotNull( strOrder, "1" );
201 
202         String strPortletTypeId = "HTML_PORTLET";
203         strAcceptAlias = getNotNull( strAcceptAlias, "0" );
204         strDisplayPortletTitle = getNotNull( strDisplayPortletTitle, "0" );
205 
206         strName = strName.replaceAll( "\"", "" );
207 
208         // Check Mandatory fields
209         if ( strName.equals( "" ) || ( strStyleId == null ) || ( strStyleId.trim(  ).equals( "" ) ) ||
210                 strOrder.equals( "" ) || strColumn.equals( "" ) || strStyleId.equals( "" ) ||
211                 strAcceptAlias.equals( "" ) || strDisplayPortletTitle.equals( "" ) )
212         {
213             // Error
214         }
215 
216         int nPageId = Integer.parseInt( strPageId );
217         int nOrder = Integer.parseInt( strOrder );
218         int nColumn = Integer.parseInt( strColumn );
219         int nStyleId = Integer.parseInt( strStyleId );
220         int nAcceptAlias = Integer.parseInt( strAcceptAlias );
221         int nDisplayPortletTitle = Integer.parseInt( strDisplayPortletTitle );
222 
223         portlet.setPageId( nPageId );
224         portlet.setName( strName );
225         portlet.setOrder( nOrder );
226         portlet.setColumn( nColumn );
227         portlet.setStyleId( nStyleId );
228         portlet.setAcceptAlias( nAcceptAlias );
229         portlet.setDisplayPortletTitle( nDisplayPortletTitle );
230         portlet.setPortletTypeId( strPortletTypeId );
231         portlet.setHtml( HtmlCleanerService.clean( strHtml ) );
232 
233         String strReturn;
234 
235         if ( bUpdate )
236         {
237             HtmlPortletHome.getInstance(  ).update( portlet );
238             strReturn = "Portlet updated successfully";
239         }
240         else
241         {
242             // creates the portlet
243             HtmlPortletHome.getInstance(  ).create( portlet );
244             strReturn = "Portlet created successfully";
245         }
246 
247         return strReturn;
248     }
249 
250     private String getNotNull( String strSrc, String strDefault )
251     {
252         return ( strSrc != null ) ? strSrc : strDefault;
253     }
254 
255     private void addPortletXml( StringBuilder sbXML, HtmlPortlet portlet )
256     {
257         sbXML.append( "<portlet>\n" );
258         sbXML.append( "<portlet-name>" ).append( portlet.getName(  ) ).append( "</portlet-name>\n" );
259         sbXML.append( "<portlet-id>" ).append( portlet.getId(  ) ).append( "</portlet-id>\n" );
260         sbXML.append( "<page-id>" ).append( portlet.getPageId(  ) ).append( "</page-id>\n" );
261         sbXML.append( "<display-portlet-title>" ).append( portlet.getDisplayPortletTitle(  ) )
262              .append( "</display-portlet-title>\n" );
263         sbXML.append( "<column>" ).append( portlet.getColumn(  ) ).append( "</column>\n" );
264         sbXML.append( "<order>" ).append( portlet.getOrder(  ) ).append( "</order>\n" );
265         sbXML.append( "<portlet-type>" ).append( portlet.getPortletTypeId(  ) ).append( "</portlet-type>\n" );
266         sbXML.append( "<style-id>" ).append( portlet.getStyleId(  ) ).append( "</style-id>\n" );
267         sbXML.append( "<status>" ).append( portlet.getStatus(  ) ).append( "</status>\n" );
268         sbXML.append( "<html-portlet-content>" ).append( portlet.getHtml(  ) ).append( "</html-portlet-content>\n" );
269         sbXML.append( "</portlet>" );
270     }
271 }