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.apache.ambari.server.view.ViewURLStreamProvider.java

/**
 * Is it allowed to make calls to the supplied url
 * @param spec the URL//from   w ww  .j  a  v a2  s . c o  m
 * @return
 */
protected boolean isProxyCallAllowed(String spec) {
    if (StringUtils.isNotBlank(spec) && this.getHostPortRestrictionHandler().proxyCallRestricted()) {
        try {
            URL url = new URL(spec);
            return this.getHostPortRestrictionHandler().allowProxy(url.getHost(),
                    Integer.toString(url.getPort() == -1 ? url.getDefaultPort() : url.getPort()));
        } catch (MalformedURLException ex) {
        }
    }

    return true;
}

From source file:net.sbbi.upnp.devices.UPNPRootDevice.java

/**
 * Parsing an URL from the descriptionXML file
 * @param url the string representation fo the URL
 * @param baseURL the base device URL, needed if the url param is relative
 * @return an URL object defining the url param
 * @throws MalformedURLException if the url param or baseURL.toExternalForm() + url
 *                               cannot be parsed to create an URL object
 *///  ww  w  .  j  a v a2s.c  o  m
public final static URL getURL(String url, URL baseURL) throws MalformedURLException {
    URL rtrVal;
    if (url == null || url.trim().length() == 0)
        return null;
    try {
        rtrVal = new URL(url);
    } catch (MalformedURLException malEx) {
        // maybe that the url is relative, we add the baseURL and reparse it
        // if relative then we take the device baser url root and add the url
        if (baseURL != null) {
            url = url.replace('\\', '/');
            if (url.charAt(0) != '/') {
                // the path is relative to the device baseURL
                String externalForm = baseURL.toExternalForm();
                if (!externalForm.endsWith("/")) {
                    externalForm += "/";
                }
                rtrVal = new URL(externalForm + url);
            } else {
                // the path is not relative
                String URLRoot = baseURL.getProtocol() + "://" + baseURL.getHost() + ":" + baseURL.getPort();
                rtrVal = new URL(URLRoot + url);
            }
        } else {
            throw malEx;
        }
    }
    return rtrVal;
}

From source file:com.conwet.wirecloud.ide.WirecloudAPI.java

public WirecloudAPI(URL deploymentServer) {
    try {//from ww  w. ja v  a  2s .  com
        deploymentServer = new URL(deploymentServer.getProtocol(), deploymentServer.getHost(),
                deploymentServer.getPort(), deploymentServer.getPath());
    } catch (Exception e) {
        // Should not happen as the URL is build from a valid URL
        e.printStackTrace();
    }
    this.url = deploymentServer;
    this.AUTH_ENDPOINT = DEFAULT_AUTH_ENDPOINT;
    this.TOKEN_ENDPOINT = DEFAULT_TOKEN_ENDPOINT;
    try {
        this.UNIVERSAL_REDIRECT_URI = new URL(deploymentServer, DEFAULT_UNIVERSAL_REDIRECT_URI_PATH).toString();
    } catch (MalformedURLException e) {
        // Should not happen as the URL is build from a valid URL using a
        // constant
        e.printStackTrace();
    }
}

From source file:com.yunmall.ymsdk.net.http.AsyncHttpClient.java

/**
 * Will encode url, if not disabled, and adds params on the end of it
 *
 * @param url             String with URL, should be valid URL without params
 * @param params          RequestParams to be appended on the end of URL
 * @param shouldEncodeUrl whether url should be encoded (replaces spaces with %20)
 * @return encoded url if requested with params appended if any available
 *///from  ww  w  .j  av a2  s.c o  m
public static String getUrlWithQueryString(boolean shouldEncodeUrl, String url, RequestParams params) {
    if (url == null) {
        return null;
    }
    if (shouldEncodeUrl) {
        try {
            String decodedURL = URLDecoder.decode(url, "UTF-8");
            URL _url = new URL(decodedURL);
            URI _uri = new URI(_url.getProtocol(), _url.getUserInfo(), _url.getHost(), _url.getPort(),
                    _url.getPath(), _url.getQuery(), _url.getRef());
            url = _uri.toASCIIString();
        } catch (Exception ex) {
            // Should not really happen, added just for sake of validity
            YmLog.e(LOG_TAG, "getUrlWithQueryString encoding URL", ex);
        }
    }

    if (params != null) {
        // Construct the query string and trim it, in case it
        // includes any excessive white spaces.
        String paramString = params.getParamString().trim();

        // Only add the query string if it isn't empty and it
        // isn't equal to '?'.
        if (!paramString.equals("") && !paramString.equals("?")) {
            url += url.contains("?") ? "&" : "?";
            url += paramString;
        }
    }
    url = url.replaceAll(" ", "%20");

    return url;
}

From source file:com.crazytest.config.TestCase.java

