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.vmware.thinapp.common.util.AfUtil.java

/**
 * Given a URI, return a new URI with the query, fragment, and top level of
 * the path removed./*w w  w .ja va 2s .c o  m*/
 *
 * @param uri the input URI
 * @return the base URI
 */
public static URI parentUri(URI uri) throws URISyntaxException {
    if (uri == null) {
        return null;
    }
    String protocol = uri.getScheme();
    String host = uri.getHost();

    // Process the port number, if any
    int port = uri.getPort();

    // Process the path, if any
    String path = uri.getPath();
    if (!StringUtils.hasLength(path)) {
        path = "";
    } else {
        if (path.endsWith("/")) {
            path = path.substring(0, path.length() - 1);
        }
        if (!path.contains("/")) {
            path = "";
        } else {
            int lastSlash = path.lastIndexOf('/');
            path = path.substring(0, lastSlash);
        }
    }

    // Build the final URL and return it
    return new URI(protocol, null, host, port, path, null, null);
}

From source file:org.soyatec.windowsazure.internal.util.HttpUtilities.java

/**
 * Remove the query part from URI./*from   ww w.jav  a 2s. c  o  m*/
 * 
 * @param uri
 * @return URI after remove the query part.
 */
public static URI removeQueryPart(URI uri) {
    try {
        return URIUtils.createURI(uri.getScheme(), uri.getHost(), uri.getPort(), uri.getPath(), null, null);
    } catch (URISyntaxException e) {
        Logger.error("Remove query part from uri failed.", e);
        return uri;
    }
}

From source file:com.vmware.thinapp.common.util.AfUtil.java

/**
 * Checks whether the given URL string begins with a protocol (http://,
 * ftp://, etc.)  If it does, the string is returned unchanged.  If it does
 * not, full URL is returned and is constructed as parentUrl "/" url.
 *
 * @param url input URL string in absolute or relative form
 * @param parentUrl base URL to use if the given URL is in relative form
 * @return an absolute URL//from  w  w  w  .  j  av a2s . c  o m
 */
public static URI relToAbs(String url, URI parentUrl) throws URISyntaxException {
    if (!StringUtils.hasLength(url)) {
        throw new URISyntaxException(url, "The input url was empty!");
    }
    URI parent2 = new URI(parentUrl.getScheme(), parentUrl.getUserInfo(), parentUrl.getAuthority(),
            parentUrl.getPort(), parentUrl.getPath() + "/", // Parent URL path must end with "/" for
            // this to work. resolve() removes any
            // duplicates.
            parentUrl.getQuery(), parentUrl.getFragment());

    return parent2.resolve(url.trim());
}

From source file:com.sangupta.jerry.oauth.OAuthUtils.java

/**
 * Return the signing base URL that is appended after the HTTP VERB in OAuth
 * header./*  www  .  j av a 2  s.  co  m*/
 * 
 * @param uri
 *            the {@link URI} instance from which the signing base is
 *            extracted
 * 
 * @return the extracted signing base from the {@link URI} instance
 */
public static String getSigningBaseURL(URI uri) {
    if (uri == null) {
        throw new IllegalArgumentException("URI cannot be null");
    }

    StringBuilder builder = new StringBuilder();
    builder.append(uri.getScheme().toLowerCase());
    builder.append("://");
    builder.append(uri.getHost().toLowerCase());

    int port = uri.getPort();
    if (!(port == 80 || port == -1)) {
        builder.append(':');
        builder.append(String.valueOf(port));
    }

    builder.append(uri.getPath());

    return builder.toString();
}

From source file:com.smartitengineering.cms.ws.resources.content.ContentResource.java

protected static ContentResource getContentResource(String contentUrl, UriBuilder absBuilder,
        ResourceContext context)//from  w w  w . jav  a2s  .c o  m
        throws ContainerException, IllegalArgumentException, ClassCastException, UriBuilderException {
    final URI uri;
    if (contentUrl.startsWith("http:")) {
        uri = URI.create(contentUrl);
    } else {
        URI absUri = absBuilder.build();
        uri = UriBuilder.fromPath(contentUrl).host(absUri.getHost()).port(absUri.getPort())
                .scheme(absUri.getScheme()).build();
    }
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("URI to content is " + uri);
    }
    ContentResource resource = context.matchResource(uri, ContentResource.class);
    return resource;
}

