Search in sources :

Example 1 with TerminatedException

use of org.exist.xquery.TerminatedException in project exist by eXist-db.

the class SystemExport method exportDocument.

private void exportDocument(final BackupHandler bh, final BackupWriter output, final Date date, final BackupDescriptor prevBackup, final SAXSerializer serializer, final int docsCount, final int count, final DocumentImpl doc) throws IOException, SAXException, TerminatedException {
    if (callback != null) {
        callback.startDocument(doc.getFileURI().toString(), count, docsCount);
    }
    if ((monitor != null) && !monitor.proceed()) {
        throw (new TerminatedException("system export terminated by db"));
    }
    final boolean needsBackup = (prevBackup == null) || (date.getTime() < doc.getLastModified());
    if (needsBackup) {
        // Note: do not auto-close the output stream or the zip will be closed!
        try {
            final OutputStream os = output.newEntry(Backup.encode(URIUtils.urlDecodeUtf8(doc.getFileURI())));
            if (doc.getResourceType() == DocumentImpl.BINARY_FILE) {
                broker.readBinaryResource((BinaryDocument) doc, os);
            } else {
                final SAXSerializer contentSerializer = (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);
                final Writer writer = new BufferedWriter(new OutputStreamWriter(os, UTF_8));
                try {
                    // write resource to contentSerializer
                    contentSerializer.setOutput(writer, defaultOutputProperties);
                    final Receiver receiver;
                    if (chainFactory != null) {
                        chainFactory.getLast().setNextInChain(contentSerializer);
                        receiver = chainFactory.getFirst();
                    } else {
                        receiver = contentSerializer;
                    }
                    writeXML(doc, receiver);
                } finally {
                    SerializerPool.getInstance().returnObject(contentSerializer);
                    writer.flush();
                }
            }
        } catch (final Exception e) {
            reportError("A write error occurred while exporting document: '" + doc.getFileURI() + "'. Continuing with next document.", e);
            return;
        } finally {
            output.closeEntry();
        }
    }
    final Permission perms = doc.getPermissions();
    // store permissions
    final AttributesImpl attr = new AttributesImpl();
    attr.addAttribute(Namespaces.EXIST_NS, "type", "type", "CDATA", (doc.getResourceType() == DocumentImpl.BINARY_FILE) ? "BinaryResource" : "XMLResource");
    attr.addAttribute(Namespaces.EXIST_NS, "name", "name", "CDATA", doc.getFileURI().toString());
    attr.addAttribute(Namespaces.EXIST_NS, "skip", "skip", "CDATA", (needsBackup ? "no" : "yes"));
    Backup.writeUnixStylePermissionAttributes(attr, perms);
    try {
        final String created = new DateTimeValue(new Date(doc.getCreated())).getStringValue();
        final String modified = new DateTimeValue(new Date(doc.getLastModified())).getStringValue();
        attr.addAttribute(Namespaces.EXIST_NS, "created", "created", "CDATA", created);
        attr.addAttribute(Namespaces.EXIST_NS, "modified", "modified", "CDATA", modified);
    } catch (final XPathException e) {
        LOG.warn(e.getMessage(), e);
    }
    attr.addAttribute(Namespaces.EXIST_NS, "filename", "filename", "CDATA", Backup.encode(URIUtils.urlDecodeUtf8(doc.getFileURI())));
    String mimeType = "application/xml";
    if (doc.getMimeType() != null) {
        mimeType = Backup.encode(doc.getMimeType());
    }
    attr.addAttribute(Namespaces.EXIST_NS, "mimetype", "mimetype", "CDATA", mimeType);
    // output by serializer
    // if( ( doc.getResourceType() == DocumentImpl.XML_FILE ) && ( metadata != null ) && ( doc.getDoctype() != null ) ) {
    // 
    // if( doc.getDoctype().getName() != null ) {
    // attr.addAttribute( Namespaces.EXIST_NS, "namedoctype", "namedoctype", "CDATA", doc.getDoctype().getName() );
    // }
    // 
    // if( doc.getDoctype().getPublicId() != null ) {
    // attr.addAttribute( Namespaces.EXIST_NS, "publicid", "publicid", "CDATA", doc.getDoctype().getPublicId() );
    // }
    // 
    // if( doc.getDoctype().getSystemId() != null ) {
    // attr.addAttribute( Namespaces.EXIST_NS, "systemid", "systemid", "CDATA", doc.getDoctype().getSystemId() );
    // }
    // }
    bh.backup(doc, attr);
    serializer.startElement(Namespaces.EXIST_NS, "resource", "resource", attr);
    if (perms instanceof ACLPermission) {
        Backup.writeACLPermission(serializer, (ACLPermission) perms);
    }
    bh.backup(doc, serializer);
    serializer.endElement(Namespaces.EXIST_NS, "resource", "resource");
}
Also used : DateTimeValue(org.exist.xquery.value.DateTimeValue) XPathException(org.exist.xquery.XPathException) ACLPermission(org.exist.security.ACLPermission) Receiver(org.exist.util.serializer.Receiver) PermissionDeniedException(org.exist.security.PermissionDeniedException) LockException(org.exist.util.LockException) TerminatedException(org.exist.xquery.TerminatedException) XMLStreamException(javax.xml.stream.XMLStreamException) SAXException(org.xml.sax.SAXException) XPathException(org.exist.xquery.XPathException) AttributesImpl(org.xml.sax.helpers.AttributesImpl) ACLPermission(org.exist.security.ACLPermission) Permission(org.exist.security.Permission) SAXSerializer(org.exist.util.serializer.SAXSerializer) TerminatedException(org.exist.xquery.TerminatedException)

