use of org.xmldb.api.base.Collection 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.xmldb.api.base.Collection 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.xmldb.api.base.Collection in project exist by eXist-db.
the class InteractiveClient method parse.
/**
* Method called by the store Dialog
*
* @param files : selected
* @param upload : GUI object
* @throws XMLDBException in case of an error uploading the resources
* @return true if the operation succeeded
*/
protected synchronized boolean parse(final List<Path> files, final UploadDialog upload) throws XMLDBException {
final Collection uploadRootCollection = current;
if (!upload.isVisible()) {
upload.setVisible(true);
}
if (uploadRootCollection instanceof Observable) {
((Observable) uploadRootCollection).addObserver(upload.getObserver());
}
upload.setTotalSize(FileUtils.sizeQuietly(files));
for (final Path file : files) {
if (upload.isCancelled()) {
break;
}
// should replace the lines above
store(uploadRootCollection, file, upload);
}
if (uploadRootCollection instanceof Observable) {
((Observable) uploadRootCollection).deleteObservers();
}
upload.uploadCompleted();
return true;
}
use of org.xmldb.api.base.Collection 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.xmldb.api.base.Collection in project exist by eXist-db.
the class TriggersDialog method setupComponents.
private void setupComponents() {
// Dialog Content Panel
final GridBagLayout grid = new GridBagLayout();
getContentPane().setLayout(grid);
// Constraints for Layout
final GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(2, 2, 2, 2);
// collection label
final JLabel label = new JLabel(Messages.getString("TriggersDialog.Collection"));
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 1;
c.anchor = GridBagConstraints.WEST;
c.fill = GridBagConstraints.NONE;
grid.setConstraints(label, c);
getContentPane().add(label);
// get the collections but not system collections
final List<PrettyXmldbURI> alCollections = new ArrayList<>();
try {
final Collection root = client.getCollection(XmldbURI.ROOT_COLLECTION);
final List<PrettyXmldbURI> alAllCollections = getCollections(root, new ArrayList<>());
for (PrettyXmldbURI alAllCollection : alAllCollections) {
// TODO : use XmldbURIs !
if (!alAllCollection.toString().contains(XmldbURI.CONFIG_COLLECTION)) {
alCollections.add(alAllCollection);
}
}
} catch (final XMLDBException e) {
ClientFrame.showErrorMessage(e.getMessage(), e);
return;
}
// Create a combobox listing the collections
cmbCollections = new JComboBox(alCollections.toArray());
cmbCollections.addActionListener(e -> {
saveChanges();
final JComboBox cb = (JComboBox) e.getSource();
actionGetTriggers(cb.getSelectedItem().toString());
});
c.gridx = 1;
c.gridy = 0;
c.gridwidth = 1;
c.anchor = GridBagConstraints.WEST;
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1;
grid.setConstraints(cmbCollections, c);
getContentPane().add(cmbCollections);
// Panel to hold controls relating to the Triggers Index
final JPanel panelTriggers = new JPanel();
panelTriggers.setBorder(new TitledBorder(Messages.getString("TriggersDialog.Triggers")));
final GridBagLayout panelTriggersGrid = new GridBagLayout();
panelTriggers.setLayout(panelTriggersGrid);
// Table to hold the Triggers with Sroll bar
triggersModel = new TriggersTableModel();
tblTriggers = new JTable(triggersModel);
tblTriggers.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
tblTriggers.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
// Toolbar with add/delete buttons for Triggers
final Box triggersToolbarBox = Box.createHorizontalBox();
// add button
final JButton btnAddTrigger = new JButton(Messages.getString("TriggersDialog.addbutton"));
btnAddTrigger.addActionListener(e -> actionAddTrigger());
triggersToolbarBox.add(btnAddTrigger);
// delete button
final JButton btnDeleteTrigger = new JButton(Messages.getString("TriggersDialog.deletebutton"));
btnDeleteTrigger.addActionListener(e -> actionDeleteTrigger());
triggersToolbarBox.add(btnDeleteTrigger);
c.gridx = 0;
c.gridy = 4;
c.gridwidth = 2;
c.anchor = GridBagConstraints.CENTER;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0;
c.weighty = 0;
panelTriggersGrid.setConstraints(triggersToolbarBox, c);
panelTriggers.add(triggersToolbarBox);
// add triggers panel to content frame
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 2;
c.anchor = GridBagConstraints.WEST;
c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 1;
grid.setConstraints(panelTriggers, c);
getContentPane().add(panelTriggers);
pack();
}
Aggregations