Search in sources :

Example 41 with Node

use of javax.jcr.Node in project jackrabbit by apache.

the class AdministratorTest method testAdminNodeCollidingWithAuthorizableFolder.

/**
     * Test for collisions that would prevent from recreate the admin user.
     * - an intermediate rep:AuthorizableFolder node with the same name
     */
public void testAdminNodeCollidingWithAuthorizableFolder() throws RepositoryException, NotExecutableException {
    Authorizable admin = userMgr.getAuthorizable(adminId);
    if (admin == null || !(admin instanceof AuthorizableImpl)) {
        throw new NotExecutableException();
    }
    // access the node corresponding to the admin user and remove it
    NodeImpl adminNode = ((AuthorizableImpl) admin).getNode();
    String adminPath = adminNode.getPath();
    String adminNodeName = adminNode.getName();
    Node parentNode = adminNode.getParent();
    Session s = adminNode.getSession();
    adminNode.remove();
    // use session obtained from the node as usermgr may point to a dedicated
    // system workspace different from the superusers workspace.
    s.save();
    Session s2 = null;
    String collidingPath = null;
    try {
        // now create a colliding node:
        Node n = parentNode.addNode(adminNodeName, "rep:AuthorizableFolder");
        collidingPath = n.getPath();
        s.save();
        // force recreation of admin user.
        s2 = getHelper().getSuperuserSession();
        admin = userMgr.getAuthorizable(adminId);
        assertNotNull(admin);
        assertEquals(adminNodeName, ((AuthorizableImpl) admin).getNode().getName());
        assertFalse(adminPath.equals(((AuthorizableImpl) admin).getNode().getPath()));
    } finally {
        if (s2 != null) {
            s2.logout();
        }
        // remove the extra folder and the admin user (created underneath) again.
        if (collidingPath != null) {
            s.getNode(collidingPath).remove();
            s.save();
        }
    }
}
Also used : NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) NodeImpl(org.apache.jackrabbit.core.NodeImpl) Node(javax.jcr.Node) Authorizable(org.apache.jackrabbit.api.security.user.Authorizable) Session(javax.jcr.Session)

Example 42 with Node

use of javax.jcr.Node in project jackrabbit by apache.

the class DefaultHandler method exportProperties.

public boolean exportProperties(PropertyExportContext exportContext, boolean isCollection) throws RepositoryException {
    if (!canExport(exportContext, isCollection)) {
        throw new RepositoryException("PropertyHandler " + getName() + " failed to export properties.");
    }
    Node cn = getContentNode(exportContext, isCollection);
    try {
        // export the properties common with normal I/O handling
        exportProperties(exportContext, isCollection, cn);
        // export all other properties as well
        PropertyIterator it = cn.getProperties();
        while (it.hasNext()) {
            Property p = it.nextProperty();
            String name = p.getName();
            PropertyDefinition def = p.getDefinition();
            if (def.isMultiple() || isDefinedByFilteredNodeType(def)) {
                log.debug("Skip property '" + name + "': not added to webdav property set.");
                continue;
            }
            if (JcrConstants.JCR_DATA.equals(name) || JcrConstants.JCR_MIMETYPE.equals(name) || JcrConstants.JCR_ENCODING.equals(name) || JcrConstants.JCR_LASTMODIFIED.equals(name)) {
                continue;
            }
            DavPropertyName davName = getDavName(name, p.getSession());
            exportContext.setProperty(davName, p.getValue().getString());
        }
        return true;
    } catch (IOException e) {
        // should not occur (log output see 'exportProperties')
        return false;
    }
}
Also used : Node(javax.jcr.Node) PropertyIterator(javax.jcr.PropertyIterator) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) Property(javax.jcr.Property) DavProperty(org.apache.jackrabbit.webdav.property.DavProperty) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition) DavPropertyName(org.apache.jackrabbit.webdav.property.DavPropertyName)

Example 43 with Node

use of javax.jcr.Node in project jackrabbit by apache.

the class DefaultHandler method exportContent.

/**
     * Retrieves the content node that will be used for exporting properties and
     * data and calls the corresponding methods.
     *
     * @param context the export context
     * @param isCollection <code>true</code> if collection
     * @see #exportProperties(ExportContext, boolean, Node)
     * @see #exportData(ExportContext, boolean, Node)
     */
public boolean exportContent(ExportContext context, boolean isCollection) throws IOException {
    if (!canExport(context, isCollection)) {
        throw new IOException(getName() + ": Cannot export " + context.getExportRoot());
    }
    try {
        Node contentNode = getContentNode(context, isCollection);
        exportProperties(context, isCollection, contentNode);
        if (context.hasStream()) {
            exportData(context, isCollection, contentNode);
        }
        // else: missing stream. ignore.
        return true;
    } catch (RepositoryException e) {
        // node must be asserted in the 'canExport' call.
        throw new IOException(e.getMessage());
    }
}
Also used : Node(javax.jcr.Node) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException)

Example 44 with Node

use of javax.jcr.Node in project jackrabbit by apache.

the class DefaultHandler method getContentNode.

/**
     * Retrieves/creates the node that will be used to import properties and
     * data. In case of a non-collection this includes and additional content node
     * to be created beside the 'file' node.<br>
     * Please note: If the jcr:content node already exists and contains child
     * nodes, those will be removed in order to make sure, that the import
     * really replaces the existing content of the file-node.
     *
     * @param context
     * @param isCollection
     * @return
     * @throws RepositoryException
     */
protected Node getContentNode(ImportContext context, boolean isCollection) throws RepositoryException {
    Node parentNode = (Node) context.getImportRoot();
    String name = context.getSystemId();
    if (parentNode.hasNode(name)) {
        parentNode = parentNode.getNode(name);
    } else {
        String ntName = (isCollection) ? getCollectionNodeType() : getNodeType();
        parentNode = parentNode.addNode(name, ntName);
    }
    Node contentNode = null;
    if (isCollection) {
        contentNode = parentNode;
    } else {
        if (parentNode.hasNode(JcrConstants.JCR_CONTENT)) {
            contentNode = parentNode.getNode(JcrConstants.JCR_CONTENT);
            // check if nodetype is compatible (might be update of an existing file)
            if (contentNode.isNodeType(getContentNodeType()) || !forceCompatibleContentNodes()) {
                // includes properties (DefaultHandler) and nodes (e.g. ZipHandler)
                if (contentNode.hasNodes()) {
                    NodeIterator it = contentNode.getNodes();
                    while (it.hasNext()) {
                        it.nextNode().remove();
                    }
                }
            } else {
                contentNode.remove();
                contentNode = null;
            }
        }
        if (contentNode == null) {
            // when the underlying repository allows it to be used
            if (parentNode.getPrimaryNodeType().canAddChildNode(JcrConstants.JCR_CONTENT, getContentNodeType())) {
                contentNode = parentNode.addNode(JcrConstants.JCR_CONTENT, getContentNodeType());
            } else {
                contentNode = parentNode.addNode(JcrConstants.JCR_CONTENT);
            }
        }
    }
    return contentNode;
}
Also used : NodeIterator(javax.jcr.NodeIterator) Node(javax.jcr.Node)

Example 45 with Node

use of javax.jcr.Node in project jackrabbit by apache.

the class DefaultHandler method delete.

/**
     * @see DeleteHandler#delete(DeleteContext, DavResource)
     */
public boolean delete(DeleteContext deleteContext, DavResource member) throws DavException {
    try {
        String itemPath = member.getLocator().getRepositoryPath();
        Item item = deleteContext.getSession().getItem(itemPath);
        if (item instanceof Node) {
            ((Node) item).removeShare();
        } else {
            item.remove();
        }
        deleteContext.getSession().save();
        log.debug("default handler deleted {}", member.getResourcePath());
        return true;
    } catch (RepositoryException e) {
        throw new JcrDavException(e);
    }
}
Also used : Item(javax.jcr.Item) JcrDavException(org.apache.jackrabbit.webdav.jcr.JcrDavException) Node(javax.jcr.Node) RepositoryException(javax.jcr.RepositoryException)

Aggregations

Node (javax.jcr.Node)2620 Session (javax.jcr.Session)645 Test (org.junit.Test)643 RepositoryException (javax.jcr.RepositoryException)317 Property (javax.jcr.Property)251 NodeIterator (javax.jcr.NodeIterator)214 JackrabbitNode (org.apache.jackrabbit.api.JackrabbitNode)182 Value (javax.jcr.Value)158 Version (javax.jcr.version.Version)155 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)151 Query (javax.jcr.query.Query)122 QueryResult (javax.jcr.query.QueryResult)108 Event (javax.jcr.observation.Event)103 VersionManager (javax.jcr.version.VersionManager)97 Resource (org.apache.sling.api.resource.Resource)96 ArrayList (java.util.ArrayList)93 AccessDeniedException (javax.jcr.AccessDeniedException)89 InvalidItemStateException (javax.jcr.InvalidItemStateException)82 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)82 AbstractRepositoryTest (org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)81