Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.fizzed.crux.jackson;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.deser.ContextualDeserializer;

import java.io.IOException;
import java.util.UUID;

public class JavaUUIDDeserializer extends JsonDeserializer<UUID> implements ContextualDeserializer {

private final JavaUUIDStyle style;

public JavaUUIDDeserializer() {
this(JavaUUIDStyle.DEFAULT);
}

public JavaUUIDDeserializer(JavaUUIDStyle style) {
this.style = style;
}

@Override
public UUID deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
final String value = p.getValueAsString();

if (value == null || value.trim().isEmpty()) {
return null;
}

// Re-insert hyphens to satisfy the standard UUID format
final String uuidStr;

if (style == JavaUUIDStyle.DEFAULT) {
uuidStr = value;
} else if (style == JavaUUIDStyle.STRIPPED) {
if (value.length() != 32) {
throw new IllegalArgumentException("MD5 hex string must be exactly 32 characters long");
}

uuidStr = value.substring(0, 8) + "-" +
value.substring(8, 12) + "-" +
value.substring(12, 16) + "-" +
value.substring(16, 20) + "-" +
value.substring(20, 32);
} else {
throw new IllegalArgumentException("Unknown UUID style: " + style);
}

try {
return UUID.fromString(uuidStr);
} catch (IllegalArgumentException e) {
return (UUID) ctxt.handleWeirdStringValue(UUID.class, value, "Invalid UUID format");
}
}

