com.github.dockerjava.netty.WebTarget Maven / Gradle / Ivy
package com.github.dockerjava.netty;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
/**
* This class is basically a replacement of javax.ws.rs.client.WebTarget to allow simpler migration of JAX-RS code to a netty based
* implementation.
*
* @author Marcus Linke
*/
public class WebTarget {
private ChannelProvider channelProvider;
private List path = new ArrayList();
private Map queryParams = new HashMap();
private static final String PATH_SEPARATOR = "/";
public WebTarget(ChannelProvider channelProvider) {
this.channelProvider = channelProvider;
}
public WebTarget path(String... components) {
for (String component : components) {
path.addAll(Arrays.asList(StringUtils.split(component, PATH_SEPARATOR)));
}
return this;
}
public InvocationBuilder request() {
String resource = PATH_SEPARATOR + StringUtils.join(path, PATH_SEPARATOR);
List params = new ArrayList();
for (Map.Entry entry : queryParams.entrySet()) {
params.add(entry.getKey() + "=" + entry.getValue());
}
if (!params.isEmpty()) {
resource = resource + "?" + StringUtils.join(params, "&");
}
return new InvocationBuilder(channelProvider, resource);
}
public WebTarget resolveTemplate(String name, Object value) {
List newPath = new ArrayList();
for (String component : path) {
component = component.replaceAll("\\{" + name + "\\}", value.toString());
newPath.add(component);
}
path = newPath;
return this;
}
public WebTarget queryParam(String name, Object value) {
if (value != null) {
queryParams.put(name, value.toString());
}
return this;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy