Search in sources :

Example 1 with EXistResult

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

the class SendRequestFunction method sendRequest.

private Sequence sendRequest(final NodeValue request, final String href, final Sequence bodies) throws XPathException {
    HttpRequest req = null;
    try {
        final org.expath.tools.model.Sequence b = new EXistSequence(bodies, getContext());
        final Element r = new EXistElement(request, getContext());
        final RequestParser parser = new RequestParser(r);
        req = parser.parse(b, href);
        // override anyway it href exists
        if (href != null && !href.isEmpty()) {
            req.setHref(href);
        }
        final URI uri = new URI(req.getHref());
        final EXistResult result = sendOnce(uri, req, parser);
        return result.getResult();
    } catch (final URISyntaxException ex) {
        throw new XPathException(this, "Href is not valid: " + req != null ? req.getHref() : "" + ". " + ex.getMessage(), ex);
    } catch (final HttpClientException hce) {
        throw new XPathException(this, hce.getMessage(), hce);
    }
}
Also used : HttpRequest(org.expath.httpclient.HttpRequest) EXistSequence(org.expath.tools.model.exist.EXistSequence) EXistElement(org.expath.tools.model.exist.EXistElement) XPathException(org.exist.xquery.XPathException) Element(org.expath.tools.model.Element) EXistElement(org.expath.tools.model.exist.EXistElement) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) HttpClientException(org.expath.httpclient.HttpClientException) RequestParser(org.expath.httpclient.impl.RequestParser) EXistResult(org.expath.httpclient.model.exist.EXistResult)

Example 2 with EXistResult

use of org.expath.httpclient.model.exist.EXistResult 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)2 EXistResult (org.expath.httpclient.model.exist.EXistResult)2 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 XPathException (org.exist.xquery.XPathException)1 HttpConnection (org.expath.httpclient.HttpConnection)1 HttpRequest (org.expath.httpclient.HttpRequest)1 HttpResponse (org.expath.httpclient.HttpResponse)1 ApacheHttpConnection (org.expath.httpclient.impl.ApacheHttpConnection)1 RequestParser (org.expath.httpclient.impl.RequestParser)1 Element (org.expath.tools.model.Element)1 EXistElement (org.expath.tools.model.exist.EXistElement)1 EXistSequence (org.expath.tools.model.exist.EXistSequence)1