Example 2 with TerminatedException

use of org.exist.xquery.TerminatedException in project exist by eXist-db.

the class SystemExport method export.

/**
 * Export a collection. Write out the collection metadata and save the resources stored in the collection.
 *
 * @param current    the collection
 * @param output     the output writer
 * @param date
 * @param prevBackup DOCUMENT ME!
 * @param errorList  a list of {@link org.exist.backup.ErrorReport} objects as returned by methods in {@link org.exist.backup.ConsistencyCheck}
 * @param docs       a document set to keep track of all written documents.
 * @throws IOException
 * @throws SAXException
 * @throws TerminatedException DOCUMENT ME!
 */
private void export(final BackupHandler bh, final Collection current, final BackupWriter output, final Date date, final BackupDescriptor prevBackup, final List<ErrorReport> errorList, final MutableDocumentSet docs) throws IOException, SAXException, TerminatedException, PermissionDeniedException {
    if ((monitor != null) && !monitor.proceed()) {
        throw (new TerminatedException("system export terminated by db"));
    }
    // if( !current.getURI().equalsInternal( XmldbURI.ROOT_COLLECTION_URI ) ) {
    output.newCollection(Backup.encode(URIUtils.urlDecodeUtf8(current.getURI())));
    // }
    final SAXSerializer serializer = (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);
    try {
        final Writer contents = output.newContents();
        // serializer writes to __contents__.xml
        serializer.setOutput(contents, contentsOutputProps);
        final Permission perm = current.getPermissionsNoLock();
        serializer.startDocument();
        serializer.startPrefixMapping("", Namespaces.EXIST_NS);
        final XmldbURI uri = current.getURI();
        final AttributesImpl attr = new AttributesImpl();
        attr.addAttribute(Namespaces.EXIST_NS, "name", "name", "CDATA", uri.toString());
        attr.addAttribute(Namespaces.EXIST_NS, "version", "version", "CDATA", String.valueOf(currVersion));
        Backup.writeUnixStylePermissionAttributes(attr, perm);
        try {
            attr.addAttribute(Namespaces.EXIST_NS, "created", "created", "CDATA", new DateTimeValue(new Date(current.getCreated())).getStringValue());
        } catch (final XPathException e) {
            e.printStackTrace();
        }
        bh.backup(current, attr);
        serializer.startElement(Namespaces.EXIST_NS, "collection", "collection", attr);
        if (perm instanceof ACLPermission) {
            Backup.writeACLPermission(serializer, (ACLPermission) perm);
        }
        bh.backup(current, serializer);
        final int docsCount = current.getDocumentCountNoLock(broker);
        int count = 0;
        for (final Iterator<DocumentImpl> i = current.iteratorNoLock(broker); i.hasNext(); count++) {
            final DocumentImpl doc = i.next();
            if (isDamaged(doc, errorList)) {
                reportError("Skipping damaged document " + doc.getFileURI(), null);
                continue;
            }
            if (doc.getFileURI().equalsInternal(CONTENTS_URI) || doc.getFileURI().equalsInternal(LOST_URI)) {
                // skip __contents__.xml documents
                continue;
            }
            exportDocument(bh, output, date, prevBackup, serializer, docsCount, count, doc);
            docs.add(doc, false);
        }
        for (final Iterator<XmldbURI> i = current.collectionIteratorNoLock(broker); i.hasNext(); ) {
            final XmldbURI childUri = i.next();
            if (childUri.equalsInternal(TEMP_COLLECTION)) {
                continue;
            }
            if (isDamagedChild(childUri, errorList)) {
                reportError("Skipping damaged child collection " + childUri, null);
                continue;
            }
            attr.clear();
            attr.addAttribute(Namespaces.EXIST_NS, "name", "name", "CDATA", childUri.toString());
            attr.addAttribute(Namespaces.EXIST_NS, "filename", "filename", "CDATA", Backup.encode(URIUtils.urlDecodeUtf8(childUri.toString())));
            serializer.startElement(Namespaces.EXIST_NS, "subcollection", "subcollection", attr);
            serializer.endElement(Namespaces.EXIST_NS, "subcollection", "subcollection");
        }
        if (prevBackup != null) {
            // Check which collections and resources have been deleted since
            // the
            // last backup
            final CheckDeletedHandler check = new CheckDeletedHandler(current, serializer);
            try {
                prevBackup.parse(broker.getBrokerPool().getParserPool(), check);
            } catch (final Exception e) {
                LOG.error("Caught exception while trying to parse previous backup descriptor: {}", prevBackup.getSymbolicPath(), e);
            }
        }
        // close <collection>
        serializer.endElement(Namespaces.EXIST_NS, "collection", "collection");
        serializer.endPrefixMapping("");
        serializer.endDocument();
        output.closeContents();
    } finally {
        SerializerPool.getInstance().returnObject(serializer);
        // if( !current.getURI().equalsInternal( XmldbURI.ROOT_COLLECTION_URI ) ) {
        output.closeCollection();
    // }
    }
}
Also used : DateTimeValue(org.exist.xquery.value.DateTimeValue) XPathException(org.exist.xquery.XPathException) ACLPermission(org.exist.security.ACLPermission) PermissionDeniedException(org.exist.security.PermissionDeniedException) LockException(org.exist.util.LockException) TerminatedException(org.exist.xquery.TerminatedException) XMLStreamException(javax.xml.stream.XMLStreamException) SAXException(org.xml.sax.SAXException) XPathException(org.exist.xquery.XPathException) AttributesImpl(org.xml.sax.helpers.AttributesImpl) ACLPermission(org.exist.security.ACLPermission) Permission(org.exist.security.Permission) SAXSerializer(org.exist.util.serializer.SAXSerializer) TerminatedException(org.exist.xquery.TerminatedException) XmldbURI(org.exist.xmldb.XmldbURI)