protected void postWithoutToken(String endpoint, List<NameValuePair> params) throws Exception {
    String endpointString = hostUrl + endpoint;

    URL url = new URL(endpointString);
    URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(),
            url.getQuery(), url.getRef());

    HttpPost postRequest = new HttpPost(uri);
    postRequest.setEntity(new UrlEncodedFormEntity(params));

    postRequest.setHeader("Content-Type", "application/x-www-form-urlencoded");
    postRequest.setHeader("User-Agent", "Safari");
    this.postRequest = postRequest;
    LOGGER.info("request: {}", postRequest.getRequestLine());

}

From source file:org.fao.geonet.utils.AbstractHttpRequest.java

public void setUrl(URL url) {
    host = url.getHost();/*www .  ja  v  a 2 s.  c o m*/
    port = url.getPort();
    protocol = url.getProtocol();
    address = url.getPath();
    query = url.getQuery();
}

From source file:com.iflytek.spider.protocol.httpclient.Http.java

/**
 * If credentials for the authentication scope determined from the specified
 * <code>url</code> is not already set in the HTTP client, then this method
 * sets the default credentials to fetch the specified <code>url</code>. If
 * credentials are found for the authentication scope, the method returns
 * without altering the client./*from  w  ww.ja  va 2  s .  c om*/
 * 
 * @param url
 *            URL to be fetched
 */
private void resolveCredentials(URL url) {

    if (defaultUsername != null && defaultUsername.length() > 0) {

        int port = url.getPort();
        if (port == -1) {
            if ("https".equals(url.getProtocol()))
                port = 443;
            else
                port = 80;
        }

        AuthScope scope = new AuthScope(url.getHost(), port);

        if (client.getState().getCredentials(scope) != null) {
            if (LOG.isTraceEnabled())
                LOG.trace("Pre-configured credentials with scope - host: " + url.getHost() + "; port: " + port
                        + "; found for url: " + url);

            // Credentials are already configured, so do nothing and return
            return;
        }

        if (LOG.isTraceEnabled())
            LOG.trace("Pre-configured credentials with scope -  host: " + url.getHost() + "; port: " + port
                    + "; not found for url: " + url);

        AuthScope serverAuthScope = getAuthScope(url.getHost(), port, defaultRealm, defaultScheme);

        NTCredentials serverCredentials = new NTCredentials(defaultUsername, defaultPassword, agentHost,
                defaultRealm);

        client.getState().setCredentials(serverAuthScope, serverCredentials);
    }
}

From source file:com.crazytest.config.TestCase.java

protected void post(String endpoint, List<NameValuePair> params) throws Exception {
    String endpointString = hostUrl + endpoint + "?auth-key=" + this.authToken + "&token=" + this.authToken
            + "&userID=" + this.userID;

    URL url = new URL(endpointString);
    URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(),
            url.getQuery(), url.getRef());

    HttpPost postRequest = new HttpPost(uri);
    postRequest.setEntity(new UrlEncodedFormEntity(params));

    postRequest.setHeader("Content-Type", "application/x-www-form-urlencoded");
    postRequest.setHeader("User-Agent", "Safari");
    this.postRequest = postRequest;
    LOGGER.info("request: {}", postRequest.getRequestLine());

}

From source file:com.crazytest.config.TestCase.java

protected void postWithCredential(String endpoint, String params)
        throws UnsupportedEncodingException, URISyntaxException, MalformedURLException {
    String endpointString = hostUrl + endpoint + "?auth-key=" + this.authToken + "&token=" + this.authToken
            + "&userID=" + this.userID + params;

    URL url = new URL(endpointString);
    URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(),
            url.getQuery(), url.getRef());

    HttpPost postRequest = new HttpPost(uri);

    postRequest.setHeader("Content-Type", "application/x-www-form-urlencoded");
    postRequest.setHeader("User-Agent", "Safari");
    this.postRequest = postRequest;

    LOGGER.info("request: {}", postRequest.getRequestLine());
    LOGGER.info("request: {}", postRequest.getParams().getParameter("licenseeID"));
}

From source file:com.crazytest.config.TestCase.java

protected void putWithCredential(String endpoint, List<NameValuePair> params)
        throws MalformedURLException, URISyntaxException, UnsupportedEncodingException {
    String endpointString = hostUrl + endpoint + "?auth-key=" + this.authToken + "&token=" + this.authToken
            + "&userID=" + this.userID;

    URL url = new URL(endpointString);
    URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(),
            url.getQuery(), url.getRef());

    HttpPut putRequest = new HttpPut(uri);
    putRequest.setEntity(new UrlEncodedFormEntity(params));

    putRequest.setHeader("Content-Type", "application/x-www-form-urlencoded");
    putRequest.setHeader("User-Agent", "Safari");
    this.putRequest = putRequest;

    LOGGER.info("request: {}", putRequest.getRequestLine());
}