Search in sources :

Example 81 with ConstraintViolationException

use of javax.jcr.nodetype.ConstraintViolationException in project jackrabbit by apache.

the class VersionHistoryTest method testSetProperty.

/**
 * Tests if <ul> <li><code>VersionHistory.setProperty(String,
 * String[])</code></li> <li><code>VersionHistory.setProperty(String,
 * String[], int)</code></li> <li><code>VersionHistory.setProperty(String,
 * Value[])</code></li> <li><code>VersionHistory.setProperty(String,
 * Value[], int)</code></li> <li><code>VersionHistory.setProperty(String,
 * boolean)</code></li> <li><code>VersionHistory.setProperty(String,
 * double)</code></li> <li><code>VersionHistory.setProperty(String,
 * InputStream)</code></li> <li><code>VersionHistory.setProperty(String,
 * String)</code></li> <li><code>VersionHistory.setProperty(String,
 * Calendar)</code></li> <li><code>VersionHistory.setProperty(String,
 * Node)</code></li> <li><code>VersionHistory.setProperty(String,
 * Value)</code></li> <li><code>VersionHistory.setProperty(String,
 * long)</code></li> </ul> all throw a {@link javax.jcr.nodetype.ConstraintViolationException}
 */
public void testSetProperty() throws Exception {
    // create Value[] object
    Value[] vArray = new Value[3];
    vArray[0] = superuser.getValueFactory().createValue("abc");
    vArray[1] = superuser.getValueFactory().createValue("xyz");
    vArray[2] = superuser.getValueFactory().createValue("123");
    // create String array
    String[] s = { "abc", "xyz", "123" };
    try {
        vHistory.setProperty(propertyName1, s);
        vHistory.getSession().save();
        fail("VersionHistory should be read-only: VersionHistory.setProperty(String,String[]) did not throw a ConstraintViolationException");
    } catch (ConstraintViolationException success) {
    }
    try {
        vHistory.setProperty(propertyName1, s, PropertyType.STRING);
        vHistory.getSession().save();
        fail("VersionHistory should be read-only: VersionHistory.setProperty(String,String[],int) did not throw a ConstraintViolationException");
    } catch (ConstraintViolationException success) {
    }
    try {
        vHistory.setProperty(propertyName1, vArray);
        vHistory.getSession().save();
        fail("VersionHistory should be read-only: VersionHistory.setProperty(String,Value[]) did not throw a ConstraintViolationException");
    } catch (ConstraintViolationException success) {
    }
    try {
        vHistory.setProperty(propertyName1, vArray, PropertyType.STRING);
        vHistory.getSession().save();
        fail("VersionHistory should be read-only: VersionHistory.setProperty(String,Value[],int]) did not throw a ConstraintViolationException");
    } catch (ConstraintViolationException success) {
    }
    try {
        vHistory.setProperty(propertyName1, true);
        vHistory.getSession().save();
        fail("VersionHistory should be read-only: VersionHistory.setProperty(String,boolean) did not throw a ConstraintViolationException");
    } catch (ConstraintViolationException success) {
    }
    try {
        vHistory.setProperty(propertyName1, 123);
        vHistory.getSession().save();
        fail("VersionHistory should be read-only: VersionHistory.setProperty(String,double) did not throw a ConstraintViolationException");
    } catch (ConstraintViolationException success) {
    }
    try {
        byte[] bytes = { 73, 26, 32, -36, 40, -43, -124 };
        InputStream inpStream = new ByteArrayInputStream(bytes);
        vHistory.setProperty(propertyName1, inpStream);
        vHistory.getSession().save();
        fail("VersionHistory should be read-only: VersionHistory.setProperty(String,InputStream) did not throw a ConstraintViolationException");
    } catch (ConstraintViolationException success) {
    }
    try {
        vHistory.setProperty(propertyName1, "abc");
        vHistory.getSession().save();
        fail("VersionHistory should be read-only: VersionHistory.setProperty(String,String) did not throw a ConstraintViolationException");
    } catch (ConstraintViolationException success) {
    }
    try {
        Calendar c = new GregorianCalendar(1945, 1, 6, 16, 20, 0);
        vHistory.setProperty(propertyName1, c);
        vHistory.getSession().save();
        fail("VersionHistory should be read-only: VersionHistory.setProperty(String,Calendar) did not throw a ConstraintViolationException");
    } catch (ConstraintViolationException success) {
    }
    try {
        vHistory.setProperty(propertyName1, vHistory);
        vHistory.getSession().save();
        fail("VersionHistory should be read-only: VersionHistory.setProperty(String,Node) did not throw a ConstraintViolationException");
    } catch (ConstraintViolationException success) {
    }
    try {
        Value v = superuser.getValueFactory().createValue("abc");
        vHistory.setProperty(propertyName1, v);
        vHistory.getSession().save();
        fail("VersionHistory should be read-only: VersionHistory.setProperty(String,Value) did not throw a ConstraintViolationException");
    } catch (ConstraintViolationException success) {
    }
    try {
        vHistory.setProperty(propertyName1, -2147483650L);
        vHistory.getSession().save();
        fail("VersionHistory should be read-only: VersionHistory.setProperty(String,long) did not throw a ConstraintViolationException");
    } catch (ConstraintViolationException success) {
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) Value(javax.jcr.Value) GregorianCalendar(java.util.GregorianCalendar) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException)

