Search in sources :

Example 1 with DigestType

use of org.exist.util.crypto.digest.DigestType in project exist by eXist-db.

the class BinaryDoc method eval.

@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
    final Sequence emptyParamReturnValue = (isCalledAs(FS_BINARY_DOC_NAME) || isCalledAs(FS_BINARY_DOC_CONTENT_DIGEST_NAME)) ? Sequence.EMPTY_SEQUENCE : BooleanValue.FALSE;
    if (args[0].isEmpty()) {
        return emptyParamReturnValue;
    }
    final String path = args[0].getStringValue();
    try (final LockedDocument lockedDoc = context.getBroker().getXMLResource(XmldbURI.xmldbUriFor(path), LockMode.READ_LOCK)) {
        if (lockedDoc == null) {
            return emptyParamReturnValue;
        }
        final DocumentImpl doc = lockedDoc.getDocument();
        if (doc.getResourceType() != DocumentImpl.BINARY_FILE) {
            return emptyParamReturnValue;
        } else if (isCalledAs(FS_BINARY_DOC_NAME)) {
            try (final Txn transaction = context.getBroker().continueOrBeginTransaction()) {
                final BinaryDocument bin = (BinaryDocument) doc;
                final InputStream is = context.getBroker().getBinaryResource(transaction, bin);
                final Base64BinaryDocument b64doc = Base64BinaryDocument.getInstance(context, is);
                b64doc.setUrl(path);
                transaction.commit();
                return b64doc;
            }
        } else if (isCalledAs(FS_BINARY_DOC_CONTENT_DIGEST_NAME)) {
            final String algorithm = args[1].getStringValue();
            final DigestType digestType;
            try {
                digestType = DigestType.forCommonName(algorithm);
            } catch (final IllegalArgumentException e) {
                throw new XPathException(this, "Invalid algorithm: " + algorithm, e);
            }
            try (final Txn transaction = context.getBroker().getBrokerPool().getTransactionManager().beginTransaction()) {
                final BinaryDocument bin = (BinaryDocument) doc;
                final MessageDigest messageDigest = context.getBroker().getBinaryResourceContentDigest(transaction, bin, digestType);
                final InputStream is = new UnsynchronizedByteArrayInputStream(messageDigest.getValue());
                final Sequence result = BinaryValueFromInputStream.getInstance(context, new HexBinaryValueType(), is);
                transaction.commit();
                return result;
            }
        } else {
            return BooleanValue.TRUE;
        }
    } catch (final URISyntaxException e) {
        logger.error("Invalid resource URI", e);
        throw new XPathException(this, "Invalid resource uri", e);
    } catch (final PermissionDeniedException e) {
        logger.error("{}: permission denied to read resource", path, e);
        throw new XPathException(this, path + ": permission denied to read resource");
    } catch (final IOException | TransactionException e) {
        logger.error("{}: I/O error while reading resource", path, e);
        throw new XPathException(this, path + ": I/O error while reading resource", e);
    }
}
Also used : XPathException(org.exist.xquery.XPathException) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) InputStream(java.io.InputStream) Txn(org.exist.storage.txn.Txn) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) DocumentImpl(org.exist.dom.persistent.DocumentImpl) BinaryDocument(org.exist.dom.persistent.BinaryDocument) TransactionException(org.exist.storage.txn.TransactionException) DigestType(org.exist.util.crypto.digest.DigestType) LockedDocument(org.exist.dom.persistent.LockedDocument) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) PermissionDeniedException(org.exist.security.PermissionDeniedException) MessageDigest(org.exist.util.crypto.digest.MessageDigest)

Example 2 with DigestType

use of org.exist.util.crypto.digest.DigestType in project exist by eXist-db.

the class RpcConnection method getContentDigest.

@Override
public Map<String, Object> getContentDigest(final String path, final String digestAlgorithm) throws EXistException, PermissionDeniedException {
    try {
        final DigestType digestType = DigestType.forCommonName(digestAlgorithm);
        final MessageDigest messageDigest = this.<MessageDigest>readDocument(XmldbURI.xmldbUriFor(path)).apply((document, broker, transaction) -> {
            if (document instanceof BinaryDocument) {
                return broker.getBinaryResourceContentDigest(transaction, (BinaryDocument) document, digestType);
            } else {
                throw new EXistException("Only supported for binary documents");
            }
        });
        final Map<String, Object> result = new HashMap<>();
        result.put("digest-algorithm", messageDigest.getDigestType().getCommonNames()[0]);
        result.put("digest", messageDigest.getValue());
        return result;
    } catch (final IllegalArgumentException | URISyntaxException e) {
        throw new EXistException(e);
    }
}
Also used : DigestType(org.exist.util.crypto.digest.DigestType) EXistException(org.exist.EXistException) URISyntaxException(java.net.URISyntaxException) MessageDigest(org.exist.util.crypto.digest.MessageDigest)

Example 3 with DigestType

use of org.exist.util.crypto.digest.DigestType in project exist by eXist-db.

the class BlobStoreDumpTool method main.

public static void main(final String[] args) throws IOException {
    try {
        CompatibleJavaVersionCheck.checkForCompatibleJavaVersion();
        final ParsedArguments arguments = CommandLineParser.withArguments(digestArg, persistentFileArg).andArguments(helpArg).parse(args);
        final DigestType digestType = arguments.get(digestArg);
        final Path persistentFile = arguments.get(persistentFileArg);
        final PrintStream printStream = System.out;
        dump(digestType, persistentFile, printStream);
    } catch (final StartException e) {
        if (e.getMessage() != null && !e.getMessage().isEmpty()) {
            System.err.println(e.getMessage());
        }
        System.exit(e.getErrorCode());
    } catch (final ArgumentException e) {
        System.out.println(e.getMessageAndUsage());
        System.exit(SystemExitCodes.INVALID_ARGUMENT_EXIT_CODE);
    }
}
Also used : Path(java.nio.file.Path) PrintStream(java.io.PrintStream) DigestType(org.exist.util.crypto.digest.DigestType) StartException(org.exist.start.StartException) ArgumentException(se.softhouse.jargo.ArgumentException) ParsedArguments(se.softhouse.jargo.ParsedArguments)

Aggregations

DigestType (org.exist.util.crypto.digest.DigestType)3 URISyntaxException (java.net.URISyntaxException)2 MessageDigest (org.exist.util.crypto.digest.MessageDigest)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 PrintStream (java.io.PrintStream)1 Path (java.nio.file.Path)1 UnsynchronizedByteArrayInputStream (org.apache.commons.io.input.UnsynchronizedByteArrayInputStream)1 EXistException (org.exist.EXistException)1 BinaryDocument (org.exist.dom.persistent.BinaryDocument)1 DocumentImpl (org.exist.dom.persistent.DocumentImpl)1 LockedDocument (org.exist.dom.persistent.LockedDocument)1 PermissionDeniedException (org.exist.security.PermissionDeniedException)1 StartException (org.exist.start.StartException)1 TransactionException (org.exist.storage.txn.TransactionException)1 Txn (org.exist.storage.txn.Txn)1 XPathException (org.exist.xquery.XPathException)1 ArgumentException (se.softhouse.jargo.ArgumentException)1 ParsedArguments (se.softhouse.jargo.ParsedArguments)1