Search in sources :

Example 1 with XmldbURL

use of org.exist.protocolhandler.xmldb.XmldbURL in project exist by eXist-db.

the class AnyUriResolver method resolveEntity.

@Override
public XMLInputSource resolveEntity(XMLResourceIdentifier xri) throws XNIException, IOException {
    if (xri.getExpandedSystemId() == null && xri.getLiteralSystemId() == null && xri.getNamespace() == null && xri.getPublicId() == null) {
        // quick fail
        return null;
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Resolving XMLResourceIdentifier: {}", getXriDetails(xri));
    }
    String resourcePath = null;
    String baseSystemId = null;
    if (firstTime) {
        // First time use constructor supplied path
        resourcePath = docPath;
        baseSystemId = parentURI;
        xri.setExpandedSystemId(docPath);
        firstTime = false;
    } else {
        resourcePath = xri.getExpandedSystemId();
    }
    xri.setBaseSystemId(docPath);
    LOG.debug("resourcePath='{}'", resourcePath);
    // prevent NPE
    if (resourcePath == null) {
        return null;
    }
    InputStream is = null;
    if (resourcePath.startsWith("xmldb:")) {
        final XmldbURL xmldbURL = new XmldbURL(resourcePath);
        if (xmldbURL.isEmbedded()) {
            is = new EmbeddedInputStream(xmldbURL);
        } else {
            is = new XmlrpcInputStream(threadGroup, xmldbURL);
        }
    } else {
        is = new URL(resourcePath).openStream();
    }
    final XMLInputSource xis = new XMLInputSource(xri.getPublicId(), resourcePath, baseSystemId, is, "UTF-8");
    if (LOG.isDebugEnabled()) {
        LOG.debug("XMLInputSource: {}", getXisDetails(xis));
    }
    return xis;
}
Also used : XmldbURL(org.exist.protocolhandler.xmldb.XmldbURL) XMLInputSource(org.apache.xerces.xni.parser.XMLInputSource) EmbeddedInputStream(org.exist.protocolhandler.embedded.EmbeddedInputStream) XmlrpcInputStream(org.exist.protocolhandler.xmlrpc.XmlrpcInputStream) InputStream(java.io.InputStream) EmbeddedInputStream(org.exist.protocolhandler.embedded.EmbeddedInputStream) XmlrpcInputStream(org.exist.protocolhandler.xmlrpc.XmlrpcInputStream) URL(java.net.URL) XmldbURL(org.exist.protocolhandler.xmldb.XmldbURL)

Example 2 with XmldbURL

use of org.exist.protocolhandler.xmldb.XmldbURL in project exist by eXist-db.

the class ExistResolver method resolveInputSource.

/* ============== */
/* Helper methods */
/* ============== */
private InputSource resolveInputSource(BrokerPool bPool, String path) throws IOException {
    LOG.debug("Resolving {}", path);
    final InputSource inputsource = new InputSource();
    if (path != null) {
        if (path.startsWith(LOCALURI) || path.startsWith(SHORTLOCALURI)) {
            final XmldbURL url = new XmldbURL(path);
            final EmbeddedInputStream eis = new EmbeddedInputStream(bPool, url);
            inputsource.setByteStream(eis);
            inputsource.setSystemId(path);
        } else {
            final InputStream is = new URL(path).openStream();
            inputsource.setByteStream(is);
            inputsource.setSystemId(path);
        }
    }
    return inputsource;
}
Also used : InputSource(org.xml.sax.InputSource) XmldbURL(org.exist.protocolhandler.xmldb.XmldbURL) EmbeddedInputStream(org.exist.protocolhandler.embedded.EmbeddedInputStream) InputStream(java.io.InputStream) EmbeddedInputStream(org.exist.protocolhandler.embedded.EmbeddedInputStream) URL(java.net.URL) XmldbURL(org.exist.protocolhandler.xmldb.XmldbURL)

Example 3 with XmldbURL

use of org.exist.protocolhandler.xmldb.XmldbURL in project exist by eXist-db.

