All Downloads are FREE. Search and download functionalities are using the official Maven repository.

edu.ksu.canvas.util.HttpParameterBuilder Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
package edu.ksu.canvas.util;

import edu.ksu.canvas.constants.CanvasConstants;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/*
 * This class transforms a map into a parameter string
 *
 * EX: '{ array[]: [a, b], item: [c] } would return the following string
 * ?array[]=a&array[]=b&item=c
 */
public class HttpParameterBuilder {
    private static final Logger LOG = LoggerFactory.getLogger(HttpParameterBuilder.class);

    public static String buildParameters(Map> parameters) {
        return parameters.entrySet().stream()
                .map(HttpParameterBuilder::buildParameter)
                .reduce((a, b) -> a + b)
                .filter(s -> s.length() > 0)
                .map(s -> s.substring(1))
                .map(paramString -> "?" + paramString)
                .orElse("");
    }

    private static String buildParameter(Map.Entry> entry) {
        String paramName = entry.getKey();
        return entry.getValue()
                .stream()
                .reduce("", (a, paramValue) -> {
                    String urlParams = "";
                    try {
                        urlParams = (a + "&" + URLEncoder.encode(paramName, CanvasConstants.URLENCODING_TYPE)
                                + "=" + URLEncoder.encode(paramValue, CanvasConstants.URLENCODING_TYPE));

                    } catch (UnsupportedEncodingException e){
                        LOG.warn("Failed to encode parameter " + paramName);
                    }
                    return urlParams;
                });
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy