Project

General

Profile

« Previous | Next » 

Revision 52829

added sushilite service

View differences:

modules/uoa-repository-manager-service/branches/dev-api/src/main/java/eu/dnetlib/repo/manager/service/controllers/SushiliteApi.java
1
package eu.dnetlib.repo.manager.service.controllers;
2

  
3
import eu.dnetlib.usagestats.sushilite.ReportResponseWrapper;
4
import io.swagger.annotations.Api;
5
import org.json.JSONException;
6
import org.springframework.http.MediaType;
7
import org.springframework.web.bind.annotation.RequestMapping;
8
import org.springframework.web.bind.annotation.RequestMethod;
9
import org.springframework.web.bind.annotation.ResponseBody;
10
import org.springframework.web.bind.annotation.RestController;
11

  
12
@RestController
13
@RequestMapping(value = "/sushilite")
14
@Api(description = "Sushi-Lite API",  tags = {"sushilite"})
15
public interface SushiliteApi {
16

  
17
    @RequestMapping(value = "/getReportResults", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
18
    @ResponseBody
19
    ReportResponseWrapper getReportResults(String Report,
20
                                           String Release,
21
                                           String RequestorID,
22
                                           String BeginDate,
23
                                           String EndDate,
24
                                           String RepositoryIdentifier,
25
                                           String ItemIdentifier,
26
                                           String ItemDataType,
27
                                           String Granularity,
28
                                           String Pretty) throws JSONException;
29

  
30
}
modules/uoa-repository-manager-service/branches/dev-api/src/main/java/eu/dnetlib/repo/manager/service/controllers/SushiliteApiImpl.java
1
package eu.dnetlib.repo.manager.service.controllers;
2

  
3
import eu.dnetlib.usagestats.sushilite.ReportResponseWrapper;
4
import org.springframework.beans.factory.annotation.Autowired;
5
import org.springframework.beans.factory.annotation.Value;
6
import org.springframework.core.ParameterizedTypeReference;
7
import org.springframework.http.HttpMethod;
8
import org.springframework.http.ResponseEntity;
9
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
10
import org.springframework.security.access.prepost.PreAuthorize;
11
import org.springframework.web.bind.annotation.RequestParam;
12
import org.springframework.web.client.RestClientException;
13
import org.springframework.web.client.RestTemplate;
14
import org.springframework.web.util.UriComponentsBuilder;
15

  
16
public class SushiliteApiImpl implements SushiliteApi {
17

  
18

  
19
    @Value("${services.repomanager.usagestats.sushiliteEndoint}")
20
    private String usagestatsSushiliteEndpoint;
21

  
22
    @Autowired
23
    private EmailUtils emailUtils;
24

  
25
    private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger
26
            .getLogger(SushiliteApiImpl.class);
27

  
28

  
29
    @Override
30
    @PreAuthorize("hasRole('ROLE_USER')")
31
    public ReportResponseWrapper getReportResults(@RequestParam(value = "Report") String Report,
32
                                                  @RequestParam(value = "Release",defaultValue="4") String Release,
33
                                                  @RequestParam(value = "RequestorID",required=false) String RequestorID,
34
                                                  @RequestParam(value = "BeginDate",required=false) String BeginDate,
35
                                                  @RequestParam(value = "EndDate",required=false) String EndDate,
36
                                                  @RequestParam(value = "RepositoryIdentifier") String RepositoryIdentifier,
37
                                                  @RequestParam(value = "ItemIdentifier",required=false) String ItemIdentifier,
38
                                                  @RequestParam(value = "ItemDataType",required=false) String ItemDataType,
39
                                                  @RequestParam(value = "Granularity") String Granularity,
40
                                                  @RequestParam(value = "Pretty",required=false) String Pretty) {
41

  
42
            //build the uri params
43
            UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(this.usagestatsSushiliteEndpoint + "GetReport/")
44
                                                                .queryParam("Report", Report)
45
                                                                .queryParam("Release", Release)
46
                                                                .queryParam("RequestorID", RequestorID)
47
                                                                .queryParam("BeginDate", BeginDate)
48
                                                                .queryParam("EndDate", EndDate)
49
                                                                .queryParam("RepositoryIdentifier", RepositoryIdentifier)
50
                                                                .queryParam("ItemIdentifier", ItemIdentifier)
51
                                                                .queryParam("ItemDataType", ItemDataType)
52
                                                                .queryParam("Granularity", Granularity)
53
                                                                .queryParam("Pretty", Pretty);
54

  
55
            //create new template engine
56
            RestTemplate template = new RestTemplate();
57
            template.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
58
            ResponseEntity<ReportResponseWrapper> resp;
59
            try {
60
                //communicate with endpoint
61
                resp = template.exchange(
62
                        builder.build().encode().toUri(),
63
                        HttpMethod.GET,
64
                        null,
65
                        new ParameterizedTypeReference<ReportResponseWrapper>() {
66
                        });
67
            } catch (RestClientException e) {
68
                LOGGER.debug("Exception on getReportResults" , e);
69
                emailUtils.reportException(e);
70
                throw e;
71
            }
72

  
73
            return resp.getBody();
74
    }
75
}

Also available in: Unified diff