org.apache.cxf.jaxrs.ext.search.SearchCondition Java Examples

The following examples show how to use org.apache.cxf.jaxrs.ext.search.SearchCondition. 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: SearchCondVisitor.java    From syncope with Apache License 2.0 6 votes vote down vote up
protected static ConditionType getConditionType(final SearchCondition<SearchBean> sc) {
    ConditionType ct = sc.getConditionType();
    if (sc instanceof SyncopeFiqlSearchCondition && sc.getConditionType() == ConditionType.CUSTOM) {
        SyncopeFiqlSearchCondition<SearchBean> sfsc = (SyncopeFiqlSearchCondition<SearchBean>) sc;
        switch (sfsc.getOperator()) {
            case SyncopeFiqlParser.IEQ:
                ct = ConditionType.EQUALS;
                break;

            case SyncopeFiqlParser.NIEQ:
                ct = ConditionType.NOT_EQUALS;
                break;

            default:
                throw new IllegalArgumentException(
                        String.format("Condition type %s is not supported", sfsc.getOperator()));
        }
    }

    return ct;
}
 
Example #2
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 #3
Source File: ConfigurationEndpointUtils.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
public static Condition buildSearchCondition(SearchCondition searchCondition) {

        if (!(searchCondition.getStatement() == null)) {
            PrimitiveStatement primitiveStatement = searchCondition.getStatement();
            if (!(primitiveStatement.getProperty() == null)) {
                return new PrimitiveCondition(
                        primitiveStatement.getProperty(),
                        getPrimitiveOperatorFromOdata(primitiveStatement.getCondition()),
                        primitiveStatement.getValue()
                );
            }
            return null;
        } else {
            List<Condition> conditions = new ArrayList<>();
            for (Object condition : searchCondition.getSearchConditions()) {
                Condition buildCondition = buildSearchCondition((SearchCondition) condition);
                if (buildCondition != null) {
                    conditions.add(buildCondition);
                }
            }
            return new ComplexCondition(
                    getComplexOperatorFromOdata(searchCondition.getConditionType()),
                    conditions
            );
        }
    }
 
Example #4
Source File: SearchApiServiceImpl.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
private Condition getSearchCondition(SearchContext searchContext)
        throws SearchConditionException {

    if (searchContext != null) {
        SearchCondition<ResourceSearchBean> searchCondition = searchContext.getCondition(ResourceSearchBean.class);
        if (searchCondition != null) {
            return buildSearchCondition(searchCondition);
        } else {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Search condition parsed from the search expression is invalid.");
            }
        }
    } else {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Cannot find a valid search context.");
        }
    }
    throw new SearchConditionException("Invalid search expression found.");
}
 
Example #5
Source File: AbstractJPATypedQueryVisitor.java    From cxf with Apache License 2.0 6 votes vote down vote up
public void visit(SearchCondition<T> sc) {
    if (builder == null) {
        builder = em.getCriteriaBuilder();
        cq = builder.createQuery(queryClass);
        root = cq.from(tClass);
        predStack.push(new ArrayList<>());
    }
    if (sc.getStatement() != null) {
        predStack.peek().add(buildPredicate(sc.getStatement()));
    } else {
        predStack.push(new ArrayList<>());
        for (SearchCondition<T> condition : sc.getSearchConditions()) {
            condition.accept(this);
        }
        List<Predicate> predsList = predStack.pop();
        Predicate[] preds = predsList.toArray(new Predicate[0]);
        Predicate newPred;
        if (sc instanceof OrSearchCondition) {
            newPred = builder.or(preds);
        } else {
            newPred = builder.and(preds);
        }
        predStack.peek().add(newPred);
    }
}
 
Example #6
Source File: LuceneQueryVisitor.java    From cxf with Apache License 2.0 6 votes vote down vote up
public void visit(SearchCondition<T> sc) {
    if (state.get() == null) {
        reset();
    }
    PrimitiveStatement statement = sc.getStatement();
    if (statement != null) {
        if (statement.getProperty() != null) {
            state.get().peek().add(buildSimpleQuery(sc.getConditionType(),
                                     statement.getProperty(),
                                     statement.getValue()));
        }
    } else {
        state.get().push(new ArrayList<>());
        for (SearchCondition<T> condition : sc.getSearchConditions()) {
            condition.accept(this);
        }
        boolean orCondition = sc.getConditionType() == ConditionType.OR;
        List<Query> queries = state.get().pop();
        state.get().peek().add(createCompositeQuery(queries, orCondition));
    }
}
 
Example #7
Source File: BookStore.java    From cxf with Apache License 2.0 6 votes vote down vote up
@GET
@Path("/books/search")
@Produces("application/xml")
public Book getBook(@Context SearchContext searchContext)
    throws BookNotFoundFault {

    SearchCondition<Book> sc = searchContext.getCondition(Book.class);
    if (sc == null) {
        throw new BookNotFoundFault("Search exception");
    }
    List<Book> found = sc.findAll(books.values());
    if (found.size() != 1) {
        throw new BookNotFoundFault("Single book is expected");
    }
    return found.get(0);
}
 
Example #8
Source File: SyncopeFiqlParser.java    From syncope with Apache License 2.0 6 votes vote down vote up
@Override
public SearchCondition<T> build() throws SearchParseException {
    String templateName = getSetter(name);
    T cond = createTemplate(templateName);
    ConditionType ct = operatorsMap.get(operator);

    if (isPrimitive(cond)) {
        return new SyncopeFiqlSearchCondition<>(ct, cond);
    } else {
        String templateNameLCase = templateName.toLowerCase();
        return new SyncopeFiqlSearchCondition<>(Map.of(templateNameLCase, ct),
                Map.of(templateNameLCase, name),
                Map.of(templateNameLCase, tvalue.getTypeInfo()),
                cond, operator);
    }
}
 
Example #9
Source File: FiqlParserTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testParseComplex1() throws SearchParseException {
    SearchCondition<Condition> filter = parser.parse("name==ami*;level=gt=10");
    assertEquals(ConditionType.AND, filter.getConditionType());

    List<SearchCondition<Condition>> conditions = filter.getSearchConditions();
    assertEquals(2, conditions.size());
    PrimitiveStatement st1 = conditions.get(0).getStatement();
    PrimitiveStatement st2 = conditions.get(1).getStatement();
    assertTrue((ConditionType.EQUALS.equals(st1.getCondition())
        && ConditionType.GREATER_THAN.equals(st2.getCondition()))
        || (ConditionType.EQUALS.equals(st2.getCondition())
            && ConditionType.GREATER_THAN.equals(st1.getCondition())));

    assertTrue(filter.isMet(new Condition("amichalec", 12, new Date())));
    assertTrue(filter.isMet(new Condition("ami", 12, new Date())));
    assertFalse(filter.isMet(new Condition("ami", 8, null)));
    assertFalse(filter.isMet(new Condition("am", 20, null)));
}
 
Example #10
Source File: FiqlParserTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testParseComplex2() throws SearchParseException {
    SearchCondition<Condition> filter = parser.parse("name==ami*,level=gt=10");
    assertEquals(ConditionType.OR, filter.getConditionType());

    List<SearchCondition<Condition>> conditions = filter.getSearchConditions();
    assertEquals(2, conditions.size());

    PrimitiveStatement st1 = conditions.get(0).getStatement();
    PrimitiveStatement st2 = conditions.get(1).getStatement();
    assertTrue((ConditionType.EQUALS.equals(st1.getCondition())
        && ConditionType.GREATER_THAN.equals(st2.getCondition()))
        || (ConditionType.EQUALS.equals(st2.getCondition())
            && ConditionType.GREATER_THAN.equals(st1.getCondition())));

    assertTrue(filter.isMet(new Condition("ami", 0, new Date())));
    assertTrue(filter.isMet(new Condition("foo", 20, null)));
    assertFalse(filter.isMet(new Condition("foo", 0, null)));
}
 
