[OLINGO-1417]OData V4: Adopt search option based on new V4 abnf

This commit is contained in:
ramya vasanth 2019-12-18 10:46:21 +05:30
parent 9f9aebde55
commit dfe1dd288c
4 changed files with 21 additions and 8 deletions

View File

@ -21,6 +21,7 @@ package org.apache.olingo.server.core.uri.parser.search;
import java.util.ArrayList;
import java.util.List;
/**
* <pre>
* searchExpr = ( OPEN BWS searchExpr BWS CLOSE / searchTerm )
@ -121,7 +122,11 @@ public class SearchTokenizer {
}
static boolean isAllowedWord(final char character) {
return Character.isUnicodeIdentifierStart(character);
return Character.isUnicodeIdentifierStart(character)
|| Character.DASH_PUNCTUATION == Character.getType(character)
|| Character.DECIMAL_DIGIT_NUMBER == Character.getType(character)
|| (Character.OTHER_PUNCTUATION == Character.getType(character) &&
(character != ';' && character != '"'));
}
/**
@ -614,6 +619,10 @@ public class SearchTokenizer {
*/
public List<SearchQueryToken> tokenize(final String searchQuery) throws SearchTokenizerException {
if (searchQuery.contains("%28") || searchQuery.contains("%29") || searchQuery.contains("%22")) {
throw new SearchTokenizerException("Invalid Token in Query string '",
SearchTokenizerException.MessageKeys.NOT_EXPECTED_TOKEN, searchQuery);
}
char[] chars = searchQuery.trim().toCharArray();
State state = new SearchExpressionState();

View File

@ -29,6 +29,7 @@ public class SearchParserAndTokenizerTest {
@Test
public void basicParsing() throws Exception {
assertQuery("99").resultsIn("'99'");
assertQuery("\"99\"").resultsIn("'99'");
assertQuery("a").resultsIn("'a'");
assertQuery("a AND b").resultsIn("{'a' AND 'b'}");
@ -83,7 +84,6 @@ public class SearchParserAndTokenizerTest {
@Test
public void invalidSearchQuery() throws Exception {
assertQuery("99").resultsIn(SearchParserException.MessageKeys.TOKENIZER_EXCEPTION);
assertQuery("NOT").resultsIn(SearchParserException.MessageKeys.INVALID_NOT_OPERAND);
assertQuery("AND").resultsInExpectedTerm(SearchQueryToken.Token.AND.name());
assertQuery("OR").resultsInExpectedTerm(SearchQueryToken.Token.OR.name());

View File

@ -54,16 +54,20 @@ public class SearchTokenizerTest {
assertQuery("AN").resultsIn(word("AN"));
assertQuery("O").resultsIn(word("O"));
assertQuery("notAw0rd").resultsIn(word("notAw0rd"));
assertQuery("1").resultsIn(word("1"));
assertQuery("-").resultsIn(word("-"));
assertQuery("1.23a").resultsIn(word("1.23a"));
assertQuery(",").resultsIn(word(","));
assertQuery("*").resultsIn(word("*"));
assertQuery("not,").resultsIn(word("not,"));
assertQuery("not.").resultsIn(word("not."));
assertQuery("B-B").resultsIn(word("B-B"));
assertQuery("Dž").resultsIn(word("Dž"));
// invalid
assertQuery("%2F").resultsIn(SearchTokenizerException.MessageKeys.FORBIDDEN_CHARACTER);
assertQuery("%3A").resultsIn(SearchTokenizerException.MessageKeys.FORBIDDEN_CHARACTER);
assertQuery("not%5B").resultsIn(SearchTokenizerException.MessageKeys.FORBIDDEN_CHARACTER);
assertQuery("not%7B").resultsIn(SearchTokenizerException.MessageKeys.FORBIDDEN_CHARACTER);
assertQuery("not%6A").resultsIn(SearchTokenizerException.MessageKeys.FORBIDDEN_CHARACTER);
assertQuery(";").resultsIn(SearchTokenizerException.MessageKeys.FORBIDDEN_CHARACTER);
assertQuery("%28").resultsIn(SearchTokenizerException.MessageKeys.NOT_EXPECTED_TOKEN);
assertQuery("%29").resultsIn(SearchTokenizerException.MessageKeys.NOT_EXPECTED_TOKEN);
assertQuery("not%22").resultsIn(SearchTokenizerException.MessageKeys.NOT_EXPECTED_TOKEN);
}
private Validator.Tuple word(final String literal) {

View File

@ -89,7 +89,7 @@ public class SearchParserTest {
.isExceptionMessage(SearchParserException.MessageKeys.TOKENIZER_EXCEPTION);
testUri.runEx("ESTwoKeyNav", "$search=%22a%5Cbc%22")
.isExceptionMessage(SearchParserException.MessageKeys.TOKENIZER_EXCEPTION);
testUri.runEx("ESTwoKeyNav", "$search=not%27allowed")
testUri.runEx("ESTwoKeyNav", "$search=not%28allowed")
.isExceptionMessage(SearchParserException.MessageKeys.TOKENIZER_EXCEPTION);
}