Search in sources :

Example 96 with Resource

use of org.xmldb.api.base.Resource in project exist by eXist-db.

the class ClientFrame method removeAction.

private void removeAction(final ActionEvent ev) {
    final ResourceDescriptor[] res = getSelectedResources();
    final Collection removeRootCollection = client.current;
    // String cmd;
    if (JOptionPane.showConfirmDialog(this, // $NON-NLS-1$ //$NON-NLS-2$
    Messages.getString("ClientFrame.104") + Messages.getString("ClientFrame.105"), Messages.getString("ClientFrame.106"), JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
        // $NON-NLS-1$
        final Runnable removeTask = () -> {
            // $NON-NLS-1$ //$NON-NLS-2$
            final ProgressMonitor monitor = new ProgressMonitor(ClientFrame.this, Messages.getString("ClientFrame.107"), Messages.getString("ClientFrame.108"), 1, res.length);
            monitor.setMillisToDecideToPopup(500);
            monitor.setMillisToPopup(500);
            for (int i = 0; i < res.length; i++) {
                final ResourceDescriptor resource = res[i];
                if (resource.isCollection()) {
                    try {
                        final EXistCollectionManagementService mgtService = (EXistCollectionManagementService) removeRootCollection.getService(// $NON-NLS-1$
                        "CollectionManagementService", // $NON-NLS-1$
                        "1.0");
                        mgtService.removeCollection(resource.getName());
                    } catch (final XMLDBException e) {
                        showErrorMessage(e.getMessage(), e);
                    }
                } else {
                    try {
                        final Resource res1 = removeRootCollection.getResource(resource.getName().toString());
                        removeRootCollection.removeResource(res1);
                    } catch (final XMLDBException e) {
                        showErrorMessage(e.getMessage(), e);
                    }
                }
                monitor.setProgress(i + 1);
                if (monitor.isCanceled()) {
                    return;
                }
            }
            try {
                removeRootCollection.close();
            } catch (final XMLDBException e) {
                showErrorMessage(e.getMessage(), e);
            }
            try {
                client.getResources();
            } catch (final XMLDBException e) {
                showErrorMessage(e.getMessage(), e);
            }
        };
        client.newClientThread("remove", removeTask).start();
    }
}
Also used : XMLResource(org.xmldb.api.modules.XMLResource) Resource(org.xmldb.api.base.Resource) Collection(org.xmldb.api.base.Collection) XMLDBException(org.xmldb.api.base.XMLDBException)

Example 97 with Resource

use of org.xmldb.api.base.Resource in project exist by eXist-db.

the class ClientFrame method setPermAction.

private void setPermAction(final ActionEvent ev) throws PermissionDeniedException {
    if (fileman.getSelectedRowCount() == 0) {
        return;
    }
    try {
        final Collection collection = client.getCollection();
        final UserManagementService service = getUserManagementService();
        String name = null;
        String created = null;
        String modified = null;
        String size = null;
        String messageDigestType = null;
        String messageDigestValue = null;
        String mimeType = null;
        String owner = null;
        String group = null;
        ModeDisplay mode = null;
        SimpleACLPermissionAider acl = null;
        final DateFormat dateTimeFormat = DateFormat.getDateTimeInstance();
        final List<ResourceDescriptor> selected = new ArrayList<>();
        boolean firstPerm = true;
        for (final int row : fileman.getSelectedRows()) {
            final ResourceDescriptor selectedRow = resources.getRow(row);
            selected.add(selectedRow);
            final XmldbURI thisName = selectedRow.getName();
            final String thisCreated;
            final String thisModified;
            final String thisMessageDigestType;
            final String thisMessageDigestValue;
            final String thisSize;
            final String thisMimeType;
            final Permission thisPerm;
            if (selectedRow.isCollection()) {
                final Collection coll = collection.getChildCollection(thisName.toString());
                thisCreated = dateTimeFormat.format(((EXistCollection) coll).getCreationTime());
                thisModified = NON_APPLICABLE;
                thisMimeType = COLLECTION_MIME_TYPE;
                thisMessageDigestType = NON_APPLICABLE;
                thisMessageDigestValue = NON_APPLICABLE;
                thisSize = NON_APPLICABLE;
                thisPerm = service.getPermissions(coll);
            } else {
                final Resource res = collection.getResource(thisName.toString());
                thisCreated = dateTimeFormat.format(((EXistResource) res).getCreationTime());
                thisModified = dateTimeFormat.format(((EXistResource) res).getLastModificationTime());
                thisMimeType = ((EXistResource) res).getMimeType();
                if (res instanceof EXistBinaryResource) {
                    final MessageDigest messageDigest = ((EXistBinaryResource) res).getContentDigest(DigestType.BLAKE_256);
                    thisMessageDigestType = messageDigest.getDigestType().getCommonNames()[0];
                    thisMessageDigestValue = messageDigest.toHexString();
                    thisSize = humanSize(((EXistBinaryResource) res).getContentLength());
                } else {
                    thisMessageDigestType = NON_APPLICABLE;
                    thisMessageDigestValue = NON_APPLICABLE;
                    thisSize = NON_APPLICABLE;
                }
                thisPerm = service.getPermissions(res);
            }
            name = getUpdated(name, () -> URIUtils.urlDecodeUtf8(thisName));
            created = getUpdated(created, thisCreated);
            modified = getUpdated(modified, thisModified);
            mimeType = getUpdated(mimeType, thisMimeType);
            messageDigestType = getUpdated(messageDigestType, thisMessageDigestType);
            messageDigestValue = getUpdated(messageDigestValue, thisMessageDigestValue);
            size = getUpdated(size, thisSize);
            owner = getUpdated(owner, () -> thisPerm.getOwner().getName());
            group = getUpdated(group, () -> thisPerm.getGroup().getName());
            mode = getUpdatedMode(mode, thisPerm);
            if (firstPerm) {
                if (thisPerm instanceof ACLPermission) {
                    final ACLPermission thisAcl = (ACLPermission) thisPerm;
                    acl = new SimpleACLPermissionAider();
                    for (int i = 0; i < thisAcl.getACECount(); i++) {
                        acl.addACE(thisAcl.getACEAccessType(i), thisAcl.getACETarget(i), thisAcl.getACEWho(i), thisAcl.getACEMode(i));
                    }
                } else {
                    acl = null;
                }
                firstPerm = false;
            } else {
                if (acl != null && thisPerm instanceof ACLPermission) {
                    final ACLPermission thisAcl = (ACLPermission) thisPerm;
                    if (!acl.aclEquals(thisAcl)) {
                        acl = null;
                    }
                }
            }
        }
        final EditPropertiesDialog editPropertiesDialog = new EditPropertiesDialog(service, client.getProperties().getProperty(InteractiveClient.USER), collection, name, mimeType, created, modified, size, messageDigestType, messageDigestValue, owner, group, mode, acl, selected);
        editPropertiesDialog.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosed(final WindowEvent e) {
                try {
                    client.reloadCollection();
                } catch (final XMLDBException xmldbe) {
                    // $NON-NLS-1$
                    showErrorMessage(Messages.getString("ClientFrame.197") + xmldbe.getMessage(), xmldbe);
                    xmldbe.printStackTrace();
                }
            }
        });
        editPropertiesDialog.setVisible(true);
    } catch (final XMLDBException e) {
        // $NON-NLS-1$
        showErrorMessage(Messages.getString("ClientFrame.197") + e.getMessage(), e);
        e.printStackTrace();
    }
}
Also used : SimpleACLPermissionAider(org.exist.security.internal.aider.SimpleACLPermissionAider) XMLResource(org.xmldb.api.modules.XMLResource) Resource(org.xmldb.api.base.Resource) EditPropertiesDialog(org.exist.client.security.EditPropertiesDialog) XMLDBException(org.xmldb.api.base.XMLDBException) ModeDisplay(org.exist.client.security.ModeDisplay) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) Collection(org.xmldb.api.base.Collection) MessageDigest(org.exist.util.crypto.digest.MessageDigest)

Example 98 with Resource

use of org.xmldb.api.base.Resource in project exist by eXist-db.

the class EditPropertiesDialog method btnSaveActionPerformed.

// </editor-fold>//GEN-END:initComponents
private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {
    try {
        for (final ResourceDescriptor desc : applyTo) {
            final String newOwner;
            if (MULTIPLE_INDICATOR.equals(lblOwnerValue.getText()) || desc.getOwner().equals(lblOwnerValue.getText())) {
                newOwner = desc.getOwner();
            } else {
                newOwner = lblOwnerValue.getText();
            }
            final String newGroup;
            if (MULTIPLE_INDICATOR.equals(lblGroupValue.getText()) || desc.getGroup().equals(lblGroupValue.getText())) {
                newGroup = desc.getGroup();
            } else {
                newGroup = lblGroupValue.getText();
            }
            final Permission existingPermission = desc.getPermissions();
            final ModeDisplay modeChanges = getBasicPermissionsTableModel().getMode();
            final Permission updatedPermission = getUpdatedPermission(existingPermission, modeChanges);
            final List<ACEAider> dlgAces = new ArrayList<>();
            if (acl == null) {
                if (existingPermission instanceof ACLPermission) {
                    final ACLPermission existingAclPermission = (ACLPermission) existingPermission;
                    for (int i = 0; i < existingAclPermission.getACECount(); i++) {
                        dlgAces.add(new ACEAider(existingAclPermission.getACEAccessType(i), existingAclPermission.getACETarget(i), existingAclPermission.getACEWho(i), existingAclPermission.getACEMode(i)));
                    }
                }
            } else {
                for (int i = 0; i < tblAcl.getRowCount(); i++) {
                    final ACLPermission.ACE_TARGET target = ACLPermission.ACE_TARGET.valueOf((String) getAclTableModel().getValueAt(i, 0));
                    final String who = (String) getAclTableModel().getValueAt(i, 1);
                    final ACLPermission.ACE_ACCESS_TYPE access = ACLPermission.ACE_ACCESS_TYPE.valueOf((String) getAclTableModel().getValueAt(i, 2));
                    int mode = 0;
                    if ((Boolean) tblAcl.getValueAt(i, 3)) {
                        mode |= Permission.READ;
                    }
                    if ((Boolean) tblAcl.getValueAt(i, 4)) {
                        mode |= Permission.WRITE;
                    }
                    if ((Boolean) tblAcl.getValueAt(i, 5)) {
                        mode |= Permission.EXECUTE;
                    }
                    dlgAces.add(new ACEAider(access, target, who, mode));
                }
            }
            if (desc.isCollection()) {
                final Collection coll = parent.getChildCollection(desc.getName().toString());
                getUserManagementService().setPermissions(coll, newOwner, newGroup, updatedPermission.getMode(), dlgAces);
            } else {
                final Resource res = parent.getResource(desc.getName().toString());
                getUserManagementService().setPermissions(res, newOwner, newGroup, updatedPermission.getMode(), dlgAces);
            }
        }
        setVisible(false);
        dispose();
    } catch (final PermissionDeniedException | XMLDBException e) {
        JOptionPane.showMessageDialog(this, "Could not update properties: " + e.getMessage(), ERROR_TITLE, JOptionPane.ERROR_MESSAGE);
    }
}
Also used : ACLPermission(org.exist.security.ACLPermission) ArrayList(java.util.ArrayList) Resource(org.xmldb.api.base.Resource) XMLDBException(org.xmldb.api.base.XMLDBException) ACEAider(org.exist.security.internal.aider.ACEAider) ACLPermission(org.exist.security.ACLPermission) Permission(org.exist.security.Permission) Collection(org.xmldb.api.base.Collection) PermissionDeniedException(org.exist.security.PermissionDeniedException)

Example 99 with Resource

use of org.xmldb.api.base.Resource in project exist by eXist-db.

the class XMLDBSecurityTest method copyDocument_doesNotPreservePermissions.

@Test
public void copyDocument_doesNotPreservePermissions() throws XMLDBException {
    Collection test = DatabaseManager.getCollection(getBaseUri() + "/db/securityTest3", "test1", "test1");
    EXistCollectionManagementService cms = (EXistCollectionManagementService) test.getService("CollectionManagementService", "1.0");
    // create resource owned by "test1", and group "users" in /db/securityTest3
    final Resource resSource = test.createResource("source.xml", XMLResource.RESOURCE_TYPE);
    resSource.setContent("<test/>");
    test.storeResource(resSource);
    // as the 'test3' user copy the resource
    test = DatabaseManager.getCollection(getBaseUri() + "/db/securityTest3", "test3", "test3");
    cms = (EXistCollectionManagementService) test.getService("CollectionManagementService", "1.0");
    cms.copyResource("/db/securityTest3/source.xml", "/db/securityTest3", "copy-of-source.xml");
    final UserManagementService ums = (UserManagementService) test.getService("UserManagementService", "1.0");
    final Permission permissions = ums.getPermissions(test.getResource("copy-of-source.xml"));
    // resource should be owned by test3:guest, i.e. permissions were not preserved from the test1 users doc /db/securityTest3/source.xml
    assertEquals("test3", permissions.getOwner().getName());
    assertEquals("guest", permissions.getGroup().getName());
}
Also used : EXistCollectionManagementService(org.exist.xmldb.EXistCollectionManagementService) BinaryResource(org.xmldb.api.modules.BinaryResource) XMLResource(org.xmldb.api.modules.XMLResource) Resource(org.xmldb.api.base.Resource) Collection(org.xmldb.api.base.Collection) UserManagementService(org.exist.xmldb.UserManagementService) Test(org.junit.Test)