Example #11
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 #12
Source File: AbstractLuceneQueryVisitorTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected Query createTermQuery(String fieldName, String expression, boolean useAnalyzer) throws Exception {
    SearchCondition<SearchBean> filter = getParser().parse(expression);
    LuceneQueryVisitor<SearchBean> lucene =
        new LuceneQueryVisitor<>("ct", fieldName, useAnalyzer ? analyzer : null);
    lucene.visit(filter);
    return lucene.getQuery();
}
 
Example #13
Source File: FiqlParserTest.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");
    String sql = SearchUtils.toSQL(filter, "table");
    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 #14
Source File: FiqlParserTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testWildcard() throws SearchParseException {
    SearchCondition<Condition> filter = parser.parse("name==*");
    try {
        filter.isMet(new Condition("foobaz", 0, null));
        fail("Failure expected on an invalid search condition");
    } catch (SearchParseException ex) {
        // expected
    }
}
 
Example #15
Source File: SQLPrinterVisitorTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testSQL5() throws SearchParseException {
    SearchCondition<Condition> filter = parser.parse("name==test");
    SQLPrinterVisitor<Condition> visitor = new SQLPrinterVisitor<>("table");
    filter.accept(visitor);
    String sql = visitor.getQuery();
    assertEquals("SELECT * FROM table WHERE name = 'test'", sql);
}
 
Example #16
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 #17
Source File: SQLPrinterVisitorTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testSQL5WithColumns() throws SearchParseException {
    SearchCondition<Condition> filter = parser.parse("name==test");
    SQLPrinterVisitor<Condition> visitor =
        new SQLPrinterVisitor<>("table", "NAMES");
    filter.accept(visitor);
    String sql = visitor.getQuery();
    assertEquals("SELECT NAMES FROM table WHERE name = 'test'", sql);
}
 
Example #18
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 #19
Source File: SQLPrinterVisitorTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testSQL4WithTLStateAndSingleThread() throws SearchParseException {
    SearchCondition<Condition> filter = parser.parse("(name==test,level==18);(name==test1,level!=19)");
    SQLPrinterVisitor<Condition> visitor = new SQLPrinterVisitor<>("table");
    visitor.setVisitorState(new SBThreadLocalVisitorState());

    filter.accept(visitor);
    String sql = visitor.getQuery();
    assertTrue(("SELECT * FROM table WHERE ((name = 'test') OR (level = '18'))"
               + " AND ((name = 'test1') OR (level <> '19'))").equals(sql)
               || ("SELECT * FROM table WHERE ((name = 'test1') OR (level <> '19'))"
               + " AND ((name = 'test') OR (level = '18'))").equals(sql));
}
 
Example #20
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 #21
Source File: FiqlParserTest.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)");
    String sql = SearchUtils.toSQL(filter, "table");
    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 #22
Source File: LuceneQueryVisitorFiqlTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testThatMultipleQueriesForTheSameFieldAreThreadSafe() throws InterruptedException, ExecutionException {
    final LuceneQueryVisitor<SearchBean> visitor = new LuceneQueryVisitor<>();
    final ExecutorService executorService = Executors.newFixedThreadPool(5);

    final Collection< Future< ? > > futures = new ArrayList<>();
    for (int i = 0; i < 5; ++i) {
        final int index = i;

        futures.add(
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    final SearchCondition<SearchBean> filter = getParser().parse("name==text" + index);
                    visitor.reset();
                    visitor.visit(filter);

                    assertNotNull("Query should not be null", visitor.getQuery());
                    assertThat(visitor.getQuery().toString(), equalTo("name:text" + index));
                }
            })
        );
    }

    executorService.shutdown();
    assertTrue("All threads should be terminated",
        executorService.awaitTermination(5, TimeUnit.SECONDS));

    for (final Future< ? > future: futures) {
        // The exception will be raised if queries are messed up
        future.get();
    }
}
 
