Java URI Create createURI(final String scheme, final String[] pathAsArray)

Here you can find the source of createURI(final String scheme, final String[] pathAsArray)

Description

create URI

License

Apache License

Declaration

protected static URI createURI(final String scheme, final String[] pathAsArray) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.net.URI;
import java.net.URISyntaxException;

public class Main {
    public static URI createURI(final String scheme, final String userInfo, final String host, final String path,
            final String query, final String fragment) {
        if (path == null) {
            throw new NullPointerException("path cannot be null");
        }/*  ww w. j a  va2 s  .  c  o m*/
        Integer port = 0;
        try {
            return new URI(scheme, userInfo, host, port, path, query, fragment);
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }

    }

    protected static URI createURI(final String scheme, final String[] pathAsArray) {
        if (pathAsArray == null) {
            throw new NullPointerException("path = null");
        }

        // always start with a slash
        StringBuilder path = new StringBuilder("/");

        int i = 0;
        for (i = 0; i < pathAsArray.length; i++) {

            String pathElement = pathAsArray[i];
            if (0 == i && pathElement.length() == 0) {
                // handle when leading slash was included in the split, resulting
                // in an empty string as first element (ignore)
                continue;
            }

            if (1 == pathAsArray.length && pathElement.equals("/")) {
                // we're on first and only element,
                // and we're saying "root", so do nothing
                continue;
            }

            validateResource(pathElement); // throws
            path.append(pathElement + "/");
        }
        try {
            URI u = new URI(scheme, null, path.toString(), null);
            return ensureNoTrailingSlash(u);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    private static void validateResource(String resourceId) {
        if (resourceId.indexOf("/") != -1 || resourceId.indexOf("\\") != -1) {
            throw new RuntimeException("Resource id cannot contain a slash.");
        }
    }

    public static URI ensureNoTrailingSlash(URI rawURI) {

        if (rawURI == null) {
            throw new NullPointerException("URI path is null.");
        }

        if (rawURI.getPath().trim().length() == 0) {
            throw new RuntimeException("URI path cannot be empty.");
        }

        String path = rawURI.getPath();
        int index = path.length() - 1;

        try {
            // if the path itself is not root, remove the trailing slash
            if (!path.equals("/") && path.charAt(index) == '/') {
                // strip slash
                String newPath = rawURI.getPath().substring(0, index);
                return new URI(rawURI.getScheme(), rawURI.getHost(), newPath, rawURI.getFragment());
            } else {
                return new URI(rawURI.getScheme(), rawURI.getHost(), rawURI.getPath(), rawURI.getFragment());
            }

        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. createURI(final String file)
  2. createUri(final String path)
  3. createUri(final String path, final String query)
  4. createURI(final String scheme, final String host, final int port)
  5. createURI(final String scheme, final String host, int port, final String path, final String query, final String fragment)
  6. createURI(List proxyPorts)
  7. createUri(String base)
  8. createURI(String extension)
  9. createURI(String filename)