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

com.starksign.utils.Url Maven / Gradle / Ivy

Go to download

Welcome to the Stark Sign Java SDK! This tool is made for Java developers who want to easily integrate with our API. This SDK version is compatible with the Stark Sign API v2.

The newest version!
package com.starksign.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.*;


final class Url {

    static StringBuilder encode(Map query) throws UnsupportedEncodingException {
        StringBuilder queryString = new StringBuilder("?");
        String separator = "&";
        for (HashMap.Entry entry : query.entrySet()) {
            String key = entry.getKey();
            if (entry.getValue() == null) {
                continue;
            }
            queryString.append(separator).append(key).append("=").append(encode(entry.getValue()));
            separator = "&";
        }
        return queryString;
    }

    private static Object encode(Object entryValue) throws UnsupportedEncodingException {
        if (!(entryValue.getClass().isArray()) && !(entryValue instanceof Collection)) {
            return encode(entryValue.toString());
        }
        StringBuilder value = new StringBuilder();
        String listSeparator = "";
        List values = convertObjectToList(entryValue);
        for (Object object : values) {
            value.append(listSeparator).append(encode(object));
            listSeparator = ",";
        }
        return value;
    }

    private static String encode(String string) throws UnsupportedEncodingException {
        return URLEncoder.encode(string, StandardCharsets.UTF_8.toString());
    }

    private static List convertObjectToList(Object obj) {
        if (obj.getClass().isArray()) {
            return Arrays.asList((Object[])obj);
        }
        return new ArrayList<>((Collection)obj);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy