Project

General

Profile

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

    
3
import javax.xml.ws.wsaddressing.W3CEndpointReference;
4

    
5
import eu.dnetlib.enabling.resultset.client.utils.EPRUtils;
6
import eu.dnetlib.enabling.resultset.rmi.ResultSetService;
7
import eu.dnetlib.enabling.tools.ServiceResolver;
8
import org.apache.commons.logging.Log;
9
import org.apache.commons.logging.LogFactory;
10
import org.apache.cxf.endpoint.Client;
11
import org.apache.cxf.frontend.ClientProxy;
12
import org.apache.cxf.transport.http.HTTPConduit;
13
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
14
import org.springframework.beans.factory.annotation.Required;
15

    
16
/**
17
 * 
18
 * @author claudio
19
 * 
20
 */
21
public class ResultSetClientFactory implements ResultSetClient {
22

    
23
	/**
24
	 * logger
25
	 */
26
	private static final Log log = LogFactory.getLog(ResultSetClientFactory.class);
27

    
28
	private final static long DEFAULT_CONNECT_TIMEOUT = 10000;
29

    
30
	private final static long DEFAULT_REQUEST_TIMEOUT = 60000;
31

    
32
	private final static int DEFAULT_PAGE_SIZE = 100;
33

    
34
	/**
35
	 * used to resolve the epr references to the service endpoint
36
	 */
37
	private ServiceResolver serviceResolver;
38

    
39
	/**
40
	 * utility object
41
	 */
42
	private EPRUtils eprUtils;
43

    
44
	/**
45
	 * actual page size
46
	 */
47
	private int pageSize;
48

    
49
	/**
50
	 * request timeout
51
	 */
52
	private long timeout;
53

    
54
	/**
55
	 * request timeout
56
	 */
57
	private long connectTimeout;
58

    
59
	public ResultSetClientFactory() {
60
		this(DEFAULT_PAGE_SIZE, DEFAULT_REQUEST_TIMEOUT, DEFAULT_CONNECT_TIMEOUT);
61
	}
62

    
63
	/**
64
	 * 
65
	 * @param pageSize
66
	 * @param timeout
67
	 * @throws IllegalArgumentException
68
	 */
69
	public ResultSetClientFactory(int pageSize, long timeout) throws IllegalArgumentException {
70
		this(pageSize, timeout, DEFAULT_CONNECT_TIMEOUT);
71
	}
72

    
73
	/**
74
	 *
75
	 * @param pageSize
76
	 * @param timeout
77
	 *          time to wait for server response before throwing a timeout exception
78
	 * @param connectTimeout
79
	 *          time to wait for server to accept the connection before throwing a connection timeout exception
80
	 * @throws IllegalArgumentException
81
	 */
82
	public ResultSetClientFactory(int pageSize, long timeout, long connectTimeout) throws IllegalArgumentException {
83
		if (pageSize <= 0 || timeout <= 0 || connectTimeout <= 0) {
84
			throw new IllegalArgumentException("parameters pageSize, timeout and connectTimeout must be greater than zero");
85
		}
86
		log.info(String.format("creating new ResultSetClientIterableFactory with pageSize (%s), read timeout (%s) and connect timeout (%s)",
87
				pageSize, timeout, connectTimeout));
88
		this.pageSize = pageSize;
89
		this.timeout = timeout;
90
		this.connectTimeout = connectTimeout;
91
	}
92

    
93
	/**
94
	 * 
95
	 * @param epr
96
	 * @param pageSize
97
	 * @return
98
	 */
99
	@Override
100
	public IterableResultSetClient getClient(W3CEndpointReference epr, int pageSize) {
101
		final ResultSetService resultSet = getResultSetService(epr, getConnectTimeout(), getTimeout());
102
		final String rsId = serviceResolver.getResourceIdentifier(epr);
103

    
104
		//using given pageSize and default timeout
105
		return new IterableResultSetClient(resultSet, rsId, pageSize, getTimeout());
106
	}
107

    
108
	/**
109
	 * 
110
	 * @param epr
111
	 * @return
112
	 */
113
	@Override
114
	public IterableResultSetClient getClient(W3CEndpointReference epr) {
115
		final ResultSetService resultSet = getResultSetService(epr, getConnectTimeout(), getTimeout());
116
		final String rsId = serviceResolver.getResourceIdentifier(epr);
117

    
118
		//using default pageSize and timeouts
119
		return new IterableResultSetClient(resultSet, rsId, getPageSize(), getTimeout());
120
	}
121

    
122
	private ResultSetService getResultSetService(final W3CEndpointReference epr, final long connectTimeout, final long requestTimeout) {
123
		final ResultSetService service = serviceResolver.getService(ResultSetService.class, epr);
124

    
125
		if (log.isDebugEnabled()) {
126
			log.debug(String.format("creting resultSet service stub with connectTimeout(%s), requestTimeout(%s)", connectTimeout, requestTimeout));
127
		}
128

    
129
		if(service instanceof Client) {
130
			final Client client = ClientProxy.getClient(service);
131
			final HTTPConduit http = (HTTPConduit) client.getConduit();
132
			final HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
133

    
134
			httpClientPolicy.setConnectionTimeout(connectTimeout);
135
			httpClientPolicy.setAllowChunking(false);
136
			httpClientPolicy.setReceiveTimeout(requestTimeout);
137

    
138
			http.setClient(httpClientPolicy);
139
		}
140
		return service;
141
	}
142

    
143
	/**
144
	 * 
145
	 * @param stringEpr
146
	 * @param pageSize
147
	 * @return
148
	 */
149
	@Override
150
	public IterableResultSetClient getClient(String stringEpr, int pageSize) {
151
		return getClient(eprUtils.getEpr(stringEpr), pageSize);
152
	}
153

    
154
	/**
155
	 * 
156
	 * @param stringEpr
157
	 * @return
158
	 */
159
	@Override
160
	public IterableResultSetClient getClient(String stringEpr) {
161
		return getClient(eprUtils.getEpr(stringEpr));
162
	}
163

    
164
	@Required
165
	public void setServiceResolver(ServiceResolver serviceResolver) {
166
		this.serviceResolver = serviceResolver;
167
	}
168

    
169
	@Required
170
	public void setEprUtils(EPRUtils eprUtils) {
171
		this.eprUtils = eprUtils;
172
	}
173

    
174
	public int getPageSize() {
175
		return pageSize;
176
	}
177

    
178
	@Required
179
	public void setPageSize(int pageSize) {
180
		this.pageSize = pageSize;
181
	}
182

    
183
	public long getTimeout() {
184
		return timeout;
185
	}
186

    
187
	@Required
188
	public void setTimeout(long timeout) {
189
		this.timeout = timeout;
190
	}
191

    
192
	public long getConnectTimeout() {
193
		return connectTimeout;
194
	}
195

    
196
	public void setConnectTimeout(final long connectTimeout) {
197
		this.connectTimeout = connectTimeout;
198
	}
199
}
(3-3/5)