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.amalto.workbench.utils.HttpClientUtil.java

private static HttpContext getPreemptiveContext(String url) {
    URI uri = URI.create(url);
    HttpHost targetHost = new HttpHost(uri.getHost(), uri.getPort());

    return getPreemptiveContext(targetHost);
}

From source file:com.amalto.workbench.utils.HttpClientUtil.java

private static DefaultHttpClient wrapAuthClient(String url, String username, String password)
        throws SecurityException {
    DefaultHttpClient client = createClient();
    URI uri = URI.create(url);
    AuthScope authScope = new AuthScope(uri.getHost(), uri.getPort());
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
    client.getCredentialsProvider().setCredentials(authScope, credentials);
    return client;
}

From source file:org.apache.jena.jdbc.remote.RemoteEndpointDriver.java

/**
 * Strips the last component of the given URI if possible
 * // w  w w.ja v a 2 s  .  c om
 * @param input
 *            URI
 * @return Reduced URI or null if no further reduction is possible
 */
private static String stripLastComponent(String input) {
    try {
        URI uri = new URI(input);
        if (uri.getFragment() != null) {
            return new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), uri.getPath(),
                    uri.getQuery(), null).toString();
        } else if (uri.getQuery() != null) {
            return new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), uri.getPath(),
                    null, null).toString();
        } else if (uri.getPath() != null) {
            // Try and strip off last segment of the path
            String currPath = uri.getPath();
            if (currPath.endsWith("/")) {
                currPath = currPath.substring(0, currPath.length() - 1);
                if (currPath.length() == 0)
                    currPath = null;
                return new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), currPath, null,
                        null).toString();
            } else if (currPath.contains("/")) {
                currPath = currPath.substring(0, currPath.lastIndexOf('/') + 1);
                if (currPath.length() == 0)
                    currPath = null;
                return new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), currPath, null,
                        null).toString();
            } else {
                // If path is non-null it must always contain a /
                // otherwise it would be an invalid path
                // In this case there are no further components to strip
                return null;
            }
        } else {
            // No further components to strip
            return null;
        }
    } catch (URISyntaxException e) {
        // Error stripping component
        return null;
    }
}

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

/**
 * Given the service endpoint in case of path-style URIs, path contains
 * container name and remaining part if they are present, this method
 * constructs the Full Uri./*from   ww w. j  a  v  a 2s. c  o  m*/
 * 
 * @param endPoint
 * @param path
 * @return Full URI
 */
private static URI constructUriFromUriAndString(URI endPoint, String path) {
    // This is where we encode the url path to be valid
    String encodedPath = Utilities.encode(path);
    if (!Utilities.isNullOrEmpty(encodedPath) && encodedPath.charAt(0) != ConstChars.Slash.charAt(0)) {
        encodedPath = ConstChars.Slash + encodedPath;
    }
    try {
        // @Note : rename blob with blank spaces in name
        return new URI(endPoint.getScheme(), null, endPoint.getHost(), endPoint.getPort(),
                encodedPath.replaceAll("%20", " "), endPoint.getQuery(), endPoint.getFragment());
        // return new URI(endPoint.getScheme(), endPoint.getHost(),
        // encodedPath, endPoint.getFragment());
    } catch (URISyntaxException e) {
        Logger.error("Can not new URI", e);
        return null;
    }
}

From source file:org.aludratest.cloud.selenium.impl.SeleniumHttpProxy.java

public static SeleniumHttpProxy create(int id, SeleniumResourceImpl resource, String prefix, long timeout,
        long maxIdleTime, String accessUrl, ScheduledExecutorService healthCheckExecutor) {
    URI oUri = URI.create(resource.getOriginalUrl());
    SeleniumHttpProxy proxy = new SeleniumHttpProxy(oUri.getScheme(), prefix, oUri.getHost(), oUri.getPort(),
            oUri.getPath());//ww  w  . j  av  a 2 s .com
    proxy.id = id;
    proxy.resource = resource;
    proxy.timeout = timeout;
    proxy.accessUrl = accessUrl;
    proxy.maxIdleTime = maxIdleTime;
    proxy.healthCheckClient = createHealthCheckHttpClient();
    proxy.healthCheckExecutor = healthCheckExecutor;

    // set resource to DISCONNECTED first
    resource.setState(ResourceState.DISCONNECTED);
    proxy.nextHealthCheck = healthCheckExecutor.schedule(proxy.checkStatusRunnable, 2000,
            TimeUnit.MILLISECONDS);

    return proxy;
}

From source file:com.gamesalutes.utils.WebUtils.java

/**
 * Adds the query parameters to the uri <code>path</code>.
 * /*  www .ja  va 2  s .co  m*/
 * @param path the uri
 * @param parameters the query parameters to set for the uri
 * @return <code>path</code> with the query parameters added
 */
public static URI setQueryParameters(URI path, Map<String, String> parameters) {
    try {
        return new URI(path.getScheme(), path.getRawUserInfo(), path.getHost(), path.getPort(),
                path.getRawPath(), urlEncode(parameters, false), path.getRawFragment());
    } catch (URISyntaxException e) {
        // shouldn't happen
        throw new AssertionError(e);
    }

}

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

