Skip to content

Commit bcb8bec

Browse files
committed
add /filter/validate endpoint
(currently undocumented / unlisted in swagger ui)
1 parent 5947451 commit bcb8bec

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ Changelog
33

44
## 1.11.0-SNAPSHOT (current main)
55

6+
### New Features
7+
* add new endpoint `/filter/validate` that checks a given ohsome filter string for syntax errors.
68

79
## 1.10.4
810

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.heigit.ohsome.ohsomeapi.controller.filter;
2+
3+
import io.swagger.annotations.Api;
4+
import io.swagger.annotations.ApiOperation;
5+
import javax.servlet.http.HttpServletRequest;
6+
import javax.servlet.http.HttpServletResponse;
7+
import org.heigit.ohsome.ohsomeapi.exception.BadRequestException;
8+
import org.heigit.ohsome.ohsomeapi.exception.ExceptionMessages;
9+
import org.heigit.ohsome.ohsomeapi.oshdb.DbConnData;
10+
import org.heigit.ohsome.oshdb.filter.FilterExpression;
11+
import org.heigit.ohsome.oshdb.filter.FilterParser;
12+
import org.jparsec.error.ParserException;
13+
import org.springframework.web.bind.annotation.RequestMapping;
14+
import org.springframework.web.bind.annotation.RequestMethod;
15+
import org.springframework.web.bind.annotation.RestController;
16+
17+
/**
18+
* REST controller for endpoints in "/filter".
19+
*/
20+
@Api(tags = "Filter")
21+
@RestController
22+
@RequestMapping("/filter")
23+
public class ValidateController {
24+
/**
25+
* Validates a provided filter string: Returns 200 OK if the syntax is valid
26+
* and an HTTP 400 error code otherwise.
27+
*
28+
* @param servletRequest <code>HttpServletRequest</code> of the incoming request
29+
* @param servletResponse <code>HttpServletResponse</code> of the outgoing response
30+
* @throws BadRequestException if a te filter cannot be parsed, the error object contains a
31+
* message about the potential causes of the invalidity of the filter.
32+
* @return if the filter is valid: the originally supplied filter
33+
*/
34+
@ApiOperation(nickname = "Filter Validator", value = "Checks a given ohsome filter string for syntax errors.")
35+
@RequestMapping(value = "/validate", method = {RequestMethod.GET, RequestMethod.POST},
36+
produces = {"text/plain", "application/json"})
37+
public String validate(HttpServletRequest servletRequest,
38+
HttpServletResponse servletResponse) {
39+
FilterParser fp = new FilterParser(DbConnData.tagTranslator, true);
40+
String filter = servletRequest.getParameter("filter");
41+
if (filter == null || filter.isEmpty()) {
42+
throw new BadRequestException("No filter parameter provided.");
43+
}
44+
try {
45+
//noinspection ResultOfMethodCallIgnored
46+
fp.parse(filter);
47+
return filter;
48+
} catch (ParserException ex) {
49+
throw new BadRequestException(ExceptionMessages.FILTER_SYNTAX + " Detailed error message: "
50+
+ ex.getMessage().replace("\n", " "));
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)