Search in sources :

Example 16 with NamespaceRegistry

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;
}
Also used : JcrDavException(org.apache.jackrabbit.webdav.jcr.JcrDavException) NamespaceRegistry(javax.jcr.NamespaceRegistry) Query(javax.jcr.query.Query) DavException(org.apache.jackrabbit.webdav.DavException) JcrDavException(org.apache.jackrabbit.webdav.jcr.JcrDavException) Node(javax.jcr.Node) QueryManager(javax.jcr.query.QueryManager) RepositoryException(javax.jcr.RepositoryException) Map(java.util.Map) JcrDavSession(org.apache.jackrabbit.webdav.jcr.JcrDavSession) Session(javax.jcr.Session)

Example 17 with NamespaceRegistry

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();
}
Also used : QNodeTypeDefinition(org.apache.jackrabbit.spi.QNodeTypeDefinition) InvalidNodeTypeDefException(org.apache.jackrabbit.core.nodetype.InvalidNodeTypeDefException) NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NamespaceRegistry(javax.jcr.NamespaceRegistry) NamespaceException(javax.jcr.NamespaceException) NodeTypeRegistry(org.apache.jackrabbit.core.nodetype.NodeTypeRegistry) NodeTypeManagerImpl(org.apache.jackrabbit.core.nodetype.NodeTypeManagerImpl) Workspace(javax.jcr.Workspace) QNodeTypeDefinitionImpl(org.apache.jackrabbit.spi.commons.QNodeTypeDefinitionImpl)

Example 18 with NamespaceRegistry

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;
    }
}
Also used : NamespaceRegistry(javax.jcr.NamespaceRegistry) Matcher(java.util.regex.Matcher) NamespaceException(javax.jcr.NamespaceException)

Example 19 with NamespaceRegistry

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);
        }
    }
}
Also used : NamespaceRegistry(javax.jcr.NamespaceRegistry)

Example 20 with NamespaceRegistry

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" });
}
Also used : NamespaceRegistry(javax.jcr.NamespaceRegistry) PrivilegeManager(org.apache.jackrabbit.api.security.authorization.PrivilegeManager) JackrabbitWorkspace(org.apache.jackrabbit.api.JackrabbitWorkspace)

Aggregations

NamespaceRegistry (javax.jcr.NamespaceRegistry)34 Session (javax.jcr.Session)14 NamespaceException (javax.jcr.NamespaceException)6 Node (javax.jcr.Node)6 RepositoryException (javax.jcr.RepositoryException)6 NodeTypeManager (javax.jcr.nodetype.NodeTypeManager)6 NodeTypeTemplate (javax.jcr.nodetype.NodeTypeTemplate)6 Test (org.junit.Test)6 InputStream (java.io.InputStream)4 JackrabbitWorkspace (org.apache.jackrabbit.api.JackrabbitWorkspace)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 BigDecimal (java.math.BigDecimal)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Binary (javax.jcr.Binary)3 Workspace (javax.jcr.Workspace)3 Value (javax.jcr.Value)2 ValueFactory (javax.jcr.ValueFactory)2 PropertyDefinitionTemplate (javax.jcr.nodetype.PropertyDefinitionTemplate)2 JackrabbitSession (org.apache.jackrabbit.api.JackrabbitSession)2