class RedirectUriValidator {

    public void validate(ClientRegistration clientRegistration, String redirectUri) throws InvalidRequestException,
            RedirectUriMismatchException {

        if (isEmpty(redirectUri)) {
            if (clientRegistration.getRedirectUris().size() == 1) {
                return;
            }

            throw new InvalidRequestException("Missing parameter: redirect_uri");
        }

        final URI request = URI.create(redirectUri);

        if (request.getFragment() != null) {
            throw new RedirectUriMismatchException();
        }
        if (!request.isAbsolute()) {
            throw new RedirectUriMismatchException();
        }
        for (URI uri : clientRegistration.getRedirectUris()) {
            if (uri.equals(request)) {
                //GOOD CASE
                return;
            }
        }
        throw new RedirectUriMismatchException();
    }

}