Search in sources :

Example 96 with EXistException

use of org.exist.EXistException in project exist by eXist-db.

the class XQueryURLRewrite method configure.

private void configure() throws ServletException {
    if (pool != null) {
        return;
    }
    try {
        final Class<?> driver = Class.forName(DRIVER);
        final Database database = (Database) driver.newInstance();
        database.setProperty("create-database", "true");
        DatabaseManager.registerDatabase(database);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Initialized database");
        }
    } catch (final Exception e) {
        final String errorMessage = "Failed to initialize database driver";
        LOG.error(errorMessage, e);
        throw new ServletException(errorMessage + ": " + e.getMessage(), e);
    }
    try {
        pool = BrokerPool.getInstance();
    } catch (final EXistException e) {
        throw new ServletException("Could not initialize db: " + e.getMessage(), e);
    }
    defaultUser = pool.getSecurityManager().getGuestSubject();
    final String username = config.getInitParameter("user");
    if (username != null) {
        final String password = config.getInitParameter("password");
        try {
            final Subject user = pool.getSecurityManager().authenticate(username, password);
            if (user != null && user.isAuthenticated()) {
                defaultUser = user;
            }
        } catch (final AuthenticationException e) {
            LOG.error("User can not be authenticated ({}), using default user.", username);
        }
    }
    authenticator = new BasicAuthenticator(pool);
}
Also used : BasicAuthenticator(org.exist.http.servlets.BasicAuthenticator) AuthenticationException(org.exist.security.AuthenticationException) Database(org.xmldb.api.base.Database) EXistException(org.exist.EXistException) URISyntaxException(java.net.URISyntaxException) PermissionDeniedException(org.exist.security.PermissionDeniedException) LockException(org.exist.util.LockException) AuthenticationException(org.exist.security.AuthenticationException) SAXException(org.xml.sax.SAXException) EXistException(org.exist.EXistException) Subject(org.exist.security.Subject)

Example 97 with EXistException

use of org.exist.EXistException in project exist by eXist-db.

the class FileLockService method prepare.

@Override
public void prepare(final BrokerPool brokerPool) throws BrokerPoolServiceException {
    // try to acquire lock on the data dir
    final FileLock fileLock = new FileLock(brokerPool, dataDir.resolve(lockFileName));
    this.dataLock.compareAndSet(null, fileLock);
    try {
        final boolean locked = fileLock.tryLock();
        if (!locked) {
            throw new BrokerPoolServiceException(new EXistException("The directory seems to be locked by another " + "database instance. Found a valid lock file: " + fileLock.getFile().toAbsolutePath().toString()));
        }
    } catch (final ReadOnlyException e) {
        LOG.warn(e);
        writable = false;
    }
    if (!writable) {
        brokerPool.setReadOnly();
    }
}
Also used : BrokerPoolServiceException(org.exist.storage.BrokerPoolServiceException) EXistException(org.exist.EXistException) ReadOnlyException(org.exist.util.ReadOnlyException)

Example 98 with EXistException

use of org.exist.EXistException in project exist by eXist-db.

the class JournalManager method prepare.

@Override
public synchronized void prepare(final BrokerPool pool) throws BrokerPoolServiceException {
    if (!journallingDisabled) {
        try {
            this.journal = new Journal(pool, journalDir);
            this.journal.initialize();
            this.initialized = true;
        } catch (final EXistException | ReadOnlyException e) {
            throw new BrokerPoolServiceException(e);
        }
    }
}
Also used : BrokerPoolServiceException(org.exist.storage.BrokerPoolServiceException) EXistException(org.exist.EXistException) ReadOnlyException(org.exist.util.ReadOnlyException)

Example 99 with EXistException

use of org.exist.EXistException in project exist by eXist-db.

the class RpcConnection method describeResource.

