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:play.modules.rabbitmq.RabbitMQPlugin.java

/**
 * On application start./*from  w  w  w . j  a  va 2 s  . c o m*/
 */
@Override
public void onApplicationStart() {
    // Parse rabbitMQ connection url if one exists
    if (Play.configuration.containsKey("rabbitmq.url")) {
        String rabbitmqUrl = Play.configuration.getProperty("rabbitmq.url");
        URI rabbitmqUri;
        try {
            rabbitmqUri = new URI(rabbitmqUrl);
        } catch (URISyntaxException e) {
            throw new RuntimeException("Unable to parse rabbitmq.url (" + rabbitmqUrl + ")", e);
        }

        Play.configuration.setProperty("rabbitmq.host", rabbitmqUri.getHost());
        Play.configuration.setProperty("rabbitmq.port", Integer.toString(rabbitmqUri.getPort()));

        if (rabbitmqUri.getPath().length() > 1) {
            Play.configuration.setProperty("rabbitmq.vhost", rabbitmqUri.getPath().substring(1));
        }

        if (rabbitmqUri.getUserInfo() != null) {
            String[] rabbitmqUserInfo = rabbitmqUri.getUserInfo().split(":");
            Play.configuration.setProperty("rabbitmq.username", rabbitmqUserInfo[0]);
            Play.configuration.setProperty("rabbitmq.password", rabbitmqUserInfo[1]);
        }
    }

    // Connection Factory
    factory.setUsername(getUserName());
    factory.setPassword(getPassword());
    factory.setVirtualHost(getVhost());
}

From source file:com.cisco.oss.foundation.http.AbstractHttpClient.java

protected S updateRequestUri(S request, InternalServerProxy serverProxy) {

    URI origUri = request.getUri();
    String host = serverProxy.getHost();
    String scheme = origUri.getScheme() == null ? (request.isHttpsEnabled() ? "https" : "http")
            : origUri.getScheme();//from   w w  w  .j  a v  a  2 s  .c o  m

    int port = serverProxy.getPort();

    String urlPath = "";
    if (origUri.getRawPath() != null && origUri.getRawPath().startsWith("/")) {
        urlPath = origUri.getRawPath();
    } else {
        urlPath = "/" + origUri.getRawPath();
    }

    URI newURI = null;
    try {
        if (autoEncodeUri) {
            String query = origUri.getQuery();
            newURI = new URI(scheme, origUri.getUserInfo(), host, port, urlPath, query, origUri.getFragment());
        } else {
            String query = origUri.getRawQuery();
            if (query != null) {
                newURI = new URI(scheme + "://" + host + ":" + port + urlPath + "?" + query);
            } else {
                newURI = new URI(scheme + "://" + host + ":" + port + urlPath);
            }

        }
    } catch (URISyntaxException e) {
        throw new ClientException(e.toString());
    }

    S req = (S) request.replaceUri(newURI);
    //        try {
    //            req = (S) this.clone();
    //        } catch (CloneNotSupportedException e) {
    //            throw new IllegalArgumentException(e);
    //        }
    //        req.uri = newURI;
    return req;
}

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

protected void setEndpoint(URI uri, Properties props) throws MalformedEndpointException {
    address = StringUtils.EMPTY;/*from   w w  w  .j av a  2 s  .  co m*/
    String host = uri.getHost();
    if (host != null && !"localhost".equals(host)) {
        address = host;
    }

    String path = uri.getPath();
    String authority = uri.getAuthority();

    if (path != null && path.length() != 0) {
        if (address.length() > 0) {
            address += "/";
        }
        address += path.substring(1);
    } else if (authority != null && !authority.equals(address)) {
        address += authority;

        int atCharIndex = -1;
        if (address != null && address.length() != 0 && ((atCharIndex = address.indexOf("@")) > -1)) {
            userInfo = address.substring(0, atCharIndex);
            address = address.substring(atCharIndex + 1);
        }

    }

    // is user info specified?
    int y = address.indexOf("@");
    if (y > -1) {
        userInfo = address.substring(0, y);
    }
    // increment to 0 or one char past the @
    y++;

    String credentials = uri.getUserInfo();
    if (credentials != null && credentials.length() != 0) {
        userInfo = credentials;
    }

    int x = address.indexOf(":", y);
    if (x > -1) {
        String resourceInfo = address.substring(y, x);
        props.setProperty(RESOURCE_INFO_PROPERTY, resourceInfo);
        address = address.substring(x + 1);
    }
}

From source file:org.apache.hadoop.fs.sftp.SFTPFileSystem.java

