@Test public void shouldValidateUrl() throws Exception { URI uri = null; try { uri = new URI(url); } catch (URISyntaxException ignored) { if (isValid) { Assert.fail(String.format("Failed to construct URI object using input: '%s'", url)); } else { return; } } assertThat( String.format("URL '%s' should yield to be %s", url, isValid ? "valid" : "invalid"), urlValidator.isValid(uri, null), is(isValid)); }
@Override public boolean isValid(final URI uri, final ConstraintValidatorContext constraintValidatorContext) { if (uri == null) { return true; } try { @SuppressWarnings("unused") URL ignored = uri.toURL(); return isValidScheme(uri.getScheme()) && isValidUserInfo(uri.getUserInfo()) && isValidHost(uri.getHost()) && isValidPort(uri.getPort()) && isValidPath(uri.getPath()) && isValidFragment(uri.getFragment()); } catch (MalformedURLException | IllegalArgumentException e) { log.debug("Failed to parse URL from {} with message {}", uri, e.getMessage()); } return false; }
UrlValidator extends ConstraintValidatorSupport<Url, URI> { @Override public boolean isValid(final URI uri, final ConstraintValidatorContext constraintValidatorContext) { if (uri == null) { return true; } try { @SuppressWarnings("unused") URL ignored = uri.toURL(); return isValidScheme(uri.getScheme()) && isValidUserInfo(uri.getUserInfo()) && isValidHost(uri.getHost()) && isValidPort(uri.getPort()) && isValidPath(uri.getPath()) && isValidFragment(uri.getFragment()); } catch (MalformedURLException | IllegalArgumentException e) { log.debug("Failed to parse URL from {} with message {}", uri, e.getMessage()); } return false; } }
UrlValidator extends ConstraintValidatorSupport<Url, URI> { @Override public boolean isValid(final URI uri, final ConstraintValidatorContext constraintValidatorContext) { if (uri == null) { return true; } try { @SuppressWarnings("unused") URL ignored = uri.toURL(); return isValidScheme(uri.getScheme()) && isValidUserInfo(uri.getUserInfo()) && isValidHost(uri.getHost()) && isValidPort(uri.getPort()) && isValidPath(uri.getPath()) && isValidFragment(uri.getFragment()); } catch (MalformedURLException | IllegalArgumentException e) { log.debug("Failed to parse URL from {} with message {}", uri, e.getMessage()); } return false; }  }
UrlValidator extends ConstraintValidatorSupport<Url, URI> { @Override public boolean isValid(final URI uri, final ConstraintValidatorContext constraintValidatorContext) { if (uri == null) { return true; } try { @SuppressWarnings("unused") URL ignored = uri.toURL(); return isValidScheme(uri.getScheme()) && isValidUserInfo(uri.getUserInfo()) && isValidHost(uri.getHost()) && isValidPort(uri.getPort()) && isValidPath(uri.getPath()) && isValidFragment(uri.getFragment()); } catch (MalformedURLException | IllegalArgumentException e) { log.debug("Failed to parse URL from {} with message {}", uri, e.getMessage()); } return false; }  @Override boolean isValid(final URI uri, final ConstraintValidatorContext constraintValidatorContext); }
UrlValidator extends ConstraintValidatorSupport<Url, URI> { @Override public boolean isValid(final URI uri, final ConstraintValidatorContext constraintValidatorContext) { if (uri == null) { return true; } try { @SuppressWarnings("unused") URL ignored = uri.toURL(); return isValidScheme(uri.getScheme()) && isValidUserInfo(uri.getUserInfo()) && isValidHost(uri.getHost()) && isValidPort(uri.getPort()) && isValidPath(uri.getPath()) && isValidFragment(uri.getFragment()); } catch (MalformedURLException | IllegalArgumentException e) { log.debug("Failed to parse URL from {} with message {}", uri, e.getMessage()); } return false; }  @Override boolean isValid(final URI uri, final ConstraintValidatorContext constraintValidatorContext);  }