All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
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.
com.github.dockerjava.okhttp.OkHttpWebTarget Maven / Gradle / Ivy
package com.github.dockerjava.okhttp;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.dockerjava.core.InvocationBuilder;
import com.github.dockerjava.core.WebTarget;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.SetMultimap;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import org.apache.commons.lang.StringUtils;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
class OkHttpWebTarget implements WebTarget {
final ObjectMapper objectMapper;
final OkHttpClient okHttpClient;
final HttpUrl baseUrl;
final ImmutableList path;
final SetMultimap queryParams;
OkHttpWebTarget(
ObjectMapper objectMapper,
OkHttpClient okHttpClient,
HttpUrl baseUrl,
ImmutableList path,
SetMultimap queryParams
) {
this.objectMapper = objectMapper;
this.okHttpClient = okHttpClient;
this.baseUrl = baseUrl;
this.path = path;
this.queryParams = queryParams;
}
@Override
public InvocationBuilder request() {
String resource = StringUtils.join(path, "/");
if (!resource.startsWith("/")) {
resource = "/" + resource;
}
HttpUrl.Builder baseUrlBuilder = baseUrl.newBuilder()
.encodedPath(resource);
for (Map.Entry> queryParamEntry : queryParams.asMap().entrySet()) {
String key = queryParamEntry.getKey();
for (String paramValue : queryParamEntry.getValue()) {
baseUrlBuilder.addQueryParameter(key, paramValue);
}
}
return new OkHttpInvocationBuilder(
objectMapper,
okHttpClient,
baseUrlBuilder.build()
);
}
@Override
public OkHttpWebTarget path(String... components) {
ImmutableList newPath = ImmutableList.builder()
.addAll(path)
.add(components)
.build();
return new OkHttpWebTarget(
objectMapper,
okHttpClient,
baseUrl,
newPath,
queryParams
);
}
@Override
public OkHttpWebTarget resolveTemplate(String name, Object value) {
ImmutableList.Builder newPath = ImmutableList.builder();
for (String component : path) {
component = component.replaceAll("\\{" + name + "\\}", value.toString());
newPath.add(component);
}
return new OkHttpWebTarget(
objectMapper,
okHttpClient,
baseUrl,
newPath.build(),
queryParams
);
}
@Override
public OkHttpWebTarget queryParam(String name, Object value) {
if (value == null) {
return this;
}
SetMultimap newQueryParams = HashMultimap.create(queryParams);
newQueryParams.put(name, value.toString());
return new OkHttpWebTarget(
objectMapper,
okHttpClient,
baseUrl,
path,
newQueryParams
);
}
@Override
public OkHttpWebTarget queryParamsSet(String name, Set> values) {
SetMultimap newQueryParams = HashMultimap.create(queryParams);
newQueryParams.replaceValues(name, values.stream().filter(Objects::nonNull).map(Object::toString).collect(Collectors.toSet()));
return new OkHttpWebTarget(
objectMapper,
okHttpClient,
baseUrl,
path,
newQueryParams
);
}
@Override
public OkHttpWebTarget queryParamsJsonMap(String name, Map values) {
if (values == null || values.isEmpty()) {
return this;
}
// when param value is JSON string
try {
return queryParam(name, objectMapper.writeValueAsString(values));
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
}