public static String figureOauthSelfEndPointUrl(final PwmRequest pwmRequest) {
    final String inputURI, debugSource;

    {/*from w  ww . ja va2s .c  o  m*/
        final String returnUrlOverride = pwmRequest.getConfig()
                .readAppProperty(AppProperty.OAUTH_RETURN_URL_OVERRIDE);
        final String siteURL = pwmRequest.getConfig().readSettingAsString(PwmSetting.PWM_SITE_URL);
        if (returnUrlOverride != null && !returnUrlOverride.trim().isEmpty()) {
            inputURI = returnUrlOverride;
            debugSource = "AppProperty(\"" + AppProperty.OAUTH_RETURN_URL_OVERRIDE.getKey() + "\")";
        } else if (siteURL != null && !siteURL.trim().isEmpty()) {
            inputURI = siteURL;
            debugSource = "SiteURL Setting";
        } else {
            debugSource = "Input Request URL";
            inputURI = pwmRequest.getHttpServletRequest().getRequestURL().toString();
        }
    }

    final String redirect_uri;
    try {
        final URI requestUri = new URI(inputURI);
        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());
    }
    LOGGER.trace("calculated oauth self end point URI as '" + redirect_uri + "' using method " + debugSource);
    return redirect_uri;
}

From source file:com.twitter.elephanttwin.util.HdfsUtils.java

/**
 * Returns the namenode from a fully-qualified HDFS path.
 *
 * @param hdfsPath fully-qualified HDFS path
 * @return the namenode or {@code null} if invalid HDFS path
 *//* w  w  w  .j  a  v  a 2 s .co  m*/
public static String getHdfsFileSystemName(@Nullable String hdfsPath) {
    if (hdfsPath == null) {
        return null;
    }
    if (!hdfsPath.startsWith("hdfs://")) {
        return null;
    }

    URI uri;
    try {
        uri = new URI(hdfsPath);
    } catch (URISyntaxException e) {
        return null;
    }

    if (uri.getPort() == -1) {
        return "hdfs://" + uri.getHost();
    }
    return "hdfs://" + uri.getHost() + ":" + uri.getPort();
}

From source file:hsyndicate.hadoop.dfs.HSyndicateDFS.java

private static SyndicateFileSystem createHSyndicateFS(URI uri, Configuration conf) throws IOException {
    if (uri == null) {
        throw new IllegalArgumentException("uri is null");
    }/* w w w. ja v a 2 s . c  om*/

    SyndicateFSConfiguration sconf = null;
    String ugHost = "";
    if (uri.getHost() != null && !uri.getHost().isEmpty()) {
        ugHost = uri.getHost();

        if (uri.getPort() > 0) {
            ugHost = ugHost + ":" + uri.getPort();
        }

        sconf = HSyndicateConfigUtils.createSyndicateConf(conf, ugHost);
    } else {
        sconf = HSyndicateConfigUtils.createSyndicateConf(conf);
    }

    try {
        return new SyndicateFileSystem(sconf, conf);
    } catch (InstantiationException ex) {
        throw new IOException(ex.getCause());
    }
}

From source file:org.bibalex.gallery.storage.BAGStorage.java

public static boolean putFile(String remoteUrlStr, File localFile, String contentType, int timeout)
        throws BAGException {
    HttpPut httpput = null;//  w  w  w .  j a  va2s .c o  m
    DefaultHttpClient httpclient = null;
    try {
        BasicHttpParams httpParams = new BasicHttpParams();

        HttpConnectionParams.setConnectionTimeout(httpParams, timeout);

        httpclient = new DefaultHttpClient(httpParams);
        URI remoteUrl = new URI(remoteUrlStr);
        String userInfo = remoteUrl.getUserInfo();
        if ((userInfo != null) && !userInfo.isEmpty()) {
            int colonIx = userInfo.indexOf(':');
            httpclient.getCredentialsProvider().setCredentials(
                    new AuthScope(remoteUrl.getHost(), remoteUrl.getPort()), new UsernamePasswordCredentials(
                            userInfo.substring(0, colonIx), userInfo.substring(colonIx + 1)));
        }

        if ((contentType == null) || contentType.isEmpty()) {
            contentType = "text/plain; charset=\"UTF-8\"";
        }

        FileEntity entity = new FileEntity(localFile, contentType);

        httpput = new HttpPut(remoteUrlStr);
        httpput.setEntity(entity);

        HttpResponse response = httpclient.execute(httpput);
        return response.getStatusLine().getStatusCode() - 200 < 100;

    } catch (IOException ex) {

        // In case of an IOException the connection will be released
        // back to the connection manager automatically
        throw new BAGException(ex);

    } catch (RuntimeException ex) {

        // In case of an unexpected exception you may want to abort
        // the HTTP request in order to shut down the underlying
        // connection and release it back to the connection manager.
        httpput.abort();
        throw ex;

    } catch (URISyntaxException e) {
        // will be still null: httpput.abort();
        throw new BAGException(e);
    } finally {

        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpclient.getConnectionManager().shutdown();

    }
}