Example 3 with TerminatedException

use of org.exist.xquery.TerminatedException in project exist by eXist-db.

the class ExportMain method process.

private static void process(final ParsedArguments arguments) {
    final boolean verbose = getBool(arguments, verboseArg);
    final boolean noCheck = getBool(arguments, noCheckArg);
    final boolean checkDocs = getBool(arguments, checkDocsArg);
    final boolean direct = getBool(arguments, directAccessArg);
    boolean export = getBool(arguments, exportArg);
    final boolean noExport = getBool(arguments, noExportArg);
    if (noExport) {
        export = false;
    }
    final boolean incremental = getBool(arguments, incrementalArg);
    boolean zip = getBool(arguments, zipArg);
    final boolean noZip = getBool(arguments, noZipArg);
    if (noZip) {
        zip = false;
    }
    final Optional<Path> dbConfig = getOpt(arguments, configArg).map(File::toPath);
    final Path exportTarget = arguments.get(outputDirArg).toPath();
    final BrokerPool pool = startDB(dbConfig);
    if (pool == null) {
        System.exit(SystemExitCodes.CATCH_ALL_GENERAL_ERROR_EXIT_CODE);
    }
    // return value
    int retval = 0;
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final Txn transaction = pool.getTransactionManager().beginTransaction()) {
        List<ErrorReport> errors = null;
        if (!noCheck) {
            final ConsistencyCheck checker = new ConsistencyCheck(broker, transaction, direct, checkDocs);
            errors = checker.checkAll(new CheckCallback());
        }
        if (errors != null && !errors.isEmpty()) {
            System.err.println("ERRORS FOUND.");
            retval = 1;
        } else {
            System.out.println("No errors.");
        }
        if (export) {
            if (!Files.exists(exportTarget)) {
                Files.createDirectories(exportTarget);
            } else if (!Files.isDirectory(exportTarget)) {
                System.err.println("Output dir already exists and is a file: " + exportTarget.toAbsolutePath().toString());
                System.exit(SystemExitCodes.INVALID_ARGUMENT_EXIT_CODE);
            }
            final SystemExport sysexport = new SystemExport(broker, transaction, new Callback(verbose), null, direct);
            sysexport.export(exportTarget.toAbsolutePath().toString(), incremental, zip, errors);
        }
        transaction.commit();
    } catch (final EXistException e) {
        System.err.println("ERROR: Failed to retrieve database broker: " + e.getMessage());
        retval = SystemExitCodes.NO_BROKER_EXIT_CODE;
    } catch (final TerminatedException e) {
        System.err.println("WARN: Export was terminated by db.");
        retval = SystemExitCodes.TERMINATED_EARLY_EXIT_CODE;
    } catch (final PermissionDeniedException pde) {
        System.err.println("ERROR: Failed to retrieve database data: " + pde.getMessage());
        retval = SystemExitCodes.PERMISSION_DENIED_EXIT_CODE;
    } catch (final IOException ioe) {
        System.err.println("ERROR: Failed to retrieve database data: " + ioe.getMessage());
        retval = SystemExitCodes.IO_ERROR_EXIT_CODE;
    } finally {
        BrokerPool.stopAll(false);
    }
    System.exit(retval);
}
Also used : Path(java.nio.file.Path) Txn(org.exist.storage.txn.Txn) EXistException(org.exist.EXistException) IOException(java.io.IOException) DBBroker(org.exist.storage.DBBroker) PermissionDeniedException(org.exist.security.PermissionDeniedException) File(java.io.File) BrokerPool(org.exist.storage.BrokerPool) TerminatedException(org.exist.xquery.TerminatedException)

