Java Code Examples for java.net.URI.getUserInfo()

The following are Jave code examples for showing how to use getUserInfo() of the java.net.URI class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: hadoop-oss   File: HarFileSystem.java   View Source Code Vote up 6 votes
/**
 * Create a har specific auth 
 * har-underlyingfs:port
 * @param underLyingUri the uri of underlying
 * filesystem
 * @return har specific auth
 */
private String getHarAuth(URI underLyingUri) {
  String auth = underLyingUri.getScheme() + "-";
  if (underLyingUri.getHost() != null) {
    if (underLyingUri.getUserInfo() != null) {
      auth += underLyingUri.getUserInfo();
      auth += "@";
    }
    auth += underLyingUri.getHost();
    if (underLyingUri.getPort() != -1) {
      auth += ":";
      auth +=  underLyingUri.getPort();
    }
  }
  else {
    auth += ":";
  }
  return auth;
}
 
Example 2
Project: rawhttp   File: MethodLine.java   View Source Code Vote up 6 votes
public MethodLine withHost(String host) {
    try {
        if (!host.matches("[a-z]{1,6}://.*")) {
            host = "http://" + host;
        }
        URI hostURI = URI.create(host);
        URI newURI = new URI(hostURI.getScheme(),
                hostURI.getUserInfo(),
                hostURI.getHost(),
                hostURI.getPort(),
                uri.getPath(), uri.getQuery(), uri.getFragment());
        return new MethodLine(method, newURI, httpVersion);
    } catch (Exception e) {
        throw new IllegalArgumentException("Invalid host format" + Optional.ofNullable(
                e.getMessage()).map(s -> ": " + s).orElse(""));
    }
}
 
Example 3
Project: Elasticsearch   File: S3ClientHelper.java   View Source Code Vote up 6 votes
public AmazonS3 client(URI uri) throws IOException {
    String accessKey = null;
    String secretKey = null;
    if (uri.getHost() == null) {
        throw new IllegalArgumentException(INVALID_URI_MSG);
    }
    if (uri.getUserInfo() != null) {
        String[] userInfoParts = uri.getUserInfo().split(":");
        try {
            accessKey = userInfoParts[0];
            secretKey = userInfoParts[1];
        } catch (ArrayIndexOutOfBoundsException e) {
            // ignore
        }
    // if the URI contains '@' and ':', a UserInfo is in fact given, but could not
    // be parsed properly because the URI is not valid (e.g. not properly encoded).
    } else if (uri.toString().contains("@") && uri.toString().contains(":")) {
        throw new IllegalArgumentException(INVALID_URI_MSG);
    }
    return client(accessKey, secretKey);
}
 
Example 4
Project: x7   File: JedisURIHelper.java   View Source Code Vote up 5 votes
public static String getPassword(URI uri) {
  String userInfo = uri.getUserInfo();
  if (userInfo != null) {
    return userInfo.split(":", 2)[1];
  }
  return null;
}
 
Example 5
Project: black-mirror   File: SpeechService.java   View Source Code Vote up 5 votes
private URI removePort(URI uri) throws StatusException {
    try {
        return new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), -1 /* port */,
                uri.getPath(), uri.getQuery(), uri.getFragment());
    } catch (URISyntaxException e) {
        throw Status.UNAUTHENTICATED
                .withDescription("Unable to construct service URI after removing port")
                .withCause(e).asException();
    }
}
 
Example 6
Project: hadoop-oss   File: FTPFileSystem.java   View Source Code Vote up 5 votes
@Override
public void initialize(URI uri, Configuration conf) throws IOException { // get
  super.initialize(uri, conf);
  // get host information from uri (overrides info in conf)
  String host = uri.getHost();
  host = (host == null) ? conf.get(FS_FTP_HOST, null) : host;
  if (host == null) {
    throw new IOException("Invalid host specified");
  }
  conf.set(FS_FTP_HOST, host);

  // get port information from uri, (overrides info in conf)
  int port = uri.getPort();
  port = (port == -1) ? FTP.DEFAULT_PORT : port;
  conf.setInt("fs.ftp.host.port", port);

  // get user/password information from URI (overrides info in conf)
  String userAndPassword = uri.getUserInfo();
  if (userAndPassword == null) {
    userAndPassword = (conf.get("fs.ftp.user." + host, null) + ":" + conf
        .get("fs.ftp.password." + host, null));
  }
  String[] userPasswdInfo = userAndPassword.split(":");
  Preconditions.checkState(userPasswdInfo.length > 1,
                           "Invalid username / password");
  conf.set(FS_FTP_USER_PREFIX + host, userPasswdInfo[0]);
  conf.set(FS_FTP_PASSWORD_PREFIX + host, userPasswdInfo[1]);
  setConf(conf);
  this.uri = uri;
}
 
Example 7
Project: JRediClients   File: JedisURIHelper.java   View Source Code Vote up 5 votes
public static String getPassword(URI uri) {
	String userInfo = uri.getUserInfo();
	if (userInfo != null) {
		return userInfo.split(":", 2)[1];
	}
	return null;
}
 
Example 8
Project: xenon-utils   File: GatewayService.java   View Source Code Vote up 5 votes
private URI createNewUri(URI nodeAddress, URI opUri) {
    try {
        return new URI(
                nodeAddress.getScheme(), opUri.getUserInfo(), nodeAddress.getHost(),
                nodeAddress.getPort(), opUri.getPath(), opUri.getQuery(), opUri.getFragment());
    } catch (URISyntaxException x) {
        throw new IllegalArgumentException(x.getMessage(), x);
    }
}
 
Example 9
Project: APacheSynapseSimplePOC   File: JBoss.java   View Source Code Vote up 5 votes
public static void main ( String[] args ) {

        if ( args.length < 3 ) {
            System.err.println("Usage " + JBoss.class.getName() + " <uri> <payload> <payload_arg>");
            System.exit(-1);
        }

        URI u = URI.create(args[ 0 ]);

        final Object payloadObject = Utils.makePayloadObject(args[1], args[2]);

        String username = null;
        String password = null;
        if ( u.getUserInfo() != null ) {
            int sep = u.getUserInfo().indexOf(':');
            if ( sep >= 0 ) {
                username = u.getUserInfo().substring(0, sep);
                password = u.getUserInfo().substring(sep + 1);
            }
            else {
                System.err.println("Need <user>:<password>@");
                System.exit(-1);
            }
        }

        doRun(u, payloadObject, username, password);
        Utils.releasePayload(args[1], payloadObject);
    }
 
Example 10
Project: ysoserial-plus   File: JBoss.java   View Source Code Vote up 5 votes
public static void main ( String[] args ) {
    
    if ( args.length < 3 ) {
        System.err.println("Usage " + JBoss.class.getName() + " <uri> <payload> <payload_arg>");
        System.exit(-1);
    }

    URI u = URI.create(args[ 0 ]);

    final Object payloadObject = Utils.makePayloadObject(args[1], args[2]);
    
    String username = null;
    String password = null;
    if ( u.getUserInfo() != null ) {
        int sep = u.getUserInfo().indexOf(':');
        if ( sep >= 0 ) {
            username = u.getUserInfo().substring(0, sep);
            password = u.getUserInfo().substring(sep + 1);
        }
        else {
            System.err.println("Need <user>:<password>@");
            System.exit(-1);
        }
    }

    doRun(u, payloadObject, username, password);
    Utils.releasePayload(args[1], payloadObject);
}
 
Example 11
Project: springboot-security-wechat   File: MyOAuth2RestTemplate.java   View Source Code Vote up 5 votes
@Override
protected URI appendQueryParameter(URI uri, OAuth2AccessToken accessToken) {
    try {
        String query = uri.getRawQuery();
        String queryFragment = this.resource.getTokenName() + "=" + URLEncoder.encode(accessToken.getValue(), "UTF-8");
        if(query == null) {
            query = queryFragment;
        } else {
            query = query + "&" + queryFragment;
        }
        String openid = (String) accessToken
                .getAdditionalInformation().get("openid");
        System.out.println("openid == " + openid);
        String openIdQueryFragment = "openid=" + URLEncoder.encode(openid, "UTF-8");
        query = query + "&" + openIdQueryFragment;
        URI update = new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), uri.getPath(), (String)null, (String)null);
        StringBuffer sb = new StringBuffer(update.toString());
        sb.append("?");
        sb.append(query);
        if(uri.getFragment() != null) {
            sb.append("#");
            sb.append(uri.getFragment());
        }
        System.out.println("appendQueryParameter == " + sb.toString());
        return new URI(sb.toString());
    } catch (URISyntaxException var7) {
        throw new IllegalArgumentException("Could not parse URI", var7);
    } catch (UnsupportedEncodingException var8) {
        throw new IllegalArgumentException("Could not encode URI", var8);
    }
}
 
