Example usage for java.net URI getRawUserInfo

List of usage examples for java.net URI getRawUserInfo

Introduction

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

Prototype

public String getRawUserInfo() 

Source Link

Document

Returns the raw user-information component of this URI.

Usage

From source file:com.mcxiaoke.next.http.util.URIBuilder.java

private void digestURI(final URI uri) {
    this.scheme = uri.getScheme();
    this.encodedSchemeSpecificPart = uri.getRawSchemeSpecificPart();
    this.encodedAuthority = uri.getRawAuthority();
    this.host = uri.getHost();
    this.port = uri.getPort();
    this.encodedUserInfo = uri.getRawUserInfo();
    this.userInfo = uri.getUserInfo();
    this.encodedPath = uri.getRawPath();
    this.path = uri.getPath();
    this.encodedQuery = uri.getRawQuery();
    this.queryParams = parseQuery(uri.getRawQuery(), Charsets.UTF_8);
    this.encodedFragment = uri.getRawFragment();
    this.fragment = uri.getFragment();
}

From source file:brooklyn.util.ResourceUtils.java

private InputStream getResourceViaHttp(String resource) throws IOException {
    URI uri = URI.create(resource);
    HttpClientBuilder builder = HttpTool.httpClientBuilder().laxRedirect(true).uri(uri);
    Credentials credentials = getUrlCredentials(uri.getRawUserInfo());
    if (credentials != null) {
        builder.credentials(credentials);
    }//from   www  .j  ava2 s  .  com
    HttpClient client = builder.build();
    HttpResponse result = client.execute(new HttpGet(resource));
    int statusCode = result.getStatusLine().getStatusCode();
    if (HttpTool.isStatusCodeHealthy(statusCode)) {
        HttpEntity entity = result.getEntity();
        if (entity != null) {
            return entity.getContent();
        } else {
            return new ByteArrayInputStream(new byte[0]);
        }
    } else {
        EntityUtils.consume(result.getEntity());
        throw new IllegalStateException(
                "Invalid response invoking " + resource + ": response code " + statusCode);
    }
}

From source file:io.curly.artifact.web.remote.PaperclipLinkCatcher.java

private String reconstructURI(String host, String href) {
    URI original;
    try {//from   ww  w . j av  a 2s  .  c o m
        original = new URI(href);
    } catch (URISyntaxException e) {
        throw new IllegalArgumentException("Cannot create URI from: " + href);
    }
    int port = 80;
    if ("https".equals(original.getScheme())) {
        port = 443;
    }
    if (host.contains(":")) {
        String[] pair = host.split(":");
        host = pair[0];
        port = Integer.valueOf(pair[1]);
    }
    if (host.equals(original.getHost()) && port == original.getPort()) {
        return href;
    }
    String scheme = original.getScheme();
    if (scheme == null) {
        scheme = port == 443 ? "https" : "http";
    }

    StringBuilder sb = new StringBuilder();
    sb.append(scheme).append("://");
    if (StringUtils.hasText(original.getRawUserInfo())) {
        sb.append(original.getRawUserInfo()).append("@");
    }
    sb.append(host);
    if (port >= 0) {
        sb.append(":").append(port);
    }
    sb.append(original.getPath());
    if (StringUtils.hasText(original.getRawQuery())) {
        sb.append("?").append(original.getRawQuery());
    }
    if (StringUtils.hasText(original.getRawFragment())) {
        sb.append("#").append(original.getRawFragment());
    }

    return sb.toString();
}

From source file:io.curly.commons.web.hateoas.ZuulAwareMappingResolver.java

/**
 * <a href="https://github.com/spring-cloud-samples/customers-stores/blob/master/rest-microservices-customers/src/main/java/example/customers/integration/StoreIntegration.java#L89">Github Spring Cloud Sample implementation<a/>
 *
 * @param host the current request host/*from   w  ww  .  ja  va  2  s.  c o  m*/
 * @param href the #resolve builder returns
 * @return reconstructed URI based on the current request host and port comparing them with the service host and port
 */
