Search in sources :

Example 56 with PathNotFoundException

use of javax.jcr.PathNotFoundException in project jackrabbit-oak by apache.

the class SessionImpl method removeItem.

@Override
public void removeItem(final String absPath) throws RepositoryException {
    checkAlive();
    final String oakPath = getOakPathOrThrowNotFound(checkNotNull(absPath));
    sd.performVoid(new WriteOperation<Void>("removeItem") {

        @Override
        public void performVoid() throws RepositoryException {
            ItemDelegate item = sd.getItem(oakPath);
            if (item == null) {
                throw new PathNotFoundException(absPath);
            } else if (item.isProtected()) {
                throw new ConstraintViolationException(item.getPath() + " is protected");
            } else if (!item.remove()) {
                throw new RepositoryException(item.getPath() + " could not be removed");
            }
        }
    });
}
Also used : ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) RepositoryException(javax.jcr.RepositoryException) PathNotFoundException(javax.jcr.PathNotFoundException) ItemDelegate(org.apache.jackrabbit.oak.jcr.delegate.ItemDelegate)

Example 57 with PathNotFoundException

use of javax.jcr.PathNotFoundException in project jackrabbit-oak by apache.

the class VersionManagerImpl method checkout.

@Override
public void checkout(final String absPath) throws RepositoryException {
    final SessionDelegate sessionDelegate = sessionContext.getSessionDelegate();
    sessionDelegate.performVoid(new SessionOperation<Void>("checkout", true) {

        @Override
        public void performVoid() throws RepositoryException {
            String oakPath = getOakPathOrThrowNotFound(absPath);
            NodeDelegate nodeDelegate = sessionDelegate.getNode(oakPath);
            if (nodeDelegate == null) {
                throw new PathNotFoundException(absPath);
            }
            checkNotLocked(absPath);
            versionManagerDelegate.checkout(nodeDelegate);
        }
    });
}
Also used : SessionDelegate(org.apache.jackrabbit.oak.jcr.delegate.SessionDelegate) RepositoryException(javax.jcr.RepositoryException) PathNotFoundException(javax.jcr.PathNotFoundException) NodeDelegate(org.apache.jackrabbit.oak.jcr.delegate.NodeDelegate)

Example 58 with PathNotFoundException

use of javax.jcr.PathNotFoundException in project jackrabbit-oak by apache.

the class VersionManagerImpl method internalGetVersionHistory.

/**
     * Returns the version history for the versionable node at the given path.
     *
     * @param absPathVersionable path to a versionable node.
     * @return the version history.
     * @throws PathNotFoundException if the given path does not reference an
     *                               existing node.
     * @throws UnsupportedRepositoryOperationException
     *                               if the node at the given path is not
     *                               mix:versionable.
     * @throws RepositoryException if some other error occurs.
     */
@Nonnull
private VersionHistoryDelegate internalGetVersionHistory(@Nonnull String absPathVersionable) throws RepositoryException, UnsupportedRepositoryOperationException {
    SessionDelegate sessionDelegate = sessionContext.getSessionDelegate();
    String oakPath = getOakPathOrThrowNotFound(checkNotNull(absPathVersionable));
    NodeDelegate nodeDelegate = sessionDelegate.getNode(oakPath);
    if (nodeDelegate == null) {
        throw new PathNotFoundException(absPathVersionable);
    }
    return versionManagerDelegate.getVersionHistory(nodeDelegate);
}
Also used : SessionDelegate(org.apache.jackrabbit.oak.jcr.delegate.SessionDelegate) PathNotFoundException(javax.jcr.PathNotFoundException) NodeDelegate(org.apache.jackrabbit.oak.jcr.delegate.NodeDelegate) Nonnull(javax.annotation.Nonnull)

Example 59 with PathNotFoundException

use of javax.jcr.PathNotFoundException in project jackrabbit-oak by apache.

the class CRUDTest method testRemoveBySetProperty.