Example 82 with ConstraintViolationException

use of javax.jcr.nodetype.ConstraintViolationException in project jackrabbit by apache.

the class SessionImporter method resolveUUIDConflict.

// ----------------------------------------------------< Private methods >---
/**
 * @param parent
 * @param conflicting
 * @param nodeInfo
 * @return
 * @throws RepositoryException
 */
NodeState resolveUUIDConflict(NodeState parent, NodeEntry conflicting, NodeInfo nodeInfo) throws ItemExistsException, RepositoryException {
    NodeState nodeState;
    switch(uuidBehavior) {
        case ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW:
            String originalUUID = nodeInfo.getUUID();
            String newUUID = UUID.randomUUID().toString();
            // reset id on nodeInfo to force creation with new uuid:
            nodeInfo.setUUID(newUUID);
            nodeState = importNode(nodeInfo, parent);
            if (nodeState != null) {
                // remember uuid mapping
                refTracker.mappedUUIDs(originalUUID, newUUID);
            }
            break;
        case ImportUUIDBehavior.IMPORT_UUID_COLLISION_THROW:
            String msg = "a node with uuid " + nodeInfo.getUUID() + " already exists!";
            log.debug(msg);
            throw new ItemExistsException(msg);
        case ImportUUIDBehavior.IMPORT_UUID_COLLISION_REMOVE_EXISTING:
            // make sure conflicting node is not importTarget or an ancestor thereof
            Path p0 = importTarget.getPath();
            Path p1 = conflicting.getPath();
            if (p1.equals(p0) || p1.isAncestorOf(p0)) {
                msg = "cannot remove ancestor node";
                log.debug(msg);
                throw new ConstraintViolationException(msg);
            }
            // do remove conflicting (recursive) including validation check
            try {
                Operation op = Remove.create(conflicting.getNodeState());
                stateMgr.execute(op);
            } catch (ItemNotFoundException e) {
            // conflicting does not exist any more. no need for a removal
            }
            // create new with given uuid:
            nodeState = importNode(nodeInfo, parent);
            break;
        case ImportUUIDBehavior.IMPORT_UUID_COLLISION_REPLACE_EXISTING:
            if (conflicting.getNodeState().isRoot()) {
                msg = "Root node cannot be replaced";
                log.debug(msg);
                throw new RepositoryException(msg);
            }
            // 'replace' current parent with parent of conflicting
            parent = conflicting.getParent().getNodeState();
            // do remove conflicting (recursive), including validation checks
            Operation op = Remove.create(conflicting.getNodeState());
            stateMgr.execute(op);
            // create new with given uuid at same location as conflicting
            nodeState = importNode(nodeInfo, parent);
            break;
        default:
            msg = "Unknown uuidBehavior: " + uuidBehavior;
            log.debug(msg);
            throw new RepositoryException(msg);
    }
    return nodeState;
}
Also used : Path(org.apache.jackrabbit.spi.Path) NodeState(org.apache.jackrabbit.jcr2spi.state.NodeState) ItemExistsException(javax.jcr.ItemExistsException) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) RepositoryException(javax.jcr.RepositoryException) Operation(org.apache.jackrabbit.jcr2spi.operation.Operation) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Example 83 with ConstraintViolationException

use of javax.jcr.nodetype.ConstraintViolationException in project jackrabbit by apache.

the class SessionImporter method checkIncludesMixReferenceable.

