public static URI buildURI(String schema, String hostPort, String path, Map<String, String> params) {
    StringTokenizer tokenizer = new StringTokenizer(hostPort, ":");
    String host = tokenizer.nextToken();
    String port = null;
    if (tokenizer.hasMoreTokens()) {
      port = tokenizer.nextToken();
    }
    URIBuilder uriBuilder = new URIBuilder().setScheme(schema).setHost(host).setPath(path);
    if (port != null) {
      uriBuilder.setPort(Integer.parseInt(port));
    }
    for (Map.Entry<String, String> entry : params.entrySet()) {
      uriBuilder.addParameter(entry.getKey(), entry.getValue());
    }
    try {
      return uriBuilder.build();
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
@Test
  public void testBuildURI() {
    URI uri = URIUtils.buildURI("http", "foo", "bar", Collections.emptyMap());
    Assert.assertEquals(uri.toString(), "http://foo/bar");

    uri = URIUtils.buildURI("http", "foo:8080", "bar/moo", Collections.emptyMap());
    Assert.assertEquals(uri.toString(), "http://foo:8080/bar/moo");
    Assert.assertEquals(uri.getHost(), "foo");
    Assert.assertEquals(uri.getPort(), 8080);

    // test that params get encoded
    Map<String, String> params = new LinkedHashMap<>();
    params.put("stringParam", "aString");
    params.put("stringParamNeedsEncoding", "{\"format\":\"JSON\",\"timeout\":1000}");
    uri = URIUtils.buildURI("http", "foo", "bar", params);
    Assert.assertEquals(uri.toString(), "http://foo/bar?stringParam=aString&stringParamNeedsEncoding=" + URIUtils
        .encode("{\"format\":\"JSON\",\"timeout\":1000}"));

    // test that path gets encoded
    uri = URIUtils.buildURI("http", "foo", "bar%moo{}", Collections.emptyMap());
    Assert.assertEquals(uri.toString(), "http://foo/" + URIUtils.encode("bar%moo{}"));
  }