Search in sources :

Example 1 with EXistResource

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

the class InteractiveClient method parse.

/**
 * Stores given Resource
 *
 * @param file file or directory
 * @return TRUE if file or files in directory were all correctly stored.
 * @throws XMLDBException An error was detected.
 */
protected synchronized boolean parse(final Path file) throws XMLDBException {
    try {
        Resource document;
        if (current instanceof Observable && options.verbose) {
            final ProgressObserver observer = new ProgressObserver();
            ((Observable) current).addObserver(observer);
        }
        List<Path> files = new ArrayList<>();
        if (Files.isReadable(file)) {
            // TODO, same logic as for the graphic client
            if (Files.isDirectory(file)) {
                if (options.reindexRecurse) {
                    filesCount = 0;
                    final long start = System.currentTimeMillis();
                    final boolean result = findRecursive(current, file, path);
                    messageln("storing " + filesCount + " files took " + ((System.currentTimeMillis() - start) / 1000) + "sec.");
                    return result;
                }
                files = FileUtils.list(file);
            } else {
                files.add(file);
            }
        } else {
            final DirectoryScanner directoryScanner = new DirectoryScanner();
            directoryScanner.setIncludes(new String[] { file.toString() });
            // TODO(AR) do we need to call scanner.setBasedir()?
            directoryScanner.setCaseSensitive(true);
            directoryScanner.scan();
            for (final String includedFile : directoryScanner.getIncludedFiles()) {
                // files.add(baseDir.resolve(includedFile));
                files.add(Paths.get(includedFile));
            }
        }
        final long start0 = System.currentTimeMillis();
        long bytes = 0;
        MimeType mimeType;
        for (int i = 0; i < files.size(); i++) {
            if (Files.isDirectory(files.get(i))) {
                continue;
            }
            final long start = System.currentTimeMillis();
            mimeType = MimeTable.getInstance().getContentTypeFor(FileUtils.fileName(files.get(i)));
            if (mimeType == null) {
                mimeType = MimeType.BINARY_TYPE;
            }
            document = current.createResource(FileUtils.fileName(files.get(i)), mimeType.getXMLDBType());
            message("storing document " + FileUtils.fileName(files.get(i)) + " (" + (i + 1) + " of " + files.size() + ") ...");
            document.setContent(files.get(i));
            ((EXistResource) document).setMimeType(mimeType.getName());
            current.storeResource(document);
            messageln("done.");
            messageln("parsing " + FileUtils.sizeQuietly(files.get(i)) + " bytes took " + (System.currentTimeMillis() - start) + "ms." + EOL);
            bytes += FileUtils.sizeQuietly(files.get(i));
        }
        messageln("parsed " + bytes + " bytes in " + (System.currentTimeMillis() - start0) + "ms.");
        return true;
    } catch (final IOException e) {
        e.printStackTrace();
        throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR, e);
    }
}
Also used : Path(java.nio.file.Path) ExtendedResource(org.exist.xmldb.ExtendedResource) BinaryResource(org.xmldb.api.modules.BinaryResource) EXistResource(org.exist.xmldb.EXistResource) EXistResource(org.exist.xmldb.EXistResource) DirectoryScanner(org.apache.tools.ant.DirectoryScanner)

Example 2 with EXistResource

use of org.exist.xmldb.EXistResource 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 3 with EXistResource

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

the class InteractiveClient method parseZip.

/**
 * stores given Resource.
 *
 * @param zipPath Path to a zip file
 *
 * @throws XMLDBException in case of error writing to the database
 * @return true if operation succeeded
 */
protected synchronized boolean parseZip(final Path zipPath) throws XMLDBException {
    try {
        final ZipFile zfile = new ZipFile(zipPath.toFile());
        if (current instanceof Observable && options.verbose) {
            final ProgressObserver observer = new ProgressObserver();
            ((Observable) current).addObserver(observer);
        }
        final long start0 = System.currentTimeMillis();
        long bytes = 0;
        final Enumeration<? extends ZipEntry> e = zfile.entries();
        int number = 0;
        Collection base = current;
        String baseStr = "";
        while (e.hasMoreElements()) {
            number++;
            final ZipEntry ze = e.nextElement();
            final String zeName = ze.getName().replace('\\', '/');
            if (!Paths.get("/db").resolve(zeName).normalize().startsWith(Paths.get("/db"))) {
                throw new IOException("Detected archive exit attack! zipFile=" + zipPath.toAbsolutePath().toString() + ", entry=" + ze.getName());
            }
            final String[] pathSteps = zeName.split("/");
            final StringBuilder currStr = new StringBuilder(pathSteps[0]);
            for (int i = 1; i < pathSteps.length - 1; i++) {
                currStr.append("/").append(pathSteps[i]);
            }
            if (!baseStr.equals(currStr)) {
                base = current;
                for (int i = 0; i < pathSteps.length - 1; i++) {
                    Collection c = base.getChildCollection(pathSteps[i]);
                    if (c == null) {
                        final EXistCollectionManagementService mgtService = (EXistCollectionManagementService) base.getService("CollectionManagementService", "1.0");
                        c = mgtService.createCollection(XmldbURI.xmldbUriFor(pathSteps[i]));
                    }
                    base = c;
                }
                if (base instanceof Observable && options.verbose) {
                    final ProgressObserver observer = new ProgressObserver();
                    ((Observable) base).addObserver(observer);
                }
                baseStr = currStr.toString();
                messageln("entering directory " + baseStr);
            }
            if (!ze.isDirectory()) {
                final String localName = pathSteps[pathSteps.length - 1];
                final long start = System.currentTimeMillis();
                MimeType mimeType = MimeTable.getInstance().getContentTypeFor(localName);
                if (mimeType == null) {
                    mimeType = MimeType.BINARY_TYPE;
                }
                final Resource document = base.createResource(localName, mimeType.getXMLDBType());
                message("storing Zip-entry document " + localName + " (" + (number) + " of " + zfile.size() + ") ...");
                document.setContent(new ZipEntryInputSource(zfile, ze));
                ((EXistResource) document).setMimeType(mimeType.getName());
                base.storeResource(document);
                messageln("done.");
                messageln("parsing " + ze.getSize() + " bytes took " + (System.currentTimeMillis() - start) + "ms." + EOL);
                bytes += ze.getSize();
            }
        }
        messageln("parsed " + bytes + " bytes in " + (System.currentTimeMillis() - start0) + "ms.");
    } catch (final URISyntaxException e) {
        errorln("uri syntax exception parsing a ZIP entry from " + zipPath.toString() + ": " + e.getMessage());
    } catch (final IOException e) {
        errorln("could not parse ZIP file " + zipPath.toAbsolutePath() + ": " + e.getMessage());
    }
    return true;
}
Also used : EXistCollectionManagementService(org.exist.xmldb.EXistCollectionManagementService) ZipEntry(java.util.zip.ZipEntry) 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) ZipFile(java.util.zip.ZipFile) Collection(org.xmldb.api.base.Collection)

Example 4 with EXistResource

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

the class InteractiveClient method storeBinary.

private void storeBinary(final String fileName) throws XMLDBException {
    final Path file = Paths.get(fileName).normalize();
    if (Files.isReadable(file)) {
        final MimeType mime = MimeTable.getInstance().getContentTypeFor(FileUtils.fileName(file));
        final BinaryResource resource = (BinaryResource) current.createResource(FileUtils.fileName(file), BinaryResource.RESOURCE_TYPE);
        resource.setContent(file);
        ((EXistResource) resource).setMimeType(mime == null ? "application/octet-stream" : mime.getName());
        current.storeResource(resource);
    }
}
Also used : Path(java.nio.file.Path) EXistResource(org.exist.xmldb.EXistResource) BinaryResource(org.xmldb.api.modules.BinaryResource)

