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());
}
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()));
}
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);
}
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.
}
}
}
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);
}
}
Aggregations