Example usage for java.net URL getPort

List of usage examples for java.net URL getPort

Introduction

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

Prototype

public int getPort() 

Source Link

Document

Gets the port number of this URL .

Usage

From source file:org.n52.web.common.RequestUtils.java

/**
 * Get the full request {@link URL} including the query parameter
 *
 * @return Request {@link URL} with query parameter
 * @throws IOException//from   w  w  w.  j av  a 2s  .c om
 * @throws URISyntaxException
 */
public static String resolveFullRequestUrl() throws IOException, URISyntaxException {
    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
            .getRequest();

    URL url = new URL(request.getRequestURL().toString());

    String scheme = url.getProtocol();
    String userInfo = url.getUserInfo();
    String host = url.getHost();

    int port = url.getPort();

    String path = request.getRequestURI();
    if (path != null && path.endsWith("/")) {
        path = path.substring(0, path.length() - 1);
    }
    String query = request.getQueryString();

    URI uri = new URI(scheme, userInfo, host, port, path, query, null);
    return uri.toString();
}

From source file:URLUtils.java

/**
 * normalize an URL,/*from  ww w.  j a  v a  2s.  com*/
 * 
 * @param u,
 *          the URL to normalize
 * @return a new URL, the normalized version of the parameter, or the u URL,
 *         if something failed in the process
 */
public static URL normalize(URL u) {
    String proto = u.getProtocol().toLowerCase();
    String host = u.getHost().toLowerCase();
    int port = u.getPort();

    if (port != -1) {
        if (url_defport != null) {
            try {
                int udp;
                udp = ((Integer) url_defport.invoke(u, (Object[]) null)).intValue();
                // we have the default, skip the port part
                if (udp == port) {
                    port = -1;
                }
            } catch (InvocationTargetException ex) {
            } catch (IllegalAccessException iex) {
            }
        } else {
            switch (port) {
            case 21:
                if (proto.equals("ftp")) {
                    port = -1;
                }
                break;
            case 80:
                if (proto.equals("http")) {
                    port = -1;
                }
                break;
            case 443:
                if (proto.equals("https")) {
                    port = -1;
                }
                break;
            }
        }
    }
    try {
        URL _nu;
        if (port == -1) {
            _nu = new URL(proto, host, u.getFile());
        } else {
            _nu = new URL(proto, host, port, u.getFile());
        }
        return _nu;
    } catch (MalformedURLException ex) {
    }
    return u;
}

From source file:org.duracloud.duradmin.spaces.controller.ContentItemController.java

public static String getBaseURL(HttpServletRequest request) throws MalformedURLException {
    URL url = new URL(request.getRequestURL().toString());
    int port = url.getPort();
    String baseURL = url.getProtocol() + "://" + url.getHost() + ":"
            + (port > 0 && port != 80 ? url.getPort() : "") + request.getContextPath();
    return baseURL;
}

From source file:com.calmio.calm.integration.Helpers.HTTPHandler.java

private static CredentialsProvider getCredentialsProvider(String url, String user, String pwd)
        throws URISyntaxException, MalformedURLException {
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    URL uri = new URL(url);
    String domain = uri.getHost();
    domain = domain.startsWith("www.") ? domain.substring(4) : domain;
    credsProvider.setCredentials(new AuthScope(domain, uri.getPort()),
            (Credentials) new UsernamePasswordCredentials(user, pwd));
    return credsProvider;
}

From source file:Main.java

private static URL getRootUrlForClass(Class<?> cls) {
    ClassLoader classLoader = cls.getClassLoader();
    String resource = cls.getName().replace('.', '/') + ".class";
    if (classLoader == null) {
        // A null class loader means the bootstrap class loader. In this case we use the
        // system class loader. This is safe since we can assume that the system class
        // loader uses parent first as delegation policy.
        classLoader = ClassLoader.getSystemClassLoader();
    }/*  w w w. j  av  a 2 s .c  om*/
    URL url = classLoader.getResource(resource);
    if (url == null) {
        return null;
    }
    String file = url.getFile();
    if (file.endsWith(resource)) {
        try {
            return new URL(url.getProtocol(), url.getHost(), url.getPort(),
                    file.substring(0, file.length() - resource.length()));
        } catch (MalformedURLException ex) {
            return null;
        }
    } else {
        return null;
    }
}

From source file:com.digitalpebble.stormcrawler.protocol.HttpRobotRulesParser.java

/**
 * Compose unique key to store and access robot rules in cache for given URL
 *///from  w w w. j  av  a 2 s. co  m
protected static String getCacheKey(URL url) {
    String protocol = url.getProtocol().toLowerCase(Locale.ROOT);
    String host = url.getHost().toLowerCase(Locale.ROOT);

    int port = url.getPort();
    if (port == -1) {
        port = url.getDefaultPort();
    }
    /*
     * Robot rules apply only to host, protocol, and port where robots.txt
     * is hosted (cf. NUTCH-1752). Consequently
     */
    String cacheKey = protocol + ":" + host + ":" + port;
    return cacheKey;
}

From source file:com.predic8.membrane.core.util.HttpUtil.java

public static int getPort(URL url) throws MalformedURLException {
    int port = url.getPort();
    if (port == -1) {
        port = url.getDefaultPort();/*w ww .  j a v a  2  s.c  o  m*/
        if (port == -1)
            port = 80;
    }
    return port;
}

From source file:com.shishu.utility.url.TableUtil.java

/**
 * Reverses a url's domain. This form is better for storing in hbase. Because
 * scans within the same domain are faster.
 * <p>//w  ww . j  a va 2  s . c om
 * E.g. "http://bar.foo.com:8983/to/index.html?a=b" becomes
 * "com.foo.bar:http:8983/to/index.html?a=b".
 *
 * @param url
 *          url to be reversed
 * @return Reversed url
 */
public static String reverseUrl(URL url) {
    String host = url.getHost();
    String file = url.getFile();
    String protocol = url.getProtocol();
    int port = url.getPort();

    StringBuilder buf = new StringBuilder();

    /* reverse host */
    reverseAppendSplits(host, buf);

    /* add protocol */
    buf.append(':');
    buf.append(protocol);

    /* add port if necessary */
    if (port != -1) {
        buf.append(':');
        buf.append(port);
    }

    /* add path */
    if (file.length() > 0 && '/' != file.charAt(0)) {
        buf.append('/');
    }
    buf.append(file);

    return buf.toString();
}

From source file:org.apache.gobblin.utils.HttpUtils.java

public static String createApacheHttpClientLimiterKey(Config config) {
    try {// w w w .  jav  a2 s.c o m
        String urlTemplate = config.getString(HttpConstants.URL_TEMPLATE);
        URL url = new URL(urlTemplate);
        String key = url.getProtocol() + "/" + url.getHost();
        if (url.getPort() > 0) {
            key = key + "/" + url.getPort();
        }
        log.info("Get limiter key [" + key + "]");
        return key;
    } catch (MalformedURLException e) {
        throw new IllegalStateException("Cannot get limiter key.", e);
    }
}

From source file:com.github.mike10004.jenkinsbbhook.WebhookHandler.java

protected static int getPortOrDefault(URL url) {
    int port = url.getPort();
    if (port == -1) {
        port = url.getDefaultPort();//from w  w  w. j a v  a 2  s  .co  m
    }
    return port;
}