URIUtils.java 1.09 KB
package pl.itcrowd.youtrack.api;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;

public class URIUtils {

    public static URI buildURI(URI base, String path)
    {
        return buildURI(base, path, (String) null);
    }

    public static URI buildURI(URI base, String path, String query)
    {
        try {
            return new URI(base.getScheme(), base.getUserInfo(), base.getHost(), base.getPort(), base.getPath().replaceAll("/+$", "") + path, query,
                base.getFragment());
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }

    public static URI buildURI(URI base, String path, Map<String, Object> params)
    {
        final StringBuilder query = new StringBuilder();
        for (Map.Entry<String, Object> entry : params.entrySet()) {
            if (null != entry.getValue()) {
                query.append("&").append(entry.getKey()).append("=").append(entry.getValue());
            }
        }
        return buildURI(base, path, query.length() > 0 ? query.substring(1) : query.toString());
    }
}