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.document.modules.ckan.rs;
35  
36  import com.sun.jersey.api.Responses;
37  
38  import fr.paris.lutece.plugins.document.modules.ckan.business.WSBase;
39  import fr.paris.lutece.plugins.document.modules.ckan.service.MapperService;
40  import fr.paris.lutece.plugins.document.modules.ckan.service.PackageServiceV3;
41  import fr.paris.lutece.plugins.rest.service.RestConstants;
42  
43  import org.apache.log4j.Logger;
44  
45  import org.xml.sax.SAXException;
46  
47  import javax.ws.rs.GET;
48  import javax.ws.rs.Path;
49  import javax.ws.rs.PathParam;
50  import javax.ws.rs.Produces;
51  import javax.ws.rs.QueryParam;
52  import javax.ws.rs.WebApplicationException;
53  import javax.ws.rs.core.MediaType;
54  
55  
56  /**
57   * Ckan Web services API implementation
58   */
59  @Path( RestConstants.BASE_PATH + Constants.PATH_CKAN_API )
60  public class CkanRest
61  {
62      private static final int VERSION_3 = 3;
63      private Logger _logger = Logger.getLogger( RestConstants.REST_LOGGER );
64  
65      /**
66       * Get package list
67       * @param nVersion The API version
68       * @return the list in JSON format
69       */
70      @GET
71      @Path( Constants.PATH_GET_PACKAGE_LIST )
72      @Produces( MediaType.APPLICATION_JSON )
73      public String getPackageList( @PathParam( "version" )
74      int nVersion )
75      {
76          _logger.debug( "getPackageList - api version " + nVersion );
77  
78          switch ( nVersion )
79          {
80              case VERSION_3:
81                  return getPackageListV3(  );
82  
83              default:
84                  break;
85          }
86  
87          throw new WebApplicationException( Responses.NOT_FOUND );
88      }
89  
90      /**
91       * Get package list for the API version 3
92       * @return the list in JSON format
93       */
94      private String getPackageListV3(  )
95      {
96          WSBase response = PackageServiceV3.getPackageList(  );
97  
98          return MapperService.getJson( response );
99      }
100 
101     /**
102      * Get a given package
103      * @param nVersion The API version
104      * @param strIdPackage The package ID
105      * @return The package data in JSON format
106      * @throws SAXException If an error occurs
107      */
108     @GET
109     @Path( Constants.PATH_GET_PACKAGE_SHOW )
110     @Produces( MediaType.APPLICATION_JSON )
111     public String getPackageShow( @PathParam( "version" )
112     int nVersion, @QueryParam( Constants.PARAMETER_ID )
113     String strIdPackage ) throws SAXException
114     {
115         _logger.debug( "getPackageShow - api version " + nVersion );
116 
117         switch ( nVersion )
118         {
119             case VERSION_3:
120                 return getPackageShowV3( strIdPackage );
121 
122             default:
123                 break;
124         }
125 
126         throw new WebApplicationException( Responses.NOT_FOUND );
127     }
128 
129     /**
130      * Get a given package for the API version 3
131      * @param strIdPackage The package ID
132      * @return The package data in JSON format
133      * @throws SAXException If an error occurs
134      */
135     private String getPackageShowV3( String strIdPackage )
136         throws SAXException
137     {
138         _logger.debug( "getPackageShowV3" );
139 
140         String strId = strIdPackage;
141         int nPos = strIdPackage.indexOf( "-" );
142 
143         if ( nPos > 0 )
144         {
145             strId = strIdPackage.substring( 0, nPos );
146         }
147 
148         WSBase response = PackageServiceV3.getPackageShow( strId );
149 
150         return MapperService.getJson( response );
151     }
152 }