Java Code Examples for org.apache.cxf.jaxrs.ext.search.SearchCondition#accept()

The following examples show how to use org.apache.cxf.jaxrs.ext.search.SearchCondition#accept() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FilterConverter.java    From syncope with Apache License 2.0 6 votes vote down vote up
/**
 * Parses a FIQL expression into ConnId's {@link Filter}, using {@link SyncopeFiqlParser}.
 *
 * @param fiql FIQL string
 * @return {@link Filter} instance for given FIQL expression
 */
public static Filter convert(final String fiql) {
    SyncopeFiqlParser<SearchBean> parser = new SyncopeFiqlParser<>(
            SearchBean.class, AbstractFiqlSearchConditionBuilder.CONTEXTUAL_PROPERTIES);

    try {
        FilterVisitor visitor = new FilterVisitor();
        SearchCondition<SearchBean> sc = parser.parse(URLDecoder.decode(fiql, StandardCharsets.UTF_8));
        sc.accept(visitor);

        return visitor.getQuery();
    } catch (Exception e) {
        SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.InvalidSearchExpression);
        sce.getElements().add(fiql);
        sce.getElements().add(ExceptionUtils.getRootCauseMessage(e));
        throw sce;
    }
}
 
Example 2
Source File: SearchCondConverter.java    From syncope with Apache License 2.0 6 votes vote down vote up
/**
 * Parses a FIQL expression into Syncope's {@link SearchCond}, using {@link SyncopeFiqlParser}.
 *
 * @param visitor visitor instance
 * @param fiql FIQL string
 * @param realms optional realm to provide to {@link SearchCondVisitor}
 * @return {@link SearchCond} instance for given FIQL expression
 */
public static SearchCond convert(final SearchCondVisitor visitor, final String fiql, final String... realms) {
    SyncopeFiqlParser<SearchBean> parser = new SyncopeFiqlParser<>(
            SearchBean.class, AbstractFiqlSearchConditionBuilder.CONTEXTUAL_PROPERTIES);

    try {
        if (realms != null && realms.length > 0) {
            visitor.setRealm(realms[0]);
        }
        SearchCondition<SearchBean> sc = parser.parse(URLDecoder.decode(fiql, StandardCharsets.UTF_8));
        sc.accept(visitor);

        return visitor.getQuery();
    } catch (Exception e) {
        SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.InvalidSearchExpression);
        sce.getElements().add(fiql);
        sce.getElements().add(ExceptionUtils.getRootCauseMessage(e));
        throw sce;
    }
}
 
Example 3
Source File: AbstractServiceImpl.java    From syncope with Apache License 2.0 6 votes vote down vote up
protected SearchCond getSearchCond(final String fiql, final String realm) {
    try {
        searchCondVisitor.setRealm(realm);
        SearchCondition<SearchBean> sc = searchContext.getCondition(fiql, SearchBean.class);
        sc.accept(searchCondVisitor);

        return searchCondVisitor.getQuery();
    } catch (Exception e) {
        LOG.error("Invalid FIQL expression: {}", fiql, e);

        SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.InvalidSearchExpression);
        sce.getElements().add(fiql);
        sce.getElements().add(ExceptionUtils.getRootCauseMessage(e));
        throw sce;
    }
}
 
Example 4
Source File: SQLPrinterVisitorTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testSQL2() throws SearchParseException {
    SearchCondition<Condition> filter = parser.parse("name==ami*,level=gt=10");
    SQLPrinterVisitor<Condition> visitor = new SQLPrinterVisitor<>("table");
    filter.accept(visitor);
    String sql = visitor.getQuery();
    assertTrue("SELECT * FROM table WHERE (name LIKE 'ami%') OR (level > '10')".equals(sql)
               || "SELECT * FROM table WHERE (level > '10') OR (name LIKE 'ami%')".equals(sql));
}
 
Example 5
Source File: LdapQueryVisitorTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testAndOrQuery() throws SearchParseException {
    SearchCondition<Condition> filter =
        parser.parse("name==foo;(name!=bar,level=le=10)");
    LdapQueryVisitor<Condition> visitor = new LdapQueryVisitor<>();
    filter.accept(visitor.visitor());
    String ldap = visitor.getQuery();

    assertEquals("(&(name=foo)(|(!name=bar)(level<=10)))", ldap);
}
 
