Search in sources :

Example 6 with LocalNameMapper

use of org.apache.jackrabbit.oak.namepath.impl.LocalNameMapper in project jackrabbit-oak by apache.

the class QueryEngineImpl method parseQuery.

/**
 * Parse the query.
 *
 * @param statement the statement
 * @param language the language
 * @param context the context
 * @param mappings the mappings
 * @return the list of queries, where the first is the original, and all
 *         others are alternatives (for example, a "union" query)
 */
private static List<Query> parseQuery(String statement, String language, ExecutionContext context, Map<String, String> mappings) throws ParseException {
    boolean isInternal = SQL2Parser.isInternal(statement);
    if (isInternal) {
        LOG.trace("Parsing {} statement: {}", language, statement);
    } else {
        LOG.debug("Parsing {} statement: {}", language, statement);
    }
    NamePathMapper mapper = new NamePathMapperImpl(new LocalNameMapper(context.getRoot(), mappings));
    NodeTypeInfoProvider nodeTypes = context.getNodeTypeInfoProvider();
    QueryEngineSettings settings = context.getSettings();
    QueryExecutionStats stats = settings.getQueryStatsReporter().getQueryExecution(statement, language);
    SQL2Parser parser = new SQL2Parser(mapper, nodeTypes, settings, stats);
    if (language.endsWith(NO_LITERALS)) {
        language = language.substring(0, language.length() - NO_LITERALS.length());
        parser.setAllowNumberLiterals(false);
        parser.setAllowTextLiterals(false);
    }
    ArrayList<Query> queries = new ArrayList<Query>();
    Query q;
    if (SQL2.equals(language) || JQOM.equals(language)) {
        q = parser.parse(statement, false);
    } else if (SQL.equals(language)) {
        parser.setSupportSQL1(true);
        q = parser.parse(statement, false);
    } else if (XPATH.equals(language)) {
        XPathToSQL2Converter converter = new XPathToSQL2Converter();
        String sql2 = converter.convert(statement);
        LOG.debug("XPath > SQL2: {}", sql2);
        try {
            // OAK-874: No artificial XPath selector name in wildcards
            parser.setIncludeSelectorNameInWildcardColumns(false);
            q = parser.parse(sql2, false);
        } catch (ParseException e) {
            ParseException e2 = new ParseException(statement + " converted to SQL-2 " + e.getMessage(), 0);
            e2.initCause(e);
            throw e2;
        }
    } else {
        throw new ParseException("Unsupported language: " + language, 0);
    }
    if (q.isInternal()) {
        stats.setInternal(true);
    } else {
        stats.setThreadName(Thread.currentThread().getName());
    }
    queries.add(q);
    if (settings.isSql2Optimisation()) {
        if (q.isInternal()) {
            LOG.trace("Skipping optimisation as internal query.");
        } else {
            LOG.trace("Attempting optimisation");
            Query q2 = q.buildAlternativeQuery();
            if (q2 != q) {
                LOG.debug("Alternative query available: {}", q2);
                queries.add(q2);
            }
        }
    }
    // initialising all the queries.
    for (Query query : queries) {
        try {
            query.init();
        } catch (Exception e) {
            ParseException e2 = new ParseException(query.getStatement() + ": " + e.getMessage(), 0);
            e2.initCause(e);
            throw e2;
        }
    }
    return queries;
}
Also used : NamePathMapper(org.apache.jackrabbit.oak.namepath.NamePathMapper) ArrayList(java.util.ArrayList) NamePathMapperImpl(org.apache.jackrabbit.oak.namepath.impl.NamePathMapperImpl) NodeTypeInfoProvider(org.apache.jackrabbit.oak.query.ast.NodeTypeInfoProvider) LocalNameMapper(org.apache.jackrabbit.oak.namepath.impl.LocalNameMapper) ParseException(java.text.ParseException) QueryExecutionStats(org.apache.jackrabbit.oak.query.stats.QueryStatsData.QueryExecutionStats) XPathToSQL2Converter(org.apache.jackrabbit.oak.query.xpath.XPathToSQL2Converter) ParseException(java.text.ParseException)

Example 7 with LocalNameMapper

use of org.apache.jackrabbit.oak.namepath.impl.LocalNameMapper in project jackrabbit-oak by apache.

the class QueryUtilTest method testEscapeForQuery.

@Test
public void testEscapeForQuery() throws Exception {
    NamePathMapper namePathMapper = new NamePathMapperImpl(new LocalNameMapper(ImmutableMap.of(NamespaceRegistry.PREFIX_JCR, NamespaceRegistry.NAMESPACE_JCR), ImmutableMap.of("myPrefix", NamespaceRegistry.NAMESPACE_JCR)));
    String value = "'string\\value";
    assertEquals(QueryUtils.escapeForQuery("myPrefix:" + value), QueryUtil.escapeForQuery("jcr:" + value, namePathMapper));
}
Also used : NamePathMapper(org.apache.jackrabbit.oak.namepath.NamePathMapper) NamePathMapperImpl(org.apache.jackrabbit.oak.namepath.impl.NamePathMapperImpl) LocalNameMapper(org.apache.jackrabbit.oak.namepath.impl.LocalNameMapper) Test(org.junit.Test)

