|
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 |
}
|
added sushilite service