Project

General

Profile

1
package eu.dnetlib.enabling.resultset.client;
2

    
3
import java.io.IOException;
4
import java.io.InputStream;
5

    
6
import org.apache.commons.io.IOUtils;
7
import org.apache.commons.logging.Log;
8
import org.apache.commons.logging.LogFactory;
9
import org.apache.http.HttpStatus;
10
import org.apache.http.client.methods.CloseableHttpResponse;
11
import org.apache.http.client.methods.HttpGet;
12
import org.apache.http.impl.client.CloseableHttpClient;
13
import org.apache.http.impl.client.HttpClients;
14
import org.springframework.beans.factory.annotation.Required;
15

    
16
import com.google.gson.Gson;
17

    
18
import eu.dnetlib.enabling.resultset.ResultSetInfo;
19
import eu.dnetlib.enabling.resultset.listener.ResultSetListener;
20
import eu.dnetlib.enabling.resultset.registry.ResultSetRegistry;
21
import eu.dnetlib.rmi.common.ResultSet;
22
import eu.dnetlib.rmi.common.ResultSetException;
23

    
24
public class ResultSetClient {
25

    
26
	private ResultSetRegistry resultSetRegistry;
27

    
28
	private static final Log log = LogFactory.getLog(ResultSetClient.class);
29

    
30
	public <T> Iterable<T> iter(final ResultSet<?> resultSet, final Class<T> clazz) {
31
		try {
32
			if (this.resultSetRegistry.contains(resultSet.getId())) {
33
				final ResultSetListener<?> listener = this.resultSetRegistry.getResultSetById(resultSet.getId());
34
				return () -> new LocalResultSetClientIterator<>(listener, clazz);
35
			} else {
36
				return () -> new HttpResultSetClientIterator<>(resultSet.getId(), resultSet.getBaseUrl(), clazz);
37
			}
38
		} catch (final Throwable e) {
39
			log.error("ITER: Error accessing resultset: " + resultSet.getId());
40
			throw new RuntimeException(e);
41
		}
42
	}
43

    
44
	public ResultSetInfo info(final ResultSet<?> resultSet) {
45
		try {
46
			if (this.resultSetRegistry.contains(resultSet.getId())) {
47
				final ResultSetListener<?> listener = this.resultSetRegistry.getResultSetById(resultSet.getId());
48
				return new ResultSetInfo(resultSet.getId(), listener);
49
			} else {
50
				return invokeRemoteInfo(resultSet);
51
			}
52
		} catch (final Throwable e) {
53
			log.error("INFO: Error accessing resultset: " + resultSet.getId() + " "+e.getMessage());
54
			throw new RuntimeException(e);
55
		}
56
	}
57

    
58
	private ResultSetInfo invokeRemoteInfo(final ResultSet<?> rs) throws ResultSetException {
59
		final HttpGet method = new HttpGet(rs.getBaseUrl() + "/" + rs.getId() + "/info");
60
		try(CloseableHttpClient httpClient = HttpClients.createDefault()) {
61
			try(CloseableHttpResponse response = httpClient.execute(method)) {
62

    
63
				int statusCode = response.getStatusLine().getStatusCode();
64

    
65
				if (HttpStatus.SC_OK != statusCode) {
66
					throw new ResultSetException("Error " + statusCode + " dowloading url: " + rs.getBaseUrl());
67
				}
68
				try(InputStream responseBody = response.getEntity().getContent()) {
69
					String json = IOUtils.toString(responseBody);
70
					return new Gson().fromJson(json, ResultSetInfo.class);
71
				}
72
			} catch (final Throwable e) {
73
				throw new ResultSetException("Error downloading url: " + rs.getBaseUrl(), e);
74
			}
75
		} catch (IOException e) {
76
			log.error("Can't close connections gracefully", e);
77
			throw new ResultSetException("Can't close connections gracefully", e);
78
		}
79
	}
80

    
81
	public ResultSetRegistry getResultSetRegistry() {
82
		return this.resultSetRegistry;
83
	}
84

    
85
	@Required
86
	public void setResultSetRegistry(final ResultSetRegistry resultSetRegistry) {
87
		this.resultSetRegistry = resultSetRegistry;
88
	}
89

    
90
}
(4-4/4)