Ignore:
Timestamp:
10/24/14 14:01:20 (10 years ago)
Author:
Oliver Schonefeld
Message:
  • more work-in-progress
File:
1 edited

Legend:

Unmodified
Added
Removed
  • SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUSimpleClient.java

    r2997 r5743  
    3232
    3333import org.apache.http.HttpEntity;
    34 import org.apache.http.HttpResponse;
    3534import org.apache.http.HttpStatus;
    3635import org.apache.http.StatusLine;
    3736import org.apache.http.client.ClientProtocolException;
    38 import org.apache.http.client.HttpClient;
     37import org.apache.http.client.config.CookieSpecs;
     38import org.apache.http.client.config.RequestConfig;
     39import org.apache.http.client.methods.CloseableHttpResponse;
    3940import 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;
     41import org.apache.http.config.SocketConfig;
     42import org.apache.http.impl.NoConnectionReuseStrategy;
     43import org.apache.http.impl.client.CloseableHttpClient;
     44import org.apache.http.impl.client.HttpClients;
     45import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
    4446import org.slf4j.Logger;
    4547import org.slf4j.LoggerFactory;
     
    6567 */
    6668public class SRUSimpleClient {
     69    private static final String USER_AGENT = "SRU-Client/1.0.0";
    6770    /** default version the client will use, if not otherwise specified */
    6871    public static final SRUVersion DEFAULT_SRU_VERSION = SRUVersion.VERSION_1_2;
     
    8184    private final SRUVersion defaultVersion;
    8285    private final Map<String, SRURecordDataParser> parsers;
    83     private final HttpClient httpClient;
     86    private final CloseableHttpClient httpClient;
    8487    private final XmlStreamReaderProxy proxy = new XmlStreamReaderProxy();
    8588    private final SRUExplainRecordDataParser explainRecordParser =
     
    138141        this.defaultVersion = defaultVersion;
    139142        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);
    143149    }
    144150
     
    206212        // create URI and perform request
    207213        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;
    216217        try {
     218            final HttpEntity entity = response.getEntity();
     219            if (entity == null) {
     220                throw new SRUClientException("cannot get entity");
     221            }
     222
    217223            stream = entity.getContent();
    218224
     
    257263
    258264            /* make sure to release allocated resources */
    259             HttpClientUtils.closeQuietly(response);
     265            try {
     266                response.close();
     267            } catch (IOException e) {
     268                /* IGNORE */
     269            }
    260270        }
    261271    }
     
    292302        // create URI and perform request
    293303        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;
    302307        try {
     308            final HttpEntity entity = response.getEntity();
     309            if (entity == null) {
     310                throw new SRUClientException("cannot get entity");
     311            }
    303312            stream = entity.getContent();
    304313
     
    343352
    344353            /* make sure to release allocated resources */
    345             HttpClientUtils.closeQuietly(response);
     354            try {
     355                response.close();
     356            } catch (IOException e) {
     357                /* IGNORE */
     358            }
    346359        }
    347360    }
     
    378391        // create URI and perform request
    379392        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;
    388396        try {
     397            final HttpEntity entity = response.getEntity();
     398            if (entity == null) {
     399                throw new SRUClientException("cannot get entity");
     400            }
     401
    389402            stream = entity.getContent();
    390403
     
    429442
    430443            /* 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;
    439457        try {
    440458            logger.debug("submitting HTTP request: {}", uri.toString());
    441459            try {
    442                 request = new HttpGet(uri);
    443                 response = httpClient.execute(request);
     460                HttpGet request = new HttpGet(uri);
     461                response        = httpClient.execute(request);
    444462                StatusLine status = response.getStatusLine();
    445463                if (status.getStatusCode() != HttpStatus.SC_OK) {
     
    451469                    }
    452470                }
     471                forceClose = false;
    453472                return response;
    454473            } catch (ClientProtocolException e) {
     
    471490             * we've used
    472491             */
    473             if (response != null) {
     492            if (forceClose && (response != null)) {
    474493                try {
    475                     EntityUtils.consume(response.getEntity());
     494                    response.close();
    476495                } catch (IOException ex) {
    477496                    /* IGNORE */
    478497                }
    479 
    480                 /* make sure to release allocated resources */
    481                 HttpClientUtils.closeQuietly(response);
    482             }
    483             if (request != null) {
    484                 request.abort();
    485498            }
    486499            throw e;
     
    13571370
    13581371
    1359     private SRUXMLStreamReader createReader(InputStream in, boolean wrap)
     1372    private static SRUXMLStreamReader createReader(InputStream in, boolean wrap)
    13601373            throws XMLStreamException {
    13611374        return new SRUXMLStreamReader(in, wrap);
    13621375    }
    13631376
     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
    13641411} // class SRUSimpleClient
Note: See TracChangeset for help on using the changeset viewer.