public String reconstructURI(String host, String href) {
    URI original;
    try {
        original = new URI(href);
    } catch (URISyntaxException e) {
        throw new IllegalArgumentException("Cannot create URI from: " + href);
    }
    int port = 80;
    if ("https".equals(original.getScheme())) {
        port = 443;
    }
    if (host.contains(":")) {
        String[] pair = host.split(":");
        host = pair[0];
        port = Integer.valueOf(pair[1]);
    }
    if (host.equals(original.getHost()) && port == original.getPort()) {
        return href;
    }
    String scheme = original.getScheme();
    if (scheme == null) {
        scheme = port == 443 ? "https" : "http";
    }

    StringBuilder sb = new StringBuilder();
    sb.append(scheme).append("://");
    if (StringUtils.hasText(original.getRawUserInfo())) {
        sb.append(original.getRawUserInfo()).append("@");
    }
    sb.append(host);
    if (port >= 0) {
        sb.append(":").append(port);
    }
    sb.append(original.getPath());
    if (StringUtils.hasText(original.getRawQuery())) {
        sb.append("?").append(original.getRawQuery());
    }
    if (StringUtils.hasText(original.getRawFragment())) {
        sb.append("#").append(original.getRawFragment());
    }

    return sb.toString();
}

From source file:com.microsoft.gittf.client.clc.connection.GitTFHTTPClientFactory.java

private CLCHTTPProxyConfiguration configureClientProxyFromEnvironment(final HttpClient httpClient,
        final HostConfiguration hostConfiguration, final HttpState httpState,
        final ConnectionInstanceData connectionInstanceData) {
    String proxyUrl = null;//from  w  w  w .  j  a  va 2  s . c  om
    String nonProxyHosts;

    /*
     * If we're doing HTTPS, check for the presence of an HTTPS proxy
     * environment variable.
     */
    if ("https".equalsIgnoreCase(connectionInstanceData.getServerURI().getScheme())) //$NON-NLS-1$
    {
        proxyUrl = PlatformMiscUtils.getInstance().getEnvironmentVariable(EnvironmentVariables.HTTPS_PROXY_URL);

        if (proxyUrl == null || proxyUrl.length() == 0) {
            proxyUrl = PlatformMiscUtils.getInstance()
                    .getEnvironmentVariable(EnvironmentVariables.HTTPS_PROXY_URL_ALTERNATE);
        }
    }

    /*
     * Check for the presence of an HTTP proxy environment variable and use
     * that as the global proxy. (lynx documented the environment variable
     * as lower case "http_proxy", so we need to check both the variable and
     * its alternate.)
     * 
     * (Note, we have always tried using the HTTP_PROXY environment variable
     * for HTTPS connections, so continue to support this.)
     */
    if (proxyUrl == null || proxyUrl.length() == 0) {
        proxyUrl = PlatformMiscUtils.getInstance().getEnvironmentVariable(EnvironmentVariables.HTTP_PROXY_URL);

        if (proxyUrl == null || proxyUrl.length() == 0) {
            proxyUrl = PlatformMiscUtils.getInstance()
                    .getEnvironmentVariable(EnvironmentVariables.HTTP_PROXY_URL_ALTERNATE);
        }
    }

    if (proxyUrl == null || proxyUrl.length() == 0) {
        return null;
    }

    /*
     * Check against the NO_PROXY environment variable. (lynx also
     * documented "no_proxy" as lower case here, so we need to check both
     * the variable and its alternate.)
     */
    nonProxyHosts = PlatformMiscUtils.getInstance().getEnvironmentVariable(EnvironmentVariables.NO_PROXY_HOSTS);

    if (nonProxyHosts == null || nonProxyHosts.length() == 0) {
        nonProxyHosts = PlatformMiscUtils.getInstance()
                .getEnvironmentVariable(EnvironmentVariables.NO_PROXY_HOSTS_ALTERNATE);
    }

    if (hostExcludedFromProxyEnvironment(connectionInstanceData.getServerURI(), nonProxyHosts)) {
        return null;
    }

    URI proxyURI;

    try {
        proxyURI = new URI(proxyUrl);
    } catch (URISyntaxException e) {
        final String messageFormat = Messages.getString("GitTFHTTPClientFactory.IllegalProxyURLFormat"); //$NON-NLS-1$
        final String message = MessageFormat.format(messageFormat, proxyUrl);
        log.warn(message, e);
        throw new IllegalConfigurationException(message, e);
    }

    if (proxyURI.getHost() == null) {
        final String messageFormat = Messages.getString("GitTFHTTPClientFactory.IllegalProxyURLFormat"); //$NON-NLS-1$
        final String message = MessageFormat.format(messageFormat, proxyUrl);
        log.warn(message);
        throw new IllegalConfigurationException(message);
    }

    String username = null, password = null;
    if (proxyURI.getRawUserInfo() != null) {
        String[] userInfo = proxyURI.getRawUserInfo().split(":", 2); //$NON-NLS-1$

        try {
            username = URLDecoder.decode(userInfo[0], "UTF-8"); //$NON-NLS-1$
            password = URLDecoder.decode(userInfo[1], "UTF-8"); //$NON-NLS-1$
        } catch (Exception e) {
            log.warn("Could not decode user info as UTF-8", e); //$NON-NLS-1$
        }
    } else {
        /*
         * If the proxy credentials were NOT specified in the URI itself,
         * look up the credentials
         */
        CachedCredentials proxyCredentials = credentialsManager.getCredentials(proxyURI);

        username = proxyCredentials != null ? proxyCredentials.getUsername() : null;
        password = proxyCredentials != null ? proxyCredentials.getPassword() : null;
    }

    return new CLCHTTPProxyConfiguration(proxyURI.getHost(), proxyURI.getPort(), username, password);
}

