/** * Adds the parameter to the URL while URL encoding them. * * @param name the name of the parameter. This value cannot be blank. * @param value the value of the parameter. * @return this builder so that calls may be changed * @throws IllegalArgumentException if name is blank */ public UrlBuilder addParameter(final String name, final String value) { notBlank("name", name); final String safeName = encode(name); final String safeValue = value == null ? "" : encode(value); addParameterUnsafe(safeName, safeValue); return this; }
/** * Add the passed anchor value to the URL while URL encoding it. The result will be something like <code>#myAnchor</code>. * Note that to be compliant with standards, you will want to call this only <em>after</em> adding all your parameters. * * @param value the value of the anchor * @return this builder so that calls may be changed * @throws IllegalArgumentException if name is blank */ public UrlBuilder addAnchor(final String value) { notBlank("value", value); final String safeValue = encode(value); anchorBuilder.append("#").append(safeValue); return this; }
/** * URL encodes and adds the given paths to the URL. If needed a '/' will be added between the existing URL and the given * paths. The paths in between the path separator ('/') will be encoded. * * @param paths paths to be encoded and added to the URL. E.g. "/one/two/three". Cannot be blank. * @return this builder so that calls may be chained * @throws IllegalArgumentException if paths is blank */ public UrlBuilder addPaths(String paths) { notBlank("paths", paths); String[] pathComponents = StringUtils.split(paths, PATH_SEPARATOR_CHAR); List<String> safePathComponents = Lists.newArrayListWithExpectedSize(pathComponents.length); for (String singlePath : pathComponents) { safePathComponents.add(encode(singlePath)); } String safePaths = StringUtils.join(safePathComponents, '/'); final boolean pathEndsWithSlash = paths.charAt(paths.length() - 1) == PATH_SEPARATOR_CHAR; if (pathEndsWithSlash) { safePaths += PATH_SEPARATOR_CHAR; } return addPathUnsafe(safePaths); }
/** * URL encodes and adds the given path to the URL. If needed a '/' will be added between the existing URL and the given * path. * * @param path path to be encoded and added to the URL. The path cannot be blank. * @return this builder so that calls may be chained * @throws IllegalArgumentException if path is blank */ public UrlBuilder addPath(String path) { notBlank("path", path); String safePath = encode(StringUtils.strip(path, "/")); final boolean pathEndsWithSlash = path.charAt(path.length() - 1) == PATH_SEPARATOR_CHAR; if (pathEndsWithSlash) { safePath += PATH_SEPARATOR_CHAR; } return addPathUnsafe(safePath); }