Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
Kiwi is a utility library. We really like Google's Guava, and also use Apache Commons.
But if they don't have something we need, and we think it is useful, this is where we put it.
package org.kiwiproject.jaxrs.client;
import static java.util.Objects.isNull;
import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.kiwiproject.base.KiwiPreconditions.checkArgumentNotBlank;
import static org.kiwiproject.base.KiwiPreconditions.checkArgumentNotNull;
import static org.kiwiproject.base.KiwiPreconditions.requireNotNull;
import static org.kiwiproject.collect.KiwiArrays.isNullOrEmpty;
import static org.kiwiproject.collect.KiwiLists.isNullOrEmpty;
import static org.kiwiproject.collect.KiwiMaps.isNullOrEmpty;
import com.google.common.annotations.Beta;
import com.google.common.annotations.VisibleForTesting;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.Invocation;
import jakarta.ws.rs.client.WebTarget;
import jakarta.ws.rs.core.Configuration;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.core.UriBuilder;
import org.apache.commons.lang3.StringUtils;
import java.net.URI;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Stream;
/**
* Use with Jakarta REST {@link WebTarget} instances to provide convenient functionality when adding query parameters.
* Most of this functionality is intended for cases when you only want to add parameters when they are not null (or not
* blank in the case of Strings). If you want a query parameter to be added regardless of whether a value is present
* or not, use the regular {@link WebTarget#queryParam(String, Object...) queryParam} method in {@code WebTarget}.
*
* The methods provided by this helper class allow you to either require query parameters or include them only when
* they have a value. When you require a query parameter, an {@link IllegalArgumentException} is thrown when
* a caller does not supply a name or value. Other methods allow you to optionally include one or more query
* parameters, as well as add them from a {@link Map} or a {@link MultivaluedMap}, such that only non-null/non-blank
* values are added.
*
* Usage example (assuming {@link WebTargetClientHelper#withClient(Client) withClient} is statically imported):
*
* This class implements {@link WebTarget}, and overridden methods return {@link WebTargetHelper}, so you can chain
* methods as you normally would. For example, using {@link #withWebTarget(WebTarget) withWebTarget}:
*
*/
@Beta
public class WebTargetHelper implements WebTarget {
private final WebTarget webTarget;
/**
* Package-private constructor. Used by {@link WebTargetClientHelper}.
*
* @param webTarget the WebTarget to wrap
*/
WebTargetHelper(WebTarget webTarget) {
this.webTarget = requireNotNull(webTarget, "webTarget must not be null");
}
/**
* @return the wrapped WebTarget
*/
@VisibleForTesting
WebTarget wrapped() {
return webTarget;
}
/**
* Convert the current state contained in this helper to a new {@link WebTarget} instance.
*
* @return a new WebTarget instance
*/
public WebTarget toWebTarget() {
return webTarget.path("");
}
/**
* Create a new instance with the given {@link WebTarget}.
*
* @param webTarget the WebTarget to use
* @return a new instance
*/
public static WebTargetHelper withWebTarget(WebTarget webTarget) {
return new WebTargetHelper(webTarget);
}
/**
* Add the required query parameter.
*
* @param name the parameter name
* @param value the parameter value
* @return a new instance
* @throws IllegalArgumentException if name is blank or value is null
*/
public WebTargetHelper queryParamRequireNotNull(String name, Object value) {
checkArgumentNotBlank(name, "name cannot be blank");
checkArgumentNotNull(value, "value cannot be null for parameter %s", name);
var newWebTarget = webTarget.queryParam(name, value);
return new WebTargetHelper(newWebTarget);
}
/**
* Add the given query parameter only if {@code name} is not blank and {@code value} is not null.
*
* @param name the parameter name
* @param value the parameter value
* @return a new instance if name and value are not blank, otherwise this instance
*/
public WebTargetHelper queryParamIfNotNull(String name, Object value) {
if (isBlank(name) || isNull(value)) {
return this;
}
var newWebTarget = this.webTarget.queryParam(name, value);
return new WebTargetHelper(newWebTarget);
}
/**
* Adds any non-null values to the given query parameter. If {@code name} is blank, this is a no-op.
*
* @param name the parameter name
* @param values one or more parameter values
* @return a new instance if name is not blank and values is not null or empty, otherwise this instance
*/
public WebTargetHelper queryParamFilterNotNull(String name, Object... values) {
if (isBlank(name) || isNullOrEmpty(values)) {
return this;
}
return queryParamFilterNotNull(name, Arrays.stream(values));
}
/**
* Adds any non-null values to the given query parameter. If {@code name} is blank, this is a no-op.
*
* @param name the parameter name
* @param values one or more parameter values
* @return a new instance if name is not blank and values is not null or empty, otherwise this instance
*/
public WebTargetHelper queryParamFilterNotNull(String name, List