Search in sources :

Example 81 with EXistException

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

the class Counters method nextValue.

/**
 * Retrieves the next value of a counter (specified by name).
 *
 * @param counterName the name of the counter
 *
 * @return the next counter value or -1 if the counter does not exist.
 *
 * @throws EXistException if an error occurs
 */
public synchronized long nextValue(final String counterName) throws EXistException {
    if (!counters.containsKey(counterName)) {
        return -1;
    }
    long c = counters.get(counterName);
    c++;
    counters.put(counterName, c);
    try {
        serializeTable();
    } catch (final IOException e) {
        throw new EXistException("Unable to save to counter store file.", e);
    }
    return c;
}
Also used : IOException(java.io.IOException) EXistException(org.exist.EXistException)

Example 82 with EXistException

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

the class EmbeddedInputStream method openStream.

private static Either<IOException, InputStream> openStream(final BrokerPool pool, final XmldbURL url) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Begin document download");
    }
    try {
        final XmldbURI path = XmldbURI.create(url.getPath());
        try (final DBBroker broker = pool.getBroker()) {
            try (final LockedDocument lockedResource = broker.getXMLResource(path, Lock.LockMode.READ_LOCK)) {
                if (lockedResource == null) {
                    // Test for collection
                    try (final Collection collection = broker.openCollection(path, Lock.LockMode.READ_LOCK)) {
                        if (collection == null) {
                            // No collection, no document
                            return Left(new IOException("Resource " + url.getPath() + " not found."));
                        } else {
                            // Collection
                            return Left(new IOException("Resource " + url.getPath() + " is a collection."));
                        }
                    }
                } else {
                    final DocumentImpl resource = lockedResource.getDocument();
                    if (resource.getResourceType() == DocumentImpl.XML_FILE) {
                        final Serializer serializer = broker.borrowSerializer();
                        try {
                            // Preserve doctype
                            serializer.setProperty(EXistOutputKeys.OUTPUT_DOCTYPE, "yes");
                            // serialize the XML to a temporary file
                            final TemporaryFileManager tempFileManager = TemporaryFileManager.getInstance();
                            final Path tempFile = tempFileManager.getTemporaryFile();
                            try (final Writer writer = Files.newBufferedWriter(tempFile, UTF_8, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE)) {
                                serializer.serialize(resource, writer);
                            }
                            // NOTE: the temp file will be returned to the manager when the InputStream is closed
                            return Right(new CloseNotifyingInputStream(Files.newInputStream(tempFile, StandardOpenOption.READ), () -> tempFileManager.returnTemporaryFile(tempFile)));
                        } finally {
                            broker.returnSerializer(serializer);
                        }
                    } else if (resource.getResourceType() == BinaryDocument.BINARY_FILE) {
                        return Right(broker.getBinaryResource((BinaryDocument) resource));
                    } else {
                        return Left(new IOException("Unknown resource type " + url.getPath() + ": " + resource.getResourceType()));
                    }
                }
            } finally {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("End document download");
                }
            }
        }
    } catch (final EXistException | PermissionDeniedException | SAXException e) {
        LOG.error(e);
        return Left(new IOException(e.getMessage(), e));
    } catch (final IOException e) {
        return Left(e);
    }
}
Also used : Path(java.nio.file.Path) CloseNotifyingInputStream(org.exist.util.io.CloseNotifyingInputStream) EXistException(org.exist.EXistException) DocumentImpl(org.exist.dom.persistent.DocumentImpl) TemporaryFileManager(org.exist.util.io.TemporaryFileManager) SAXException(org.xml.sax.SAXException) DBBroker(org.exist.storage.DBBroker) LockedDocument(org.exist.dom.persistent.LockedDocument) Collection(org.exist.collections.Collection) PermissionDeniedException(org.exist.security.PermissionDeniedException) XmldbURI(org.exist.xmldb.XmldbURI) Serializer(org.exist.storage.serializers.Serializer)

Example 83 with EXistException

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

the class Deployment method storeRepoXML.

/**
 * Store repo.xml into the db. Adds the time of deployment to the descriptor.
 *
 * @param repoXML
 * @param targetCollection
 * @throws XPathException
 */
