Example usage for java.net URI getHost

List of usage examples for java.net URI getHost

Introduction

In this page you can find the example usage for java.net URI getHost.

Prototype

public String getHost() 

Source Link

Document

Returns the host component of this URI.

Usage

From source file:org.dvbviewer.controller.io.ServerRequest.java

/**
 * Gets the Recording service auth scope.
 * /*w  w  w .j ava  2s  . c o  m*/
 * @return the rs auth scope
 * @author RayBa
 * @date 13.04.2012
 */
private static AuthScope getRsAuthScope() {
    if (rsAuthScope == null) {
        if (urlValidator.isValid(ServerConsts.REC_SERVICE_URL)) {
            URI uri;
            try {
                uri = new URI(ServerConsts.REC_SERVICE_URL);
                rsAuthScope = new AuthScope(uri.getHost(), uri.getPort());
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
        }
    }
    return rsAuthScope;
}

From source file:org.dvbviewer.controller.io.ServerRequest.java

/**
 * Gets the client auth scope./* w w  w.j a v  a 2 s  .co  m*/
 * 
 * @return the client auth scope
 * @author RayBa
 * @date 13.04.2012
 */
private static AuthScope getClientAuthScope() {
    if (clientAuthScope == null) {
        if (urlValidator.isValid(ServerConsts.DVBVIEWER_URL)) {
            URI uri;
            try {
                uri = new URI(ServerConsts.DVBVIEWER_URL);
                clientAuthScope = new AuthScope(uri.getHost(), uri.getPort());
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
        }
    }
    return clientAuthScope;
}

From source file:org.fcrepo.server.utilities.ServerUtility.java

/**
 * Tell whether the given URL appears to be referring to somewhere within
 * the Fedora webapp.//from  w w  w.  j a v  a2 s. co m
 */
public static boolean isURLFedoraServer(String url) {

    // scheme must be http or https
    URI uri = URI.create(url);
    String scheme = uri.getScheme();
    if (!scheme.equals("http") && !scheme.equals("https")) {
        return false;
    }

    // host must be configured hostname or localhost
    String fHost = CONFIG.getParameter(FEDORA_SERVER_HOST, Parameter.class).getValue();
    String host = uri.getHost();
    if (!host.equals(fHost) && !host.equals("localhost")) {
        return false;
    }

    // path must begin with configured webapp context
    String path = uri.getPath();
    String fedoraContext = CONFIG.getParameter(FEDORA_SERVER_CONTEXT, Parameter.class).getValue();
    if (!path.startsWith("/" + fedoraContext + "/")) {
        return false;
    }

    // port specification must match http or https port as appropriate
    String httpPort = CONFIG.getParameter(FEDORA_SERVER_PORT, Parameter.class).getValue();
    String httpsPort = CONFIG.getParameter(FEDORA_REDIRECT_PORT, Parameter.class).getValue();
    if (uri.getPort() == -1) {
        // unspecified, so fedoraPort must be 80 (http), or 443 (https)
        if (scheme.equals("http")) {
            return httpPort.equals("80");
        } else {
            return httpsPort.equals("443");
        }
    } else {
        // specified, so must match appropriate http or https port
        String port = "" + uri.getPort();
        if (scheme.equals("http")) {
            return port.equals(httpPort);
        } else {
            return port.equals(httpsPort);
        }
    }

}

From source file:ee.ria.xroad.proxy.clientproxy.AuthTrustVerifier.java

private static void verifyAuthCert(ClientId serviceProvider, X509Certificate[] certs, URI address)
        throws Exception {
    CertChain chain;/*  w w  w  . j a  v a  2s.  c  om*/
    List<OCSPResp> ocspResponses;
    try {
        List<X509Certificate> additionalCerts = Arrays
                .asList((X509Certificate[]) ArrayUtils.subarray(certs, 1, certs.length));
        chain = CertChain.create(serviceProvider.getXRoadInstance(), certs[0], additionalCerts);
        ocspResponses = getOcspResponses(chain.getAllCertsWithoutTrustedRoot(), address.getHost());
    } catch (CodedException e) {
        throw e.withPrefix(X_SSL_AUTH_FAILED);
    }

    CertHelper.verifyAuthCert(chain, ocspResponses, serviceProvider);
}

From source file:com.sap.prd.mobile.ios.mios.SCMUtil.java

public static String getConnectionString(final Properties versionInfo,
        final boolean hideConfidentialInformation) throws IOException {

    final String type = versionInfo.getProperty("type");

    final StringBuilder connectionString = new StringBuilder(128);

    if (type != null && type.equals("git")) {

        final String repo = versionInfo.getProperty("repo");

        if (StringUtils.isBlank(repo)) {
            if (!StringUtils.isBlank(versionInfo.getProperty("repo_1"))) {
                throw new IllegalStateException(
                        "Multipe git repositories provided. This use case is not supported. Provide only one git repository.");
            }//from w  ww.  ja  v a 2s.  c om
            throw new IllegalStateException("No git repository provided. ");
        }

        if (hideConfidentialInformation) {
            try {
                URI uri = new URI(repo);
                int port = uri.getPort();
                if (port == -1) {
                    final String scheme = uri.getScheme();
                    if (scheme != null && gitDefaultPorts.containsKey(scheme)) {
                        port = gitDefaultPorts.get(scheme);
                    }
                }
                connectionString.append(port);

                if (uri.getHost() != null) {
                    connectionString.append(uri.getPath());
                }
            } catch (URISyntaxException e) {
                throw new IllegalStateException(String.format("Invalid repository uri: %s", repo));
            }
        } else {
            connectionString.append(PROTOCOL_PREFIX_GIT).append(repo);
        }

    } else {

        final String port = versionInfo.getProperty("port");

        if (StringUtils.isBlank(port))
            throw new IllegalStateException("No SCM port provided.");

        final String depotPath = versionInfo.getProperty("depotpath");

        if (hideConfidentialInformation) {
            try {
                URI perforceUri = new URI("perforce://" + port);

                int _port = perforceUri.getPort();
                if (_port == -1) {
                    _port = PERFORCE_DEFAULT_PORT;
                }
                connectionString.append(_port);
                connectionString.append(depotPath);
            } catch (URISyntaxException e) {
                throw new IllegalStateException(String.format("Invalid port: %s", port));
            }
        } else {

            if (StringUtils.isBlank(depotPath))
                throw new IllegalStateException("No depot path provided.");

            connectionString.append(PROTOCOL_PREFIX_PERFORCE).append(port).append(":")
                    .append(getDepotPath(depotPath));
        }
    }

    return connectionString.toString();
}

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  va  2  s. c  o  m
 * @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 www  . j av a  2 s  . 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:password.pwm.http.servlet.oauth.OAuthConsumerServlet.java

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

    {/*from   w ww .  jav  a2 s  .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;
}