Search in sources :

Example 11 with ItemNotFoundException

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

the class RemoveOrphanVersionHistoryTest method testWorkspaceRemoveOrphanVersionHistory.

/**
     * Test orphan version history cleaning in multiple workspace.
     * @throws RepositoryException if an error occurs.
     */
public void testWorkspaceRemoveOrphanVersionHistory() throws RepositoryException {
    Node n = testRootNode.addNode(nodeName1);
    n.addMixin(mixVersionable);
    testRootNode.save();
    Session session = n.getSession();
    VersionHistory vh = n.getVersionHistory();
    String vhUuid = vh.getUUID();
    assertExists(session, vhUuid);
    // First version
    Version v10 = n.checkin();
    n.checkout();
    Workspace defaultWorkspace = n.getSession().getWorkspace();
    Session otherWsSession = n.getSession().getRepository().login(new SimpleCredentials("superuser", "".toCharArray()), workspaceName);
    // Clone the node in another workspace
    otherWsSession.getWorkspace().clone(defaultWorkspace.getName(), n.getPath(), n.getPath(), false);
    Node otherWsRootNode = otherWsSession.getRootNode();
    Node clonedNode = otherWsRootNode.getNode(n.getPath().substring(1));
    // Ensure that version histories are the same
    assertEquals(vhUuid, clonedNode.getVersionHistory().getUUID());
    Version v11 = clonedNode.checkin();
    clonedNode.checkout();
    // Remove node
    n.remove();
    testRootNode.save();
    assertExists(session, vhUuid);
    assertExists(otherWsSession, vhUuid);
    // Remove the first version
    vh.removeVersion(v10.getName());
    assertExists(session, vhUuid);
    assertExists(otherWsSession, vhUuid);
    // Remove cloned node
    clonedNode.remove();
    otherWsRootNode.save();
    assertExists(session, vhUuid);
    assertExists(otherWsSession, vhUuid);
    // Remove the last version
    vh.removeVersion(v11.getName());
    try {
        session.getNodeByUUID(vhUuid);
        fail("Orphan version history must have been removed from the default workspace");
    } catch (ItemNotFoundException e) {
    // Expected
    }
    try {
        otherWsSession.getNodeByUUID(vhUuid);
        fail("Orphan version history must have been removed from the other workspace");
    } catch (ItemNotFoundException e) {
    // Expected
    }
}
Also used : SimpleCredentials(javax.jcr.SimpleCredentials) Version(javax.jcr.version.Version) Node(javax.jcr.Node) VersionHistory(javax.jcr.version.VersionHistory) Session(javax.jcr.Session) Workspace(javax.jcr.Workspace) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Example 12 with ItemNotFoundException

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

the class NamePropertyTest method testGetNode.

/**
     * Since JCR 2.0 a path property can be dereferenced if it points to a
     * Node.
     * TODO: create several tests out of this one
     */
public void testGetNode() throws RepositoryException {
    if (!multiple) {
        String path = prop.getString();
        if (prop.getParent().hasNode(path)) {
            Node n = prop.getNode();
            assertEquals("The name of the dereferenced property must be equal to the value", path, n.getName());
        } else {
            try {
                prop.getNode();
                fail("Calling Property.getNode() for a NAME value that doesn't have a corresponding Node, ItemNotFoundException is expected");
            } catch (ItemNotFoundException e) {
            // success.
            }
        }
    } else {
        try {
            prop.getNode();
            fail("Property.getNode() called on a multivalue property " + "should throw a ValueFormatException.");
        } catch (ValueFormatException vfe) {
        //ok
        }
    }
}
Also used : Node(javax.jcr.Node) ValueFormatException(javax.jcr.ValueFormatException) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Example 13 with ItemNotFoundException

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

the class Move method create.

