private String constructRedirectUrl(final String serviceUrl, final Map<String, String> parameters) throws Exception {
        val properties = configurationContext.getCasProperties();
        val urlBuilder = new URIBuilder(properties.getServer().getLoginUrl());
        urlBuilder.addParameter(CasProtocolConstants.PARAMETER_SERVICE, serviceUrl);
        parameters
            .entrySet()
            .stream()
            .filter(entry -> StringUtils.isNotBlank(entry.getValue()))
            .forEach(entry -> urlBuilder.addParameter(entry.getKey(), EncodingUtils.urlEncode(entry.getValue())));
        return urlBuilder.build().toASCIIString();
    }
Handle SAML2 exceptions.

@param req the req
@param ex  the ex
@return the model and view
support/cas-server-support-saml-idp-web/src/main/java/org/apereo/cas/support/saml/web/idp/profile/AbstractSamlIdPProfileHandlerController.java