View Javadoc
1   package fr.paris.lutece.plugins.workflow.modules.eudonetrestdirectory.service;
2   
3   import javax.ws.rs.core.MediaType;
4   
5   import com.sun.jersey.api.client.Client;
6   import com.sun.jersey.api.client.ClientResponse;
7   import com.sun.jersey.api.client.WebResource;
8   import com.sun.jersey.api.client.config.ClientConfig;
9   import com.sun.jersey.api.client.config.DefaultClientConfig;
10  import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
11  import com.sun.jersey.api.json.JSONConfiguration;
12  
13  public class EudonetClient
14  {
15      private static Client _client;
16      private static WebResource _webResource;
17      private static EudonetClient _singleton;
18  
19      public EudonetClient( String baseUrl )
20      {
21          ClientConfig clientConfig = new DefaultClientConfig( );
22          clientConfig.getFeatures( ).put( JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE );
23          _client = Client.create( clientConfig );
24          // _client.addFilter( new HTTPBasicAuthFilter( "", "" ) );
25          _webResource = _client.resource( baseUrl );
26      }
27  
28      public static EudonetClient getService( String baseUrl )
29      {
30          if ( _singleton == null )
31          {
32              _singleton = new EudonetClient( baseUrl );
33  
34              return _singleton;
35          }
36  
37          return _singleton;
38      }
39  
40      public EudonetClient( String baseUrl, String userName, String password )
41      {
42          ClientConfig clientConfig = new DefaultClientConfig( );
43          clientConfig.getFeatures( ).put( JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE );
44          _client = Client.create( clientConfig );
45          _client.addFilter( new HTTPBasicAuthFilter( userName, password ) );
46          _webResource = _client.resource( baseUrl );
47      }
48  
49      /**
50       * return the token
51       * 
52       * @param strUrlApi
53       * @param strAuthenticateJson
54       * @return ClientResponse
55       */
56      public ClientResponse getTokenAuthenticate( String strAuthenticateJson )
57      {
58          return _webResource.path( "Authenticate" ).path( "Token" ).type( MediaType.APPLICATION_JSON ).accept( MediaType.APPLICATION_JSON )
59                  .post( ClientResponse.class, strAuthenticateJson );
60  
61      }
62  
63      /**
64       * create a new record in the table strIdTable
65       * 
66       * @param strUrlApi
67       * @param strAuthenticate
68       * @param strIdTable
69       * @param strBodyJson
70       * @return ClientResponse
71       */
72      public ClientResponse createRecord( String strAuthenticate, String strIdTable, String strBodyJson )
73      {
74          return _webResource.path( "CUD" ).path( strIdTable ).header( "x-auth", strAuthenticate ).type( MediaType.APPLICATION_JSON )
75                  .accept( MediaType.APPLICATION_JSON ).post( ClientResponse.class, strBodyJson );
76      }
77  
78      /**
79       * update the record
80       * 
81       * @param strUrlApi
82       * @param strAuthenticate
83       * @param strIdTable
84       * @param strBodyJson
85       * @param strIdRecord
86       * @return ClientResponse
87       */
88      public ClientResponse updateRecord( String strAuthenticate, String strIdTable, String strBodyJson, String strIdRecord )
89      {
90          return _webResource.path( "CUD" ).path( strIdTable ).path( strIdRecord ).header( "x-auth", strAuthenticate ).type( MediaType.APPLICATION_JSON )
91                  .accept( MediaType.APPLICATION_JSON ).post( ClientResponse.class, strBodyJson );
92      }
93  
94      /**
95       * delete the record
96       * 
97       * @param strUrlApi
98       * @param strAuthenticate
99       * @param strIdTable
100      * @param strIdRecord
101      * @return ClientResponse
102      */
103     public ClientResponse deleteRecord( String strAuthenticate, String strIdTable, String strIdRecord )
104     {
105         return _webResource.path( "CUD" ).path( "Delete" ).path( strIdTable ).path( strIdRecord ).header( "x-auth", strAuthenticate )
106                 .type( MediaType.APPLICATION_JSON ).accept( MediaType.APPLICATION_JSON ).delete( ClientResponse.class );
107     }
108 
109     /**
110      * search a record by id
111      * 
112      * @param strUrlApi
113      * @param strAuthenticate
114      * @param strIdTable
115      * @param strIdRecord
116      * @return ClientResponse
117      */
118     public ClientResponse searchRecordById( String strAuthenticate, String strIdTable, String strIdRecord )
119     {
120         return _webResource.path( "Search" ).path( strIdTable ).path( strIdRecord ).header( "x-auth", strAuthenticate ).type( MediaType.APPLICATION_JSON )
121                 .accept( MediaType.APPLICATION_JSON ).get( ClientResponse.class );
122     }
123 
124     /**
125      * search records by Crieria
126      * 
127      * @param strUrlApi
128      * @param strAuthenticate
129      * @param strIdTable
130      * @param strBodyJson
131      * @return ClientResponse
132      */
133     public ClientResponse searchRecordByCrieria( String strAuthenticate, String strIdTable, String strBodyJson )
134     {
135         return _webResource.path( "Search" ).path( strIdTable ).header( "x-auth", strAuthenticate ).type( MediaType.APPLICATION_JSON )
136                 .accept( MediaType.APPLICATION_JSON ).post( ClientResponse.class, strBodyJson );
137     }
138 
139     /**
140      * search records
141      * 
142      * @param strUrlApi
143      * @param strAuthenticate
144      * @param strIdTable
145      * @param strBodyJson
146      * @return ClientResponse
147      */
148     public ClientResponse searchFastRecord( String strAuthenticate, String strIdTable, String strBodyJson )
149     {
150         return _webResource.path( "Search/Fast" ).path( strIdTable ).header( "x-auth", strAuthenticate ).type( MediaType.APPLICATION_JSON )
151                 .accept( MediaType.APPLICATION_JSON ).post( ClientResponse.class, strBodyJson );
152     }
153 
154     /**
155      * add a Annexes
156      * 
157      * @param strUrlApi
158      * @param strAuthenticate
159      * @param strBodyJson
160      * @return ClientResponse
161      */
162     public ClientResponse addAnnexes( String strAuthenticate, String strBodyJson )
163     {
164         return _webResource.path( "Annexes/Add" ).header( "x-auth", strAuthenticate ).type( MediaType.APPLICATION_JSON ).accept( MediaType.APPLICATION_JSON )
165                 .post( ClientResponse.class, strBodyJson );
166     }
167 
168     /**
169      * get a MetaInfos
170      * 
171      * @param strAuthenticate
172      * @param strBodyJson
173      * @return ClientResponse
174      */
175     public ClientResponse getAttributListMetaInfos( String strAuthenticate, String strBodyJson )
176     {
177         return _webResource.path( "MetaInfos" ).header( "x-auth", strAuthenticate ).type( MediaType.APPLICATION_JSON ).accept( MediaType.APPLICATION_JSON )
178                 .post( ClientResponse.class, strBodyJson );
179     }
180 
181     /**
182      * get a MetaInfos
183      * 
184      * @param strAuthenticate
185      * @return ClientResponse
186      */
187     public ClientResponse getTableListMetaInfos( String strAuthenticate )
188     {
189         return _webResource.path( "MetaInfos" ).path( "ListTabs/" ).header( "x-auth", strAuthenticate ).type( MediaType.APPLICATION_JSON )
190                 .accept( MediaType.APPLICATION_JSON ).get( ClientResponse.class );
191     }
192 
193     /**
194      * @return the _client
195      */
196     public static Client getClient( )
197     {
198         return _client;
199     }
200 
201     /**
202      * @return the _webResource
203      */
204     public static WebResource getWebResource( )
205     {
206         return _webResource;
207     }
208 
209 }