@Test
public void testRemoveBySetProperty() throws RepositoryException {
    Session session = getAdminSession();
    Node root = session.getRootNode();
    try {
        root.setProperty("test", "abc");
        assertNotNull(root.setProperty("test", (String) null));
    } catch (PathNotFoundException e) {
    // success
    }
}
Also used : Node(javax.jcr.Node) JUnitMatchers.containsString(org.junit.matchers.JUnitMatchers.containsString) PathNotFoundException(javax.jcr.PathNotFoundException) Session(javax.jcr.Session) Test(org.junit.Test)

Example 60 with PathNotFoundException

use of javax.jcr.PathNotFoundException in project sling by apache.

the class MockedResource method adaptTo.

@SuppressWarnings("unchecked")
@Override
public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
    if (type.equals(Node.class)) {
        try {
            return (AdapterType) getSession().getNode(getPath());
        } catch (Exception e) {
            logger.error("Exception occurred: " + e, e);
            throw new RuntimeException("Exception occurred: " + e, e);
        }
    } else if (type.equals(ValueMap.class)) {
        try {
            Session session = getSession();
            Node node = session.getNode(getPath());
            HashMap<String, Object> map = new HashMap<String, Object>();
            PropertyIterator properties = node.getProperties();
            while (properties.hasNext()) {
                Property p = properties.nextProperty();
                if (p.getType() == PropertyType.BOOLEAN) {
                    map.put(p.getName(), p.getBoolean());
                } else if (p.getType() == PropertyType.STRING) {
                    map.put(p.getName(), p.getString());
                } else if (p.getType() == PropertyType.DATE) {
                    map.put(p.getName(), p.getDate().getTime());
                } else if (p.getType() == PropertyType.NAME) {
                    map.put(p.getName(), p.getName());
                } else if (p.getType() == PropertyType.LONG) {
                    map.put(p.getName(), p.getLong());
                } else {
                    throw new RuntimeException("Unsupported property type: " + p.getType());
                }
            }
            ValueMap valueMap = new ValueMapDecorator(map);
            return (AdapterType) valueMap;
        } catch (Exception e) {
            logger.error("adaptTo failed with : " + e);
            return null;
        }
    } else if (type.equals(ModifiableValueMap.class)) {
        return (AdapterType) new ModifiableValueMap() {

            public Collection<Object> values() {
                throw new UnsupportedOperationException();
            }

            public int size() {
                throw new UnsupportedOperationException();
            }

            public Object remove(Object arg0) {
                Session session = getSession();
                try {
                    final Node node = session.getNode(getPath());
                    final Property p = node.getProperty(String.valueOf(arg0));
                    if (p != null) {
                        p.remove();
                    }
                    // the return value is never used
                    return null;
                } catch (PathNotFoundException pnfe) {
                    // perfectly fine
                    return null;
                } catch (RepositoryException e) {
                    throw new RuntimeException(e);
                }
            }

            public void putAll(Map<? extends String, ? extends Object> arg0) {
                throw new UnsupportedOperationException();
            }

            public Object put(String arg0, Object arg1) {
                Session session = getSession();
                try {
                    final Node node = session.getNode(getPath());
                    Object result = null;
                    if (node.hasProperty(arg0)) {
                        final Property previous = node.getProperty(arg0);
                        if (previous == null) {
                        // null
                        } else if (previous.getType() == PropertyType.STRING) {
                            result = previous.getString();
                        } else if (previous.getType() == PropertyType.DATE) {
                            result = previous.getDate();
                        } else if (previous.getType() == PropertyType.BOOLEAN) {
                            result = previous.getBoolean();
                        } else if (previous.getType() == PropertyType.LONG) {
                            result = previous.getLong();
                        } else {
                            throw new UnsupportedOperationException();
                        }
                    }
                    if (arg1 instanceof String) {
                        node.setProperty(arg0, (String) arg1);
                    } else if (arg1 instanceof Calendar) {
                        node.setProperty(arg0, (Calendar) arg1);
                    } else if (arg1 instanceof Boolean) {
                        node.setProperty(arg0, (Boolean) arg1);
                    } else if (arg1 instanceof Date) {
                        final Calendar c = Calendar.getInstance();
                        c.setTime((Date) arg1);
                        node.setProperty(arg0, c);
                    } else if (arg1 instanceof Long) {
                        node.setProperty(arg0, (Long) arg1);
                    } else if (arg1 == null) {
                        node.setProperty(arg0, (Value) null);
                    } else {
                        throw new UnsupportedOperationException();
                    }
                    return result;
                } catch (RepositoryException e) {
                    throw new RuntimeException(e);
                }
            }

            public Set<String> keySet() {
                Session session = getSession();
                try {
                    final Node node = session.getNode(getPath());
                    final PropertyIterator pi = node.getProperties();
                    final Set<String> result = new HashSet<String>();
                    while (pi.hasNext()) {
                        final Property p = pi.nextProperty();
                        result.add(p.getName());
                    }
                    return result;
                } catch (RepositoryException e) {
                    throw new RuntimeException(e);
                }
            }

            public boolean isEmpty() {
                throw new UnsupportedOperationException();
            }

            public Object get(Object arg0) {
                Session session = getSession();
                try {
                    final Node node = session.getNode(getPath());
                    final String key = String.valueOf(arg0);
                    if (node.hasProperty(key)) {
                        return node.getProperty(key);
                    } else {
                        return null;
                    }
                } catch (RepositoryException re) {
                    throw new RuntimeException(re);
                }
            }

            public Set<Entry<String, Object>> entrySet() {
                throw new UnsupportedOperationException();
            }

            public boolean containsValue(Object arg0) {
                throw new UnsupportedOperationException();
            }

            public boolean containsKey(Object arg0) {
                Session session = getSession();
                try {
                    final Node node = session.getNode(getPath());
                    return node.hasProperty(String.valueOf(arg0));
                } catch (RepositoryException re) {
                    throw new RuntimeException(re);
                }
            }

            public void clear() {
                throw new UnsupportedOperationException();
            }

            public <T> T get(String name, T defaultValue) {
                throw new UnsupportedOperationException();
            }

            public <T> T get(String name, Class<T> type) {
                Session session = getSession();
                try {
                    final Node node = session.getNode(getPath());
                    if (node == null) {
                        return null;
                    }
                    if (!node.hasProperty(name)) {
                        return null;
                    }
                    Property p = node.getProperty(name);
                    if (p == null) {
                        return null;
                    }
                    if (type.equals(Calendar.class)) {
                        return (T) p.getDate();
                    } else if (type.equals(String.class)) {
                        return (T) p.getString();
                    } else {
                        throw new UnsupportedOperationException();
                    }
                } catch (RepositoryException e) {
                    throw new RuntimeException(e);
                }
            }
        };
    } else {
        return super.adaptTo(type);
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) ValueMap(org.apache.sling.api.resource.ValueMap) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Node(javax.jcr.Node) Property(javax.jcr.Property) Calendar(java.util.Calendar) PropertyIterator(javax.jcr.PropertyIterator) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) RepositoryException(javax.jcr.RepositoryException) PathNotFoundException(javax.jcr.PathNotFoundException) RepositoryException(javax.jcr.RepositoryException) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Date(java.util.Date) Collection(java.util.Collection) PathNotFoundException(javax.jcr.PathNotFoundException) Session(javax.jcr.Session)

Aggregations

PathNotFoundException (javax.jcr.PathNotFoundException)136 Node (javax.jcr.Node)58 RepositoryException (javax.jcr.RepositoryException)46 ItemNotFoundException (javax.jcr.ItemNotFoundException)24 Session (javax.jcr.Session)23 Path (org.apache.jackrabbit.spi.Path)22 AccessDeniedException (javax.jcr.AccessDeniedException)14 Property (javax.jcr.Property)14 Test (org.junit.Test)14 Item (javax.jcr.Item)11 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)10 NodeIterator (javax.jcr.NodeIterator)9 Value (javax.jcr.Value)9 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)9 Name (org.apache.jackrabbit.spi.Name)8 PropertyIterator (javax.jcr.PropertyIterator)7 NodeDelegate (org.apache.jackrabbit.oak.jcr.delegate.NodeDelegate)7 HashSet (java.util.HashSet)6 ItemExistsException (javax.jcr.ItemExistsException)6 ValueFormatException (javax.jcr.ValueFormatException)6