class SAML2StateGenerator implements ValueGenerator {

    /** Constant <code>SAML_RELAY_STATE_ATTRIBUTE="samlRelayState"</code> */
    public static final String SAML_RELAY_STATE_ATTRIBUTE = "samlRelayState";

    private final SAML2Client client;

    /** {@inheritDoc} */
    @Override
    public String generateValue(final CallContext ctx) {<FILL_FUNCTION_BODY>}
}

        val webContext = ctx.webContext();
        val sessionStore = ctx.sessionStore();

        val relayState = sessionStore.get(webContext, SAML_RELAY_STATE_ATTRIBUTE);
        // clean from session after retrieving it
        if (relayState.isPresent()) {
            sessionStore.set(webContext, SAML_RELAY_STATE_ATTRIBUTE, null);
        }
        return relayState.isPresent() ? (String) relayState.get() : client.computeFinalCallbackUrl(webContext);
    
<no_super_class>