Search in sources :

Example 16 with ItemNotFoundException

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

the class URIResolverImpl method getNodeId.

private NodeId getNodeId(String uri, SessionInfo sessionInfo, boolean nodeIsGone) throws RepositoryException {
    IdURICache cache = getCache(sessionInfo.getWorkspaceName());
    if (cache.containsUri(uri)) {
        // id has been accessed before and is cached
        ItemId id = cache.getItemId(uri);
        if (id.denotesNode()) {
            return (NodeId) id;
        }
    }
    if (nodeIsGone) {
        throw new RepositoryException("Can't reconstruct nodeId from URI when the remote node is gone.");
    }
    // retrieve parentId from cache or by recursive calls
    NodeId parentId;
    if (isSameURI(uri, getRootItemUri(sessionInfo.getWorkspaceName()))) {
        parentId = null;
    } else {
        String parentUri = Text.getRelativeParent(uri, 1, true);
        parentId = getNodeId(parentUri, sessionInfo, false);
    }
    DavPropertyNameSet nameSet = new DavPropertyNameSet();
    nameSet.add(JcrRemotingConstants.JCR_UUID_LN, ItemResourceConstants.NAMESPACE);
    nameSet.add(JcrRemotingConstants.JCR_NAME_LN, ItemResourceConstants.NAMESPACE);
    nameSet.add(JcrRemotingConstants.JCR_INDEX_LN, ItemResourceConstants.NAMESPACE);
    HttpPropfind request = null;
    try {
        request = new HttpPropfind(uri, nameSet, DavConstants.DEPTH_0);
        HttpResponse response = service.executeRequest(sessionInfo, request);
        MultiStatusResponse[] responses = request.getResponseBodyAsMultiStatus(response).getResponses();
        if (responses.length != 1) {
            throw new ItemNotFoundException("Unable to retrieve the node with id " + uri);
        }
        return buildNodeId(parentId, uri, responses[0], sessionInfo.getWorkspaceName(), service.getNamePathResolver(sessionInfo));
    } catch (IOException e) {
        throw new RepositoryException(e);
    } catch (DavException e) {
        throw ExceptionConverter.generate(e);
    } finally {
        if (request != null) {
            request.releaseConnection();
        }
    }
}
Also used : 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) ItemId(org.apache.jackrabbit.spi.ItemId) HttpPropfind(org.apache.jackrabbit.webdav.client.methods.HttpPropfind) NodeId(org.apache.jackrabbit.spi.NodeId) DavPropertyNameSet(org.apache.jackrabbit.webdav.property.DavPropertyNameSet) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Example 17 with ItemNotFoundException

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

the class URIResolverImpl method getItemUri.