Example 4 with TerminatedException

use of org.exist.xquery.TerminatedException in project exist by eXist-db.

the class NativeBroker method findCollectionsMatching.

@Override
public List<String> findCollectionsMatching(final String regexp) {
    final List<String> collections = new ArrayList<>();
    final Pattern p = Pattern.compile(regexp);
    final Matcher m = p.matcher("");
    try (final ManagedLock<ReentrantLock> collectionsDbLock = lockManager.acquireBtreeReadLock(collectionsDb.getLockName())) {
        // TODO write a regexp lookup for key data in BTree.query
        // final IndexQuery idxQuery = new IndexQuery(IndexQuery.REGEXP, regexp);
        // List<Value> keys = collectionsDb.findKeysByCollectionName(idxQuery);
        final List<Value> keys = collectionsDb.getKeys();
        for (final Value key : keys) {
            final byte[] data = key.getData();
            if (data[0] == CollectionStore.KEY_TYPE_COLLECTION) {
                final String collectionName = UTF8.decode(data, 1, data.length - 1).toString();
                m.reset(collectionName);
                if (m.matches()) {
                    collections.add(collectionName);
                }
            }
        }
    } catch (final UnsupportedEncodingException e) {
    // LOG.error("Unable to encode '" + uri + "' in UTF-8");
    // return null;
    } catch (final LockException e) {
        LOG.error("Failed to acquire lock on {}", FileUtils.fileName(collectionsDb.getFile()));
    // return null;
    } catch (final TerminatedException | IOException | BTreeException e) {
        LOG.error(e.getMessage(), e);
    // return null;
    }
    return collections;
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) TerminatedException(org.exist.xquery.TerminatedException)

