Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add /filter/validate endpoint #336

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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", " "));
}
}
}