package org.apereo.cas.web.flow.actions;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apereo.cas.util.EncodingUtils;
import org.springframework.webflow.context.servlet.DefaultFlowUrlHandler;
import org.springframework.webflow.core.collection.AttributeMap;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Stream;

import lombok.Setter;

/**
 * Provides special handling for parameters in requests made to the CAS login
 * webflow.
 *
 * @author Scott Battaglia
 * @since 3.4
 */
@Slf4j
@Setter
public class CasDefaultFlowUrlHandler extends DefaultFlowUrlHandler {

    /**
     * Default flow execution key parameter name, {@value}.
     * Same as that used by {@link DefaultFlowUrlHandler}.
     **/
    public static final String DEFAULT_FLOW_EXECUTION_KEY_PARAMETER = "execution";

    /**
     * Flow execution parameter name.
     */
    private String flowExecutionKeyParameter = DEFAULT_FLOW_EXECUTION_KEY_PARAMETER;

    /**
     * Get the flow execution key.
     *
     * @param request the current HTTP servlet request.
     * @return the flow execution key.
     */
    @Override
    public String getFlowExecutionKey(final HttpServletRequest request) {
        return request.getParameter(this.flowExecutionKeyParameter);
    }

    @Override
    public String createFlowExecutionUrl(final String flowId, final String flowExecutionKey, final HttpServletRequest request) {
        final String encoding = getEncodingScheme(request);
        final StringBuilder builder = new StringBuilder(request.getRequestURI()).append('?');
        final Map<String, String[]> flowParams = new LinkedHashMap<>(request.getParameterMap());
        flowParams.put(this.flowExecutionKeyParameter, new String[]{flowExecutionKey});
        final String queryString = flowParams.entrySet().stream()
            .flatMap(entry -> encodeMultiParameter(entry.getKey(), entry.getValue(), encoding))
            .reduce((param1, param2) -> param1 + '&' + param2).orElse(StringUtils.EMPTY);
        builder.append(queryString);
        return builder.toString();
    }

    @Override
    public String createFlowDefinitionUrl(final String flowId, final AttributeMap input, final HttpServletRequest request) {
        return request.getRequestURI() + (request.getQueryString() != null ? '?' + request.getQueryString() : StringUtils.EMPTY);
    }

    private static Stream<String> encodeMultiParameter(final String key, final String[] values, final String encoding) {
        return Stream.of(values).map(value -> encodeSingleParameter(key, value, encoding));
    }

    private static String encodeSingleParameter(final String key, final String value, final String encoding) {
        return EncodingUtils.urlEncode(key, encoding) + '=' + EncodingUtils.urlEncode(value, encoding);
    }
}

@@ -363,43 +363,25 @@
 til.
-LinkedHashMap;%0Aimport java.util.Map
+stream.Collectors
 ;%0Aim
@@ -880,16 +880,65 @@
 cution%22;
+%0A    private static final String DELIMITER = %22&%22;
 %0A%0A    /*
@@ -1591,24 +1591,25 @@
 quest);%0A
+%0A
         
 final St
@@ -1604,324 +1604,40 @@
     
-final StringBuilder builder = new StringBuilder(request.getRequestURI()).append('?');%0A        final Map%3CString, String%5B%5D%3E flowParams = new LinkedHashMap%3C%3E(request.getParameterMap());%0A        flowParams.put(this.flowExecutionKeyParameter, new String%5B%5D%7BflowExecutionKey%7D);%0A        final String queryString = flowParams
+return request.getParameterMap()
 .ent
@@ -1653,16 +1653,20 @@
 tream()%0A
+    
         
@@ -1769,155 +1769,200 @@
     
-.reduce((param1, param2) -%3E param1 + '&' + param2).orElse(StringUtils.EMPTY);%0A        builder.append(queryString);%0A        return builder.toString(
+    .collect(Collectors.joining(DELIMITER, request.getRequestURI() + '?',%0A                        DELIMITER + encodeSingleParameter(this.flowExecutionKeyParameter, flowExecutionKey, encoding))
 );%0A 

Use streams utilities instead of creating intermediate variables (#3192)