Example 12
Project: hadoop   File: FTPFileSystem.java   View Source Code Vote up 5 votes
@Override
public void initialize(URI uri, Configuration conf) throws IOException { // get
  super.initialize(uri, conf);
  // get host information from uri (overrides info in conf)
  String host = uri.getHost();
  host = (host == null) ? conf.get(FS_FTP_HOST, null) : host;
  if (host == null) {
    throw new IOException("Invalid host specified");
  }
  conf.set(FS_FTP_HOST, host);

  // get port information from uri, (overrides info in conf)
  int port = uri.getPort();
  port = (port == -1) ? FTP.DEFAULT_PORT : port;
  conf.setInt("fs.ftp.host.port", port);

  // get user/password information from URI (overrides info in conf)
  String userAndPassword = uri.getUserInfo();
  if (userAndPassword == null) {
    userAndPassword = (conf.get("fs.ftp.user." + host, null) + ":" + conf
        .get("fs.ftp.password." + host, null));
  }
  String[] userPasswdInfo = userAndPassword.split(":");
  Preconditions.checkState(userPasswdInfo.length > 1,
                           "Invalid username / password");
  conf.set(FS_FTP_USER_PREFIX + host, userPasswdInfo[0]);
  conf.set(FS_FTP_PASSWORD_PREFIX + host, userPasswdInfo[1]);
  setConf(conf);
  this.uri = uri;
}
 
Example 13
Project: Saiy-PS   File: GoogleCredentialsInterceptor.java   View Source Code Vote up 5 votes
private URI removePort(URI uri) throws StatusException {
    try {
        return new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), -1 /* port */,
                uri.getPath(), uri.getQuery(), uri.getFragment());
    } catch (URISyntaxException e) {
        throw Status.UNAUTHENTICATED
                .withDescription("Unable to construct service URI after removing port")
                .withCause(e).asException();
    }
}
 
Example 14
Project: lams   File: URIUtils.java   View Source Code Vote up 5 votes
/**
 * A convenience method that creates a new {@link URI} whose scheme, host, port, path,
 * query are taken from the existing URI, dropping any fragment or user-information.
 * The existing URI is returned unmodified if it has no fragment or user-information.
 *
 * @param uri
 *            original URI.
 * @throws URISyntaxException
 *             If the resulting URI is invalid.
 */
public static URI rewriteURI(final URI uri) throws URISyntaxException {
    if (uri == null) {
        throw new IllegalArgumentException("URI may not be null");
    }
    if (uri.getFragment() != null || uri.getUserInfo() != null) {
        return new URIBuilder(uri).setFragment(null).setUserInfo(null).build();
    } else {
        return uri;
    }
}
 
Example 15
Project: FirefoxData-android   File: URIBuilder.java   View Source Code Vote up 5 votes
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(), Consts.UTF_8);
    this.encodedFragment = uri.getRawFragment();
    this.fragment = uri.getFragment();
}
 
Example 16
Project: pac4j-plus   File: UriUtils.java   View Source Code Vote up 5 votes
/**
 * Normalizes a URI. If it contains the default port for the used scheme, the method replaces the port with "default".
 * 
 * @param uri
 *            The URI to normalize.
 * 
 * @return A normalized URI.
 * 
 * @throws URISyntaxException
 *             If a URI cannot be created because of wrong syntax.
 */
private static URI normalizePortNumbersInUri(final URI uri) throws URISyntaxException {
    int port = uri.getPort();
    final String scheme = uri.getScheme();

    if (SCHEME_HTTP.equals(scheme) && port == DEFAULT_HTTP_PORT) {
        port = -1;
    }
    if (SCHEME_HTTPS.equals(scheme) && port == DEFAULT_HTTPS_PORT) {
        port = -1;
    }
    
    final URI result = new URI(scheme, uri.getUserInfo(), uri.getHost(), port, uri.getPath(), uri.getQuery(), uri.getFragment());
    return result;
}
 