Example 8 with LocalNameMapper

use of org.apache.jackrabbit.oak.namepath.impl.LocalNameMapper in project jackrabbit-oak by apache.

the class PrivilegeManagerImplTest method testGetPrivilegeRemappedNamespace.

@Test
public void testGetPrivilegeRemappedNamespace() throws Exception {
    NamePathMapper mapper = new NamePathMapperImpl(new LocalNameMapper(root, ImmutableMap.of("prefix", NamespaceRegistry.NAMESPACE_JCR)));
    Privilege p = create(root, mapper).getPrivilege("prefix:read");
    assertNotNull(p);
    assertNotEquals(Privilege.JCR_READ, p.getName());
    assertNotEquals(PrivilegeConstants.JCR_READ, p.getName());
    assertEquals("prefix:read", p.getName());
}
Also used : NamePathMapper(org.apache.jackrabbit.oak.namepath.NamePathMapper) NamePathMapperImpl(org.apache.jackrabbit.oak.namepath.impl.NamePathMapperImpl) Privilege(javax.jcr.security.Privilege) LocalNameMapper(org.apache.jackrabbit.oak.namepath.impl.LocalNameMapper) AbstractSecurityTest(org.apache.jackrabbit.oak.AbstractSecurityTest) Test(org.junit.Test)

Example 9 with LocalNameMapper

use of org.apache.jackrabbit.oak.namepath.impl.LocalNameMapper in project jackrabbit-oak by apache.

the class ACLTest method testGetOakPath.

@Test
public void testGetOakPath() {
    NamePathMapper npMapper = new NamePathMapperImpl(new LocalNameMapper(singletonMap("oak", "http://jackrabbit.apache.org"), singletonMap("jcr", "http://jackrabbit.apache.org")));
    // map of jcr-path to oak path
    Map<String, String> paths = new HashMap();
    paths.put(null, null);
    paths.put(TEST_PATH, TEST_PATH);
    paths.put("/", "/");
    String oakPath = "/oak:testPath";
    String jcrPath = "/jcr:testPath";
    paths.put(jcrPath, oakPath);
    jcrPath = "/{http://jackrabbit.apache.org}testPath";
    paths.put(jcrPath, oakPath);
    // test if oak-path is properly set.
    for (String path : paths.keySet()) {
        AbstractAccessControlList acl = createACL(path, Collections.<ACE>emptyList(), npMapper);
        assertEquals(paths.get(path), acl.getOakPath());
    }
}
Also used : AbstractAccessControlList(org.apache.jackrabbit.oak.spi.security.authorization.accesscontrol.AbstractAccessControlList) HashMap(java.util.HashMap) NamePathMapper(org.apache.jackrabbit.oak.namepath.NamePathMapper) NamePathMapperImpl(org.apache.jackrabbit.oak.namepath.impl.NamePathMapperImpl) LocalNameMapper(org.apache.jackrabbit.oak.namepath.impl.LocalNameMapper) Test(org.junit.Test)

Aggregations

LocalNameMapper (org.apache.jackrabbit.oak.namepath.impl.LocalNameMapper)9 NamePathMapperImpl (org.apache.jackrabbit.oak.namepath.impl.NamePathMapperImpl)9 NamePathMapper (org.apache.jackrabbit.oak.namepath.NamePathMapper)8 Test (org.junit.Test)8 AbstractSecurityTest (org.apache.jackrabbit.oak.AbstractSecurityTest)6 Privilege (javax.jcr.security.Privilege)3 ArrayList (java.util.ArrayList)2 PrivilegeManager (org.apache.jackrabbit.api.security.authorization.PrivilegeManager)2 ParseException (java.text.ParseException)1 HashMap (java.util.HashMap)1 AccessControlManager (javax.jcr.security.AccessControlManager)1 JackrabbitAccessControlManager (org.apache.jackrabbit.api.security.JackrabbitAccessControlManager)1 Tree (org.apache.jackrabbit.oak.api.Tree)1 NameMapper (org.apache.jackrabbit.oak.namepath.NameMapper)1 GlobalNameMapper (org.apache.jackrabbit.oak.namepath.impl.GlobalNameMapper)1 ReadWriteNamespaceRegistry (org.apache.jackrabbit.oak.plugins.name.ReadWriteNamespaceRegistry)1 NodeTypeInfoProvider (org.apache.jackrabbit.oak.query.ast.NodeTypeInfoProvider)1 QueryExecutionStats (org.apache.jackrabbit.oak.query.stats.QueryStatsData.QueryExecutionStats)1 XPathToSQL2Converter (org.apache.jackrabbit.oak.query.xpath.XPathToSQL2Converter)1 AbstractAccessControlList (org.apache.jackrabbit.oak.spi.security.authorization.accesscontrol.AbstractAccessControlList)1