Search in sources :

Example 1 with HttpConnection

use of org.expath.httpclient.HttpConnection in project exist by eXist-db.

the class SendRequestFunction method sendOnce.

/**
 * Send one request, not following redirect but handling authentication.
 *
 * Authentication may require to reply to an authentication challenge,
 * by sending again the request, with credentials.
 */
private EXistResult sendOnce(final URI uri, final HttpRequest request, final RequestParser parser) throws HttpClientException {
    EXistResult result = new EXistResult(getContext());
    HttpConnection conn = null;
    try {
        conn = new ApacheHttpConnection(uri);
        final HttpResponse response = request.send(result, conn, parser.getCredentials());
        if (response.getStatus() == HttpStatus.SC_UNAUTHORIZED && parser.getCredentials() != null) {
            // requires authorization, try again with auth
            result = new EXistResult(getContext());
            request.send(result, conn, parser.getCredentials());
        }
    } finally {
        if (conn != null) {
            try {
                conn.disconnect();
            } catch (final HttpClientException hcee) {
                logger.warn(hcee.getMessage(), hcee);
            }
        }
    }
    return result;
}
Also used : HttpClientException(org.expath.httpclient.HttpClientException) EXistResult(org.expath.httpclient.model.exist.EXistResult) ApacheHttpConnection(org.expath.httpclient.impl.ApacheHttpConnection) HttpConnection(org.expath.httpclient.HttpConnection) HttpResponse(org.expath.httpclient.HttpResponse) ApacheHttpConnection(org.expath.httpclient.impl.ApacheHttpConnection)

Aggregations

HttpClientException (org.expath.httpclient.HttpClientException)1 HttpConnection (org.expath.httpclient.HttpConnection)1 HttpResponse (org.expath.httpclient.HttpResponse)1 ApacheHttpConnection (org.expath.httpclient.impl.ApacheHttpConnection)1 EXistResult (org.expath.httpclient.model.exist.EXistResult)1