Example usage for java.net URI getPort

List of usage examples for java.net URI getPort

Introduction

In this page you can find the example usage for java.net URI getPort.

Prototype

public int getPort() 

Source Link

Document

Returns the port number of this URI.

Usage

From source file:com.cloudbees.jenkins.plugins.bitbucket.endpoints.BitbucketEndpointConfiguration.java

/**
 * Fix a serverUrl.// w  w  w  .j a va 2  s. c  o m
 *
 * @param serverUrl the server URL.
 * @return the normalized server URL.
 */
@NonNull
public static String normalizeServerUrl(@CheckForNull String serverUrl) {
    serverUrl = StringUtils.defaultIfBlank(serverUrl, BitbucketCloudEndpoint.SERVER_URL);
    try {
        URI uri = new URI(serverUrl).normalize();
        String scheme = uri.getScheme();
        if ("http".equals(scheme) || "https".equals(scheme)) {
            // we only expect http / https, but also these are the only ones where we know the authority
            // is server based, i.e. [userinfo@]server[:port]
            // DNS names must be US-ASCII and are case insensitive, so we force all to lowercase

            String host = uri.getHost() == null ? null : uri.getHost().toLowerCase(Locale.ENGLISH);
            int port = uri.getPort();
            if ("http".equals(scheme) && port == 80) {
                port = -1;
            } else if ("https".equals(scheme) && port == 443) {
                port = -1;
            }
            serverUrl = new URI(scheme, uri.getUserInfo(), host, port, uri.getPath(), uri.getQuery(),
                    uri.getFragment()).toASCIIString();
        }
    } catch (URISyntaxException e) {
        // ignore, this was a best effort tidy-up
    }
    return serverUrl.replaceAll("/$", "");
}

From source file:com.fujitsu.dc.core.rs.cell.MessageODataResource.java

/**
 * To????baseUrl??????.//  w  ww.ja v  a  2 s  . com
 * @param toValue to???
 * @param baseUrl baseUrl
 */
public static void validateToValue(String toValue, String baseUrl) {
    if (toValue == null) {
        return;
    }
    // URL??
    String checkBaseUrl = null;
    String[] uriList = toValue.split(",");
    for (String uriStr : uriList) {
        try {
            URI uri = new URI(uriStr);
            checkBaseUrl = uri.getScheme() + "://" + uri.getHost();
            int port = uri.getPort();
            if (port != -1) {
                checkBaseUrl += ":" + Integer.toString(port);
            }
            checkBaseUrl += "/";
        } catch (URISyntaxException e) {
            log.info(e.getMessage());
            throw DcCoreException.OData.REQUEST_FIELD_FORMAT_ERROR.params(SentMessage.P_TO.getName());
        }

        if (checkBaseUrl == null || !checkBaseUrl.equals(baseUrl)) {
            throw DcCoreException.OData.REQUEST_FIELD_FORMAT_ERROR.params(SentMessage.P_TO.getName());
        }
    }

}

From source file:com.microsoft.tfs.core.util.URIUtils.java

/**
 * Returns a new {@link URI} containing only the scheme, user info, host,
 * and port of the given {@link URI}.// ww w .  j  ava2  s  . c om
 *
 * @param uri
 *        the {@link URI} to remove the path and query parts from (must not
 *        be <code>null</code>)
 * @return a new {@link URI} containing only the scheme, user info, host,
 *         and port of the given {@link URI}
 */
public static URI removePathAndQueryParts(final URI uri) {
    Check.notNull(uri, "uri"); //$NON-NLS-1$

    try {
        return new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), null, null, null);
    } catch (final URISyntaxException e) {
        final IllegalArgumentException e2 = new IllegalArgumentException(
                MessageFormat.format(Messages.getString("URIUtils.IllegalURIFormat"), uri)); //$NON-NLS-1$
        e2.initCause(e);
        throw e2;
    }
}

From source file:com.microsoft.tfs.core.util.URIUtils.java

