Project

General

Profile

1
package eu.dnetlib.openaire.exporter.datasource.clients;
2

    
3
import eu.dnetlib.OpenaireExporterConfig;
4
import eu.dnetlib.enabling.datasources.common.DsmException;
5
import eu.dnetlib.openaire.exporter.vocabularies.Vocabulary;
6
import org.apache.commons.logging.Log;
7
import org.apache.commons.logging.LogFactory;
8
import org.springframework.beans.factory.annotation.Autowired;
9
import org.springframework.cache.annotation.CacheEvict;
10
import org.springframework.cache.annotation.Cacheable;
11
import org.springframework.http.ResponseEntity;
12
import org.springframework.stereotype.Component;
13
import org.springframework.web.client.RestTemplate;
14

    
15
/**
16
 * Created by claudio on 15/09/2017.
17
 */
18
@Component
19
public class VocabularyClient {
20

    
21
	private static final Log log = LogFactory.getLog(VocabularyClient.class);
22

    
23
	@Autowired
24
	private OpenaireExporterConfig config;
25

    
26
	@Cacheable("vocabularies-cache")
27
	public Vocabulary getCountries() throws DsmException {
28
		return _getVocabulary(config.getVocabularies().getCountriesEndpoint(), Vocabulary.class);
29
	}
30

    
31
	@Cacheable("vocabularies-cache")
32
	public Vocabulary getDatasourceTypologies() throws DsmException {
33
		return _getVocabulary(config.getVocabularies().getDatasourceTypologiesEndpoint(), Vocabulary.class);
34
	}
35

    
36
	private <T> T _getVocabulary(final String endpoint, Class<T> clazz) throws DsmException {
37
		final RestTemplate rt = new RestTemplate();
38
		log.info("get vocabulary from " + endpoint);
39
		final ResponseEntity<T> rsp = rt.getForEntity(endpoint, clazz);
40

    
41
		if (!rsp.getStatusCode().is2xxSuccessful()) {
42
			throw new DsmException(rsp.getStatusCodeValue(), "unable to read content from " + endpoint);
43
		}
44

    
45
		return rsp.getBody();
46
	}
47

    
48
	@CacheEvict(cacheNames = "vocabularies-cache", allEntries = true)
49
	public void dropCache() {
50
		log.info("dropped dsManager vocabulary cache");
51
	}
52

    
53
}
(8-8/8)