Search in sources :

Example 21 with NodeTypeTemplate

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

the class RepositoryTest method importWithRegisteredType.

// OAK-2038
@Test
public void importWithRegisteredType() throws Exception {
    Session session = getAdminSession();
    NodeTypeManager ntMgr = getAdminSession().getWorkspace().getNodeTypeManager();
    NodeTypeTemplate ntd = ntMgr.createNodeTypeTemplate();
    ntd.setName("fooType");
    PropertyDefinitionTemplate propDefTemplate = ntMgr.createPropertyDefinitionTemplate();
    propDefTemplate.setName("fooProp");
    propDefTemplate.setRequiredType(PropertyType.STRING);
    ntd.getPropertyDefinitionTemplates().add(propDefTemplate);
    ntMgr.registerNodeType(ntd, false);
    Node node = session.getRootNode().addNode("node", "fooType");
    node.setProperty("fooProp", "fooValue");
    session.save();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    session.exportDocumentView("/node", out, true, false);
    node.remove();
    session.save();
    session.getWorkspace().importXML("/", new ByteArrayInputStream(out.toByteArray()), IMPORT_UUID_CREATE_NEW);
    session.save();
    assertEquals("fooValue", session.getProperty("/node/fooProp").getString());
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) PropertyDefinitionTemplate(javax.jcr.nodetype.PropertyDefinitionTemplate) ByteArrayInputStream(java.io.ByteArrayInputStream) JackrabbitNode(org.apache.jackrabbit.api.JackrabbitNode) Node(javax.jcr.Node) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Session(javax.jcr.Session) Test(org.junit.Test)

Example 22 with NodeTypeTemplate

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

the class CompatibilityIssuesTest method testBinaryCoercion.

@Test
public void testBinaryCoercion() throws RepositoryException, IOException {
    Session session = getAdminSession();
    // node type with default child-node type of to nt:base
    String ntName = "binaryCoercionTest";
    NodeTypeManager ntm = session.getWorkspace().getNodeTypeManager();
    NodeTypeTemplate ntt = ntm.createNodeTypeTemplate();
    ntt.setName(ntName);
    PropertyDefinitionTemplate propertyWithType = ntm.createPropertyDefinitionTemplate();
    propertyWithType.setName("javaObject");
    propertyWithType.setRequiredType(PropertyType.STRING);
    PropertyDefinitionTemplate unnamed = ntm.createPropertyDefinitionTemplate();
    unnamed.setName("*");
    unnamed.setRequiredType(PropertyType.UNDEFINED);
    List<PropertyDefinition> properties = ntt.getPropertyDefinitionTemplates();
    properties.add(propertyWithType);
    properties.add(unnamed);
    ntm.registerNodeType(ntt, false);
    Node node = session.getRootNode().addNode("testNodeForBinary", ntName);
    ByteArrayOutputStream bos = serializeObject("testValue");
    node.setProperty("javaObject", session.getValueFactory().createBinary(new ByteArrayInputStream(bos.toByteArray())));
    Assert.assertTrue(IOUtils.contentEquals(new ByteArrayInputStream(bos.toByteArray()), node.getProperty("javaObject").getStream()));
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) PropertyDefinitionTemplate(javax.jcr.nodetype.PropertyDefinitionTemplate) ByteArrayInputStream(java.io.ByteArrayInputStream) Node(javax.jcr.Node) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition) Session(javax.jcr.Session) Test(org.junit.Test)

Example 23 with NodeTypeTemplate

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

the class AbstractRepositoryOperationTest method testRegisterNodeTypeWithPrivilege.

public void testRegisterNodeTypeWithPrivilege() throws Exception {
    assertDefaultPrivileges(NameConstants.JCR_NODE_TYPE_DEFINITION_MANAGEMENT);
    assertPermission(Permission.NODE_TYPE_DEF_MNGMT, false);
    modifyPrivileges(null, NameConstants.JCR_NODE_TYPE_DEFINITION_MANAGEMENT.toString(), true);
    assertPrivilege(NameConstants.JCR_NODE_TYPE_DEFINITION_MANAGEMENT, true);
    assertPermission(Permission.NODE_TYPE_DEF_MNGMT, true);
    try {
        Workspace testWsp = getTestWorkspace();
        NodeTypeManager ntm = testWsp.getNodeTypeManager();
        NodeTypeTemplate ntd = ntm.createNodeTypeTemplate();
        ntd.setName("testNodeType");
        ntd.setMixin(true);
        ntm.registerNodeType(ntd, true);
        NodeTypeTemplate[] ntds = new NodeTypeTemplate[2];
        ntds[0] = ntd;
        ntds[1] = ntm.createNodeTypeTemplate();
        ntds[1].setName("anotherNodeType");
        ntds[1].setDeclaredSuperTypeNames(new String[] { "nt:file" });
        ntm.registerNodeTypes(ntds, true);
    } finally {
        modifyPrivileges(null, NameConstants.JCR_NODE_TYPE_DEFINITION_MANAGEMENT.toString(), false);
    }
    assertPrivilege(NameConstants.JCR_NODE_TYPE_DEFINITION_MANAGEMENT, false);
    assertPermission(Permission.NODE_TYPE_DEF_MNGMT, false);
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) JackrabbitWorkspace(org.apache.jackrabbit.api.JackrabbitWorkspace) Workspace(javax.jcr.Workspace)

