Example usage for java.net URI getScheme

List of usage examples for java.net URI getScheme

Introduction

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

Prototype

public String getScheme() 

Source Link

Document

Returns the scheme component of this URI.

Usage

From source file:URIUtil.java

/**
 * Get the parent URI of the supplied URI
 * @param uri The input URI for which the parent URI is being requrested.
 * @return The parent URI.  Returns a URI instance equivalent to "../" if
 * the supplied URI path has no parent./*from   w  w  w  .  j av  a 2  s . c o  m*/
 * @throws URISyntaxException Failed to reconstruct the parent URI.
 */
public static URI getParent(URI uri) throws URISyntaxException {

    String parentPath = new File(uri.getPath()).getParent();

    if (parentPath == null) {
        return new URI("../");
    }

    return new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(),
            parentPath.replace('\\', '/'), uri.getQuery(), uri.getFragment());
}

From source file:com.qwazr.cluster.manager.ClusterNode.java

private static URI toUri(String address) throws URISyntaxException {
    if (!address.contains("//"))
        address = "//" + address;
    URI u = new URI(address);
    return new URI(StringUtils.isEmpty(u.getScheme()) ? "http" : u.getScheme(), null, u.getHost(), u.getPort(),
            null, null, null);/*from w  ww.jav  a 2  s. co  m*/
}

From source file:io.druid.security.kerberos.DruidKerberosUtil.java

public static HttpCookie getAuthCookie(CookieStore cookieStore, URI uri) {
    if (cookieStore == null) {
        return null;
    }/*from w w  w. j  a  v  a  2  s . com*/
    boolean isSSL = uri.getScheme().equals("https");
    List<HttpCookie> cookies = cookieStore.getCookies();

    for (HttpCookie c : cookies) {
        // If this is a secured cookie and the current connection is non-secured,
        // then, skip this cookie. We need to skip this cookie because, the cookie
        // replay will not be transmitted to the server.
        if (c.getSecure() && !isSSL) {
            continue;
        }
        if (c.getName().equals(AuthenticatedURL.AUTH_COOKIE)) {
            return c;
        }
    }
    return null;
}

From source file:org.brutusin.rpc.RpcUtils.java

private static int getPort(URI uri) {
    if (uri.getPort() >= 0) {
        return uri.getPort();
    }//  w w  w  .j  a  v a  2s. co  m
    if ("http".equals(uri.getScheme())) {
        return 80;
    }
    if ("https".equals(uri.getScheme())) {
        return 443;
    }
    return uri.getPort();
}

From source file:oracle.custom.ui.oauth.vo.OpenIdConfiguration.java

public static OpenIdConfiguration getButDontSave(String baseUrl) throws Exception {

    String url = baseUrl + endpoint;
    System.out.println("URL is '" + url + "'");
    HttpClient client = ServerUtils.getClient();
    URI uri = new URI(url);
    HttpGet get = new HttpGet(uri);
    HttpHost host = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
    HttpResponse response = client.execute(host, get);
    try {/*from  w ww  .  j  av a 2 s .  c o m*/
        HttpEntity entity2 = response.getEntity();
        String res = EntityUtils.toString(entity2);
        EntityUtils.consume(entity2);
        ObjectMapper mapper = new ObjectMapper();
        //res = res.replaceAll("secureoracle.idcs.internal.oracle.com:443", "oc-140-86-12-131.compute.oraclecloud.com");
        OpenIdConfiguration openIdConfigInstance = mapper.readValue(res, OpenIdConfiguration.class);

        return openIdConfigInstance;
    } finally {
        if (response instanceof CloseableHttpResponse) {
            ((CloseableHttpResponse) response).close();
        }
    }
}

From source file:oracle.custom.ui.oauth.vo.OpenIdConfiguration.java

