use of javax.jcr.NamespaceRegistry in project jackrabbit-oak by apache.
the class RepositoryTest method registryRemappedNamespace.
@Test
public void registryRemappedNamespace() throws RepositoryException {
NamespaceRegistry nsReg = getAdminSession().getWorkspace().getNamespaceRegistry();
nsReg.registerNamespace("foo", "file:///foo");
getAdminSession().getRootNode().addNode("foo:test");
getAdminSession().save();
try {
nsReg.registerNamespace("bar", "file:///foo");
fail("Remapping namespace through NamespaceRegistry must not be allowed");
} catch (NamespaceException e) {
// expected
} finally {
getAdminSession().getRootNode().getNode("foo:test").remove();
getAdminSession().save();
nsReg.unregisterNamespace("foo");
}
}
use of javax.jcr.NamespaceRegistry in project sling by apache.
the class RegisterNodetypesTest method NSregistered.
@Test
public void NSregistered() throws Exception {
final NamespaceRegistry ns = U.getAdminSession().getWorkspace().getNamespaceRegistry();
assertEquals(NS_URI, ns.getURI(NS_PREFIX));
}
use of javax.jcr.NamespaceRegistry in project sling by apache.
the class NodeTypeDefinitionScanner method registerNodeTypes.
/**
* Registers node types found in classpath in JCR repository.
* @param session Session
* @param nodeTypeResources List of classpath resource URLs pointing to node type definitions
*/
private void registerNodeTypes(Session session, List<String> nodeTypeResources) throws RepositoryException {
ClassLoader classLoader = getClass().getClassLoader();
Workspace workspace = session.getWorkspace();
NodeTypeManager nodeTypeManager = workspace.getNodeTypeManager();
NamespaceRegistry namespaceRegistry = workspace.getNamespaceRegistry();
ValueFactory valueFactory = session.getValueFactory();
// try registering node types multiple times because the exact order is not known
int iteration = 0;
List<String> remainingNodeTypeResources = new ArrayList<String>(nodeTypeResources);
while (!remainingNodeTypeResources.isEmpty()) {
registerNodeTypesAndRemoveSucceeds(remainingNodeTypeResources, classLoader, nodeTypeManager, namespaceRegistry, valueFactory, false);
iteration++;
if (iteration >= MAX_ITERATIONS) {
break;
}
}
if (!remainingNodeTypeResources.isEmpty()) {
registerNodeTypesAndRemoveSucceeds(remainingNodeTypeResources, classLoader, nodeTypeManager, namespaceRegistry, valueFactory, true);
}
}
use of javax.jcr.NamespaceRegistry 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.NamespaceRegistry in project jackrabbit by apache.
the class WorkspaceResourceImpl method initProperties.
@Override
protected void initProperties() {
super.initProperties();
try {
// init workspace specific properties
NamespaceRegistry nsReg = getRepositorySession().getWorkspace().getNamespaceRegistry();
DavProperty<?> namespacesProp = new NamespacesProperty(nsReg);
properties.add(namespacesProp);
} catch (RepositoryException e) {
log.error("Failed to access NamespaceRegistry: " + e.getMessage());
}
}
Aggregations