Example 24 with NodeTypeTemplate

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

the class AbstractRepositoryOperationTest method testUnRegisterNodeType.

public void testUnRegisterNodeType() throws Exception {
    assertDefaultPrivileges(NameConstants.JCR_NODE_TYPE_DEFINITION_MANAGEMENT);
    assertPermission(Permission.NODE_TYPE_DEF_MNGMT, false);
    NodeTypeManager ntm = superuser.getWorkspace().getNodeTypeManager();
    NodeTypeTemplate ntd = ntm.createNodeTypeTemplate();
    ntd.setName("testNodeType");
    ntd.setMixin(true);
    ntm.registerNodeType(ntd, true);
    Workspace testWsp = getTestWorkspace();
    try {
        try {
            NodeTypeManager testNtm = testWsp.getNodeTypeManager();
            testNtm.unregisterNodeType(ntd.getName());
            fail("Namespace unregistration should be denied.");
        } catch (AccessDeniedException e) {
        // success
        }
        try {
            NodeTypeManager testNtm = testWsp.getNodeTypeManager();
            testNtm.unregisterNodeTypes(new String[] { ntd.getName() });
            fail("Namespace unregistration should be denied.");
        } catch (AccessDeniedException e) {
        // success
        }
    } finally {
        // clean up (not supported by jackrabbit-core)
        try {
            ntm.unregisterNodeType(ntd.getName());
        } catch (Exception e) {
        // ns unregistration is not supported by jackrabbit-core.
        }
    }
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) AccessDeniedException(javax.jcr.AccessDeniedException) NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) AccessDeniedException(javax.jcr.AccessDeniedException) RepositoryException(javax.jcr.RepositoryException) UnsupportedRepositoryOperationException(javax.jcr.UnsupportedRepositoryOperationException) NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) JackrabbitWorkspace(org.apache.jackrabbit.api.JackrabbitWorkspace) Workspace(javax.jcr.Workspace)

Example 25 with NodeTypeTemplate

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

the class WorkspaceResourceImpl method setProperty.

/**
     * Allows to alter the registered namespaces ({@link ItemResourceConstants#JCR_NAMESPACES})
     * or register node types {@link ItemResourceConstants#JCR_NODETYPES_CND}
     * where the passed value is a cnd string containing the definition
     * and forwards any other property to the super class.
     * <p>
     * Note that again no property status is set. Any failure while setting
     * a property results in an exception (violating RFC 2518).
     *
     * @param property
     * @throws DavException
     * @see DavResource#setProperty(org.apache.jackrabbit.webdav.property.DavProperty)
     */
