Search in sources :

Example 1 with XmldbURI

use of org.exist.xmldb.XmldbURI in project exist by eXist-db.

the class InteractiveClient method store.

/**
 * Pass to this method a java file object
 * (may be a file or a directory), GUI object
 * will create relative collections or resources
 * recursively
 */
private void store(final Collection collection, final Path file, final UploadDialog upload) {
    // cancel, stop crawl
    if (upload.isCancelled()) {
        return;
    }
    // can't read there, inform client
    if (!Files.isReadable(file)) {
        upload.showMessage(file.toAbsolutePath() + " impossible to read ");
        return;
    }
    final XmldbURI filenameUri;
    try {
        filenameUri = XmldbURI.xmldbUriFor(FileUtils.fileName(file));
    } catch (final URISyntaxException e1) {
        upload.showMessage(file.toAbsolutePath() + " could not be encoded as a URI");
        return;
    }
    // Directory, create collection, and crawl it
    if (Files.isDirectory(file)) {
        Collection c = null;
        try {
            c = collection.getChildCollection(filenameUri.toString());
            if (c == null) {
                final EXistCollectionManagementService mgtService = (EXistCollectionManagementService) collection.getService("CollectionManagementService", "1.0");
                c = mgtService.createCollection(filenameUri);
            }
        } catch (final XMLDBException e) {
            upload.showMessage("Impossible to create a collection " + file.toAbsolutePath() + ": " + e.getMessage());
            e.printStackTrace();
        }
        // change displayed collection if it's OK
        upload.setCurrentDir(file.toAbsolutePath().toString());
        if (c instanceof Observable) {
            ((Observable) c).addObserver(upload.getObserver());
        }
        // maybe a depth or recurs flag could be added here
        final Collection childCollection = c;
        try (final Stream<Path> children = Files.list(file)) {
            children.forEach(child -> store(childCollection, child, upload));
        } catch (final IOException e) {
            upload.showMessage("Impossible to upload " + file.toAbsolutePath() + ": " + e.getMessage());
            e.printStackTrace();
        }
        return;
    }
    // File, create and store resource
    if (!Files.isDirectory(file)) {
        upload.reset();
        upload.setCurrent(FileUtils.fileName(file));
        final long fileSize = FileUtils.sizeQuietly(file);
        upload.setCurrentSize(fileSize);
        MimeType mimeType = MimeTable.getInstance().getContentTypeFor(FileUtils.fileName(file));
        // unknown mime type, here prefered is to do nothing
        if (mimeType == null) {
            upload.showMessage(file.toAbsolutePath() + " - unknown suffix. No matching mime-type found in : " + MimeTable.getInstance().getSrc());
            // if some one prefers to store it as binary by default, but dangerous
            mimeType = MimeType.BINARY_TYPE;
        }
        try {
            final Resource res = collection.createResource(filenameUri.toString(), mimeType.getXMLDBType());
            ((EXistResource) res).setMimeType(mimeType.getName());
            res.setContent(file);
            collection.storeResource(res);
            ++filesCount;
            this.totalLength += fileSize;
            upload.setStoredSize(this.totalLength);
        } catch (final XMLDBException e) {
            upload.showMessage("Impossible to store a resource " + file.toAbsolutePath() + ": " + e.getMessage());
        }
    }
}
Also used : Path(java.nio.file.Path) EXistCollectionManagementService(org.exist.xmldb.EXistCollectionManagementService) ExtendedResource(org.exist.xmldb.ExtendedResource) BinaryResource(org.xmldb.api.modules.BinaryResource) EXistResource(org.exist.xmldb.EXistResource) URISyntaxException(java.net.URISyntaxException) EXistResource(org.exist.xmldb.EXistResource) Collection(org.xmldb.api.base.Collection) XmldbURI(org.exist.xmldb.XmldbURI)

Example 2 with XmldbURI

use of org.exist.xmldb.XmldbURI in project exist by eXist-db.

the class MutableCollection method serialize.

/**
 * Serializes the Collection to a byte representation
 *
 * Counterpart method to {@link #deserialize(DBBroker, XmldbURI, VariableByteInput)}
 *
 * @param outputStream The output stream to write the collection contents to
 */
@Override
public void serialize(final VariableByteOutputStream outputStream) throws IOException, LockException {
    outputStream.writeInt(collectionId);
    final int size;
    final Iterator<XmldbURI> i;
    // TODO(AR) should we READ_LOCK the Collection to stop it being modified concurrently? see NativeBroker#saveCollection line 1801 - already has WRITE_LOCK ;-)
    // try(final ManagedCollectionLock collectionLock = lockManager.acquireCollectionReadLock(path)) {
    size = subCollections.size();
    // i = subCollections.stableIterator();
    i = subCollections.iterator();
    // }
    outputStream.writeInt(size);
    while (i.hasNext()) {
        final XmldbURI childCollectionURI = i.next();
        outputStream.writeUTF(childCollectionURI.toString());
    }
    permissions.write(outputStream);
    outputStream.writeLong(created);
}
Also used : XmldbURI(org.exist.xmldb.XmldbURI)