From source file:com.microsoft.tfs.client.clc.CLCHTTPClientFactory.java

private CLCHTTPProxyConfiguration configureClientProxyFromEnvironment(final HttpClient httpClient,
        final HostConfiguration hostConfiguration, final HttpState httpState,
        final ConnectionInstanceData connectionInstanceData) {
    String proxyUrl = null;//ww w . j av  a2  s  .  co  m
    String nonProxyHosts;

    log.debug("Trying to configure proxy from environment variables:"); //$NON-NLS-1$

    /*
     * If we're doing HTTPS, check for the presence of an HTTPS proxy
     * environment variable.
     */
    if ("https".equalsIgnoreCase(connectionInstanceData.getServerURI().getScheme())) //$NON-NLS-1$
    {
        proxyUrl = PlatformMiscUtils.getInstance().getEnvironmentVariable(EnvironmentVariables.HTTPS_PROXY_URL);
        logIfDefined(EnvironmentVariables.HTTPS_PROXY_URL, proxyUrl);

        if (StringUtil.isNullOrEmpty(proxyUrl)) {
            proxyUrl = PlatformMiscUtils.getInstance()
                    .getEnvironmentVariable(EnvironmentVariables.HTTPS_PROXY_URL_ALTERNATE);
            logIfDefined(EnvironmentVariables.HTTPS_PROXY_URL_ALTERNATE, proxyUrl);
        }
    }

    /*
     * Check for the presence of an HTTP proxy environment variable and use
     * that as the global proxy. (lynx documented the environment variable
     * as lower case "http_proxy", so we need to check both the variable and
     * its alternate.)
     *
     * (Note, we have always tried using the HTTP_PROXY environment variable
     * for HTTPS connections, so continue to support this.)
     */
    if (StringUtil.isNullOrEmpty(proxyUrl)) {
        proxyUrl = PlatformMiscUtils.getInstance().getEnvironmentVariable(EnvironmentVariables.HTTP_PROXY_URL);
        logIfDefined(EnvironmentVariables.HTTP_PROXY_URL, proxyUrl);

        if (StringUtil.isNullOrEmpty(proxyUrl)) {
            proxyUrl = PlatformMiscUtils.getInstance()
                    .getEnvironmentVariable(EnvironmentVariables.HTTP_PROXY_URL_ALTERNATE);
            logIfDefined(EnvironmentVariables.HTTP_PROXY_URL_ALTERNATE, proxyUrl);
        }
    }

    if (StringUtil.isNullOrEmpty(proxyUrl)) {
        log.debug("    proxy is not defined in environment variables"); //$NON-NLS-1$
        return null;
    }

    /*
     * Check against the NO_PROXY environment variable. (lynx also
     * documented "no_proxy" as lower case here, so we need to check both
     * the variable and its alternate.)
     */
    nonProxyHosts = PlatformMiscUtils.getInstance().getEnvironmentVariable(EnvironmentVariables.NO_PROXY_HOSTS);
    logIfDefined(EnvironmentVariables.NO_PROXY_HOSTS, nonProxyHosts);

    if (StringUtil.isNullOrEmpty(nonProxyHosts)) {
        nonProxyHosts = PlatformMiscUtils.getInstance()
                .getEnvironmentVariable(EnvironmentVariables.NO_PROXY_HOSTS_ALTERNATE);
        logIfDefined(EnvironmentVariables.NO_PROXY_HOSTS_ALTERNATE, nonProxyHosts);
    }

    if (hostExcludedFromProxyEnvironment(connectionInstanceData.getServerURI(), nonProxyHosts)) {
        log.debug("    proxy is defined, but excluded in environment variables"); //$NON-NLS-1$
        return null;
    }

    URI proxyURI;

    try {
        proxyURI = new URI(proxyUrl);
    } catch (final URISyntaxException e) {
        final String messageFormat = Messages.getString("CLCHttpClientFactory.IllegalProxyURLFormat"); //$NON-NLS-1$
        final String message = MessageFormat.format(messageFormat, proxyUrl);
        log.warn(message, e);
        throw new IllegalConfigurationException(message, e);
    }

    if (proxyURI.getHost() == null) {
        final String messageFormat = Messages.getString("CLCHttpClientFactory.IllegalProxyURLFormat"); //$NON-NLS-1$
        final String message = MessageFormat.format(messageFormat, proxyUrl);
        log.warn(message);
        throw new IllegalConfigurationException(message);
    }

    String username = null, password = null;
    if (proxyURI.getRawUserInfo() != null) {
        final String[] userInfo = proxyURI.getRawUserInfo().split(":", 2); //$NON-NLS-1$

        try {
            username = URLDecoder.decode(userInfo[0], "UTF-8"); //$NON-NLS-1$
            password = URLDecoder.decode(userInfo[1], "UTF-8"); //$NON-NLS-1$
        } catch (final Exception e) {
            log.warn("Could not decode user info as UTF-8", e); //$NON-NLS-1$
        }
    } else {
        /*
         * If the proxy credentials were NOT specified in the URI itself,
         * look up the credentials
         */
        final CachedCredentials proxyCredentials = credentialsManager.getCredentials(proxyURI);

        username = proxyCredentials != null ? proxyCredentials.getUsername() : null;
        password = proxyCredentials != null ? proxyCredentials.getPassword() : null;
    }

    return new CLCHTTPProxyConfiguration(proxyURI.getHost(), proxyURI.getPort(), username, password);
}

