Search in sources :

Example 21 with XQueryContext

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

the class DocUtils method getDocumentByPathFromDB.

private static Sequence getDocumentByPathFromDB(final XQueryContext context, final String path) throws XPathException, PermissionDeniedException {
    // check if the loaded documents should remain locked
    final LockMode lockType = context.lockDocumentsOnLoad() ? LockMode.WRITE_LOCK : LockMode.READ_LOCK;
    try {
        final XmldbURI baseURI = context.getBaseURI().toXmldbURI();
        final XmldbURI pathUri;
        if (baseURI != null && !(baseURI.equals("") || baseURI.equals("/db"))) {
            // relative collection Path: add the current base URI
            pathUri = baseURI.resolveCollectionPath(XmldbURI.xmldbUriFor(path, false));
        } else {
            pathUri = XmldbURI.xmldbUriFor(path, false);
        }
        // relative collection Path: add the current module call URI if applicable
        final XmldbURI resourceUri = Optional.ofNullable(context.getModuleLoadPath()).filter(moduleLoadPath -> !moduleLoadPath.isEmpty()).flatMap(moduleLoadPath -> Try(() -> XmldbURI.xmldbUriFor(moduleLoadPath)).toOption()).map(moduleLoadPath -> moduleLoadPath.resolveCollectionPath(pathUri)).orElse(pathUri);
        // try to open the document and acquire a lock
        try (final LockedDocument lockedDoc = context.getBroker().getXMLResource(resourceUri, lockType)) {
            if (lockedDoc == null) {
                return Sequence.EMPTY_SEQUENCE;
            } else {
                final DocumentImpl doc = lockedDoc.getDocument();
                if (!doc.getPermissions().validate(context.getSubject(), Permission.READ)) {
                    throw new PermissionDeniedException("Insufficient privileges to read resource " + path);
                }
                if (doc.getResourceType() == DocumentImpl.BINARY_FILE) {
                    throw new XPathException("Document " + path + " is a binary resource, not an XML document. Please consider using the function util:binary-doc() to retrieve a reference to it.");
                }
                return new NodeProxy(doc);
            }
        }
    } catch (final URISyntaxException e) {
        throw new XPathException(e);
    }
}
Also used : LockMode(org.exist.storage.lock.Lock.LockMode) SAXNotSupportedException(org.xml.sax.SAXNotSupportedException) BrokerPool(org.exist.storage.BrokerPool) XMLReaderPool(org.exist.util.XMLReaderPool) ErrorCodes(org.exist.xquery.ErrorCodes) NodeProxy(org.exist.dom.persistent.NodeProxy) PermissionDeniedException(org.exist.security.PermissionDeniedException) SAXNotRecognizedException(org.xml.sax.SAXNotRecognizedException) XMLReader(org.xml.sax.XMLReader) Source(org.exist.source.Source) java.net(java.net) Namespaces(org.exist.Namespaces) Document(org.w3c.dom.Document) XmldbURI(org.exist.xmldb.XmldbURI) DocumentImpl(org.exist.dom.persistent.DocumentImpl) Permission(org.exist.security.Permission) XQueryContext(org.exist.xquery.XQueryContext) Nullable(javax.annotation.Nullable) InputSource(org.xml.sax.InputSource) LockedDocument(org.exist.dom.persistent.LockedDocument) AnyURIValue(org.exist.xquery.value.AnyURIValue) IOException(java.io.IOException) Try(com.evolvedbinary.j8fu.Try.Try) URLSource(org.exist.source.URLSource) SAXException(org.xml.sax.SAXException) SourceFactory(org.exist.source.SourceFactory) Optional(java.util.Optional) Sequence(org.exist.xquery.value.Sequence) Pattern(java.util.regex.Pattern) SAXAdapter(org.exist.dom.memtree.SAXAdapter) XPathException(org.exist.xquery.XPathException) InputStream(java.io.InputStream) XPathException(org.exist.xquery.XPathException) LockedDocument(org.exist.dom.persistent.LockedDocument) PermissionDeniedException(org.exist.security.PermissionDeniedException) LockMode(org.exist.storage.lock.Lock.LockMode) DocumentImpl(org.exist.dom.persistent.DocumentImpl) NodeProxy(org.exist.dom.persistent.NodeProxy) XmldbURI(org.exist.xmldb.XmldbURI)

Example 22 with XQueryContext

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

the class LowLevelTextTest method setUp.

@Before
public void setUp() throws DatabaseConfigurationException, EXistException, XPathException, PermissionDeniedException, IOException {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
    xqueryPool = pool.getXQueryPool();
    stringSource = new StringSource(TEST_XQUERY_SOURCE);
    final XQuery xquery = pool.getXQueryService();
    final XQueryContext context = new XQueryContext(broker.getBrokerPool());
    preCompiledXQuery = xquery.compile(context, stringSource);
}
Also used : CompiledXQuery(org.exist.xquery.CompiledXQuery) XQuery(org.exist.xquery.XQuery) XQueryContext(org.exist.xquery.XQueryContext) StringSource(org.exist.source.StringSource)

Example 23 with XQueryContext

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