@Override
public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanProperty property) {
if (property != null) {
final JavaUUIDFormat format = property.getAnnotation(JavaUUIDFormat.class);
if (format != null) {
// Return a version of the deserializer tailored to this field's annotation
return new JavaUUIDDeserializer(format.value());
}
}
return this; // Return default if no annotation is found
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.fizzed.crux.jackson;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface JavaUUIDFormat {

JavaUUIDStyle value();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.fizzed.crux.jackson;

import com.fasterxml.jackson.databind.module.SimpleModule;

import java.util.UUID;

public class JavaUUIDModule extends SimpleModule {

public JavaUUIDModule() {
this(JavaUUIDStyle.DEFAULT);
}

public JavaUUIDModule(JavaUUIDStyle defaultStyle) {
this.addSerializer(UUID.class, new JavaUUIDSerializer(defaultStyle));
this.addDeserializer(UUID.class, new JavaUUIDDeserializer(defaultStyle));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.fizzed.crux.jackson;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.ContextualSerializer;

import java.io.IOException;
import java.util.UUID;

public class JavaUUIDSerializer extends JsonSerializer<UUID> implements ContextualSerializer {

private final JavaUUIDStyle style;

public JavaUUIDSerializer() {
this(JavaUUIDStyle.DEFAULT);
}

public JavaUUIDSerializer(JavaUUIDStyle style) {
this.style = style;
}


@Override
public void serialize(UUID value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
if (value == null) {
gen.writeNull();
return;
}

final String uuidStr = value.toString();

switch (this.style) {
case STRIPPED:
gen.writeString(uuidStr.replace("-", ""));
break;
case DEFAULT:
default:
gen.writeString(uuidStr);
break;
}
}

@Override
public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property) {
if (property != null) {
JavaUUIDFormat format = property.getAnnotation(JavaUUIDFormat.class);
if (format != null) {
// Return a new instance configured with the specific enum value
return new JavaUUIDSerializer(format.value());
}
}
return this; // Use default if no annotation is present
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.fizzed.crux.jackson;

public enum JavaUUIDStyle {

DEFAULT,
STRIPPED;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package com.fizzed.crux.jackson;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;

import java.util.UUID;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

public class JavaUUIDModuleTest {

@Test
public void serializeDefaultStyle() throws Exception {
final ObjectMapper objectMapper = new ObjectMapper()
.registerModule(new JavaUUIDModule());

final UUID uuid1 = UUID.fromString("185F18D6-DEBC-49dd-9f62-a8bf2831a868");

assertThat(objectMapper.writeValueAsString(uuid1), is("\"185f18d6-debc-49dd-9f62-a8bf2831a868\""));
}

@Test
public void serializeStrippedStyle() throws Exception {
final ObjectMapper objectMapper = new ObjectMapper()
.registerModule(new JavaUUIDModule(JavaUUIDStyle.STRIPPED));

final UUID uuid1 = UUID.fromString("185f18d6-debc-49dd-9f62-a8bf2831a868");

assertThat(objectMapper.writeValueAsString(uuid1), is("\"185f18d6debc49dd9f62a8bf2831a868\""));
}

@Test
public void deserializeDefaultStyle() throws Exception {
final ObjectMapper objectMapper = new ObjectMapper()
.registerModule(new JavaUUIDModule());

final UUID uuid1 = UUID.fromString("185F18D6-DEBC-49dd-9f62-a8bf2831a868");

assertThat(objectMapper.readValue("\"185f18d6-debc-49dd-9f62-a8bf2831a868\"", UUID.class), is(uuid1));
}

@Test
public void deserializeStrippedStyle() throws Exception {
final ObjectMapper objectMapper = new ObjectMapper()
.registerModule(new JavaUUIDModule(JavaUUIDStyle.STRIPPED));

final UUID uuid1 = UUID.fromString("185F18D6-DEBC-49dd-9f62-a8bf2831a868");

// fails if not 32 chars long
try {
assertThat(objectMapper.readValue("\"185f18d6-debc-49dd-9f62-a8bf2831a868\"", UUID.class), is(uuid1));
} catch (IllegalArgumentException e) {
// expected
}

assertThat(objectMapper.readValue("\"185f18d6debc49dd9f62a8bf2831a868\"", UUID.class), is(uuid1));
assertThat(objectMapper.readValue("\"185f18D6deBC49dd9f62a8bf2831a868\"", UUID.class), is(uuid1));
}

static public class Widget {
@JavaUUIDFormat(JavaUUIDStyle.STRIPPED)
private UUID uuid;

public UUID getUuid() {
return uuid;
}

public Widget setUuid(UUID uuid) {
this.uuid = uuid;
return this;
}
}

@Test
public void serializeContextualStyle() throws Exception {
final ObjectMapper objectMapper = new ObjectMapper()
.registerModule(new JavaUUIDModule(JavaUUIDStyle.DEFAULT));

final Widget w = new Widget()
.setUuid(UUID.fromString("185f18d6-debc-49dd-9f62-a8bf2831a868"));

assertThat(objectMapper.writeValueAsString(w), is("{\"uuid\":\"185f18d6debc49dd9f62a8bf2831a868\"}"));
}

@Test
public void deserializeContextualStyle() throws Exception {
final ObjectMapper objectMapper = new ObjectMapper()
.registerModule(new JavaUUIDModule(JavaUUIDStyle.DEFAULT));

final Widget w = objectMapper.readValue("{\"uuid\":\"185f18d6debc49dd9f62a8bf2831a868\"}", Widget.class);

assertThat(w.getUuid(), is(UUID.fromString("185f18d6-debc-49dd-9f62-a8bf2831a868")));
}

}
1 change: 0 additions & 1 deletion crux-mediatype/pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.fizzed</groupId>
<artifactId>crux-mediatype</artifactId>
<name>crux-mediatype</name>
<packaging>jar</packaging>
Expand Down
21 changes: 17 additions & 4 deletions crux-okhttp/src/main/java/com/fizzed/crux/okhttp/OkHttpLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,10 @@ public void logRequest(
}

//Slf4jUtil.log(messageLevel, logger, requestStartMessage);
sb.append("\n");

if (logHeaders) {
sb.append("\n");

if (hasRequestBody) {
// Request body headers are only present when installed as a network interceptor. Force
// them to be included (when available) so there values are known.
Expand Down Expand Up @@ -146,7 +147,12 @@ public void logRequest(
}
}
}


// before we log anything, we need to strip any trailing newlines
while (sb.length() > 0 && sb.charAt(sb.length() - 1) == '\n') {
sb.setLength(sb.length() - 1);
}

Slf4jUtil.log(messageLevel, logger, "{}", sb);
}

Expand All @@ -169,9 +175,11 @@ public void logResponse(
.append(response.request().url())
.append(" (").append(tookMs).append("ms")
.append(!logHeaders ? ", " + bodySize + " body" : "")
.append(')').append("\n");
.append(')');

if (logHeaders) {
sb.append("\n");

final Headers headers = response.headers();
for (int i = 0, count = headers.size(); i < count; i++) {
//logHeader(messageLevel, logger, headers, i);
Expand Down Expand Up @@ -245,7 +253,12 @@ public void logResponse(
}
}
}


// before we log anything, we need to strip any trailing newlines
while (sb.length() > 0 && sb.charAt(sb.length() - 1) == '\n') {
sb.setLength(sb.length() - 1);
}

Slf4jUtil.log(messageLevel, logger, "{}", sb);
}

Expand Down
5 changes: 2 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.fizzed</groupId>
<artifactId>crux</artifactId>
<version>1.0.49-SNAPSHOT</version>
<packaging>pom</packaging>

<parent>
<groupId>com.fizzed</groupId>
<artifactId>maven-parent</artifactId>
<version>2.6.0</version>
<version>3.4.0</version>
</parent>

<properties>
Expand Down Expand Up @@ -74,7 +73,7 @@

<dependency>
<groupId>com.fizzed</groupId>
<artifactId>crux-mime</artifactId>
<artifactId>crux-mediatype</artifactId>
<version>${project.version}</version>
</dependency>

Expand Down
Loading