Example 6
Source File: SQLPrinterVisitorTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testSQL1WithSearchBean() throws SearchParseException {
    FiqlParser<SearchBean> beanParser = new FiqlParser<>(SearchBean.class);
    SearchCondition<SearchBean> filter = beanParser.parse("name==ami*;level=gt=10");
    SQLPrinterVisitor<SearchBean> visitor = new SQLPrinterVisitor<>("table");
    filter.accept(visitor);
    String sql = visitor.getQuery();

    assertTrue("SELECT * FROM table WHERE (name LIKE 'ami%') AND (level > '10')".equals(sql)
               || "SELECT * FROM table WHERE (level > '10') AND (name LIKE 'ami%')".equals(sql));
}
 
Example 7
Source File: SQLPrinterVisitorTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testSQL1() throws SearchParseException {
    SearchCondition<Condition> filter = parser.parse("name==ami%*;level=gt=10");
    SQLPrinterVisitor<Condition> visitor = new SQLPrinterVisitor<>("table");
    filter.accept(visitor.visitor());
    String sql = visitor.getQuery();

    assertTrue("SELECT * FROM table WHERE (name LIKE 'ami\\%%') AND (level > '10')".equals(sql)
               || "SELECT * FROM table WHERE (level > '10') AND (name LIKE 'ami\\%%')".equals(sql));
}
 
Example 8
Source File: SQLHierarchicalQueryTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testSimpleHierarchicalQuery() throws SearchParseException {
    FiqlParser<SearchBean> parser = new FiqlParser<>(SearchBean.class);
    SearchCondition<SearchBean> filter = parser.parse("cartridges.colour==blue");
    SQLPrinterVisitor<SearchBean> visitor = new SQLPrinterVisitor<>("printers");
    filter.accept(visitor.visitor());
    String sql = visitor.getQuery();

    assertEquals("SELECT * FROM printers left join cartridges"
                 + " on printers.id = cartridges.printer_id"
                 + " WHERE cartridges.colour = 'blue'",
                 sql);
}
 
Example 9
Source File: LdapQueryVisitorTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testSimple() throws SearchParseException {
    SearchCondition<Condition> filter = parser.parse("name!=ami");
    LdapQueryVisitor<Condition> visitor = new LdapQueryVisitor<>();
    filter.accept(visitor.visitor());
    String ldap = visitor.getQuery();

    assertEquals("(!name=ami)", ldap);
}
 
Example 10
Source File: AbstractJPATypedQueryVisitorTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected List<BookInfo> criteriaQueryBooksConstruct(String expression) throws Exception {
    SearchCondition<Book> filter = getParser().parse(expression);
    JPACriteriaQueryVisitor<Book, BookInfo> jpa =
        new JPACriteriaQueryVisitor<Book, BookInfo>(em, Book.class, BookInfo.class);
    filter.accept(jpa);

    List<SingularAttribute<Book, ?>> selections = new ArrayList<>();
    selections.add(Book_.id);
    selections.add(Book_.bookTitle);

    jpa.selectConstruct(selections);

    CriteriaQuery<BookInfo> cquery = jpa.getQuery();
    return em.createQuery(cquery).getResultList();
}
 
Example 11
Source File: SQLPrinterVisitorTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testSQL3() throws SearchParseException {
    SearchCondition<Condition> filter = parser.parse("name==foo*;(name!=*bar,level=gt=10)");
    SQLPrinterVisitor<Condition> visitor = new SQLPrinterVisitor<>("table");
    filter.accept(visitor);
    String sql = visitor.getQuery();
    assertTrue(("SELECT * FROM table WHERE (name LIKE 'foo%') AND ((name NOT LIKE '%bar') "
               + "OR (level > '10'))").equals(sql)
               || ("SELECT * FROM table WHERE (name LIKE 'foo%') AND "
               + "((level > '10') OR (name NOT LIKE '%bar'))").equals(sql));
}
 
Example 12
Source File: AbstractJPATypedQueryVisitorTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected long criteriaQueryBooksCount(String expression) throws Exception {
    SearchCondition<Book> filter = getParser().parse(expression);
    JPACriteriaQueryVisitor<Book, Long> jpa =
        new JPACriteriaQueryVisitor<Book, Long>(em, Book.class, Long.class);
    filter.accept(jpa);
    return jpa.count();
}
 
Example 13
Source File: LdapQueryVisitorTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testComplexQuery() throws SearchParseException {
    SearchCondition<Condition> filter =
        parser.parse("(name==test,level==18);(name==test1,level!=19)");
    LdapQueryVisitor<Condition> visitor = new LdapQueryVisitor<>();
    filter.accept(visitor.visitor());
    String ldap = visitor.getQuery();
    assertEquals("(&(|(name=test)(level=18))(|(name=test1)(!level=19)))", ldap);
}
 
Example 14
Source File: SQLPrinterVisitorTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testSQL3WithSearchBean() throws SearchParseException {
    FiqlParser<SearchBean> beanParser = new FiqlParser<>(SearchBean.class);
    SearchCondition<SearchBean> filter = beanParser.parse("name==foo*;(name!=*bar,level=gt=10)");
    SQLPrinterVisitor<SearchBean> visitor = new SQLPrinterVisitor<>("table");
    filter.accept(visitor);
    String sql = visitor.getQuery();
    assertTrue(("SELECT * FROM table WHERE (name LIKE 'foo%') AND ((name NOT LIKE '%bar') "
               + "OR (level > '10'))").equals(sql)
               || ("SELECT * FROM table WHERE (name LIKE 'foo%') AND "
               + "((level > '10') OR (name NOT LIKE '%bar'))").equals(sql));
}
 
Example 15
Source File: SQLHierarchicalQueryTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test(expected = SearchParseException.class)
public void testTooManyJoins() {
    FiqlParser<SearchBean> parser = new FiqlParser<>(SearchBean.class);
    SearchCondition<SearchBean> filter = parser.parse("cartridges.colour==blue;cartridges.location==Japan");
    SQLPrinterVisitor<SearchBean> visitor = new SQLPrinterVisitor<>("printers");
    filter.accept(visitor.visitor());
}
 
Example 16
Source File: LdapQueryVisitorTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testAndQuery() throws SearchParseException {
    SearchCondition<Condition> filter = parser.parse("name==ami*;level=gt=10");
    LdapQueryVisitor<Condition> visitor = new LdapQueryVisitor<>();
    visitor.setEncodeQueryValues(false);
    filter.accept(visitor.visitor());
    String ldap = visitor.getQuery();

    assertEquals("(&(name=ami*)(level>=10))", ldap);
}
 
Example 17
Source File: SQLPrinterVisitorTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testSQL5WithFieldMap() throws SearchParseException {
    SearchCondition<Condition> filter = parser.parse("name==test");
    SQLPrinterVisitor<Condition> visitor =
        new SQLPrinterVisitor<Condition>(
            Collections.singletonMap("name", "NAMES"),
            "table", Collections.singletonList("NAMES"));
    filter.accept(visitor);
    String sql = visitor.getQuery();
    assertEquals("SELECT NAMES FROM table WHERE NAMES = 'test'", sql);
}
 
Example 18
Source File: LdapQueryVisitor.java    From cxf with Apache License 2.0 4 votes vote down vote up
public void visit(SearchCondition<T> sc) {

        StringBuilder sb = getStringBuilder();
        if (sb == null) {
            sb = new StringBuilder();
        }

        PrimitiveStatement statement = sc.getStatement();
        if (statement != null) {
            if (statement.getProperty() != null) {
                String name = getRealPropertyName(statement.getProperty());
                String rvalStr = getPropertyValue(name, statement.getValue());
                validatePropertyValue(name, rvalStr);

                sb.append('(');
                if (sc.getConditionType() == ConditionType.NOT_EQUALS) {
                    sb.append('!');
                }

                String ldapOperator = conditionTypeToLdapOperator(sc.getConditionType());
                String encodedRValStr = encodeQueryValues ? Util.doRFC2254Encoding(rvalStr) : rvalStr;
                sb.append(name).append(ldapOperator).append(encodedRValStr);

                sb.append(')');
            }
        } else {
            sb.append('(');
            if (sc.getConditionType() == ConditionType.AND) {
                sb.append('&');
            } else {
                sb.append('|');
            }

            for (SearchCondition<T> condition : sc.getSearchConditions()) {
                saveStringBuilder(sb);
                condition.accept(this);
                sb = getStringBuilder();
            }
            sb.append(')');
        }
        saveStringBuilder(sb);
    }
 