the class ExistResolver method resolveStreamSource.

private StreamSource resolveStreamSource(BrokerPool bPool, String path) throws TransformerException {
    LOG.debug("Resolving {}", path);
    final StreamSource streamsource = new StreamSource();
    try {
        if (path != null) {
            if (path.startsWith(LOCALURI) || path.startsWith(SHORTLOCALURI)) {
                final XmldbURL url = new XmldbURL(path);
                final EmbeddedInputStream eis = new EmbeddedInputStream(bPool, url);
                streamsource.setInputStream(eis);
                streamsource.setSystemId(path);
            } else {
                final InputStream is = new URL(path).openStream();
                streamsource.setInputStream(is);
                streamsource.setSystemId(path);
            }
        }
    } catch (final IOException ex) {
        throw new TransformerException(ex);
    }
    return streamsource;
}
Also used : XmldbURL(org.exist.protocolhandler.xmldb.XmldbURL) EmbeddedInputStream(org.exist.protocolhandler.embedded.EmbeddedInputStream) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) EmbeddedInputStream(org.exist.protocolhandler.embedded.EmbeddedInputStream) IOException(java.io.IOException) URL(java.net.URL) XmldbURL(org.exist.protocolhandler.xmldb.XmldbURL) TransformerException(javax.xml.transform.TransformerException)

Example 4 with XmldbURL

use of org.exist.protocolhandler.xmldb.XmldbURL in project exist by eXist-db.

the class XmlrpcDownload method stream.

/**
 * Write document referred by the URL to the output stream.
 *
 * @param xmldbURL Document location in database.
 * @param os Stream to which the document is written.
 * @throws IOException An IO error occurred.
 */
public void stream(final XmldbURL xmldbURL, final OutputStream os) throws IOException {
    LOG.debug("Begin document download");
    try {
        final XmlRpcClient client = new XmlRpcClient();
        final XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
        config.setEncoding("UTF-8");
        config.setEnabledForExtensions(true);
        config.setServerURL(new URL(xmldbURL.getXmlRpcURL()));
        // Setup client client
        if (xmldbURL.hasUserInfo()) {
            config.setBasicUserName(xmldbURL.getUsername());
            config.setBasicPassword(xmldbURL.getPassword());
        }
        client.setConfig(config);
        // Setup xml serializer
        final Map<String, String> options = new HashMap<>();
        options.put("indent", "no");
        options.put("encoding", "UTF-8");
        // Setup client parameters
        final List<Object> params = new ArrayList<>();
        params.add(xmldbURL.getCollectionPath());
        params.add(options);
        // Shoot first method write data
        Map ht = (Map) client.execute("getDocumentData", params);
        int offset = (int) ht.get("offset");
        byte[] data = (byte[]) ht.get("data");
        final String handle = (String) ht.get("handle");
        os.write(data);
        // When there is more data to download
        while (offset != 0) {
            // Clean and re-setup client parameters
            params.clear();
            params.add(handle);
            params.add(offset);
            // Get and write next chunk
            ht = (Map) client.execute("getNextChunk", params);
            data = (byte[]) ht.get("data");
            offset = (int) ht.get("offset");
            os.write(data);
        }
    } catch (final XmlRpcException ex) {
        LOG.error(ex);
        throw new IOException(ex.getMessage(), ex);
    } catch (final IOException ex) {
        LOG.error(ex);
        throw ex;
    } finally {
        LOG.debug("Finished document download");
    }
}
Also used : XmlRpcClient(org.apache.xmlrpc.client.XmlRpcClient) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) XmlRpcClientConfigImpl(org.apache.xmlrpc.client.XmlRpcClientConfigImpl) IOException(java.io.IOException) URL(java.net.URL) XmldbURL(org.exist.protocolhandler.xmldb.XmldbURL) HashMap(java.util.HashMap) Map(java.util.Map) XmlRpcException(org.apache.xmlrpc.XmlRpcException)

Example 5 with XmldbURL

