Search in sources :

Example 31 with ItemNotFoundException

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

the class RepositoryServiceImpl method getPropertyInfo.

@Override
public PropertyInfo getPropertyInfo(SessionInfo sessionInfo, PropertyId propertyId) throws RepositoryException {
    HttpGet request = null;
    try {
        String uri = getItemUri(propertyId, sessionInfo);
        request = new HttpGet(uri);
        HttpResponse response = executeRequest(sessionInfo, request);
        int status = response.getStatusLine().getStatusCode();
        if (status != DavServletResponse.SC_OK) {
            throw ExceptionConverter.generate(new DavException(status, response.getStatusLine().getReasonPhrase()));
        }
        Path path = uriResolver.getQPath(uri, sessionInfo);
        HttpEntity entity = response.getEntity();
        ContentType ct = ContentType.get(entity);
        boolean isMultiValued;
        QValue[] values;
        int type;
        NamePathResolver resolver = getNamePathResolver(sessionInfo);
        if (ct != null && ct.getMimeType().startsWith("jcr-value")) {
            type = JcrValueType.typeFromContentType(ct.getMimeType());
            QValue v;
            if (type == PropertyType.BINARY) {
                v = getQValueFactory().create(entity.getContent());
            } else {
                Reader reader = new InputStreamReader(entity.getContent(), ct.getCharset());
                StringBuffer sb = new StringBuffer();
                int c;
                while ((c = reader.read()) > -1) {
                    sb.append((char) c);
                }
                Value jcrValue = valueFactory.createValue(sb.toString(), type);
                if (jcrValue instanceof QValueValue) {
                    v = ((QValueValue) jcrValue).getQValue();
                } else {
                    v = ValueFormat.getQValue(jcrValue, resolver, getQValueFactory());
                }
            }
            values = new QValue[] { v };
            isMultiValued = false;
        } else if (ct != null && ct.getMimeType().equals("text/xml")) {
            // jcr:values property spooled
            values = getValues(entity.getContent(), resolver, propertyId);
            type = (values.length > 0) ? values[0].getType() : loadType(uri, getClient(sessionInfo), propertyId, sessionInfo, resolver);
            isMultiValued = true;
        } else {
            throw new ItemNotFoundException("Unable to retrieve the property with id " + saveGetIdString(propertyId, resolver));
        }
        return new PropertyInfoImpl(propertyId, path, type, isMultiValued, values);
    } catch (IOException e) {
        throw new RepositoryException(e);
    } catch (DavException e) {
        throw ExceptionConverter.generate(e);
    } catch (NameException e) {
        throw new RepositoryException(e);
    } finally {
        if (request != null) {
            request.releaseConnection();
        }
    }
}
Also used : Path(org.apache.jackrabbit.spi.Path) QValueValue(org.apache.jackrabbit.spi.commons.value.QValueValue) NamePathResolver(org.apache.jackrabbit.spi.commons.conversion.NamePathResolver) QValue(org.apache.jackrabbit.spi.QValue) HttpEntity(org.apache.http.HttpEntity) ContentType(org.apache.http.entity.ContentType) InputStreamReader(java.io.InputStreamReader) DavException(org.apache.jackrabbit.webdav.DavException) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) NameException(org.apache.jackrabbit.spi.commons.conversion.NameException) IllegalNameException(org.apache.jackrabbit.spi.commons.conversion.IllegalNameException) QValue(org.apache.jackrabbit.spi.QValue) Value(javax.jcr.Value) QValueValue(org.apache.jackrabbit.spi.commons.value.QValueValue) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Example 32 with ItemNotFoundException

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

the class RepositoryServiceImpl method getChildInfos.

