Search in sources :

Example 46 with XMLDBException

use of org.xmldb.api.base.XMLDBException in project exist by eXist-db.

the class LocalCollection method getResource.

Resource getResource(final DBBroker broker, final Txn transaction, final XmldbURI idURI) throws XMLDBException {
    return this.<Resource>read(broker, transaction).apply((collection, broker1, transaction1) -> {
        try (final LockedDocument lockedDocument = collection.getDocumentWithLock(broker1, idURI, LockMode.READ_LOCK)) {
            // NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
            collection.close();
            final DocumentImpl document = lockedDocument == null ? null : lockedDocument.getDocument();
            if (document == null) {
                LOG.warn("Resource {} not found", idURI);
                return null;
            }
            final Resource r;
            switch(document.getResourceType()) {
                case DocumentImpl.XML_FILE:
                    r = new LocalXMLResource(user, brokerPool, this, idURI);
                    break;
                case DocumentImpl.BINARY_FILE:
                    r = new LocalBinaryResource(user, brokerPool, this, idURI);
                    break;
                default:
                    throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, "Unknown resource type");
            }
            ((AbstractEXistResource) r).setMimeType(document.getMimeType());
            return r;
        }
    });
}
Also used : LockedDocument(org.exist.dom.persistent.LockedDocument) BinaryResource(org.xmldb.api.modules.BinaryResource) XMLResource(org.xmldb.api.modules.XMLResource) Resource(org.xmldb.api.base.Resource) XMLDBException(org.xmldb.api.base.XMLDBException) DocumentImpl(org.exist.dom.persistent.DocumentImpl)

Example 47 with XMLDBException

use of org.xmldb.api.base.XMLDBException in project exist by eXist-db.

the class LocalResourceSet method getMembersAsResource.

@Override
public Resource getMembersAsResource() throws XMLDBException {
    return this.<Resource>withDb((broker, transaction) -> {
        final Serializer serializer = broker.borrowSerializer();
        final SAXSerializer handler = (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);
        final StringWriter writer = new StringWriter();
        handler.setOutput(writer, outputProperties);
        try {
            // configure the serializer
            collection.setProperty(Serializer.GENERATE_DOC_EVENTS, "false");
            serializer.setProperties(outputProperties);
            serializer.setUser(user);
            serializer.setSAXHandlers(handler, handler);
            // serialize results
            handler.startDocument();
            handler.startPrefixMapping("exist", Namespaces.EXIST_NS);
            final AttributesImpl attribs = new AttributesImpl();
            attribs.addAttribute("", "hitCount", "hitCount", "CDATA", Integer.toString(resources.size()));
            handler.startElement(Namespaces.EXIST_NS, "result", "exist:result", attribs);
            Item current;
            char[] value;
            for (Object resource : resources) {
                current = (Item) resource;
                if (Type.subTypeOf(current.getType(), Type.NODE)) {
                    ((NodeValue) current).toSAX(broker, handler, outputProperties);
                } else {
                    value = current.toString().toCharArray();
                    handler.characters(value, 0, value.length);
                }
            }
            handler.endElement(Namespaces.EXIST_NS, "result", "exist:result");
            handler.endPrefixMapping("exist");
            handler.endDocument();
            final Resource res = new LocalXMLResource(user, brokerPool, collection, XmldbURI.EMPTY_URI);
            res.setContent(writer.toString());
            return res;
        } catch (final SAXException e) {
            throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR, "serialization error", e);
        } finally {
            SerializerPool.getInstance().returnObject(handler);
            broker.returnSerializer(serializer);
        }
    });
}
Also used : AttributesImpl(org.xml.sax.helpers.AttributesImpl) StringWriter(java.io.StringWriter) Resource(org.xmldb.api.base.Resource) XMLDBException(org.xmldb.api.base.XMLDBException) SAXSerializer(org.exist.util.serializer.SAXSerializer) SAXSerializer(org.exist.util.serializer.SAXSerializer) Serializer(org.exist.storage.serializers.Serializer) SAXException(org.xml.sax.SAXException)

Example 48 with XMLDBException

use of org.xmldb.api.base.XMLDBException in project exist by eXist-db.

the class AbstractLocal method withDb.

/**
 * Higher-order-function for performing an XMLDB operation on
 * the database
 *
 * @param dbOperation The operation to perform on the database
 * @param <R> The return type of the operation
 *
 * @return the result of the operation
 *
 * @throws XMLDBException if an error occurs when executing the operation.
 */