@Override
public void setProperty(DavProperty<?> property) throws DavException {
    if (ItemResourceConstants.JCR_NAMESPACES.equals(property.getName())) {
        NamespacesProperty nsp = new NamespacesProperty(property);
        try {
            Map<String, String> changes = new HashMap<String, String>(nsp.getNamespaces());
            NamespaceRegistry nsReg = getRepositorySession().getWorkspace().getNamespaceRegistry();
            for (String prefix : nsReg.getPrefixes()) {
                if (!changes.containsKey(prefix)) {
                    // prefix not present amongst the new values any more > unregister
                    nsReg.unregisterNamespace(prefix);
                } else if (changes.get(prefix).equals(nsReg.getURI(prefix))) {
                    // present with same uri-value >> no action required
                    changes.remove(prefix);
                }
            }
            // it has not been present before.
            for (String prefix : changes.keySet()) {
                String uri = changes.get(prefix);
                nsReg.registerNamespace(prefix, uri);
            }
        } catch (RepositoryException e) {
            throw new JcrDavException(e);
        }
    } else if (ItemResourceConstants.JCR_NODETYPES_CND.equals(property.getName())) {
        try {
            Object value = property.getValue();
            List<?> cmds;
            if (value instanceof List) {
                cmds = (List) value;
            } else if (value instanceof Element) {
                cmds = Collections.singletonList(value);
            } else {
                log.warn("Unexpected structure of dcr:nodetypes-cnd property.");
                throw new DavException(DavServletResponse.SC_INTERNAL_SERVER_ERROR);
            }
            String registerCnd = null;
            boolean allowUpdate = false;
            List<String> unregisterNames = new ArrayList<String>();
            for (Object listEntry : cmds) {
                if (listEntry instanceof Element) {
                    Element e = (Element) listEntry;
                    String localName = e.getLocalName();
                    if (ItemResourceConstants.XML_CND.equals(localName)) {
                        registerCnd = DomUtil.getText(e);
                    } else if (ItemResourceConstants.XML_ALLOWUPDATE.equals(localName)) {
                        String allow = DomUtil.getTextTrim(e);
                        allowUpdate = Boolean.parseBoolean(allow);
                    } else if (ItemResourceConstants.XML_NODETYPENAME.equals(localName)) {
                        unregisterNames.add(DomUtil.getTextTrim(e));
                    }
                }
            }
            // TODO: for simplicity it's currently either registration or unregistration as nt-modifications are immediately persisted.
            Session s = getRepositorySession();
            NodeTypeManager ntMgr = s.getWorkspace().getNodeTypeManager();
            if (registerCnd != null) {
                StringReader reader = new StringReader(registerCnd);
                DefinitionBuilderFactory<NodeTypeTemplate, NamespaceRegistry> factory = new TemplateBuilderFactory(ntMgr, s.getValueFactory(), s.getWorkspace().getNamespaceRegistry());
                CompactNodeTypeDefReader<NodeTypeTemplate, NamespaceRegistry> cndReader = new CompactNodeTypeDefReader<NodeTypeTemplate, NamespaceRegistry>(reader, "davex", factory);
                List<NodeTypeTemplate> ntts = cndReader.getNodeTypeDefinitions();
                ntMgr.registerNodeTypes(ntts.toArray(new NodeTypeTemplate[ntts.size()]), allowUpdate);
            } else if (!unregisterNames.isEmpty()) {
                ntMgr.unregisterNodeTypes(unregisterNames.toArray(new String[unregisterNames.size()]));
            }
        } catch (ParseException e) {
            throw new DavException(DavServletResponse.SC_BAD_REQUEST, e);
        } catch (RepositoryException e) {
            throw new JcrDavException(e);
        }
    } else {
        // only jcr:namespace or node types can be modified
        throw new DavException(DavServletResponse.SC_CONFLICT);
    }
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) HashMap(java.util.HashMap) DavException(org.apache.jackrabbit.webdav.DavException) Element(org.w3c.dom.Element) TemplateBuilderFactory(org.apache.jackrabbit.commons.cnd.TemplateBuilderFactory) RepositoryException(javax.jcr.RepositoryException) NamespaceRegistry(javax.jcr.NamespaceRegistry) NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) StringReader(java.io.StringReader) DefinitionBuilderFactory(org.apache.jackrabbit.commons.cnd.DefinitionBuilderFactory) List(java.util.List) ArrayList(java.util.ArrayList) ParseException(org.apache.jackrabbit.commons.cnd.ParseException) NamespacesProperty(org.apache.jackrabbit.webdav.jcr.property.NamespacesProperty) CompactNodeTypeDefReader(org.apache.jackrabbit.commons.cnd.CompactNodeTypeDefReader) Session(javax.jcr.Session)

Aggregations

NodeTypeTemplate (javax.jcr.nodetype.NodeTypeTemplate)46 NodeTypeManager (javax.jcr.nodetype.NodeTypeManager)29 Node (javax.jcr.Node)17 PropertyDefinitionTemplate (javax.jcr.nodetype.PropertyDefinitionTemplate)13 Test (org.junit.Test)12 Session (javax.jcr.Session)11 NodeDefinitionTemplate (javax.jcr.nodetype.NodeDefinitionTemplate)11 List (java.util.List)7 Workspace (javax.jcr.Workspace)7 NamespaceRegistry (javax.jcr.NamespaceRegistry)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)5 NodeTypeDefinition (javax.jcr.nodetype.NodeTypeDefinition)5 JackrabbitWorkspace (org.apache.jackrabbit.api.JackrabbitWorkspace)5 InputStream (java.io.InputStream)4 ArrayList (java.util.ArrayList)4 AccessDeniedException (javax.jcr.AccessDeniedException)4 RepositoryException (javax.jcr.RepositoryException)4 JackrabbitNode (org.apache.jackrabbit.api.JackrabbitNode)4 BigDecimal (java.math.BigDecimal)3