From source file:com.buaa.cfs.utils.NetUtils.java

/**
 * Resolve the uri's hostname and add the default port if not in the uri
 *
 * @param uri         to resolve//w  ww .j a  v  a2 s.  c om
 * @param defaultPort if none is given
 *
 * @return URI
 */
public static URI getCanonicalUri(URI uri, int defaultPort) {
    // skip if there is no authority, ie. "file" scheme or relative uri
    String host = uri.getHost();
    if (host == null) {
        return uri;
    }
    String fqHost = canonicalizeHost(host);
    int port = uri.getPort();
    // short out if already canonical with a port
    if (host.equals(fqHost) && port != -1) {
        return uri;
    }
    // reconstruct the uri with the canonical host and port
    try {
        uri = new URI(uri.getScheme(), uri.getUserInfo(), fqHost, (port == -1) ? defaultPort : port,
                uri.getPath(), uri.getQuery(), uri.getFragment());
    } catch (URISyntaxException e) {
        throw new IllegalArgumentException(e);
    }
    return uri;
}

From source file:com.smartitengineering.cms.ws.resources.content.ContentResource.java

protected static ContentTypeResource getContentTypeResource(String uri, ServerResourceInjectables injectables)
        throws ClassCastException, ContainerException {
    final URI checkUri;
    if (uri.startsWith("http:")) {
        checkUri = URI.create(uri);
    } else {/*from   w  ww  . j a v a 2 s . c  om*/
        URI absUri = injectables.getUriInfo().getBaseUriBuilder().build();
        checkUri = UriBuilder.fromPath(uri).host(absUri.getHost()).port(absUri.getPort())
                .scheme(absUri.getScheme()).build();
    }
    return injectables.getResourceContext().matchResource(checkUri, ContentTypeResource.class);
}

From source file:org.herrlado.engeo.Utils.java

/**
 * Update cookies from response.//from  w ww .j  av  a  2 s .c  o  m
 * 
 * @param cookies
 *            old {@link Cookie} list
 * @param headers
 *            {@link Header}s from {@link HttpResponse}
 * @param url
 *            requested URL
 * @throws URISyntaxException
 *             malformed URI
 * @throws MalformedCookieException
 *             malformed {@link Cookie}
 */
@Deprecated
public static void updateCookies(final ArrayList<Cookie> cookies, final Header[] headers, final String url)
        throws URISyntaxException, MalformedCookieException {
    final URI uri = new URI(url);
    int port = uri.getPort();
    if (port < 0) {
        if (url.startsWith("https")) {
            port = PORT_HTTPS;
        } else {
            port = PORT_HTTP;
        }
    }
    final CookieOrigin origin = new CookieOrigin(uri.getHost(), port, uri.getPath(), false);
    final CookieSpecBase cookieSpecBase = new BrowserCompatSpec();
    String name;
    String value;
    for (final Header header : headers) {
        for (final Cookie cookie : cookieSpecBase.parse(header, origin)) {
            // THE cookie
            name = cookie.getName();
            value = cookie.getValue();
            if (value == null || value.equals("")) {
                continue;
            }
            for (final Cookie c : cookies) {
                if (name.equals(c.getName())) {
                    cookies.remove(c);
                    cookies.add(cookie);
                    name = null;
                    break;
                }
            }
            if (name != null) {
                cookies.add(cookie);
            }
        }
    }
}

From source file:com.nestorledon.employeedirectory.http.HttpComponentsClientHttpRequestFactoryBasicAuth.java

protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) {
    HttpHost host = new HttpHost(uri.getHost(), uri.getPort());
    return createHttpContext(host);
}

From source file:com.orange.clara.cloud.servicedbdumper.helper.UrlForge.java

private String getPortInString() {
    URI appInUri = URI.create(appUri);
    String port = "";
    if (appInUri.getPort() != -1) {
        port = ":" + appInUri.getPort();
    }/*from  www.ja v  a 2  s  . c om*/
    return port;
}