diff --git a/src/main/java/eu/dnetlib/repo/manager/service/controllers/SushiliteApi.java b/src/main/java/eu/dnetlib/repo/manager/service/controllers/SushiliteApi.java new file mode 100644 index 0000000..9e0402a --- /dev/null +++ b/src/main/java/eu/dnetlib/repo/manager/service/controllers/SushiliteApi.java @@ -0,0 +1,30 @@ +package eu.dnetlib.repo.manager.service.controllers; + +import eu.dnetlib.usagestats.sushilite.ReportResponseWrapper; +import io.swagger.annotations.Api; +import org.json.JSONException; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping(value = "/sushilite") +@Api(description = "Sushi-Lite API", tags = {"sushilite"}) +public interface SushiliteApi { + + @RequestMapping(value = "/getReportResults", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) + @ResponseBody + ReportResponseWrapper getReportResults(String Report, + String Release, + String RequestorID, + String BeginDate, + String EndDate, + String RepositoryIdentifier, + String ItemIdentifier, + String ItemDataType, + String Granularity, + String Pretty) throws JSONException; + +} diff --git a/src/main/java/eu/dnetlib/repo/manager/service/controllers/SushiliteApiImpl.java b/src/main/java/eu/dnetlib/repo/manager/service/controllers/SushiliteApiImpl.java new file mode 100644 index 0000000..fd65e6a --- /dev/null +++ b/src/main/java/eu/dnetlib/repo/manager/service/controllers/SushiliteApiImpl.java @@ -0,0 +1,75 @@ +package eu.dnetlib.repo.manager.service.controllers; + +import eu.dnetlib.usagestats.sushilite.ReportResponseWrapper; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.ParameterizedTypeReference; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; +import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.client.RestClientException; +import org.springframework.web.client.RestTemplate; +import org.springframework.web.util.UriComponentsBuilder; + +public class SushiliteApiImpl implements SushiliteApi { + + + @Value("${services.repomanager.usagestats.sushiliteEndoint}") + private String usagestatsSushiliteEndpoint; + + @Autowired + private EmailUtils emailUtils; + + private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger + .getLogger(SushiliteApiImpl.class); + + + @Override + @PreAuthorize("hasRole('ROLE_USER')") + public ReportResponseWrapper getReportResults(@RequestParam(value = "Report") String Report, + @RequestParam(value = "Release",defaultValue="4") String Release, + @RequestParam(value = "RequestorID",required=false) String RequestorID, + @RequestParam(value = "BeginDate",required=false) String BeginDate, + @RequestParam(value = "EndDate",required=false) String EndDate, + @RequestParam(value = "RepositoryIdentifier") String RepositoryIdentifier, + @RequestParam(value = "ItemIdentifier",required=false) String ItemIdentifier, + @RequestParam(value = "ItemDataType",required=false) String ItemDataType, + @RequestParam(value = "Granularity") String Granularity, + @RequestParam(value = "Pretty",required=false) String Pretty) { + + //build the uri params + UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(this.usagestatsSushiliteEndpoint + "GetReport/") + .queryParam("Report", Report) + .queryParam("Release", Release) + .queryParam("RequestorID", RequestorID) + .queryParam("BeginDate", BeginDate) + .queryParam("EndDate", EndDate) + .queryParam("RepositoryIdentifier", RepositoryIdentifier) + .queryParam("ItemIdentifier", ItemIdentifier) + .queryParam("ItemDataType", ItemDataType) + .queryParam("Granularity", Granularity) + .queryParam("Pretty", Pretty); + + //create new template engine + RestTemplate template = new RestTemplate(); + template.getMessageConverters().add(new MappingJackson2HttpMessageConverter()); + ResponseEntity resp; + try { + //communicate with endpoint + resp = template.exchange( + builder.build().encode().toUri(), + HttpMethod.GET, + null, + new ParameterizedTypeReference() { + }); + } catch (RestClientException e) { + LOGGER.debug("Exception on getReportResults" , e); + emailUtils.reportException(e); + throw e; + } + + return resp.getBody(); + } +}