protected T encodeFinalResponse(final SamlProfileBuilderContext context,
                                    final T finalResponse) throws Exception {
        val scratch = context.getMessageContext().ensureSubcontext(ScratchContext.class);
        val map = (Map) Objects.requireNonNull(scratch).getMap();
        val encodeResponse = (Boolean) map.getOrDefault(SamlProtocolConstants.PARAMETER_ENCODE_RESPONSE, Boolean.TRUE);

        if (encodeResponse) {
            val relayState = SAMLBindingSupport.getRelayState(context.getMessageContext());
            LOGGER.trace("Relay state is [{}]", relayState);
            return encode(context, finalResponse, relayState);
        }
        return finalResponse;
    }
The {@link BaseSamlProfileSamlResponseBuilder} is responsible for
building the final SAML assertion for the relying party.

@author Misagh Moayyed
@since 4.2
support/cas-server-support-saml-idp-web/src/main/java/org/apereo/cas/support/saml/web/idp/profile/builders/response/BaseSamlProfileSamlResponseBuilder.java