Search in sources :

Example 1 with HttpClientException

use of org.expath.httpclient.HttpClientException 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 HttpClientException

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

the class EXistResult method add.

@Override
public void add(final InputStream is) throws HttpClientException {
    try {
        // we have to make a temporary copy of the data stream, as the socket will be closed shortly
        final TemporaryFileManager temporaryFileManager = TemporaryFileManager.getInstance();
        final Path tempFile = temporaryFileManager.getTemporaryFile();
        Files.copy(is, tempFile, StandardCopyOption.REPLACE_EXISTING);
        result.add(BinaryValueFromFile.getInstance(context, new Base64BinaryValueType(), tempFile, (isClosed, file) -> temporaryFileManager.returnTemporaryFile(file)));
    } catch (final XPathException | IOException xpe) {
        throw new HttpClientException("Unable to add binary value to result:" + xpe.getMessage(), xpe);
    } finally {
        try {
            is.close();
        } catch (final IOException ioe) {
            logger.warn(ioe.getMessage(), ioe);
        }
    }
}
Also used : Path(java.nio.file.Path) TypeTest(org.exist.xquery.TypeTest) Files(java.nio.file.Files) TemporaryFileManager(org.exist.util.io.TemporaryFileManager) Result(org.expath.httpclient.model.Result) org.exist.xquery.value(org.exist.xquery.value) IOException(java.io.IOException) Reader(java.io.Reader) Source(javax.xml.transform.Source) StandardCopyOption(java.nio.file.StandardCopyOption) HttpResponse(org.expath.httpclient.HttpResponse) Logger(org.apache.logging.log4j.Logger) Charset(java.nio.charset.Charset) ModuleUtils(org.exist.xquery.modules.ModuleUtils) HttpClientException(org.expath.httpclient.HttpClientException) SAXException(org.xml.sax.SAXException) NodeTest(org.exist.xquery.NodeTest) DocumentImpl(org.exist.dom.memtree.DocumentImpl) Path(java.nio.file.Path) LogManager(org.apache.logging.log4j.LogManager) XPathException(org.exist.xquery.XPathException) XQueryContext(org.exist.xquery.XQueryContext) InputStream(java.io.InputStream) HttpClientException(org.expath.httpclient.HttpClientException) XPathException(org.exist.xquery.XPathException) IOException(java.io.IOException) TemporaryFileManager(org.exist.util.io.TemporaryFileManager)

Example 3 with HttpClientException

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

the class EXistResult method add.

@Override
public void add(final HttpResponse response) throws HttpClientException {
    final EXistTreeBuilder builder = new EXistTreeBuilder(context);
    response.outputResponseElement(builder);
    final DocumentImpl doc = builder.close();
    try {
        // we add the root *element* to the result sequence
        final NodeTest kind = new TypeTest(Type.ELEMENT);
        // add the original items after
        if (result.isEmpty()) {
            doc.selectChildren(kind, result);
        } else {
            final ValueSequence newResult = new ValueSequence();
            doc.selectChildren(kind, newResult);
            newResult.addAll(result);
            result = newResult;
        }
    } catch (final XPathException xpe) {
        throw new HttpClientException("Unable to add HttpResponse to result:" + xpe.getMessage(), xpe);
    }
}
Also used : HttpClientException(org.expath.httpclient.HttpClientException) XPathException(org.exist.xquery.XPathException) TypeTest(org.exist.xquery.TypeTest) DocumentImpl(org.exist.dom.memtree.DocumentImpl) NodeTest(org.exist.xquery.NodeTest)

Example 4 with HttpClientException

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

the class EXistTreeBuilder method outputHeaders.

@Override
public void outputHeaders(HeaderSet headers) throws HttpClientException {
    for (Header h : headers) {
        assert h.getName() != null : "Header name cannot be null";
        String name = h.getName().toLowerCase();
        try {
            startElem("header");
            attribute("name", name);
            attribute("value", h.getValue());
            // startContent();
            endElem();
        } catch (ToolsException ex) {
            throw new HttpClientException("Error building the header " + name, ex);
        }
    }
}
Also used : ToolsException(org.expath.tools.ToolsException) HttpClientException(org.expath.httpclient.HttpClientException) Header(org.apache.http.Header)

Example 5 with HttpClientException

use of org.expath.httpclient.HttpClientException 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)7 IOException (java.io.IOException)3 XPathException (org.exist.xquery.XPathException)3 DocumentImpl (org.exist.dom.memtree.DocumentImpl)2 NodeTest (org.exist.xquery.NodeTest)2 TypeTest (org.exist.xquery.TypeTest)2 HttpResponse (org.expath.httpclient.HttpResponse)2 EXistResult (org.expath.httpclient.model.exist.EXistResult)2 SAXException (org.xml.sax.SAXException)2 InputStream (java.io.InputStream)1 Reader (java.io.Reader)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Charset (java.nio.charset.Charset)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 StandardCopyOption (java.nio.file.StandardCopyOption)1 Source (javax.xml.transform.Source)1 Header (org.apache.http.Header)1 LogManager (org.apache.logging.log4j.LogManager)1