Example 3 with XmldbURI

use of org.exist.xmldb.XmldbURI in project exist by eXist-db.

the class MutableCollection method storeDocument.

@Override
public void storeDocument(final Txn transaction, final DBBroker broker, final XmldbURI name, final InputSource source, @Nullable MimeType mimeType, @Nullable final Date createdDate, @Nullable final Date lastModifiedDate, @Nullable final Permission permission, @Nullable final DocumentType documentType, @Nullable final XMLReader xmlReader) throws EXistException, PermissionDeniedException, SAXException, LockException, IOException {
    if (mimeType == null) {
        mimeType = MimeType.BINARY_TYPE;
    }
    if (mimeType.isXMLType()) {
        // Store XML Document
        final BiConsumer2E<XMLReader, IndexInfo, SAXException, EXistException> validatorFn = (xmlReader1, validateIndexInfo) -> {
            validateIndexInfo.setReader(xmlReader1, null);
            try {
                xmlReader1.parse(source);
            } catch (final SAXException e) {
                throw new SAXException("The XML parser reported a problem: " + e.getMessage(), e);
            } catch (final IOException e) {
                throw new EXistException(e);
            }
        };
        final BiConsumer2E<XMLReader, IndexInfo, SAXException, EXistException> parserFn = (xmlReader1, storeIndexInfo) -> {
            try {
                storeIndexInfo.setReader(xmlReader1, null);
                xmlReader1.parse(source);
            } catch (final IOException e) {
                throw new EXistException(e);
            }
        };
        storeXmlDocument(transaction, broker, name, mimeType, createdDate, lastModifiedDate, permission, documentType, xmlReader, validatorFn, parserFn);
    } else {
        // Store Binary Document
        try (final InputStream is = source.getByteStream()) {
            if (is == null) {
                throw new IOException("storeDocument received a null InputStream when trying to store a Binary Document");
            }
            addBinaryResource(transaction, broker, name, is, mimeType.getName(), -1, createdDate, lastModifiedDate, permission);
        }
    }
}
Also used : CloseShieldReader(org.apache.commons.io.input.CloseShieldReader) java.util(java.util) LockMode(org.exist.storage.lock.Lock.LockMode) Txn(org.exist.storage.txn.Txn) Consumer2E(com.evolvedbinary.j8fu.function.Consumer2E) QName(org.exist.dom.QName) PermissionDeniedException(org.exist.security.PermissionDeniedException) org.exist.dom.persistent(org.exist.dom.persistent) Constants(org.exist.xquery.Constants) VariableByteOutputStream(org.exist.storage.io.VariableByteOutputStream) VALIDATION_SETTING(org.exist.util.XMLReaderObjectFactory.VALIDATION_SETTING) MimeType(org.exist.util.MimeType) Account(org.exist.security.Account) XMLReader(org.xml.sax.XMLReader) org.exist.storage(org.exist.storage) IndexController(org.exist.indexing.IndexController) CloseShieldInputStream(org.apache.commons.io.input.CloseShieldInputStream) LockException(org.exist.util.LockException) Subject(org.exist.security.Subject) VariableByteInput(org.exist.storage.io.VariableByteInput) Node(org.w3c.dom.Node) XmldbURI(org.exist.xmldb.XmldbURI) LockType(org.exist.storage.lock.Lock.LockType) BiConsumer2E(com.evolvedbinary.j8fu.function.BiConsumer2E) EXistException(org.exist.EXistException) Indexer(org.exist.Indexer) Permission(org.exist.security.Permission) Nullable(javax.annotation.Nullable) PermissionFactory(org.exist.security.PermissionFactory) InputSource(org.xml.sax.InputSource) Database(org.exist.Database) XMLReaderObjectFactory(org.exist.util.XMLReaderObjectFactory) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) org.exist.storage.lock(org.exist.storage.lock) DOMStreamer(org.exist.util.serializer.DOMStreamer) Sync(org.exist.storage.sync.Sync) NotThreadSafe(net.jcip.annotations.NotThreadSafe) org.exist.collections.triggers(org.exist.collections.triggers) DocumentType(org.w3c.dom.DocumentType) StreamListener(org.exist.indexing.StreamListener) Logger(org.apache.logging.log4j.Logger) java.io(java.io) SAXException(org.xml.sax.SAXException) GuardedBy(net.jcip.annotations.GuardedBy) Configuration(org.exist.util.Configuration) LogManager(org.apache.logging.log4j.LogManager) CloseShieldInputStream(org.apache.commons.io.input.CloseShieldInputStream) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) EXistException(org.exist.EXistException) XMLReader(org.xml.sax.XMLReader) SAXException(org.xml.sax.SAXException)

Example 4 with XmldbURI

use of org.exist.xmldb.XmldbURI in project exist by eXist-db.

the class MutableCollection method deserialize.

/**
 * Read collection contents from the stream
 *
 * Counterpart method to {@link #serialize(VariableByteOutputStream)}
 *
 * @param broker The database broker
 * @param path The path of the Collection
 * @param istream The input stream to deserialize the Collection from
 */
private static MutableCollection deserialize(final DBBroker broker, final XmldbURI path, final VariableByteInput istream) throws IOException, PermissionDeniedException, LockException {
    final int collectionId = istream.readInt();
    if (collectionId < 0) {
        throw new IOException("Internal error reading collection: invalid collection id");
    }
    final int collLen = istream.readInt();
    // TODO(AR) should we WRITE_LOCK the Collection to stop it being loaded from disk concurrently? see NativeBroker#openCollection line 1030 - already has READ_LOCK ;-)
    // try(final ManagedCollectionLock collectionLock = lockManager.acquireCollectionWriteLock(path, false)) {
    final LinkedHashSet<XmldbURI> subCollections = new LinkedHashSet<>(Math.max(16, collLen));
    for (int i = 0; i < collLen; i++) {
        subCollections.add(XmldbURI.create(istream.readUTF()));
    }
    final Permission permission = PermissionFactory.getDefaultCollectionPermission(broker.getBrokerPool().getSecurityManager());
    permission.read(istream);
    if (!permission.validate(broker.getCurrentSubject(), Permission.EXECUTE)) {
        throw new PermissionDeniedException("Permission denied to open the Collection " + path);
    }
    final long created = istream.readLong();
    final LinkedHashMap<String, DocumentImpl> documents = new LinkedHashMap<>();
    final MutableCollection collection = new MutableCollection(broker, collectionId, path, permission, created, subCollections, documents);
    broker.getCollectionResources(new InternalAccess() {

        @Override
        public void addDocument(final DocumentImpl doc) throws EXistException {
            doc.setCollection(collection);
            if (doc.getDocId() == DocumentImpl.UNKNOWN_DOCUMENT_ID) {
                LOG.error("Document must have ID. [{}]", doc);
                throw new EXistException("Document must have ID.");
            }
            documents.put(doc.getFileURI().lastSegmentString(), doc);
        }

        @Override
        public int getId() {
            return collectionId;
        }
    });
    return collection;
// }
}
Also used : EXistException(org.exist.EXistException) Permission(org.exist.security.Permission) PermissionDeniedException(org.exist.security.PermissionDeniedException) XmldbURI(org.exist.xmldb.XmldbURI)

Example 5 with XmldbURI

use of org.exist.xmldb.XmldbURI in project exist by eXist-db.

the class MutableCollection method setPath.

@Override
public final void setPath(XmldbURI path, final boolean updateChildren) {
    path = path.toCollectionPathURI();
    // TODO : see if the URI resolves against DBBroker.TEMP_COLLECTION
    this.isTempCollection = path.getRawCollectionPath().equals(XmldbURI.TEMP_COLLECTION);
    this.path = path;
    if (updateChildren) {
        for (final Map.Entry<String, DocumentImpl> docEntry : documents.entrySet()) {
            final XmldbURI docUri = path.append(docEntry.getKey());
            try (final ManagedDocumentLock documentLock = lockManager.acquireDocumentWriteLock(docUri)) {
                final DocumentImpl doc = docEntry.getValue();
                // this will invalidate the cached `uri` in DocumentImpl
                doc.setCollection(this);
            } catch (final LockException e) {
                LOG.error(e.getMessage(), e);
                throw new IllegalStateException(e);
            }
        }
    }
}
Also used : LockException(org.exist.util.LockException) XmldbURI(org.exist.xmldb.XmldbURI)

Aggregations

XmldbURI (org.exist.xmldb.XmldbURI)260 Collection (org.exist.collections.Collection)100 PermissionDeniedException (org.exist.security.PermissionDeniedException)69 Test (org.junit.Test)56 Txn (org.exist.storage.txn.Txn)55 EXistException (org.exist.EXistException)42 URISyntaxException (java.net.URISyntaxException)39 LockedDocument (org.exist.dom.persistent.LockedDocument)39 IOException (java.io.IOException)38 DBBroker (org.exist.storage.DBBroker)38 DocumentImpl (org.exist.dom.persistent.DocumentImpl)34 SAXException (org.xml.sax.SAXException)33 Permission (org.exist.security.Permission)30 LockException (org.exist.util.LockException)27 Path (java.nio.file.Path)22 XPathException (org.exist.xquery.XPathException)22 BrokerPool (org.exist.storage.BrokerPool)21 TransactionManager (org.exist.storage.txn.TransactionManager)20 Subject (org.exist.security.Subject)19 StringInputSource (org.exist.util.StringInputSource)17