Example usage for java.net URI getUserInfo

List of usage examples for java.net URI getUserInfo

Introduction

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

Prototype

public String getUserInfo() 

Source Link

Document

Returns the decoded user-information component of this URI.

Usage

From source file:org.jenkinsci.plugins.github_branch_source.GitHubConfiguration.java

/**
 * Fix an apiUri.//from ww w  .java2s.  c  om
 *
 * @param apiUri the api URI.
 * @return the normalized api URI.
 */
@CheckForNull
public static String normalizeApiUri(@CheckForNull String apiUri) {
    if (apiUri == null) {
        return null;
    }
    try {
        URI uri = new URI(apiUri).normalize();
        String scheme = uri.getScheme();
        if ("http".equals(scheme) || "https".equals(scheme)) {
            // we only expect http / https, but also these are the only ones where we know the authority
            // is server based, i.e. [userinfo@]server[:port]
            // DNS names must be US-ASCII and are case insensitive, so we force all to lowercase

            String host = uri.getHost() == null ? null : uri.getHost().toLowerCase(Locale.ENGLISH);
            int port = uri.getPort();
            if ("http".equals(scheme) && port == 80) {
                port = -1;
            } else if ("https".equals(scheme) && port == 443) {
                port = -1;
            }
            apiUri = new URI(scheme, uri.getUserInfo(), host, port, uri.getPath(), uri.getQuery(),
                    uri.getFragment()).toASCIIString();
        }
    } catch (URISyntaxException e) {
        // ignore, this was a best effort tidy-up
    }
    return apiUri.replaceAll("/$", "");
}

From source file:com.ctriposs.r2.message.rest.QueryTunnelUtil.java

/**
 * @param request   a RestRequest object to be encoded as a tunneled POST
 * @param threshold the size of the query params above which the request will be encoded
 *
 * @return an encoded RestRequest/*from  ww w.java 2s. co  m*/
 */
public static RestRequest encode(final RestRequest request, int threshold)
        throws URISyntaxException, MessagingException, IOException {
    URI uri = request.getURI();

    // Check to see if we should tunnel this request by testing the length of the query
    // if the query is NULL, we won't bother to encode.
    // 0 length is a special case that could occur with a url like http://www.foo.com?
    // which we don't want to encode, because we'll lose the "?" in the process
    // Otherwise only encode queries whose length is greater than or equal to the
    // threshold value.

    String query = uri.getRawQuery();

    if (query == null || query.length() == 0 || query.length() < threshold) {
        return request;
    }

    RestRequestBuilder requestBuilder = new RestRequestBuilder(request);

    // reconstruct URI without query
    uri = new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), uri.getPath(), null,
            uri.getFragment());

    // If there's no existing body, just pass the request as x-www-form-urlencoded
    ByteString entity = request.getEntity();
    if (entity == null || entity.length() == 0) {
        requestBuilder.setHeader(HEADER_CONTENT_TYPE, FORM_URL_ENCODED);
        requestBuilder.setEntity(ByteString.copyString(query, Data.UTF_8_CHARSET));
    } else {
        // If we have a body, we must preserve it, so use multipart/mixed encoding

        MimeMultipart multi = createMultiPartEntity(entity, request.getHeader(HEADER_CONTENT_TYPE), query);
        requestBuilder.setHeader(HEADER_CONTENT_TYPE, multi.getContentType());
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        multi.writeTo(os);
        requestBuilder.setEntity(ByteString.copy(os.toByteArray()));
    }

    // Set the base uri, supply the original method in the override header, and change method to POST
    requestBuilder.setURI(uri);
    requestBuilder.setHeader(HEADER_METHOD_OVERRIDE, requestBuilder.getMethod());
    requestBuilder.setMethod(RestMethod.POST);

    return requestBuilder.build();
}

From source file:f1db.configuration.ProductionProfile.java

@Bean
public BasicDataSource dataSource() throws URISyntaxException {
    URI dbUri = new URI(System.getenv("DATABASE_URL"));
    String username = dbUri.getUserInfo().split(":")[0];
    String password = dbUri.getUserInfo().split(":")[1];
    String dbUrl = "jdbc:postgresql://" + dbUri.getHost() + ':' + dbUri.getPort() + dbUri.getPath();
    BasicDataSource basicDataSource = new BasicDataSource();
    basicDataSource.setUrl(dbUrl);/*  w  ww  . j a  va 2s.c  o m*/
    basicDataSource.setUsername(username);
    basicDataSource.setPassword(password);
    return basicDataSource;
}

From source file:bibibi.configs.ProductionProfile.java

@Bean
public BasicDataSource dataSource() throws URISyntaxException {
    URI dbUri = new URI(System.getenv("DATABASE_URL"));

    String username = dbUri.getUserInfo().split(":")[0];
    String password = dbUri.getUserInfo().split(":")[1];
    String dbUrl = "jdbc:postgresql://" + dbUri.getHost() + ':' + dbUri.getPort() + dbUri.getPath();

    BasicDataSource basicDataSource = new BasicDataSource();
    basicDataSource.setUrl(dbUrl);//from w  w w . j av  a 2s .c  o  m
    basicDataSource.setUsername(username);
    basicDataSource.setPassword(password);

    return basicDataSource;
}

