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 @@ -12,11 +12,10 @@
import com.aws.greengrass.logging.api.Logger;
import com.aws.greengrass.logging.impl.LogManager;
import com.aws.greengrass.util.Coerce;
import com.aws.greengrass.util.SerializerFactory;
import com.aws.greengrass.util.Utils;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;

import java.io.IOException;
import java.util.ArrayList;
Expand All @@ -30,8 +29,7 @@

public final class AuthorizationPolicyParser {
private static final Logger logger = LogManager.getLogger(AuthorizationPolicyParser.class);
private static final ObjectMapper OBJECT_MAPPER =
JsonMapper.builder().configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true).build();
private static final ObjectMapper OBJECT_MAPPER = SerializerFactory.getCaseInsensitiveJsonObjectMapper();
/**
* Given a kernel object, construct and return a map of AuthorizationPolicy objects that may exist,
* grouped into lists of the same destination component.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.aws.greengrass.logging.api.Logger;
import com.aws.greengrass.logging.impl.LogManager;
import com.aws.greengrass.util.OrderedExecutorService;
import com.aws.greengrass.util.SerializerFactory;
import com.aws.greengrass.util.Utils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -49,7 +50,7 @@ public class PubSubIPCEventStreamAgent {
private static final String MQTT_SINGLELEVEL_WILDCARD = "+";
private static final String MQTT_MULTILEVEL_WILDCARD = "#";
private static final String GLOB_WILDCARD = "*";
private static final ObjectMapper SERIALIZER = new ObjectMapper();
private static final ObjectMapper SERIALIZER = SerializerFactory.getJsonObjectMapper();
@Getter(AccessLevel.PACKAGE)
private final SubscriptionTrie<SubscriptionCallback> listeners = new SubscriptionTrie<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@
import com.aws.greengrass.util.Coerce;
import com.aws.greengrass.util.CrashableFunction;
import com.aws.greengrass.util.NucleusPaths;
import com.aws.greengrass.util.SerializerFactory;
import com.aws.greengrass.util.Utils;
import com.fasterxml.jackson.core.JsonPointer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.vdurmont.semver4j.Semver;

import java.io.IOException;
Expand Down Expand Up @@ -89,9 +88,7 @@ public class KernelConfigResolver {
Pattern.compile("\\{([.\\w-]+):([.\\w-]+):([^:}]*)}");
// https://tools.ietf.org/html/rfc6901#section-5
private static final String JSON_POINTER_WHOLE_DOC = "";
private static final ObjectMapper MAPPER = new ObjectMapper()
.enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY)
.enable(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS);
private static final ObjectMapper MAPPER = SerializerFactory.getSortedJsonObjectMapper();
// Map from Namespace -> Key -> Function which returns the replacement value
private final Map<String, Map<String, CrashableFunction<ComponentIdentifier, String, IOException>>>
systemParameters = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
import com.aws.greengrass.logging.api.Logger;
import com.aws.greengrass.logging.impl.LogManager;
import com.aws.greengrass.util.Coerce;
import com.aws.greengrass.util.SerializerFactory;
import com.aws.greengrass.util.Utils;
import com.aws.greengrass.util.platforms.Platform;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import software.amazon.awssdk.aws.greengrass.GreengrassCoreIPCService;
import software.amazon.awssdk.aws.greengrass.GreengrassCoreIPCServiceModel;
Expand All @@ -40,9 +40,7 @@
public class IPCEventStreamService implements Startable, Closeable {
public static final long DEFAULT_STREAM_MESSAGE_TIMEOUT_SECONDS = 5;
public static final int DEFAULT_PORT_NUMBER = 8033;
private static final ObjectMapper OBJECT_MAPPER =
new ObjectMapper().configure(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE, false)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
private static final ObjectMapper OBJECT_MAPPER = SerializerFactory.getFailSafeJsonObjectMapper();

public static final String NUCLEUS_DOMAIN_SOCKET_FILEPATH = "AWS_GG_NUCLEUS_DOMAIN_SOCKET_FILEPATH";
public static final String NUCLEUS_DOMAIN_SOCKET_FILEPATH_FOR_COMPONENT =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.aws.greengrass.telemetry.impl.TelemetryLoggerMessage;
import com.aws.greengrass.telemetry.impl.config.TelemetryConfig;
import com.aws.greengrass.util.Coerce;
import com.aws.greengrass.util.SerializerFactory;
import com.aws.greengrass.util.platforms.Platform;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -34,7 +35,7 @@
public class MetricsAggregator {
public static final Logger logger = LogManager.getLogger(MetricsAggregator.class);
protected static final String AGGREGATE_METRICS_FILE = "AggregateMetrics";
private static final ObjectMapper objectMapper = new ObjectMapper();
private static final ObjectMapper objectMapper = SerializerFactory.getJsonObjectMapper();
private final MetricFactory metricFactory = new MetricFactory(AGGREGATE_METRICS_FILE);

/**
Expand Down Expand Up @@ -269,7 +270,7 @@ protected Map<Long, List<AggregatedNamespaceData>> getMetricsToPublish(long last
});

try {
logger.atDebug().kv("metrics", new ObjectMapper().writeValueAsString(aggUploadMetrics))
logger.atDebug().kv("metrics", objectMapper.writeValueAsString(aggUploadMetrics))
.log("Preparing to upload metrics");
} catch (JsonProcessingException e) {
logger.atWarn().setCause(e).log("Could not convert aggregated metrics to json, continuing");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
import com.aws.greengrass.util.DefaultConcurrentHashMap;
import com.aws.greengrass.util.LockFactory;
import com.aws.greengrass.util.LockScope;
import com.aws.greengrass.util.SerializerFactory;
import com.aws.greengrass.util.exceptions.TLSAuthException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.net.httpserver.HttpExchange;
Expand Down Expand Up @@ -63,8 +63,7 @@ public class CredentialRequestHandler implements HttpHandler {
private static final String SESSION_TOKEN_DOWNSTREAM_STR = "Token";
private static final String EXPIRATION_UPSTREAM_STR = "expiration";
private static final String EXPIRATION_DOWNSTREAM_STR = "Expiration";
private static final ObjectMapper OBJECT_MAPPER =
new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);
private static final ObjectMapper OBJECT_MAPPER = SerializerFactory.getStrictJsonObjectMapper();
public static final String AUTH_HEADER = "Authorization";
public static final String IOT_CREDENTIALS_HTTP_VERB = "GET";
public static final String SUPPORTED_REQUEST_VERB = "GET";
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/aws/greengrass/util/Coerce.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
public final class Coerce {
private static final Pattern SEPARATORS = Pattern.compile(" *, *");
private static final Pattern unwrap = Pattern.compile(" *\\[ *(.*) *\\] *");
private static final ObjectMapper MAPPER = new ObjectMapper();
private static final ObjectMapper MAPPER = SerializerFactory.getJsonObjectMapper();

private Coerce() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
public class MqttChunkedPayloadPublisher<T> {
private static final Logger logger = LogManager.getLogger(MqttChunkedPayloadPublisher.class);
private static final String topicKey = "topic";
private static final ObjectMapper SERIALIZER = new ObjectMapper();
private static final ObjectMapper SERIALIZER = SerializerFactory.getJsonObjectMapper();
private final MqttClient mqttClient;
@Setter
private String updateTopic;
Expand Down
45 changes: 45 additions & 0 deletions src/main/java/com/aws/greengrass/util/SerializerFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
package com.aws.greengrass.util;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.json.JsonMapper;

public final class SerializerFactory {

Expand All @@ -15,10 +18,52 @@ public final class SerializerFactory {
new ObjectMapper().configure(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE, false)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

private static final ObjectMapper JSON_OBJECT_MAPPER = new ObjectMapper();

private static final ObjectMapper STRICT_JSON_OBJECT_MAPPER =
new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);

private static final ObjectMapper CASE_INSENSITIVE_JSON_OBJECT_MAPPER =
JsonMapper.builder().configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true).build();

private static final ObjectMapper SORTED_JSON_OBJECT_MAPPER =
JsonMapper.builder()
.enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY)
.enable(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS)
.build();

public static ObjectMapper getFailSafeJsonObjectMapper() {
return FAIL_SAFE_JSON_OBJECT_MAPPER;
}

/**
* Get a default ObjectMapper with no special configuration.
*/
public static ObjectMapper getJsonObjectMapper() {
return JSON_OBJECT_MAPPER;
}

/**
* Get an ObjectMapper that fails on unknown properties.
*/
public static ObjectMapper getStrictJsonObjectMapper() {
return STRICT_JSON_OBJECT_MAPPER;
}

/**
* Get an ObjectMapper with case-insensitive property matching.
*/
public static ObjectMapper getCaseInsensitiveJsonObjectMapper() {
return CASE_INSENSITIVE_JSON_OBJECT_MAPPER;
}

/**
* Get an ObjectMapper that sorts properties alphabetically and map entries by keys.
*/
public static ObjectMapper getSortedJsonObjectMapper() {
return SORTED_JSON_OBJECT_MAPPER;
}

private SerializerFactory() {
}
}
Loading