Example 100 with Resource

use of org.xmldb.api.base.Resource in project exist by eXist-db.

the class XMLDBSecurityTest method copyCollectionWithResources_destExists_destIsNotWritable.

/**
 * As the 'test1' user, creates the collection and resource:
 *
 *  test1:users /db/securityTest3/source
 *  test1:users /db/securityTest3/source/source1.xml
 *  test1:users /db/securityTest3/source/source2.xml
 *
 * We then also create the Collection
 *  test1:users /db/securityTest3/copy-of-source (0755)
 * so that the destination (for the copy we are about
 * to do) already exists and is NOT writable...
 *
 * As the 'test3' user, copy the collection:
 *
 *  /db/securityTest3/source
 *      -> /db/securityTest3/copy-of-source
 */
@Test(expected = XMLDBException.class)
public void copyCollectionWithResources_destExists_destIsNotWritable() throws XMLDBException {
    Collection test = DatabaseManager.getCollection(getBaseUri() + "/db/securityTest3", "test1", "test1");
    EXistCollectionManagementService cms = (EXistCollectionManagementService) test.getService("CollectionManagementService", "1.0");
    // create collection owned by "test1", and group "users" in /db/securityTest3
    Collection source = cms.createCollection("source");
    // create resource owned by "test1", and group "users" in /db/securityTest3/source
    Resource resSource = source.createResource("source1.xml", XMLResource.RESOURCE_TYPE);
    resSource.setContent("<test/>");
    source.storeResource(resSource);
    resSource = source.createResource("source2.xml", XMLResource.RESOURCE_TYPE);
    resSource.setContent("<test/>");
    source.storeResource(resSource);
    // pre-create the destination with default mode (0755)
    // so that it is not writable by 'test3' user
    final Collection dest = cms.createCollection("copy-of-source");
    // as the 'test3' user copy the collection
    test = DatabaseManager.getCollection(getBaseUri() + "/db/securityTest3", "test3", "test3");
    cms = (EXistCollectionManagementService) test.getService("CollectionManagementService", "1.0");
    cms.copy("/db/securityTest3/source", "/db/securityTest3", "copy-of-source");
}
Also used : EXistCollectionManagementService(org.exist.xmldb.EXistCollectionManagementService) BinaryResource(org.xmldb.api.modules.BinaryResource) XMLResource(org.xmldb.api.modules.XMLResource) Resource(org.xmldb.api.base.Resource) Collection(org.xmldb.api.base.Collection) Test(org.junit.Test)

Aggregations

Resource (org.xmldb.api.base.Resource)173 XMLResource (org.xmldb.api.modules.XMLResource)126 Collection (org.xmldb.api.base.Collection)111 BinaryResource (org.xmldb.api.modules.BinaryResource)86 Test (org.junit.Test)77 UserManagementService (org.exist.xmldb.UserManagementService)52 ResourceSet (org.xmldb.api.base.ResourceSet)46 XMLDBException (org.xmldb.api.base.XMLDBException)38 EXistCollectionManagementService (org.exist.xmldb.EXistCollectionManagementService)32 EXistResource (org.exist.xmldb.EXistResource)27 CollectionManagementService (org.xmldb.api.modules.CollectionManagementService)25 XPathQueryService (org.xmldb.api.modules.XPathQueryService)18 EXistXPathQueryService (org.exist.xmldb.EXistXPathQueryService)16 Path (java.nio.file.Path)11 Database (org.xmldb.api.base.Database)11 XPathException (org.exist.xquery.XPathException)10 InputStream (java.io.InputStream)9 Source (javax.xml.transform.Source)9 BuildException (org.apache.tools.ant.BuildException)9 Diff (org.xmlunit.diff.Diff)9