Skip to content

Commit

Permalink
CSW GetRecords doesn't escape query values when creating the Elastics…
Browse files Browse the repository at this point in the history
…earch query / Escape Elasticsearch special chars in IS LIKE literal queries
  • Loading branch information
josegar74 committed Jan 5, 2024
1 parent 8e0bad2 commit 6c18b14
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ protected static String escapeLikeLiteral(String text) {
}

protected static String convertLikePattern(PropertyIsLike filter) {
String result = StringEscapeUtils.escapeJson(filter.getLiteral());
String result = filter.getLiteral();
if (!filter.getWildCard().equals("*")) {
final String wildcardRe =
StringUtils.isNotEmpty(filter.getEscape())
Expand All @@ -196,6 +196,8 @@ protected static String convertLikePattern(PropertyIsLike filter) {
: filter.getSingleChar();
result = result.replaceAll(singleCharRe, "?");
}

result = StringEscapeUtils.escapeJson(escapeLikeLiteral(result));
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,86 @@ void testPropertyIsEqualTo() throws IOException {
assertFilterEquals(expected, input);
}

@Test
void testPropertyIsEqualToSpecialChars() throws IOException {
final String input =
"<Filter xmlns=\"http://www.opengis.net/ogc\">\n" //
+ " <PropertyIsEqualTo>\n" //
+ " <PropertyName>OnlineResourceType</PropertyName>\n" //
+ " <Literal>OGC:WMS</Literal>\n" //
+ " </PropertyIsEqualTo>\n" //
+ " </Filter>" //
+ "";

// EXPECTED:
final ObjectNode expected = EsJsonHelper.boolbdr(). //
must(array(queryStringPart("OnlineResourceType", "OGC\\:WMS"))). //
filter(queryStringPart()). //
bld();

assertFilterEquals(expected, input);
}

@Test
void testPropertyIsLike() throws IOException {

final String input =
"<Filter xmlns=\"http://www.opengis.net/ogc\">\n" //
+ " <PropertyIsLike wildCard=\"%\" singleChar=\"_\" escapeChar=\"\\\">\n" //
+ " <PropertyName>AnyText</PropertyName>\n" //
+ " <Literal>s\\_rvice\\%</Literal>\n" //
+ " </PropertyIsLike>\n" //
+ " </Filter>" //
+ "";

// EXPECTED:
final ObjectNode expected = EsJsonHelper.boolbdr(). //
must(array(queryStringPart("AnyText", "s?rvice*"))). //
filter(queryStringPart()). //
bld();

assertFilterEquals(expected, input);
}

@Test
void testPropertyIsLikeSpecialChars() throws IOException {

final String input =
"<Filter xmlns=\"http://www.opengis.net/ogc\">\n" //
+ " <PropertyIsLike wildCard=\"%\" singleChar=\"_\" escapeChar=\"\\\">\n" //
+ " <PropertyName>AnyText</PropertyName>\n" //
+ " <Literal>\"service\"</Literal>\n" //
+ " </PropertyIsLike>\n" //
+ " </Filter>" //
+ "";

// EXPECTED:
final ObjectNode expected = EsJsonHelper.boolbdr(). //
must(array(queryStringPart("AnyText", "\\\"service\\\""))). //
filter(queryStringPart()). //
bld();

assertFilterEquals(expected, input);


final String input2 =
"<Filter xmlns=\"http://www.opengis.net/ogc\">\n" //
+ " <PropertyIsLike wildCard=\"%\" singleChar=\"_\" escapeChar=\"\\\">\n" //
+ " <PropertyName>AnyText</PropertyName>\n" //
+ " <Literal>OGC:WMS\\%</Literal>\n" //
+ " </PropertyIsLike>\n" //
+ " </Filter>" //
+ "";

// EXPECTED:
final ObjectNode expected2 = EsJsonHelper.boolbdr(). //
must(array(queryStringPart("AnyText", "OGC\\:WMS*"))). //
filter(queryStringPart()). //
bld();

assertFilterEquals(expected2, input2);
}

@Test
void testLogicalAnd() throws IOException {

Expand Down

0 comments on commit 6c18b14

Please sign in to comment.