Example 5 with EXistResource

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

the class InteractiveClient method findGZipRecursive.

private synchronized boolean findGZipRecursive(final Collection collection, final Path dir, final XmldbURI base) throws XMLDBException, IOException {
    final List<Path> files = FileUtils.list(dir);
    Collection c;
    Resource document;
    EXistCollectionManagementService mgtService;
    // The XmldbURIs here aren't really used...
    XmldbURI next;
    MimeType mimeType;
    int i = 0;
    for (final Path file : files) {
        i++;
        next = base.append(FileUtils.fileName(file));
        try {
            if (Files.isDirectory(file)) {
                messageln("entering directory " + file.toAbsolutePath().toString());
                c = collection.getChildCollection(FileUtils.fileName(file));
                if (c == null) {
                    mgtService = (EXistCollectionManagementService) collection.getService("CollectionManagementService", "1.0");
                    c = mgtService.createCollection(XmldbURI.xmldbUriFor(FileUtils.fileName(file)));
                }
                if (c instanceof Observable && options.verbose) {
                    final ProgressObserver observer = new ProgressObserver();
                    ((Observable) c).addObserver(observer);
                }
                findGZipRecursive(c, file, next);
            } else {
                final long start1 = System.currentTimeMillis();
                final String compressedName = FileUtils.fileName(file);
                String localName = compressedName;
                final String[] cSuffix = { ".gz", ".Z" };
                boolean isCompressed = false;
                for (final String suf : cSuffix) {
                    if (localName.endsWith(suf)) {
                        // Removing compressed prefix to validate
                        localName = compressedName.substring(0, localName.length() - suf.length());
                        isCompressed = true;
                        break;
                    }
                }
                mimeType = MimeTable.getInstance().getContentTypeFor(localName);
                if (mimeType == null) {
                    messageln("File " + compressedName + " has an unknown suffix. Cannot determine file type.");
                    mimeType = MimeType.BINARY_TYPE;
                }
                message("storing document " + compressedName + " (" + i + " of " + files.size() + ") " + "...");
                document = collection.createResource(compressedName, mimeType.getXMLDBType());
                document.setContent(isCompressed ? new GZIPInputSource(file) : file);
                ((EXistResource) document).setMimeType(mimeType.getName());
                collection.storeResource(document);
                ++filesCount;
                messageln(" " + Files.size(file) + (isCompressed ? " compressed" : "") + " bytes in " + (System.currentTimeMillis() - start1) + "ms.");
            }
        } catch (final URISyntaxException e) {
            errorln("uri syntax exception parsing " + file.toAbsolutePath().toString() + ": " + e.getMessage());
        }
    }
    return true;
}
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)

Aggregations

EXistResource (org.exist.xmldb.EXistResource)42 Collection (org.xmldb.api.base.Collection)20 BinaryResource (org.xmldb.api.modules.BinaryResource)20 Resource (org.xmldb.api.base.Resource)17 XMLResource (org.xmldb.api.modules.XMLResource)17 ResourceSet (org.xmldb.api.base.ResourceSet)12 Path (java.nio.file.Path)11 XMLDBException (org.xmldb.api.base.XMLDBException)10 ExtendedResource (org.exist.xmldb.ExtendedResource)9 EXistXPathQueryService (org.exist.xmldb.EXistXPathQueryService)8 URISyntaxException (java.net.URISyntaxException)6 XPathQueryService (org.xmldb.api.modules.XPathQueryService)6 EXistCollectionManagementService (org.exist.xmldb.EXistCollectionManagementService)5 UserManagementService (org.exist.xmldb.UserManagementService)5 BeforeClass (org.junit.BeforeClass)5 Test (org.junit.Test)5 DirectoryScanner (org.apache.tools.ant.DirectoryScanner)4 MimeType (org.exist.util.MimeType)4 XPathException (org.exist.xquery.XPathException)4 LocalBinaryResource (org.exist.xmldb.LocalBinaryResource)3