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:Main.java

/**
 * Removes dot segments according to RFC 3986, section 5.2.4
 *
 * @param uri the original URI//from  ww w. java2s  .  c  o m
 * @return the URI without dot segments
 */
private static URI removeDotSegments(URI uri) {
    String path = uri.getPath();
    if ((path == null) || (path.indexOf("/.") == -1)) {
        // No dot segments to remove
        return uri;
    }
    String[] inputSegments = path.split("/");
    Stack<String> outputSegments = new Stack<String>();
    for (int i = 0; i < inputSegments.length; i++) {
        if ((inputSegments[i].length() == 0) || (".".equals(inputSegments[i]))) {
            // Do nothing
        } else if ("..".equals(inputSegments[i])) {
            if (!outputSegments.isEmpty()) {
                outputSegments.pop();
            }
        } else {
            outputSegments.push(inputSegments[i]);
        }
    }
    StringBuilder outputBuffer = new StringBuilder();
    for (String outputSegment : outputSegments) {
        outputBuffer.append('/').append(outputSegment);
    }
    try {
        return new URI(uri.getScheme(), uri.getAuthority(), outputBuffer.toString(), uri.getQuery(),
                uri.getFragment());
    } catch (URISyntaxException e) {
        throw new IllegalArgumentException(e);
    }
}

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//w  w  w.j a v a2  s  . c om
 */
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:de.openknowledge.cdi.common.property.source.ClassPathPropertySourceLoader.java

@Override
public boolean supports(URI source) {
    return source.getScheme() == null || CLASSPATH_SCHEME.equals(source.getScheme());
}

From source file:org.apache.servicemix.document.impl.DocumentFactoryTest.java

public void testDoc() {
    ApplicationContext ctx = new ClassPathXmlApplicationContext("test.xml");
    URI uri = (URI) ctx.getBean("uri");
    assertEquals("document", uri.getScheme());
}

From source file:com.yoho.core.trace.instrument.web.client.AbstractTraceHttpRequestInterceptor.java

private String uriScheme(URI uri) {
    return uri.getScheme() == null ? "http" : uri.getScheme();
}

From source file:com.comoyo.emjar.EmJarTest.java

protected File getResourceFile(String name) throws URISyntaxException {
    final ClassLoader cl = getClass().getClassLoader();
    final URI base = cl.getResource("com/comoyo/emjar/").toURI();
    URIBuilder builder = new URIBuilder(base);
    builder.setPath(builder.getPath() + name);
    final URI uri = builder.build();
    if (!"file".equals(uri.getScheme())) {
        throw new IllegalArgumentException("Resource " + name + " not present as file (" + uri + ")");
    }/*from  www .ja  va2 s .c  o m*/
    return new File(uri.getSchemeSpecificPart());
}

From source file:com.buaa.cfs.fs.AbstractFileSystem.java

private static URI getBaseUri(URI uri) {
    String scheme = uri.getScheme();
    String authority = uri.getAuthority();
    String baseUriString = scheme + "://";
    if (authority != null) {
        baseUriString = baseUriString + authority;
    } else {/*from w w w  .  j  a va 2s  . co m*/
        baseUriString = baseUriString + "/";
    }
    return URI.create(baseUriString);
}

From source file:com.buaa.cfs.fs.AbstractFileSystem.java

/**
 * Get the statistics for a particular file system.
 *
 * @param uri used as key to lookup STATISTICS_TABLE. Only scheme and authority part of the uri are used.
 *
 * @return a statistics object/* www .j  av  a2 s . c om*/
 */
protected static synchronized FileSystem.Statistics getStatistics(URI uri) {
    String scheme = uri.getScheme();
    if (scheme == null) {
        throw new IllegalArgumentException("Scheme not defined in the uri: " + uri);
    }
    URI baseUri = getBaseUri(uri);
    FileSystem.Statistics result = STATISTICS_TABLE.get(baseUri);
    if (result == null) {
        result = new FileSystem.Statistics(scheme);
        STATISTICS_TABLE.put(baseUri, result);
    }
    return result;
}

From source file:com.smartitengineering.cms.ws.resources.content.ContentResource.java

protected static ContentResource getContentResource(String contentUrl, UriBuilder absBuilder,
        ResourceContext context)/*from  w w  w. ja  va2s.c  om*/
        throws ContainerException, IllegalArgumentException, ClassCastException, UriBuilderException {
    final URI uri;
    if (contentUrl.startsWith("http:")) {
        uri = URI.create(contentUrl);
    } else {
        URI absUri = absBuilder.build();
        uri = UriBuilder.fromPath(contentUrl).host(absUri.getHost()).port(absUri.getPort())
                .scheme(absUri.getScheme()).build();
    }
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("URI to content is " + uri);
    }
    ContentResource resource = context.matchResource(uri, ContentResource.class);
    return resource;
}

From source file:de.openknowledge.cdi.common.property.source.FilePropertySourceLoader.java

public boolean supports(URI source) {
    return FILE_SCHEME.equals(source.getScheme());
}