the class DebuggeeJointImpl method evalution.

@Override
public String evalution(String script) throws Exception {
    Database db = compiledXQuery.getContext().getDatabase();
    // TODO: account required
    try (final DBBroker broker = db.getBroker()) {
        XQueryContext context = compiledXQuery.getContext().copyContext();
        context.setDebuggeeJoint(null);
        context.undeclareGlobalVariable(Debuggee.SESSION);
        context.undeclareGlobalVariable(Debuggee.IDEKEY);
        XQuery service = broker.getBrokerPool().getXQueryService();
        CompiledXQuery compiled = service.compile(broker, context, script);
        Sequence resultSequence = service.execute(broker, compiled, null);
        SAXSerializer sax = null;
        Serializer serializer = broker.getSerializer();
        serializer.reset();
        try {
            sax = (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);
            Properties outputProps = new Properties();
            StringWriter writer = new StringWriter();
            sax.setOutput(writer, outputProps);
            serializer.setSAXHandlers(sax, sax);
            for (SequenceIterator i = resultSequence.iterate(); i.hasNext(); ) {
                Item next = i.nextItem();
                if (Type.subTypeOf(next.getType(), Type.NODE))
                    serializer.toSAX((NodeValue) next);
                else
                    writer.write(next.getStringValue());
            }
            return writer.toString();
        } finally {
            if (sax != null) {
                SerializerPool.getInstance().returnObject(sax);
            }
        }
    }
}
Also used : CompiledXQuery(org.exist.xquery.CompiledXQuery) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQueryContext(org.exist.xquery.XQueryContext) DBBroker(org.exist.storage.DBBroker) StringWriter(java.io.StringWriter) Database(org.exist.Database) SAXSerializer(org.exist.util.serializer.SAXSerializer) SAXSerializer(org.exist.util.serializer.SAXSerializer) Serializer(org.exist.storage.serializers.Serializer)

Example 24 with XQueryContext

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

the class Util method compileQuery.

static CompiledXQuery compileQuery(final DBBroker broker, final XQuery xqueryService, final XQueryPool xqueryPool, final Source query) throws PermissionDeniedException, XPathException, IOException {
    CompiledXQuery compiled = xqueryPool.borrowCompiledXQuery(broker, query);
    XQueryContext context;
    if (compiled == null) {
        context = new XQueryContext(broker.getBrokerPool());
    } else {
        context = compiled.getContext();
        context.prepareForReuse();
    }
    if (compiled == null) {
        compiled = xqueryService.compile(context, query);
    } else {
        compiled.getContext().updateContext(context);
        context.getWatchDog().reset();
    }
    return compiled;
}
Also used : CompiledXQuery(org.exist.xquery.CompiledXQuery) XQueryContext(org.exist.xquery.XQueryContext)

Example 25 with XQueryContext

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

the class DebuggeeImpl method joint.

public boolean joint(CompiledXQuery compiledXQuery) {
    synchronized (this) {
        IoSession session = connection.connect();
        if (session == null)
            return false;
        // link debugger session & script
        DebuggeeJointImpl joint = new DebuggeeJointImpl();
        joint.setCompiledScript(compiledXQuery);
        XQueryContext context = compiledXQuery.getContext();
        context.setDebuggeeJoint(joint);
        String idesession = "";
        if (context.isVarDeclared(Debuggee.SESSION)) {
            try {
                Variable var = context.resolveVariable(Debuggee.SESSION);
                idesession = var.getValue().toString();
            } catch (XPathException e) {
            }
        }
        String idekey = "";
        if (context.isVarDeclared(Debuggee.IDEKEY)) {
            try {
                Variable var = context.resolveVariable(Debuggee.IDEKEY);
                idekey = var.getValue().toString();
            } catch (XPathException e) {
            }
        }
        joint.continuation(new Init(session, idesession, idekey));
        return true;
    }
}
Also used : Init(org.exist.debuggee.dbgp.packets.Init) Variable(org.exist.xquery.Variable) XPathException(org.exist.xquery.XPathException) XQueryContext(org.exist.xquery.XQueryContext) IoSession(org.apache.mina.core.session.IoSession)

Aggregations

XQueryContext (org.exist.xquery.XQueryContext)70 CompiledXQuery (org.exist.xquery.CompiledXQuery)37 Sequence (org.exist.xquery.value.Sequence)34 XQuery (org.exist.xquery.XQuery)33 DBBroker (org.exist.storage.DBBroker)24 Test (org.junit.Test)23 XPathException (org.exist.xquery.XPathException)18 BrokerPool (org.exist.storage.BrokerPool)17 IOException (java.io.IOException)11 Source (org.exist.source.Source)10 XQueryPool (org.exist.storage.XQueryPool)10 Node (org.w3c.dom.Node)10 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)8 PermissionDeniedException (org.exist.security.PermissionDeniedException)8 AnyURIValue (org.exist.xquery.value.AnyURIValue)8 URI (java.net.URI)7 StringValue (org.exist.xquery.value.StringValue)7 InputSource (org.xml.sax.InputSource)7 EXistException (org.exist.EXistException)6 QName (org.exist.dom.QName)6