Project

General

Profile

1
package eu.dnetlib.repo.manager.service.customHystrixCommands;
2

    
3
import com.netflix.hystrix.HystrixCommand;
4
import com.netflix.hystrix.HystrixCommandGroupKey;
5
import org.springframework.http.HttpMethod;
6
import org.springframework.http.ResponseEntity;
7
import org.springframework.web.client.RestTemplate;
8
import org.springframework.web.util.UriComponents;
9
import org.springframework.web.util.UriComponentsBuilder;
10

    
11
import java.util.Map;
12

    
13
public class AggregatorsHystrixCommand extends HystrixCommand<String> {
14

    
15
    RestTemplate restTemplate;
16
    String baseAddress;
17

    
18
    public AggregatorsHystrixCommand(String baseAddress, RestTemplate restTemplate) {
19
        super(HystrixCommandGroupKey.Factory.asKey("StatisticsGroup"));
20
        this.baseAddress = baseAddress;
21
        this.restTemplate = restTemplate;
22
    }
23

    
24
    @Override
25
    protected String run() throws Exception {
26
        String url = baseAddress + "/resources" +
27
                "?query= " +
28
                " oaftype exact datasource and " +
29
                " ( datasourcetypename exact Institutional Repository Aggregator " +
30
                "     or datasourcetypename exact Publication Repository Aggregator )";
31

    
32
        UriComponents uriComponents = UriComponentsBuilder
33
                .fromHttpUrl(url)
34
                .queryParam("page", 0)
35
                .queryParam("size", 0)
36
                .queryParam("format", "json")
37
                .build().encode();
38

    
39
        ResponseEntity rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET, null, Map.class);
40
        Map metadata = (Map) ((Map) rs.getBody()).get("meta");
41
        return String.valueOf(metadata.get("total"));
42
    }
43

    
44
    @Override
45
    protected String getFallback() {
46
        return null;
47
    }
48
}
(1-1/9)