use of org.exist.protocolhandler.xmldb.XmldbURL in project exist by eXist-db.

the class XmlrpcUpload method stream.

/**
 * Write data from a (input)stream to the specified XMLRPC url and leave
 * the input stream open.
 *
 * @param xmldbURL URL pointing to location on eXist-db server.
 * @param is Document stream
 * @throws IOException When something is wrong.
 */
public void stream(XmldbURL xmldbURL, InputStream is) throws IOException {
    LOG.debug("Begin document upload");
    try {
        // Setup xmlrpc client
        final XmlRpcClient client = new XmlRpcClient();
        final XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
        config.setEncoding("UTF-8");
        config.setEnabledForExtensions(true);
        config.setServerURL(new URL(xmldbURL.getXmlRpcURL()));
        if (xmldbURL.hasUserInfo()) {
            config.setBasicUserName(xmldbURL.getUsername());
            config.setBasicPassword(xmldbURL.getPassword());
        }
        client.setConfig(config);
        String contentType = MimeType.BINARY_TYPE.getName();
        final MimeType mime = MimeTable.getInstance().getContentTypeFor(xmldbURL.getDocumentName());
        if (mime != null) {
            contentType = mime.getName();
        }
        // Initialize xmlrpc parameters
        final List<Object> params = new ArrayList<>(5);
        String handle = null;
        // Copy data from inputstream to database
        final byte[] buf = new byte[4096];
        int len;
        while ((len = is.read(buf)) > 0) {
            params.clear();
            if (handle != null) {
                params.add(handle);
            }
            params.add(buf);
            params.add(len);
            handle = (String) client.execute("upload", params);
        }
        // All data transported, now parse data on server
        params.clear();
        params.add(handle);
        params.add(xmldbURL.getCollectionPath());
        params.add(Boolean.TRUE);
        params.add(contentType);
        final Boolean result = (Boolean) client.execute("parseLocal", params);
        // Check XMLRPC result
        if (result) {
            LOG.debug("Document stored.");
        } else {
            LOG.debug("Could not store document.");
            throw new IOException("Could not store document.");
        }
    } catch (final IOException ex) {
        LOG.debug(ex);
        throw ex;
    } catch (final Exception ex) {
        LOG.debug(ex);
        throw new IOException(ex.getMessage(), ex);
    } finally {
        LOG.debug("Finished document upload");
    }
}
Also used : XmlRpcClient(org.apache.xmlrpc.client.XmlRpcClient) ArrayList(java.util.ArrayList) XmlRpcClientConfigImpl(org.apache.xmlrpc.client.XmlRpcClientConfigImpl) IOException(java.io.IOException) URL(java.net.URL) XmldbURL(org.exist.protocolhandler.xmldb.XmldbURL) MimeType(org.exist.util.MimeType) IOException(java.io.IOException)

Aggregations

XmldbURL (org.exist.protocolhandler.xmldb.XmldbURL)7 URL (java.net.URL)5 InputStream (java.io.InputStream)4 EmbeddedInputStream (org.exist.protocolhandler.embedded.EmbeddedInputStream)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)2 XmlRpcClient (org.apache.xmlrpc.client.XmlRpcClient)2 XmlRpcClientConfigImpl (org.apache.xmlrpc.client.XmlRpcClientConfigImpl)2 XmlrpcInputStream (org.exist.protocolhandler.xmlrpc.XmlrpcInputStream)2 OutputStream (java.io.OutputStream)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TransformerException (javax.xml.transform.TransformerException)1 StreamSource (javax.xml.transform.stream.StreamSource)1 XMLInputSource (org.apache.xerces.xni.parser.XMLInputSource)1 XmlRpcException (org.apache.xmlrpc.XmlRpcException)1 EmbeddedOutputStream (org.exist.protocolhandler.embedded.EmbeddedOutputStream)1 XmlrpcOutputStream (org.exist.protocolhandler.xmlrpc.XmlrpcOutputStream)1 MimeType (org.exist.util.MimeType)1 InputSource (org.xml.sax.InputSource)1