Last active
August 19, 2020 14:45
-
-
Save mjg123/cb5ee1be4c2992df47cf67d9b76b3062 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This method exists to work around a quirk in HttpServletRequest. For validation we only | |
// need to consider the parameters from the request *body*, but HSR.getParameterMap() *also* includes | |
// any parameters found in the query String, which will cause validation to fail. | |
// For POST requests, Twilio will not add any queryString params, but it's perfectly possible for a | |
// user to do that by including them in the webhook URL. | |
private HashMap<String, String> extractOnlyBodyParams(HttpServletRequest request, String validationUrl) throws IOException { | |
var allRequestParameters = request.getParameterMap(); | |
var queryStringParams = UriComponentsBuilder.fromUriString(validationUrl).build().getQueryParams(); | |
var validationParams = new HashMap<String, String>(); | |
// loop through _all_ parameters, only keeping ones which _don't_ appear in the query string. | |
allRequestParameters.forEach((name, values) -> { | |
for (String value : values) { | |
if (!(queryStringParams.containsKey(name) && queryStringParams.get(name).contains(value))) { | |
validationParams.put(name, value); | |
} | |
} | |
}); | |
return validationParams; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment