Search in sources :

Example 26 with Collection

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

the class ServerShutdown method process.

private static void process(final ParsedArguments arguments) {
    final Properties properties = loadProperties();
    final String user = arguments.get(userArg);
    final String passwd = arguments.get(passwordArg);
    String uri = getOpt(arguments, uriArg).orElseGet(() -> properties.getProperty("uri", "xmldb:exist://localhost:8080/exist/xmlrpc"));
    try {
        // initialize database drivers
        final Class<?> cl = Class.forName("org.exist.xmldb.DatabaseImpl");
        // create the default database
        final Database database = (Database) cl.newInstance();
        DatabaseManager.registerDatabase(database);
        if (!uri.endsWith(XmldbURI.ROOT_COLLECTION)) {
            uri = uri + XmldbURI.ROOT_COLLECTION;
        }
        final Collection root = DatabaseManager.getCollection(uri, user, passwd);
        final DatabaseInstanceManager manager = (DatabaseInstanceManager) root.getService("DatabaseInstanceManager", "1.0");
        System.out.println("Shutting down database instance at ");
        System.out.println('\t' + uri);
        manager.shutdown();
    } catch (final XMLDBException e) {
        System.err.println("ERROR: " + e.getMessage());
        final Throwable t = e.getCause();
        if (t != null && t instanceof XmlRpcException) {
            System.err.println("CAUSE: " + t.getMessage());
        } else {
            e.printStackTrace();
        }
    } catch (final Exception e) {
        e.printStackTrace();
    }
}
Also used : Database(org.xmldb.api.base.Database) Collection(org.xmldb.api.base.Collection) XMLDBException(org.xmldb.api.base.XMLDBException) Properties(java.util.Properties) DatabaseInstanceManager(org.exist.xmldb.DatabaseInstanceManager) XmlRpcException(org.apache.xmlrpc.XmlRpcException) XMLDBException(org.xmldb.api.base.XMLDBException) ArgumentException(se.softhouse.jargo.ArgumentException) IOException(java.io.IOException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) StartException(org.exist.start.StartException)

Example 27 with Collection

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

the class XMLDBAbstractCollectionManipulator method createCollectionPath.

public static final Collection createCollectionPath(final Collection parentColl, final String relPath) throws XMLDBException, XPathException {
    Collection current = parentColl;
    final StringTokenizer tok = new StringTokenizer(new AnyURIValue(relPath).toXmldbURI().toString(), "/");
    while (tok.hasMoreTokens()) {
        final String token = tok.nextToken();
        current = createCollection(current, token);
    }
    return current;
}
Also used : StringTokenizer(java.util.StringTokenizer) AnyURIValue(org.exist.xquery.value.AnyURIValue) InTxnLocalCollection(org.exist.xmldb.txn.bridge.InTxnLocalCollection) LocalCollection(org.exist.xmldb.LocalCollection) Collection(org.xmldb.api.base.Collection)

Example 28 with Collection

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

the class XMLDBAbstractCollectionManipulator method eval.

