Example usage for org.apache.http.conn ConnectionPoolTimeoutException getClass

List of usage examples for org.apache.http.conn ConnectionPoolTimeoutException getClass

Introduction

In this page you can find the example usage for org.apache.http.conn ConnectionPoolTimeoutException getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:com.morphoss.acal.service.connector.AcalRequestor.java

/**
 * Marshall and send the request.// w  w  w  . j  av a  2s.c om
 * @param headers
 * @param entityString
 * @return
 * @throws SendRequestFailedException
 * @throws SSLException
 * @throws AuthenticationFailure
 * @throws ConnectionFailedException
 * @throws ConnectionPoolTimeoutException
 */
private synchronized InputStream sendRequest(Header[] headers, String entityString)
        throws SendRequestFailedException, SSLException, AuthenticationFailure, ConnectionFailedException,
        ConnectionPoolTimeoutException {
    long down = 0;
    long up = 0;
    long start = System.currentTimeMillis();

    if (!initialised)
        throw new IllegalStateException("AcalRequestor has not been initialised!");
    statusCode = -1;
    try {
        // Create request and add headers and entity
        request = new DavRequest(method, this.fullUrl());
        //         request.addHeader(new BasicHeader("User-Agent", AcalConnectionPool.getUserAgent()));
        if (headers != null)
            for (Header h : headers)
                request.addHeader(h);

        if (authRequired && authType != Servers.AUTH_NONE)
            request.addHeader(buildAuthHeader());
        else if (authRequired) {
            // Assume basicAuth
            request.addHeader(basicAuthHeader());
        }

        if (entityString != null) {
            request.setEntity(new StringEntity(entityString.toString(), "UTF-8"));
            up = request.getEntity().getContentLength();
        }

        // This trick greatly reduces the occurrence of host not found errors.
        try {
            InetAddress.getByName(this.hostName);
        } catch (UnknownHostException e1) {
            Thread.sleep(100);
            try {
                InetAddress.getByName(this.hostName);
            } catch (UnknownHostException e2) {
                Thread.sleep(100);
            }
        }

        int requestPort = -1;
        if (this.protocol == null)
            this.protocol = PROTOCOL_HTTP;
        String requestProtocol = this.protocol;
        if ((this.protocol.equals(PROTOCOL_HTTP) && this.port != 80)
                || (this.protocol.equals(PROTOCOL_HTTPS) && this.port != 443)) {
            requestPort = this.port;
        }

        if (Constants.LOG_DEBUG || debugThisRequest) {
            Log.println(Constants.LOGD, TAG,
                    String.format("Method: %s, Protocol: %s, Hostname: %s, Port: %d, Path: %s", method,
                            requestProtocol, hostName, requestPort, path));
        }
        HttpHost host = new HttpHost(this.hostName, requestPort, requestProtocol);

        if (debugThisRequest)
            logRequest(Constants.LOGV);

        // Send request and get response
        response = null;

        if (Constants.debugHeap)
            AcalDebug.heapDebug(TAG, "Making HTTP request");
        try {
            response = httpClient.execute(host, request);
        } catch (ConnectionPoolTimeoutException e) {
            Log.println(Constants.LOGI, TAG,
                    e.getClass().getSimpleName() + ": " + e.getMessage() + " to " + fullUrl());
            Log.println(Constants.LOGI, TAG, "Retrying...");
            response = httpClient.execute(host, request);
        }
        if (Constants.debugHeap)
            AcalDebug.heapDebug(TAG, "Finished HTTP request");

        this.responseHeaders = response.getAllHeaders();
        this.statusCode = response.getStatusLine().getStatusCode();

        HttpEntity entity = response.getEntity();
        down = (entity == null ? 0 : entity.getContentLength());

        long finish = System.currentTimeMillis();
        double timeTaken = (finish - start) / 1000.0;

        if (Constants.LOG_DEBUG || debugThisRequest)
            Log.println(Constants.LOGD, TAG, "Response: " + statusCode + ", Sent: " + up + ", Received: " + down
                    + ", Took: " + timeTaken + " seconds");

        if (debugThisRequest) {
            return logResponse(Constants.LOGV);
        } else if (entity != null) {
            if (entity.getContentLength() > 0)
                return entity.getContent();

            // Kind of admitting defeat here, but I can't track down why we seem
            // to end up in never-never land if we just return entity.getContent()
            // directly when entity.getContentLength() is -1 ('unknown', apparently).
            // Horribly inefficient too.
            //
            // @todo: Check whether this problem was caused by failing to close the InputStream
            // and this hack can be removed...  Need to find a server which does not send Content-Length headers.
            //
            String tmpEntity = entityToString(entity);
            return new ByteArrayInputStream(tmpEntity.getBytes());
        }

    } catch (SSLProtocolException e) {
        Log.i(TAG, e.getClass().getSimpleName() + ": " + e.getMessage() + " to " + fullUrl());
        return null;
    } catch (SSLHandshakeException e) {
        Log.i(TAG, e.getClass().getSimpleName() + ": " + e.getMessage() + " to " + fullUrl());
        throw e;
    } catch (SSLException e) {
        if (debugThisRequest)
            Log.println(Constants.LOGD, TAG, Log.getStackTraceString(e));
        throw e;
    } catch (AuthenticationFailure e) {
        if (debugThisRequest)
            Log.println(Constants.LOGD, TAG, Log.getStackTraceString(e));
        throw e;
    } catch (ConnectionPoolTimeoutException e) {
        Log.i(TAG, e.getClass().getSimpleName() + ": " + e.getMessage() + " to " + fullUrl());
        throw e;
    } catch (SocketTimeoutException e) {
        Log.i(TAG, e.getClass().getSimpleName() + ": " + e.getMessage() + " to " + fullUrl());
        return null;
    } catch (ConnectTimeoutException e) {
        Log.i(TAG, e.getClass().getSimpleName() + ": " + e.getMessage() + " to " + fullUrl());
        return null;
    } catch (UnknownHostException e) {
        Log.i(TAG, e.getClass().getSimpleName() + ": " + e.getMessage() + " to " + fullUrl());
        return null;
    } catch (IOException e) {
        Log.i(TAG, e.getClass().getSimpleName() + ": " + e.getMessage() + " to " + fullUrl());
        return null;
    } catch (Exception e) {
        Log.println(Constants.LOGD, TAG, Log.getStackTraceString(e));
        if (statusCode < 300 || statusCode > 499)
            throw new SendRequestFailedException(e.getMessage());
    }
    return null;
}