Search in sources :

Example 76 with PermissionDeniedException

use of org.exist.security.PermissionDeniedException in project exist by eXist-db.

the class XPathUtil method getNode.

/**
 * Converts an XMLResource into a NodeProxy.
 *
 * @param broker The DBBroker to use to access the database
 * @param xres The XMLResource to convert
 * @return A NodeProxy for accessing the content represented by xres
 * @throws XPathException if an XMLDBException is encountered
 */
public static final NodeProxy getNode(DBBroker broker, XMLResource xres) throws XPathException {
    if (xres instanceof LocalXMLResource) {
        final LocalXMLResource lres = (LocalXMLResource) xres;
        try {
            return lres.getNode();
        } catch (final XMLDBException xe) {
            throw new XPathException("Failed to convert LocalXMLResource to node: " + xe.getMessage());
        }
    }
    DocumentImpl document;
    try {
        document = broker.getCollection(XmldbURI.xmldbUriFor(xres.getParentCollection().getName())).getDocument(broker, XmldbURI.xmldbUriFor(xres.getDocumentId()));
    } catch (final URISyntaxException xe) {
        throw new XPathException(xe);
    } catch (final XMLDBException xe) {
        throw new XPathException("Failed to get document for RemoteXMLResource: " + xe.getMessage());
    } catch (final PermissionDeniedException pde) {
        throw new XPathException("Failed to get document: " + pde.getMessage());
    }
    final NodeId nodeId = broker.getBrokerPool().getNodeFactory().createFromString(((RemoteXMLResource) xres).getNodeId());
    return new NodeProxy(document, nodeId);
}
Also used : LocalXMLResource(org.exist.xmldb.LocalXMLResource) NodeId(org.exist.numbering.NodeId) XMLDBException(org.xmldb.api.base.XMLDBException) PermissionDeniedException(org.exist.security.PermissionDeniedException) URISyntaxException(java.net.URISyntaxException) DocumentImpl(org.exist.dom.persistent.DocumentImpl) NodeProxy(org.exist.dom.persistent.NodeProxy)

Example 77 with PermissionDeniedException

use of org.exist.security.PermissionDeniedException in project exist by eXist-db.

the class XQueryContext method importModuleFromLocation.

private Module importModuleFromLocation(final String namespaceURI, @Nullable final String prefix, final AnyURIValue locationHint) throws XPathException {
    String location = locationHint.toString();
    // Is the module's namespace mapped to a URL ?
    if (mappedModules.containsKey(location)) {
        location = mappedModules.get(location).toString();
    }
    // is it a Java module?
    if (location.startsWith(JAVA_URI_START)) {
        location = location.substring(JAVA_URI_START.length());
        return loadBuiltInModule(namespaceURI, location);
    }
    if (location.startsWith(XmldbURI.XMLDB_URI_PREFIX) || ((location.indexOf(':') == -1) && moduleLoadPath.startsWith(XmldbURI.XMLDB_URI_PREFIX))) {
        // Is the module source stored in the database?
        try {
            XmldbURI locationUri = XmldbURI.xmldbUriFor(location);
            if (moduleLoadPath.startsWith(XmldbURI.XMLDB_URI_PREFIX)) {
                final XmldbURI moduleLoadPathUri = XmldbURI.xmldbUriFor(moduleLoadPath);
                locationUri = moduleLoadPathUri.resolveCollectionPath(locationUri);
            }
            try (final LockedDocument lockedSourceDoc = getBroker().getXMLResource(locationUri.toCollectionPathURI(), LockMode.READ_LOCK)) {
                final DocumentImpl sourceDoc = lockedSourceDoc == null ? null : lockedSourceDoc.getDocument();
                if (sourceDoc == null) {
                    throw moduleLoadException("Module location hint URI '" + location + "' does not refer to anything.", location);
                }
                if ((sourceDoc.getResourceType() != DocumentImpl.BINARY_FILE) || !"application/xquery".equals(sourceDoc.getMimeType())) {
                    throw moduleLoadException("Module location hint URI '" + location + "' does not refer to an XQuery.", location);
                }
                final Source moduleSource = new DBSource(getBroker(), (BinaryDocument) sourceDoc, true);
                return compileOrBorrowModule(prefix, namespaceURI, location, moduleSource);
            } catch (final PermissionDeniedException e) {
                throw moduleLoadException("Permission denied to read module source from location hint URI '" + location + ".", location, e);
            }
        } catch (final URISyntaxException e) {
            throw moduleLoadException("Invalid module location hint URI '" + location + "'.", location, e);
        }
    } else {
        // No. Load from file or URL
        final Source moduleSource;
        try {
            // TODO: use URIs to ensure proper resolution of relative locations
            moduleSource = SourceFactory.getSource(getBroker(), moduleLoadPath, location, true);
            if (moduleSource == null) {
                throw moduleLoadException("Source for module '" + namespaceURI + "' not found module location hint URI '" + location + "'.", location);
            }
        } catch (final MalformedURLException e) {
            throw moduleLoadException("Invalid module location hint URI '" + location + "'.", location, e);
        } catch (final IOException e) {
            throw moduleLoadException("Source for module '" + namespaceURI + "' could not be read, module location hint URI '" + location + "'.", location, e);
        } catch (final PermissionDeniedException e) {
            throw moduleLoadException("Permission denied to read module source from location hint URI '" + location + ".", location, e);
        }
        return compileOrBorrowModule(prefix, namespaceURI, location, moduleSource);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) PermissionDeniedException(org.exist.security.PermissionDeniedException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) XmldbURI(org.exist.xmldb.XmldbURI)

Example 78 with PermissionDeniedException

use of org.exist.security.PermissionDeniedException in project exist by eXist-db.

the class FluentBrokerAPITest method collectionThenCollectionAndDoc.

@Test
public void collectionThenCollectionAndDoc() throws PermissionDeniedException, EXistException, LockException {
    final XmldbURI docUri = uri("all-test.xml");
    final long collectionCreated = 1234;
    final IMocksControl ctrl = createStrictControl();
    ctrl.checkOrder(true);
    final BrokerPool mockBrokerPool = ctrl.createMock(BrokerPool.class);
    final DBBroker mockBroker = ctrl.createMock(DBBroker.class);
    final Collection mockCollection = ctrl.createMock(Collection.class);
    final LockedDocument mockLockedDocument = ctrl.createMock(LockedDocument.class);
    final DocumentImpl mockDocument = ctrl.createMock(DocumentImpl.class);
    expect(mockBrokerPool.getBroker()).andReturn(mockBroker);
    expect(mockBroker.openCollection(TEST_COLLECTION_URI, READ_LOCK)).andReturn(mockCollection);
    expect(mockCollection.getCreated()).andReturn(collectionCreated);
    expect(mockCollection.getDocumentWithLock(mockBroker, docUri, READ_LOCK)).andReturn(mockLockedDocument);
    expect(mockLockedDocument.getDocument()).andReturn(mockDocument);
    expect(mockCollection.getURI()).andReturn(TEST_COLLECTION_URI);
    expect(mockDocument.getFileURI()).andReturn(docUri);
    // NOTE: checks that Collection lock is release before Document lock
    mockCollection.close();
    mockLockedDocument.close();
    mockBroker.close();
    ctrl.replay();
    final Function<Collection, Long> collectionOp = collection -> collection.getCreated();
    final BiFunction<Collection, DocumentImpl, String> collectionDocOp = (collection, doc) -> collection.getURI().append(doc.getFileURI()).toString();
    final Tuple2<Long, String> result = FluentBrokerAPI.builder(mockBrokerPool).withCollection(TEST_COLLECTION_URI, READ_LOCK).execute(collectionOp).withDocument(collection -> new Tuple2<>(docUri, READ_LOCK)).execute(collectionDocOp).doAll();
    assertEquals(collectionCreated, result._1.longValue());
    assertEquals(TEST_COLLECTION_URI.append(docUri), result._2);
    ctrl.verify();
}
Also used : Tuple3(com.evolvedbinary.j8fu.tuple.Tuple3) Tuple2(com.evolvedbinary.j8fu.tuple.Tuple2) IMocksControl(org.easymock.IMocksControl) LockMode(org.exist.storage.lock.Lock.LockMode) LockedDocument(org.exist.dom.persistent.LockedDocument) EasyMock.createStrictControl(org.easymock.EasyMock.createStrictControl) BiFunction(java.util.function.BiFunction) Test(org.junit.Test) PermissionDeniedException(org.exist.security.PermissionDeniedException) EasyMock.expect(org.easymock.EasyMock.expect) Function(java.util.function.Function) FluentBrokerAPI.uri(org.exist.storage.FluentBrokerAPI.uri) LockException(org.exist.util.LockException) Collection(org.exist.collections.Collection) XmldbURI(org.exist.xmldb.XmldbURI) DocumentImpl(org.exist.dom.persistent.DocumentImpl) EXistException(org.exist.EXistException) Lock(org.exist.storage.lock.Lock) Assert.assertEquals(org.junit.Assert.assertEquals) DocumentImpl(org.exist.dom.persistent.DocumentImpl) IMocksControl(org.easymock.IMocksControl) LockedDocument(org.exist.dom.persistent.LockedDocument) Collection(org.exist.collections.Collection) XmldbURI(org.exist.xmldb.XmldbURI) Test(org.junit.Test)

Example 79 with PermissionDeniedException

use of org.exist.security.PermissionDeniedException in project exist by eXist-db.

the class FluentBrokerAPITest method collectionOnly.

@Test
public void collectionOnly() throws PermissionDeniedException, EXistException, LockException {
    final long collectionCreated = 1234;
    final IMocksControl ctrl = createStrictControl();
    ctrl.checkOrder(true);
    final BrokerPool mockBrokerPool = ctrl.createMock(BrokerPool.class);
    final DBBroker mockBroker = ctrl.createMock(DBBroker.class);
    final Collection mockCollection = ctrl.createMock(Collection.class);
    expect(mockBrokerPool.getBroker()).andReturn(mockBroker);
    expect(mockBroker.openCollection(TEST_COLLECTION_URI, READ_LOCK)).andReturn(mockCollection);
    expect(mockCollection.getCreated()).andReturn(collectionCreated);
    mockCollection.close();
    mockBroker.close();
    ctrl.replay();
    final Function<Collection, Long> collectionOp = collection -> collection.getCreated();
    final long result = FluentBrokerAPI.builder(mockBrokerPool).withCollection(TEST_COLLECTION_URI, READ_LOCK).execute(collectionOp).doAll();
    assertEquals(collectionCreated, result);
    ctrl.verify();
}
Also used : IMocksControl(org.easymock.IMocksControl) Tuple3(com.evolvedbinary.j8fu.tuple.Tuple3) Tuple2(com.evolvedbinary.j8fu.tuple.Tuple2) IMocksControl(org.easymock.IMocksControl) LockMode(org.exist.storage.lock.Lock.LockMode) LockedDocument(org.exist.dom.persistent.LockedDocument) EasyMock.createStrictControl(org.easymock.EasyMock.createStrictControl) BiFunction(java.util.function.BiFunction) Test(org.junit.Test) PermissionDeniedException(org.exist.security.PermissionDeniedException) EasyMock.expect(org.easymock.EasyMock.expect) Function(java.util.function.Function) FluentBrokerAPI.uri(org.exist.storage.FluentBrokerAPI.uri) LockException(org.exist.util.LockException) Collection(org.exist.collections.Collection) XmldbURI(org.exist.xmldb.XmldbURI) DocumentImpl(org.exist.dom.persistent.DocumentImpl) EXistException(org.exist.EXistException) Lock(org.exist.storage.lock.Lock) Assert.assertEquals(org.junit.Assert.assertEquals) Collection(org.exist.collections.Collection) Test(org.junit.Test)

Example 80 with PermissionDeniedException

use of org.exist.security.PermissionDeniedException in project exist by eXist-db.

the class GMLHSQLIndexWorker method search.

@Override
protected NodeSet search(DBBroker broker, NodeSet contextSet, Geometry EPSG4326_geometry, int spatialOp, Connection conn) throws SQLException {
    String extraSelection = null;
    String bboxConstraint = null;
    // TODO : generate it in AbstractGMLJDBCIndexWorker
    String docConstraint = "";
    boolean refine_query_on_doc = false;
    if (contextSet != null) {
        if (contextSet.getDocumentSet().getDocumentCount() <= index.getMaxDocsInContextToRefineQuery()) {
            refine_query_on_doc = true;
            DocumentImpl doc;
            Iterator<DocumentImpl> it = contextSet.getDocumentSet().getDocumentIterator();
            doc = it.next();
            docConstraint = "(DOCUMENT_URI = '" + doc.getURI().toString() + "')";
            while (it.hasNext()) {
                doc = it.next();
                docConstraint = docConstraint + " OR (DOCUMENT_URI = '" + doc.getURI().toString() + "')";
            }
            if (LOG.isDebugEnabled()) {
                LOG.debug("Refine query on documents is enabled.");
            }
        } else {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Refine query on documents is disabled.");
            }
        }
    }
    switch(spatialOp) {
        // BBoxes are equal
        case SpatialOperator.EQUALS:
            bboxConstraint = "(EPSG4326_MINX = ? AND EPSG4326_MAXX = ?)" + " AND (EPSG4326_MINY = ? AND EPSG4326_MAXY = ?)";
            break;
        // Nothing much we can do with the BBox at this stage
        case SpatialOperator.DISJOINT:
            // Retrieve the BBox though...
            extraSelection = ", EPSG4326_MINX, EPSG4326_MAXX, EPSG4326_MINY, EPSG4326_MAXY";
            break;
        // BBoxes intersect themselves
        case SpatialOperator.INTERSECTS:
        case SpatialOperator.TOUCHES:
        case SpatialOperator.CROSSES:
        case SpatialOperator.OVERLAPS:
            bboxConstraint = "(EPSG4326_MAXX >= ? AND EPSG4326_MINX <= ?)" + " AND (EPSG4326_MAXY >= ? AND EPSG4326_MINY <= ?)";
            break;
        // BBox is fully within
        case SpatialOperator.WITHIN:
            bboxConstraint = "(EPSG4326_MINX >= ? AND EPSG4326_MAXX <= ?)" + " AND (EPSG4326_MINY >= ? AND EPSG4326_MAXY <= ?)";
            break;
        // BBox fully contains
        case SpatialOperator.CONTAINS:
            bboxConstraint = "(EPSG4326_MINX <= ? AND EPSG4326_MAXX >= ?)" + " AND (EPSG4326_MINY <= ? AND EPSG4326_MAXY >= ?)";
            break;
        default:
            throw new IllegalArgumentException("Unsupported spatial operator:" + spatialOp);
    }
    PreparedStatement ps = conn.prepareStatement("SELECT EPSG4326_WKB, DOCUMENT_URI, NODE_ID_UNITS, NODE_ID" + (extraSelection == null ? "" : extraSelection) + " FROM " + GMLHSQLIndex.TABLE_NAME + (bboxConstraint == null ? (refine_query_on_doc ? " WHERE " + docConstraint : "") : " WHERE " + (refine_query_on_doc ? "(" + docConstraint + ") AND " : "") + bboxConstraint) + ";");
    if (bboxConstraint != null) {
        ps.setDouble(1, EPSG4326_geometry.getEnvelopeInternal().getMinX());
        ps.setDouble(2, EPSG4326_geometry.getEnvelopeInternal().getMaxX());
        ps.setDouble(3, EPSG4326_geometry.getEnvelopeInternal().getMinY());
        ps.setDouble(4, EPSG4326_geometry.getEnvelopeInternal().getMaxY());
    }
    ResultSet rs = null;
    NodeSet result = null;
    try {
        int disjointPostFiltered = 0;
        rs = ps.executeQuery();
        // new ExtArrayNodeSet(docs.getLength(), 250)
        result = new ExtArrayNodeSet();
        while (rs.next()) {
            DocumentImpl doc = null;
            try {
                doc = (DocumentImpl) broker.getXMLResource(XmldbURI.create(rs.getString("DOCUMENT_URI")));
            } catch (PermissionDeniedException e) {
                LOG.debug(e);
                // Ignore since the broker has no right on the document
                continue;
            }
            // contextSet == null should be used to scan the whole index
            if (contextSet == null || refine_query_on_doc || contextSet.getDocumentSet().contains(doc.getDocId())) {
                NodeId nodeId = new DLN(rs.getInt("NODE_ID_UNITS"), rs.getBytes("NODE_ID"), 0);
                NodeProxy p = new NodeProxy(doc, nodeId);
                // VirtualNodeSet when on the DESCENDANT_OR_SELF axis
                if (contextSet == null || contextSet.get(p) != null) {
                    boolean geometryMatches = false;
                    if (spatialOp == SpatialOperator.DISJOINT) {
                        // No BBox intersection : obviously disjoint
                        if (rs.getDouble("EPSG4326_MAXX") < EPSG4326_geometry.getEnvelopeInternal().getMinX() || rs.getDouble("EPSG4326_MINX") > EPSG4326_geometry.getEnvelopeInternal().getMaxX() || rs.getDouble("EPSG4326_MAXY") < EPSG4326_geometry.getEnvelopeInternal().getMinY() || rs.getDouble("EPSG4326_MINY") > EPSG4326_geometry.getEnvelopeInternal().getMaxY()) {
                            geometryMatches = true;
                            disjointPostFiltered++;
                        }
                    }
                    // Possible match : check the geometry
                    if (!geometryMatches) {
                        try {
                            Geometry geometry = wkbReader.read(rs.getBytes("EPSG4326_WKB"));
                            switch(spatialOp) {
                                case SpatialOperator.EQUALS:
                                    geometryMatches = geometry.equals(EPSG4326_geometry);
                                    break;
                                case SpatialOperator.DISJOINT:
                                    geometryMatches = geometry.disjoint(EPSG4326_geometry);
                                    break;
                                case SpatialOperator.INTERSECTS:
                                    geometryMatches = geometry.intersects(EPSG4326_geometry);
                                    break;
                                case SpatialOperator.TOUCHES:
                                    geometryMatches = geometry.touches(EPSG4326_geometry);
                                    break;
                                case SpatialOperator.CROSSES:
                                    geometryMatches = geometry.crosses(EPSG4326_geometry);
                                    break;
                                case SpatialOperator.WITHIN:
                                    geometryMatches = geometry.within(EPSG4326_geometry);
                                    break;
                                case SpatialOperator.CONTAINS:
                                    geometryMatches = geometry.contains(EPSG4326_geometry);
                                    break;
                                case SpatialOperator.OVERLAPS:
                                    geometryMatches = geometry.overlaps(EPSG4326_geometry);
                                    break;
                            }
                        } catch (ParseException e) {
                            // Transforms the exception into an SQLException.
                            // Very unlikely to happen though...
                            SQLException ee = new SQLException(e.getMessage());
                            ee.initCause(e);
                            throw ee;
                        }
                    }
                    if (geometryMatches)
                        result.add(p);
                }
            }
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("{} eligible geometries, {}selected{}", rs.getRow(), result.getItemCount(), spatialOp == SpatialOperator.DISJOINT ? "(" + disjointPostFiltered + " post filtered)" : "");
        }
        return result;
    } finally {
        if (rs != null)
            rs.close();
        if (ps != null)
            ps.close();
    }
}
Also used : DLN(org.exist.numbering.DLN) Geometry(com.vividsolutions.jts.geom.Geometry) NodeId(org.exist.numbering.NodeId) PermissionDeniedException(org.exist.security.PermissionDeniedException) ParseException(com.vividsolutions.jts.io.ParseException)

Aggregations

PermissionDeniedException (org.exist.security.PermissionDeniedException)182 EXistException (org.exist.EXistException)82 XmldbURI (org.exist.xmldb.XmldbURI)70 IOException (java.io.IOException)58 DocumentImpl (org.exist.dom.persistent.DocumentImpl)48 Collection (org.exist.collections.Collection)44 DBBroker (org.exist.storage.DBBroker)41 Txn (org.exist.storage.txn.Txn)38 LockException (org.exist.util.LockException)35 SAXException (org.xml.sax.SAXException)35 LockedDocument (org.exist.dom.persistent.LockedDocument)31 XPathException (org.exist.xquery.XPathException)31 Permission (org.exist.security.Permission)23 URISyntaxException (java.net.URISyntaxException)22 TriggerException (org.exist.collections.triggers.TriggerException)22 Source (org.exist.source.Source)20 Path (java.nio.file.Path)19 Account (org.exist.security.Account)18 InputSource (org.xml.sax.InputSource)18 Sequence (org.exist.xquery.value.Sequence)17