Search in sources :

Example 76 with Item

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

the class DavResourceFactoryImpl method createResourceForItem.

/**
     * Tries to retrieve the repository item defined by the locator's resource
     * path and build the corresponding WebDAV resource. The following distinction
     * is made between items: Version nodes, VersionHistory nodes, root node,
     * unspecified nodes and finally property items.
     *
     * @param locator
     * @param sessionImpl
     * @return DavResource representing a repository item.
     * @throws RepositoryException if {@link javax.jcr.Session#getItem(String)} fails.
     */
private DavResource createResourceForItem(DavResourceLocator locator, JcrDavSession sessionImpl) throws RepositoryException, DavException {
    DavResource resource;
    Item item = getItem(sessionImpl, locator);
    if (item.isNode()) {
        // create special resources for Version and VersionHistory
        if (item instanceof Version) {
            resource = new VersionItemCollection(locator, sessionImpl, this, item);
        } else if (item instanceof VersionHistory) {
            resource = new VersionHistoryItemCollection(locator, sessionImpl, this, item);
        } else {
            resource = new VersionControlledItemCollection(locator, sessionImpl, this, item);
        }
    } else {
        resource = new DefaultItemResource(locator, sessionImpl, this, item);
    }
    return resource;
}
Also used : Item(javax.jcr.Item) VersionHistoryItemCollection(org.apache.jackrabbit.webdav.jcr.version.VersionHistoryItemCollection) DavResource(org.apache.jackrabbit.webdav.DavResource) Version(javax.jcr.version.Version) VersionItemCollection(org.apache.jackrabbit.webdav.jcr.version.VersionItemCollection) VersionHistory(javax.jcr.version.VersionHistory)

Example 77 with Item

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

the class JsonDiffHandler method setProperty.

/**
     * @see DiffHandler#setProperty(String, String) 
     */
@Override
public void setProperty(String targetPath, String diffValue) throws DiffException {
    try {
        String itemPath = getItemPath(targetPath);
        Item item = session.getItem(Text.getRelativeParent(itemPath, 1));
        if (!item.isNode()) {
            throw new DiffException("No such node " + itemPath, new ItemNotFoundException(itemPath));
        }
        Node parent = (Node) item;
        String propName = Text.getName(itemPath);
        if (JcrConstants.JCR_MIXINTYPES.equals(propName)) {
            setMixins(parent, extractValuesFromRequest(targetPath));
        } else if (JcrConstants.JCR_PRIMARYTYPE.equals(propName)) {
            setPrimaryType(parent, extractValuesFromRequest(targetPath));
        } else {
            if (diffValue == null || diffValue.length() == 0) {
                // single valued property with value present in multipart.
                Value[] vs = extractValuesFromRequest(targetPath);
                if (vs.length == 0) {
                    if (parent.hasProperty(propName)) {
                        // avoid problems with single vs. multi valued props.
                        parent.getProperty(propName).remove();
                    } else {
                        // property does not exist -> stick to rule that missing
                        // [] indicates single valued.
                        parent.setProperty(propName, (Value) null);
                    }
                } else if (vs.length == 1) {
                    parent.setProperty(propName, vs[0]);
                } else {
                    throw new DiffException("Unexpected number of values in multipart. Was " + vs.length + " but expected 1.");
                }
            } else if (diffValue.startsWith("[") && diffValue.endsWith("]")) {
                // multivalued property
                if (diffValue.length() == 2) {
                    // empty array OR values in multipart
                    Value[] vs = extractValuesFromRequest(targetPath);
                    parent.setProperty(propName, vs);
                } else {
                    // json array
                    Value[] vs = extractValues(diffValue);
                    parent.setProperty(propName, vs);
                }
            } else {
                // single prop value included in the diff
                Value v = extractValue(diffValue);
                parent.setProperty(propName, v);
            }
        }
    } catch (RepositoryException e) {
        throw new DiffException(e.getMessage(), e);
    } catch (IOException e) {
        if (e instanceof DiffException) {
            throw (DiffException) e;
        } else {
            throw new DiffException(e.getMessage(), e);
        }
    }
}
Also used : Item(javax.jcr.Item) Node(javax.jcr.Node) Value(javax.jcr.Value) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Example 78 with Item

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

the class JsonDiffHandler method addNode.

private void addNode(String parentPath, String nodeName, String diffValue) throws DiffException, RepositoryException {
    Item item = session.getItem(parentPath);
    if (!item.isNode()) {
        throw new ItemNotFoundException(parentPath);
    }
    Node parent = (Node) item;
    try {
        NodeHandler hndlr = new NodeHandler(parent, nodeName);
        new JsonParser(hndlr).parse(diffValue);
    } catch (IOException e) {
        if (e instanceof DiffException) {
            throw (DiffException) e;
        } else {
            throw new DiffException(e.getMessage(), e);
        }
    }
}
Also used : Item(javax.jcr.Item) Node(javax.jcr.Node) IOException(java.io.IOException) ItemNotFoundException(javax.jcr.ItemNotFoundException) JsonParser(org.apache.jackrabbit.commons.json.JsonParser)

Example 79 with Item

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

the class JsonDiffHandler method move.

/**
     * @see DiffHandler#move(String, String) 
     */
@Override
public void move(String targetPath, String diffValue) throws DiffException {
    if (diffValue == null || diffValue.length() == 0) {
        throw new DiffException("Invalid 'move' value '" + diffValue + "'");
    }
    try {
        String srcPath = getItemPath(targetPath);
        String orderPosition = getOrderPosition(diffValue);
        if (orderPosition == null) {
            // simple move
            String destPath = getItemPath(diffValue);
            session.move(srcPath, destPath);
        } else {
            String srcName = Text.getName(srcPath);
            int pos = diffValue.lastIndexOf('#');
            String destName = (pos == 0) ? null : Text.getName(diffValue.substring(0, pos));
            Item item = session.getItem(Text.getRelativeParent(srcPath, 1));
            if (!item.isNode()) {
                throw new ItemNotFoundException(srcPath);
            }
            Node parent = (Node) item;
            if (ORDER_POSITION_FIRST.equals(orderPosition)) {
                if (destName != null) {
                    throw new DiffException(ORDER_POSITION_FIRST + " may not have a leading destination.");
                }
                destName = Text.getName(parent.getNodes().nextNode().getPath());
                parent.orderBefore(srcName, destName);
            } else if (ORDER_POSITION_LAST.equals(orderPosition)) {
                if (destName != null) {
                    throw new DiffException(ORDER_POSITION_LAST + " may not have a leading destination.");
                }
                parent.orderBefore(srcName, null);
            } else if (ORDER_POSITION_AFTER.equals(orderPosition)) {
                if (destName == null) {
                    throw new DiffException(ORDER_POSITION_AFTER + " must have a leading destination.");
                }
                for (NodeIterator it = parent.getNodes(); it.hasNext(); ) {
                    Node child = it.nextNode();
                    if (destName.equals(child.getName())) {
                        if (it.hasNext()) {
                            destName = Text.getName(it.nextNode().getName());
                        } else {
                            destName = null;
                        }
                        break;
                    }
                }
                // reorder... if no child node matches the original destName
                // the reorder will fail. no extra check.
                parent.orderBefore(srcName, destName);
            } else {
                // standard jcr reorder (before)
                parent.orderBefore(srcName, destName);
            }
        }
    } catch (RepositoryException e) {
        throw new DiffException(e.getMessage(), e);
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) Item(javax.jcr.Item) Node(javax.jcr.Node) RepositoryException(javax.jcr.RepositoryException) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Example 80 with Item

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

the class DefaultHandler method canExport.

/**
     * Returns true if the export root is a node and if it contains a child node
     * with name {@link JcrConstants#JCR_CONTENT jcr:content} in case this
     * export is not intended for a collection.
     *
     * @return true if the export root is a node. If the specified boolean parameter
     * is false (not a collection export) the given export root must contain a
     * child node with name {@link JcrConstants#JCR_CONTENT jcr:content}.
     *
     * @see IOHandler#canExport(ExportContext, boolean)
     */
public boolean canExport(ExportContext context, boolean isCollection) {
    if (context == null || context.isCompleted()) {
        return false;
    }
    Item exportRoot = context.getExportRoot();
    boolean success = exportRoot != null && exportRoot.isNode();
    if (success && !isCollection) {
        try {
            Node n = ((Node) exportRoot);
            success = n.hasNode(JcrConstants.JCR_CONTENT);
        } catch (RepositoryException e) {
            // should never occur.
            success = false;
        }
    }
    return success;
}
Also used : Item(javax.jcr.Item) Node(javax.jcr.Node) RepositoryException(javax.jcr.RepositoryException)

Aggregations

Item (javax.jcr.Item)117 Node (javax.jcr.Node)48 RepositoryException (javax.jcr.RepositoryException)28 Property (javax.jcr.Property)19 Session (javax.jcr.Session)15 PathNotFoundException (javax.jcr.PathNotFoundException)11 ArrayList (java.util.ArrayList)7 ItemNotFoundException (javax.jcr.ItemNotFoundException)6 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)6 IOException (java.io.IOException)5 JackrabbitSession (org.apache.jackrabbit.api.JackrabbitSession)5 HashSet (java.util.HashSet)4 NodeIterator (javax.jcr.NodeIterator)4 Principal (java.security.Principal)3 NoSuchElementException (java.util.NoSuchElementException)3 AccessDeniedException (javax.jcr.AccessDeniedException)3 PropertyIterator (javax.jcr.PropertyIterator)3 Version (javax.jcr.version.Version)3 ResourceNotFoundException (org.apache.sling.api.resource.ResourceNotFoundException)3 OutputStreamWriter (java.io.OutputStreamWriter)2