Skip to content

Commit

Permalink
Avoid NullPointerException caused by missing DittoHeaders.
Browse files Browse the repository at this point in the history
Signed-off-by: Juergen Fickel <juergen.fickel@bosch-si.com>
  • Loading branch information
Juergen Fickel committed Jun 5, 2018
1 parent 506a4e9 commit 1c6194f
Showing 1 changed file with 17 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.util.function.BiFunction;
import java.util.function.Function;

import javax.annotation.Nullable;

import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonFieldDefinition;
import org.eclipse.ditto.json.JsonObject;
Expand Down Expand Up @@ -90,15 +92,8 @@ public String manifest(final Object o) {
@Override
public byte[] toBinary(final Object object) {
if (object instanceof Jsonifiable) {
final DittoHeaders dittoHeaders;
if (object instanceof WithDittoHeaders) {
dittoHeaders = ((WithDittoHeaders) object).getDittoHeaders();
} else {
dittoHeaders = DittoHeaders.empty();
}

final JsonObjectBuilder jsonObjectBuilder = JsonObject.newBuilder();

final DittoHeaders dittoHeaders = getDittoHeadersOrEmpty(object);
jsonObjectBuilder.set(JSON_DITTO_HEADERS, dittoHeaders.toJson());

final JsonValue jsonValue;
Expand All @@ -118,13 +113,24 @@ public byte[] toBinary(final Object object) {
.toString()
.getBytes(UTF8_CHARSET);
} else {
LOG.error("Could not serialize class '{}' as it does not implement '{}'", object.getClass(),
LOG.error("Could not serialize class <{}> as it does not implement <{}>!", object.getClass(),
Jsonifiable.WithPredicate.class);
final String error = new NotSerializableException(object.getClass().getName()).getMessage();
return error.getBytes(UTF8_CHARSET);
}
}

private static DittoHeaders getDittoHeadersOrEmpty(final Object object) {
if (object instanceof WithDittoHeaders) {
@Nullable final DittoHeaders dittoHeaders = ((WithDittoHeaders) object).getDittoHeaders();
if (null != dittoHeaders) {
return dittoHeaders;
}
LOG.warn("Object <{}> did not contain DittoHeaders although it should! Using empty DittoHeaders instead.");
}
return DittoHeaders.empty();
}

@Override
public Object fromBinary(final byte[] bytes, final String manifest) {
final String json = new String(bytes, UTF8_CHARSET);
Expand All @@ -140,7 +146,7 @@ private Jsonifiable tryToCreateKnownJsonifiableFrom(final String manifest, final
try {
return createJsonifiableFrom(manifest, json);
} catch (final DittoRuntimeException | JsonRuntimeException e) {
LOG.error("Got {} during fromBinary(byte[],String) deserialization for manifest '{}' and JSON: '{}'",
LOG.error("Got <{}> during fromBinary(byte[],String) deserialization for manifest <{}> and JSON: '{}'",
e.getClass().getSimpleName(), manifest, json, e);
throw new NotSerializableException(manifest);
}
Expand All @@ -151,7 +157,7 @@ private Jsonifiable createJsonifiableFrom(final String manifest, final String js

final BiFunction<JsonObject, DittoHeaders, Jsonifiable> mappingFunction = mappingStrategies.get(manifest);
if (null == mappingFunction) {
LOG.warn("No strategy found to map manifest '{}' to a Jsonifiable.WithPredicate!", manifest);
LOG.warn("No strategy found to map manifest <{}> to a Jsonifiable.WithPredicate!", manifest);
throw new NotSerializableException(manifest);
}

Expand Down

0 comments on commit 1c6194f

Please sign in to comment.