public static OpenIdConfiguration getInstance(String tenantName) throws Exception {
    if (configMap.containsKey(tenantName.toLowerCase())) {
        return configMap.get(tenantName.toLowerCase());
    }//from   ww  w. j av a 2  s.co m
    String url = ServerUtils.getIDCSBaseURL(tenantName) + endpoint;
    System.out.println("URL for tenant '" + tenantName + "' is '" + url + "'");
    HttpClient client = ServerUtils.getClient(tenantName);
    URI uri = new URI(url);
    HttpGet get = new HttpGet(uri);
    HttpHost host = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
    HttpResponse response = client.execute(host, get);
    try {
        HttpEntity entity2 = response.getEntity();
        String res = EntityUtils.toString(entity2);
        EntityUtils.consume(entity2);
        ObjectMapper mapper = new ObjectMapper();
        //res = res.replaceAll("secureoracle.idcs.internal.oracle.com:443", "oc-140-86-12-131.compute.oraclecloud.com");
        OpenIdConfiguration openIdConfigInstance = mapper.readValue(res, OpenIdConfiguration.class);

        configMap.put(tenantName.toLowerCase(), openIdConfigInstance);
        return openIdConfigInstance;
    } finally {
        if (response instanceof CloseableHttpResponse) {
            ((CloseableHttpResponse) response).close();
        }
    }
}

From source file:com.knowledgecode.cordova.websocket.ConnectionTask.java

/**
 * Complement default port number.// www . java2  s.c o  m
 *
 * @param url
 * @return URI
 * @throws URISyntaxException
 */
private static URI complementPort(String url) throws URISyntaxException {
    URI uri = new URI(url);
    int port = uri.getPort();

    if (port < 0) {
        if ("ws".equals(uri.getScheme())) {
            port = 80;
        } else if ("wss".equals(uri.getScheme())) {
            port = 443;
        }
        uri = new URI(uri.getScheme(), "", uri.getHost(), port, uri.getPath(), uri.getQuery(), "");
    }
    return uri;
}

From source file:de.vandermeer.skb.datatool.commons.DataUtilities.java

/**
 * Loads an data for an SKB link.//from w  w  w.  ja v  a  2  s  . com
 * @param skbLink the link to load an data for
 * @param loadedTypes loaded types as lookup for links
 * @return an object if successfully loaded, null otherwise
 * @throws IllegalArgumentException if any of the required arguments or map entries are not set or empty
 * @throws URISyntaxException if creating a URI for an SKB link failed
 */
public static Object loadLink(Object skbLink, LoadedTypeMap loadedTypes) throws URISyntaxException {
    if (skbLink == null) {
        throw new IllegalArgumentException("skb link null");
    }
    if (loadedTypes == null) {
        throw new IllegalArgumentException("trying to load a link, but no loaded types given");
    }

    URI uri = new URI(skbLink.toString());
    if (!"skb".equals(uri.getScheme())) {
        throw new IllegalArgumentException("unknown scheme in link <" + skbLink + ">");
    }

    String uriReq = uri.getScheme() + "://" + uri.getAuthority();
    DataEntryType type = null;
    for (DataEntryType det : loadedTypes.keySet()) {
        if (uriReq.equals(det.getLinkUri())) {
            type = det;
            break;
        }
    }
    if (type == null) {
        throw new IllegalArgumentException(
                "no data entry type for link <" + uri.getScheme() + "://" + uri.getAuthority() + ">");
    }

    @SuppressWarnings("unchecked")
    Map<String, Object> map = (Map<String, Object>) loadedTypes.getTypeMap(type);
    if (map == null) {
        throw new IllegalArgumentException("no entry for type <" + type.getType() + "> in link map");
    }

    String key = StringUtils.substringAfterLast(uri.getPath(), "/");
    Object ret = map.get(key);
    if (ret == null) {
        throw new IllegalArgumentException(
                "no entry for <" + uri.getAuthority() + "> key <" + key + "> in link map");
    }
    return ret;
}

From source file:eu.esdihumboldt.hale.io.haleconnect.HaleConnectUrnBuilder.java

