/** * Returns whether or not the given URI is valid as a base file system URI. It must not have a * path, query or fragment. */ private static boolean isValidFileSystemUri(URI uri) { // would like to just check null, but fragment appears to be the empty string when not present return isNullOrEmpty(uri.getPath()) && isNullOrEmpty(uri.getQuery()) && isNullOrEmpty(uri.getFragment()); }
protected static Text getDtService(URI uri) { Text service; // remove fragment for forward compatibility with logical naming. final String fragment = uri.getFragment(); if (fragment != null) { service = new Text( uri.getScheme() + ":" + uri.getSchemeSpecificPart()); } else { service = new Text(uri.toString()); } return service; }
/** * Creates a URI with the given query, removing an previous query value from the given URI. * * @param uri The source URI whose existing query is replaced with the newly supplied one. * @param query The new URI query string that should be appended to the given URI. * @return a new URI that is a combination of the original URI and the given query string. * @throws java.net.URISyntaxException */ public static URI createURIWithQuery(URI uri, String query) throws URISyntaxException { String schemeSpecificPart = uri.getRawSchemeSpecificPart(); // strip existing query if any int questionMark = schemeSpecificPart.lastIndexOf("?"); // make sure question mark is not within parentheses if (questionMark < schemeSpecificPart.lastIndexOf(")")) { questionMark = -1; } if (questionMark > 0) { schemeSpecificPart = schemeSpecificPart.substring(0, questionMark); } if (query != null && query.length() > 0) { schemeSpecificPart += "?" + query; } return new URI(uri.getScheme(), schemeSpecificPart, uri.getFragment()); }
/** * Mangle given local java keystore file URI to allow use as a * LocalJavaKeyStoreProvider. * @param localFile absolute URI with file scheme and no authority component. * i.e. return of File.toURI, * e.g. file:///home/larry/creds.jceks * @return URI of the form localjceks://file/home/larry/creds.jceks * @throws IllegalArgumentException if localFile isn't not a file uri or if it * has an authority component. * @throws URISyntaxException if the wrapping process violates RFC 2396 */ public static URI nestURIForLocalJavaKeyStoreProvider(final URI localFile) throws URISyntaxException { if (!("file".equals(localFile.getScheme()))) { throw new IllegalArgumentException("passed URI had a scheme other than " + "file."); } if (localFile.getAuthority() != null) { throw new IllegalArgumentException("passed URI must not have an " + "authority component. For non-local keystores, please use " + JavaKeyStoreProvider.class.getName()); } return new URI(LocalJavaKeyStoreProvider.SCHEME_NAME, "//file" + localFile.getSchemeSpecificPart(), localFile.getFragment()); }
public static List<TransportConfiguration> getTransportConfigurations(URI uri, Map<String, String> query, Set<String> allowableProperties, String name, String factoryName) throws URISyntaxException { HashMap<String, Object> props = new HashMap<>(); Map<String, Object> extraProps = new HashMap<>(); BeanSupport.setData(uri, props, allowableProperties, query, extraProps); List<TransportConfiguration> transportConfigurations = new ArrayList<>(); TransportConfiguration config = new TransportConfiguration(factoryName, props, name, extraProps); transportConfigurations.add(config); String connectors = uri.getFragment(); if (connectors != null && !connectors.trim().isEmpty()) { String[] split = connectors.split(","); for (String s : split) { URI extraUri = new URI(s); HashMap<String, Object> newProps = new HashMap<>(); extraProps = new HashMap<>(); BeanSupport.setData(extraUri, newProps, allowableProperties, query, extraProps); BeanSupport.setData(extraUri, newProps, allowableProperties, parseQuery(extraUri.getQuery(), null), extraProps); transportConfigurations.add(new TransportConfiguration(factoryName, newProps, name + ":" + extraUri.toString(), extraProps)); } } return transportConfigurations; }
@Override public String toString() { // we can't use uri.toString(), which escapes everything, because we want // illegal characters unescaped in the string, for glob processing, etc. StringBuilder buffer = new StringBuilder(); if (uri.getScheme() != null) { buffer.append(uri.getScheme()); buffer.append(":"); } if (uri.getAuthority() != null) { buffer.append("//"); buffer.append(uri.getAuthority()); } if (uri.getPath() != null) { String path = uri.getPath(); if (path.indexOf('/')==0 && hasWindowsDrive(path) && // has windows drive uri.getScheme() == null && // but no scheme uri.getAuthority() == null) // or authority path = path.substring(1); // remove slash before drive buffer.append(path); } if (uri.getFragment() != null) { buffer.append("#"); buffer.append(uri.getFragment()); } return buffer.toString(); }
/** * Given a composite URI, parse the individual URI elements contained within that URI and return * a CompositeData instance that contains the parsed URI values. * * @param uri The target URI that should be parsed. * @return a new CompositeData instance representing the parsed composite URI. * @throws java.net.URISyntaxException */ public static CompositeData parseComposite(URI uri) throws URISyntaxException { CompositeData rc = new CompositeData(); rc.scheme = uri.getScheme(); String ssp = stripPrefix(uri.getRawSchemeSpecificPart().trim(), "//").trim(); parseComposite(uri, rc, ssp); rc.fragment = uri.getFragment(); return rc; }
/** * Creates a URI with the given query, removing an previous query value from the given URI. * * @param uri * The source URI whose existing query is replaced with the newly supplied one. * @param query * The new URI query string that should be appended to the given URI. * * @return a new URI that is a combination of the original URI and the given query string. * @throws URISyntaxException */ public static URI createURIWithQuery(URI uri, String query) throws URISyntaxException { String schemeSpecificPart = uri.getRawSchemeSpecificPart(); // strip existing query if any int questionMark = schemeSpecificPart.lastIndexOf("?"); // make sure question mark is not within parentheses if (questionMark < schemeSpecificPart.lastIndexOf(")")) { questionMark = -1; } if (questionMark > 0) { schemeSpecificPart = schemeSpecificPart.substring(0, questionMark); } if (query != null && query.length() > 0) { schemeSpecificPart += "?" + query; } return new URI(uri.getScheme(), schemeSpecificPart, uri.getFragment()); }
/** * Convert a nested URI to decode the underlying path. The translation takes * the authority and parses it into the underlying scheme and authority. * For example, "myscheme://hdfs@nn/my/path" is converted to * "hdfs://nn/my/path". * @param nestedUri the URI from the nested URI * @return the unnested path */ public static Path unnestUri(URI nestedUri) { StringBuilder result = new StringBuilder(); String authority = nestedUri.getAuthority(); if (authority != null) { String[] parts = nestedUri.getAuthority().split("@", 2); result.append(parts[0]); result.append("://"); if (parts.length == 2) { result.append(parts[1]); } } result.append(nestedUri.getPath()); if (nestedUri.getQuery() != null) { result.append("?"); result.append(nestedUri.getQuery()); } if (nestedUri.getFragment() != null) { result.append("#"); result.append(nestedUri.getFragment()); } return new Path(result.toString()); }
/** * Given a composite URI, parse the individual URI elements contained within that URI and return * a CompsoteData instance that contains the parsed URI values. * * @param uri * The target URI that should be parsed. * * @return a new CompsiteData instance representing the parsed composite URI. * @throws URISyntaxException */ public static CompositeData parseComposite(URI uri) throws URISyntaxException { CompositeData rc = new CompositeData(); rc.scheme = uri.getScheme(); String ssp = stripPrefix(uri.getRawSchemeSpecificPart().trim(), "//").trim(); parseComposite(uri, rc, ssp); rc.fragment = uri.getFragment(); return rc; }
/** * Set the URI scheme, AKA the 'protocol.' e.g. * <code>setScheme('https')</code> * * @throws URISyntaxException if the given scheme contains illegal characters. */ public URIBuilder setScheme(String scheme) throws URISyntaxException { this.base = new URI(scheme, base.getUserInfo(), base.getHost(), base.getPort(), base.getPath(), base.getQuery(), base.getFragment()); return this; }
/** * Creates a new {@link URI} from the given URI by replacing the host value. * @param uri Original URI * @param newHostPrefix New host for the uri */ public static URI updateUriHost(URI uri, String newHostPrefix) { try { return new URI(uri.getScheme(), uri.getUserInfo(), newHostPrefix + uri.getHost(), uri.getPort(), uri.getPath(), uri.getQuery(), uri.getFragment()); } catch (URISyntaxException e) { throw new RuntimeException(e); } } }
private static URI buildURI(NodeId nodeId, URI baseUri) throws URISyntaxException { final String queryParams = "anonid=" + nodeId.anonymize() + "&version=" + ServerVersion.VERSION.toString(); return new URI( baseUri.getScheme(), baseUri.getUserInfo(), baseUri.getHost(), baseUri.getPort(), baseUri.getPath(), isNullOrEmpty(baseUri.getQuery()) ? queryParams : baseUri.getQuery() + "&" + queryParams, baseUri.getFragment() ); }
/** * Appends the given path to the given base URI. * * @param base the base URI * @param path the path to append * @return the URI resulting from appending the base and the path * @throws URISyntaxException if URI syntax error is encountered */ public static URI appendPath(URI base, String path) throws URISyntaxException { return new URI(base.getScheme(), base.getAuthority(), PathUtils.concatPath(base.getPath(), path), base.getQuery(), base.getFragment()); }
/** * Given a URI value create and return a new URI that matches the target one but with the scheme value * supplied to this method. * * @param bindAddr The URI whose scheme value should be altered. * @param scheme The new scheme value to use for the returned URI. * @return a new URI that is a copy of the original except that its scheme matches the supplied one. * @throws java.net.URISyntaxException */ public static URI changeScheme(URI bindAddr, String scheme) throws URISyntaxException { return new URI(scheme, bindAddr.getUserInfo(), bindAddr.getHost(), bindAddr.getPort(), bindAddr.getPath(), bindAddr.getQuery(), bindAddr.getFragment()); }