/**
 * Validate the given <code>NodeInfo</code>: make sure, that if a uuid is
 * defined, the primary or the mixin types include mix:referenceable.
 *
 * @param nodeInfo
 * @throws RepositoryException
 */
private void checkIncludesMixReferenceable(Importer.NodeInfo nodeInfo) throws RepositoryException {
    List<Name> l = new ArrayList<Name>();
    l.add(nodeInfo.getNodeTypeName());
    Name[] mixinNames = nodeInfo.getMixinNames();
    if (mixinNames != null && mixinNames.length > 0) {
        l.addAll(Arrays.asList(nodeInfo.getMixinNames()));
    }
    if (l.contains(NameConstants.MIX_REFERENCEABLE)) {
        // shortcut
        return;
    }
    Name[] ntNames = l.toArray(new Name[l.size()]);
    EffectiveNodeType ent = session.getEffectiveNodeTypeProvider().getEffectiveNodeType(ntNames);
    if (!ent.includesNodeType(NameConstants.MIX_REFERENCEABLE)) {
        throw new ConstraintViolationException("XML defines jcr:uuid without defining import node to be referenceable.");
    }
}
Also used : EffectiveNodeType(org.apache.jackrabbit.jcr2spi.nodetype.EffectiveNodeType) ArrayList(java.util.ArrayList) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) Name(org.apache.jackrabbit.spi.Name)

Example 84 with ConstraintViolationException

use of javax.jcr.nodetype.ConstraintViolationException 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 85 with ConstraintViolationException

use of javax.jcr.nodetype.ConstraintViolationException in project jackrabbit by apache.

the class ShareableNodeTest method testRemoveMixinFromSharedNode.

/**
 * Remove mix:shareable from a shareable node that has 2 nodes in the shared set.
 */
public void testRemoveMixinFromSharedNode() throws Exception {
    // setup parent nodes and first child
    Node a1 = testRootNode.addNode("a1");
    Node a2 = testRootNode.addNode("a2");
    Node b1 = a1.addNode("b1");
    testRootNode.getSession().save();
    // add mixin
    ensureMixinType(b1, mixShareable);
    b1.getSession().save();
    // clone
    Workspace workspace = b1.getSession().getWorkspace();
    workspace.clone(workspace.getName(), b1.getPath(), a2.getPath() + "/b2", false);
    Node[] shared = getSharedSet(b1);
    assertEquals(2, shared.length);
    b1 = shared[0];
    Node b2 = shared[1];
    assertTrue(b2.isSame(b1));
    // (per Section 14.15 of JSR-283 specification)
    try {
        // remove mixin
        b1.removeMixin(mixShareable);
        b1.getSession().save();
        // If this happens, then b1 shouldn't be shareable anymore
        // ...
        assertFalse(b1.isNodeType(mixShareable));
        assertFalse(b2.isSame(b1));
    } catch (ConstraintViolationException e) {
    // one possible outcome if removing 'mix:shareable' isn't supported
    } catch (UnsupportedRepositoryOperationException e) {
    // also possible if the implementation doesn't support this
    // capability
    }
}
Also used : UnsupportedRepositoryOperationException(javax.jcr.UnsupportedRepositoryOperationException) Node(javax.jcr.Node) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) Workspace(javax.jcr.Workspace)

Aggregations

ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)180 Node (javax.jcr.Node)74 RepositoryException (javax.jcr.RepositoryException)40 Name (org.apache.jackrabbit.spi.Name)33 Value (javax.jcr.Value)31 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)28 Test (org.junit.Test)27 PropertyDefinition (javax.jcr.nodetype.PropertyDefinition)23 QPropertyDefinition (org.apache.jackrabbit.spi.QPropertyDefinition)23 Session (javax.jcr.Session)18 ItemExistsException (javax.jcr.ItemExistsException)17 NodeState (org.apache.jackrabbit.core.state.NodeState)16 QNodeDefinition (org.apache.jackrabbit.spi.QNodeDefinition)16 Property (javax.jcr.Property)14 NodeTypeManager (javax.jcr.nodetype.NodeTypeManager)14 ArrayList (java.util.ArrayList)13 NodeId (org.apache.jackrabbit.core.id.NodeId)13 EffectiveNodeType (org.apache.jackrabbit.core.nodetype.EffectiveNodeType)13 NodeType (javax.jcr.nodetype.NodeType)12 ChildNodeEntry (org.apache.jackrabbit.core.state.ChildNodeEntry)12