use of org.exist.security.PermissionDeniedException in project exist by eXist-db.
the class EmbeddedOutputStream method uploadToDb.
private static void uploadToDb(final BrokerPool pool, final XmldbURL url, final Path tempFile) throws IOException {
try (final DBBroker broker = pool.getBroker()) {
final XmldbURI collectionUri = XmldbURI.create(url.getCollection());
final XmldbURI documentUri = XmldbURI.create(url.getDocumentName());
try (final Collection collection = broker.openCollection(collectionUri, Lock.LockMode.WRITE_LOCK)) {
if (collection == null) {
throw new IOException("Resource " + collectionUri.toString() + " is not a collection.");
}
if (collection.hasChildCollection(broker, documentUri)) {
throw new IOException("Resource " + documentUri.toString() + " is a collection.");
}
final MimeType mime = MimeTable.getInstance().getContentTypeFor(documentUri);
final TransactionManager transact = pool.getTransactionManager();
try (final Txn txn = transact.beginTransaction()) {
broker.storeDocument(txn, documentUri, new FileInputSource(tempFile), mime, collection);
txn.commit();
}
}
} catch (final EXistException | PermissionDeniedException | LockException | SAXException e) {
LOG.error(e);
throw new IOException(e.getMessage(), e);
} finally {
if (LOG.isDebugEnabled()) {
LOG.debug("End document upload");
}
}
}
use of org.exist.security.PermissionDeniedException in project exist by eXist-db.
the class ConsistencyCheckTask method execute.
@Override
public void execute(final DBBroker broker, final Txn transaction) throws EXistException {
final Agent agentInstance = AgentFactory.getInstance();
final BrokerPool brokerPool = broker.getBrokerPool();
final TaskStatus endStatus = new TaskStatus(TaskStatus.Status.STOPPED_OK);
agentInstance.changeStatus(brokerPool, new TaskStatus(TaskStatus.Status.INIT));
if (paused) {
LOG.info("Consistency check is paused.");
agentInstance.changeStatus(brokerPool, new TaskStatus(TaskStatus.Status.PAUSED));
return;
}
brokerPool.getProcessMonitor().startJob(ProcessMonitor.ACTION_BACKUP, null, monitor);
PrintWriter report = null;
try {
boolean doBackup = createBackup;
// TODO: don't use the direct access feature for now. needs more testing
List<ErrorReport> errors = null;
if (!incremental || incrementalCheck) {
LOG.info("Starting consistency check...");
report = openLog();
final CheckCallback cb = new CheckCallback(report);
final ConsistencyCheck check = new ConsistencyCheck(broker, transaction, false, checkDocs);
agentInstance.changeStatus(brokerPool, new TaskStatus(TaskStatus.Status.RUNNING_CHECK));
errors = check.checkAll(cb);
if (!errors.isEmpty()) {
endStatus.setStatus(TaskStatus.Status.STOPPED_ERROR);
endStatus.setReason(errors);
LOG.error("Errors found: {}", errors.size());
doBackup = true;
if (fatalErrorsFound(errors)) {
LOG.error("Fatal errors were found: pausing the consistency check task.");
paused = true;
}
}
LOG.info("Finished consistency check");
}
if (doBackup) {
LOG.info("Starting backup...");
final SystemExport sysexport = new SystemExport(broker, transaction, logCallback, monitor, false);
lastExportedBackup = sysexport.export(exportDir, incremental, maxInc, createZip, errors);
agentInstance.changeStatus(brokerPool, new TaskStatus(TaskStatus.Status.RUNNING_BACKUP));
if (lastExportedBackup != null) {
LOG.info("Created backup to file: {}", lastExportedBackup.toAbsolutePath().toString());
}
LOG.info("Finished backup");
}
} catch (final TerminatedException | PermissionDeniedException e) {
throw new EXistException(e.getMessage(), e);
} finally {
if (report != null) {
report.close();
}
agentInstance.changeStatus(brokerPool, endStatus);
brokerPool.getProcessMonitor().endJob();
}
}
use of org.exist.security.PermissionDeniedException in project exist by eXist-db.
the class LocalXPathQueryService method compileAndCheck.
private Either<XPathException, CompiledExpression> compileAndCheck(final DBBroker broker, final Txn transaction, final String query) throws XMLDBException {
final long start = System.currentTimeMillis();
final XQuery xquery = broker.getBrokerPool().getXQueryService();
final XQueryContext context = new XQueryContext(broker.getBrokerPool());
try {
setupContext(null, context);
final CompiledExpression expr = xquery.compile(context, query);
if (LOG.isDebugEnabled()) {
LOG.debug("compilation took {}", System.currentTimeMillis() - start);
}
return Either.Right(expr);
} catch (final PermissionDeniedException e) {
throw new XMLDBException(ErrorCodes.PERMISSION_DENIED, e.getMessage(), e);
} catch (final IllegalArgumentException e) {
throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.getMessage(), e);
} catch (final XPathException e) {
return Either.Left(e);
}
}
use of org.exist.security.PermissionDeniedException in project exist by eXist-db.
the class RemoteCollection method getResource.
@Override
public Resource getResource(final String name) throws XMLDBException {
final List<String> params = new ArrayList<>(1);
XmldbURI docUri;
try {
docUri = XmldbURI.xmldbUriFor(name);
} catch (final URISyntaxException e) {
throw new XMLDBException(ErrorCodes.INVALID_URI, e);
}
params.add(getPathURI().append(docUri).toString());
final Map hash;
hash = (Map) execute("describeResource", params);
final String docName = (String) hash.get("name");
if (docName == null) {
// resource does not exist!
return null;
}
try {
docUri = XmldbURI.xmldbUriFor(docName).lastSegment();
} catch (final URISyntaxException e) {
throw new XMLDBException(ErrorCodes.INVALID_URI, e);
}
final String owner = (String) hash.get("owner");
final String group = (String) hash.get("group");
final int mode = (Integer) hash.get("permissions");
final Stream<ACEAider> aces = extractAces(hash.get("acl"));
final Permission perm;
try {
perm = getPermission(owner, group, mode, aces);
} catch (final PermissionDeniedException pde) {
throw new XMLDBException(ErrorCodes.PERMISSION_DENIED, "Unable to retrieve permissions for resource '" + name + "': " + pde.getMessage(), pde);
}
final String type = (String) hash.get("type");
long contentLen = 0;
if (hash.containsKey("content-length-64bit")) {
final Object o = hash.get("content-length-64bit");
if (o instanceof Long) {
contentLen = (Long) o;
} else {
contentLen = Long.parseLong((String) o);
}
} else if (hash.containsKey("content-length")) {
contentLen = (Integer) hash.get("content-length");
}
final AbstractRemoteResource r;
if (type == null || "XMLResource".equals(type)) {
r = new RemoteXMLResource(this, -1, -1, docUri, Optional.empty());
} else {
r = new RemoteBinaryResource(this, docUri);
if (hash.containsKey("blob-id")) {
final byte[] blobId = (byte[]) hash.get("blob-id");
((RemoteBinaryResource) r).setBlobId(new BlobId(blobId));
}
if (hash.containsKey("digest-algorithm") && hash.containsKey("digest")) {
final String digestAlgorithm = (String) hash.get("digest-algorithm");
final byte[] digest = (byte[]) hash.get("digest");
final MessageDigest messageDigest = new MessageDigest(DigestType.forCommonName(digestAlgorithm), digest);
((RemoteBinaryResource) r).setContentDigest(messageDigest);
}
}
r.setPermissions(perm);
r.setContentLength(contentLen);
r.dateCreated = (Date) hash.get("created");
r.dateModified = (Date) hash.get("modified");
if (hash.containsKey("mime-type")) {
r.setMimeType((String) hash.get("mime-type"));
}
return r;
}
use of org.exist.security.PermissionDeniedException in project exist by eXist-db.
the class DatabaseResources method executeQuery.
public Sequence executeQuery(String queryPath, Map<String, String> params, Subject user) {
final String namespace = params.get(TARGETNAMESPACE);
final String publicId = params.get(PUBLICID);
final String catalogPath = params.get(CATALOG);
final String collection = params.get(COLLECTION);
if (logger.isDebugEnabled()) {
logger.debug("collection={} namespace={} publicId={} catalogPath={}", collection, namespace, publicId, catalogPath);
}
Sequence result = null;
try (final DBBroker broker = brokerPool.get(Optional.ofNullable(user))) {
final XQuery xquery = brokerPool.getXQueryService();
final XQueryContext context = new XQueryContext(brokerPool);
if (collection != null) {
context.declareVariable(COLLECTION, collection);
}
if (namespace != null) {
context.declareVariable(TARGETNAMESPACE, namespace);
}
if (publicId != null) {
context.declareVariable(PUBLICID, publicId);
}
if (catalogPath != null) {
context.declareVariable(CATALOG, catalogPath);
}
CompiledXQuery compiled = xquery.compile(context, new ClassLoaderSource(queryPath));
result = xquery.execute(broker, compiled, null);
} catch (final EXistException | XPathException | IOException | PermissionDeniedException ex) {
logger.error("Problem executing xquery", ex);
result = null;
}
return result;
}
Aggregations