Search in sources :

Example 21 with UnsupportedRepositoryOperationException

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

the class AbstractRepositoryOperationTest method testRepoPolicyAPI.

public void testRepoPolicyAPI() throws Exception {
    try {
        // initial state: no repo level policy
        AccessControlPolicy[] policies = acMgr.getPolicies(null);
        assertNotNull(policies);
        assertEquals(0, policies.length);
        AccessControlPolicy[] effective = acMgr.getEffectivePolicies(null);
        assertNotNull(effective);
        assertEquals(0, effective.length);
        AccessControlPolicyIterator it = acMgr.getApplicablePolicies(null);
        assertNotNull(it);
        assertTrue(it.hasNext());
        AccessControlPolicy acp = it.nextAccessControlPolicy();
        assertNotNull(acp);
        assertTrue(acp instanceof JackrabbitAccessControlPolicy);
        // modify the repo level policy
        modifyPrivileges(null, NameConstants.JCR_NODE_TYPE_DEFINITION_MANAGEMENT.toString(), false);
        modifyPrivileges(null, NameConstants.JCR_NAMESPACE_MANAGEMENT.toString(), true);
        AccessControlPolicy[] plcs = acMgr.getPolicies(null);
        assertNotNull(plcs);
        assertEquals(1, plcs.length);
        assertTrue(plcs[0] instanceof AccessControlList);
        AccessControlList acl = (AccessControlList) plcs[0];
        AccessControlEntry[] aces = acl.getAccessControlEntries();
        assertNotNull(aces);
        assertEquals(2, aces.length);
        assertPrivilege(NameConstants.JCR_NAMESPACE_MANAGEMENT, true);
        assertPermission(Permission.NAMESPACE_MNGMT, true);
        assertPrivilege(NameConstants.JCR_NODE_TYPE_DEFINITION_MANAGEMENT, false);
        assertPermission(Permission.NODE_TYPE_DEF_MNGMT, false);
        effective = acMgr.getEffectivePolicies(null);
        assertNotNull(effective);
        assertEquals(1, effective.length);
        assertTrue(effective[0] instanceof AccessControlList);
        acl = (AccessControlList) effective[0];
        aces = acl.getAccessControlEntries();
        assertNotNull(aces);
        assertEquals(2, aces.length);
        // change the policy: removing the second entry in the access control list
        acl = (AccessControlList) acMgr.getPolicies(null)[0];
        AccessControlEntry toRemove = acl.getAccessControlEntries()[1];
        acl.removeAccessControlEntry(toRemove);
        acMgr.setPolicy(null, acl);
        superuser.save();
        acl = (AccessControlList) acMgr.getPolicies(null)[0];
        aces = acl.getAccessControlEntries();
        assertNotNull(aces);
        assertEquals(1, aces.length);
        assertPrivilege(NameConstants.JCR_NAMESPACE_MANAGEMENT, false);
        assertPermission(Permission.NAMESPACE_MNGMT, false);
        assertPrivilege(NameConstants.JCR_NODE_TYPE_DEFINITION_MANAGEMENT, false);
        assertPermission(Permission.NODE_TYPE_DEF_MNGMT, false);
    } catch (UnsupportedRepositoryOperationException e) {
        throw new NotExecutableException();
    } finally {
        // remove it again
        for (AccessControlPolicy plc : acMgr.getPolicies(null)) {
            acMgr.removePolicy(null, plc);
        }
        superuser.save();
        // back to initial state: no repo level policy
        AccessControlPolicy[] policies = acMgr.getPolicies(null);
        assertNotNull(policies);
        assertEquals(0, policies.length);
        AccessControlPolicy[] effective = acMgr.getEffectivePolicies(null);
        assertNotNull(effective);
        assertEquals(0, effective.length);
        AccessControlPolicyIterator it = acMgr.getApplicablePolicies(null);
        assertNotNull(it);
        assertTrue(it.hasNext());
        AccessControlPolicy acp = it.nextAccessControlPolicy();
        assertNotNull(acp);
        assertTrue(acp instanceof JackrabbitAccessControlPolicy);
    }
}
Also used : AccessControlList(javax.jcr.security.AccessControlList) UnsupportedRepositoryOperationException(javax.jcr.UnsupportedRepositoryOperationException) JackrabbitAccessControlPolicy(org.apache.jackrabbit.api.security.JackrabbitAccessControlPolicy) AccessControlPolicy(javax.jcr.security.AccessControlPolicy) NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) AccessControlEntry(javax.jcr.security.AccessControlEntry) AccessControlPolicyIterator(javax.jcr.security.AccessControlPolicyIterator) JackrabbitAccessControlPolicy(org.apache.jackrabbit.api.security.JackrabbitAccessControlPolicy)

Example 22 with UnsupportedRepositoryOperationException

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

the class AbstractJCRTest method cleanUpTestRoot.

/**
     * Reverts any pending changes made by <code>s</code> and deletes any nodes
     * under {@link #testRoot}. If there is no node at {@link #testRoot} then
     * the necessary nodes are created.
     *
     * @param s the session to clean up.
     * @return the {@link javax.jcr.Node} that represents the test root.
     * @throws RepositoryException if an error occurs.
     */
protected Node cleanUpTestRoot(Session s) throws RepositoryException {
    // do a 'rollback'
    s.refresh(false);
    Node root = s.getRootNode();
    Node testRootNode;
    if (root.hasNode(testPath)) {
        RetentionManager rm;
        try {
            rm = s.getRetentionManager();
        } catch (UnsupportedRepositoryOperationException e) {
            rm = null;
        }
        // clean test root
        testRootNode = root.getNode(testPath);
        NodeIterator children = testRootNode.getNodes();
        while (children.hasNext()) {
            Node child = children.nextNode();
            // Remove retention policy if needed
            String childPath = child.getPath();
            if (rm != null && rm.getRetentionPolicy(childPath) != null) {
                rm.removeRetentionPolicy(childPath);
                s.save();
            }
            NodeDefinition nodeDef = child.getDefinition();
            if (!nodeDef.isMandatory() && !nodeDef.isProtected()) {
                // try to remove child
                try {
                    child.remove();
                } catch (ConstraintViolationException e) {
                    log.println("unable to remove node: " + child.getPath());
                }
            }
        }
    } else {
        // create nodes to testPath
        StringTokenizer names = new StringTokenizer(testPath, "/");
        Node currentNode = root;
        while (names.hasMoreTokens()) {
            String name = names.nextToken();
            if (currentNode.hasNode(name)) {
                currentNode = currentNode.getNode(name);
            } else {
                currentNode = currentNode.addNode(name, testNodeTypeTestRoot);
            }
        }
        testRootNode = currentNode;
    }
    s.save();
    return testRootNode;
}
Also used : RetentionManager(javax.jcr.retention.RetentionManager) NodeIterator(javax.jcr.NodeIterator) UnsupportedRepositoryOperationException(javax.jcr.UnsupportedRepositoryOperationException) StringTokenizer(java.util.StringTokenizer) Node(javax.jcr.Node) NodeDefinition(javax.jcr.nodetype.NodeDefinition) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException)

Example 23 with UnsupportedRepositoryOperationException

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

the class AccessControlProviderStub method newInstance.

/**
	 * Instantiates and returns a concrete AccessControlProvider implementation.
	 * 
	 * @param config
	 *            The RepositoryConfig to read configuration parameters.
	 * @return
	 * @throws RepositoryException
	 */
public static AccessControlProvider newInstance(RepositoryConfig config) throws RepositoryException {
    String className = getProviderClass(config);
    if (className != null) {
        try {
            Class<?> acProviderClass = Class.forName(className);
            if (AccessControlProvider.class.isAssignableFrom(acProviderClass)) {
                AccessControlProvider acProvider = (AccessControlProvider) acProviderClass.newInstance();
                acProvider.init(config);
                return acProvider;
            } else {
                throw new RepositoryException("Fail to create AccessControlProvider from configuration.");
            }
        } catch (Exception e) {
            throw new RepositoryException("Fail to create AccessControlProvider from configuration.");
        }
    }
    // ac not supported in this setup.
    throw new UnsupportedRepositoryOperationException("Access control is not supported");
}
Also used : UnsupportedRepositoryOperationException(javax.jcr.UnsupportedRepositoryOperationException) RepositoryException(javax.jcr.RepositoryException) UnsupportedRepositoryOperationException(javax.jcr.UnsupportedRepositoryOperationException) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException)

Example 24 with UnsupportedRepositoryOperationException

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

the class RepositoryServiceImpl method getEvents.

/**
     * {@inheritDoc}
     */
public EventBundle getEvents(SessionInfo sessionInfo, EventFilter filter, long after) throws RepositoryException, UnsupportedRepositoryOperationException {
    SessionInfoImpl sInfo = getSessionInfoImpl(sessionInfo);
    EventJournal journal = sInfo.getSession().getWorkspace().getObservationManager().getEventJournal();
    if (journal == null) {
        throw new UnsupportedRepositoryOperationException();
    }
    EventFactory factory = new EventFactory(sInfo.getSession(), sInfo.getNamePathResolver(), idFactory, qValueFactory);
    journal.skipTo(after);
    List<Event> events = new ArrayList<Event>();
    int batchSize = 1024;
    boolean distinctDates = true;
    long lastDate = Long.MIN_VALUE;
    while (journal.hasNext() && (batchSize > 0 || !distinctDates)) {
        Event e = factory.fromJCREvent(journal.nextEvent());
        if (filter.accept(e, false)) {
            distinctDates = lastDate != e.getDate();
            lastDate = e.getDate();
            events.add(e);
            batchSize--;
        }
    }
    return new EventBundleImpl(events, false);
}
Also used : UnsupportedRepositoryOperationException(javax.jcr.UnsupportedRepositoryOperationException) EventBundleImpl(org.apache.jackrabbit.spi.commons.EventBundleImpl) ArrayList(java.util.ArrayList) Event(org.apache.jackrabbit.spi.Event) EventJournal(javax.jcr.observation.EventJournal)

Example 25 with UnsupportedRepositoryOperationException

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

the class NodeTypeManagerImpl method registerNodeTypes.

/**
     * Registers the node types defined in the given input stream depending
     * on the content type specified for the stream. This will also register
     * any namespaces identified in the input stream if they have not already
     * been registered.
     *
     * @param in node type XML stream
     * @param contentType type of the input stream
     * @param reregisterExisting flag indicating whether node types should be
     *                           reregistered if they already exist
     * @return registered node types
     * @throws IOException if the input stream could not be read or parsed
     * @throws RepositoryException if the node types are invalid or another
     *                             repository error occurs
     */