@Override
public Iterator<ChildInfo> getChildInfos(SessionInfo sessionInfo, NodeId parentId) throws RepositoryException {
    // set of properties to be retrieved
    DavPropertyNameSet nameSet = new DavPropertyNameSet();
    nameSet.add(JcrRemotingConstants.JCR_NAME_LN, ItemResourceConstants.NAMESPACE);
    nameSet.add(JcrRemotingConstants.JCR_INDEX_LN, ItemResourceConstants.NAMESPACE);
    nameSet.add(JcrRemotingConstants.JCR_PARENT_LN, ItemResourceConstants.NAMESPACE);
    nameSet.add(JcrRemotingConstants.JCR_UUID_LN, ItemResourceConstants.NAMESPACE);
    nameSet.add(DavPropertyName.RESOURCETYPE);
    HttpPropfind request = null;
    try {
        String uri = getItemUri(parentId, sessionInfo);
        request = new HttpPropfind(uri, nameSet, DEPTH_1);
        HttpResponse response = executeRequest(sessionInfo, request);
        request.checkSuccess(response);
        List<ChildInfo> childEntries;
        MultiStatusResponse[] mresponses = request.getResponseBodyAsMultiStatus(response).getResponses();
        if (mresponses.length < 1) {
            throw new ItemNotFoundException("Unable to retrieve the node with id " + saveGetIdString(parentId, sessionInfo));
        } else if (mresponses.length == 1) {
            // no child nodes nor properties
            childEntries = Collections.emptyList();
            return childEntries.iterator();
        }
        childEntries = new ArrayList<ChildInfo>();
        for (MultiStatusResponse mresponse : mresponses) {
            if (!isSameResource(uri, mresponse)) {
                DavPropertySet childProps = mresponse.getProperties(DavServletResponse.SC_OK);
                if (childProps.contains(DavPropertyName.RESOURCETYPE) && childProps.get(DavPropertyName.RESOURCETYPE).getValue() != null) {
                    childEntries.add(buildChildInfo(childProps, sessionInfo));
                }
            // else: property -> ignore
            }
        // else: ignore the response related to the parent
        }
        return childEntries.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) ChildInfo(org.apache.jackrabbit.spi.ChildInfo) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) DavPropertySet(org.apache.jackrabbit.webdav.property.DavPropertySet) HttpPropfind(org.apache.jackrabbit.webdav.client.methods.HttpPropfind) DavPropertyNameSet(org.apache.jackrabbit.webdav.property.DavPropertyNameSet) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Example 33 with ItemNotFoundException

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

the class ExceptionConverter method generate.