From source file:com.groupon.odo.bmp.http.BrowserMobHttpClient.java

private URI makeUri(String url) throws URISyntaxException {
    // MOB-120: check for | character and change to correctly escaped %7C
    url = url.replace(" ", "%20");
    url = url.replace(">", "%3C");
    url = url.replace("<", "%3E");
    url = url.replace("#", "%23");
    url = url.replace("{", "%7B");
    url = url.replace("}", "%7D");
    url = url.replace("|", "%7C");
    url = url.replace("\\", "%5C");
    url = url.replace("^", "%5E");
    url = url.replace("~", "%7E");
    url = url.replace("[", "%5B");
    url = url.replace("]", "%5D");
    url = url.replace("`", "%60");
    url = url.replace("\"", "%22");

    URI uri = new URI(url);

    // are we using the default ports for http/https? if so, let's rewrite the URI to make sure the :80 or :443
    // is NOT included in the string form the URI. The reason we do this is that in HttpClient 4.0 the Host header
    // would include a value such as "yahoo.com:80" rather than "yahoo.com". Not sure why this happens but we don't
    // want it to, and rewriting the URI solves it
    if ((uri.getPort() == 80 && "http".equals(uri.getScheme()))
            || (uri.getPort() == 443 && "https".equals(uri.getScheme()))) {
        // we rewrite the URL with a StringBuilder (vs passing in the components of the URI) because if we were
        // to pass in these components using the URI's 7-arg constructor query parameters get double escaped (bad!)
        StringBuilder sb = new StringBuilder(uri.getScheme()).append("://");
        if (uri.getRawUserInfo() != null) {
            sb.append(uri.getRawUserInfo()).append("@");
        }/*  w  ww .ja  v  a  2s . co  m*/
        sb.append(uri.getHost());
        if (uri.getRawPath() != null) {
            sb.append(uri.getRawPath());
        }
        if (uri.getRawQuery() != null) {
            sb.append("?").append(uri.getRawQuery());
        }
        if (uri.getRawFragment() != null) {
            sb.append("#").append(uri.getRawFragment());
        }

        uri = new URI(sb.toString());
    }
    return uri;
}

