use of org.exist.xmldb.EXistCollectionManagementService in project exist by eXist-db.
the class InteractiveClient method store.
/**
* Pass to this method a java file object
* (may be a file or a directory), GUI object
* will create relative collections or resources
* recursively
*/
private void store(final Collection collection, final Path file, final UploadDialog upload) {
// cancel, stop crawl
if (upload.isCancelled()) {
return;
}
// can't read there, inform client
if (!Files.isReadable(file)) {
upload.showMessage(file.toAbsolutePath() + " impossible to read ");
return;
}
final XmldbURI filenameUri;
try {
filenameUri = XmldbURI.xmldbUriFor(FileUtils.fileName(file));
} catch (final URISyntaxException e1) {
upload.showMessage(file.toAbsolutePath() + " could not be encoded as a URI");
return;
}
// Directory, create collection, and crawl it
if (Files.isDirectory(file)) {
Collection c = null;
try {
c = collection.getChildCollection(filenameUri.toString());
if (c == null) {
final EXistCollectionManagementService mgtService = (EXistCollectionManagementService) collection.getService("CollectionManagementService", "1.0");
c = mgtService.createCollection(filenameUri);
}
} catch (final XMLDBException e) {
upload.showMessage("Impossible to create a collection " + file.toAbsolutePath() + ": " + e.getMessage());
e.printStackTrace();
}
// change displayed collection if it's OK
upload.setCurrentDir(file.toAbsolutePath().toString());
if (c instanceof Observable) {
((Observable) c).addObserver(upload.getObserver());
}
// maybe a depth or recurs flag could be added here
final Collection childCollection = c;
try (final Stream<Path> children = Files.list(file)) {
children.forEach(child -> store(childCollection, child, upload));
} catch (final IOException e) {
upload.showMessage("Impossible to upload " + file.toAbsolutePath() + ": " + e.getMessage());
e.printStackTrace();
}
return;
}
// File, create and store resource
if (!Files.isDirectory(file)) {
upload.reset();
upload.setCurrent(FileUtils.fileName(file));
final long fileSize = FileUtils.sizeQuietly(file);
upload.setCurrentSize(fileSize);
MimeType mimeType = MimeTable.getInstance().getContentTypeFor(FileUtils.fileName(file));
// unknown mime type, here prefered is to do nothing
if (mimeType == null) {
upload.showMessage(file.toAbsolutePath() + " - unknown suffix. No matching mime-type found in : " + MimeTable.getInstance().getSrc());
// if some one prefers to store it as binary by default, but dangerous
mimeType = MimeType.BINARY_TYPE;
}
try {
final Resource res = collection.createResource(filenameUri.toString(), mimeType.getXMLDBType());
((EXistResource) res).setMimeType(mimeType.getName());
res.setContent(file);
collection.storeResource(res);
++filesCount;
this.totalLength += fileSize;
upload.setStoredSize(this.totalLength);
} catch (final XMLDBException e) {
upload.showMessage("Impossible to store a resource " + file.toAbsolutePath() + ": " + e.getMessage());
}
}
}
use of org.exist.xmldb.EXistCollectionManagementService in project exist by eXist-db.
the class InteractiveClient method rmcol.
private void rmcol(final XmldbURI collection) throws XMLDBException {
final EXistCollectionManagementService mgtService = (EXistCollectionManagementService) current.getService("CollectionManagementService", "1.0");
message("removing collection " + collection + " ...");
mgtService.removeCollection(collection);
messageln("done.");
}
use of org.exist.xmldb.EXistCollectionManagementService in project exist by eXist-db.
the class InteractiveClient method parseZip.
/**
* stores given Resource.
*
* @param zipPath Path to a zip file
*
* @throws XMLDBException in case of error writing to the database
* @return true if operation succeeded
*/
protected synchronized boolean parseZip(final Path zipPath) throws XMLDBException {
try {
final ZipFile zfile = new ZipFile(zipPath.toFile());
if (current instanceof Observable && options.verbose) {
final ProgressObserver observer = new ProgressObserver();
((Observable) current).addObserver(observer);
}
final long start0 = System.currentTimeMillis();
long bytes = 0;
final Enumeration<? extends ZipEntry> e = zfile.entries();
int number = 0;
Collection base = current;
String baseStr = "";
while (e.hasMoreElements()) {
number++;
final ZipEntry ze = e.nextElement();
final String zeName = ze.getName().replace('\\', '/');
if (!Paths.get("/db").resolve(zeName).normalize().startsWith(Paths.get("/db"))) {
throw new IOException("Detected archive exit attack! zipFile=" + zipPath.toAbsolutePath().toString() + ", entry=" + ze.getName());
}
final String[] pathSteps = zeName.split("/");
final StringBuilder currStr = new StringBuilder(pathSteps[0]);
for (int i = 1; i < pathSteps.length - 1; i++) {
currStr.append("/").append(pathSteps[i]);
}
if (!baseStr.equals(currStr)) {
base = current;
for (int i = 0; i < pathSteps.length - 1; i++) {
Collection c = base.getChildCollection(pathSteps[i]);
if (c == null) {
final EXistCollectionManagementService mgtService = (EXistCollectionManagementService) base.getService("CollectionManagementService", "1.0");
c = mgtService.createCollection(XmldbURI.xmldbUriFor(pathSteps[i]));
}
base = c;
}
if (base instanceof Observable && options.verbose) {
final ProgressObserver observer = new ProgressObserver();
((Observable) base).addObserver(observer);
}
baseStr = currStr.toString();
messageln("entering directory " + baseStr);
}
if (!ze.isDirectory()) {
final String localName = pathSteps[pathSteps.length - 1];
final long start = System.currentTimeMillis();
MimeType mimeType = MimeTable.getInstance().getContentTypeFor(localName);
if (mimeType == null) {
mimeType = MimeType.BINARY_TYPE;
}
final Resource document = base.createResource(localName, mimeType.getXMLDBType());
message("storing Zip-entry document " + localName + " (" + (number) + " of " + zfile.size() + ") ...");
document.setContent(new ZipEntryInputSource(zfile, ze));
((EXistResource) document).setMimeType(mimeType.getName());
base.storeResource(document);
messageln("done.");
messageln("parsing " + ze.getSize() + " bytes took " + (System.currentTimeMillis() - start) + "ms." + EOL);
bytes += ze.getSize();
}
}
messageln("parsed " + bytes + " bytes in " + (System.currentTimeMillis() - start0) + "ms.");
} catch (final URISyntaxException e) {
errorln("uri syntax exception parsing a ZIP entry from " + zipPath.toString() + ": " + e.getMessage());
} catch (final IOException e) {
errorln("could not parse ZIP file " + zipPath.toAbsolutePath() + ": " + e.getMessage());
}
return true;
}
use of org.exist.xmldb.EXistCollectionManagementService in project exist by eXist-db.
the class InteractiveClient method findGZipRecursive.
private synchronized boolean findGZipRecursive(final Collection collection, final Path dir, final XmldbURI base) throws XMLDBException, IOException {
final List<Path> files = FileUtils.list(dir);
Collection c;
Resource document;
EXistCollectionManagementService mgtService;
// The XmldbURIs here aren't really used...
XmldbURI next;
MimeType mimeType;
int i = 0;
for (final Path file : files) {
i++;
next = base.append(FileUtils.fileName(file));
try {
if (Files.isDirectory(file)) {
messageln("entering directory " + file.toAbsolutePath().toString());
c = collection.getChildCollection(FileUtils.fileName(file));
if (c == null) {
mgtService = (EXistCollectionManagementService) collection.getService("CollectionManagementService", "1.0");
c = mgtService.createCollection(XmldbURI.xmldbUriFor(FileUtils.fileName(file)));
}
if (c instanceof Observable && options.verbose) {
final ProgressObserver observer = new ProgressObserver();
((Observable) c).addObserver(observer);
}
findGZipRecursive(c, file, next);
} else {
final long start1 = System.currentTimeMillis();
final String compressedName = FileUtils.fileName(file);
String localName = compressedName;
final String[] cSuffix = { ".gz", ".Z" };
boolean isCompressed = false;
for (final String suf : cSuffix) {
if (localName.endsWith(suf)) {
// Removing compressed prefix to validate
localName = compressedName.substring(0, localName.length() - suf.length());
isCompressed = true;
break;
}
}
mimeType = MimeTable.getInstance().getContentTypeFor(localName);
if (mimeType == null) {
messageln("File " + compressedName + " has an unknown suffix. Cannot determine file type.");
mimeType = MimeType.BINARY_TYPE;
}
message("storing document " + compressedName + " (" + i + " of " + files.size() + ") " + "...");
document = collection.createResource(compressedName, mimeType.getXMLDBType());
document.setContent(isCompressed ? new GZIPInputSource(file) : file);
((EXistResource) document).setMimeType(mimeType.getName());
collection.storeResource(document);
++filesCount;
messageln(" " + Files.size(file) + (isCompressed ? " compressed" : "") + " bytes in " + (System.currentTimeMillis() - start1) + "ms.");
}
} catch (final URISyntaxException e) {
errorln("uri syntax exception parsing " + file.toAbsolutePath().toString() + ": " + e.getMessage());
}
}
return true;
}
use of org.exist.xmldb.EXistCollectionManagementService in project exist by eXist-db.
the class XMLDBCopy method evalWithCollection.
@Override
public Sequence evalWithCollection(final Collection collection, final Sequence[] args, final Sequence contextSequence) throws XPathException {
if (isCalledAs(FS_COPY_RESOURCE_NAME)) {
final XmldbURI destination = new AnyURIValue(args[2].itemAt(0).getStringValue()).toXmldbURI();
final XmldbURI doc = new AnyURIValue(args[1].itemAt(0).getStringValue()).toXmldbURI();
try {
final Resource resource = collection.getResource(doc.toString());
if (resource == null) {
logger.error("Resource {} not found", doc);
throw new XPathException(this, "Resource " + doc + " not found");
}
final EXistCollectionManagementService service = (EXistCollectionManagementService) collection.getService("CollectionManagementService", "1.0");
final DBBroker.PreserveType preserve;
if (getArgumentCount() == 5) {
final boolean preserveArg = args[4].itemAt(0).toJavaObject(boolean.class);
if (preserveArg) {
preserve = DBBroker.PreserveType.PRESERVE;
} else {
preserve = DBBroker.PreserveType.DEFAULT;
}
} else {
preserve = DBBroker.PreserveType.DEFAULT;
}
final XmldbURI newName;
if (getArgumentCount() >= 4) {
if (!args[3].isEmpty()) {
newName = XmldbURI.create(args[3].itemAt(0).getStringValue());
} else {
newName = doc.lastSegment();
}
} else {
newName = null;
}
service.copyResource(doc, destination, newName, preserve.name());
if (isCalledAs(FS_COPY_RESOURCE_NAME)) {
return new StringValue(destination.append(newName).getRawCollectionPath());
} else {
return Sequence.EMPTY_SEQUENCE;
}
} catch (final XMLDBException e) {
logger.error("XMLDB exception caught: ", e);
throw new XPathException(this, "XMLDB exception caught: " + e.getMessage(), e);
}
} else {
final XmldbURI destination = new AnyURIValue(args[1].itemAt(0).getStringValue()).toXmldbURI();
try {
final EXistCollectionManagementService service = (EXistCollectionManagementService) collection.getService("CollectionManagementService", "1.0");
final DBBroker.PreserveType preserve;
if (getArgumentCount() == 3) {
final boolean preserveArg = args[2].itemAt(0).toJavaObject(boolean.class);
if (preserveArg) {
preserve = DBBroker.PreserveType.PRESERVE;
} else {
preserve = DBBroker.PreserveType.DEFAULT;
}
} else {
preserve = DBBroker.PreserveType.DEFAULT;
}
service.copy(XmldbURI.xmldbUriFor(collection.getName()), destination, null, preserve.name());
if (isCalledAs(FS_COPY_COLLECTION_NAME)) {
final XmldbURI targetName = XmldbURI.xmldbUriFor(collection.getName()).lastSegment();
return new StringValue(destination.append(targetName).getRawCollectionPath());
} else {
return Sequence.EMPTY_SEQUENCE;
}
} catch (final XMLDBException e) {
logger.error("Cannot copy collection: ", e);
throw new XPathException(this, "Cannot copy collection: " + e.getMessage(), e);
} catch (final URISyntaxException e) {
logger.error("URI exception: ", e);
throw new XPathException(this, "URI exception: " + e.getMessage(), e);
}
}
}
Aggregations