@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
    if (0 == args.length) {
        throw new XPathException(this, "Expected a collection as the argument " + (paramNumber + 1) + ".");
    }
    final boolean collectionNeedsClose = false;
    Collection collection = null;
    final Item item = args[paramNumber].itemAt(0);
    if (Type.subTypeOf(item.getType(), Type.NODE)) {
        final NodeValue node = (NodeValue) item;
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Found node");
        }
        if (node.getImplementationType() == NodeValue.PERSISTENT_NODE) {
            final org.exist.collections.Collection internalCol = ((NodeProxy) node).getOwnerDocument().getCollection();
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Found node");
            }
            try {
                // TODO: use xmldbURI
                collection = getLocalCollection(context, internalCol.getURI().toString());
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("Loaded collection {}", collection.getName());
                }
            } catch (final XMLDBException e) {
                throw new XPathException(this, "Failed to access collection: " + internalCol.getURI(), e);
            }
        } else {
            return Sequence.EMPTY_SEQUENCE;
        }
    }
    if (collection == null) {
        // Otherwise, just extract the name as a string:
        final String collectionURI = args[paramNumber].getStringValue();
        if (collectionURI != null) {
            try {
                collection = getCollection(context, collectionURI, Optional.empty(), Optional.empty());
            } catch (final XMLDBException xe) {
                if (errorIfAbsent) {
                    throw new XPathException(this, "Could not locate collection: " + collectionURI, xe);
                }
                collection = null;
            }
        }
        if (collection == null && errorIfAbsent) {
            throw new XPathException(this, "Unable to find collection: " + collectionURI);
        }
    }
    Sequence s = Sequence.EMPTY_SEQUENCE;
    try {
        s = evalWithCollection(collection, args, contextSequence);
    } finally {
        if (collectionNeedsClose && collection != null) {
            try {
                collection.close();
            } catch (final Exception e) {
                throw new XPathException(this, "Unable to close collection", e);
            }
        }
    }
    return s;
}
Also used : Item(org.exist.xquery.value.Item) NodeValue(org.exist.xquery.value.NodeValue) XPathException(org.exist.xquery.XPathException) InTxnLocalCollection(org.exist.xmldb.txn.bridge.InTxnLocalCollection) LocalCollection(org.exist.xmldb.LocalCollection) Collection(org.xmldb.api.base.Collection) XMLDBException(org.xmldb.api.base.XMLDBException) Sequence(org.exist.xquery.value.Sequence) XMLDBException(org.xmldb.api.base.XMLDBException) XPathException(org.exist.xquery.XPathException)

Example 29 with Collection

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

the class XmldbApiSecurityTest method createXmlResource.

@Override
protected void createXmlResource(String resourceUri, String content, String uid, String pwd) throws ApiException {
    Collection col = null;
    try {
        col = DatabaseManager.getCollection(getBaseUri() + getCollectionUri(resourceUri), uid, pwd);
        Resource resource = col.createResource(getResourceName(resourceUri), XMLResource.RESOURCE_TYPE);
        resource.setContent(content);
        col.storeResource(resource);
    } catch (final XMLDBException xmldbe) {
        throw new ApiException(xmldbe);
    } finally {
        if (col != null) {
            try {
                col.close();
            } catch (final XMLDBException xmldbe) {
                throw new ApiException(xmldbe);
            }
        }
    }
}
Also used : BinaryResource(org.xmldb.api.modules.BinaryResource) 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 30 with Collection

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

the class XMLDBSecurityTest method ownerChownGidResource.

/**
 * Owner can NOT change the owner gid of a resource
 * to a group of which they are not a member
 *
 * As the user 'test1' attempt to change the
 * ownership gid of /db/securityTest1/test.xml
 * to 'guest' group
 */
@Test(expected = XMLDBException.class)
public void ownerChownGidResource() throws XMLDBException {
    final Collection test = DatabaseManager.getCollection(getBaseUri() + "/db/securityTest1", "test1", "test1");
    final Resource resource = test.getResource("test.xml");
    final UserManagementService ums = (UserManagementService) test.getService("UserManagementService", "1.0");
    // attempt to change gid ownership of /db/securityTest1/test.xml to the guest group
    ums.chgrp(resource, "guest");
}
Also used : 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)

Aggregations

Collection (org.xmldb.api.base.Collection)345 XMLResource (org.xmldb.api.modules.XMLResource)140 Test (org.junit.Test)115 Resource (org.xmldb.api.base.Resource)111 UserManagementService (org.exist.xmldb.UserManagementService)91 CollectionManagementService (org.xmldb.api.modules.CollectionManagementService)85 BinaryResource (org.xmldb.api.modules.BinaryResource)80 XMLDBException (org.xmldb.api.base.XMLDBException)68 ResourceSet (org.xmldb.api.base.ResourceSet)55 EXistCollectionManagementService (org.exist.xmldb.EXistCollectionManagementService)48 XPathQueryService (org.xmldb.api.modules.XPathQueryService)31 EXistResource (org.exist.xmldb.EXistResource)25 EXistXPathQueryService (org.exist.xmldb.EXistXPathQueryService)20 Before (org.junit.Before)20 URISyntaxException (java.net.URISyntaxException)18 Path (java.nio.file.Path)18 InputStream (java.io.InputStream)17 BuildException (org.apache.tools.ant.BuildException)14 XmldbURI (org.exist.xmldb.XmldbURI)13 Account (org.exist.security.Account)10