From source file:org.echocat.jomon.net.service.UriBasedServicesManager.java

@Nonnull
protected URI toUri(@Nonnull URI original, @Nonnull InetSocketAddress address) {
    final String scheme = original.getScheme();
    final int port = address.getPort();
    final String userInfo = original.getRawUserInfo();
    final String path = original.getRawPath();
    final String query = original.getRawQuery();
    final String fragment = original.getRawFragment();
    final StringBuilder sb = new StringBuilder();
    sb.append(scheme).append("://");
    if (isNotEmpty(userInfo)) {
        sb.append(userInfo).append('@');
    }/*w  w  w .  ja v a2s  .c o m*/
    sb.append(address.getHostString());
    if (canAppendPort(scheme, port)) {
        sb.append(':').append(port);
    }
    if (isNotEmpty(path)) {
        sb.append(path);
    }
    if (isNotEmpty(query)) {
        sb.append('?').append(query);
    }
    if (isNotEmpty(fragment)) {
        sb.append('#').append(fragment);
    }
    return URI.create(sb.toString());
}

From source file:org.mule.endpoint.AbstractEndpoint.java

@Override
public String toString() {
    // Use the interface to retrieve the string and set
    // the endpoint uri to a default value
    String sanitizedEndPointUri = null;
    URI uri = null;
    if (endpointUri != null) {
        sanitizedEndPointUri = endpointUri.toString();
        uri = endpointUri.getUri();/*w  w  w. j  a va 2  s .  com*/
    }
    // The following will further sanitize the endpointuri by removing
    // the embedded password. This will only remove the password if the
    // uri contains all the necessary information to successfully rebuild the url
    if (uri != null && (uri.getRawUserInfo() != null) && (uri.getScheme() != null) && (uri.getHost() != null)
            && (uri.getRawPath() != null)) {
        // build a pattern up that matches what we need tp strip out the password
        Pattern sanitizerPattern = Pattern.compile("(.*):.*");
        Matcher sanitizerMatcher = sanitizerPattern.matcher(uri.getRawUserInfo());
        if (sanitizerMatcher.matches()) {
            sanitizedEndPointUri = new StringBuffer(uri.getScheme()).append("://")
                    .append(sanitizerMatcher.group(1)).append(":<password>").append("@").append(uri.getHost())
                    .append(uri.getRawPath()).toString();
        }
        if (uri.getRawQuery() != null) {
            sanitizedEndPointUri = sanitizedEndPointUri + "?" + uri.getRawQuery();
        }

    }

    return ClassUtils.getClassName(getClass()) + "{endpointUri=" + sanitizedEndPointUri + ", connector="
            + connector + ",  name='" + name + "', mep=" + messageExchangePattern + ", properties=" + properties
            + ", transactionConfig=" + transactionConfig + ", deleteUnacceptedMessages="
            + deleteUnacceptedMessages + ", initialState=" + initialState + ", responseTimeout="
            + responseTimeout + ", endpointEncoding=" + endpointEncoding + ", disableTransportTransformer="
            + disableTransportTransformer + "}";
}