use of javax.jcr.nodetype.NodeTypeManager 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);
}
}
use of javax.jcr.nodetype.NodeTypeManager in project jackrabbit by apache.
the class SetValueConstraintViolationExceptionTest method testReferenceProperty.
/**
* Tests if setValue(Node value) and setValue(Value value) where value is a
* ReferenceValue throw a ConstraintViolationException if the change would
* violate a value constraint
*/
public void testReferenceProperty() throws NotExecutableException, RepositoryException {
// locate a PropertyDefinition with ValueConstraints
PropertyDefinition propDef = NodeTypeUtil.locatePropertyDef(superuser, PropertyType.REFERENCE, false, false, true, false);
if (propDef == null) {
throw new NotExecutableException("No reference property def with " + "testable value constraints has been found");
}
String[] valueConstraints = propDef.getValueConstraints();
if (valueConstraints == null || valueConstraints.length == 0) {
throw new NotExecutableException("No reference property def with " + "testable value constraints has been found");
}
List<String> constraints = Arrays.asList(valueConstraints);
String nodeTypeSatisfied = constraints.get(0);
String nodeTypeNotSatisfied = null;
NodeTypeManager manager = superuser.getWorkspace().getNodeTypeManager();
NodeTypeIterator types = manager.getAllNodeTypes();
// find a NodeType which is not satisfying the constraints
while (types.hasNext()) {
NodeType type = types.nextNodeType();
String name = type.getName();
if (constraints.contains(name) || ntFrozenNode.equals(name)) {
continue;
}
if (type.getChildNodeDefinitions() != null && type.getChildNodeDefinitions().length > 0) {
continue;
}
nodeTypeNotSatisfied = name;
break;
}
if (nodeTypeNotSatisfied == null) {
throw new NotExecutableException("No reference property def with " + "testable value constraints has been found");
}
// create a sub node of testRootNode of type propDef.getDeclaringNodeType()
// and add a property with constraints to this node
Node node;
Property prop;
Node nodeSatisfied;
Node nodeNotSatisfied;
try {
String nodeType = propDef.getDeclaringNodeType().getName();
node = testRootNode.addNode(nodeName2, nodeType);
// create a referenceable node satisfying the constraint
nodeSatisfied = testRootNode.addNode(nodeName3, nodeTypeSatisfied);
ensureMixinType(nodeSatisfied, mixReferenceable);
// create a referenceable node not satisfying the constraint
nodeNotSatisfied = testRootNode.addNode(nodeName4, nodeTypeNotSatisfied);
ensureMixinType(nodeNotSatisfied, mixReferenceable);
// some implementations may require a save after addMixin()
testRootNode.getSession().save();
prop = node.setProperty(propDef.getName(), nodeSatisfied);
testRootNode.getSession().save();
} catch (ConstraintViolationException e) {
// implementation specific constraints do not allow to set up test environment
throw new NotExecutableException("Not able to create required test items.");
}
// test of signature setValue(Node value)
try {
prop.setValue(nodeNotSatisfied);
node.save();
fail("setValue(Node value) must throw a ConstraintViolationException " + "if the change would violate a node type constraint " + "either immediately or on save");
} catch (ConstraintViolationException e) {
// success
}
// test of signature setValue(Value value)
try {
prop.setValue(superuser.getValueFactory().createValue(nodeNotSatisfied));
node.save();
fail("setValue(Value value) must throw a ConstraintViolationException " + "if the change would violate a node type constraint " + "either immediately or on save");
} catch (ConstraintViolationException e) {
// success
}
}
use of javax.jcr.nodetype.NodeTypeManager in project jackrabbit by apache.
the class NodeTypeUtil method locateChildNodeDef.
/**
* Locate a non-protected child node def declared by a non-abstract node type
* parsing all node types
*
* @param session the session to access the node types
* @param regardDefaultPrimaryType if true, the default primary type of the
* returned <code>NodeDef</code> is
* according to param <code>defaultPrimaryType</code>.
* If false, the returned <code>NodeDef</code>
* might have a default primary type or
* not.
* @param defaultPrimaryType if <code>regardDefaultPrimaryType</code>
* is true: if true, the returned
* <code>NodeDef</code> has a default
* primary type, else not
* @param residual if true, the returned <code>NodeDef</code>
* is of the residual name "*", else not
* @return
* @throws RepositoryException
*/
public static NodeDefinition locateChildNodeDef(Session session, boolean regardDefaultPrimaryType, boolean defaultPrimaryType, boolean residual) throws RepositoryException {
NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();
NodeTypeIterator types = manager.getAllNodeTypes();
boolean skip = false;
while (types.hasNext()) {
NodeType type = types.nextNodeType();
// node types with more than one residual child node definition
// will cause trouble in test cases. the implementation
// might pick another definition than the definition returned by
// this method, when a child node is set.
NodeDefinition[] childDefs = type.getChildNodeDefinitions();
int residuals = 0;
for (int i = 0; i < childDefs.length; i++) {
if (childDefs[i].getName().equals("*")) {
residuals++;
}
}
if (residuals > 1) {
// more than one residual, not suitable for tests
continue;
}
NodeDefinition[] nodeDefs = type.getDeclaredChildNodeDefinitions();
for (int i = 0; i < nodeDefs.length; i++) {
NodeDefinition nodeDef = nodeDefs[i];
if (nodeDef.getDeclaringNodeType().isAbstract()) {
continue;
}
if (nodeDef.isProtected()) {
continue;
}
if (nodeDef.getRequiredPrimaryTypes().length > 1) {
// of primary node types is not specified
continue;
}
if (regardDefaultPrimaryType) {
if (defaultPrimaryType && nodeDef.getDefaultPrimaryType() == null) {
continue;
}
if (!defaultPrimaryType && nodeDef.getDefaultPrimaryType() != null) {
continue;
}
}
if (residual && !nodeDef.getName().equals("*")) {
continue;
}
if (!residual) {
// if another child node def is a residual definition
// skip the current node type
NodeDefinition[] nodeDefsAll = type.getChildNodeDefinitions();
for (int j = 0; j < nodeDefsAll.length; j++) {
if (nodeDefsAll[j].getName().equals("*")) {
skip = true;
break;
}
}
if (skip) {
// break the loop of the current child not defs
skip = false;
break;
}
}
return nodeDef;
}
}
return null;
}
use of javax.jcr.nodetype.NodeTypeManager in project jackrabbit by apache.
the class NodeTypeUtil method locateAllChildNodeDef.
/**
* Locate all non-protected child node def declared by a non-abstract node type
* parsing all node types
*
* @param session the session to access the node types
* @param regardDefaultPrimaryType if true, the default primary type of the
* returned <code>NodeDef</code> is
* according to param <code>defaultPrimaryType</code>.
* If false, the returned <code>NodeDef</code>
* might have a default primary type or
* not.
* @param defaultPrimaryType if <code>regardDefaultPrimaryType</code>
* is true: if true, the returned
* <code>NodeDef</code> has a default
* primary type, else not
* @param residual if true, the returned <code>NodeDef</code>
* is of the residual name "*", else not
* @return
* @throws RepositoryException
*/
public static List<NodeDefinition> locateAllChildNodeDef(Session session, boolean regardDefaultPrimaryType, boolean defaultPrimaryType, boolean residual) throws RepositoryException {
List<NodeDefinition> nodeTypes = new ArrayList<NodeDefinition>();
NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();
NodeTypeIterator types = manager.getAllNodeTypes();
boolean skip = false;
while (types.hasNext()) {
NodeType type = types.nextNodeType();
// node types with more than one residual child node definition
// will cause trouble in test cases. the implementation
// might pick another definition than the definition returned by
// this method, when a child node is set.
NodeDefinition[] childDefs = type.getChildNodeDefinitions();
int residuals = 0;
for (int i = 0; i < childDefs.length; i++) {
if (childDefs[i].getName().equals("*")) {
residuals++;
}
}
if (residuals > 1) {
// more than one residual, not suitable for tests
continue;
}
NodeDefinition[] nodeDefs = type.getDeclaredChildNodeDefinitions();
for (int i = 0; i < nodeDefs.length; i++) {
NodeDefinition nodeDef = nodeDefs[i];
if (nodeDef.getDeclaringNodeType().isAbstract()) {
continue;
}
if (nodeDef.isProtected()) {
continue;
}
if (nodeDef.getRequiredPrimaryTypes().length > 1) {
// of primary node types is not specified
continue;
}
if (regardDefaultPrimaryType) {
if (defaultPrimaryType && nodeDef.getDefaultPrimaryType() == null) {
continue;
}
if (!defaultPrimaryType && nodeDef.getDefaultPrimaryType() != null) {
continue;
}
}
if (residual && !nodeDef.getName().equals("*")) {
continue;
}
if (!residual) {
// if another child node def is a residual definition
// skip the current node type
NodeDefinition[] nodeDefsAll = type.getChildNodeDefinitions();
for (int j = 0; j < nodeDefsAll.length; j++) {
if (nodeDefsAll[j].getName().equals("*")) {
skip = true;
break;
}
}
if (skip) {
// break the loop of the current child not defs
skip = false;
break;
}
}
nodeTypes.add(nodeDef);
}
}
return nodeTypes;
}
use of javax.jcr.nodetype.NodeTypeManager in project jackrabbit by apache.
the class AbstractMergeTest method setUp.
/**
* Initialising used variables coming from the properties file.<br> Setup
* some nodes on the 2 workspaces.<br>
*/
protected void setUp() throws Exception {
super.setUp();
super.checkSupportedOption(Repository.OPTION_VERSIONING_SUPPORTED);
NodeTypeManager ntm = superuser.getWorkspace().getNodeTypeManager();
// versionable node type
versionableNodeType = getProperty(PROP_VERSIONABLE_NODE_TYPE);
if (versionableNodeType == null) {
fail("Property '" + PROP_VERSIONABLE_NODE_TYPE + "' is not defined.");
}
NodeType vNt = ntm.getNodeType(versionableNodeType);
if (!vNt.isNodeType(mixVersionable)) {
fail("Property '" + PROP_VERSIONABLE_NODE_TYPE + "' does not define a versionable nodetype.");
}
// non versionable node type
// test node type defines always a non versionable node type
nonVersionableNodeType = testNodeType;
if (nonVersionableNodeType == null) {
fail("Property '" + testNodeType + "' is not defined.");
}
NodeType nvNt = ntm.getNodeType(nonVersionableNodeType);
if (nvNt.isNodeType(mixVersionable)) {
fail("Property '" + testNodeType + "' does define a versionable nodetype.");
}
// initialise a new session on second workspace as superuser
superuserW2 = getHelper().getSuperuserSession(workspaceName);
workspace = superuser.getWorkspace();
workspaceW2 = superuserW2.getWorkspace();
// get/create test root node on second workspace
testRootNodeW2 = cleanUpTestRoot(superuserW2);
// initialize test nodes
initNodes();
}
Aggregations