use of org.exist.xmldb.XmldbURI in project exist by eXist-db.
the class SystemImportHandler method restoreResourceEntry.
private DeferredPermission restoreResourceEntry(final Attributes atts) throws SAXException {
@Nullable final String skip = atts.getValue("skip");
// Don't process entries which should be skipped
if (skip != null && !"no".equals(skip)) {
return new SkippedEntryDeferredPermission();
}
@Nullable final String name = atts.getValue("name");
if (name == null) {
throw new SAXException("Resource requires a name attribute");
}
final boolean xmlType = Optional.ofNullable(atts.getValue("type")).filter(s -> s.equals("XMLResource")).isPresent();
final String owner = getAttr(atts, "owner", SecurityManager.SYSTEM);
final String group = getAttr(atts, "group", SecurityManager.DBA_GROUP);
final String perms = getAttr(atts, "mode", "644");
final String filename = getAttr(atts, "filename", name);
@Nullable final String mimeTypeStr = atts.getValue("mimetype");
@Nullable final String dateCreatedStr = atts.getValue("created");
@Nullable final String dateModifiedStr = atts.getValue("modified");
@Nullable final String publicId = atts.getValue("publicid");
@Nullable final String systemId = atts.getValue("systemid");
@Nullable final String nameDocType = atts.getValue("namedoctype");
MimeType mimeType = null;
if (mimeTypeStr != null) {
mimeType = MimeTable.getInstance().getContentType(mimeTypeStr);
}
if (mimeType == null) {
mimeType = xmlType ? MimeType.XML_TYPE : MimeType.BINARY_TYPE;
}
Date dateCreated = null;
if (dateCreatedStr != null) {
try {
dateCreated = new DateTimeValue(dateCreatedStr).getDate();
} catch (final XPathException xpe) {
listener.warn("Illegal creation date. Ignoring date...");
}
}
Date dateModified = null;
if (dateModifiedStr != null) {
try {
dateModified = new DateTimeValue(dateModifiedStr).getDate();
} catch (final XPathException xpe) {
listener.warn("Illegal modification date. Ignoring date...");
}
}
final DocumentType docType;
if (publicId != null || systemId != null) {
docType = new DocumentTypeImpl(nameDocType, publicId, systemId);
} else {
docType = null;
}
final XmldbURI docUri;
if (version >= STRICT_URI_VERSION) {
docUri = XmldbURI.create(name);
} else {
try {
docUri = URIUtils.encodeXmldbUriFor(name);
} catch (final URISyntaxException e) {
final String msg = "Could not parse document name into a URI: " + e.getMessage();
listener.error(msg);
LOG.error(msg, e);
return new SkippedEntryDeferredPermission();
}
}
try (final EXistInputSource is = descriptor.getInputSource(filename)) {
if (is == null) {
final String msg = "Failed to restore resource '" + name + "'\nfrom file '" + descriptor.getSymbolicPath(name, false) + "'.\nReason: Unable to obtain its EXistInputSource";
listener.warn(msg);
throw new RuntimeException(msg);
}
try (final Txn transaction = beginTransaction()) {
broker.storeDocument(transaction, docUri, is, mimeType, dateCreated, dateModified, null, docType, null, currentCollection);
try (final LockedDocument doc = currentCollection.getDocumentWithLock(broker, docUri, Lock.LockMode.READ_LOCK)) {
rh.startDocumentRestore(doc.getDocument(), atts);
}
transaction.commit();
final DeferredPermission deferredPermission;
if (name.startsWith(XmldbURI.SYSTEM_COLLECTION)) {
// prevents restore of a backup from changing system collection resource ownership
deferredPermission = new ResourceDeferredPermission(listener, currentCollection.getURI().append(name), SecurityManager.SYSTEM, SecurityManager.DBA_GROUP, Integer.parseInt(perms, 8));
} else {
deferredPermission = new ResourceDeferredPermission(listener, currentCollection.getURI().append(name), owner, group, Integer.parseInt(perms, 8));
}
try (final LockedDocument doc = currentCollection.getDocumentWithLock(broker, docUri, Lock.LockMode.READ_LOCK)) {
rh.endDocumentRestore(doc.getDocument());
}
listener.restoredResource(name);
return deferredPermission;
} catch (final Exception e) {
throw new IOException(e);
}
} catch (final Exception e) {
listener.warn("Failed to restore resource '" + name + "'\nfrom file '" + descriptor.getSymbolicPath(name, false) + "'.\nReason: " + e.getMessage());
LOG.error(e.getMessage(), e);
return new SkippedEntryDeferredPermission();
}
}
use of org.exist.xmldb.XmldbURI in project exist by eXist-db.
the class RestoreHandler method restoreCollectionEntry.
private DeferredPermission restoreCollectionEntry(final Attributes atts) throws SAXException {
final String name = atts.getValue("name");
if (name == null) {
throw new SAXException("Collection requires a name attribute");
}
final String owner = getAttr(atts, "owner", SecurityManager.SYSTEM);
final String group = getAttr(atts, "group", SecurityManager.DBA_GROUP);
final String mode = getAttr(atts, "mode", "644");
final String created = atts.getValue("created");
final String strVersion = atts.getValue("version");
if (strVersion != null) {
try {
this.version = Integer.parseInt(strVersion);
} catch (final NumberFormatException nfe) {
final String msg = "Could not parse version number for Collection '" + name + "', defaulting to version 0";
listener.warn(msg);
LOG.warn(msg);
this.version = 0;
}
}
try {
listener.createdCollection(name);
final XmldbURI collUri;
if (version >= STRICT_URI_VERSION) {
collUri = XmldbURI.create(name);
} else {
try {
collUri = URIUtils.encodeXmldbUriFor(name);
} catch (final URISyntaxException e) {
listener.warn("Could not parse document name into a URI: " + e.getMessage());
return new SkippedEntryDeferredPermission();
}
}
if (version >= BLOB_STORE_VERSION) {
this.deduplicateBlobs = Boolean.parseBoolean(atts.getValue("deduplicate-blobs"));
} else {
this.deduplicateBlobs = false;
}
final LockManager lockManager = broker.getBrokerPool().getLockManager();
try (final Txn transaction = beginTransaction();
final ManagedCollectionLock colLock = lockManager.acquireCollectionWriteLock(collUri)) {
Collection collection = broker.getCollection(collUri);
if (collection == null) {
final Tuple2<Permission, Long> creationAttributes = Tuple(null, getDateFromXSDateTimeStringForItem(created, name).getTime());
collection = broker.getOrCreateCollection(transaction, collUri, Optional.of(creationAttributes));
broker.saveCollection(transaction, collection);
}
transaction.commit();
this.currentCollectionUri = collection.getURI();
}
final DeferredPermission deferredPermission;
if (name.startsWith(XmldbURI.SYSTEM_COLLECTION)) {
// prevents restore of a backup from changing System collection ownership
deferredPermission = new CollectionDeferredPermission(listener, currentCollectionUri, SecurityManager.SYSTEM, SecurityManager.DBA_GROUP, Integer.parseInt(mode, 8));
} else {
deferredPermission = new CollectionDeferredPermission(listener, currentCollectionUri, owner, group, Integer.parseInt(mode, 8));
}
return deferredPermission;
} catch (final IOException | LockException | TransactionException | PermissionDeniedException e) {
final String msg = "An unrecoverable error occurred while restoring collection '" + name + "': " + e.getMessage() + ". Aborting restore!";
LOG.error(msg, e);
listener.warn(msg);
throw new SAXException(msg, e);
}
}
use of org.exist.xmldb.XmldbURI in project exist by eXist-db.
the class XMLDBCreateTask method mkcol.
private Collection mkcol(final Collection root, final String base, /*String path,*/
final String relPath) throws XMLDBException, URISyntaxException {
CollectionManagementService mgtService;
Collection current = root;
Collection c;
XmldbURI baseUri = XmldbURI.xmldbUriFor(base);
final XmldbURI collPath = XmldbURI.xmldbUriFor(relPath);
log("BASEURI=" + baseUri, Project.MSG_DEBUG);
log("RELPATH=" + relPath, Project.MSG_DEBUG);
// log("PATH=" + path, Project.MSG_DEBUG);
final XmldbURI[] segments = collPath.getPathSegments();
for (final XmldbURI segment : segments) {
baseUri = baseUri.append(segment);
log("Get collection " + baseUri, Project.MSG_DEBUG);
c = DatabaseManager.getCollection(baseUri.toString(), user, password);
if (c == null) {
log("Create collection management service for collection " + current.getName(), Project.MSG_DEBUG);
mgtService = (CollectionManagementService) current.getService("CollectionManagementService", "1.0");
log("Create child collection " + segment);
current = mgtService.createCollection(segment.toString());
log("Created collection " + current.getName() + '.');
} else {
current = c;
}
}
return (current);
}
use of org.exist.xmldb.XmldbURI in project exist by eXist-db.
the class XQueryStartupTrigger method createAutostartCollection.
/**
* Create autostart collection when not existent
*
* @param broker The exist-db broker
*/
private void createAutostartCollection(DBBroker broker) {
LOG.info("Creating {}", AUTOSTART_COLLECTION);
final TransactionManager txnManager = broker.getBrokerPool().getTransactionManager();
try (final Txn txn = txnManager.beginTransaction()) {
XmldbURI newCollection = XmldbURI.create(AUTOSTART_COLLECTION, true);
// Create collection
final Collection created = broker.getOrCreateCollection(txn, newCollection);
// Set ownership and mode
PermissionFactory.chown(broker, created, Optional.of(SecurityManager.SYSTEM), Optional.of(SecurityManager.DBA_GROUP));
PermissionFactory.chmod(broker, created, Optional.of(Permission.DEFAULT_SYSTEM_SECURITY_COLLECTION_PERM), Optional.empty());
broker.saveCollection(txn, created);
broker.flush();
// Commit change
txnManager.commit(txn);
if (LOG.isDebugEnabled()) {
LOG.debug("Finished creation of collection");
}
} catch (Throwable ex) {
LOG.error(ex);
}
}
use of org.exist.xmldb.XmldbURI in project exist by eXist-db.
the class ConfigurationDocumentTrigger method afterUpdateDocument.
@Override
public void afterUpdateDocument(final DBBroker broker, final Txn txn, final DocumentImpl document) throws TriggerException {
// check saving list
if (Configurator.saving.contains(Configurator.getFullURI(broker.getBrokerPool(), document.getURI()))) {
return;
}
checkForUpdates(broker, document.getURI(), document);
final XmldbURI uri = document.getCollection().getURI();
if (uri.startsWith(SecurityManager.SECURITY_COLLECTION_URI)) {
try {
broker.getBrokerPool().getSecurityManager().processParameter(broker, document);
} catch (final ConfigurationException e) {
LOG.error("Configuration can't be processed [{}]", document.getURI(), e);
// TODO : raise exception ? -pb
}
}
this.broker = null;
this.createOrUpdate = false;
}
Aggregations