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:com.collective.celos.ci.deploy.WorkflowFilesDeployer.java

URI getTargetJsFileUri(URI uri) throws URISyntaxException {
    String path = uri.getPath() + File.separator + config.getWorkflowName() + ".js";
    return new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), path, uri.getQuery(),
            uri.getFragment());// w  w  w. jav  a 2s  .  c o m
}

From source file:beadsan.AppConfig.java

@ConfigurationProperties("spring.datasource")
@Bean(destroyMethod = "close")
DataSource realDataSource() throws URISyntaxException {
    String url;//from  w  w  w  .  jav  a  2 s  .c  om
    String username;
    String password;

    String databaseUrl = System.getenv("DATABASE_URL");
    if (databaseUrl != null) {
        URI dbUri = new URI(databaseUrl);
        url = "jdbc:postgresql://" + dbUri.getHost() + ":" + dbUri.getPort() + dbUri.getPath();
        username = dbUri.getUserInfo().split(":")[0];
        password = dbUri.getUserInfo().split(":")[1];
    } else {
        url = this.properties.getUrl();
        username = this.properties.getUsername();
        password = this.properties.getPassword();
    }

    DataSourceBuilder factory = DataSourceBuilder.create(this.properties.getClassLoader()).url(url)
            .username(username).password(password);
    this.dataSource = factory.build();
    return this.dataSource;
}

From source file:org.apache.synapse.commons.evaluators.source.URLTextRetriever.java

public String getSourceText(EvaluatorContext context) throws EvaluatorException {
    if (fragment == null) {
        return context.getUrl();
    }/*from  www  . j  av a2s  . c  o m*/

    try {
        URI uri = new URI(context.getUrl());
        switch (fragment) {
        case protocol:
            return uri.getScheme();
        case user:
            return uri.getUserInfo();
        case host:
            return uri.getHost();
        case port:
            return String.valueOf(uri.getPort());
        case path:
            return uri.getPath();
        case query:
            return uri.getQuery();
        case ref:
            return uri.getFragment();
        default:
            return context.getUrl();
        }
    } catch (URISyntaxException e) {
        String message = "Error parsing URL: " + context.getUrl();
        log.error(message);
        throw new EvaluatorException(message);
    }
}

From source file:org.mcxiaoke.commons.http.util.URIUtilsEx.java

/**
 * 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 path is set to "/" if not explicitly specified.
 * The existing URI is returned unmodified if it has no fragment or
 * user-information and has a path.//from w  ww. j av a 2s.  c  o  m
 * 
 * @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
            || (uri.getPath() == null || uri.getPath().length() == 0)) {
        URIBuilderEx uribuilder = new URIBuilderEx(uri);
        uribuilder.setFragment(null).setUserInfo(null);
        if (uribuilder.getPath() == null || uribuilder.getPath().length() == 0) {
            uribuilder.setPath("/");
        }
        return uribuilder.build();
    } else {
        return uri;
    }
}

From source file:org.springframework.cloud.sleuth.zipkin2.sender.ZipkinRestTemplateSenderConfiguration.java

private URI resolvedZipkinUri(URI originalUrl, URI resolvedZipkinUri) {
    try {/* ww w  . j av  a 2 s . c  o  m*/
        return new URI(resolvedZipkinUri.getScheme(), resolvedZipkinUri.getUserInfo(),
                resolvedZipkinUri.getHost(), resolvedZipkinUri.getPort(), originalUrl.getPath(),
                originalUrl.getQuery(), originalUrl.getFragment());
    } catch (URISyntaxException e) {
        if (log.isDebugEnabled()) {
            log.debug("Failed to create the new URI from original [" + originalUrl + "] and new one ["
                    + resolvedZipkinUri + "]");
        }
        return originalUrl;
    }
}

From source file:org.addhen.birudo.data.net.BaseHttpClient.java

private void addHeader() {
    try {/*from   www . ja  v  a  2s . c o m*/
        URI uri = new URI(url);
        String userInfo = uri.getUserInfo();
        if (userInfo != null) {
            setHeader("Authorization", "Basic " + base64Encode(userInfo));
        }
    } catch (URISyntaxException e) {
        debug(e);
    }

    StringBuilder userAgent = new StringBuilder("BirudoRogu-Android");
    setHeader("User-Agent", userAgent.toString());
    // set headers on request
    Headers.Builder headerBuilder = new Headers.Builder();
    for (String key : header.keySet()) {
        headerBuilder.set(key, header.get(key));
    }
    setHeaders(headerBuilder.build());
}

From source file:org.neo4j.ogm.session.SessionFactory.java