String getItemUri(ItemId itemId, String workspaceName, SessionInfo sessionInfo) throws RepositoryException {
    IdURICache cache = getCache(workspaceName);
    // check if uri is available from cache
    if (cache.containsItemId(itemId)) {
        return cache.getUri(itemId);
    } else {
        StringBuffer uriBuffer = new StringBuffer();
        Path path = itemId.getPath();
        String uniqueID = itemId.getUniqueID();
        // resolver uuid part
        if (uniqueID != null) {
            ItemId uuidId = (path == null) ? itemId : service.getIdFactory().createNodeId(uniqueID);
            if (path != null && cache.containsItemId(uuidId)) {
                // append uri of parent node, that is already cached
                uriBuffer.append(cache.getUri(uuidId));
            } else {
                // try to request locate-by-uuid report to build the uri
                ReportInfo rInfo = new ReportInfo(JcrRemotingConstants.REPORT_LOCATE_BY_UUID, ItemResourceConstants.NAMESPACE);
                rInfo.setContentElement(DomUtil.hrefToXml(uniqueID, domFactory));
                HttpReport request = null;
                try {
                    String wspUri = getWorkspaceUri(workspaceName);
                    request = new HttpReport(wspUri, rInfo);
                    HttpResponse response = service.executeRequest(sessionInfo, request);
                    request.checkSuccess(response);
                    MultiStatus ms = request.getResponseBodyAsMultiStatus(response);
                    if (ms.getResponses().length == 1) {
                        String absoluteUri = resolve(wspUri, ms.getResponses()[0].getHref());
                        uriBuffer.append(absoluteUri);
                        cache.add(absoluteUri, uuidId);
                    } else {
                        throw new ItemNotFoundException("Cannot identify item with uniqueID " + uniqueID);
                    }
                } catch (IOException e) {
                    throw new RepositoryException(e.getMessage());
                } catch (DavException e) {
                    throw ExceptionConverter.generate(e);
                } finally {
                    if (request != null) {
                        request.releaseConnection();
                    }
                }
            }
        } else {
            // start build uri from root-item
            uriBuffer.append(getRootItemUri(workspaceName));
        }
        // resolve relative-path part unless it denotes the root-item
        if (path != null && !path.denotesRoot()) {
            String jcrPath = service.getNamePathResolver(sessionInfo).getJCRPath(path);
            if (!path.isAbsolute() && !uriBuffer.toString().endsWith("/")) {
                uriBuffer.append("/");
            }
            uriBuffer.append(Text.escapePath(jcrPath));
        }
        String itemUri = uriBuffer.toString();
        if (!cache.containsItemId(itemId)) {
            cache.add(itemUri, itemId);
        }
        return itemUri;
    }
}
Also used : Path(org.apache.jackrabbit.spi.Path) HttpReport(org.apache.jackrabbit.webdav.client.methods.HttpReport) DavException(org.apache.jackrabbit.webdav.DavException) ReportInfo(org.apache.jackrabbit.webdav.version.report.ReportInfo) HttpResponse(org.apache.http.HttpResponse) MultiStatus(org.apache.jackrabbit.webdav.MultiStatus) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) ItemId(org.apache.jackrabbit.spi.ItemId) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Example 18 with ItemNotFoundException

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

the class RepositoryServiceImpl method getReferences.

@Override
public Iterator<PropertyId> getReferences(SessionInfo sessionInfo, NodeId nodeId, Name propertyName, boolean weakReferences) throws RepositoryException {
    // set of properties to be retrieved
    DavPropertyNameSet nameSet = new DavPropertyNameSet();
    String refType = weakReferences ? JcrRemotingConstants.JCR_WEAK_REFERENCES_LN : JcrRemotingConstants.JCR_REFERENCES_LN;
    nameSet.add(refType, ItemResourceConstants.NAMESPACE);
    HttpPropfind request = null;
    try {
        String uri = getItemUri(nodeId, sessionInfo);
        request = new HttpPropfind(uri, nameSet, 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 node with id " + saveGetIdString(nodeId, sessionInfo));
        }
        List<PropertyId> refIds = Collections.emptyList();
        for (MultiStatusResponse mresponse : mresponses) {
            if (isSameResource(uri, mresponse)) {
                DavPropertySet props = mresponse.getProperties(DavServletResponse.SC_OK);
                DavProperty<?> p = props.get(refType, ItemResourceConstants.NAMESPACE);
                if (p != null) {
                    refIds = new ArrayList<PropertyId>();
                    HrefProperty hp = new HrefProperty(p);
                    for (String propHref : hp.getHrefs()) {
                        PropertyId propId = uriResolver.getPropertyId(resolve(uri, propHref), sessionInfo);
                        if (propertyName == null || propertyName.equals(propId.getName())) {
                            refIds.add(propId);
                        }
                    }
                }
            }
        }
        return refIds.iterator();
    } catch (IOException e) {
        throw new RepositoryException(e);
    } catch (DavException e) {
        throw ExceptionConverter.generate(e);
    } finally {
        if (request != null) {
            request.releaseConnection();
        }
    }
}
Also used : 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) PropertyId(org.apache.jackrabbit.spi.PropertyId) DavPropertySet(org.apache.jackrabbit.webdav.property.DavPropertySet) HttpPropfind(org.apache.jackrabbit.webdav.client.methods.HttpPropfind) HrefProperty(org.apache.jackrabbit.webdav.property.HrefProperty) DavPropertyNameSet(org.apache.jackrabbit.webdav.property.DavPropertyNameSet) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Example 19 with ItemNotFoundException

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