From source file:com.facebook.presto.hive.s3.PrestoS3FileSystem.java

/**
 * Helper function used to work around the fact that if you use an S3 bucket with an '_' that java.net.URI
 * behaves differently and sets the host value to null whereas S3 buckets without '_' have a properly
 * set host field. '_' is only allowed in S3 bucket names in us-east-1.
 *
 * @param uri The URI from which to extract a host value.
 * @return The host value where uri.getAuthority() is used when uri.getHost() returns null as long as no UserInfo is present.
 * @throws IllegalArgumentException If the bucket can not be determined from the URI.
 *//*  w w  w  .j a va2s. c  o  m*/
public static String getBucketName(URI uri) {
    if (uri.getHost() != null) {
        return uri.getHost();
    }

    if (uri.getUserInfo() == null) {
        return uri.getAuthority();
    }

    throw new IllegalArgumentException("Unable to determine S3 bucket from URI.");
}

From source file:net.sf.ufsc.http.HttpSession.java

/**
 * @param uri/*  ww  w.  j  av  a  2s  .  com*/
 */
public HttpSession(URI uri) {
    super(uri);

    if (uri.getUserInfo() != null) {
        UserInfo userInfo = new UserInfo(uri);

        this.client.getState().setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials(userInfo.getUser(), userInfo.getPassword()));
    }
}

From source file:com.facebook.presto.hive.PrestoS3FileSystem.java

private static Optional<AWSCredentials> getAwsCredentials(URI uri, Configuration conf) {
    String accessKey = conf.get(S3_ACCESS_KEY);
    String secretKey = conf.get(S3_SECRET_KEY);

    String userInfo = uri.getUserInfo();
    if (userInfo != null) {
        int index = userInfo.indexOf(':');
        if (index < 0) {
            accessKey = userInfo;/* w w  w.ja v  a2 s  .  co m*/
        } else {
            accessKey = userInfo.substring(0, index);
            secretKey = userInfo.substring(index + 1);
        }
    }

    if (isNullOrEmpty(accessKey) || isNullOrEmpty(secretKey)) {
        return Optional.empty();
    }
    return Optional.of(new BasicAWSCredentials(accessKey, secretKey));
}

From source file:com.collective.celos.ci.deploy.JScpWorker.java

URI getURIRespectingUsername(URI uri) throws URISyntaxException {

    if (userName != null && uri.getUserInfo() == null) {
        uri = new URI(uri.getScheme(), userName, uri.getHost(), uri.getPort(), uri.getPath(), uri.getQuery(),
                uri.getFragment());/*from  w  ww  . ja  v a 2s .c o  m*/
    }
    return uri;
}

From source file:edu.infsci2560.DatabaseConfig.java

@Bean
public BasicDataSource dataSource() throws URISyntaxException {
    URI dbUri = new URI(System.getenv("DATABASE_URL"));

    String username = dbUri.getUserInfo().split(":")[0];
    String password = dbUri.getUserInfo().split(":")[1];
    String dbUrl = "jdbc:postgresql://" + dbUri.getHost() + ':' + dbUri.getPort() + dbUri.getPath()
            + "?sslmode=require";

    BasicDataSource basicDataSource = new BasicDataSource();
    basicDataSource.setUrl(dbUrl);/*from   ww w  .j  a  va  2 s  .c  o m*/
    basicDataSource.setUsername(username);
    basicDataSource.setPassword(password);

    return basicDataSource;
}

From source file:com.cloudbees.jenkins.plugins.bitbucket.endpoints.BitbucketEndpointConfiguration.java

/**
 * Fix a serverUrl.//from   w  w  w .  j  av a  2 s  .  co m
 *
 * @param serverUrl the server URL.
 * @return the normalized server URL.
 */
@NonNull
public static String normalizeServerUrl(@CheckForNull String serverUrl) {
    serverUrl = StringUtils.defaultIfBlank(serverUrl, BitbucketCloudEndpoint.SERVER_URL);
    try {
        URI uri = new URI(serverUrl).normalize();
        String scheme = uri.getScheme();
        if ("http".equals(scheme) || "https".equals(scheme)) {
            // we only expect http / https, but also these are the only ones where we know the authority
            // is server based, i.e. [userinfo@]server[:port]
            // DNS names must be US-ASCII and are case insensitive, so we force all to lowercase

            String host = uri.getHost() == null ? null : uri.getHost().toLowerCase(Locale.ENGLISH);
            int port = uri.getPort();
            if ("http".equals(scheme) && port == 80) {
                port = -1;
            } else if ("https".equals(scheme) && port == 443) {
                port = -1;
            }
            serverUrl = new URI(scheme, uri.getUserInfo(), host, port, uri.getPath(), uri.getQuery(),
                    uri.getFragment()).toASCIIString();
        }
    } catch (URISyntaxException e) {
        // ignore, this was a best effort tidy-up
    }
    return serverUrl.replaceAll("/$", "");
}