/**
 * Returns a new {@link URI} containing only the scheme, user info, host,
 * port, and path of the given {@link URI}.
 *
 * @param uri/*  w  w w . jav  a 2  s  . c  om*/
 *        the {@link URI} to remove the query parts from (must not be
 *        <code>null</code>)
 * @return a new {@link URI} containing only the scheme, user info, host,
 *         port, and path of the given {@link URI}
 */
public static URI removeQueryParts(final URI uri) {
    Check.notNull(uri, "uri"); //$NON-NLS-1$

    try {
        return new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), uri.getPath(), null,
                null);
    } catch (final URISyntaxException e) {
        final IllegalArgumentException e2 = new IllegalArgumentException(
                MessageFormat.format(Messages.getString("URIUtils.IllegalURIFormat"), uri)); //$NON-NLS-1$
        e2.initCause(e);
        throw e2;
    }
}

From source file:org.cloudfoundry.caldecott.client.TunnelHelper.java

public static Map<String, String> getTunnelServiceInfo(CloudFoundryClient client, String serviceName) {
    String urlToUse = getTunnelUri(client) + "/services/" + serviceName;
    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.set("Auth-Token", getTunnelAuth(client));
    HttpEntity<?> requestEntity = new HttpEntity(requestHeaders);
    HttpEntity<String> response = restTemplate.exchange(urlToUse, HttpMethod.GET, requestEntity, String.class);
    String json = response.getBody().trim();
    Map<String, String> svcInfo = new HashMap<String, String>();
    try {/*from  w w  w  .j  a v  a2s . co  m*/
        svcInfo = convertJsonToMap(json);
    } catch (IOException e) {
        return new HashMap<String, String>();
    }
    if (svcInfo.containsKey("url")) {
        String svcUrl = svcInfo.get("url");
        try {
            URI uri = new URI(svcUrl);
            String[] userInfo;
            if (uri.getUserInfo().contains(":")) {
                userInfo = uri.getUserInfo().split(":");
            } else {
                userInfo = new String[2];
                userInfo[0] = uri.getUserInfo();
                userInfo[1] = "";
            }
            svcInfo.put("user", userInfo[0]);
            svcInfo.put("username", userInfo[0]);
            svcInfo.put("password", userInfo[1]);
            svcInfo.put("host", uri.getHost());
            svcInfo.put("hostname", uri.getHost());
            svcInfo.put("port", "" + uri.getPort());
            svcInfo.put("path", (uri.getPath().startsWith("/") ? uri.getPath().substring(1) : uri.getPath()));
            svcInfo.put("vhost", svcInfo.get("path"));
        } catch (URISyntaxException e) {
        }
    }
    return svcInfo;
}

From source file:com.daskiworks.ghwatch.backend.RemoteSystemClient.java

protected static DefaultHttpClient prepareHttpClient(URI uri, GHCredentials apiCredentials) {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpParams params = httpClient.getParams();
    HttpConnectionParams.setConnectionTimeout(params, 30000);
    HttpConnectionParams.setSoTimeout(params, 30000);
    httpClient.addRequestInterceptor(preemptiveAuth, 0);

    if (apiCredentials != null) {
        httpClient.getCredentialsProvider().setCredentials(
                new AuthScope(uri.getHost(), uri.getPort(), AuthScope.ANY_SCHEME),
                new UsernamePasswordCredentials(apiCredentials.getUsername(), apiCredentials.getPassword()));
    }//from  w w  w.j a v a 2 s .  c  o m
    return httpClient;
}

From source file:edu.internet2.middleware.openid.message.encoding.EncodingUtils.java

/**
 * Append the URL encoded OpenID message parameters to the query string of the provided URI.
 * //from  ww  w . j  a  va 2  s .  co m
 * @param uri URI to append OpenID message parameter to
 * @param message URL encoded OpenID message parameters
 * @return URI with message parameters appended
 */
