uoa-repository-manager-service/src/main/java/eu/dnetlib/repo/manager/service/customHystrixCommands/DataRepositoriesHystrixComm...

47 lines
1.6 KiB
Java

package eu.dnetlib.repo.manager.service.customHystrixCommands;
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
import java.util.Map;
public class DataRepositoriesHystrixCommand extends HystrixCommand<String> {
RestTemplate restTemplate ;
private String baseAddress;
public DataRepositoriesHystrixCommand(String baseAddress,RestTemplate restTemplate) {
super(HystrixCommandGroupKey.Factory.asKey("StatisticsGroup"));
this.baseAddress = baseAddress;
this.restTemplate = restTemplate;
}
@Override
protected String run() {
String url = baseAddress+"/resources" +
"?query= " +
" oaftype exact datasource and " +
" datasourcetypename exact Data Repository ";
UriComponents uriComponents = UriComponentsBuilder
.fromHttpUrl(url)
.queryParam("page",0)
.queryParam("size",0)
.queryParam("format","json")
.build().encode();
ResponseEntity rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET,null,Map.class);
Map metadata = (Map) ((Map)rs.getBody()).get("meta");
return String.valueOf(metadata.get("total"));
}
@Override
protected String getFallback() {
return null;
}
}