private void validateUserInfo(String uri, String userInfo, int index) throws URISyntaxException { for (int i = 0; i < userInfo.length(); i++) { char ch = userInfo.charAt(i); if (ch == ']' || ch == '[') { throw new URISyntaxException(uri, "Illegal character in userInfo", index + i); } } }
public URI getHarUri(URI original) throws URISyntaxException { URI relative = originalBase.relativize(original); if (relative.isAbsolute()) { throw new URISyntaxException("Could not create URI for location.", "Relative: " + relative + " Base: " + base + " OriginalBase: " + originalBase); } return base.resolve(relative); } }
private String validateScheme(String uri, int end) throws URISyntaxException { if (end == 0) { throw new URISyntaxException(uri, "Scheme expected", 0); } for (int i = 0; i < end; i++) { if (!UrlUtils.isValidSchemeChar(i, uri.charAt(i))) { throw new URISyntaxException(uri, "Illegal character in scheme", 0); } } return uri.substring(0, end); }
public URI getHarUri(URI original) throws URISyntaxException { URI relative = originalBase.relativize(original); if (relative.isAbsolute()) { throw new URISyntaxException("Couldn't create URI for location.", "Relative: " + relative + " Base: " + base + " OriginalBase: " + originalBase); } return base.resolve(relative); } }
private static URI validateIssuer(String issuer) throws URISyntaxException { try { new URL(issuer); } catch (MalformedURLException x) { throw new URISyntaxException(issuer, x.getMessage()); } return new URI(issuer); }
/** * Throws if {@code s} contains characters that are not letters, digits or * in {@code legal}. */ public static void validateSimple(String s, String legal) throws URISyntaxException { for (int i = 0; i < s.length(); i++) { char ch = s.charAt(i); if (!((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || legal.indexOf(ch) > -1)) { throw new URISyntaxException(s, "Illegal character", i); } } }
private static URI resolveBaseUrl(HttpServerRequest request) throws URISyntaxException { String scheme = getScheme(request); String header = request.requestHeaders().get(HttpHeaderNames.HOST); if (header != null) { final int portIndex; if (header.startsWith("[")) { portIndex = header.indexOf(':', header.indexOf(']')); } else { portIndex = header.indexOf(':'); } if (portIndex != -1) { try { return new URI(scheme, null, header.substring(0, portIndex), Integer.parseInt(header.substring(portIndex + 1)), null, null, null); } catch (NumberFormatException ex) { throw new URISyntaxException(header, "Unable to parse port", portIndex); } } else { return new URI(scheme, header, null, null); } } else { InetSocketAddress localAddress = request.hostAddress(); return new URI(scheme, null, localAddress.getHostString(), localAddress.getPort(), null, null, null); } }
private List<Map<String, String>> computeExcludeList(String excludeString) throws URISyntaxException { String excludes[] = excludeString.split(","); List<Map<String, String>> excludeList = new LinkedList<Map<String, String>>(); for (String exclude : excludes) { Map<String, String> tempMap = new HashMap<String, String>(); String args[] = exclude.split(":"); if (args.length != 2) { throw new URISyntaxException(excludeString, "Invalid exclude string: expected 'org:module,org:module,..', found " + excludeString); } tempMap.put("group", args[0]); tempMap.put("module", args[1]); excludeList.add(tempMap); } return excludeList; }
/** * It's a lot easier to configure and manage the location of the graphite server if we combine * the ip and port into a single string. Using a "fake" transport and the ipString means we get * standard host/port parsing (including domain names, ipv4 and ipv6) for free. */ private static URI parseStringAsUri(String ipString) { try { URI uri = new URI("socket://" + ipString); if (uri.getHost() == null || uri.getPort() == -1) { throw new URISyntaxException(ipString, "URI must have host and port parts"); } return uri; } catch (URISyntaxException e) { throw (IllegalArgumentException) new IllegalArgumentException( "Graphite server address needs to be defined as {host}:{port}.").initCause(e); } } }
private List<Map<String, String>> computeExcludeList(String excludeString) throws URISyntaxException { String excludes[] = excludeString.split(","); List<Map<String, String>> excludeList = new LinkedList<Map<String, String>>(); for (String exclude : excludes) { Map<String, String> tempMap = new HashMap<String, String>(); String args[] = exclude.split(":"); if (args.length != 2) { throw new URISyntaxException(excludeString, "Invalid exclude string: expected 'org:module,org:module,..', found " + excludeString); } tempMap.put("group", args[0]); tempMap.put("module", args[1]); excludeList.add(tempMap); } return excludeList; }
String string = ... // some string which has to be validated String host; int port; try { // WORKAROUND: add any scheme to make the resulting URI valid. URI uri = new URI("my://" + string); // may throw URISyntaxException host = uri.getHost(); port = uri.getPort(); if (uri.getHost() == null || uri.getPort() == -1) { throw new URISyntaxException(uri.toString(), "URI must have host and port parts"); } // here, additional checks can be performed, such as // presence of path, query, fragment, ... } catch (URISyntaxException ex) { // validation failed } // validation succeeded
throw new URISyntaxException(str, "URI did not contain a matching parenthesis.");
/** * * @param uri * @return List of URIs of downloaded jars * @throws URISyntaxException * @throws IOException */ public List<URI> downloadDependencies(URI uri) throws URISyntaxException, IOException { Map<String, Object> dependencyMap = new HashMap<String, Object>(); String authority = uri.getAuthority(); if (authority == null) { throw new URISyntaxException(authority, "Invalid url: Expected 'org:module:version', found null"); } String[] authorityTokens = authority.split(":"); if (authorityTokens.length != 3) { throw new URISyntaxException(authority, "Invalid url: Expected 'org:module:version', found " + authority); } dependencyMap.put("org", authorityTokens[0]); dependencyMap.put("module", authorityTokens[1]); dependencyMap.put("version", authorityTokens[2]); Map<String, Object> queryMap = parseQueryString(uri.getQuery()); if (queryMap != null) { dependencyMap.putAll(queryMap); } return grab(dependencyMap); }
/** * Give a URI break off any URI options and store them in a Key / Value Mapping. * * @param uri The URI whose query should be extracted and processed. * @return A Mapping of the URI options. * @throws java.net.URISyntaxException */ public static Map<String, String> parseQuery(String uri) throws URISyntaxException { try { uri = uri.substring(uri.lastIndexOf("?") + 1); // get only the relevant part of the query Map<String, String> rc = new HashMap<>(); if (uri != null && !uri.isEmpty()) { parseParameters(rc, uri.split("[&;]")); } return rc; } catch (UnsupportedEncodingException e) { throw (URISyntaxException) new URISyntaxException(e.toString(), "Invalid encoding").initCause(e); } }
/** * Throws if {@code s} is invalid according to this encoder. */ public final String validate(String uri, int start, int end, String name) throws URISyntaxException { for (int i = start; i < end; ) { char ch = uri.charAt(i); if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || isRetained(ch)) { i++; } else if (ch == '%') { if (i + 2 >= end) { throw new URISyntaxException(uri, "Incomplete % sequence in " + name, i); } int d1 = hexToInt(uri.charAt(i + 1)); int d2 = hexToInt(uri.charAt(i + 2)); if (d1 == -1 || d2 == -1) { throw new URISyntaxException(uri, "Invalid % sequence: " + uri.substring(i, i + 3) + " in " + name, i); } i += 3; } else { throw new URISyntaxException(uri, "Illegal character in " + name, i); } } return uri.substring(start, end); }
/** * * @param uri * @return List of URIs of downloaded jars * @throws URISyntaxException * @throws IOException */ public List<URI> downloadDependencies(URI uri) throws URISyntaxException, IOException { Map<String, Object> dependencyMap = new HashMap<String, Object>(); String authority = uri.getAuthority(); if (authority == null) { throw new URISyntaxException(authority, "Invalid url: Expected 'org:module:version', found null"); } String[] authorityTokens = authority.split(":"); if (authorityTokens.length != 3) { throw new URISyntaxException(authority, "Invalid url: Expected 'org:module:version', found " + authority); } dependencyMap.put("org", authorityTokens[0]); dependencyMap.put("module", authorityTokens[1]); dependencyMap.put("version", authorityTokens[2]); Map<String, Object> queryMap = parseQueryString(uri.getQuery()); if (queryMap != null) { dependencyMap.putAll(queryMap); } return grab(dependencyMap); }
private static URI resolveBaseUrl(HttpServerRequest request) throws URISyntaxException { String scheme = getScheme(request); String header = request.requestHeaders().get(HttpHeaderNames.HOST); if (header != null) { final int portIndex; if (header.startsWith("[")) { portIndex = header.indexOf(':', header.indexOf(']')); } else { portIndex = header.indexOf(':'); } if (portIndex != -1) { try { return new URI(scheme, null, header.substring(0, portIndex), Integer.parseInt(header.substring(portIndex + 1)), null, null, null); } catch (NumberFormatException ex) { throw new URISyntaxException(header, "Unable to parse port", portIndex); } } else { return new URI(scheme, header, null, null); } } else { InetSocketAddress localAddress = request.hostAddress(); return new URI(scheme, null, localAddress.getHostString(), localAddress.getPort(), null, null, null); } }
throw (URISyntaxException) new URISyntaxException(e.toString(), "Invalid encoding").initCause(e);
public static Map<String, String> parseQuery(String uri, Map<String, String> propertyOverrides) throws URISyntaxException { try { Map<String, String> rc = new HashMap<>(); if (uri != null && !uri.isEmpty()) { String[] parameters = uri.split("&"); for (String parameter : parameters) { int p = parameter.indexOf("="); if (p >= 0) { String name = BeanSupport.decodeURI(parameter.substring(0, p)); String value = BeanSupport.decodeURI(parameter.substring(p + 1)); rc.put(name, value); } else { if (!parameter.trim().isEmpty()) { rc.put(parameter, null); } } } } if (propertyOverrides != null) { for (Map.Entry<String, String> entry : propertyOverrides.entrySet()) { rc.put(entry.getKey(), entry.getValue()); } } return rc; } catch (UnsupportedEncodingException e) { throw (URISyntaxException) new URISyntaxException(e.toString(), "Invalid encoding").initCause(e); } }
@Override public void validate(ValidatorContext ctx) { DataElement element = ctx.dataElement(); Object value = element.getValue(); String str = String.valueOf(value); boolean valid = true; try { Iterable<String> uriStrings = Splitter.on(",").omitEmptyStrings().trimResults().split(str); for (String uriString : uriStrings) { URI uri = new URI(uriString); if (!uri.getScheme().equalsIgnoreCase(FS_SCHEME)) { throw new URISyntaxException(uriString, "Scheme is not FS"); } } } catch (URISyntaxException e) { valid = false; } if (!valid) { ctx.addResult(new Message(element.path(), "\"%1$s\" is not a well-formed comma-separated list of URIs", str)); } } }