Example 5 with TerminatedException

use of org.exist.xquery.TerminatedException in project exist by eXist-db.

the class NativeBroker method getResourceById.

@Override
public DocumentImpl getResourceById(final int collectionId, final byte resourceType, final int documentId) throws PermissionDeniedException {
    XmldbURI uri;
    try (final ManagedLock<ReentrantLock> collectionsDbLock = lockManager.acquireBtreeReadLock(collectionsDb.getLockName())) {
        // get the collection uri
        String collectionUri = null;
        if (collectionId == FIRST_COLLECTION_ID) {
            collectionUri = "/db";
        } else {
            for (final Value collectionDbKey : collectionsDb.getKeys()) {
                final byte[] data = collectionDbKey.data();
                if (data[0] == CollectionStore.KEY_TYPE_COLLECTION) {
                    // Value collectionDbValue = collectionsDb.get(collectionDbKey);
                    final VariableByteInput vbi = collectionsDb.getAsStream(collectionDbKey);
                    final int id = vbi.readInt();
                    // check if the collection id matches (first 4 bytes)
                    if (collectionId == id) {
                        collectionUri = new String(Arrays.copyOfRange(data, 1, data.length));
                        break;
                    }
                }
            }
        }
        // get the resource uri
        final Value key = new CollectionStore.DocumentKey(collectionId, resourceType, documentId);
        final VariableByteInput vbi = collectionsDb.getAsStream(key);
        // skip doc id
        vbi.readInt();
        final String resourceUri = vbi.readUTF();
        // get the resource
        uri = XmldbURI.createInternal(collectionUri + "/" + resourceUri);
    } catch (final TerminatedException te) {
        LOG.error("Query Terminated", te);
        return null;
    } catch (final BTreeException bte) {
        LOG.error("Problem reading btree", bte);
        return null;
    } catch (final LockException e) {
        LOG.error("Failed to acquire lock on {}", FileUtils.fileName(collectionsDb.getFile()));
        return null;
    } catch (final IOException e) {
        LOG.error("IOException while reading resource data", e);
        return null;
    }
    return getResource(uri, Permission.READ);
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) VariableByteInput(org.exist.storage.io.VariableByteInput) XmldbURI(org.exist.xmldb.XmldbURI) TerminatedException(org.exist.xquery.TerminatedException)

Aggregations

TerminatedException (org.exist.xquery.TerminatedException)19 EXistException (org.exist.EXistException)9 IOException (java.io.IOException)8 ReentrantLock (java.util.concurrent.locks.ReentrantLock)8 PermissionDeniedException (org.exist.security.PermissionDeniedException)6 BTreeException (org.exist.storage.btree.BTreeException)6 IndexQuery (org.exist.storage.btree.IndexQuery)6 Value (org.exist.storage.btree.Value)6 XMLStreamException (javax.xml.stream.XMLStreamException)3 PooledObject (org.apache.commons.pool2.PooledObject)3 DefaultPooledObject (org.apache.commons.pool2.impl.DefaultPooledObject)3 Collection (org.exist.collections.Collection)3 DOMTransaction (org.exist.storage.dom.DOMTransaction)3 XmldbURI (org.exist.xmldb.XmldbURI)3 XPathException (org.exist.xquery.XPathException)3 DateTimeValue (org.exist.xquery.value.DateTimeValue)3 Path (java.nio.file.Path)2 ACLPermission (org.exist.security.ACLPermission)2 Permission (org.exist.security.Permission)2 DBBroker (org.exist.storage.DBBroker)2