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);
}
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);
}
}
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();
}
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();
}
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();
}
}
Aggregations