//------------------------------------------------------------< Factory >---
public static Operation create(Path srcPath, Path destPath, HierarchyManager hierMgr, PathResolver resolver, boolean sessionMove) throws ItemExistsException, NoSuchNodeTypeException, RepositoryException {
    // src must not be ancestor of destination
    if (srcPath.isAncestorOf(destPath)) {
        String msg = "Invalid destination path: cannot be descendant of source path (" + LogUtil.safeGetJCRPath(destPath, resolver) + "," + LogUtil.safeGetJCRPath(srcPath, resolver) + ")";
        log.debug(msg);
        throw new RepositoryException(msg);
    }
    // destination must not contain an index
    int index = destPath.getIndex();
    if (index != Path.INDEX_UNDEFINED) {
        // subscript in name element
        String msg = "Invalid destination path: subscript in name element is not allowed (" + LogUtil.safeGetJCRPath(destPath, resolver) + ")";
        log.debug(msg);
        throw new RepositoryException(msg);
    }
    // root node cannot be moved:
    if (srcPath.denotesRoot() || destPath.denotesRoot()) {
        String msg = "Cannot move the root node.";
        log.debug(msg);
        throw new RepositoryException(msg);
    }
    NodeState srcState = getNodeState(srcPath, hierMgr);
    NodeState srcParentState = getNodeState(srcPath.getAncestor(1), hierMgr);
    NodeState destParentState = getNodeState(destPath.getAncestor(1), hierMgr);
    Name destName = destPath.getName();
    if (sessionMove) {
        NodeEntry destEntry = (NodeEntry) destParentState.getHierarchyEntry();
        // force child node entries list to be present before the move is executed
        // on the hierarchy entry.
        assertChildNodeEntries(srcParentState);
        assertChildNodeEntries(destParentState);
        if (destEntry.hasNodeEntry(destName)) {
            NodeEntry existing = destEntry.getNodeEntry(destName, Path.INDEX_DEFAULT);
            if (existing != null && sessionMove) {
                try {
                    if (!existing.getNodeState().getDefinition().allowsSameNameSiblings()) {
                        throw new ItemExistsException("Node existing at move destination does not allow same name siblings.");
                    }
                } catch (ItemNotFoundException e) {
                // existing apparent not valid any more -> probably no conflict
                }
            }
        }
    }
    Move move = new Move(srcState, srcParentState, destParentState, destName, sessionMove);
    return move;
}
Also used : NodeState(org.apache.jackrabbit.jcr2spi.state.NodeState) NodeEntry(org.apache.jackrabbit.jcr2spi.hierarchy.NodeEntry) ItemExistsException(javax.jcr.ItemExistsException) RepositoryException(javax.jcr.RepositoryException) Name(org.apache.jackrabbit.spi.Name) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Example 14 with ItemNotFoundException

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

the class RepositoryServiceImpl method getPropertyInfo.

/**
     * @see RepositoryService#getPropertyInfo(SessionInfo, PropertyId)
     */
@Override
public PropertyInfo getPropertyInfo(SessionInfo sessionInfo, PropertyId propertyId) throws RepositoryException {
    Path p = getPath(propertyId, sessionInfo);
    String uri = getURI(p, sessionInfo);
    HttpPropfind request = null;
    try {
        request = new HttpPropfind(uri, LAZY_PROPERTY_NAME_SET, DavConstants.DEPTH_0);
        HttpResponse response = executeRequest(sessionInfo, request);
        request.checkSuccess(response);
        MultiStatusResponse[] mresponses = request.getResponseBodyAsMultiStatus(response).getResponses();
        if (mresponses.length != 1) {
            throw new ItemNotFoundException("Unable to retrieve the PropertyInfo. No such property " + uri);
        }
        MultiStatusResponse mresponse = mresponses[0];
        DavPropertySet props = mresponse.getProperties(DavServletResponse.SC_OK);
        int propertyType = PropertyType.valueFromName(props.get(JCR_TYPE).getValue().toString());
        if (propertyType == PropertyType.BINARY) {
            DavProperty<?> lengthsProp = props.get(JCR_LENGTHS);
            if (lengthsProp != null) {
                // multivalued binary property
                long[] lengths = ValueUtil.lengthsFromXml(lengthsProp.getValue());
                QValue[] qValues = new QValue[lengths.length];
                for (int i = 0; i < lengths.length; i++) {
                    qValues[i] = getQValueFactory(sessionInfo).create(lengths[i], uri, i);
                }
                return new PropertyInfoImpl(propertyId, p, propertyType, qValues);
            } else {
                // single valued binary property
                long length = Long.parseLong(props.get(JCR_LENGTH).getValue().toString());
                QValue qValue = getQValueFactory(sessionInfo).create(length, uri, QValueFactoryImpl.NO_INDEX);
                return new PropertyInfoImpl(propertyId, p, propertyType, qValue);
            }
        } else if (props.contains(JCR_GET_STRING)) {
            // single valued non-binary property
            Object v = props.get(JCR_GET_STRING).getValue();
            String str = (v == null) ? "" : v.toString();
            QValue qValue = ValueFormat.getQValue(str, propertyType, getNamePathResolver(sessionInfo), getQValueFactory(sessionInfo));
            return new PropertyInfoImpl(propertyId, p, propertyType, qValue);
        } else {
            // didn't expose the JCR_GET_STRING dav property.
            return super.getPropertyInfo(sessionInfo, propertyId);
        }
    } catch (IOException e) {
        log.error("Internal error while retrieving ItemInfo.", e);
        throw new RepositoryException(e.getMessage());
    } catch (DavException e) {
        throw ExceptionConverter.generate(e);
    } finally {
        if (request != null) {
            request.releaseConnection();
        }
    }
}
Also used : Path(org.apache.jackrabbit.spi.Path) QValue(org.apache.jackrabbit.spi.QValue) DavException(org.apache.jackrabbit.webdav.DavException) MultiStatusResponse(org.apache.jackrabbit.webdav.MultiStatusResponse) HttpResponse(org.apache.http.HttpResponse) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) DavPropertySet(org.apache.jackrabbit.webdav.property.DavPropertySet) HttpPropfind(org.apache.jackrabbit.webdav.client.methods.HttpPropfind) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Example 15 with ItemNotFoundException

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

the class ValueLoader method loadType.

int loadType(String uri) throws RepositoryException, IOException {
    DavPropertyNameSet nameSet = new DavPropertyNameSet();
    nameSet.add(JcrRemotingConstants.JCR_TYPE_LN, ItemResourceConstants.NAMESPACE);
    HttpPropfind request = null;
    try {
        request = new HttpPropfind(uri, nameSet, DavConstants.DEPTH_0);
        HttpResponse response = client.execute(request, context);
        request.checkSuccess(response);
        MultiStatusResponse[] responses = request.getResponseBodyAsMultiStatus(response).getResponses();
        if (responses.length == 1) {
            DavPropertySet props = responses[0].getProperties(DavServletResponse.SC_OK);
            DavProperty<?> type = props.get(JcrRemotingConstants.JCR_TYPE_LN, ItemResourceConstants.NAMESPACE);
            if (type != null) {
                return PropertyType.valueFromName(type.getValue().toString());
            } else {
                throw new RepositoryException("Internal error. Cannot retrieve property type at " + uri);
            }
        } else {
            throw new ItemNotFoundException("Internal error. Cannot retrieve property type at " + uri);
        }
    } catch (DavException e) {
        throw ExceptionConverter.generate(e);
    } finally {
        if (request != null) {
            request.releaseConnection();
        }
    }
}
Also used : DavPropertySet(org.apache.jackrabbit.webdav.property.DavPropertySet) HttpPropfind(org.apache.jackrabbit.webdav.client.methods.HttpPropfind) DavException(org.apache.jackrabbit.webdav.DavException) DavPropertyNameSet(org.apache.jackrabbit.webdav.property.DavPropertyNameSet) MultiStatusResponse(org.apache.jackrabbit.webdav.MultiStatusResponse) HttpResponse(org.apache.http.HttpResponse) RepositoryException(javax.jcr.RepositoryException) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Aggregations

ItemNotFoundException (javax.jcr.ItemNotFoundException)139 RepositoryException (javax.jcr.RepositoryException)61 Node (javax.jcr.Node)44 Path (org.apache.jackrabbit.spi.Path)25 PathNotFoundException (javax.jcr.PathNotFoundException)23 NodeId (org.apache.jackrabbit.core.id.NodeId)22 ItemStateException (org.apache.jackrabbit.core.state.ItemStateException)16 IOException (java.io.IOException)14 InvalidItemStateException (javax.jcr.InvalidItemStateException)14 NodeState (org.apache.jackrabbit.core.state.NodeState)14 Name (org.apache.jackrabbit.spi.Name)14 AccessDeniedException (javax.jcr.AccessDeniedException)13 HttpResponse (org.apache.http.HttpResponse)13 DavException (org.apache.jackrabbit.webdav.DavException)13 ItemExistsException (javax.jcr.ItemExistsException)12 Session (javax.jcr.Session)12 NoSuchItemStateException (org.apache.jackrabbit.core.state.NoSuchItemStateException)12 ChildNodeEntry (org.apache.jackrabbit.core.state.ChildNodeEntry)11 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)10 MultiStatusResponse (org.apache.jackrabbit.webdav.MultiStatusResponse)10