private Map<String, Object> describeResource(final XmldbURI resourceUri) throws EXistException, PermissionDeniedException {
    try {
        return this.<Map<String, Object>>readDocument(resourceUri).apply((document, broker, transaction) -> {
            final Map<String, Object> hash = new HashMap<>(11);
            final Permission perms = document.getPermissions();
            hash.put("name", resourceUri.toString());
            hash.put("owner", perms.getOwner().getName());
            hash.put("group", perms.getGroup().getName());
            hash.put("permissions", perms.getMode());
            if (perms instanceof ACLPermission) {
                hash.put("acl", getACEs(perms));
            }
            hash.put("type", document.getResourceType() == DocumentImpl.BINARY_FILE ? "BinaryResource" : "XMLResource");
            final long resourceLength = document.getContentLength();
            hash.put("content-length", (resourceLength > (long) Integer.MAX_VALUE) ? Integer.MAX_VALUE : (int) resourceLength);
            hash.put("content-length-64bit", Long.toString(resourceLength));
            hash.put("mime-type", document.getMimeType());
            hash.put("created", new Date(document.getCreated()));
            hash.put("modified", new Date(document.getLastModified()));
            if (document.getResourceType() == DocumentImpl.BINARY_FILE) {
                hash.put("blob-id", ((BinaryDocument) document).getBlobId().getId());
                final MessageDigest messageDigest = broker.getBinaryResourceContentDigest(transaction, (BinaryDocument) document, DigestType.BLAKE_256);
                hash.put("digest-algorithm", messageDigest.getDigestType().getCommonNames()[0]);
                hash.put("digest", messageDigest.getValue());
            }
            return hash;
        });
    } catch (final EXistException e) {
        if (LOG.isDebugEnabled()) {
            LOG.debug(e);
        }
        return new HashMap<>();
    }
}
Also used : ACLPermission(org.exist.security.ACLPermission) ACLPermission(org.exist.security.ACLPermission) Permission(org.exist.security.Permission) EXistException(org.exist.EXistException) MessageDigest(org.exist.util.crypto.digest.MessageDigest)

Example 100 with EXistException

use of org.exist.EXistException in project exist by eXist-db.

the class RpcConnection method getContentDigest.

@Override
public Map<String, Object> getContentDigest(final String path, final String digestAlgorithm) throws EXistException, PermissionDeniedException {
    try {
        final DigestType digestType = DigestType.forCommonName(digestAlgorithm);
        final MessageDigest messageDigest = this.<MessageDigest>readDocument(XmldbURI.xmldbUriFor(path)).apply((document, broker, transaction) -> {
            if (document instanceof BinaryDocument) {
                return broker.getBinaryResourceContentDigest(transaction, (BinaryDocument) document, digestType);
            } else {
                throw new EXistException("Only supported for binary documents");
            }
        });
        final Map<String, Object> result = new HashMap<>();
        result.put("digest-algorithm", messageDigest.getDigestType().getCommonNames()[0]);
        result.put("digest", messageDigest.getValue());
        return result;
    } catch (final IllegalArgumentException | URISyntaxException e) {
        throw new EXistException(e);
    }
}
Also used : DigestType(org.exist.util.crypto.digest.DigestType) EXistException(org.exist.EXistException) URISyntaxException(java.net.URISyntaxException) MessageDigest(org.exist.util.crypto.digest.MessageDigest)

Aggregations

EXistException (org.exist.EXistException)218 PermissionDeniedException (org.exist.security.PermissionDeniedException)80 IOException (java.io.IOException)63 DBBroker (org.exist.storage.DBBroker)58 Txn (org.exist.storage.txn.Txn)46 XmldbURI (org.exist.xmldb.XmldbURI)42 Collection (org.exist.collections.Collection)41 SAXException (org.xml.sax.SAXException)32 LockException (org.exist.util.LockException)31 DocumentImpl (org.exist.dom.persistent.DocumentImpl)28 Subject (org.exist.security.Subject)23 XPathException (org.exist.xquery.XPathException)22 LockedDocument (org.exist.dom.persistent.LockedDocument)21 TriggerException (org.exist.collections.triggers.TriggerException)20 Path (java.nio.file.Path)19 URISyntaxException (java.net.URISyntaxException)18 BrokerPool (org.exist.storage.BrokerPool)18 TransactionManager (org.exist.storage.txn.TransactionManager)18 InputSource (org.xml.sax.InputSource)18 Sequence (org.exist.xquery.value.Sequence)17