public NodeType[] registerNodeTypes(InputStream in, String contentType, boolean reregisterExisting) throws IOException, RepositoryException {
    // make sure the editing session is allowed to register node types.
    context.getAccessManager().checkRepositoryPermission(Permission.NODE_TYPE_DEF_MNGMT);
    try {
        Map<String, String> namespaceMap = new HashMap<String, String>();
        List<QNodeTypeDefinition> nodeTypeDefs = new ArrayList<QNodeTypeDefinition>();
        if (contentType.equalsIgnoreCase(TEXT_XML) || contentType.equalsIgnoreCase(APPLICATION_XML)) {
            try {
                NodeTypeReader ntr = new NodeTypeReader(in);
                Properties namespaces = ntr.getNamespaces();
                if (namespaces != null) {
                    Enumeration<?> prefixes = namespaces.propertyNames();
                    while (prefixes.hasMoreElements()) {
                        String prefix = (String) prefixes.nextElement();
                        String uri = namespaces.getProperty(prefix);
                        namespaceMap.put(prefix, uri);
                    }
                }
                QNodeTypeDefinition[] defs = ntr.getNodeTypeDefs();
                nodeTypeDefs.addAll(Arrays.asList(defs));
            } catch (NameException e) {
                throw new RepositoryException("Illegal JCR name", e);
            }
        } else if (contentType.equalsIgnoreCase(TEXT_X_JCR_CND)) {
            try {
                NamespaceMapping mapping = new NamespaceMapping(context.getSessionImpl());
                CompactNodeTypeDefReader<QNodeTypeDefinition, NamespaceMapping> reader = new CompactNodeTypeDefReader<QNodeTypeDefinition, NamespaceMapping>(new InputStreamReader(in), "cnd input stream", mapping, new QDefinitionBuilderFactory());
                namespaceMap.putAll(mapping.getPrefixToURIMapping());
                for (QNodeTypeDefinition ntDef : reader.getNodeTypeDefinitions()) {
                    nodeTypeDefs.add(ntDef);
                }
            } catch (ParseException e) {
                IOException e2 = new IOException(e.getMessage());
                e2.initCause(e);
                throw e2;
            }
        } else {
            throw new UnsupportedRepositoryOperationException("Unsupported content type: " + contentType);
        }
        new NamespaceHelper(context.getSessionImpl()).registerNamespaces(namespaceMap);
        if (reregisterExisting) {
            NodeTypeRegistry registry = context.getNodeTypeRegistry();
            // split the node types into new and already registered node types.
            // this way we can register new node types together with already
            // registered node types which make circular dependencies possible
            List<QNodeTypeDefinition> newNodeTypeDefs = new ArrayList<QNodeTypeDefinition>();
            List<QNodeTypeDefinition> registeredNodeTypeDefs = new ArrayList<QNodeTypeDefinition>();
            for (QNodeTypeDefinition nodeTypeDef : nodeTypeDefs) {
                if (registry.isRegistered(nodeTypeDef.getName())) {
                    registeredNodeTypeDefs.add(nodeTypeDef);
                } else {
                    newNodeTypeDefs.add(nodeTypeDef);
                }
            }
            ArrayList<NodeType> nodeTypes = new ArrayList<NodeType>();
            // register new node types
            nodeTypes.addAll(registerNodeTypes(newNodeTypeDefs));
            // re-register already existing node types
            for (QNodeTypeDefinition nodeTypeDef : registeredNodeTypeDefs) {
                registry.reregisterNodeType(nodeTypeDef);
                nodeTypes.add(getNodeType(nodeTypeDef.getName()));
            }
            return nodeTypes.toArray(new NodeType[nodeTypes.size()]);
        } else {
            Collection<NodeType> types = registerNodeTypes(nodeTypeDefs);
            return types.toArray(new NodeType[types.size()]);
        }
    } catch (InvalidNodeTypeDefException e) {
        throw new RepositoryException("Invalid node type definition", e);
    }
}
Also used : UnsupportedRepositoryOperationException(javax.jcr.UnsupportedRepositoryOperationException) HashMap(java.util.HashMap) NamespaceMapping(org.apache.jackrabbit.spi.commons.namespace.NamespaceMapping) ArrayList(java.util.ArrayList) Properties(java.util.Properties) QNodeTypeDefinition(org.apache.jackrabbit.spi.QNodeTypeDefinition) NamespaceHelper(org.apache.jackrabbit.commons.NamespaceHelper) QDefinitionBuilderFactory(org.apache.jackrabbit.spi.commons.nodetype.QDefinitionBuilderFactory) CompactNodeTypeDefReader(org.apache.jackrabbit.commons.cnd.CompactNodeTypeDefReader) InputStreamReader(java.io.InputStreamReader) NodeTypeReader(org.apache.jackrabbit.core.nodetype.xml.NodeTypeReader) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) NameException(org.apache.jackrabbit.spi.commons.conversion.NameException) NodeType(javax.jcr.nodetype.NodeType) ParseException(org.apache.jackrabbit.commons.cnd.ParseException)

Aggregations

UnsupportedRepositoryOperationException (javax.jcr.UnsupportedRepositoryOperationException)57 RepositoryException (javax.jcr.RepositoryException)17 Node (javax.jcr.Node)14 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)12 NodeId (org.apache.jackrabbit.core.id.NodeId)8 ArrayList (java.util.ArrayList)7 Workspace (javax.jcr.Workspace)5 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)5 Session (javax.jcr.Session)4 VersionManager (javax.jcr.version.VersionManager)4 NodeState (org.apache.jackrabbit.core.state.NodeState)4 Name (org.apache.jackrabbit.spi.Name)4 Path (org.apache.jackrabbit.spi.Path)4 IOException (java.io.IOException)3 ItemExistsException (javax.jcr.ItemExistsException)3 ItemNotFoundException (javax.jcr.ItemNotFoundException)3 NodeType (javax.jcr.nodetype.NodeType)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 AccessDeniedException (javax.jcr.AccessDeniedException)2