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:cn.tiup.httpproxy.ProxyServlet.java

private static String getHost(HttpServletRequest request) {
    StringBuffer requestURL = request.getRequestURL();
    try {/*from   w ww  .  j  a  v  a  2 s  . co m*/
        URI uri = new URI(requestURL.toString());
        int port = uri.getPort();
        if (port > 0 && port != 80 && port != 443) {
            return uri.getHost() + ":" + Integer.toString(port);
        } else {
            return uri.getHost();
        }
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
    return request.getServerName();
}

From source file:com.netflix.iep.http.RxHttp.java

/**
 * Return the port taking care of handling defaults for http and https if not explicit in the
 * uri./*from  w ww  .j  a  va2 s. c  o  m*/
 */
static int getPort(URI uri) {
    final int defaultPort = ("https".equals(uri.getScheme())) ? 443 : 80;
    return (uri.getPort() <= 0) ? defaultPort : uri.getPort();
}

From source file:com.cloud.utils.UriUtils.java

public static Pair<String, Integer> validateUrl(String format, String url) throws IllegalArgumentException {
    try {/*from www  .j  a va 2 s .  c  o  m*/
        URI uri = new URI(url);
        if ((uri.getScheme() == null) || (!uri.getScheme().equalsIgnoreCase("http")
                && !uri.getScheme().equalsIgnoreCase("https") && !uri.getScheme().equalsIgnoreCase("file"))) {
            throw new IllegalArgumentException("Unsupported scheme for url: " + url);
        }
        int port = uri.getPort();
        if (!(port == 80 || port == 8080 || port == 443 || port == -1)) {
            throw new IllegalArgumentException("Only ports 80, 8080 and 443 are allowed");
        }

        if (port == -1 && uri.getScheme().equalsIgnoreCase("https")) {
            port = 443;
        } else if (port == -1 && uri.getScheme().equalsIgnoreCase("http")) {
            port = 80;
        }

        String host = uri.getHost();
        try {
            InetAddress hostAddr = InetAddress.getByName(host);
            if (hostAddr.isAnyLocalAddress() || hostAddr.isLinkLocalAddress() || hostAddr.isLoopbackAddress()
                    || hostAddr.isMulticastAddress()) {
                throw new IllegalArgumentException("Illegal host specified in url");
            }
            if (hostAddr instanceof Inet6Address) {
                throw new IllegalArgumentException(
                        "IPV6 addresses not supported (" + hostAddr.getHostAddress() + ")");
            }
        } catch (UnknownHostException uhe) {
            throw new IllegalArgumentException("Unable to resolve " + host);
        }

        // verify format
        if (format != null) {
            String uripath = uri.getPath();
            checkFormat(format, uripath);
        }
        return new Pair<String, Integer>(host, port);
    } catch (URISyntaxException use) {
        throw new IllegalArgumentException("Invalid URL: " + url);
    }
}

From source file:org.hawk.service.api.utils.APIUtils.java

@SuppressWarnings({ "deprecation", "restriction" })
public static <T extends TServiceClient> T connectTo(Class<T> clazz, String url, ThriftProtocol thriftProtocol,
        final Credentials credentials) throws TTransportException, URISyntaxException {
    try {/*from ww  w.j  av a  2  s.  com*/
        final URI parsed = new URI(url);

        TTransport transport;
        if (parsed.getScheme().startsWith("http")) {
            final DefaultHttpClient httpClient = APIUtils.createGZipAwareHttpClient();
            if (credentials != null) {
                httpClient.getCredentialsProvider().setCredentials(new AuthScope(null, -1), credentials);
            }
            transport = new THttpClient(url, httpClient);
        } else {
            transport = new TZlibTransport(new TSocket(parsed.getHost(), parsed.getPort()));
            transport.open();
        }
        Constructor<T> constructor = clazz.getDeclaredConstructor(org.apache.thrift.protocol.TProtocol.class);
        return constructor.newInstance(thriftProtocol.getProtocolFactory().getProtocol(transport));
    } catch (InstantiationException | IllegalAccessException | IllegalArgumentException
            | InvocationTargetException | NoSuchMethodException | SecurityException e) {
        throw new TTransportException(e);
    }
}

From source file:com.offbytwo.jenkins.client.JenkinsHttpClient.java

protected static HttpClientBuilder addAuthentication(HttpClientBuilder builder, URI uri, String username,
        String password) {// w  w  w .  ja  v a2s .  c o m
    if (isNotBlank(username)) {
        CredentialsProvider provider = new BasicCredentialsProvider();
        AuthScope scope = new AuthScope(uri.getHost(), uri.getPort(), "realm");
        UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
        provider.setCredentials(scope, credentials);
        builder.setDefaultCredentialsProvider(provider);

        builder.addInterceptorFirst(new PreemptiveAuth());
    }
    return builder;
}

From source file:annis.libgui.Helper.java

public static String generateClassicCitation(String aql, List<String> corpora, int contextLeft,
        int contextRight) {
    StringBuilder sb = new StringBuilder();

    URI appURI = UI.getCurrent().getPage().getLocation();

    sb.append(getContext());//from w  w w  . j a v a  2s.  com
    sb.append("/Cite/AQL(");
    sb.append(aql);
    sb.append("),CIDS(");
    sb.append(StringUtils.join(corpora, ","));
    sb.append("),CLEFT(");
    sb.append(contextLeft);
    sb.append("),CRIGHT(");
    sb.append(contextRight);
    sb.append(")");

    try {
        return new URI(appURI.getScheme(), null, appURI.getHost(), appURI.getPort(), sb.toString(), null, null)
                .toASCIIString();
    } catch (URISyntaxException ex) {
        log.error(null, ex);
    }
    return "ERROR";
}

From source file:password.pwm.http.servlet.OAuthConsumerServlet.java

public static String figureOauthSelfEndPointUrl(final PwmRequest pwmRequest) {
    final HttpServletRequest req = pwmRequest.getHttpServletRequest();
    final String redirect_uri;
    try {//from   www  . ja v a2 s.co  m
        final URI requestUri = new URI(req.getRequestURL().toString());
        redirect_uri = requestUri.getScheme() + "://" + requestUri.getHost()
                + (requestUri.getPort() > 0 ? ":" + requestUri.getPort() : "")
                + PwmServletDefinition.OAuthConsumer.servletUrl();
    } catch (URISyntaxException e) {
        throw new IllegalStateException(
                "unable to parse inbound request uri while generating oauth redirect: " + e.getMessage());
    }
    return redirect_uri;
}

From source file:com.sworddance.util.UriFactoryImpl.java

/**
 * Utility method to calculate port based on defaults for different schemas.
 *
 * @param uri uri to calculate port for/*w  w  w.  j a v  a2 s. c om*/
 * @return integer port presentation
 */
public static int getPort(URI uri) {
    int port = uri.getPort();
    if (port == -1) {
        String scheme = uri.getScheme();
        if (HTTPS_SCHEME.equalsIgnoreCase(scheme)) {
            port = DEFAULT_HTTPS_PORT;
        } else {
            port = DEFAULT_HTTP_PORT;
        }
    }
    return port;
}

From source file:org.soyatec.windowsazure.authenticate.HttpRequestAccessor.java

/**
 * Given the host suffix part, service name and account name, this method
 * constructs the account Uri//from   w w w.  j a v a2 s  .  com
 * 
 * @param hostSuffix
 * @param accountName
 * @return URI
 */
private static URI constructHostStyleAccountUri(URI hostSuffix, String accountName) {
    URI serviceUri = hostSuffix;
    String hostString = hostSuffix.toString();
    if (!hostString.startsWith(HttpHost.DEFAULT_SCHEME_NAME)) {
        hostString = HttpHost.DEFAULT_SCHEME_NAME + "://" + hostString;
        serviceUri = URI.create(hostString);
    }
    // Example:
    // Input: serviceEndpoint="http://blob.windows.net/",
    // accountName="youraccount"
    // Output: accountUri="http://youraccount.blob.windows.net/"
    // serviceUri in our example would be "http://blob.windows.net/"
    String accountUriString = null;
    if (serviceUri.getPort() == -1) {
        accountUriString = MessageFormat.format("{0}{1}{2}.{3}/",
                Utilities.isNullOrEmpty(serviceUri.getScheme()) ? HttpHost.DEFAULT_SCHEME_NAME
                        : serviceUri.getScheme(),
                SCHEME_DELIMITER, accountName, serviceUri.getHost());
    } else {
        accountUriString = MessageFormat.format("{0}{1}{2}.{3}:{4}/",
                Utilities.isNullOrEmpty(serviceUri.getScheme()) ? HttpHost.DEFAULT_SCHEME_NAME
                        : serviceUri.getScheme(),
                SCHEME_DELIMITER, accountName, serviceUri.getHost(), serviceUri.getPort());
    }

    return URI.create(accountUriString);
}

From source file:com.net.cookie.HttpCookie.java

/** @hide */
public static int getEffectivePort(URI uri) {
    return getEffectivePort(uri.getScheme(), uri.getPort());
}