/**
 * Set configuration from UI./*w  w w . ja va  2  s. co m*/
 *
 * @param uri
 * @param conf
 * @throws IOException
 */
private void setConfigurationFromURI(URI uriInfo, Configuration conf) throws IOException {

    // get host information from URI
    String host = uriInfo.getHost();
    host = (host == null) ? conf.get(FS_SFTP_HOST, null) : host;
    if (host == null) {
        throw new IOException(E_HOST_NULL);
    }
    conf.set(FS_SFTP_HOST, host);

    int port = uriInfo.getPort();
    port = (port == -1) ? conf.getInt(FS_SFTP_HOST_PORT, DEFAULT_SFTP_PORT) : port;
    conf.setInt(FS_SFTP_HOST_PORT, port);

    // get user/password information from URI
    String userAndPwdFromUri = uriInfo.getUserInfo();
    if (userAndPwdFromUri != null) {
        String[] userPasswdInfo = userAndPwdFromUri.split(":");
        String user = userPasswdInfo[0];
        user = URLDecoder.decode(user, "UTF-8");
        conf.set(FS_SFTP_USER_PREFIX + host, user);
        if (userPasswdInfo.length > 1) {
            conf.set(FS_SFTP_PASSWORD_PREFIX + host + "." + user, userPasswdInfo[1]);
        }
    }

    String user = conf.get(FS_SFTP_USER_PREFIX + host);
    if (user == null || user.equals("")) {
        throw new IllegalStateException(E_USER_NULL);
    }

    int connectionMax = conf.getInt(FS_SFTP_CONNECTION_MAX, DEFAULT_MAX_CONNECTION);
    connectionPool = new SFTPConnectionPool(connectionMax);
}

From source file:org.apache.hadoop.fs.ftp.FTPFileSystem.java

@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();/*  w  w w .j av a2  s . c o  m*/
    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));
        if (userAndPassword == null) {
            throw new IOException("Invalid user/passsword specified");
        }
    }
    String[] userPasswdInfo = userAndPassword.split(":");
    conf.set("fs.ftp.user." + host, userPasswdInfo[0]);
    if (userPasswdInfo.length > 1) {
        conf.set("fs.ftp.password." + host, userPasswdInfo[1]);
    } else {
        conf.set("fs.ftp.password." + host, null);
    }
    setConf(conf);
    this.uri = uri;
}

From source file:org.apache.hadoop.fs.HarFileSystem.java

/**
 * Create a har specific auth //from w  w  w  .  ja  v  a  2 s  .c  o  m
 * 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;
}

From source file:com.seajas.search.contender.service.modifier.FeedModifierService.java

/**
 * Retrieve the content of a result feed URL.
 * // ww  w  .  j a  v  a  2  s  .com
 * @param uri
 * @param encodingOverride
 * @param userAgent
 * @param resultHeaders
 * @return Reader
 */
private Reader getContent(final URI uri, final String encodingOverride, final String userAgent,
        final Map<String, String> resultHeaders) {
    Reader result = null;
    String contentType = null;

    // Retrieve the feed

    try {
        InputStream inputStream = null;

        if (uri.getScheme().equalsIgnoreCase("ftp") || uri.getScheme().equalsIgnoreCase("ftps")) {
            FTPClient ftpClient = uri.getScheme().equalsIgnoreCase("ftps") ? new FTPSClient() : new FTPClient();

            try {
                ftpClient.connect(uri.getHost(), uri.getPort() != -1 ? uri.getPort() : 21);

                if (StringUtils.hasText(uri.getUserInfo())) {
                    if (uri.getUserInfo().contains(":"))
                        ftpClient.login(uri.getUserInfo().substring(0, uri.getUserInfo().indexOf(":")),
                                uri.getUserInfo().substring(uri.getUserInfo().indexOf(":") + 1));
                    else
                        ftpClient.login(uri.getUserInfo(), "");

                    inputStream = ftpClient.retrieveFileStream(uri.getPath());
                }
            } finally {
                ftpClient.disconnect();
            }
        } else if (uri.getScheme().equalsIgnoreCase("file")) {
            File file = new File(uri);

            if (!file.isDirectory())
                inputStream = new FileInputStream(uri.getPath());
            else
                inputStream = RSSDirectoryBuilder.build(file);
        } else if (uri.getScheme().equalsIgnoreCase("http") || uri.getScheme().equalsIgnoreCase("https")) {
            try {
                HttpGet method = new HttpGet(uri.toString());

                if (resultHeaders != null)
                    for (Entry<String, String> resultHeader : resultHeaders.entrySet())
                        method.setHeader(new BasicHeader(resultHeader.getKey(), resultHeader.getValue()));
                if (userAgent != null)
                    method.setHeader(CoreProtocolPNames.USER_AGENT, userAgent);

                SizeRestrictedHttpResponse response = httpClient.execute(method,
                        new SizeRestrictedResponseHandler(maximumContentLength, uri));

                try {
                    if (response != null) {
                        inputStream = new ByteArrayInputStream(response.getResponse());
                        contentType = response.getContentType() != null ? response.getContentType().getValue()
                                : null;
                    } else
                        return null;
                } catch (RuntimeException e) {
                    method.abort();

                    throw e;
                }
            } catch (IllegalArgumentException e) {
                logger.error("Invalid URL " + uri.toString() + " - not returning content", e);

                return null;
            }
        } else {
            logger.error("Unknown protocol " + uri.getScheme() + ". Skipping feed.");

            return null;
        }

        // Guess the character encoding using ROME's reader, then buffer it so we can discard the input stream (and close the connection)

        InputStream readerInputStream = new BufferedInputStream(inputStream);
        MediaType mediaType = autoDetectParser.getDetector().detect(readerInputStream, new Metadata());

        try {
            Reader reader = null;

            if (mediaType.getType().equals("application")) {
                if (mediaType.getSubtype().equals("x-gzip")) {
                    GZIPInputStream gzipInputStream = new GZIPInputStream(readerInputStream);

                    if (encodingOverride != null)
                        reader = readerToBuffer(new StringBuffer(),
                                new InputStreamReader(gzipInputStream, encodingOverride), false);
                    else
                        reader = readerToBuffer(new StringBuffer(),
                                contentType != null ? new XmlHtmlReader(gzipInputStream, contentType, true)
                                        : new XmlReader(gzipInputStream, true),
                                false);

                    gzipInputStream.close();
                } else if (mediaType.getSubtype().equals("zip")) {
                    ZipFile zipFile = null;

                    // ZipInputStream can't do read-aheads, so we have to use a temporary on-disk file instead

                    File temporaryFile = File.createTempFile("profiler-", ".zip");

                    try {
                        FileOutputStream zipOutputStream = new FileOutputStream(temporaryFile);
                        IOUtils.copy(readerInputStream, zipOutputStream);

                        readerInputStream.close();

                        zipOutputStream.flush();
                        zipOutputStream.close();

                        // Create a new entry and process it

                        zipFile = new ZipFile(temporaryFile);
                        Enumeration<? extends ZipEntry> zipEnumeration = zipFile.entries();

                        ZipEntry zipEntry = zipEnumeration.nextElement();

                        if (zipEntry == null || zipEntry.isDirectory() || zipEnumeration.hasMoreElements()) {
                            logger.error(
                                    "ZIP files are currently expected to contain one and only one entry, which is to be a file");

                            return null;
                        }

                        // We currently only perform prolog stripping for ZIP files

                        InputStream zipInputStream = new BufferedInputStream(zipFile.getInputStream(zipEntry));

                        if (encodingOverride != null)
                            reader = readerToBuffer(new StringBuffer(), new InputStreamReader(
                                    new BufferedInputStream(zipInputStream), encodingOverride), true);
                        else
                            result = readerToBuffer(new StringBuffer(),
                                    contentType != null
                                            ? new XmlHtmlReader(new BufferedInputStream(zipInputStream),
                                                    contentType, true)
                                            : new XmlReader(new BufferedInputStream(zipInputStream), true),
                                    true);
                    } catch (Exception e) {
                        logger.error("An error occurred during ZIP file processing", e);

                        return null;
                    } finally {
                        if (zipFile != null)
                            zipFile.close();

                        if (!temporaryFile.delete())
                            logger.error("Unable to delete temporary file");
                    }
                }
            }

            if (result == null) {
                if (encodingOverride != null)
                    result = readerToBuffer(new StringBuffer(), reader != null ? reader
                            : new InputStreamReader(readerInputStream, encodingOverride), false);
                else
                    result = readerToBuffer(new StringBuffer(),
                            reader != null ? reader
                                    : contentType != null
                                            ? new XmlHtmlReader(readerInputStream, contentType, true)
                                            : new XmlReader(readerInputStream, true),
                            false);
            }
        } catch (Exception e) {
            logger.error("An error occurred during stream processing", e);

            return null;
        } finally {
            inputStream.close();
        }
    } catch (IOException e) {
        logger.error("Could not retrieve the given feed: " + e.getMessage(), e);

        return null;
    }

    return result;
}