use of javax.jcr.NamespaceRegistry in project jackrabbit by apache.
the class SearchResourceImpl method getQuery.
/**
* Create a query from the information present in the <code>sInfo</code>
* object.<br>The following JCR specific logic is applied:
* <ul>
* <li>If the requested resource represents a node with nodetype nt:query, the
* request body is ignored and the query defined with the node is executed
* instead.</li>
* <li>If the requested resource does not represent an existing item, the
* specified query is persisted by calling {@link Query#storeAsNode(String)}.</li>
* </ul>
* @param sInfo defining the query to be executed
* @return <code>Query</code> object.
* @throws javax.jcr.query.InvalidQueryException if the query defined by <code>sInfo</code> is invalid
* @throws RepositoryException the query manager cannot be accessed or if
* another error occurs.
* @throws DavException if <code>sInfo</code> is <code>null</code> and
* the underlying repository item is not an nt:query node or if an error
* occurs when calling {@link Query#storeAsNode(String)}/
*/
private Query getQuery(SearchInfo sInfo) throws InvalidQueryException, RepositoryException, DavException {
Session session = getRepositorySession();
NamespaceRegistry nsReg = session.getWorkspace().getNamespaceRegistry();
Node rootNode = session.getRootNode();
QueryManager qMgr = getRepositorySession().getWorkspace().getQueryManager();
// test if query is defined by requested repository node
String itemPath = locator.getRepositoryPath();
if (itemPath != null && !rootNode.getPath().equals(itemPath)) {
String qNodeRelPath = itemPath.substring(1);
if (rootNode.hasNode(qNodeRelPath)) {
Node qNode = rootNode.getNode(qNodeRelPath);
if (qNode.isNodeType(JcrConstants.NT_QUERY)) {
return qMgr.getQuery(qNode);
}
}
}
Query q;
if (sInfo != null) {
// apply namespace mappings to session
Map<String, String> namespaces = sInfo.getNamespaces();
try {
for (Map.Entry<String, String> entry : namespaces.entrySet()) {
String prefix = entry.getKey();
String uri = entry.getValue();
session.setNamespacePrefix(prefix, uri);
}
q = qMgr.createQuery(sInfo.getQuery(), sInfo.getLanguageName());
if (SearchInfo.NRESULTS_UNDEFINED != sInfo.getNumberResults()) {
q.setLimit(sInfo.getNumberResults());
}
if (SearchInfo.OFFSET_UNDEFINED != sInfo.getOffset()) {
q.setOffset(sInfo.getOffset());
}
} finally {
// reset namespace mappings
for (String uri : namespaces.values()) {
try {
session.setNamespacePrefix(nsReg.getPrefix(uri), uri);
} catch (RepositoryException e) {
log.warn("Unable to reset mapping of namespace: " + uri);
}
}
}
} else {
throw new DavException(DavServletResponse.SC_BAD_REQUEST, locator.getResourcePath() + " is not a nt:query node -> searchRequest body required.");
}
/* test if resource path does not exist -> thus indicating that
the query must be made persistent by calling Query.save(String) */
if (itemPath != null && !getRepositorySession().itemExists(itemPath)) {
try {
q.storeAsNode(itemPath);
} catch (RepositoryException e) {
// ItemExistsException should never occur.
throw new JcrDavException(e);
}
}
return q;
}
use of javax.jcr.NamespaceRegistry in project jackrabbit by apache.
the class LostFromCacheIssueTest method setUp.
public void setUp() throws Exception {
super.setUp();
Workspace workspace = superuser.getWorkspace();
NamespaceRegistry namespaceRegistry = workspace.getNamespaceRegistry();
NodeTypeManager ntmgr = workspace.getNodeTypeManager();
NodeTypeRegistry nodetypeRegistry = ((NodeTypeManagerImpl) ntmgr).getNodeTypeRegistry();
try {
namespaceRegistry.registerNamespace(NAMESPACE_PREFIX, NAMESPACE_URI);
} catch (NamespaceException ignore) {
//already exists
}
QNodeTypeDefinition nodeTypeDefinition = new QNodeTypeDefinitionImpl(((SessionImpl) superuser).getQName(NODETYPE_1), Name.EMPTY_ARRAY, Name.EMPTY_ARRAY, true, false, true, false, null, QPropertyDefinition.EMPTY_ARRAY, QNodeDefinition.EMPTY_ARRAY);
try {
nodetypeRegistry.registerNodeType(nodeTypeDefinition);
} catch (InvalidNodeTypeDefException ignore) {
//already exists
}
nodeTypeDefinition = new QNodeTypeDefinitionImpl(((SessionImpl) superuser).getQName(NODETYPE_2), Name.EMPTY_ARRAY, Name.EMPTY_ARRAY, true, false, true, false, null, QPropertyDefinition.EMPTY_ARRAY, QNodeDefinition.EMPTY_ARRAY);
try {
nodetypeRegistry.registerNodeType(nodeTypeDefinition);
} catch (InvalidNodeTypeDefException ignore) {
//already exists
}
getOrCreate(superuser.getRootNode(), TESTNODE_PATH);
superuser.save();
}
use of javax.jcr.NamespaceRegistry in project jackrabbit by apache.
the class PropertyUtil method checkNameFormat.
/**
* checks if the given name follows the NAME syntax rules and if a present
* prefix is mapped to a registered namespace
*
* @param name the string to test
*/
public static boolean checkNameFormat(String name, Session session) throws RepositoryException {
if (name == null || name.length() == 0) {
return false;
} else {
NamespaceRegistry nsr = session.getWorkspace().getNamespaceRegistry();
boolean prefixOk = true;
// validate name element
Matcher matcher = PATTERN_NAME.matcher(name);
// validate namespace prefixes if present
String[] split = name.split(":");
if (split.length > 1) {
String prefix = split[0];
try {
nsr.getURI(prefix);
} catch (NamespaceException nse) {
prefixOk = false;
}
}
return matcher.matches() && prefixOk;
}
}
use of javax.jcr.NamespaceRegistry in project jackrabbit by apache.
the class RepositoryCopier method copyNamespaces.
private void copyNamespaces() throws RepositoryException {
NamespaceRegistry sourceRegistry = source.getNamespaceRegistry();
NamespaceRegistry targetRegistry = target.getNamespaceRegistry();
logger.info("Copying registered namespaces");
Collection<String> existing = Arrays.asList(targetRegistry.getURIs());
for (String uri : sourceRegistry.getURIs()) {
if (!existing.contains(uri)) {
// TODO: what if the prefix is already taken?
targetRegistry.registerNamespace(sourceRegistry.getPrefix(uri), uri);
}
}
}
use of javax.jcr.NamespaceRegistry in project jackrabbit-oak by apache.
the class PrivilegeUpgradeTest method createSourceContent.
@Override
protected void createSourceContent(Session session) throws Exception {
JackrabbitWorkspace workspace = (JackrabbitWorkspace) session.getWorkspace();
NamespaceRegistry registry = workspace.getNamespaceRegistry();
registry.registerNamespace("test", "http://www.example.org/");
PrivilegeManager privilegeManager = workspace.getPrivilegeManager();
privilegeManager.registerPrivilege("test:privilege", false, null);
privilegeManager.registerPrivilege("test:aggregate", false, new String[] { "jcr:read", "test:privilege" });
privilegeManager.registerPrivilege("test:privilege2", true, null);
privilegeManager.registerPrivilege("test:aggregate2", true, new String[] { "test:aggregate", "test:privilege2" });
}
Aggregations