Skip to content

Commit

Permalink
add /filter/validate endpoint
Browse files Browse the repository at this point in the history
(currently undocumented / unlisted in swagger ui)
  • Loading branch information
tyrasd committed Nov 15, 2024
1 parent 5947451 commit bcb8bec
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ Changelog

## 1.11.0-SNAPSHOT (current main)

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

## 1.10.4

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.heigit.ohsome.ohsomeapi.controller.filter;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.heigit.ohsome.ohsomeapi.exception.BadRequestException;
import org.heigit.ohsome.ohsomeapi.exception.ExceptionMessages;
import org.heigit.ohsome.ohsomeapi.oshdb.DbConnData;
import org.heigit.ohsome.oshdb.filter.FilterExpression;
import org.heigit.ohsome.oshdb.filter.FilterParser;
import org.jparsec.error.ParserException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
* REST controller for endpoints in "/filter".
*/
@Api(tags = "Filter")
@RestController
@RequestMapping("/filter")
public class ValidateController {
/**
* Validates a provided filter string: Returns 200 OK if the syntax is valid
* and an HTTP 400 error code otherwise.
*
* @param servletRequest <code>HttpServletRequest</code> of the incoming request
* @param servletResponse <code>HttpServletResponse</code> of the outgoing response
* @throws BadRequestException if a te filter cannot be parsed, the error object contains a
* message about the potential causes of the invalidity of the filter.
* @return if the filter is valid: the originally supplied filter
*/
@ApiOperation(nickname = "Filter Validator", value = "Checks a given ohsome filter string for syntax errors.")
@RequestMapping(value = "/validate", method = {RequestMethod.GET, RequestMethod.POST},
produces = {"text/plain", "application/json"})
public String validate(HttpServletRequest servletRequest,
HttpServletResponse servletResponse) {
FilterParser fp = new FilterParser(DbConnData.tagTranslator, true);
String filter = servletRequest.getParameter("filter");
if (filter == null || filter.isEmpty()) {
throw new BadRequestException("No filter parameter provided.");
}
try {
//noinspection ResultOfMethodCallIgnored
fp.parse(filter);
return filter;
} catch (ParserException ex) {
throw new BadRequestException(ExceptionMessages.FILTER_SYNTAX + " Detailed error message: "
+ ex.getMessage().replace("\n", " "));
}
}
}

0 comments on commit bcb8bec

Please sign in to comment.