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;
}
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);
}
}
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);
}
}
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);
}
}
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;
}
Aggregations