Example #23
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 #24
Source File: FiqlParserTest.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");
    String sql = SearchUtils.toSQL(filter, "table");
    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 #25
Source File: FiqlParserTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testParseDateWithCustomFormat() throws SearchParseException, ParseException {
    Map<String, String> props = new HashMap<>();
    props.put(SearchUtils.DATE_FORMAT_PROPERTY, "yyyy-MM-dd'T'HH:mm:ss");
    props.put(SearchUtils.TIMEZONE_SUPPORT_PROPERTY, "false");
    parser = new FiqlParser<>(Condition.class, props);

    SearchCondition<Condition> filter = parser.parse("time=le=2010-03-11T18:00:00");
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    assertTrue(filter.isMet(new Condition("whatever", 15, df.parse("2010-03-11T18:00:00"))));
    assertTrue(filter.isMet(new Condition(null, null, df.parse("2010-03-10T22:22:00"))));
    assertFalse(filter.isMet(new Condition("blah", null, df.parse("2010-03-12T00:00:00"))));
    assertFalse(filter.isMet(new Condition(null, 123, df.parse("2010-03-12T00:00:00"))));
}
 
Example #26
Source File: FiqlParserTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testParseDateWithDefaultFormat() throws SearchParseException, ParseException {
    SearchCondition<Condition> filter = parser.parse("time=le=2010-03-11T18:00:00.000+00:00");
    DateFormat df = new SimpleDateFormat(SearchUtils.DEFAULT_DATE_FORMAT);
    assertTrue(filter.isMet(new Condition("whatever", 15, df.parse("2010-03-11T18:00:00.000+0000"))));
    assertTrue(filter.isMet(new Condition(null, null, df.parse("2010-03-10T22:22:00.000+0000"))));
    assertFalse(filter.isMet(new Condition("blah", null, df.parse("2010-03-12T00:00:00.000+0000"))));
    assertFalse(filter.isMet(new Condition(null, 123, df.parse("2010-03-12T00:00:00.000+0000"))));
}
 
Example #27
Source File: FiqlParserTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testParseLevel() throws SearchParseException {
    SearchCondition<Condition> filter = parser.parse("level=gt=10");
    assertTrue(filter.isMet(new Condition("whatever", 15, new Date())));
    assertTrue(filter.isMet(new Condition(null, 15, null)));
    assertFalse(filter.isMet(new Condition("blah", 5, new Date())));
    assertFalse(filter.isMet(new Condition("foobar", 0, null)));
}
 
Example #28
Source File: FiqlParserTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
private void doTestParseName(String exp) throws SearchParseException {
    SearchCondition<Condition> filter = parser.parse(exp);
    assertTrue(filter.isMet(new Condition("king", 10, new Date())));
    assertTrue(filter.isMet(new Condition("king", 0, null)));
    assertFalse(filter.isMet(new Condition("diamond", 10, new Date())));
    assertFalse(filter.isMet(new Condition("diamond", 0, null)));
}
 
Example #29
Source File: FiqlParserTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
private void doTestParseName2(String exp) throws SearchParseException {
    SearchCondition<Condition> filter = parser.parse(exp);
    assertTrue(filter.isMet(new Condition("king", 10, new Date(), "king2")));
    assertTrue(filter.isMet(new Condition("king", 0, null, "king2")));
    assertFalse(filter.isMet(new Condition("diamond", 10, new Date(), "theking2")));
    assertFalse(filter.isMet(new Condition("diamond", 0, null, "theking2")));
}
 
Example #30
Source File: AbstractJPATypedQueryVisitorTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected List<Book> criteriaQueryBooksOrderBy(String expression, boolean asc) throws Exception {
    SearchCondition<Book> filter = getParser().parse(expression);
    JPACriteriaQueryVisitor<Book, Book> jpa =
        new JPACriteriaQueryVisitor<Book, Book>(em, Book.class, Book.class);
    filter.accept(jpa);

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

    return jpa.getOrderedTypedQuery(selections, asc).getResultList();
}