Skip to content
Draft
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 @@ -9,7 +9,7 @@ plugins {
}

group 'com.github.bibsysdev'
version = '1.38.3'
version = '1.39.0'


java.sourceCompatibility = JavaVersion.VERSION_17 // source-code version and must be <= targetCompatibility
Expand Down
1 change: 1 addition & 0 deletions eventhandlers/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dependencies {
implementation libs.jackson.databind
implementation libs.jackson.core

implementation libs.awsjavkit.events
implementation libs.aws.sdk.dynamo
implementation libs.aws.sdk2.dynamo
implementation libs.aws.sdk2.core
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import com.amazonaws.services.lambda.runtime.Context;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.awsjavakit.eventbridge.handlers.EventParser;
import com.github.awsjavakit.eventbridge.models.AwsEventBridgeDetail;
import com.github.awsjavakit.eventbridge.models.AwsEventBridgeEvent;
import no.unit.nva.events.EventsConfig;
import no.unit.nva.events.models.AwsEventBridgeDetail;
import no.unit.nva.events.models.AwsEventBridgeEvent;


public abstract class DestinationsEventBridgeEventHandler<I, O>
extends EventHandler<AwsEventBridgeDetail<I>, O> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,85 +1,30 @@
package no.unit.nva.events.handlers;

import static nva.commons.core.exceptions.ExceptionUtils.stackTraceInSingleLine;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import no.unit.nva.events.EventsConfig;
import no.unit.nva.events.models.AwsEventBridgeEvent;
import nva.commons.core.ioutils.IoUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/*
Implemented as RequestStreamHandler because RequestHandler has problem with java.time.Instant class.
Probably the class RequestHandler does not include the java-8-module.
*/
public abstract class EventHandler<I, O> implements RequestStreamHandler {

public static final String HANDLER_INPUT = "Handler input:\n";
public static final String ERROR_WRITING_TO_OUTPUT_STREAM = "Error writing output to output stream. Output is: ";
private static final Logger logger = LoggerFactory.getLogger(EventHandler.class);
protected final ObjectMapper objectMapper;
private final Class<I> iclass;
/*
Raw class usage in order to support parameterized types when EventHandler is extended by another class.
*/
/**
* Handler for handling EventBridge Events.
* @param <I> the input type.
* @param <O> the output type.
*
* Deprecated: Use the {@link com.github.awsjavakit.eventbridge.handlers.EventHandler} instead.
*/
@Deprecated
public abstract class EventHandler<I, O> extends com.github.awsjavakit.eventbridge.handlers.EventHandler<I, O> {


@SuppressWarnings({"rawtypes", "unchecked"})
protected EventHandler(Class iclass, ObjectMapper objectMapper) {
super();
this.iclass = (Class<I>) iclass;
this.objectMapper = objectMapper;
super(iclass, objectMapper);
}

@SuppressWarnings({"rawtypes", "unchecked"})
protected EventHandler(Class iclass) {
this(iclass, EventsConfig.objectMapper);
}

@Override
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) {
String inputString = null;
try {
inputString = IoUtils.streamToString(inputStream);
logger.trace(HANDLER_INPUT + inputString);
AwsEventBridgeEvent<I> input = parseEvent(inputString);
O output = processInput(input.getDetail(), input, context);

writeOutput(outputStream, output);
} catch (Exception e) {
handleError(e, inputString);
throw e;
}
}

protected void writeOutput(OutputStream outputStream, O output) {
{
try (BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(outputStream, StandardCharsets.UTF_8))) {
String responseJson = objectMapper.writeValueAsString(output);
writer.write(responseJson);
} catch (IOException e) {
logger.error(ERROR_WRITING_TO_OUTPUT_STREAM + output.toString());
throw new UncheckedIOException(e);
}
}
}

protected abstract O processInput(I input, AwsEventBridgeEvent<I> event, Context context);

protected AwsEventBridgeEvent<I> parseEvent(String input) {
return new EventParser<I>(input, objectMapper).parse(iclass);
}

protected void handleError(Exception e, String inputString) {
logger.error(stackTraceInSingleLine(e));
}
}

This file was deleted.

This file was deleted.

Loading