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.ecwid.consul.v1.kv.KeyValueConsulClient Maven / Gradle / Ivy
package com.ecwid.consul.v1.kv;
import java.util.List;
import com.ecwid.consul.ConsulException;
import com.ecwid.consul.SingleUrlParameters;
import com.ecwid.consul.UrlParameters;
import com.ecwid.consul.json.GsonFactory;
import com.ecwid.consul.transport.HttpResponse;
import com.ecwid.consul.transport.TLSConfig;
import com.ecwid.consul.v1.*;
import com.ecwid.consul.v1.kv.model.GetBinaryValue;
import com.ecwid.consul.v1.kv.model.GetValue;
import com.ecwid.consul.v1.kv.model.PutParams;
import com.google.gson.reflect.TypeToken;
/**
* @author Vasily Vasilkov ([email protected] )
*/
public final class KeyValueConsulClient implements KeyValueClient {
private final ConsulRawClient rawClient;
public KeyValueConsulClient(ConsulRawClient rawClient) {
this.rawClient = rawClient;
}
public KeyValueConsulClient() {
this(new ConsulRawClient());
}
public KeyValueConsulClient(String agentHost) {
this(new ConsulRawClient(agentHost));
}
public KeyValueConsulClient(String agentHost, int agentPort) {
this(new ConsulRawClient(agentHost, agentPort));
}
public KeyValueConsulClient(TLSConfig tlsConfig) {
this(new ConsulRawClient(tlsConfig));
}
public KeyValueConsulClient(String agentHost, TLSConfig tlsConfig) {
this(new ConsulRawClient(agentHost, tlsConfig));
}
public KeyValueConsulClient(String agentHost, int agentPort, TLSConfig tlsConfig) {
this(new ConsulRawClient(agentHost, agentPort, tlsConfig));
}
@Override
public Response getKVValue(String key) {
return getKVValue(key, QueryParams.DEFAULT);
}
@Override
public Response getKVValue(String key, String token) {
return getKVValue(key, token, QueryParams.DEFAULT);
}
@Override
public Response getKVValue(String key, QueryParams queryParams) {
return getKVValue(key, null, queryParams);
}
@Override
public Response getKVValue(String key, String token, QueryParams queryParams) {
UrlParameters tokenParams = token != null ? new SingleUrlParameters("token", token) : null;
HttpResponse httpResponse = rawClient.makeGetRequest("/v1/kv/" + key, tokenParams, queryParams);
if (httpResponse.getStatusCode() == 200) {
List value = GsonFactory.getGson().fromJson(httpResponse.getContent(), new TypeToken>() {
}.getType());
if (value.size() == 0) {
return new Response(null, httpResponse);
} else if (value.size() == 1) {
return new Response(value.get(0), httpResponse);
} else {
throw new ConsulException("Strange response (list size=" + value.size() + ")");
}
} else if (httpResponse.getStatusCode() == 404) {
return new Response(null, httpResponse);
} else {
throw new OperationException(httpResponse);
}
}
@Override
public Response getKVBinaryValue(String key) {
return getKVBinaryValue(key, QueryParams.DEFAULT);
}
@Override
public Response getKVBinaryValue(String key, String token) {
return getKVBinaryValue(key, token, QueryParams.DEFAULT);
}
@Override
public Response getKVBinaryValue(String key, QueryParams queryParams) {
return getKVBinaryValue(key, null, queryParams);
}
@Override
public Response getKVBinaryValue(String key, String token, QueryParams queryParams) {
UrlParameters tokenParams = token != null ? new SingleUrlParameters("token", token) : null;
HttpResponse httpResponse = rawClient.makeGetRequest("/v1/kv/" + key, tokenParams, queryParams);
if (httpResponse.getStatusCode() == 200) {
List value = GsonFactory.getGson().fromJson(httpResponse.getContent(), new TypeToken>() {
}.getType());
if (value.size() == 0) {
return new Response(null, httpResponse);
} else if (value.size() == 1) {
return new Response(value.get(0), httpResponse);
} else {
throw new ConsulException("Strange response (list size=" + value.size() + ")");
}
} else if (httpResponse.getStatusCode() == 404) {
return new Response(null, httpResponse);
} else {
throw new OperationException(httpResponse);
}
}
@Override
public Response> getKVValues(String keyPrefix) {
return getKVValues(keyPrefix, QueryParams.DEFAULT);
}
@Override
public Response> getKVValues(String keyPrefix, String token) {
return getKVValues(keyPrefix, token, QueryParams.DEFAULT);
}
@Override
public Response> getKVValues(String keyPrefix, QueryParams queryParams) {
return getKVValues(keyPrefix, null, queryParams);
}
@Override
public Response> getKVValues(String keyPrefix, String token, QueryParams queryParams) {
UrlParameters recurseParam = new SingleUrlParameters("recurse");
UrlParameters tokenParam = token != null ? new SingleUrlParameters("token", token) : null;
HttpResponse httpResponse = rawClient.makeGetRequest("/v1/kv/" + keyPrefix, recurseParam, tokenParam, queryParams);
if (httpResponse.getStatusCode() == 200) {
List value = GsonFactory.getGson().fromJson(httpResponse.getContent(), new TypeToken>() {
}.getType());
return new Response>(value, httpResponse);
} else if (httpResponse.getStatusCode() == 404) {
return new Response>(null, httpResponse);
} else {
throw new OperationException(httpResponse);
}
}
@Override
public Response> getKVBinaryValues(String keyPrefix) {
return getKVBinaryValues(keyPrefix, QueryParams.DEFAULT);
}
@Override
public Response> getKVBinaryValues(String keyPrefix, String token) {
return getKVBinaryValues(keyPrefix, token, QueryParams.DEFAULT);
}
@Override
public Response> getKVBinaryValues(String keyPrefix, QueryParams queryParams) {
return getKVBinaryValues(keyPrefix, null, queryParams);
}
@Override
public Response> getKVBinaryValues(String keyPrefix, String token, QueryParams queryParams) {
UrlParameters recurseParam = new SingleUrlParameters("recurse");
UrlParameters tokenParam = token != null ? new SingleUrlParameters("token", token) : null;
HttpResponse httpResponse = rawClient.makeGetRequest("/v1/kv/" + keyPrefix, recurseParam, tokenParam, queryParams);
if (httpResponse.getStatusCode() == 200) {
List value = GsonFactory.getGson().fromJson(httpResponse.getContent(), new TypeToken>() {
}.getType());
return new Response>(value, httpResponse);
} else if (httpResponse.getStatusCode() == 404) {
return new Response>(null, httpResponse);
} else {
throw new OperationException(httpResponse);
}
}
@Override
public Response> getKVKeysOnly(String keyPrefix) {
return getKVKeysOnly(keyPrefix, QueryParams.DEFAULT);
}
@Override
public Response> getKVKeysOnly(String keyPrefix, String separator, String token) {
return getKVKeysOnly(keyPrefix, separator, token, QueryParams.DEFAULT);
}
@Override
public Response> getKVKeysOnly(String keyPrefix, QueryParams queryParams) {
return getKVKeysOnly(keyPrefix, null, null, queryParams);
}
@Override
public Response> getKVKeysOnly(String keyPrefix, String separator, String token, QueryParams queryParams) {
UrlParameters keysParam = new SingleUrlParameters("keys");
UrlParameters separatorParam = separator != null ? new SingleUrlParameters("separator", separator) : null;
UrlParameters tokenParam = token != null ? new SingleUrlParameters("token", token) : null;
HttpResponse httpResponse = rawClient.makeGetRequest("/v1/kv/" + keyPrefix, keysParam, separatorParam, tokenParam, queryParams);
if (httpResponse.getStatusCode() == 200) {
List value = GsonFactory.getGson().fromJson(httpResponse.getContent(), new TypeToken>() {
}.getType());
return new Response>(value, httpResponse);
} else if (httpResponse.getStatusCode() == 404) {
return new Response>(null, httpResponse);
} else {
throw new OperationException(httpResponse);
}
}
@Override
public Response setKVValue(String key, String value) {
return setKVValue(key, value, QueryParams.DEFAULT);
}
@Override
public Response setKVValue(String key, String value, PutParams putParams) {
return setKVValue(key, value, putParams, QueryParams.DEFAULT);
}
@Override
public Response setKVValue(String key, String value, String token, PutParams putParams) {
return setKVValue(key, value, token, putParams, QueryParams.DEFAULT);
}
@Override
public Response setKVValue(String key, String value, QueryParams queryParams) {
return setKVValue(key, value, null, null, queryParams);
}
@Override
public Response setKVValue(String key, String value, PutParams putParams, QueryParams queryParams) {
return setKVValue(key, value, null, putParams, queryParams);
}
@Override
public Response setKVValue(String key, String value, String token, PutParams putParams, QueryParams queryParams) {
UrlParameters tokenParam = token != null ? new SingleUrlParameters("token", token) : null;
HttpResponse httpResponse = rawClient.makePutRequest("/v1/kv/" + key, value, putParams, tokenParam, queryParams);
if (httpResponse.getStatusCode() == 200) {
boolean result = GsonFactory.getGson().fromJson(httpResponse.getContent(), boolean.class);
return new Response(result, httpResponse);
} else {
throw new OperationException(httpResponse);
}
}
@Override
public Response setKVBinaryValue(String key, byte[] value) {
return setKVBinaryValue(key, value, QueryParams.DEFAULT);
}
@Override
public Response setKVBinaryValue(String key, byte[] value, PutParams putParams) {
return setKVBinaryValue(key, value, putParams, QueryParams.DEFAULT);
}
@Override
public Response setKVBinaryValue(String key, byte[] value, String token, PutParams putParams) {
return setKVBinaryValue(key, value, token, putParams, QueryParams.DEFAULT);
}
@Override
public Response setKVBinaryValue(String key, byte[] value, QueryParams queryParams) {
return setKVBinaryValue(key, value, null, null, queryParams);
}
@Override
public Response setKVBinaryValue(String key, byte[] value, PutParams putParams, QueryParams queryParams) {
return setKVBinaryValue(key, value, null, putParams, queryParams);
}
@Override
public Response setKVBinaryValue(String key, byte[] value, String token, PutParams putParams, QueryParams queryParams) {
Request request = Request.Builder.newBuilder()
.setEndpoint("/v1/kv/" + key)
.setToken(token)
.addUrlParameter(queryParams)
.addUrlParameter(putParams)
.setBinaryContent(value)
.build();
HttpResponse httpResponse = rawClient.makePutRequest(request);
if (httpResponse.getStatusCode() == 200) {
boolean result = GsonFactory.getGson().fromJson(httpResponse.getContent(), boolean.class);
return new Response(result, httpResponse);
} else {
throw new OperationException(httpResponse);
}
}
@Override
public Response deleteKVValue(String key) {
return deleteKVValue(key, QueryParams.DEFAULT);
}
@Override
public Response deleteKVValue(String key, String token) {
return deleteKVValue(key, token, QueryParams.DEFAULT);
}
@Override
public Response deleteKVValue(String key, QueryParams queryParams) {
return deleteKVValue(key, null, queryParams);
}
@Override
public Response deleteKVValue(String key, String token, QueryParams queryParams) {
Request request = Request.Builder.newBuilder()
.setEndpoint("/v1/kv/" + key)
.setToken(token)
.addUrlParameter(queryParams)
.build();
HttpResponse httpResponse = rawClient.makeDeleteRequest(request);
if (httpResponse.getStatusCode() == 200) {
return new Response<>(null, httpResponse);
} else {
throw new OperationException(httpResponse);
}
}
@Override
public Response deleteKVValues(String key) {
return deleteKVValues(key, QueryParams.DEFAULT);
}
@Override
public Response deleteKVValues(String key, String token) {
return deleteKVValues(key, token, QueryParams.DEFAULT);
}
@Override
public Response deleteKVValues(String key, QueryParams queryParams) {
return deleteKVValues(key, null, queryParams);
}
@Override
public Response deleteKVValues(String key, String token, QueryParams queryParams) {
UrlParameters recurseParam = new SingleUrlParameters("recurse");
Request request = Request.Builder.newBuilder()
.setEndpoint("/v1/kv/" + key)
.setToken(token)
.addUrlParameter(recurseParam)
.addUrlParameter(queryParams)
.build();
HttpResponse httpResponse = rawClient.makeDeleteRequest(request);
if (httpResponse.getStatusCode() == 200) {
return new Response<>(null, httpResponse);
} else {
throw new OperationException(httpResponse);
}
}
}