Example 17
Project: pooled-jms   File: SocketProxy.java   View Source Code Vote up 4 votes
private URI urlFromSocket(URI uri, ServerSocket serverSocket) throws Exception {
    int listenPort = serverSocket.getLocalPort();

    return new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), listenPort, uri.getPath(), uri.getQuery(),
            uri.getFragment());
}
 
Example 18
Project: pooled-jms   File: SocketProxy.java   View Source Code Vote up 4 votes
private URI urlFromSocket(URI uri, ServerSocket serverSocket) throws Exception {
    int listenPort = serverSocket.getLocalPort();

    return new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), listenPort, uri.getPath(), uri.getQuery(),
            uri.getFragment());
}
 
Example 19
Project: jdk8u-jdk   File: WindowsUriSupport.java   View Source Code Vote up 4 votes
/**
 * Converts given URI to a Path
 */
static WindowsPath fromUri(WindowsFileSystem fs, URI uri) {
    if (!uri.isAbsolute())
        throw new IllegalArgumentException("URI is not absolute");
    if (uri.isOpaque())
        throw new IllegalArgumentException("URI is not hierarchical");
    String scheme = uri.getScheme();
    if ((scheme == null) || !scheme.equalsIgnoreCase("file"))
        throw new IllegalArgumentException("URI scheme is not \"file\"");
    if (uri.getFragment() != null)
        throw new IllegalArgumentException("URI has a fragment component");
    if (uri.getQuery() != null)
        throw new IllegalArgumentException("URI has a query component");
    String path = uri.getPath();
    if (path.equals(""))
        throw new IllegalArgumentException("URI path component is empty");

    // UNC
    String auth = uri.getAuthority();
    if (auth != null && !auth.equals("")) {
        String host = uri.getHost();
        if (host == null)
            throw new IllegalArgumentException("URI authority component has undefined host");
        if (uri.getUserInfo() != null)
            throw new IllegalArgumentException("URI authority component has user-info");
        if (uri.getPort() != -1)
            throw new IllegalArgumentException("URI authority component has port number");

        // IPv6 literal
        // 1. drop enclosing brackets
        // 2. replace ":" with "-"
        // 3. replace "%" with "s" (zone/scopeID delimiter)
        // 4. Append .ivp6-literal.net
        if (host.startsWith("[")) {
            host = host.substring(1, host.length()-1)
                       .replace(':', '-')
                       .replace('%', 's');
            host += IPV6_LITERAL_SUFFIX;
        }

        // reconstitute the UNC
        path = "\\\\" + host + path;
    } else {
        if ((path.length() > 2) && (path.charAt(2) == ':')) {
            // "/c:/foo" --> "c:/foo"
            path = path.substring(1);
        }
    }
    return WindowsPath.parse(fs, path);
}
 
Example 20
Project: RLibrary   File: ProxySettings.java   View Source Code Vote up 3 votes
/**
 * Set the proxy server by a URI. The parameters are updated as
 * described below.
 *
 * <blockquote>
 * <dl>
 *   <dt>Secure</dt>
 *   <dd><p>
 *     If the URI contains the scheme part and its value is
 *     either {@code "http"} or {@code "https"} (case-insensitive),
 *     the {@code secure} parameter is updated to {@code false}
 *     or to {@code true} accordingly. In other cases, the parameter
 *     is not updated.
 *   </p></dd>
 *   <dt>ID &amp; Password</dt>
 *   <dd><p>
 *     If the URI contains the userinfo part and the ID embedded
 *     in the userinfo part is not an empty string, the {@code
 *     id} parameter and the {@code password} parameter are updated
 *     accordingly. In other cases, the parameters are not updated.
 *   </p></dd>
 *   <dt>Host</dt>
 *   <dd><p>
 *     The {@code host} parameter is always updated by the given URI.
 *   </p></dd>
 *   <dt>Port</dt>
 *   <dd><p>
 *     The {@code port} parameter is always updated by the given URI.
 *   </p></dd>
 * </dl>
 * </blockquote>
 *
 * @param uri
 *         The URI of the proxy server. If {@code null} is given,
 *         none of the parameters is updated.
 *
 * @return
 *         {@code this} object.
 */
public ProxySettings setServer(URI uri)
{
    if (uri == null)
    {
        return this;
    }

    String scheme   = uri.getScheme();
    String userInfo = uri.getUserInfo();
    String host     = uri.getHost();
    int port        = uri.getPort();

    return setServer(scheme, userInfo, host, port);
}