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 DataRepositoriesHystrixCommand extends HystrixCommand<String> {
14

    
15
    RestTemplate restTemplate ;
16
    private String baseAddress;
17

    
18
    public DataRepositoriesHystrixCommand(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() {
26
        String url = baseAddress+"/resources" +
27
                "?query= " +
28
                " oaftype exact datasource and " +
29
                " datasourcetypename exact Data Repository ";
30

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

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

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