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

com.soento.core.util.UrlUtil Maven / Gradle / Ivy

package com.soento.core.util;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * @author soento
 */
public final class UrlUtil {

    public static Map getParamaters(String url) {
        Map params = new HashMap<>(16);
        if (StringUtil.isBlank(url)) {
            return params;
        }
        if (url.indexOf("?") <= 0) {
            return params;
        }
        String kvData = url.substring(url.indexOf("?") + 1);
        if (StringUtil.isBlank(kvData)) {
            return params;
        }
        String[] kvs = kvData.split("&");
        if (kvs != null && kvs.length > 0) {
            for (String kv : kvs) {
                String k = kv.substring(0, kv.indexOf("="));
                String v = kv.substring(kv.indexOf("=") + 1);
                params.put(k, v);
            }
        }
        return params;
    }

    public static String encrypt(String input, Charset charset) {
        try {
            return URLEncoder.encode(input, charset.name());
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    public static String encrypt(String input) {
        return encrypt(input, StandardCharsets.UTF_8);
    }

    public static String decrypt(String input, Charset charset) {
        try {
            return URLDecoder.decode(input, charset.name());
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    public static String decrypt(String input) {
        return decrypt(input, StandardCharsets.UTF_8);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy