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,18 @@
package org.folio.rest.workflow.exception;

/**
* Failure in the AbstractConverter.
*/
public class AbstractConverterFailure extends RuntimeException {

private static final long serialVersionUID = 1062422697623L;

public AbstractConverterFailure(String message) {
super(message);
}

public AbstractConverterFailure(String message, Exception e) {
super(message, e);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public abstract class Node extends AbstractBaseEntity implements HasId, HasInfor
@Convert(converter = JsonNodeConverter.class, attributeName = "deserializeAs")
private String deserializeAs = this.getClass().getSimpleName();

public Node() {
protected Node() {
super();

name = "";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package org.folio.rest.workflow.model.converter;

import com.fasterxml.jackson.annotation.JsonInclude;
import jakarta.persistence.AttributeConverter;
import org.folio.rest.workflow.exception.AbstractConverterFailure;
import tools.jackson.core.JacksonException;
import tools.jackson.core.StreamReadFeature;
import tools.jackson.core.type.TypeReference;
import tools.jackson.databind.DeserializationFeature;
import tools.jackson.databind.MapperFeature;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.SerializationFeature;
import tools.jackson.databind.json.JsonMapper;

/**
Expand All @@ -17,21 +19,28 @@
*/
public abstract class AbstractConverter<T> implements AttributeConverter<T, String> {

private ObjectMapper objectMapper = JsonMapper.builder()
.enable(StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION)
.disable(MapperFeature.REQUIRE_TYPE_ID_FOR_SUBTYPES)
.disable(DeserializationFeature.FAIL_ON_MISSING_EXTERNAL_TYPE_ID_PROPERTY)
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
private JsonMapper mapper = JsonMapper
.builderWithJackson2Defaults()
.configure(DeserializationFeature.FAIL_ON_MISSING_EXTERNAL_TYPE_ID_PROPERTY, false)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.configure(MapperFeature.REQUIRE_TYPE_ID_FOR_SUBTYPES, true)
.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
.configure(StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION, true)
.changeDefaultPropertyInclusion(incl -> incl
.withValueInclusion(JsonInclude.Include.NON_NULL)
.withContentInclusion(JsonInclude.Include.NON_NULL)
)
.findAndAddModules()
.build();

@Override
public String convertToDatabaseColumn(T attribute) {
if (attribute == null) return null;

try {
return objectMapper.writeValueAsString(attribute);
return mapper.writeValueAsString(attribute);
} catch (JacksonException e) {
throw new RuntimeException(e.getMessage());
throw new AbstractConverterFailure(e.getMessage(), e);
}
}

Expand All @@ -40,9 +49,9 @@ public T convertToEntityAttribute(String dbData) {
if (dbData == null) return null;

try {
return objectMapper.readValue(dbData, getTypeReference());
return mapper.readValue(dbData, getTypeReference());
} catch (JacksonException e) {
throw new RuntimeException(e.getMessage());
throw new AbstractConverterFailure(e.getMessage(), e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
import java.util.stream.Stream;
import java.util.stream.Stream.Builder;
import org.folio.spring.test.helper.MapperHelper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -21,13 +22,12 @@
import tools.jackson.databind.DatabindContext;
import tools.jackson.databind.JavaType;
import tools.jackson.databind.JsonNode;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.json.JsonMapper;

@ExtendWith(MockitoExtension.class)
class DeserializeAsNodeJsonResolverTest {

private ObjectMapper mapper;
private JsonMapper mapper;

@Mock
private DatabindContext databindContext;
Expand All @@ -37,7 +37,7 @@ class DeserializeAsNodeJsonResolverTest {

@BeforeEach
void beforeEach() {
mapper = JsonMapper.builder().build();
mapper = MapperHelper.build();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class KafkaProducerConfig {
private String bootstrapAddress;

@Bean
public ProducerFactory<String, Event> eventProducerFactory() {
ProducerFactory<String, Event> eventProducerFactory() {
Map<String, Object> configProps = new HashMap<>();
configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapAddress);
configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.HandlerMapping;
import tools.jackson.databind.JsonNode;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.json.JsonMapper;
import tools.jackson.databind.node.ObjectNode;

@RestController
Expand All @@ -55,13 +55,13 @@ public class EventController {

private PathMatcher pathMatcher;

private ObjectMapper objectMapper;
private JsonMapper mapper;

public EventController(EventProducer eventProducer, TriggerRepo triggerRepo, PathMatcher pathMatcher, ObjectMapper objectMapper) {
public EventController(EventProducer eventProducer, TriggerRepo triggerRepo, PathMatcher pathMatcher, JsonMapper mapper) {
this.eventProducer = eventProducer;
this.triggerRepo = triggerRepo;
this.pathMatcher = pathMatcher;
this.objectMapper = objectMapper;
this.mapper = mapper;
}

@PostMapping(value = "/**", consumes = "application/json", produces = { MediaType.APPLICATION_JSON_VALUE })
Expand All @@ -84,7 +84,7 @@ public JsonNode postHandleEventsWithFile(
throw new FileSystemException("Invalid tenant directory name");
}

ObjectNode body = objectMapper.createObjectNode();
ObjectNode body = mapper.createObjectNode();

Path tenantPath = Path.of(eventUploadsDirectory)
.resolve(tenant)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import tools.jackson.core.JacksonException;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.json.JsonMapper;

abstract class AbstractAdvice extends RequestMappingHandlerMapping {

/**
* Get the object mapper
* Get the JSON mapper
*
* @return objectMapper The object mapper.
* @return The JSON mapper.
*/
protected abstract ObjectMapper getObjectMapper();
protected abstract JsonMapper getMapper();

/**
* Build the error message, with default JSON media type.
Expand Down Expand Up @@ -46,7 +46,7 @@ protected ResponseEntity<String> buildError(Exception ex, HttpStatus code, Medi
// The exception handler should ideally not throw its own exceptions.
// Catch the exceptions and report it, then fall back to a plain text error message.
try {
message = getObjectMapper().writeValueAsString(ErrorUtility.buildError(ex, code));
message = getMapper().writeValueAsString(ErrorUtility.buildError(ex, code));
} catch (JacksonException e) {
logger.error("Mapping error to JSON Object failed.", e);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,42 @@
package org.folio.rest.workflow.controller.advice;
import com.fasterxml.jackson.annotation.JsonInclude;
import java.nio.file.FileSystemException;
import org.folio.rest.workflow.exception.EventPublishException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.core.StreamReadFeature;
import tools.jackson.databind.DeserializationFeature;
import tools.jackson.databind.MapperFeature;
import tools.jackson.databind.SerializationFeature;
import tools.jackson.databind.json.JsonMapper;

@RestControllerAdvice
public class EventControllerAdvice extends AbstractAdvice {

ObjectMapper objectMapper;
JsonMapper mapper;

public EventControllerAdvice() {
this.objectMapper = JsonMapper.builder().build();
this.mapper = JsonMapper
.builderWithJackson2Defaults()
.configure(DeserializationFeature.FAIL_ON_MISSING_EXTERNAL_TYPE_ID_PROPERTY, false)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.configure(MapperFeature.REQUIRE_TYPE_ID_FOR_SUBTYPES, true)
.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
.configure(StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION, true)
.changeDefaultPropertyInclusion(incl -> incl
.withValueInclusion(JsonInclude.Include.NON_NULL)
.withContentInclusion(JsonInclude.Include.NON_NULL)
)
.findAndAddModules()
.build();
}

@Override
protected ObjectMapper getObjectMapper() {
return objectMapper;
protected JsonMapper getMapper() {
return mapper;
}

@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,41 @@
package org.folio.rest.workflow.controller.advice;

import com.fasterxml.jackson.annotation.JsonInclude;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.TransactionSystemException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.core.StreamReadFeature;
import tools.jackson.databind.DeserializationFeature;
import tools.jackson.databind.MapperFeature;
import tools.jackson.databind.SerializationFeature;
import tools.jackson.databind.json.JsonMapper;

@RestControllerAdvice
public class GlobalAdvice extends AbstractAdvice {

ObjectMapper objectMapper;
JsonMapper mapper;

public GlobalAdvice() {
this.objectMapper = JsonMapper.builder().build();
this.mapper = JsonMapper
.builderWithJackson2Defaults()
.configure(DeserializationFeature.FAIL_ON_MISSING_EXTERNAL_TYPE_ID_PROPERTY, false)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.configure(MapperFeature.REQUIRE_TYPE_ID_FOR_SUBTYPES, true)
.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
.configure(StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION, true)
.changeDefaultPropertyInclusion(incl -> incl
.withValueInclusion(JsonInclude.Include.NON_NULL)
.withContentInclusion(JsonInclude.Include.NON_NULL)
)
.findAndAddModules()
.build();
}

@Override
protected ObjectMapper getObjectMapper() {
return objectMapper;
protected JsonMapper getMapper() {
return mapper;
}

@ExceptionHandler({ TransactionSystemException.class })
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.folio.rest.workflow.controller.advice;

import com.fasterxml.jackson.annotation.JsonInclude;
import jakarta.persistence.EntityNotFoundException;
import org.folio.rest.workflow.exception.WorkflowAlreadyActiveException;
import org.folio.rest.workflow.exception.WorkflowCreateAlreadyExistsException;
Expand All @@ -12,21 +13,36 @@
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.core.StreamReadFeature;
import tools.jackson.databind.DeserializationFeature;
import tools.jackson.databind.MapperFeature;
import tools.jackson.databind.SerializationFeature;
import tools.jackson.databind.json.JsonMapper;

@RestControllerAdvice
public class WorkflowControllerAdvice extends AbstractAdvice {

ObjectMapper objectMapper;
JsonMapper mapper;

public WorkflowControllerAdvice() {
this.objectMapper = JsonMapper.builder().build();
this.mapper = JsonMapper
.builderWithJackson2Defaults()
.configure(DeserializationFeature.FAIL_ON_MISSING_EXTERNAL_TYPE_ID_PROPERTY, false)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.configure(MapperFeature.REQUIRE_TYPE_ID_FOR_SUBTYPES, true)
.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
.configure(StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION, true)
.changeDefaultPropertyInclusion(incl -> incl
.withValueInclusion(JsonInclude.Include.NON_NULL)
.withContentInclusion(JsonInclude.Include.NON_NULL)
)
.findAndAddModules()
.build();
}

@Override
protected ObjectMapper getObjectMapper() {
return objectMapper;
protected JsonMapper getMapper() {
return mapper;
}

@ResponseStatus(HttpStatus.NOT_FOUND)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.folio.rest.workflow.service;

import java.util.List;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.json.JsonMapper;
import tools.jackson.databind.node.ArrayNode;
import tools.jackson.databind.node.ObjectNode;

Expand All @@ -18,9 +18,9 @@ public abstract class AbstractCqlService<T> {
public static final String TOTAL_RECORDS = "totalRecords";

/**
* The object mapper, which must be initialized in the extending class' constructor.
* The mapper, which must be initialized in the extending class' constructor.
*/
protected ObjectMapper mapper;
protected JsonMapper mapper;

/**
* Use CQL to find a list of Workflows.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import tools.jackson.databind.JsonNode;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.json.JsonMapper;

@Service
public class OkapiDiscoveryService {
Expand All @@ -44,11 +44,11 @@ public class OkapiDiscoveryService {

private HttpService httpService;

private ObjectMapper objectMapper;
private JsonMapper mapper;

public OkapiDiscoveryService(HttpService httpService, ObjectMapper objectMapper) {
public OkapiDiscoveryService(HttpService httpService, JsonMapper mapper) {
this.httpService = httpService;
this.objectMapper = objectMapper;
this.mapper = mapper;
}

public List<Action> getActionsByTenant(String tenant) {
Expand Down Expand Up @@ -82,7 +82,7 @@ public Map<String, List<Handler>> getHandlers(String tenant, String id) {
List<Handler> handlers = new ArrayList<>();
String interfaceName = interfaceNode.get(ID).asString();
for (JsonNode handlersNode : interfaceNode.get(HANDLERS)) {
handlers.add(objectMapper.readValue(handlersNode.toString(), Handler.class));
handlers.add(mapper.readValue(handlersNode.toString(), Handler.class));
}
handlerMap.put(interfaceName, handlers);
}
Expand Down
Loading
Loading