* @return A URI representing a validated form of the url.
* @throws GadgetException If the url is not valid.
*/
protected Uri validateUrl(String urlToValidate) throws GadgetException {
if (urlToValidate == null) {
throw new GadgetException(GadgetException.Code.INVALID_PARAMETER,
"url parameter is missing.", HttpResponse.SC_BAD_REQUEST);
}
try {
UriBuilder url = UriBuilder.parse(urlToValidate);
if (!"http".equals(url.getScheme()) && !"https".equals(url.getScheme())) {
throw new GadgetException(GadgetException.Code.INVALID_PARAMETER,
"Invalid request url scheme in url: " + Utf8UrlCoder.encode(urlToValidate) +
"; only \"http\" and \"https\" supported.", HttpResponse.SC_BAD_REQUEST);
}
if (url.getPath() == null || url.getPath().length() == 0) {
url.setPath("/");
}
return url.toUri();
} catch (IllegalArgumentException e) {
throw new GadgetException(GadgetException.Code.INVALID_PARAMETER,
"url parameter is not a valid url: " + urlToValidate, HttpResponse.SC_BAD_REQUEST);
}
}