@Nullable
    public static URI getUriWithPort(@Nullable final URI uri, final int port) {
        if (uri == null) {
            return null;
        }

        try {
            if (uri.getPort() == -1) {
                final int realPort;
                switch (uri.getScheme()) {
                    case "http":
                        realPort = 80;
                        break;
                    case "https":
                        realPort = 443;
                        break;
                    default:
                        realPort = port;
                }

                return new URI(
                        uri.getScheme(),
                        uri.getUserInfo(),
                        uri.getHost(),
                        realPort,
                        uri.getPath(),
                        uri.getQuery(),
                        uri.getFragment());
            }

            return uri;
        } catch (URISyntaxException e) {
            throw new RuntimeException("Could not parse URI.", e);
        }
    }
@Test
    public void testGetUriWithPort() throws Exception {
        final URI uriWithPort = new URI("http://example.com:12345");
        final URI httpUriWithoutPort = new URI("http://example.com");
        final URI httpsUriWithoutPort = new URI("https://example.com");
        final URI uriWithUnknownSchemeAndWithoutPort = new URI("foobar://example.com");

        assertEquals(12345, Tools.getUriWithPort(uriWithPort, 1).getPort());
        assertEquals(80, Tools.getUriWithPort(httpUriWithoutPort, 1).getPort());
        assertEquals(443, Tools.getUriWithPort(httpsUriWithoutPort, 1).getPort());
        assertEquals(1, Tools.getUriWithPort(uriWithUnknownSchemeAndWithoutPort, 1).getPort());
    }