use of org.exist.xmldb.EXistResource in project exist by eXist-db.
the class InteractiveClient method getResources.
/**
* Get list of resources contained in collection.
*
* @throws XMLDBException Description of the Exception
*/
protected void getResources() throws XMLDBException {
if (current == null) {
return;
}
setProperties();
final UserManagementService mgtService = (UserManagementService) current.getService("UserManagementService", "1.0");
final String[] childCollections = current.listChildCollections();
final String[] childResources = current.listResources();
resources = new String[childCollections.length + childResources.length];
// Collection child;
Permission perm;
// A list of ResourceDescriptor for the GUI
final List<ResourceDescriptor> tableData = new ArrayList<>(resources.length);
int i = 0;
for (; i < childCollections.length; i++) {
// child = current.getChildCollection(childCollections[i]);
perm = mgtService.getSubCollectionPermissions(current, childCollections[i]);
final Date created = mgtService.getSubCollectionCreationTime(current, childCollections[i]);
if ("true".equals(properties.getProperty(PERMISSIONS))) {
resources[i] = 'c' + perm.toString() + '\t' + getOwnerName(perm) + '\t' + getGroupName(perm) + '\t' + created.toString() + '\t' + childCollections[i];
} else {
resources[i] = childCollections[i];
}
if (options.startGUI) {
try {
tableData.add(new ResourceDescriptor.Collection(XmldbURI.xmldbUriFor(childCollections[i]), perm, created));
} catch (final URISyntaxException e) {
errorln("could not parse collection name into a valid URI: " + e.getMessage());
}
}
completitions.add(childCollections[i]);
}
Resource res;
for (int j = 0; j < childResources.length; i++, j++) {
res = current.getResource(childResources[j]);
perm = mgtService.getPermissions(res);
if (perm == null) {
// TODO this is not useful!
System.out.println("null");
}
final Date lastModificationTime = ((EXistResource) res).getLastModificationTime();
if ("true".equals(properties.getProperty(PERMISSIONS))) {
resources[i] = '-' + perm.toString() + '\t' + getOwnerName(perm) + '\t' + getGroupName(perm) + '\t' + lastModificationTime.toString() + '\t' + childResources[j];
} else {
resources[i] = childResources[j];
}
if (options.startGUI) {
try {
tableData.add(new ResourceDescriptor.Document(XmldbURI.xmldbUriFor(childResources[j]), perm, lastModificationTime));
} catch (final URISyntaxException e) {
errorln("could not parse document name into a valid URI: " + e.getMessage());
}
}
completitions.add(childResources[j]);
}
if (options.startGUI) {
frame.setResources(tableData);
}
}
use of org.exist.xmldb.EXistResource in project exist by eXist-db.
the class XMLDBStoreTask method execute.
@Override
public void execute() throws BuildException {
if (uri == null) {
throw new BuildException("you have to specify an XMLDB collection URI");
}
if ((fileSetList == null) && (srcFile == null)) {
throw new BuildException("no file set specified");
}
registerDatabase();
int p = uri.indexOf(XmldbURI.ROOT_COLLECTION);
if (p == Constants.STRING_NOT_FOUND) {
throw new BuildException("invalid uri: '" + uri + "'");
}
final String baseURI = uri.substring(0, p);
final String path;
if (p == (uri.length() - 3)) {
path = "";
} else {
path = uri.substring(p + 3);
}
Collection root = null;
try {
if (createCollection) {
root = DatabaseManager.getCollection(baseURI + XmldbURI.ROOT_COLLECTION, user, password);
root = mkcol(root, baseURI, XmldbURI.ROOT_COLLECTION, path);
} else {
root = DatabaseManager.getCollection(uri, user, password);
}
} catch (final XMLDBException e) {
final String msg = "XMLDB exception caught: " + e.getMessage();
if (failonerror) {
throw new BuildException(msg, e);
} else {
log(msg, e, Project.MSG_ERR);
return;
}
}
if (root == null) {
final String msg = "Collection " + uri + " could not be found.";
if (failonerror) {
throw new BuildException(msg);
} else {
log(msg, Project.MSG_ERR);
}
} else {
Resource res;
Collection col = root;
String relDir;
String prevDir = null;
if (srcFile != null) {
log("Storing " + srcFile.getName());
MimeType mime = getMimeTable().getContentTypeFor(srcFile.getName());
final String baseMimeType;
if (forceMimeType != null) {
baseMimeType = forceMimeType;
} else if (mime != null) {
baseMimeType = mime.getName();
} else {
baseMimeType = defaultMimeType;
}
if (type != null) {
if ("xml".equals(type)) {
mime = (baseMimeType != null) ? (new MimeType(baseMimeType, MimeType.XML)) : MimeType.XML_TYPE;
} else if ("binary".equals(type)) {
mime = (baseMimeType != null) ? (new MimeType(baseMimeType, MimeType.BINARY)) : MimeType.BINARY_TYPE;
}
}
// single file
if (mime == null) {
final String msg = "Cannot guess mime-type kind for " + srcFile.getName() + ". Treating it as a binary.";
log(msg, Project.MSG_ERR);
mime = (baseMimeType != null) ? (new MimeType(baseMimeType, MimeType.BINARY)) : MimeType.BINARY_TYPE;
}
final String resourceType = mime.isXMLType() ? XMLResource.RESOURCE_TYPE : BinaryResource.RESOURCE_TYPE;
if (targetFile == null) {
targetFile = srcFile.getName();
}
try {
log("Creating resource " + targetFile + " in collection " + col.getName() + " of type " + resourceType + " with mime-type: " + mime.getName(), Project.MSG_DEBUG);
res = col.createResource(targetFile, resourceType);
if (srcFile.length() == 0) {
// note: solves bug id 2429889 when this task hits empty files
} else {
res.setContent(srcFile);
((EXistResource) res).setMimeType(mime.getName());
col.storeResource(res);
}
if (permissions != null) {
setPermissions(res);
}
} catch (final XMLDBException e) {
final String msg = "XMLDB exception caught: " + e.getMessage();
if (failonerror) {
throw new BuildException(msg, e);
} else {
log(msg, e, Project.MSG_ERR);
}
}
} else {
for (final FileSet fileSet : fileSetList) {
log("Storing fileset", Project.MSG_DEBUG);
// using fileset
final DirectoryScanner scanner = fileSet.getDirectoryScanner(getProject());
scanner.scan();
final String[] includedFiles = scanner.getIncludedFiles();
final String[] includedDirs = scanner.getIncludedDirectories();
log("Found " + includedDirs.length + " directories and " + includedFiles.length + " files.\n");
final File baseDir = scanner.getBasedir();
if (includeEmptyDirs && createSubcollections) {
for (final String included : includedDirs) {
try {
log("Creating " + included + " ...\n");
// TODO : use dedicated function in XmldbURI
// check whether the relative file path contains file seps
p = included.lastIndexOf(File.separatorChar);
if (p != Constants.STRING_NOT_FOUND) {
relDir = included.substring(0, p);
// It's necessary to do this translation on Windows, and possibly MacOS:
relDir = relDir.replace(File.separatorChar, '/');
if (createSubcollections && ((prevDir == null) || (!relDir.equals(prevDir)))) {
// TODO : use dedicated function in XmldbURI
col = mkcol(root, baseURI, XmldbURI.ROOT_COLLECTION + path, relDir);
prevDir = relDir;
}
} else {
col = mkcol(root, baseURI, XmldbURI.ROOT_COLLECTION + path, included);
}
} catch (final XMLDBException e) {
final String msg = "XMLDB exception caught: " + e.getMessage();
if (failonerror) {
throw new BuildException(msg, e);
} else {
log(msg, e, Project.MSG_ERR);
}
}
}
}
for (final String included : includedFiles) {
try {
final File file = new File(baseDir, included);
log("Storing " + included + " ...\n");
// TODO : use dedicated function in XmldbURI
// check whether the relative file path contains file seps
p = included.lastIndexOf(File.separatorChar);
if (p != Constants.STRING_NOT_FOUND) {
relDir = included.substring(0, p);
// It's necessary to do this translation on Windows, and possibly MacOS:
relDir = relDir.replace(File.separatorChar, '/');
if (createSubcollections && ((prevDir == null) || (!relDir.equals(prevDir)))) {
// TODO : use dedicated function in XmldbURI
col = mkcol(root, baseURI, XmldbURI.ROOT_COLLECTION + path, relDir);
prevDir = relDir;
}
} else {
// No file separator found in resource name, reset col to the root collection
col = root;
}
MimeType currentMime = getMimeTable().getContentTypeFor(file.getName());
final String currentBaseMimeType;
if (forceMimeType != null) {
currentBaseMimeType = forceMimeType;
} else if (currentMime != null) {
currentBaseMimeType = currentMime.getName();
} else {
currentBaseMimeType = defaultMimeType;
}
if (type != null) {
if ("xml".equals(type)) {
currentMime = (currentBaseMimeType != null) ? (new MimeType(currentBaseMimeType, MimeType.XML)) : MimeType.XML_TYPE;
} else if ("binary".equals(type)) {
currentMime = (currentBaseMimeType != null) ? (new MimeType(currentBaseMimeType, MimeType.BINARY)) : MimeType.BINARY_TYPE;
}
}
if (currentMime == null) {
final String msg = "Cannot find mime-type kind for " + file.getName() + ". Treating it as a binary.";
log(msg, Project.MSG_ERR);
currentMime = (currentBaseMimeType != null) ? (new MimeType(currentBaseMimeType, MimeType.BINARY)) : MimeType.BINARY_TYPE;
}
final String resourceType = currentMime.isXMLType() ? XMLResource.RESOURCE_TYPE : BinaryResource.RESOURCE_TYPE;
log("Creating resource " + file.getName() + " in collection " + col.getName() + " of type " + resourceType + " with mime-type: " + currentMime.getName(), Project.MSG_DEBUG);
res = col.createResource(file.getName(), resourceType);
res.setContent(file);
((EXistResource) res).setMimeType(currentMime.getName());
col.storeResource(res);
if (permissions != null) {
setPermissions(res);
}
} catch (final XMLDBException e) {
final String msg = "XMLDB exception caught: " + e.getMessage();
if (failonerror) {
throw new BuildException(msg, e);
} else {
log(msg, e, Project.MSG_ERR);
}
}
}
}
}
}
}
use of org.exist.xmldb.EXistResource in project exist by eXist-db.
the class FileTaskTest method chmod.
@Test
public void chmod() throws XMLDBException {
final Project project = buildFileRule.getProject();
project.setProperty(PROP_ANT_TEST_DATA_TEST_COLLECTION, TEST_COLLECTION_NAME);
project.setProperty(PROP_ANT_TEST_DATA_TEST_RESOURCE, TEST_RESOURCE_NAME);
buildFileRule.executeTarget("chmod");
final String result = project.getProperty(PROP_ANT_TEST_DATA_RESULT);
assertThat(result, containsString(TEST_RESOURCE_NAME));
final Collection col = existEmbeddedServer.getRoot().getChildCollection(TEST_COLLECTION_NAME);
final EXistResource res = (EXistResource) col.getResource(TEST_RESOURCE_NAME);
assertEquals("---rwxrwx", res.getPermissions().toString());
}
use of org.exist.xmldb.EXistResource in project exist by eXist-db.
the class GetParameterTest method beforeClass.
@BeforeClass
public static void beforeClass() throws XMLDBException {
root = DatabaseManager.getCollection("xmldb:exist://localhost:" + existWebServer.getPort() + "/xmlrpc/db", "admin", "");
BinaryResource res = (BinaryResource) root.createResource(XQUERY_FILENAME, "BinaryResource");
((EXistResource) res).setMimeType("application/xquery");
res.setContent(XQUERY);
root.storeResource(res);
UserManagementService ums = (UserManagementService) root.getService("UserManagementService", "1.0");
ums.chmod(res, 0777);
}
use of org.exist.xmldb.EXistResource in project exist by eXist-db.
the class XQueryTriggerTest method setup.
/**
* create "log" document that will be updated by the trigger,
* and store the XQuery module implementing the trigger under test
*/
@Before
public void setup() throws XMLDBException {
final CollectionManagementService service = (CollectionManagementService) existEmbeddedServer.getRoot().getService("CollectionManagementService", "1.0");
testCollection = service.createCollection(TEST_COLLECTION);
assertNotNull(testCollection);
final XMLResource doc = (XMLResource) testCollection.createResource(LOG_NAME, "XMLResource");
doc.setContent(EMPTY_LOG);
testCollection.storeResource(doc);
final BinaryResource module = (BinaryResource) testCollection.createResource(MODULE_NAME, "BinaryResource");
((EXistResource) module).setMimeType("application/xquery");
module.setContent(MODULE.getBytes());
testCollection.storeResource(module);
}
Aggregations