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

net.n2oapp.framework.mvc.util.ServletUtil Maven / Gradle / Ivy

There is a newer version: 7.28.2
Show newest version
package net.n2oapp.framework.mvc.util;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Predicate;

import static java.util.Collections.emptyMap;

/**
 * @author iryabov
 * @since 24.03.2016
 */
public class ServletUtil {

    public static Map decodeParameters(HttpServletRequest request) {
        try {
            if (request.getQueryString() == null)
                return emptyMap();

            Map map = new LinkedHashMap<>();
            String[] parameters = request.getQueryString().split("&");
            for (String parameter : parameters) {
                String[] keyValue = parameter.split("=");
                String key = URLDecoder.decode(keyValue[0], "UTF-8");
                if (keyValue.length == 2) {
                    String value = URLDecoder.decode(keyValue[1], "UTF-8");
                    map.put(key, value);
                } else if (keyValue.length == 1) {
                    map.put(key, null);
                }
            }
            return map;
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    public static Map filterParameters(Map parameters, Predicate filter) {
        Map result = new LinkedHashMap<>(parameters);
        parameters.keySet().stream().filter(filter.negate()).forEach(result::remove);
        return result;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy