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

com.pkulak.httpclient.util.Form Maven / Gradle / Ivy

The newest version!
package com.pkulak.httpclient.util;

import com.google.common.collect.LinkedListMultimap;
import com.google.common.collect.Multimap;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.function.Supplier;
import java.util.stream.Collectors;

public class Form {
    public static String encode(String s) {
        try {
            return URLEncoder.encode(s, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("encoding not supported: UTF-8", e);
        }
    }

    public static String encode(Multimap map) {
        return map.entries().stream()
                .map(entry -> encode(entry.getKey()) + "=" + encode(entry.getValue().toString()))
                .collect(Collectors.joining("&"));
    }

    public static String encodeSupplied(Multimap> map) {
        return map.entries().stream()
                .map(entry -> encode(entry.getKey()) + "=" + encode(entry.getValue().get()))
                .collect(Collectors.joining("&"));
    }

    public static Multimap decode(String input) {
        Multimap accum = LinkedListMultimap.create();

        for (String part : input.split("&")) {
            String[] keyValue = part.split("=");

            try {
                accum.put(
                        URLDecoder.decode(keyValue[0], "UTF-8"),
                        URLDecoder.decode(keyValue[1], "UTF-8"));
            } catch (UnsupportedEncodingException e) { /* oh well... */ }
        }

        return accum;
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy