Search in sources :

Example 81 with XmldbURI

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

the class ZipEntryFunctions method eval.

@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
    final XmldbURI uri = ((AnyURIValue) args[0].itemAt(0)).toXmldbURI();
    final String entryName = args[1].itemAt(0).getStringValue();
    final ZipFileSource zipFileSource = new ZipFileFromDb(uri);
    ZipInputStream zis = null;
    boolean mustClose = true;
    Sequence result = Sequence.EMPTY_SEQUENCE;
    try {
        zis = zipFileSource.getStream(context.getBroker());
        ZipEntry zipEntry;
        while ((zipEntry = zis.getNextEntry()) != null) {
            try {
                if (zipEntry.getName().equals(entryName)) {
                    // process
                    if (isCalledAs(BINARY_ENTRY_NAME)) {
                        result = extractBinaryEntry(zis);
                        mustClose = false;
                    } else if (isCalledAs(HTML_ENTRY_NAME)) {
                        result = extractHtmlEntry(zis);
                    } else if (isCalledAs(TEXT_ENTRY_NAME)) {
                        result = extractStringEntry(zis);
                    } else if (isCalledAs(XML_ENTRY_NAME)) {
                        result = extractXmlEntry(zis);
                    }
                    break;
                }
            } finally {
            // DONT need to close as the extract functions
            // close the stream on the zip entry
            /*if(mustClose) {
                        zis.closeEntry();
                    }*/
            }
        }
    } catch (final IOException | PermissionDeniedException ioe) {
        LOG.error(ioe.getMessage(), ioe);
        throw new XPathException(this, ioe.getMessage(), ioe);
    } finally {
        if (zis != null && mustClose) {
            try {
                zis.close();
            } catch (final IOException ioe) {
                LOG.warn(ioe.getMessage(), ioe);
            }
        }
        zipFileSource.close();
    }
    return result;
}
Also used : XPathException(org.exist.xquery.XPathException) AnyURIValue(org.exist.xquery.value.AnyURIValue) ZipEntry(java.util.zip.ZipEntry) Sequence(org.exist.xquery.value.Sequence) IOException(java.io.IOException) ZipInputStream(java.util.zip.ZipInputStream) PermissionDeniedException(org.exist.security.PermissionDeniedException) XmldbURI(org.exist.xmldb.XmldbURI)

Example 82 with XmldbURI

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

the class Sync method pruneCollectionEntries.

private void pruneCollectionEntries(final Collection collection, final String rootTargetAbsPath, final Path targetDir, final List<String> excludes, final MemTreeBuilder output) {
    try (final Stream<Path> fileStream = Files.walk(targetDir, 1)) {
        fileStream.forEach(path -> {
            try {
                // guard against deletion of output folder
                if (rootTargetAbsPath.startsWith(path.toString())) {
                    return;
                }
                if (isExcludedPath(rootTargetAbsPath, path, excludes)) {
                    return;
                }
                final String fileName = path.getFileName().toString();
                final XmldbURI dbname = XmldbURI.xmldbUriFor(fileName);
                final String currentCollection = collection.getURI().getCollectionPath();
                if (collection.hasDocument(context.getBroker(), dbname) || collection.hasChildCollection(context.getBroker(), dbname) || currentCollection.endsWith("/" + fileName)) {
                    return;
                }
                // handle non-empty directories
                if (Files.isDirectory(path)) {
                    deleteWithExcludes(rootTargetAbsPath, path, excludes, output);
                } else {
                    Files.deleteIfExists(path);
                    // reporting
                    output.startElement(FILE_DELETE_ELEMENT, null);
                    output.addAttribute(FILE_ATTRIBUTE, path.toAbsolutePath().toString());
                    output.addAttribute(NAME_ATTRIBUTE, fileName);
                    output.endElement();
                }
            } catch (final IOException | URISyntaxException | PermissionDeniedException | LockException e) {
                reportError(output, e.getMessage());
            }
        });
    } catch (final IOException e) {
        reportError(output, e.getMessage());
    }
}
Also used : Path(java.nio.file.Path) LockException(org.exist.util.LockException) PermissionDeniedException(org.exist.security.PermissionDeniedException) URISyntaxException(java.net.URISyntaxException) XmldbURI(org.exist.xmldb.XmldbURI)

Example 83 with XmldbURI

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

the class Sync method handleCollection.

private List<XmldbURI> handleCollection(final XmldbURI collectionPath, final String rootTargetAbsPath, final Path targetDirectory, final Date startDate, final boolean prune, final List<String> excludes, final MemTreeBuilder output) throws PermissionDeniedException, LockException {
    try (final Collection collection = context.getBroker().openCollection(collectionPath, LockMode.READ_LOCK)) {
        if (collection == null) {
            reportError(output, "Collection not found: " + collectionPath);
            return Collections.emptyList();
        }
        if (prune) {
            pruneCollectionEntries(collection, rootTargetAbsPath, targetDirectory, excludes, output);
        }
        for (final Iterator<DocumentImpl> i = collection.iterator(context.getBroker()); i.hasNext(); ) {
            final DocumentImpl doc = i.next();
            final Path targetFile = targetDirectory.resolve(doc.getFileURI().toASCIIString());
            saveFile(targetFile, doc, startDate, output);
        }
        final List<XmldbURI> subCollections = new ArrayList<>(collection.getChildCollectionCount(context.getBroker()));
        for (final Iterator<XmldbURI> i = collection.collectionIterator(context.getBroker()); i.hasNext(); ) {
            subCollections.add(i.next());
        }
        return subCollections;
    }
}
Also used : Path(java.nio.file.Path) Collection(org.exist.collections.Collection) DocumentImpl(org.exist.dom.persistent.DocumentImpl) XmldbURI(org.exist.xmldb.XmldbURI)

Example 84 with XmldbURI

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

the class Sync method syncCollection.

private void syncCollection(final XmldbURI collectionPath, final String rootTargetAbsPath, final Path targetDir, final Date startDate, final boolean prune, final List<String> excludes, final MemTreeBuilder output) throws PermissionDeniedException, LockException {
    final Path targetDirectory;
    try {
        targetDirectory = Files.createDirectories(targetDir);
    } catch (final IOException ioe) {
        reportError(output, "Failed to create output directory: " + targetDir.toAbsolutePath() + " for collection " + collectionPath);
        return;
    }
    if (!Files.isWritable(targetDirectory)) {
        reportError(output, "Failed to write to output directory: " + targetDirectory.toAbsolutePath());
        return;
    }
    final List<XmldbURI> subCollections = handleCollection(collectionPath, rootTargetAbsPath, targetDirectory, startDate, prune, excludes, output);
    for (final XmldbURI childURI : subCollections) {
        final Path childDir = targetDirectory.resolve(childURI.lastSegment().toString());
        syncCollection(collectionPath.append(childURI), rootTargetAbsPath, childDir, startDate, prune, excludes, output);
    }
}
Also used : Path(java.nio.file.Path) XmldbURI(org.exist.xmldb.XmldbURI)

Example 85 with XmldbURI

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

the class GetThumbnailsFunction method eval.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.exist.xquery.BasicFunction#eval(org.exist.xquery.value.Sequence[],
	 *      org.exist.xquery.value.Sequence)
	 */
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    ValueSequence result = new ValueSequence();
    // boolean isDatabasePath = false;
    boolean isSaveToDataBase = false;
    if (args[0].isEmpty()) {
        return Sequence.EMPTY_SEQUENCE;
    }
    AnyURIValue picturePath = (AnyURIValue) args[0].itemAt(0);
    if (picturePath.getStringValue().startsWith("xmldb:exist://")) {
        picturePath = new AnyURIValue(picturePath.getStringValue().substring(14));
    }
    AnyURIValue thumbPath = null;
    if (args[1].isEmpty()) {
        thumbPath = new AnyURIValue(picturePath.toXmldbURI().append(THUMBPATH));
        isSaveToDataBase = true;
    } else {
        thumbPath = (AnyURIValue) args[1].itemAt(0);
        if (thumbPath.getStringValue().startsWith("file:")) {
            isSaveToDataBase = false;
            thumbPath = new AnyURIValue(thumbPath.getStringValue().substring(5));
        } else {
            isSaveToDataBase = true;
            try {
                XmldbURI thumbsURI = XmldbURI.xmldbUriFor(thumbPath.getStringValue());
                if (!thumbsURI.isAbsolute())
                    thumbsURI = picturePath.toXmldbURI().append(thumbPath.toString());
                thumbPath = new AnyURIValue(thumbsURI.toString());
            } catch (URISyntaxException e) {
                throw new XPathException(this, e.getMessage());
            }
        }
    }
    // result.add(new StringValue(picturePath.getStringValue()));
    // result.add(new StringValue(thumbPath.getStringValue() + " isDB?= "
    // + isSaveToDataBase));
    int maxThumbHeight = MAXTHUMBHEIGHT;
    int maxThumbWidth = MAXTHUMBWIDTH;
    if (!args[2].isEmpty()) {
        maxThumbHeight = ((IntegerValue) args[2].itemAt(0)).getInt();
        if (args[2].hasMany())
            maxThumbWidth = ((IntegerValue) args[2].itemAt(1)).getInt();
    }
    String prefix = THUMBPREFIX;
    if (!args[3].isEmpty()) {
        prefix = args[3].itemAt(0).getStringValue();
    }
    // result.add(new StringValue("maxThumbHeight = " + maxThumbHeight
    // + ", maxThumbWidth = " + maxThumbWidth));
    BrokerPool pool = null;
    try {
        pool = BrokerPool.getInstance();
    } catch (Exception e) {
        result.add(new StringValue(e.getMessage()));
        return result;
    }
    final DBBroker dbbroker = context.getBroker();
    // Start transaction
    final TransactionManager transact = pool.getTransactionManager();
    try (final Txn transaction = transact.beginTransaction()) {
        Collection thumbCollection = null;
        Path thumbDir = null;
        if (isSaveToDataBase) {
            try {
                thumbCollection = dbbroker.getOrCreateCollection(transaction, thumbPath.toXmldbURI());
                dbbroker.saveCollection(transaction, thumbCollection);
            } catch (Exception e) {
                throw new XPathException(this, e.getMessage());
            }
        } else {
            thumbDir = Paths.get(thumbPath.toString());
            if (!Files.isDirectory(thumbDir))
                try {
                    Files.createDirectories(thumbDir);
                } catch (IOException e) {
                    throw new XPathException(this, e.getMessage());
                }
        }
        Collection allPictures = null;
        Collection existingThumbsCol = null;
        List<Path> existingThumbsArray = null;
        try {
            allPictures = dbbroker.getCollection(picturePath.toXmldbURI());
            if (allPictures == null) {
                return Sequence.EMPTY_SEQUENCE;
            }
            if (isSaveToDataBase) {
                existingThumbsCol = dbbroker.getCollection(thumbPath.toXmldbURI());
            } else {
                existingThumbsArray = FileUtils.list(thumbDir, path -> {
                    final String fileName = FileUtils.fileName(path);
                    return fileName.endsWith(".jpeg") || fileName.endsWith(".jpg");
                });
            }
        } catch (PermissionDeniedException | IOException e) {
            throw new XPathException(this, e.getMessage(), e);
        }
        DocumentImpl docImage = null;
        BinaryDocument binImage = null;
        @SuppressWarnings("unused") BufferedImage bImage = null;
        @SuppressWarnings("unused") byte[] imgData = null;
        Image image = null;
        UnsynchronizedByteArrayOutputStream os = null;
        try {
            Iterator<DocumentImpl> i = allPictures.iterator(dbbroker);
            while (i.hasNext()) {
                docImage = i.next();
                // is not already existing??
                if (!((fileExist(context.getBroker(), existingThumbsCol, docImage, prefix)) || (fileExist(existingThumbsArray, docImage, prefix)))) {
                    if (docImage.getResourceType() == DocumentImpl.BINARY_FILE)
                        // TODO maybe extends for gifs too.
                        if (docImage.getMimeType().startsWith("image/jpeg")) {
                            binImage = (BinaryDocument) docImage;
                            try (final InputStream is = dbbroker.getBinaryResource(transaction, binImage)) {
                                image = ImageIO.read(is);
                            } catch (IOException ioe) {
                                throw new XPathException(this, ioe.getMessage());
                            }
                            try {
                                bImage = ImageModule.createThumb(image, maxThumbHeight, maxThumbWidth);
                            } catch (Exception e) {
                                throw new XPathException(this, e.getMessage());
                            }
                            if (isSaveToDataBase) {
                                os = new UnsynchronizedByteArrayOutputStream();
                                try {
                                    ImageIO.write(bImage, "jpg", os);
                                } catch (Exception e) {
                                    throw new XPathException(this, e.getMessage());
                                }
                                try (final StringInputSource sis = new StringInputSource(os.toByteArray())) {
                                    thumbCollection.storeDocument(transaction, dbbroker, XmldbURI.create(prefix + docImage.getFileURI()), sis, new MimeType("image/jpeg", MimeType.BINARY));
                                } catch (final Exception e) {
                                    throw new XPathException(this, e.getMessage());
                                }
                            // result.add(new
                            // StringValue(""+docImage.getFileURI()+"|"+thumbCollection.getURI()+THUMBPREFIX
                            // + docImage.getFileURI()));
                            } else {
                                try {
                                    ImageIO.write(bImage, "jpg", Paths.get(thumbPath.toString() + "/" + prefix + docImage.getFileURI()).toFile());
                                } catch (Exception e) {
                                    throw new XPathException(this, e.getMessage());
                                }
                            // result.add(new StringValue(
                            // thumbPath.toString() + "/"
                            // + THUMBPREFIX
                            // + docImage.getFileURI()));
                            }
                        }
                } else {
                    // result.add(new StringValue(""+docImage.getURI()+"|"
                    // + ((existingThumbsCol != null) ? ""
                    // + existingThumbsCol.getURI() : thumbDir
                    // .toString()) + "/" + prefix
                    // + docImage.getFileURI()));
                    result.add(new StringValue(docImage.getFileURI().toString()));
                }
            }
        } catch (final PermissionDeniedException | LockException e) {
            throw new XPathException(this, e.getMessage(), e);
        }
        try {
            transact.commit(transaction);
        } catch (Exception e) {
            throw new XPathException(this, e.getMessage());
        }
    }
    final Optional<JournalManager> journalManager = pool.getJournalManager();
    journalManager.ifPresent(j -> j.flush(true, false));
    dbbroker.closeDocument();
    return result;
}
Also used : Txn(org.exist.storage.txn.Txn) BrokerPool(org.exist.storage.BrokerPool) SequenceType(org.exist.xquery.value.SequenceType) FunctionParameterSequenceType(org.exist.xquery.value.FunctionParameterSequenceType) QName(org.exist.dom.QName) URISyntaxException(java.net.URISyntaxException) ValueSequence(org.exist.xquery.value.ValueSequence) StringInputSource(org.exist.util.StringInputSource) FunctionSignature(org.exist.xquery.FunctionSignature) PermissionDeniedException(org.exist.security.PermissionDeniedException) Cardinality(org.exist.xquery.Cardinality) MimeType(org.exist.util.MimeType) FileUtils(org.exist.util.FileUtils) BasicFunction(org.exist.xquery.BasicFunction) StringValue(org.exist.xquery.value.StringValue) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) LockException(org.exist.util.LockException) FunctionReturnSequenceType(org.exist.xquery.value.FunctionReturnSequenceType) ImageIO(javax.imageio.ImageIO) Collection(org.exist.collections.Collection) XmldbURI(org.exist.xmldb.XmldbURI) DocumentImpl(org.exist.dom.persistent.DocumentImpl) Path(java.nio.file.Path) XQueryContext(org.exist.xquery.XQueryContext) Iterator(java.util.Iterator) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) Files(java.nio.file.Files) JournalManager(org.exist.storage.journal.JournalManager) AnyURIValue(org.exist.xquery.value.AnyURIValue) Type(org.exist.xquery.value.Type) IOException(java.io.IOException) TransactionManager(org.exist.storage.txn.TransactionManager) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Paths(java.nio.file.Paths) DBBroker(org.exist.storage.DBBroker) IntegerValue(org.exist.xquery.value.IntegerValue) Optional(java.util.Optional) Sequence(org.exist.xquery.value.Sequence) LogManager(org.apache.logging.log4j.LogManager) BinaryDocument(org.exist.dom.persistent.BinaryDocument) XPathException(org.exist.xquery.XPathException) InputStream(java.io.InputStream) XPathException(org.exist.xquery.XPathException) URISyntaxException(java.net.URISyntaxException) Txn(org.exist.storage.txn.Txn) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) DocumentImpl(org.exist.dom.persistent.DocumentImpl) BufferedImage(java.awt.image.BufferedImage) MimeType(org.exist.util.MimeType) StringInputSource(org.exist.util.StringInputSource) LockException(org.exist.util.LockException) ValueSequence(org.exist.xquery.value.ValueSequence) StringValue(org.exist.xquery.value.StringValue) XmldbURI(org.exist.xmldb.XmldbURI) Path(java.nio.file.Path) InputStream(java.io.InputStream) AnyURIValue(org.exist.xquery.value.AnyURIValue) IntegerValue(org.exist.xquery.value.IntegerValue) JournalManager(org.exist.storage.journal.JournalManager) IOException(java.io.IOException) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) URISyntaxException(java.net.URISyntaxException) PermissionDeniedException(org.exist.security.PermissionDeniedException) LockException(org.exist.util.LockException) IOException(java.io.IOException) XPathException(org.exist.xquery.XPathException) BinaryDocument(org.exist.dom.persistent.BinaryDocument) DBBroker(org.exist.storage.DBBroker) TransactionManager(org.exist.storage.txn.TransactionManager) Collection(org.exist.collections.Collection) PermissionDeniedException(org.exist.security.PermissionDeniedException) BrokerPool(org.exist.storage.BrokerPool)

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