- Timestamp:
- 10/24/14 14:01:20 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUSimpleClient.java
r2997 r5743 32 32 33 33 import org.apache.http.HttpEntity; 34 import org.apache.http.HttpResponse;35 34 import org.apache.http.HttpStatus; 36 35 import org.apache.http.StatusLine; 37 36 import org.apache.http.client.ClientProtocolException; 38 import org.apache.http.client.HttpClient; 37 import org.apache.http.client.config.CookieSpecs; 38 import org.apache.http.client.config.RequestConfig; 39 import org.apache.http.client.methods.CloseableHttpResponse; 39 40 import org.apache.http.client.methods.HttpGet; 40 import org.apache.http.client.utils.HttpClientUtils; 41 import org.apache.http.impl.client.DefaultHttpClient; 42 import org.apache.http.params.CoreProtocolPNames; 43 import org.apache.http.util.EntityUtils; 41 import org.apache.http.config.SocketConfig; 42 import org.apache.http.impl.NoConnectionReuseStrategy; 43 import org.apache.http.impl.client.CloseableHttpClient; 44 import org.apache.http.impl.client.HttpClients; 45 import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; 44 46 import org.slf4j.Logger; 45 47 import org.slf4j.LoggerFactory; … … 65 67 */ 66 68 public class SRUSimpleClient { 69 private static final String USER_AGENT = "SRU-Client/1.0.0"; 67 70 /** default version the client will use, if not otherwise specified */ 68 71 public static final SRUVersion DEFAULT_SRU_VERSION = SRUVersion.VERSION_1_2; … … 81 84 private final SRUVersion defaultVersion; 82 85 private final Map<String, SRURecordDataParser> parsers; 83 private final HttpClient httpClient;86 private final CloseableHttpClient httpClient; 84 87 private final XmlStreamReaderProxy proxy = new XmlStreamReaderProxy(); 85 88 private final SRUExplainRecordDataParser explainRecordParser = … … 138 141 this.defaultVersion = defaultVersion; 139 142 this.parsers = parsers; 140 this.httpClient = new DefaultHttpClient(); 141 this.httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, 142 "eu.clarin.sru.client/0.0.1"); 143 144 // create HTTP client 145 // FIXME: get timeout values from somewhere? 146 final int connectTimeout = 30 * 1000; 147 final int socketTimeout = 180 * 1000; 148 httpClient = createHttpClient(connectTimeout, socketTimeout); 143 149 } 144 150 … … 206 212 // create URI and perform request 207 213 final URI uri = request.makeURI(defaultVersion); 208 HttpResponse response = executeRequest(uri); 209 HttpEntity entity = response.getEntity(); 210 if (entity == null) { 211 throw new SRUClientException("cannot get entity"); 212 } 213 214 InputStream stream = null; 215 SRUXMLStreamReader reader = null; 214 CloseableHttpResponse response = executeRequest(uri); 215 InputStream stream = null; 216 SRUXMLStreamReader reader = null; 216 217 try { 218 final HttpEntity entity = response.getEntity(); 219 if (entity == null) { 220 throw new SRUClientException("cannot get entity"); 221 } 222 217 223 stream = entity.getContent(); 218 224 … … 257 263 258 264 /* make sure to release allocated resources */ 259 HttpClientUtils.closeQuietly(response); 265 try { 266 response.close(); 267 } catch (IOException e) { 268 /* IGNORE */ 269 } 260 270 } 261 271 } … … 292 302 // create URI and perform request 293 303 final URI uri = request.makeURI(defaultVersion); 294 HttpResponse response = executeRequest(uri); 295 HttpEntity entity = response.getEntity(); 296 if (entity == null) { 297 throw new SRUClientException("cannot get entity"); 298 } 299 300 InputStream stream = null; 301 SRUXMLStreamReader reader = null; 304 CloseableHttpResponse response = executeRequest(uri); 305 InputStream stream = null; 306 SRUXMLStreamReader reader = null; 302 307 try { 308 final HttpEntity entity = response.getEntity(); 309 if (entity == null) { 310 throw new SRUClientException("cannot get entity"); 311 } 303 312 stream = entity.getContent(); 304 313 … … 343 352 344 353 /* make sure to release allocated resources */ 345 HttpClientUtils.closeQuietly(response); 354 try { 355 response.close(); 356 } catch (IOException e) { 357 /* IGNORE */ 358 } 346 359 } 347 360 } … … 378 391 // create URI and perform request 379 392 final URI uri = request.makeURI(defaultVersion); 380 HttpResponse response = executeRequest(uri); 381 HttpEntity entity = response.getEntity(); 382 if (entity == null) { 383 throw new SRUClientException("cannot get entity"); 384 } 385 386 InputStream stream = null; 387 SRUXMLStreamReader reader = null; 393 CloseableHttpResponse response = executeRequest(uri); 394 InputStream stream = null; 395 SRUXMLStreamReader reader = null; 388 396 try { 397 final HttpEntity entity = response.getEntity(); 398 if (entity == null) { 399 throw new SRUClientException("cannot get entity"); 400 } 401 389 402 stream = entity.getContent(); 390 403 … … 429 442 430 443 /* make sure to release allocated resources */ 431 HttpClientUtils.closeQuietly(response); 432 } 433 } 434 435 436 private HttpResponse executeRequest(URI uri) throws SRUClientException { 437 HttpGet request = null; 438 HttpResponse response = null; 444 try { 445 response.close(); 446 } catch (IOException e) { 447 /* IGNORE */ 448 } 449 } 450 } 451 452 453 private CloseableHttpResponse executeRequest(URI uri) 454 throws SRUClientException { 455 CloseableHttpResponse response = null; 456 boolean forceClose = true; 439 457 try { 440 458 logger.debug("submitting HTTP request: {}", uri.toString()); 441 459 try { 442 request = new HttpGet(uri);443 response = httpClient.execute(request);460 HttpGet request = new HttpGet(uri); 461 response = httpClient.execute(request); 444 462 StatusLine status = response.getStatusLine(); 445 463 if (status.getStatusCode() != HttpStatus.SC_OK) { … … 451 469 } 452 470 } 471 forceClose = false; 453 472 return response; 454 473 } catch (ClientProtocolException e) { … … 471 490 * we've used 472 491 */ 473 if ( response != null) {492 if (forceClose && (response != null)) { 474 493 try { 475 EntityUtils.consume(response.getEntity());494 response.close(); 476 495 } catch (IOException ex) { 477 496 /* IGNORE */ 478 497 } 479 480 /* make sure to release allocated resources */481 HttpClientUtils.closeQuietly(response);482 }483 if (request != null) {484 request.abort();485 498 } 486 499 throw e; … … 1357 1370 1358 1371 1359 private SRUXMLStreamReader createReader(InputStream in, boolean wrap)1372 private static SRUXMLStreamReader createReader(InputStream in, boolean wrap) 1360 1373 throws XMLStreamException { 1361 1374 return new SRUXMLStreamReader(in, wrap); 1362 1375 } 1363 1376 1377 1378 private static CloseableHttpClient createHttpClient(int connectTimeout, 1379 int socketTimeout) { 1380 final PoolingHttpClientConnectionManager manager = 1381 new PoolingHttpClientConnectionManager(); 1382 manager.setDefaultMaxPerRoute(8); 1383 manager.setMaxTotal(128); 1384 1385 final SocketConfig socketConfig = SocketConfig.custom() 1386 .setSoReuseAddress(true) 1387 .setSoLinger(0) 1388 .build(); 1389 1390 final RequestConfig requestConfig = RequestConfig.custom() 1391 .setAuthenticationEnabled(false) 1392 .setRedirectsEnabled(true) 1393 .setMaxRedirects(4) 1394 .setCircularRedirectsAllowed(false) 1395 .setCookieSpec(CookieSpecs.IGNORE_COOKIES) 1396 .setConnectTimeout(connectTimeout) 1397 .setSocketTimeout(socketTimeout) 1398 .setConnectionRequestTimeout(0) /* infinite */ 1399 .setStaleConnectionCheckEnabled(false) 1400 .build(); 1401 1402 return HttpClients.custom() 1403 .setUserAgent(USER_AGENT) 1404 .setConnectionManager(manager) 1405 .setDefaultSocketConfig(socketConfig) 1406 .setDefaultRequestConfig(requestConfig) 1407 .setConnectionReuseStrategy(new NoConnectionReuseStrategy()) 1408 .build(); 1409 } 1410 1364 1411 } // class SRUSimpleClient
Note: See TracChangeset
for help on using the changeset viewer.