protected <R> R withDb(final LocalXmldbFunction<R> dbOperation) throws XMLDBException {
    try (final DBBroker broker = brokerPool.get(Optional.ofNullable(user));
        final Txn transaction = transaction().apply(broker)) {
        try {
            final R result = dbOperation.apply(broker, transaction);
            transaction.commit();
            return result;
        } catch (final XMLDBException | EXistException e) {
            transaction.abort();
            throw e;
        }
    } catch (final EXistException e) {
        throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.getMessage(), e);
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) XMLDBException(org.xmldb.api.base.XMLDBException) Txn(org.exist.storage.txn.Txn) EXistException(org.exist.EXistException)

Example 49 with XMLDBException

use of org.xmldb.api.base.XMLDBException in project exist by eXist-db.

the class DatabaseImpl method configure.

/**
 * In embedded mode: configure the database instance
 *
 * @throws XMLDBException Description of the Exception
 */
private void configure(final String instanceName) throws XMLDBException {
    try {
        final Configuration config = new Configuration(configuration, Optional.empty());
        if (dataDir != null) {
            config.setProperty(BrokerPool.PROPERTY_DATA_DIR, Paths.get(dataDir));
        }
        if (journalDir != null) {
            config.setProperty(Journal.PROPERTY_RECOVERY_JOURNAL_DIR, Paths.get(journalDir));
        }
        BrokerPool.configure(instanceName, 1, 5, config);
        if (shutdown != null) {
            BrokerPool.getInstance(instanceName).registerShutdownListener(shutdown);
        }
        currentInstanceName = instanceName;
    } catch (final Exception e) {
        throw new XMLDBException(ErrorCodes.VENDOR_ERROR, "configuration error: " + e.getMessage(), e);
    }
}
Also used : Configuration(org.exist.util.Configuration) XMLDBException(org.xmldb.api.base.XMLDBException) URISyntaxException(java.net.URISyntaxException) EXistException(org.exist.EXistException) XMLDBException(org.xmldb.api.base.XMLDBException) AuthenticationException(org.exist.security.AuthenticationException) MalformedURLException(java.net.MalformedURLException)

Example 50 with XMLDBException

use of org.xmldb.api.base.XMLDBException in project exist by eXist-db.

the class DatabaseImpl method getCollection.

/**
 * @deprecated {@link org.exist.xmldb.DatabaseImpl#getCollection(org.exist.xmldb.XmldbURI, java.lang.String, java.lang.String)}
 */
@Deprecated
@Override
public Collection getCollection(final String uri, final String user, final String password) throws XMLDBException {
    try {
        // Ugly workaround for non-URI compliant collection names (most likely IRIs)
        final String newURIString = XmldbURI.recoverPseudoURIs(uri);
        // Remember that DatabaseManager (provided in xmldb.jar) trims the leading "xmldb:" !!!
        // ... prepend it to have a real xmldb URI again...
        final XmldbURI xmldbURI = XmldbURI.xmldbUriFor(XmldbURI.XMLDB_URI_PREFIX + newURIString);
        return getCollection(xmldbURI, user, password);
    } catch (final URISyntaxException e) {
        // ... even in the error message
        throw new XMLDBException(ErrorCodes.INVALID_DATABASE, "xmldb URI is not well formed: " + XmldbURI.XMLDB_URI_PREFIX + uri);
    }
}
Also used : XMLDBException(org.xmldb.api.base.XMLDBException) URISyntaxException(java.net.URISyntaxException)

Aggregations

XMLDBException (org.xmldb.api.base.XMLDBException)174 Collection (org.xmldb.api.base.Collection)66 Resource (org.xmldb.api.base.Resource)34 URISyntaxException (java.net.URISyntaxException)30 XMLResource (org.xmldb.api.modules.XMLResource)30 ResourceSet (org.xmldb.api.base.ResourceSet)23 BuildException (org.apache.tools.ant.BuildException)21 IOException (java.io.IOException)19 XPathException (org.exist.xquery.XPathException)18 PermissionDeniedException (org.exist.security.PermissionDeniedException)16 SAXException (org.xml.sax.SAXException)16 EXistException (org.exist.EXistException)15 UserManagementService (org.exist.xmldb.UserManagementService)15 XPathQueryService (org.xmldb.api.modules.XPathQueryService)13 BinaryResource (org.xmldb.api.modules.BinaryResource)12 ArrayList (java.util.ArrayList)11 Account (org.exist.security.Account)11 EXistResource (org.exist.xmldb.EXistResource)10 EXistXPathQueryService (org.exist.xmldb.EXistXPathQueryService)10 Properties (java.util.Properties)9