use of org.exist.xmldb.XmldbURI 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.XmldbURI in project exist by eXist-db.
the class MutableCollection method serialize.
/**
* Serializes the Collection to a byte representation
*
* Counterpart method to {@link #deserialize(DBBroker, XmldbURI, VariableByteInput)}
*
* @param outputStream The output stream to write the collection contents to
*/
@Override
public void serialize(final VariableByteOutputStream outputStream) throws IOException, LockException {
outputStream.writeInt(collectionId);
final int size;
final Iterator<XmldbURI> i;
// TODO(AR) should we READ_LOCK the Collection to stop it being modified concurrently? see NativeBroker#saveCollection line 1801 - already has WRITE_LOCK ;-)
// try(final ManagedCollectionLock collectionLock = lockManager.acquireCollectionReadLock(path)) {
size = subCollections.size();
// i = subCollections.stableIterator();
i = subCollections.iterator();
// }
outputStream.writeInt(size);
while (i.hasNext()) {
final XmldbURI childCollectionURI = i.next();
outputStream.writeUTF(childCollectionURI.toString());
}
permissions.write(outputStream);
outputStream.writeLong(created);
}
use of org.exist.xmldb.XmldbURI in project exist by eXist-db.
the class MutableCollection method storeDocument.
@Override
public void storeDocument(final Txn transaction, final DBBroker broker, final XmldbURI name, final InputSource source, @Nullable MimeType mimeType, @Nullable final Date createdDate, @Nullable final Date lastModifiedDate, @Nullable final Permission permission, @Nullable final DocumentType documentType, @Nullable final XMLReader xmlReader) throws EXistException, PermissionDeniedException, SAXException, LockException, IOException {
if (mimeType == null) {
mimeType = MimeType.BINARY_TYPE;
}
if (mimeType.isXMLType()) {
// Store XML Document
final BiConsumer2E<XMLReader, IndexInfo, SAXException, EXistException> validatorFn = (xmlReader1, validateIndexInfo) -> {
validateIndexInfo.setReader(xmlReader1, null);
try {
xmlReader1.parse(source);
} catch (final SAXException e) {
throw new SAXException("The XML parser reported a problem: " + e.getMessage(), e);
} catch (final IOException e) {
throw new EXistException(e);
}
};
final BiConsumer2E<XMLReader, IndexInfo, SAXException, EXistException> parserFn = (xmlReader1, storeIndexInfo) -> {
try {
storeIndexInfo.setReader(xmlReader1, null);
xmlReader1.parse(source);
} catch (final IOException e) {
throw new EXistException(e);
}
};
storeXmlDocument(transaction, broker, name, mimeType, createdDate, lastModifiedDate, permission, documentType, xmlReader, validatorFn, parserFn);
} else {
// Store Binary Document
try (final InputStream is = source.getByteStream()) {
if (is == null) {
throw new IOException("storeDocument received a null InputStream when trying to store a Binary Document");
}
addBinaryResource(transaction, broker, name, is, mimeType.getName(), -1, createdDate, lastModifiedDate, permission);
}
}
}
use of org.exist.xmldb.XmldbURI in project exist by eXist-db.
the class MutableCollection method deserialize.
/**
* Read collection contents from the stream
*
* Counterpart method to {@link #serialize(VariableByteOutputStream)}
*
* @param broker The database broker
* @param path The path of the Collection
* @param istream The input stream to deserialize the Collection from
*/
private static MutableCollection deserialize(final DBBroker broker, final XmldbURI path, final VariableByteInput istream) throws IOException, PermissionDeniedException, LockException {
final int collectionId = istream.readInt();
if (collectionId < 0) {
throw new IOException("Internal error reading collection: invalid collection id");
}
final int collLen = istream.readInt();
// TODO(AR) should we WRITE_LOCK the Collection to stop it being loaded from disk concurrently? see NativeBroker#openCollection line 1030 - already has READ_LOCK ;-)
// try(final ManagedCollectionLock collectionLock = lockManager.acquireCollectionWriteLock(path, false)) {
final LinkedHashSet<XmldbURI> subCollections = new LinkedHashSet<>(Math.max(16, collLen));
for (int i = 0; i < collLen; i++) {
subCollections.add(XmldbURI.create(istream.readUTF()));
}
final Permission permission = PermissionFactory.getDefaultCollectionPermission(broker.getBrokerPool().getSecurityManager());
permission.read(istream);
if (!permission.validate(broker.getCurrentSubject(), Permission.EXECUTE)) {
throw new PermissionDeniedException("Permission denied to open the Collection " + path);
}
final long created = istream.readLong();
final LinkedHashMap<String, DocumentImpl> documents = new LinkedHashMap<>();
final MutableCollection collection = new MutableCollection(broker, collectionId, path, permission, created, subCollections, documents);
broker.getCollectionResources(new InternalAccess() {
@Override
public void addDocument(final DocumentImpl doc) throws EXistException {
doc.setCollection(collection);
if (doc.getDocId() == DocumentImpl.UNKNOWN_DOCUMENT_ID) {
LOG.error("Document must have ID. [{}]", doc);
throw new EXistException("Document must have ID.");
}
documents.put(doc.getFileURI().lastSegmentString(), doc);
}
@Override
public int getId() {
return collectionId;
}
});
return collection;
// }
}
use of org.exist.xmldb.XmldbURI in project exist by eXist-db.
the class MutableCollection method setPath.
@Override
public final void setPath(XmldbURI path, final boolean updateChildren) {
path = path.toCollectionPathURI();
// TODO : see if the URI resolves against DBBroker.TEMP_COLLECTION
this.isTempCollection = path.getRawCollectionPath().equals(XmldbURI.TEMP_COLLECTION);
this.path = path;
if (updateChildren) {
for (final Map.Entry<String, DocumentImpl> docEntry : documents.entrySet()) {
final XmldbURI docUri = path.append(docEntry.getKey());
try (final ManagedDocumentLock documentLock = lockManager.acquireDocumentWriteLock(docUri)) {
final DocumentImpl doc = docEntry.getValue();
// this will invalidate the cached `uri` in DocumentImpl
doc.setCollection(this);
} catch (final LockException e) {
LOG.error(e.getMessage(), e);
throw new IllegalStateException(e);
}
}
}
}
Aggregations