Example 19
Source File: UserServiceImpl.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Override
public User searchUser(@PathParam("query") String query, @Context SearchContext searchContext)
    throws UserNotFoundFault {

    SearchCondition<User> sc = searchContext.getCondition(query, User.class);
    if (sc == null) {
        throw new UserNotFoundFault("Search exception");
    }

    LdapQueryVisitor<User> visitor =
        new LdapQueryVisitor<>(Collections.singletonMap("name", "cn"));
    visitor.setEncodeQueryValues(encodeQueryValues);
    sc.accept(visitor.visitor());
    String parsedQuery = visitor.getQuery();

    ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext("ldap-jaxrsport.xml");
    LdapTemplate template = (LdapTemplate)appContext.getBean("ldapTemplate");

    String userBaseDn = "OU=users,DC=example,DC=com";
    String[] attributes = new String[] {"sn", "cn"};
    Map<String, Attribute> attrs =
        getAttributesOfEntry(template, userBaseDn, "person", parsedQuery, attributes);

    appContext.close();

    if (attrs == null || attrs.isEmpty()) {
        throw new UserNotFoundFault("Search exception");
    }
    User user = new User();
    try {
        for (Entry<String, Attribute> result : attrs.entrySet()) {
            if ("sn".equals(result.getKey())) {
                user.setSurname((String)result.getValue().get());
            } else if ("cn".equals(result.getKey())) {
                user.setName((String)result.getValue().get());
            }
        }
    } catch (NamingException e) {
        throw new UserNotFoundFault("Search exception");
    }

    return user;
}
 
Example 20
Source File: ResourceServiceImpl.java    From syncope with Apache License 2.0 4 votes vote down vote up
@Override
public PagedConnObjectTOResult searchConnObjects(
        final String key, final String anyTypeKey, final ConnObjectTOQuery query) {

    Filter filter = null;
    Set<String> moreAttrsToGet = Set.of();
    if (StringUtils.isNotBlank(query.getFiql())) {
        try {
            FilterVisitor visitor = new FilterVisitor();
            SearchCondition<SearchBean> sc = searchContext.getCondition(query.getFiql(), SearchBean.class);
            sc.accept(visitor);

            filter = visitor.getQuery();
            moreAttrsToGet = visitor.getAttrs();
        } catch (Exception e) {
            LOG.error("Invalid FIQL expression: {}", query.getFiql(), e);

            SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.InvalidSearchExpression);
            sce.getElements().add(query.getFiql());
            sce.getElements().add(ExceptionUtils.getRootCauseMessage(e));
            throw sce;
        }
    }

    Pair<SearchResult, List<ConnObjectTO>> list = logic.searchConnObjects(
            filter,
            moreAttrsToGet,
            key,
            anyTypeKey,
            query.getSize(),
            query.getPagedResultsCookie(),
            getOrderByClauses(query.getOrderBy()));

    PagedConnObjectTOResult result = new PagedConnObjectTOResult();
    if (list.getLeft() != null) {
        result.setAllResultsReturned(list.getLeft().isAllResultsReturned());
        result.setPagedResultsCookie(list.getLeft().getPagedResultsCookie());
        result.setRemainingPagedResults(list.getLeft().getRemainingPagedResults());
    }
    result.getResult().addAll(list.getRight());

    UriBuilder builder = uriInfo.getAbsolutePathBuilder();
    MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters();
    for (Map.Entry<String, List<String>> queryParam : queryParams.entrySet()) {
        builder = builder.queryParam(queryParam.getKey(), queryParam.getValue().toArray());
    }

    if (StringUtils.isNotBlank(result.getPagedResultsCookie())) {
        result.setNext(builder.
                replaceQueryParam(PARAM_CONNID_PAGED_RESULTS_COOKIE, result.getPagedResultsCookie()).
                replaceQueryParam(PARAM_SIZE, query.getSize()).
                build());
    }

    return result;
}