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.plugins.directory.modules.rest.rs;
35  
36  import fr.paris.lutece.plugins.directory.business.Directory;
37  import fr.paris.lutece.plugins.directory.business.Record;
38  import fr.paris.lutece.plugins.directory.modules.rest.service.IDirectoryRestService;
39  import fr.paris.lutece.plugins.directory.modules.rest.service.http.DirectoryRestHttpServletRequest;
40  import fr.paris.lutece.plugins.directory.modules.rest.util.constants.DirectoryRestConstants;
41  import fr.paris.lutece.plugins.directory.service.DirectoryPlugin;
42  import fr.paris.lutece.plugins.rest.service.RestConstants;
43  import fr.paris.lutece.portal.service.template.AppTemplateService;
44  import fr.paris.lutece.portal.service.util.AppLogService;
45  import fr.paris.lutece.portal.service.util.AppPathService;
46  import fr.paris.lutece.portal.web.upload.MultipartHttpServletRequest;
47  import fr.paris.lutece.util.html.HtmlTemplate;
48  
49  import org.apache.commons.lang.StringUtils;
50  
51  import java.util.ArrayList;
52  import java.util.HashMap;
53  import java.util.List;
54  import java.util.Map;
55  
56  import javax.servlet.http.HttpServletRequest;
57  
58  import javax.ws.rs.Consumes;
59  import javax.ws.rs.DELETE;
60  import javax.ws.rs.GET;
61  import javax.ws.rs.POST;
62  import javax.ws.rs.PUT;
63  import javax.ws.rs.Path;
64  import javax.ws.rs.PathParam;
65  import javax.ws.rs.Produces;
66  import javax.ws.rs.core.Context;
67  import javax.ws.rs.core.MediaType;
68  import javax.ws.rs.core.MultivaluedMap;
69  
70  
71  /**
72   *
73   * DirectoryRest
74   *
75   */
76  @Path( RestConstants.BASE_PATH + DirectoryPlugin.PLUGIN_NAME )
77  public class DirectoryRest
78  {
79      private IDirectoryRestService _directoryRestService;
80  
81      // SET
82  
83      /**
84       * Set the directory rest service
85       * @param directoryRestService the directory rest service
86       */
87      public void setDirectoryRestService( IDirectoryRestService directoryRestService )
88      {
89          _directoryRestService = directoryRestService;
90      }
91  
92      /**
93       * Get the wadl.xml content
94       * @param request {@link HttpServletRequest}
95       * @return the content of wadl.xml
96       */
97      @GET
98      @Path( DirectoryRestConstants.PATH_WADL )
99      @Produces( MediaType.APPLICATION_XML )
100     public String getWADL( @Context
101     HttpServletRequest request )
102     {
103         StringBuilder sbBase = new StringBuilder( AppPathService.getBaseUrl( request ) );
104 
105         if ( sbBase.toString(  ).endsWith( DirectoryRestConstants.SLASH ) )
106         {
107             sbBase.deleteCharAt( sbBase.length(  ) - 1 );
108         }
109 
110         sbBase.append( RestConstants.BASE_PATH + DirectoryPlugin.PLUGIN_NAME );
111 
112         Map<String, Object> model = new HashMap<String, Object>(  );
113         model.put( DirectoryRestConstants.MARK_BASE_URL, sbBase.toString(  ) );
114 
115         HtmlTemplate t = AppTemplateService.getTemplate( DirectoryRestConstants.TEMPLATE_WADL, request.getLocale(  ),
116                 model );
117 
118         return t.getHtml(  );
119     }
120 
121     // GET
122 
123     /**
124      * Get the record
125      * @param nIdDirectory the id directory
126      * @param nIdRecord the id record
127      * @param request the HTTP request
128      * @return the record
129      */
130     @GET
131     @Path( DirectoryRestConstants.PATH_ID_DIRECTORY + DirectoryRestConstants.PATH_RECORD +
132     DirectoryRestConstants.PATH_ID_DIRECTORY_RECORD )
133     @Produces( {MediaType.APPLICATION_JSON,
134         MediaType.APPLICATION_XML
135     } )
136     public List<Record> getRecord( @PathParam( DirectoryRestConstants.PARAMETER_ID_DIRECTORY )
137     int nIdDirectory, @PathParam( DirectoryRestConstants.PARAMETER_ID_DIRECTORY_RECORD )
138     int nIdRecord, @Context
139     HttpServletRequest request )
140     {
141         try
142         {
143             Record record = _directoryRestService.getRecord( nIdRecord, request );
144 
145             if ( ( record != null ) && ( record.getDirectory(  ) != null ) &&
146                     ( record.getDirectory(  ).getIdDirectory(  ) == nIdDirectory ) )
147             {
148                 List<Record> listRecord = new ArrayList<Record>(  );
149                 listRecord.add( record );
150 
151                 return listRecord;
152             }
153         }
154         catch ( Exception e )
155         {
156             AppLogService.error( e );
157         }
158 
159         return null;
160     }
161 
162     /**
163      * Get the records list
164      * @param nIdDirectory the id directory
165      * @param request the HTTP request
166      * @return the list of records
167      */
168     @GET
169     @Path( DirectoryRestConstants.PATH_ID_DIRECTORY + DirectoryRestConstants.PATH_RECORDS )
170     @Produces( {MediaType.APPLICATION_JSON,
171         MediaType.APPLICATION_XML
172     } )
173     public List<Record> getRecordsList( @PathParam( DirectoryRestConstants.PARAMETER_ID_DIRECTORY )
174     int nIdDirectory, @Context
175     HttpServletRequest request )
176     {
177         try
178         {
179             return _directoryRestService.getRecordsList( nIdDirectory, request );
180         }
181         catch ( Exception e )
182         {
183             AppLogService.error( e );
184         }
185 
186         return null;
187     }
188 
189     /**
190      * Get the directory
191      * @param nIdDirectory the id directory
192      * @return the directory
193      */
194     @GET
195     @Path( DirectoryRestConstants.PATH_ID_DIRECTORY )
196     @Produces( {MediaType.APPLICATION_JSON,
197         MediaType.APPLICATION_XML
198     } )
199     public List<Directory> getDirectory( @PathParam( DirectoryRestConstants.PARAMETER_ID_DIRECTORY )
200     int nIdDirectory )
201     {
202         List<Directory> listDirectories = new ArrayList<Directory>(  );
203         listDirectories.add( _directoryRestService.getDirectory( nIdDirectory ) );
204 
205         return listDirectories;
206     }
207 
208     /**
209      * Get the directories list
210      * @param nIdDirectory the id directory
211      * @return the list of directories
212      */
213     @GET
214     @Path( StringUtils.EMPTY )
215     @Produces( {MediaType.APPLICATION_JSON,
216         MediaType.APPLICATION_XML
217     } )
218     public List<Directory> getDirectoriesList( 
219         @PathParam( DirectoryRestConstants.PARAMETER_ID_DIRECTORY )
220     int nIdDirectory )
221     {
222         return _directoryRestService.getDirectoriesList(  );
223     }
224 
225     // ACTIONS
226 
227     /**
228      * Insert or complete a record
229      * @param formParams the parameters of the form
230      * @param request the HTTP request
231      * @return the record
232      */
233     @POST
234     @Path( DirectoryRestConstants.PATH_RECORD )
235     @Consumes( MediaType.APPLICATION_FORM_URLENCODED )
236     @Produces( {MediaType.APPLICATION_JSON,
237         MediaType.APPLICATION_XML
238     } )
239     public List<Record> doInsertOrCompleteRecord( MultivaluedMap<String, String> formParams,
240         @Context
241     HttpServletRequest request )
242     {
243         try
244         {
245             HttpServletRequest directoryRestRequest = new DirectoryRestHttpServletRequest( request, formParams );
246             Record record = _directoryRestService.insertOrCompleteRecord( directoryRestRequest );
247 
248             if ( record != null )
249             {
250                 List<Record> listRecords = new ArrayList<Record>(  );
251                 listRecords.add( record );
252 
253                 return listRecords;
254             }
255         }
256         catch ( Exception e )
257         {
258             AppLogService.error( e );
259         }
260 
261         return null;
262     }
263 
264     /**
265      * Insert or complete a record in enctype multipart
266      * @param request the HTTP request
267      * @return the record
268      */
269     @POST
270     @Path( DirectoryRestConstants.PATH_RECORD )
271     @Consumes( MediaType.MULTIPART_FORM_DATA )
272     @Produces( {MediaType.APPLICATION_JSON,
273         MediaType.APPLICATION_XML
274     } )
275     public List<Record> doMultipartInsertOrCompleteRecord( @Context
276     HttpServletRequest request )
277     {
278         try
279         {
280             MultipartHttpServletRequest multipartRequest = _directoryRestService.convertRequest( request );
281             Record record = null;
282 
283             if ( multipartRequest != null )
284             {
285                 record = _directoryRestService.insertOrCompleteRecord( multipartRequest );
286             }
287             else
288             {
289                 record = _directoryRestService.insertOrCompleteRecord( request );
290             }
291 
292             if ( record != null )
293             {
294                 List<Record> listRecords = new ArrayList<Record>(  );
295                 listRecords.add( record );
296 
297                 return listRecords;
298             }
299         }
300         catch ( Exception e )
301         {
302             AppLogService.error( e );
303         }
304 
305         return null;
306     }
307 
308     /**
309      * Update a record
310      * @param formParams the parameter of the form
311      * @param request the HTTP form
312      * @return the record
313      */
314     @PUT
315     @Path( DirectoryRestConstants.PATH_RECORD )
316     @Consumes( MediaType.APPLICATION_FORM_URLENCODED )
317     @Produces( {MediaType.APPLICATION_JSON,
318         MediaType.APPLICATION_XML
319     } )
320     public List<Record> doUpdateRecord( MultivaluedMap<String, String> formParams, @Context
321     HttpServletRequest request )
322     {
323         try
324         {
325             HttpServletRequest directoryRestRequest = new DirectoryRestHttpServletRequest( request, formParams );
326             Record record = _directoryRestService.updateRecord( directoryRestRequest );
327 
328             if ( record != null )
329             {
330                 List<Record> listRecords = new ArrayList<Record>(  );
331                 listRecords.add( record );
332 
333                 return listRecords;
334             }
335         }
336         catch ( Exception e )
337         {
338             AppLogService.error( e );
339         }
340 
341         return null;
342     }
343 
344     /**
345      * Update a record in enctype multipart
346      * @param request the HTTP form
347      * @return the record
348      */
349     @PUT
350     @Path( DirectoryRestConstants.PATH_RECORD )
351     @Consumes( MediaType.MULTIPART_FORM_DATA )
352     @Produces( {MediaType.APPLICATION_JSON,
353         MediaType.APPLICATION_XML
354     } )
355     public List<Record> doMultipartUpdateRecord( @Context
356     HttpServletRequest request )
357     {
358         try
359         {
360             MultipartHttpServletRequest multipartRequest = _directoryRestService.convertRequest( request );
361             Record record = null;
362 
363             if ( multipartRequest != null )
364             {
365                 record = _directoryRestService.updateRecord( multipartRequest );
366             }
367             else
368             {
369                 record = _directoryRestService.updateRecord( request );
370             }
371 
372             if ( record != null )
373             {
374                 List<Record> listRecords = new ArrayList<Record>(  );
375                 listRecords.add( record );
376 
377                 return listRecords;
378             }
379         }
380         catch ( Exception e )
381         {
382             AppLogService.error( e );
383         }
384 
385         return null;
386     }
387 
388     /**
389      * Delete a record
390      * @param nIdRecord the id record
391      * @param request the HTTP request
392      * @return the response
393      */
394     @DELETE
395     @Path( DirectoryRestConstants.PATH_RECORD + DirectoryRestConstants.PATH_ID_DIRECTORY_RECORD )
396     @Consumes( MediaType.APPLICATION_FORM_URLENCODED )
397     @Produces( MediaType.TEXT_HTML )
398     public String doDeleteRecord( @PathParam( DirectoryRestConstants.PARAMETER_ID_DIRECTORY_RECORD )
399     int nIdRecord, @Context
400     HttpServletRequest request )
401     {
402         String strResponse = StringUtils.EMPTY;
403 
404         try
405         {
406             strResponse = _directoryRestService.deleteRecord( nIdRecord, request );
407         }
408         catch ( Exception e )
409         {
410             AppLogService.error( e );
411             strResponse = e.getMessage(  );
412         }
413 
414         return strResponse;
415     }
416 }