Project

General

Profile

« Previous | Next » 

Revision 49868

1. Broker api ready.
2. Piwi api ready.
3. Create repomanager.datasource bean.

View differences:

BrokerApiImpl.java
1 1
package eu.dnetlib.repo.manager.service.controllers;
2 2

  
3

  
4 3
import com.fasterxml.jackson.databind.ObjectMapper;
5
import com.google.gson.Gson;
6 4
import eu.dnetlib.domain.data.Repository;
7 5
import eu.dnetlib.repo.manager.shared.BrokerException;
8 6
import eu.dnetlib.repo.manager.shared.Tuple;
9
import eu.dnetlib.repo.manager.shared.broker.AdvQueryObject;
10
import eu.dnetlib.repo.manager.shared.broker.BrowseEntry;
11
import eu.dnetlib.repo.manager.shared.broker.DatasourcesBroker;
12
import eu.dnetlib.repo.manager.shared.broker.EventsPage;
13
import org.apache.xpath.operations.Bool;
7
import eu.dnetlib.repo.manager.shared.broker.*;
14 8
import org.json.JSONException;
15 9
import org.json.JSONObject;
16 10
import org.springframework.beans.factory.annotation.Autowired;
......
28 22
import org.springframework.web.client.RestTemplate;
29 23
import org.springframework.web.util.UriComponentsBuilder;
30 24

  
25
import java.io.IOException;
31 26
import java.util.*;
32 27

  
33 28
@Component
......
38 33
    private RepositoryApiImpl repoAPI;
39 34
    @Value("${services.broker.url}:${services.broker.port}/${services.broker.api}${services.broker.openaire}")
40 35
    private String openairePath;
36
    @Value("${services.broker.url}:${services.broker.port}/${services.broker.api}")
37
    private String apiPath;
41 38

  
39
    private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger
40
            .getLogger(BrokerApiImpl.class);
41

  
42 42
    @Override
43 43
    public DatasourcesBroker getDatasourcesOfUser(String params) throws JSONException {
44 44
        JSONObject json_params = new JSONObject(params);
......
91 91
    }
92 92

  
93 93
    @Override
94
    public EventsPage advancedShowEvents(String params) throws BrokerException, JSONException {
94
    public EventsPage advancedShowEvents(String params) throws BrokerException, JSONException ,IOException {
95 95
        JSONObject json_params = new JSONObject(params);
96 96

  
97 97
        String page = json_params.getString("page");
98 98
        String pagesize = json_params.getString("pagesize");
99
        String json_advQueryObject = json_params.getString("advQueryObject");
99 100

  
100
        JSONObject json_advQueryObject = json_params.getJSONObject("advQueryObject");
101
        ObjectMapper obj = new ObjectMapper();
102
        AdvQueryObject advQueryObject = new Gson().fromJson(json_advQueryObject.toString(),AdvQueryObject.class);
101
        ObjectMapper mapper = new ObjectMapper();
102
        AdvQueryObject advQueryObject = mapper.readValue(json_advQueryObject, AdvQueryObject.class);
103 103

  
104

  
105 104
        final String service = "/events/{page}/{pageSize}";
106 105

  
107 106
        Map<String, Long> uriParams = new HashMap<>();
......
218 217
        return resp.getBody();
219 218
    }
220 219

  
220
    @Override
221
    public Map<String, List<SimpleSubscriptionDesc>> getSubscriptionsOfUser(@PathVariable("userEmail") String userEmail) throws BrokerException {
221 222

  
223
        final String service = "/subscriptions";
224

  
225
        //build the uri params
226
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(openairePath + service)
227
                .queryParam("email", userEmail);
228

  
229
        //create new template engine
230
        RestTemplate template = new RestTemplate();
231
        template.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
232
        ResponseEntity<Map<String, List<SimpleSubscriptionDesc>>> resp;
233
        try {
234
            //communicate with endpoint
235
            resp = template.exchange(
236
                    builder.build().encode().toUri(),
237
                    HttpMethod.GET,
238
                    null,
239
                    new ParameterizedTypeReference<Map<String, List<SimpleSubscriptionDesc>>>() {
240
                    });
241
        } catch (RestClientException e) {
242
            throw new BrokerException(e);
243
        }
244

  
245
        return resp.getBody();
246
    }
247

  
248
    @Override
249
    public Subscription subscribe(OpenaireSubscription obj) throws BrokerException {
250
        final String service = "/subscribe";
251

  
252
        //build the uri params
253
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(openairePath + service);
254

  
255
        //Header info
256
        MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
257
        headers.add("Content-Type", "application/json");
258
        HttpEntity<OpenaireSubscription> entity = new HttpEntity<>(obj, headers);
259

  
260
        //create new template engine
261
        RestTemplate template = new RestTemplate();
262
        template.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
263
        ResponseEntity<Subscription> resp;
264
        try {
265
            //communicate with endpoint
266
            resp = template.exchange(
267
                    builder.build().encode().toUri(),
268
                    HttpMethod.POST,
269
                    entity,
270
                    new ParameterizedTypeReference<Subscription>() {
271
                    });
272
        } catch (RestClientException e) {
273
            throw new BrokerException(e);
274
        }
275

  
276
        return resp.getBody();
277
    }
278

  
279
    @Override
280
    public void unsubscribe(@PathVariable("subscriptionId") String subscriptionId) throws BrokerException {
281
        final String service = "/subscriptions/" + subscriptionId;
282

  
283
        //build the uri params
284
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(apiPath + service);
285

  
286
        //create new template engine
287
        RestTemplate template = new RestTemplate();
288
        template.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
289
        try {
290
            //communicate with endpoint
291
            template.exchange(
292
                    builder.build().encode().toUri(),
293
                    HttpMethod.DELETE,
294
                    null,
295
                    new ParameterizedTypeReference<Void>() {
296
                    });
297
        } catch (RestClientException e) {
298
            throw new BrokerException(e);
299
        }
300
    }
301

  
302
    @Override
303
    public Subscription getSubscription(@PathVariable("subscriptionId") String subscriptionId) throws BrokerException {
304
        final String service = "/subscriptions/" + subscriptionId;
305

  
306
        //build the uri params
307
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(apiPath + service);
308

  
309
        //create new template engine
310
        RestTemplate template = new RestTemplate();
311
        template.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
312
        ResponseEntity<Subscription> resp;
313
        try {
314
            //communicate with endpoint
315
            resp = template.exchange(
316
                    builder.build().encode().toUri(),
317
                    HttpMethod.GET,
318
                    null,
319
                    new ParameterizedTypeReference<Subscription>() {
320
                    });
321
        } catch (RestClientException e) {
322
            throw new BrokerException(e);
323
        }
324
        return resp.getBody();
325
    }
326

  
327

  
328

  
222 329
}

Also available in: Unified diff