/** * Creates a URI from the original URI and the remaining parameters. * * When the query options of a URI are applied to certain objects the used portion of the query options needs * to be removed and replaced with those that remain so that other parts of the code can attempt to apply the * remainder or give an error is unknown values were given. This method is used to update a URI with those * remainder values. * * @param originalURI The URI whose current parameters are remove and replaced with the given remainder value. * @param params The URI params that should be used to replace the current ones in the target. * @return a new URI that matches the original one but has its query options replaced with the given ones. * @throws java.net.URISyntaxException */ public static URI createRemainingURI(URI originalURI, Map<String, String> params) throws URISyntaxException { String s = createQueryString(params); if (s.length() == 0) { s = null; } return createURIWithQuery(originalURI, s); }
/** * Given a URI parse and extract any URI query options and return them as a Key / Value mapping. * * This method differs from the {@link parseQuery} method in that it handles composite URI types and * will extract the URI options from the outermost composite URI. * * @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> parseParameters(URI uri) throws URISyntaxException { if (!isCompositeURI(uri)) { return uri.getQuery() == null ? emptyMap() : parseQuery(stripPrefix(uri.getQuery(), "?")); } else { CompositeData data = URISupport.parseComposite(uri); Map<String, String> parameters = new HashMap<>(); parameters.putAll(data.getParameters()); if (parameters.isEmpty()) { parameters = emptyMap(); } return parameters; } }
/** * 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); } }
/** * Examine a URI and determine if it is a Composite type or not. * * @param uri The URI that is to be examined. * @return true if the given URI is a Composite type. */ public static boolean isCompositeURI(URI uri) { String ssp = stripPrefix(uri.getRawSchemeSpecificPart().trim(), "//").trim(); if (ssp.indexOf('(') == 0 && checkParenthesis(ssp)) { return true; } return false; }
String params; if (!checkParenthesis(ssp)) { throw new URISyntaxException(uri.toString(), "Not a matching number of '(' and ')' parenthesis"); p = indexOfParenthesisMatch(ssp, initialParen); componentString = ssp.substring(initialParen + 1, p); params = ssp.substring(p + 1).trim(); String[] components = splitComponents(componentString); rc.components = new URI[components.length]; for (int i = 0; i < components.length; i++) { if (p >= 0) { if (p > 0) { rc.path = stripPrefix(params.substring(0, p), "/"); rc.parameters = parseQuery(params.substring(p + 1)); } else { if (params.length() > 0) { rc.path = stripPrefix(params, "/"); rc.parameters = emptyMap();
/** * 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; }
public static String toParameterisedAddress(String address, Map<String, String> parameters) throws URISyntaxException { if (parameters != null && !parameters.isEmpty()) { return appendParameters(new StringBuilder(address), parameters).toString(); } else { return address; } }
public static boolean isParameterised(String address) { return URISupport.containsQuery(address); }
/** * Removes any URI query from the given uri and return a new URI that does not contain the query portion. * * @param uri The URI whose query value is to be removed. * @return a new URI that does not contain a query value. * @throws java.net.URISyntaxException */ public static URI removeQuery(URI uri) throws URISyntaxException { return createURIWithQuery(uri, null); }
public static StringBuilder appendParameters(StringBuilder sb, Map<String, String> parameters) throws URISyntaxException { if (!parameters.isEmpty()) { sb.append('?'); sb.append(createQueryString(parameters)); } return sb; }
@Override public void populateObject(URI uri, ClusterConnectionConfiguration bean) throws Exception { if (URISupport.isCompositeURI(uri)) { super.populateObject(uri, bean); } else { bean.setDiscoveryGroupName(uri.getHost()); Map<String, String> parameters = URISupport.parseParameters(uri); BeanSupport.setData(uri, bean, parameters); } }
/** * Given a Key / Value mapping create and append a URI query value that represents the mapped entries, return the * newly updated URI that contains the value of the given URI and the appended query value. * * @param uri The source URI that will have the Map entries appended as a URI query value. * @param queryParameters The Key / Value mapping that will be transformed into a URI query string. * @return A new URI value that combines the given URI and the constructed query string. * @throws java.net.URISyntaxException */ public static URI applyParameters(URI uri, Map<String, String> queryParameters) throws URISyntaxException { return applyParameters(uri, queryParameters, ""); }
@Override public void populateObject(URI uri, ClusterConnectionConfiguration bean) throws Exception { URISupport.CompositeData compositeData = URISupport.parseComposite(uri); bean.setCompositeMembers(compositeData); BeanSupport.setData(uri, bean, compositeData.getParameters()); }
String params; if (!checkParenthesis(ssp)) { throw new URISyntaxException(uri.toString(), "Not a matching number of '(' and ')' parenthesis"); p = indexOfParenthesisMatch(ssp, initialParen); componentString = ssp.substring(initialParen + 1, p); params = ssp.substring(p + 1).trim(); String[] components = splitComponents(componentString); rc.components = new URI[components.length]; for (int i = 0; i < components.length; i++) { if (p >= 0) { if (p > 0) { rc.path = stripPrefix(params.substring(0, p), "/"); rc.parameters = parseQuery(params.substring(p + 1)); } else { if (params.length() > 0) { rc.path = stripPrefix(params, "/"); rc.parameters = emptyMap();
/** * 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; }
/** * Examine a URI and determine if it is a Composite type or not. * * @param uri The URI that is to be examined. * @return true if the given URI is a Composite type. */ public static boolean isCompositeURI(URI uri) { String ssp = stripPrefix(uri.getRawSchemeSpecificPart().trim(), "//").trim(); if (ssp.indexOf('(') == 0 && checkParenthesis(ssp)) { return true; } return false; }
public URI toURI() throws URISyntaxException { StringBuilder sb = new StringBuilder(); if (scheme != null) { sb.append(scheme); sb.append(':'); } if (host != null && host.length() != 0) { sb.append(host); } else { sb.append('('); for (int i = 0; i < components.length; i++) { if (i != 0) { sb.append(','); } sb.append(components[i].toString()); } sb.append(')'); } if (path != null) { sb.append('/'); sb.append(path); } appendParameters(sb, parameters); if (fragment != null) { sb.append('#'); sb.append(fragment); } return new URI(sb.toString()); } }
public static boolean isParameterised(SimpleString address) { return URISupport.containsQuery(address); }
/** * Given a Key / Value mapping create and append a URI query value that represents the mapped entries, return the * newly updated URI that contains the value of the given URI and the appended query value. Each entry in the query * string is prefixed by the supplied optionPrefix string. * * @param uri The source URI that will have the Map entries appended as a URI query value. * @param queryParameters The Key / Value mapping that will be transformed into a URI query string. * @param optionPrefix A string value that when not null or empty is used to prefix each query option key. * @return A new URI value that combines the given URI and the constructed query string. * @throws java.net.URISyntaxException */ public static URI applyParameters(URI uri, Map<String, String> queryParameters, String optionPrefix) throws URISyntaxException { if (queryParameters != null && !queryParameters.isEmpty()) { StringBuilder newQuery = uri.getRawQuery() != null ? new StringBuilder(uri.getRawQuery()) : new StringBuilder(); for (Map.Entry<String, String> param : queryParameters.entrySet()) { if (param.getKey().startsWith(optionPrefix)) { if (newQuery.length() != 0) { newQuery.append('&'); } final String key = param.getKey().substring(optionPrefix.length()); newQuery.append(key).append('=').append(param.getValue()); } } uri = createURIWithQuery(uri, newQuery.toString()); } return uri; }
public static StringBuilder appendParameters(StringBuilder sb, Map<String, String> parameters) throws URISyntaxException { if (!parameters.isEmpty()) { sb.append('?'); sb.append(createQueryString(parameters)); } return sb; }