Project

General

Profile

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

    
3
import java.nio.charset.Charset;
4
import java.util.Arrays;
5

    
6
import eu.dnetlib.OpenaireExporterConfig;
7
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
8
import eu.dnetlib.openaire.exporter.datasource.ApiException;
9
import eu.dnetlib.openaire.exporter.datasource.clients.utils.IndexDsInfo;
10
import org.apache.commons.io.IOUtils;
11
import org.apache.commons.logging.Log;
12
import org.apache.commons.logging.LogFactory;
13
import org.springframework.beans.factory.annotation.Autowired;
14
import org.springframework.cache.annotation.Cacheable;
15
import org.springframework.http.HttpStatus;
16
import org.springframework.stereotype.Component;
17

    
18
/**
19
 * Created by claudio on 20/10/2016.
20
 */
21
@Component
22
public class ISLookupClient {
23

    
24
	private static final Log log = LogFactory.getLog(ISLookupClient.class);
25

    
26
	@Autowired
27
	private OpenaireExporterConfig config;
28

    
29
	@Autowired
30
	private ISLookUpService isLookUpService;
31

    
32
	@Cacheable("datasources-is-cache")
33
	public IndexDsInfo calculateCurrentIndexDsInfo() throws ApiException {
34
		log.warn("calculateCurrentIndexDsInfo(): not using cache");
35
		try {
36
			final String queryUrl = IOUtils.toString(config.getFindSolrIndexUrl().getInputStream(), Charset.defaultCharset());
37
			final String queryDs = IOUtils.toString(config.getFindIndexDsInfo().getInputStream(), Charset.defaultCharset());
38

    
39
			final String indexBaseUrl = isLookUpService.getResourceProfileByQuery(queryUrl);
40
			if (log.isDebugEnabled()) {
41
				log.debug(String.format("got solr url: '%s'", indexBaseUrl));
42
			}
43
			final String[] arr = isLookUpService.getResourceProfileByQuery(queryDs).split("@@@");
44
			if (log.isDebugEnabled()) {
45
				log.debug(String.format("got info: '%s'", Arrays.asList(arr)));
46
			}
47

    
48
			return new IndexDsInfo(indexBaseUrl, arr[0].trim(), arr[1].trim(), arr[2].trim());
49
		} catch (Throwable e) {
50
			throw new ApiException(HttpStatus.INTERNAL_SERVER_ERROR.value(), "Error querying information system", e);
51
		}
52
	}
53

    
54
}
(4-4/6)