public static RepositoryException generate(DavException davExc, int methodCode, String name) {
    String msg = davExc.getMessage();
    if (davExc.hasErrorCondition()) {
        try {
            Element error = davExc.toXml(DomUtil.createDocument());
            if (DomUtil.matches(error, DavException.XML_ERROR, DavConstants.NAMESPACE)) {
                if (DomUtil.hasChildElement(error, "exception", null)) {
                    Element exc = DomUtil.getChildElement(error, "exception", null);
                    if (DomUtil.hasChildElement(exc, "message", null)) {
                        msg = DomUtil.getChildText(exc, "message", null);
                    }
                    if (DomUtil.hasChildElement(exc, "class", null)) {
                        Class<?> cl = Class.forName(DomUtil.getChildText(exc, "class", null));
                        Constructor<?> excConstr = cl.getConstructor(String.class);
                        if (excConstr != null) {
                            Object o = excConstr.newInstance(msg);
                            if (o instanceof PathNotFoundException && methodCode == DavMethods.DAV_POST) {
                                // see JCR-2536
                                return new InvalidItemStateException(msg);
                            } else if (o instanceof RepositoryException) {
                                return (RepositoryException) o;
                            } else if (o instanceof Exception) {
                                return new RepositoryException(msg, (Exception) o);
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            return new RepositoryException(e);
        }
    }
    // make sure an exception is generated
    switch(davExc.getErrorCode()) {
        // TODO: mapping DAV_error to jcr-exception is ambiguous. to be improved
        case DavServletResponse.SC_NOT_FOUND:
            switch(methodCode) {
                case DavMethods.DAV_DELETE:
                case DavMethods.DAV_MKCOL:
                case DavMethods.DAV_PUT:
                case DavMethods.DAV_POST:
                    // been made.
                    return new InvalidItemStateException(msg, davExc);
                default:
                    return new ItemNotFoundException(msg, davExc);
            }
        case DavServletResponse.SC_LOCKED:
            return new LockException(msg, davExc);
        case DavServletResponse.SC_METHOD_NOT_ALLOWED:
            return new ConstraintViolationException(msg, davExc);
        case DavServletResponse.SC_CONFLICT:
            return new InvalidItemStateException(msg, davExc);
        case DavServletResponse.SC_PRECONDITION_FAILED:
            return new LockException(msg, davExc);
        case DavServletResponse.SC_NOT_IMPLEMENTED:
            if (methodCode > 0 && name != null) {
                return new UnsupportedRepositoryOperationException("Missing implementation: Method " + name + " could not be executed", davExc);
            } else {
                return new UnsupportedRepositoryOperationException("Missing implementation", davExc);
            }
        default:
            return new RepositoryException(msg, davExc);
    }
}
Also used : UnsupportedRepositoryOperationException(javax.jcr.UnsupportedRepositoryOperationException) InvalidItemStateException(javax.jcr.InvalidItemStateException) Element(org.w3c.dom.Element) RepositoryException(javax.jcr.RepositoryException) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) ItemNotFoundException(javax.jcr.ItemNotFoundException) PathNotFoundException(javax.jcr.PathNotFoundException) UnsupportedRepositoryOperationException(javax.jcr.UnsupportedRepositoryOperationException) DavException(org.apache.jackrabbit.webdav.DavException) RepositoryException(javax.jcr.RepositoryException) LockException(javax.jcr.lock.LockException) InvalidItemStateException(javax.jcr.InvalidItemStateException) LockException(javax.jcr.lock.LockException) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) PathNotFoundException(javax.jcr.PathNotFoundException) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Example 34 with ItemNotFoundException

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

the class RepositoryServiceImpl method loadType.

private int loadType(String propertyURI, HttpClient client, PropertyId propertyId, SessionInfo sessionInfo, NamePathResolver resolver) throws IOException, DavException, RepositoryException {
    DavPropertyNameSet nameSet = new DavPropertyNameSet();
    nameSet.add(JcrRemotingConstants.JCR_TYPE_LN, ItemResourceConstants.NAMESPACE);
    HttpPropfind request = null;
    try {
        request = new HttpPropfind(propertyURI, nameSet, DEPTH_0);
        HttpResponse response = executeRequest(sessionInfo, request);
        request.checkSuccess(response);
        MultiStatusResponse[] mresponses = request.getResponseBodyAsMultiStatus(response).getResponses();
        if (mresponses.length == 1) {
            DavPropertySet props = mresponses[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 " + saveGetIdString(propertyId, resolver));
            }
        } else {
            throw new ItemNotFoundException("Internal error. Cannot retrieve property type at " + saveGetIdString(propertyId, resolver));
        }
    } finally {
        if (request != null) {
            request.releaseConnection();
        }
    }
}
Also used : DavPropertySet(org.apache.jackrabbit.webdav.property.DavPropertySet) HttpPropfind(org.apache.jackrabbit.webdav.client.methods.HttpPropfind) 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)

Example 35 with ItemNotFoundException

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

the class RepositoryServiceImpl method getLockInfo.

@Override
public LockInfo getLockInfo(SessionInfo sessionInfo, NodeId nodeId) throws RepositoryException {
    // set of Dav-properties to be retrieved
    DavPropertyNameSet nameSet = new DavPropertyNameSet();
    nameSet.add(DavPropertyName.LOCKDISCOVERY);
    nameSet.add(JcrRemotingConstants.JCR_PARENT_LN, ItemResourceConstants.NAMESPACE);
    HttpPropfind request = null;
    try {
        String uri = getItemUri(nodeId, sessionInfo);
        request = new HttpPropfind(uri, nameSet, DEPTH_0);
        initMethod(request, sessionInfo, false);
        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 LockInfo. No such node " + saveGetIdString(nodeId, sessionInfo));
        }
        DavPropertySet ps = mresponses[0].getProperties(DavServletResponse.SC_OK);
        if (ps.contains(DavPropertyName.LOCKDISCOVERY)) {
            DavProperty<?> p = ps.get(DavPropertyName.LOCKDISCOVERY);
            LockDiscovery ld = LockDiscovery.createFromXml(p.toXml(DomUtil.createDocument()));
            NodeId parentId = getParentId(uri, ps, sessionInfo);
            return retrieveLockInfo(ld, sessionInfo, nodeId, parentId);
        } else {
            // no lock present
            log.debug("No Lock present on node with id " + saveGetIdString(nodeId, sessionInfo));
            return null;
        }
    } catch (IOException e) {
        throw new RepositoryException(e);
    } catch (ParserConfigurationException 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) LockDiscovery(org.apache.jackrabbit.webdav.lock.LockDiscovery) DavPropertySet(org.apache.jackrabbit.webdav.property.DavPropertySet) HttpPropfind(org.apache.jackrabbit.webdav.client.methods.HttpPropfind) DavPropertyNameSet(org.apache.jackrabbit.webdav.property.DavPropertyNameSet) NodeId(org.apache.jackrabbit.spi.NodeId) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) 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