private void storeRepoXML(final DBBroker broker, final Txn transaction, final DocumentImpl repoXML, final XmldbURI targetCollection, final Optional<RequestedPerms> requestedPerms) throws PackageException, XPathException {
    // Store repo.xml
    final DateTimeValue time = new DateTimeValue(new Date());
    final MemTreeBuilder builder = new MemTreeBuilder();
    builder.startDocument();
    final UpdatingDocumentReceiver receiver = new UpdatingDocumentReceiver(builder, time.getStringValue());
    try {
        repoXML.copyTo(broker, receiver);
    } catch (final SAXException e) {
        throw new PackageException("Error while updating repo.xml in-memory: " + e.getMessage(), e);
    }
    builder.endDocument();
    final DocumentImpl updatedXML = builder.getDocument();
    try {
        final Collection collection = broker.getOrCreateCollection(transaction, targetCollection);
        final XmldbURI name = XmldbURI.createInternal("repo.xml");
        final Permission permission = PermissionFactory.getDefaultResourcePermission(broker.getBrokerPool().getSecurityManager());
        setPermissions(broker, requestedPerms, false, MimeType.XML_TYPE, permission);
        collection.storeDocument(transaction, broker, name, updatedXML, MimeType.XML_TYPE, null, null, permission, null, null);
    } catch (final PermissionDeniedException | IOException | SAXException | LockException | EXistException e) {
        throw new PackageException("Error while storing updated repo.xml: " + e.getMessage(), e);
    }
}
Also used : DateTimeValue(org.exist.xquery.value.DateTimeValue) IOException(java.io.IOException) EXistException(org.exist.EXistException) SAXException(org.xml.sax.SAXException) UnixStylePermission(org.exist.security.UnixStylePermission) Permission(org.exist.security.Permission) Collection(org.exist.collections.Collection) PermissionDeniedException(org.exist.security.PermissionDeniedException) XmldbURI(org.exist.xmldb.XmldbURI)

Example 84 with EXistException

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

the class AbstractRealm method initialiseRealmStorage.

private void initialiseRealmStorage(final DBBroker broker, final Txn transaction) throws EXistException {
    final XmldbURI realmCollectionURL = SecurityManager.SECURITY_COLLECTION_URI.append(getId());
    try {
        collectionRealm = Utils.getOrCreateCollection(broker, transaction, realmCollectionURL);
        collectionAccounts = Utils.getOrCreateCollection(broker, transaction, realmCollectionURL.append("accounts"));
        collectionGroups = Utils.getOrCreateCollection(broker, transaction, realmCollectionURL.append("groups"));
        collectionRemovedAccounts = Utils.getOrCreateCollection(broker, transaction, realmCollectionURL.append("accounts").append("removed"));
        collectionRemovedGroups = Utils.getOrCreateCollection(broker, transaction, realmCollectionURL.append("groups").append("removed"));
    } catch (final PermissionDeniedException | IOException | TriggerException | LockException e) {
        throw new EXistException(e);
    }
}
Also used : LockException(org.exist.util.LockException) IOException(java.io.IOException) EXistException(org.exist.EXistException) TriggerException(org.exist.collections.triggers.TriggerException) XmldbURI(org.exist.xmldb.XmldbURI)

Example 85 with EXistException

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

the class EXistDBLoginModule method login.

/**
 * Authenticate the user by prompting for a user name and password.
 *
 * @return true in all cases since this <code>LoginModule</code> should not
 *         be ignored.
 *
 * @throws FailedLoginException
 *                if the authentication fails.
 *
 * @throws LoginException
 *                if this <code>LoginModule</code> is unable to perform the
 *                authentication.
 */
public boolean login() throws LoginException {
    // prompt for a user name and password
    if (callbackHandler == null) {
        throw new LoginException("Error: no CallbackHandler available " + "to garner authentication information from the user");
    }
    final Callback[] callbacks = new Callback[2];
    callbacks[0] = new NameCallback("user name: ");
    callbacks[1] = new PasswordCallback("password: ", false);
    // username and password
    String username;
    char[] password;
    try {
        callbackHandler.handle(callbacks);
        username = ((NameCallback) callbacks[0]).getName();
        char[] tmpPassword = ((PasswordCallback) callbacks[1]).getPassword();
        if (tmpPassword == null) {
            // treat a NULL password as an empty password
            tmpPassword = new char[0];
        }
        password = new char[tmpPassword.length];
        System.arraycopy(tmpPassword, 0, password, 0, tmpPassword.length);
        ((PasswordCallback) callbacks[1]).clearPassword();
    } catch (final java.io.IOException ioe) {
        throw new LoginException(ioe.toString());
    } catch (final UnsupportedCallbackException uce) {
        throw new LoginException("Error: " + uce.getCallback().toString() + " not available to garner authentication information" + " from the user");
    }
    // print debugging information
    if (debug) {
        System.out.println("\t\t[eXistLoginModule] user entered user name: " + username);
    }
    try {
        userPrincipal = BrokerPool.getInstance().getSecurityManager().authenticate(username, password);
    } catch (final AuthenticationException e) {
        if (debug) {
            System.out.println("\t\t[eXistLoginModule] authentication failed");
        }
        throw new FailedLoginException(e.getMessage());
    } catch (final EXistException e) {
        throw new FailedLoginException(e.getMessage());
    }
    succeeded = userPrincipal.isAuthenticated();
    return true;
}
Also used : AuthenticationException(org.exist.security.AuthenticationException) EXistException(org.exist.EXistException) PasswordCallback(javax.security.auth.callback.PasswordCallback) NameCallback(javax.security.auth.callback.NameCallback) Callback(javax.security.auth.callback.Callback) NameCallback(javax.security.auth.callback.NameCallback) FailedLoginException(javax.security.auth.login.FailedLoginException) LoginException(javax.security.auth.login.LoginException) FailedLoginException(javax.security.auth.login.FailedLoginException) PasswordCallback(javax.security.auth.callback.PasswordCallback) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException)

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