private static String[] splitProjectUrn(URI urn) {
    if (urn == null) {
        throw new NullPointerException("URN must not be null");
    } else if (!SCHEME_HALECONNECT.equals(urn.getScheme().toLowerCase())) {
        throw new IllegalArgumentException(
                MessageFormat.format("URN must have scheme \"{0}\"", SCHEME_HALECONNECT));
    }//from www .j a  v a 2s  .c  om

    if (StringUtils.isEmpty(urn.getSchemeSpecificPart())) {
        throw new IllegalArgumentException(MessageFormat.format("Malformed URN: {0}", urn.toString()));
    }

    String[] parts = urn.getSchemeSpecificPart().split(":");
    if (parts.length != 4) {
        throw new IllegalArgumentException(MessageFormat.format("Malformed URN: {0}", urn.toString()));
    } else if (!"project".equals(parts[0])) {
        throw new IllegalArgumentException(MessageFormat.format("No a project URN: {0}", urn.toString()));
    }
    return parts;
}

From source file:ws.wamp.jawampa.transport.WampClientChannelFactoryResolver.java

public static WampClientChannelFactory getFactory(final URI uri, final SslContext sslCtx)
        throws ApplicationError {
    String scheme = uri.getScheme();
    scheme = scheme != null ? scheme : "";

    if (scheme.equalsIgnoreCase("ws") || scheme.equalsIgnoreCase("wss")) {

        // Check the host and port field for validity
        if (uri.getHost() == null || uri.getPort() == 0) {
            throw new ApplicationError(ApplicationError.INVALID_URI);
        }//  www  . j a  va  2s  . c o m

        // Return a factory that creates a channel for websocket connections            
        return new WampClientChannelFactory() {
            @Override
            public ChannelFuture createChannel(final ChannelHandler handler, final EventLoopGroup eventLoop,
                    final ObjectMapper objectMapper) throws Exception {
                // Initialize SSL when required
                final boolean needSsl = uri.getScheme().equalsIgnoreCase("wss");
                final SslContext sslCtx0;
                if (needSsl && sslCtx == null) {
                    // Create a default SslContext when we got none provided through the constructor
                    try {
                        sslCtx0 = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE);
                    } catch (SSLException e) {
                        throw e;
                    }
                } else if (needSsl) {
                    sslCtx0 = sslCtx;
                } else {
                    sslCtx0 = null;
                }

                // Use well-known ports if not explicitly specified
                final int port;
                if (uri.getPort() == -1) {
                    if (needSsl)
                        port = 443;
                    else
                        port = 80;
                } else
                    port = uri.getPort();

                final WebSocketClientHandshaker handshaker = WebSocketClientHandshakerFactory.newHandshaker(uri,
                        WebSocketVersion.V13, WampHandlerConfiguration.WAMP_WEBSOCKET_PROTOCOLS, false,
                        new DefaultHttpHeaders());

                Bootstrap b = new Bootstrap();
                b.group(eventLoop).channel(NioSocketChannel.class)
                        .handler(new ChannelInitializer<SocketChannel>() {
                            @Override
                            protected void initChannel(SocketChannel ch) {
                                ChannelPipeline p = ch.pipeline();
                                if (sslCtx0 != null) {
                                    p.addLast(sslCtx0.newHandler(ch.alloc(), uri.getHost(), port));
                                }
                                p.addLast(new HttpClientCodec(), new HttpObjectAggregator(8192),
                                        new WebSocketClientProtocolHandler(handshaker, false),
                                        new WebSocketFrameAggregator(
                                                WampHandlerConfiguration.MAX_WEBSOCKET_FRAME_SIZE),
                                        new WampClientWebsocketHandler(handshaker, objectMapper), handler);
                            }
                        });

                return b.connect(uri.getHost(), port);
            }
        };
    }

    throw new ApplicationError(ApplicationError.INVALID_URI);
}