public static URI appendMessageParameters(URI uri, String message) {
    if (uri == null || message == null) {
        return uri;
    }

    // build new query string
    StringBuffer queryBuffer = new StringBuffer();
    if (uri.getRawQuery() != null) {
        queryBuffer.append(uri.getRawQuery());
    }
    if (queryBuffer.length() > 0 && queryBuffer.charAt(queryBuffer.length() - 1) != '&') {
        queryBuffer.append('&');
    }
    queryBuffer.append(message);

    try {
        return URIUtils.createURI(uri.getScheme(), uri.getHost(), uri.getPort(), uri.getPath(),
                queryBuffer.toString(), uri.getFragment());
    } catch (URISyntaxException e) {
        log.error("Unable to append message parameters to URI: {}", e);
    }

    return null;
}

From source file:io.personium.core.rs.cell.MessageODataResource.java

/**
 * To????baseUrl??????.//  w  w  w.j a va  2 s.c  om
 * @param toValue to???
 * @param baseUrl baseUrl
 */
public static void validateToValue(String toValue, String baseUrl) {
    if (toValue == null) {
        return;
    }
    // URL??
    String checkBaseUrl = null;
    String[] uriList = toValue.split(",");
    for (String uriStr : uriList) {
        try {
            URI uri = new URI(uriStr);
            checkBaseUrl = uri.getScheme() + "://" + uri.getHost();
            int port = uri.getPort();
            if (port != -1) {
                checkBaseUrl += ":" + Integer.toString(port);
            }
            checkBaseUrl += "/";
        } catch (URISyntaxException e) {
            log.info(e.getMessage());
            throw PersoniumCoreException.OData.REQUEST_FIELD_FORMAT_ERROR.params(SentMessage.P_TO.getName());
        }

        if (checkBaseUrl == null || !checkBaseUrl.equals(baseUrl)) {
            throw PersoniumCoreException.OData.REQUEST_FIELD_FORMAT_ERROR.params(SentMessage.P_TO.getName());
        }
    }

}

From source file:org.hl7.fhir.client.ResourceAddress.java

public static URI appendHttpParameters(URI basePath, Map<String, String> parameters) {
    try {/*from w w  w. j a  v a  2  s.co m*/
        Set<String> httpParameterNames = parameters.keySet();
        String query = basePath.getQuery();

        for (String httpParameterName : httpParameterNames) {
            if (query != null) {
                query += "&";
            } else {
                query = "";
            }
            query += httpParameterName + "=" + parameters.get(httpParameterName);
        }

        return new URI(basePath.getScheme(), basePath.getUserInfo(), basePath.getHost(), basePath.getPort(),
                basePath.getPath(), query, basePath.getFragment());
    } catch (Exception e) {
        throw new EFhirClientException("Error appending http parameter", e);
    }
}

From source file:com.microsoft.tfs.core.credentials.internal.KeychainCredentialsManager.java

/**
 * @param uri//from   w ww .  j av  a2 s.  c o m
 *        the {@link URI} to use; should already have path and query parts
 *        removed ({@link URIUtils#removePathAndQueryParts(URI)}) (must not
 *        be <code>null</code>)
 */
private static KeychainInternetPassword newKeychainInternetPasswordFromURI(final URI uri) {
    Check.notNull(uri, "uri"); //$NON-NLS-1$

    final KeychainInternetPassword keychainPassword = new KeychainInternetPassword();

    /*
     * Compute the protocol.
     */
    if ("http".equalsIgnoreCase(uri.getScheme())) //$NON-NLS-1$
    {
        keychainPassword.setProtocol(KeychainProtocol.HTTP);
    } else if ("https".equalsIgnoreCase(uri.getScheme())) //$NON-NLS-1$
    {
        keychainPassword.setProtocol(KeychainProtocol.HTTPS);
    } else {
        keychainPassword.setProtocol(KeychainProtocol.ANY);
    }

    if (uri.getHost() != null && uri.getHost().length() > 0) {
        keychainPassword.setServerName(uri.getHost());
    }

    if (uri.getPort() > 0) {
        keychainPassword.setPort(uri.getPort());
    }

    if (uri.getPath() != null) {
        keychainPassword.setPath(uri.getPath());
    }

    return keychainPassword;
}