the class UserManagerImpl method internalGetAuthorizable.

/**
     * @param id The user or group ID.
     * @return The authorizable with the given <code>id</code> or <code>null</code>.
     * @throws RepositoryException If an error occurs.
     */
private Authorizable internalGetAuthorizable(String id) throws RepositoryException {
    NodeId nodeId = buildNodeId(id);
    NodeImpl n = null;
    try {
        n = session.getNodeById(nodeId);
    } catch (ItemNotFoundException e) {
        boolean compatibleJR16 = config.getConfigValue(PARAM_COMPATIBLE_JR16, false);
        if (compatibleJR16) {
            // backwards-compatibility with JR < 2.0 user/group structure that doesn't
            // allow to determine existence of an authorizable from the id directly.
            // search for it the node belonging to that id
            n = (NodeImpl) authResolver.findNode(P_USERID, id, NT_REP_USER);
            if (n == null) {
                // no user -> look for group.
                // NOTE: JR < 2.0 always returned groupIDs that didn't contain any
                // illegal JCR chars. Since Group.getID() 'unescapes' the node
                // name additional escaping is required.
                Name nodeName = session.getQName(Text.escapeIllegalJcrChars(id));
                n = (NodeImpl) authResolver.findNode(nodeName, NT_REP_GROUP);
            }
        }
    // else: no matching node found -> ignore exception.
    }
    return getAuthorizable(n);
}
Also used : NodeImpl(org.apache.jackrabbit.core.NodeImpl) NodeId(org.apache.jackrabbit.core.id.NodeId) ItemNotFoundException(javax.jcr.ItemNotFoundException) Name(org.apache.jackrabbit.spi.Name)

Example 20 with ItemNotFoundException

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

the class PathPropertyTest method testGetProperty.

/**
     * Since JCR 2.0 a path property can be dereferenced if it points to a
     * Property.
     * TODO: create several tests out of this one
     */
public void testGetProperty() throws RepositoryException {
    if (!multiple) {
        String propPath = prop.getPath();
        String propName = prop.getName();
        // absolute property path
        prop.getParent().setProperty(propName, propPath, PropertyType.PATH);
        String path = prop.getString();
        Property p = prop.getProperty();
        assertEquals("The path of the dereferenced property must be equal to the value", path, p.getPath());
        assertTrue("The property value must be resolved to the correct property.", prop.isSame(p));
        // relative property path
        prop.getParent().setProperty(propName, propName, PropertyType.PATH);
        path = prop.getString();
        p = prop.getProperty();
        assertEquals("The path of the dereferenced property must be equal to the value", path, p.getName());
        assertTrue("The property value must be resolved to the correct property.", prop.getParent().getProperty(path).isSame(p));
        // non-existing property path
        while (session.propertyExists(propPath)) {
            propPath += "x";
        }
        prop.getParent().setProperty(propName, propPath, PropertyType.PATH);
        try {
            prop.getProperty();
            fail("Calling Property.getProperty() for a PATH value that doesn't have a corresponding Property, ItemNotFoundException is expected");
        } catch (ItemNotFoundException e) {
        //ok
        }
    } else {
        try {
            prop.getProperty();
            fail("Property.getNode() called on a multivalue property " + "should throw a ValueFormatException.");
        } catch (ValueFormatException vfe) {
        // ok
        }
    }
}
Also used : ValueFormatException(javax.jcr.ValueFormatException) Property(javax.jcr.Property) 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