1
|
package eu.dnetlib.enabling.resultset.client;
|
2
|
|
3
|
import java.util.Map;
|
4
|
import javax.xml.ws.BindingProvider;
|
5
|
import javax.xml.ws.wsaddressing.W3CEndpointReference;
|
6
|
|
7
|
import eu.dnetlib.enabling.resultset.client.utils.EPRUtils;
|
8
|
import eu.dnetlib.enabling.resultset.rmi.ResultSetService;
|
9
|
import eu.dnetlib.enabling.tools.ServiceResolver;
|
10
|
|
11
|
import org.apache.commons.logging.Log;
|
12
|
import org.apache.commons.logging.LogFactory;
|
13
|
|
14
|
import org.apache.cxf.endpoint.Client;
|
15
|
import org.apache.cxf.frontend.ClientProxy;
|
16
|
|
17
|
import org.apache.cxf.transport.http.HTTPConduit;
|
18
|
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
|
19
|
import org.springframework.beans.factory.annotation.Required;
|
20
|
|
21
|
/**
|
22
|
*
|
23
|
* @author claudio
|
24
|
*
|
25
|
*/
|
26
|
public class ResultSetClientFactory implements ResultSetClient {
|
27
|
|
28
|
/**
|
29
|
* logger
|
30
|
*/
|
31
|
private static final Log log = LogFactory.getLog(ResultSetClientFactory.class);
|
32
|
|
33
|
private final static long DEFAULT_CONNECT_TIMEOUT = 10000;
|
34
|
|
35
|
private final static long DEFAULT_REQUEST_TIMEOUT = 60000;
|
36
|
|
37
|
private final static int DEFAULT_PAGE_SIZE = 100;
|
38
|
|
39
|
/**
|
40
|
* used to resolve the epr references to the service endpoint
|
41
|
*/
|
42
|
private ServiceResolver serviceResolver;
|
43
|
|
44
|
/**
|
45
|
* utility object
|
46
|
*/
|
47
|
private EPRUtils eprUtils;
|
48
|
|
49
|
/**
|
50
|
* actual page size
|
51
|
*/
|
52
|
private int pageSize;
|
53
|
|
54
|
/**
|
55
|
* request timeout
|
56
|
*/
|
57
|
private long timeout;
|
58
|
|
59
|
/**
|
60
|
* request timeout
|
61
|
*/
|
62
|
private long connectTimeout;
|
63
|
|
64
|
public ResultSetClientFactory() {
|
65
|
this(DEFAULT_PAGE_SIZE, DEFAULT_REQUEST_TIMEOUT, DEFAULT_CONNECT_TIMEOUT);
|
66
|
}
|
67
|
|
68
|
/**
|
69
|
*
|
70
|
* @param pageSize
|
71
|
* @param timeout
|
72
|
* @throws IllegalArgumentException
|
73
|
*/
|
74
|
public ResultSetClientFactory(int pageSize, long timeout) throws IllegalArgumentException {
|
75
|
this(pageSize, timeout, DEFAULT_CONNECT_TIMEOUT);
|
76
|
}
|
77
|
|
78
|
/**
|
79
|
*
|
80
|
* @param pageSize
|
81
|
* @param timeout
|
82
|
* time to wait for server response before throwing a timeout exception
|
83
|
* @param connectTimeout
|
84
|
* time to wait for server to accept the connection before throwing a connection timeout exception
|
85
|
* @throws IllegalArgumentException
|
86
|
*/
|
87
|
public ResultSetClientFactory(int pageSize, long timeout, long connectTimeout) throws IllegalArgumentException {
|
88
|
if (pageSize <= 0 || timeout <= 0 || connectTimeout <= 0) {
|
89
|
throw new IllegalArgumentException("parameters pageSize, timeout and connectTimeout must be greater than zero");
|
90
|
}
|
91
|
log.info(String.format("creating new ResultSetClientIterableFactory with pageSize (%s), read timeout (%s) and connect timeout (%s)",
|
92
|
pageSize, timeout, connectTimeout));
|
93
|
this.pageSize = pageSize;
|
94
|
this.timeout = timeout;
|
95
|
this.connectTimeout = connectTimeout;
|
96
|
}
|
97
|
|
98
|
/**
|
99
|
*
|
100
|
* @param epr
|
101
|
* @param pageSize
|
102
|
* @return
|
103
|
*/
|
104
|
@Override
|
105
|
public IterableResultSetClient getClient(W3CEndpointReference epr, int pageSize) {
|
106
|
final ResultSetService resultSet = getResultSetService(epr, getConnectTimeout(), getTimeout());
|
107
|
final String rsId = serviceResolver.getResourceIdentifier(epr);
|
108
|
|
109
|
//using given pageSize and default timeout
|
110
|
return new IterableResultSetClient(resultSet, rsId, pageSize, getTimeout());
|
111
|
}
|
112
|
|
113
|
/**
|
114
|
*
|
115
|
* @param epr
|
116
|
* @return
|
117
|
*/
|
118
|
@Override
|
119
|
public IterableResultSetClient getClient(W3CEndpointReference epr) {
|
120
|
final ResultSetService resultSet = getResultSetService(epr, getConnectTimeout(), getTimeout());
|
121
|
final String rsId = serviceResolver.getResourceIdentifier(epr);
|
122
|
|
123
|
//using default pageSize and timeouts
|
124
|
return new IterableResultSetClient(resultSet, rsId, getPageSize(), getTimeout());
|
125
|
}
|
126
|
|
127
|
private ResultSetService getResultSetService(final W3CEndpointReference epr, final long connectTimeout, final long requestTimeout) {
|
128
|
final ResultSetService service = serviceResolver.getService(ResultSetService.class, epr);
|
129
|
|
130
|
log.debug(String.format("creting resultSet service stub (%s) with connectTimeout(%s), requestTimeout(%s)", service.getClass().getName(), connectTimeout, requestTimeout));
|
131
|
|
132
|
if(service instanceof Client) {
|
133
|
log.debug(String.format("setting timeouts for %s", Client.class));
|
134
|
final Client client = ClientProxy.getClient(service);
|
135
|
final HTTPConduit http = (HTTPConduit) client.getConduit();
|
136
|
final HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
|
137
|
|
138
|
httpClientPolicy.setConnectionTimeout(connectTimeout);
|
139
|
httpClientPolicy.setAllowChunking(false);
|
140
|
httpClientPolicy.setReceiveTimeout(requestTimeout);
|
141
|
|
142
|
http.setClient(httpClientPolicy);
|
143
|
} else if (service instanceof BindingProvider) {
|
144
|
log.debug(String.format("setting timeouts for %s", BindingProvider.class));
|
145
|
final Map<String, Object> requestContext = ((BindingProvider) service).getRequestContext();
|
146
|
|
147
|
// can't be sure about which will be used. Set them all.
|
148
|
requestContext.put("com.sun.xml.internal.ws.request.timeout", requestTimeout);
|
149
|
requestContext.put("com.sun.xml.internal.ws.connect.timeout", connectTimeout);
|
150
|
|
151
|
requestContext.put("com.sun.xml.ws.request.timeout", requestTimeout);
|
152
|
requestContext.put("com.sun.xml.ws.connect.timeout", connectTimeout);
|
153
|
|
154
|
requestContext.put("javax.xml.ws.client.receiveTimeout", requestTimeout);
|
155
|
requestContext.put("javax.xml.ws.client.connectionTimeout", connectTimeout);
|
156
|
}
|
157
|
|
158
|
return service;
|
159
|
}
|
160
|
|
161
|
/**
|
162
|
*
|
163
|
* @param stringEpr
|
164
|
* @param pageSize
|
165
|
* @return
|
166
|
*/
|
167
|
@Override
|
168
|
public IterableResultSetClient getClient(String stringEpr, int pageSize) {
|
169
|
return getClient(eprUtils.getEpr(stringEpr), pageSize);
|
170
|
}
|
171
|
|
172
|
/**
|
173
|
*
|
174
|
* @param stringEpr
|
175
|
* @return
|
176
|
*/
|
177
|
@Override
|
178
|
public IterableResultSetClient getClient(String stringEpr) {
|
179
|
return getClient(eprUtils.getEpr(stringEpr));
|
180
|
}
|
181
|
|
182
|
@Required
|
183
|
public void setServiceResolver(ServiceResolver serviceResolver) {
|
184
|
this.serviceResolver = serviceResolver;
|
185
|
}
|
186
|
|
187
|
@Required
|
188
|
public void setEprUtils(EPRUtils eprUtils) {
|
189
|
this.eprUtils = eprUtils;
|
190
|
}
|
191
|
|
192
|
public int getPageSize() {
|
193
|
return pageSize;
|
194
|
}
|
195
|
|
196
|
@Required
|
197
|
public void setPageSize(int pageSize) {
|
198
|
this.pageSize = pageSize;
|
199
|
}
|
200
|
|
201
|
public long getTimeout() {
|
202
|
return timeout;
|
203
|
}
|
204
|
|
205
|
@Required
|
206
|
public void setTimeout(long timeout) {
|
207
|
this.timeout = timeout;
|
208
|
}
|
209
|
|
210
|
public long getConnectTimeout() {
|
211
|
return connectTimeout;
|
212
|
}
|
213
|
|
214
|
public void setConnectTimeout(final long connectTimeout) {
|
215
|
this.connectTimeout = connectTimeout;
|
216
|
}
|
217
|
}
|