All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nva.commons.apigateway.ApiGatewayProxyHandler Maven / Gradle / Ivy

There is a newer version: 1.41.0
Show newest version
package nva.commons.apigateway;

import com.amazonaws.services.lambda.runtime.Context;
import com.fasterxml.jackson.databind.ObjectMapper;
import nva.commons.apigateway.exceptions.ApiGatewayException;
import nva.commons.core.Environment;
import nva.commons.core.JacocoGenerated;

/**
 * An extension of ApiGatewayHandler where you are also able to specify the http status-code.
 *
 * @param  Class of the object in the body field of the ApiGateway message.
 * @param  Class of the response object.
 */
public abstract class ApiGatewayProxyHandler extends ApiGatewayHandler {

    private Integer statusCode;

    @JacocoGenerated
    protected ApiGatewayProxyHandler(Class iclass) {
        this(iclass, new Environment());
    }
    
    @JacocoGenerated
    protected ApiGatewayProxyHandler(Class iclass, Environment environment) {
        super(iclass, environment);
    }
    
    @JacocoGenerated
    protected ApiGatewayProxyHandler(Class iclass, Environment environment, ObjectMapper objectMapper) {
        super(iclass, environment, objectMapper);
    }

    @Override
    protected O processInput(I input, RequestInfo requestInfo, Context context) throws ApiGatewayException {
        var result = processProxyInput(input, requestInfo, context);
        statusCode = result.getStatusCode();
        return result.getBody();
    }

    @Override
    protected Integer getSuccessStatusCode(I input, O output) {
        if (statusCode == null) {
            throw new IllegalStateException("getSuccessStatusCode was called before processInput");
        }
        return statusCode;
    }

    /**
     * Implements the main logic of the handler. Any exception thrown by this method will be handled by {@link
     * RestRequestHandler#handleExpectedException} method.
     *
     * @param input       The input object to the method. Usually a deserialized json.
     * @param requestInfo Request headers and path.
     * @param context     the ApiGateway context.
     *
     * @return A Pair which consists of:
     *     - the Response body that is going to be serialized in json
     *     - the http response code
     * @throws ApiGatewayException all exceptions are caught by writeFailure and mapped to error codes through the
     *                             method {@link RestRequestHandler#getFailureStatusCode}
     */
    protected abstract ProxyResponse processProxyInput(I input, RequestInfo requestInfo, Context context)
        throws ApiGatewayException;
}