Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.io.BaseEncoding;
import com.jivesoftware.os.jive.utils.logger.MetricLogger;
import com.jivesoftware.os.jive.utils.logger.MetricLoggerFactory;
import com.jivesoftware.os.tasmo.id.Id;
Expand All @@ -31,6 +32,7 @@ public class JsonEventConventions {

private static final MetricLogger LOG = MetricLoggerFactory.getLogger();
private static final ObjectMapper mapper = new ObjectMapper();
private static final BaseEncoding coder = BaseEncoding.base32().lowerCase().omitPadding();

public JsonEventConventions() {
}
Expand Down Expand Up @@ -84,7 +86,7 @@ public Id getInstanceId(ObjectNode eventNode, String className) {
return null;
}
try {
return new Id(got.textValue());
return new Id(coder.decode(getTextValue(got)));
} catch (Exception ex) {
LOG.debug("Failed to get instanceId for className '" + className + "': " + eventNode, ex);
return null;
Expand Down Expand Up @@ -159,14 +161,21 @@ public JsonNode getInstanceField(ObjectNode eventNode, String className, String
}
}

private String getTextValue(JsonNode got) {
String gotTextValue = got.textValue();
if (gotTextValue == null || gotTextValue.length() == 0) {
throw new IllegalArgumentException("stringForm can not be null and must be at least 1 or more chars." + gotTextValue);
}
return gotTextValue;
}

private Id getId(ObjectNode objectNode, String fieldName) {
JsonNode got = objectNode.get(fieldName);
if (got == null || got.isNull()) {
return null;
}
try {
return new Id(got.textValue());
return new Id(coder.decode(getTextValue(got)));
} catch (Exception ex) {
LOG.debug("Failed to get objectId for field " + fieldName + ": " + objectNode, ex);
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonValue;
import com.google.common.io.BaseEncoding;

import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import java.util.Arrays;

Expand All @@ -23,7 +25,8 @@ public class Id implements Comparable<Id> {

@JsonCreator
public static Id idFromJsonObject(@JsonProperty("id") String stringForm) {
return new Id(stringForm);
byte [] id = coder.decode(stringForm);
return (id.length == 8) ? new Id(id) : new Id(stringForm, true);
}

public Id(long id) {
Expand All @@ -37,11 +40,29 @@ public Id(byte[] id) {
this.id = id;
}

@JsonCreator
@Deprecated
public Id(String stringForm) {
if (stringForm == null || stringForm.length() == 0) {
throw new IllegalArgumentException("stringForm can not be null and must be at least 1 or more chars." + stringForm);
}

if(NULL.toStringForm().equals(stringForm)) {
this.id = coder.decode(stringForm);
} else {
byte[] decodedString = coder.decode(stringForm);
if (decodedString.length < 8) decodedString = Arrays.copyOf(decodedString, 8);
ByteBuffer buffer = ByteBuffer.wrap(decodedString);
this.id = ByteBuffer.allocate(8).putLong(buffer.getLong()).array();
}
}

protected Id(String stringForm, boolean legacy) {
if (!legacy) {
throw new IllegalArgumentException("this private constructor is for legacy purposes only");
}
if (stringForm == null || stringForm.length() == 0) {
throw new IllegalArgumentException("stringForm can not be null and must be at least 1 or more chars." + stringForm);
}
this.id = coder.decode(stringForm);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Random;

import com.google.common.io.BaseEncoding;
import org.testng.Assert;
import org.testng.annotations.Test;

import static org.testng.FileAssert.fail;


public class IdTest {

private static final ObjectMapper MAPPER = new ObjectMapper();
private static final BaseEncoding CODER = BaseEncoding.base32().lowerCase().omitPadding();

@Test
public void idStringTest() throws Exception {
Expand Down Expand Up @@ -62,4 +67,34 @@ public void mapsFromObject() throws Exception {

Assert.assertEquals(MAPPER.readValue(json, Id.class), new Id(2882));
}

@Test
public void testStringConstructorReturnsLong() throws Exception {
Id stringId = new Id("eight");
Assert.assertTrue(isLong(stringId.toStringForm()));

Id fromStringForm = new Id(stringId.toStringForm());
Assert.assertEquals(stringId, fromStringForm);
}

@Test
public void mapsFromLegacyObject() throws Exception {
Id legacy = new Id("eight", true);
String json = "{ \"id\": \"" + legacy.toStringForm() + "\"}";
Assert.assertEquals(MAPPER.readValue(json, Id.class), legacy);
}

@Test
public void test() {
Id numberId = new Id(1);
Id stringFormId = new Id(numberId.toStringForm());

Assert.assertEquals(numberId, stringFormId);
Assert.assertEquals(numberId.toStringForm(), stringFormId.toStringForm());
}

private static boolean isLong(String stringForm) {
byte [] id = CODER.decode(stringForm);
return (id.length == 8);
}
}