Skip to content
This repository has been archived by the owner on Jul 27, 2023. It is now read-only.

Add possibility to read/write KV values with different charset #283

Merged
merged 2 commits into from
Dec 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 69 additions & 4 deletions src/main/java/com/orbitz/consul/KeyValueClient.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.orbitz.consul;

import com.fasterxml.jackson.core.JsonProcessingException;

import java.nio.charset.Charset;
import java.util.Optional;
import com.google.common.collect.ImmutableMap;
import com.google.common.primitives.UnsignedLongs;
Expand Down Expand Up @@ -262,8 +264,22 @@ public void getValues(String key, QueryOptions queryOptions, ConsulResponseCallb
* {@link Optional#empty()}
*/
public Optional<String> getValueAsString(String key) {
return getValueAsString(key, Charset.defaultCharset());
}

/**
* Retrieves a string value for a specific key from the key/value store.
*
* GET /v1/kv/{key}
*
* @param key The key to retrieve.
* @param charset The charset of the value
* @return An {@link Optional} containing the value as a string or
* {@link Optional#empty()}
*/
public Optional<String> getValueAsString(String key, Charset charset) {
for (Value v: getValue(key).map(Collections::singleton).orElse(Collections.emptySet())) {
return v.getValueAsString();
return v.getValueAsString(charset);
}
return Optional.empty();
}
Expand All @@ -278,11 +294,25 @@ public Optional<String> getValueAsString(String key) {
* @return A list of zero to many string values.
*/
public List<String> getValuesAsString(String key) {
return getValuesAsString(key, Charset.defaultCharset());
}

/**
* Retrieves a list of string values for a specific key from the key/value
* store.
*
* GET /v1/kv/{key}?recurse
*
* @param key The key to retrieve.
* @param charset The charset of the value
* @return A list of zero to many string values.
*/
public List<String> getValuesAsString(String key, Charset charset) {
List<String> result = new ArrayList<String>();

for(Value value : getValues(key)) {
if (value.getValueAsString().isPresent()) {
result.add(value.getValueAsString().get());
if (value.getValueAsString(charset).isPresent()) {
result.add(value.getValueAsString(charset).get());
}
}

Expand Down Expand Up @@ -310,6 +340,17 @@ public boolean putValue(String key, String value) {
return putValue(key, value, 0L, PutOptions.BLANK);
}

/**
* Puts a value into the key/value store.
*
* @param key The key to use as index.
* @param value The value to index.
* @return <code>true</code> if the value was successfully indexed.
*/
public boolean putValue(String key, String value, Charset charset) {
return putValue(key, value, 0L, PutOptions.BLANK, charset);
}

/**
* Puts a value into the key/value store.
*
Expand All @@ -322,6 +363,18 @@ public boolean putValue(String key, String value, long flags) {
return putValue(key, value, flags, PutOptions.BLANK);
}

/**
* Puts a value into the key/value store.
*
* @param key The key to use as index.
* @param value The value to index.
* @param flags The flags for this key.
* @return <code>true</code> if the value was successfully indexed.
*/
public boolean putValue(String key, String value, long flags, Charset charset) {
return putValue(key, value, flags, PutOptions.BLANK, charset);
}

/**
* Puts a value into the key/value store.
*
Expand All @@ -331,6 +384,18 @@ public boolean putValue(String key, String value, long flags) {
* @return <code>true</code> if the value was successfully indexed.
*/
public boolean putValue(String key, String value, long flags, PutOptions putOptions) {
return putValue(key, value, flags, putOptions, Charset.defaultCharset());
}

/**
* Puts a value into the key/value store.
*
* @param key The key to use as index.
* @param value The value to index.
* @param putOptions PUT options (e.g. wait, acquire).
* @return <code>true</code> if the value was successfully indexed.
*/
public boolean putValue(String key, String value, long flags, PutOptions putOptions, Charset charset) {

checkArgument(StringUtils.isNotEmpty(key), "Key must be defined");
Map<String, Object> query = putOptions.toQuery();
Expand All @@ -344,7 +409,7 @@ public boolean putValue(String key, String value, long flags, PutOptions putOpti
query));
} else {
return extract(api.putValue(trimLeadingSlash(key),
RequestBody.create(MediaType.parse("text/plain"), value), query));
RequestBody.create(MediaType.parse("text/plain; charset=" + charset.name()), value), query));
}
}

Expand Down
12 changes: 10 additions & 2 deletions src/main/java/com/orbitz/consul/model/kv/Value.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import java.util.Optional;
import com.google.common.io.BaseEncoding;
import com.orbitz.consul.util.UnsignedLongDeserializer;

import java.nio.charset.Charset;
import java.util.Optional;

@org.immutables.value.Value.Immutable
@JsonDeserialize(as = ImmutableValue.class)
@JsonSerialize(as = ImmutableValue.class)
Expand Down Expand Up @@ -40,10 +42,16 @@ public abstract class Value {
@JsonIgnore
@org.immutables.value.Value.Lazy
public Optional<String> getValueAsString() {
return getValueAsString(Charset.defaultCharset());
}

@JsonIgnore
@org.immutables.value.Value.Lazy
public Optional<String> getValueAsString(Charset charset) {

if (getValue().isPresent()) {
return Optional.of(
new String(BaseEncoding.base64().decode(getValue().get()))
new String(BaseEncoding.base64().decode(getValue().get()), charset)
);
} else {
return Optional.empty();
Expand Down
70 changes: 70 additions & 0 deletions src/test/java/com/orbitz/consul/KeyValueTests.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.orbitz.consul;

import java.nio.charset.Charset;
import java.util.Optional;
import com.orbitz.consul.async.ConsulResponseCallback;
import com.orbitz.consul.model.ConsulResponse;
Expand All @@ -11,6 +12,7 @@
import com.orbitz.consul.model.session.SessionCreatedResponse;
import com.orbitz.consul.option.ImmutableDeleteOptions;
import com.orbitz.consul.option.ImmutableDeleteOptions.Builder;
import com.orbitz.consul.option.PutOptions;
import com.orbitz.consul.option.QueryOptions;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.RandomStringUtils;
Expand All @@ -32,6 +34,7 @@
import static org.junit.Assert.fail;

public class KeyValueTests extends BaseIntegrationTest {
private static final Charset TEST_CHARSET = Charset.forName("IBM297");

@Test
public void shouldPutAndReceiveString() throws UnknownHostException {
Expand All @@ -43,6 +46,16 @@ public void shouldPutAndReceiveString() throws UnknownHostException {
assertEquals(value, keyValueClient.getValueAsString(key).get());
}

@Test
public void shouldPutAndReceiveStringWithAnotherCharset() throws UnknownHostException {
KeyValueClient keyValueClient = client.keyValueClient();
String key = UUID.randomUUID().toString();
String value = UUID.randomUUID().toString();

assertTrue(keyValueClient.putValue(key, value, TEST_CHARSET));
assertEquals(value, keyValueClient.getValueAsString(key, TEST_CHARSET).get());
}

@Test
public void shouldPutAndReceiveValue() throws UnknownHostException {
KeyValueClient keyValueClient = client.keyValueClient();
Expand All @@ -56,6 +69,19 @@ public void shouldPutAndReceiveValue() throws UnknownHostException {

}

@Test
public void shouldPutAndReceiveValueWithAnotherCharset() throws UnknownHostException {
KeyValueClient keyValueClient = client.keyValueClient();
String key = UUID.randomUUID().toString();
String value = UUID.randomUUID().toString();

assertTrue(keyValueClient.putValue(key, value, TEST_CHARSET));
Value received = keyValueClient.getValue(key).get();
assertEquals(value, received.getValueAsString(TEST_CHARSET).get());
assertEquals(0L, received.getFlags());

}

@Test
public void shouldPutAndReceiveWithFlags() throws UnknownHostException {
KeyValueClient keyValueClient = client.keyValueClient();
Expand All @@ -70,6 +96,20 @@ public void shouldPutAndReceiveWithFlags() throws UnknownHostException {

}

@Test
public void shouldPutAndReceiveWithFlagsAndCharset() throws UnknownHostException {
KeyValueClient keyValueClient = client.keyValueClient();
String key = UUID.randomUUID().toString();
String value = UUID.randomUUID().toString();
long flags = UUID.randomUUID().getMostSignificantBits();

assertTrue(keyValueClient.putValue(key, value, flags, TEST_CHARSET));
Value received = keyValueClient.getValue(key).get();
assertEquals(value, received.getValueAsString(TEST_CHARSET).get());
assertEquals(flags, received.getFlags());

}

@Test
public void putNullValue() {

Expand All @@ -82,6 +122,18 @@ public void putNullValue() {
assertFalse(received.getValue().isPresent());
}

@Test
public void putNullValueWithAnotherCharset() {

KeyValueClient keyValueClient = client.keyValueClient();
String key = UUID.randomUUID().toString();

assertTrue(keyValueClient.putValue(key, null, 0, PutOptions.BLANK, TEST_CHARSET));

Value received = keyValueClient.getValue(key).get();
assertFalse(received.getValue().isPresent());
}

@Test
public void shouldPutAndReceiveStrings() throws UnknownHostException {
KeyValueClient keyValueClient = client.keyValueClient();
Expand All @@ -100,6 +152,24 @@ public void shouldPutAndReceiveStrings() throws UnknownHostException {
}, new HashSet<String>(keyValueClient.getValuesAsString(key)));
}

@Test
public void shouldPutAndReceiveStringsWithAnotherCharset() throws UnknownHostException {
KeyValueClient keyValueClient = client.keyValueClient();
String key = UUID.randomUUID().toString();
String key2 = key + "/" + UUID.randomUUID().toString();
final String value = UUID.randomUUID().toString();
final String value2 = UUID.randomUUID().toString();

assertTrue(keyValueClient.putValue(key, value, TEST_CHARSET));
assertTrue(keyValueClient.putValue(key2, value2, TEST_CHARSET));
assertEquals(new HashSet<String>() {
{
add(value);
add(value2);
}
}, new HashSet<String>(keyValueClient.getValuesAsString(key, TEST_CHARSET)));
}

@Test
public void shouldDelete() throws Exception {
KeyValueClient keyValueClient = client.keyValueClient();
Expand Down