/**
 * Opens a new Neo4j mapping {@link Session} against the specified Neo4j database.
 * The url may optionally contain the username and password to use while authenticating
 * for example, http://username:password@neo-server:port
 * Otherwise, if authentication is required, the username and password will be read from System properties.
 *
 * @param url The base URL of the Neo4j database with which to communicate.
 * @return A new {@link Session}/* www .  ja v  a2  s . c  o  m*/
 */
public Session openSession(String url) {
    try {
        URI uri = new URI(url);
        String username = null;
        String password = null;
        String uriStr = uri.toString();
        String auth = uri.getUserInfo();
        if (auth != null && !auth.trim().isEmpty()) {
            username = auth.split(":")[0];
            password = auth.split(":")[1];

            uriStr = uri.getScheme() + "://"
                    + uri.toString().substring(uri.toString().indexOf(auth) + auth.length() + 1);
        }

        if (username != null && password != null) {
            return new Neo4jSession(metaData, uriStr, httpClient, objectMapper,
                    new UsernamePasswordCredentials(username, password));

        }
        return new Neo4jSession(metaData, uriStr, httpClient, objectMapper);
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.kitodo.dataaccess.format.xml.Namespaces.java

/**
 * Converts a URI to a globally unique name space. The name space is formed
 * with the host name of the machine the program is running on.
 *
 * @param uri//from w w  w .j  a  v  a 2 s.co m
 *            URI to create a globally unique URI for
 * @return globally unique namespace
 * @throws IOException
 *             if it fails
 */
public static String namespaceFromURI(URI uri) throws IOException {
    try {
        String host = uri.getHost();
        String path = uri.getPath();
        if (host == null) {
            host = InetAddress.getLocalHost().getCanonicalHostName();
            if ((path != null) && path.startsWith("//")) {
                int pathStart = path.indexOf('/', 2);
                String remote = path.substring(2, pathStart);
                path = path.substring(pathStart);
                host = remote.contains(".") ? remote
                        : remote.concat(host.substring(InetAddress.getLocalHost().getHostName().length()));
            }
        }
        String scheme = uri.getScheme();
        if ((scheme == null) || !scheme.toLowerCase().startsWith("http")) {
            scheme = "http";
        }
        return new URI(scheme, uri.getUserInfo(), host, uri.getPort(), path, uri.getQuery(), "")
                .toASCIIString();
    } catch (URISyntaxException e) {
        String message = e.getMessage();
        throw new IllegalArgumentException(message != null ? message : e.getClass().getName(), e);
    }
}

From source file:org.eclipse.smarthome.ui.internal.proxy.ProxyServlet.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String sitemapName = request.getParameter("sitemap");
    String widgetId = request.getParameter("widgetId");

    if (sitemapName == null) {
        throw new ServletException("Parameter 'sitemap' must be provided!");
    }/*from  w  w  w .j  a v  a2  s  . c o m*/
    if (widgetId == null) {
        throw new ServletException("Parameter 'widget' must be provided!");
    }

    String uriString = null;

    Sitemap sitemap = (Sitemap) modelRepository.getModel(sitemapName);
    if (sitemap != null) {
        Widget widget = itemUIRegistry.getWidget(sitemap, widgetId);
        if (widget instanceof Image) {
            Image image = (Image) widget;
            uriString = image.getUrl();
        } else if (widget instanceof Video) {
            Video video = (Video) widget;
            uriString = video.getUrl();
        } else {
            if (widget == null) {
                throw new ServletException("Widget '" + widgetId + "' could not be found!");
            } else {
                throw new ServletException(
                        "Widget type '" + widget.getClass().getName() + "' is not supported!");
            }
        }
    } else {
        throw new ServletException("Sitemap '" + sitemapName + "' could not be found!");
    }

    HttpClient httpClient = new HttpClient();

    try {
        // check if the uri uses credentials and configure the http client accordingly
        URI uri = URI.create(uriString);

        if (uri.getUserInfo() != null) {
            String[] userInfo = uri.getUserInfo().split(":");
            httpClient.getParams().setAuthenticationPreemptive(true);
            Credentials creds = new UsernamePasswordCredentials(userInfo[0], userInfo[1]);
            httpClient.getState()
                    .setCredentials(new AuthScope(uri.getHost(), uri.getPort(), AuthScope.ANY_REALM), creds);
        }
    } catch (IllegalArgumentException e) {
        throw new ServletException("URI '" + uriString + "' is not valid: " + e.getMessage());
    }

    // do the client request
    GetMethod method = new GetMethod(uriString);
    httpClient.executeMethod(method);

    // copy all headers
    for (Header header : method.getResponseHeaders()) {
        response.setHeader(header.getName(), header.getValue());
    }

    // now copy/stream the body content
    IOUtils.copy(method.getResponseBodyAsStream(), response.getOutputStream());
    method.releaseConnection();
}