1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
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
67
68
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
92
93
94 private String getPackageListV3( )
95 {
96 WSBase response = PackageServiceV3.getPackageList( );
97
98 return MapperService.getJson( response );
99 }
100
101
102
103
104
105
106
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
131
132
133
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 }