use of org.xmldb.api.base.ResourceSet in project exist by eXist-db.
the class XMLDBXPathTask method execute.
@Override
public void execute() throws BuildException {
if (uri == null) {
throw new BuildException("you have to specify an XMLDB collection URI");
}
if (text != null) {
final PropertyHelper helper = PropertyHelper.getPropertyHelper(getProject());
query = helper.replaceProperties(null, text, null);
}
if (query == null) {
throw new BuildException("you have to specify a query");
}
log("XPath is: " + query, org.apache.tools.ant.Project.MSG_DEBUG);
registerDatabase();
try {
log("Get base collection: " + uri, Project.MSG_DEBUG);
final Collection base = DatabaseManager.getCollection(uri, user, password);
if (base == null) {
final String msg = "Collection " + uri + " could not be found.";
if (failonerror) {
throw new BuildException(msg);
} else {
log(msg, Project.MSG_ERR);
}
} else {
final XPathQueryService service = (XPathQueryService) base.getService("XPathQueryService", "1.0");
// set pretty-printing on
service.setProperty(OutputKeys.INDENT, "yes");
service.setProperty(OutputKeys.ENCODING, "UTF-8");
if (namespace != null) {
log("Using namespace: " + namespace, Project.MSG_DEBUG);
service.setNamespace("ns", namespace);
}
final ResourceSet results;
if (resource != null) {
log("Query resource: " + resource, Project.MSG_DEBUG);
results = service.queryResource(resource, query);
} else {
log("Query collection", Project.MSG_DEBUG);
results = service.query(query);
}
log("Found " + results.getSize() + " results", Project.MSG_INFO);
if ((destDir != null) && (results != null)) {
log("write results to directory " + destDir.getAbsolutePath(), Project.MSG_INFO);
final ResourceIterator iter = results.getIterator();
log("Writing results to directory " + destDir.getAbsolutePath(), Project.MSG_DEBUG);
while (iter.hasMoreResources()) {
final XMLResource res = (XMLResource) iter.nextResource();
log("Writing resource " + res.getId(), Project.MSG_DEBUG);
writeResource(res, destDir);
}
} else if (outputproperty != null) {
if (count) {
getProject().setNewProperty(outputproperty, String.valueOf(results.getSize()));
} else {
final ResourceIterator iter = results.getIterator();
final StringBuilder result = new StringBuilder();
while (iter.hasMoreResources()) {
final XMLResource res = (XMLResource) iter.nextResource();
result.append(res.getContent().toString());
result.append("\n");
}
getProject().setNewProperty(outputproperty, result.toString());
}
}
}
} catch (final XMLDBException e) {
final String msg = "XMLDB exception caught while executing query: " + e.getMessage();
if (failonerror) {
throw new BuildException(msg, e);
} else {
log(msg, e, Project.MSG_ERR);
}
} catch (final IOException e) {
final String msg = "XMLDB exception caught while writing destination file: " + e.getMessage();
if (failonerror) {
throw new BuildException(msg, e);
} else {
log(msg, e, Project.MSG_ERR);
}
}
}
use of org.xmldb.api.base.ResourceSet in project exist by eXist-db.
the class XMLDBSecurityTest method nonSetGidXQueryCannotWriteRestrictedCollection.
@Test(expected = XMLDBException.class)
public void nonSetGidXQueryCannotWriteRestrictedCollection() throws XMLDBException {
final Collection test = DatabaseManager.getCollection(getBaseUri() + "/db/securityTest2", "test1", "test1");
final long timestamp = System.currentTimeMillis();
final String content = "<not_setgid>" + timestamp + "</not_setgid>";
// create an XQuery /db/securityTest1/not_setgid.xquery
final String xquery = "xmldb:store('/db/securityTest2/forSetGidWrite', 'not_setgid.xml', " + content + ")";
Resource xqueryResource = test.createResource("not_setgid.xquery", "BinaryResource");
xqueryResource.setContent(xquery);
test.storeResource(xqueryResource);
// set the xquery to be owned by 'test1':'users' and set it 'setgid', and set it 'rx' by ohers, so 'test3' can execute it!
UserManagementService ums = (UserManagementService) test.getService("UserManagementService", "1.0");
xqueryResource = test.getResource("not_setgid.xquery");
// NOT setgid
ums.chmod(xqueryResource, 00705);
// create a collection for the XQuery to write into
final CollectionManagementService cms = (CollectionManagementService) test.getService("CollectionManagementService", "1.0");
final Collection colForSetUid = cms.createCollection("forSetGidWrite");
// only allow the group 'users' to write into the collection
ums = (UserManagementService) colForSetUid.getService("UserManagementService", "1.0");
ums.chmod(0070);
// execute the XQuery as the 'test3' user... it should become 'setgid' of 'users' and succeed.
final Collection test3 = DatabaseManager.getCollection(getBaseUri() + "/db/securityTest2", "test3", "test3");
final EXistXPathQueryService queryService = (EXistXPathQueryService) test3.getService("XPathQueryService", "1.0");
final ResourceSet result = queryService.executeStoredQuery("/db/securityTest2/not_setgid.xquery");
assertFalse("/db/securityTest2/forSetGidWrite/not_setgid.xml".equals(result.getResource(0).getContent()));
}
use of org.xmldb.api.base.ResourceSet in project exist by eXist-db.
the class XMLDBSecurityTest method canExecuteXQueryWithOnlyExecutePermissionOnParentCollection.
@Test
public void canExecuteXQueryWithOnlyExecutePermissionOnParentCollection() throws XMLDBException {
Collection test = DatabaseManager.getCollection(getBaseUri() + "/db/securityTest1", "test1", "test1");
final UserManagementService ums = (UserManagementService) test.getService("UserManagementService", "1.0");
final String xquery = "<xquery>{ 1 + 1 }</xquery>";
Resource xqueryResource = test.createResource("test.xquery", BinaryResource.RESOURCE_TYPE);
xqueryResource.setContent(xquery);
test.storeResource(xqueryResource);
ums.chmod("--x------");
// set execute bit on xquery (its off by default!)
ums.chmod(xqueryResource, "rwx------");
test.close();
test = DatabaseManager.getCollection(getBaseUri() + "/db/securityTest1", "test1", "test1");
xqueryResource = test.getResource("test.xquery");
assertEquals(xquery, new String((byte[]) xqueryResource.getContent()));
// execute the stored XQuery
final EXistXPathQueryService queryService = (EXistXPathQueryService) test.getService("XPathQueryService", "1.0");
final ResourceSet result = queryService.executeStoredQuery("/db/securityTest1/test.xquery");
assertEquals("<xquery>2</xquery>", result.getResource(0).getContent());
}
use of org.xmldb.api.base.ResourceSet in project exist by eXist-db.
the class XMLDBSecurityTest method nonSetUidXQueryCannotWriteRestrictedCollection.
@Test(expected = XMLDBException.class)
public void nonSetUidXQueryCannotWriteRestrictedCollection() throws XMLDBException {
final Collection test = DatabaseManager.getCollection(getBaseUri() + "/db/securityTest1", "test1", "test1");
final long timestamp = System.currentTimeMillis();
final String content = "<not_setuid>" + timestamp + "</not_setuid>";
// create an XQuery /db/securityTest1/not_setuid.xquery
final String xquery = "xmldb:store('/db/securityTest1/forSetUidWrite', 'not_setuid.xml', " + content + ")";
Resource xqueryResource = test.createResource("not_setuid.xquery", "BinaryResource");
xqueryResource.setContent(xquery);
test.storeResource(xqueryResource);
// set the xquery to be owned by 'test1' and do NOT set it 'setuid', and do set it 'rx' by 'users' group so 'test2' can execute it!
UserManagementService ums = (UserManagementService) test.getService("UserManagementService", "1.0");
xqueryResource = test.getResource("not_setuid.xquery");
// NOT SETUID
ums.chmod(xqueryResource, 00750);
// create a collection for the XQuery to write into
final CollectionManagementService cms = (CollectionManagementService) test.getService("CollectionManagementService", "1.0");
final Collection colForSetUid = cms.createCollection("forSetUidWrite");
// only allow the user 'test1' to write into the collection
ums = (UserManagementService) colForSetUid.getService("UserManagementService", "1.0");
ums.chmod(0700);
// execute the XQuery as the 'test2' user... it should become 'setuid' of 'test1' and succeed.
final Collection test2 = DatabaseManager.getCollection(getBaseUri() + "/db/securityTest1", "test2", "test2");
final EXistXPathQueryService queryService = (EXistXPathQueryService) test2.getService("XPathQueryService", "1.0");
final ResourceSet result = queryService.executeStoredQuery("/db/securityTest1/not_setuid.xquery");
assertFalse("/db/securityTest1/forSetUidWrite/not_setuid.xml".equals(result.getResource(0).getContent()));
}
use of org.xmldb.api.base.ResourceSet in project exist by eXist-db.
the class CollectionConfigurationTest method collectionConfigurationService6.
@Test
public void collectionConfigurationService6() throws XMLDBException {
Collection testCollection = DatabaseManager.getCollection(XmldbURI.LOCAL_DB + "/" + TEST_COLLECTION);
// Add document....
XMLResource doc = (XMLResource) testCollection.createResource(TestConstants.TEST_XML_URI.toString(), "XMLResource");
doc.setContent(DOCUMENT_CONTENT);
testCollection.storeResource(doc);
// ... then configure collection *manually*
XmldbURI configurationFileName = XmldbURI.create(CollectionConfiguration.DEFAULT_COLLECTION_CONFIG_FILE);
storeConfiguration(CONF_COLL_URI, configurationFileName, CONFIG1);
// ... then configure collection automatically
IndexQueryService idxConf = (IndexQueryService) testCollection.getService("IndexQueryService", "1.0");
idxConf.configureCollection(CONFIG1);
XPathQueryService service = (XPathQueryService) testCollection.getService("XPathQueryService", "1.0");
// our config file
ResourceSet result = service.query("xmldb:get-child-resources('" + CONF_COLL_URI + "')");
assertEquals(configurationFileName.toString(), result.getResource(0).getContent());
// No numeric values because we have no index
result = service.query("util:index-key-occurrences( /test/a, 1 ) ");
assertEquals(0, result.getSize());
// No string value because we have no index
result = service.query("util:index-key-occurrences( /test/b, \"1\" ) ");
assertEquals(0, result.getSize());
// No numeric values because we have no index
result = service.query("util:qname-index-lookup( xs:QName(\"a\"), 1 ) ");
assertEquals(0, result.getSize());
// No string value because we have no index
result = service.query("util:qname-index-lookup( xs:QName(\"b\"), \"1\" ) ");
assertEquals(0, result.getSize());
// ...let's activate the index
idxConf.reindexCollection();
// WARNING : the code hereafter used to *not* work whereas
// testCollectionConfigurationService4 did.
// Adding confMgr.invalidateAll(getName()); in Collection.storeInternal solved the problem
// Strange case that needs investigations... -pb
// 3 numeric values
result = service.query("util:index-key-occurrences(/test/a, 1)");
assertEquals("3", result.getResource(0).getContent());
// ... but 1 string value
result = service.query("util:index-key-occurrences(/test/b, \"1\")");
assertEquals("1", result.getResource(0).getContent());
// 3 numeric values
result = service.query("util:qname-index-lookup( xs:QName(\"a\"), 1 ) ");
assertEquals(3, result.getSize());
// ... but 1 string value
result = service.query("util:qname-index-lookup( xs:QName(\"b\"), \"1\" ) ");
assertEquals(1, result.getSize());
}
Aggregations