com.amazonaws.services.lambda.runtime.RequestHandler Java Examples

The following examples show how to use com.amazonaws.services.lambda.runtime.RequestHandler. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AmazonLambdaRecorder.java    From quarkus with Apache License 2.0 6 votes vote down vote up
private Method discoverHandlerMethod(Class<? extends RequestHandler<?, ?>> handlerClass) {
    final Method[] methods = handlerClass.getMethods();
    Method method = null;
    for (int i = 0; i < methods.length && method == null; i++) {
        if (methods[i].getName().equals("handleRequest")) {
            final Class<?>[] types = methods[i].getParameterTypes();
            if (types.length == 2 && !types[0].equals(Object.class)) {
                method = methods[i];
            }
        }
    }
    if (method == null) {
        method = methods[0];
    }
    return method;
}
 
Example #2
Source File: MicronautLambdaRuntime.java    From micronaut-aws with Apache License 2.0 5 votes vote down vote up
@Override
protected RequestHandler<AwsProxyRequest, AwsProxyResponse> createRequestHandler(String... args) {
    try {
        return new MicronautLambdaHandler(createApplicationContextBuilderWithArgs(args));
    } catch (ContainerInitializationException e) {
        throw new ConfigurationException("Exception thrown instantiating MicronautLambdaRuntimeHandler");
    }
}
 
Example #3
Source File: AbstractMicronautLambdaRuntime.java    From micronaut-aws with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @throws ConfigurationException if the handler is not of type RequestHandler or RequestStreamHandler
 */
protected void validateHandler() throws ConfigurationException {
    if (handler == null) {
        throw new ConfigurationException("no handler instantiated. Override either createHandler() or createRequestStreamHandler() or annotate your Handler class with @Introspected");
    }
    if (!(handler instanceof RequestHandler || handler instanceof RequestStreamHandler)) {
        throw new ConfigurationException("handler must be of type com.amazonaws.services.lambda.runtime.RequestHandler or com.amazonaws.services.lambda.runtime.RequestStreamHandler");
    }
}
 
Example #4
Source File: AbstractMicronautLambdaRuntime.java    From micronaut-aws with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @param args Command Line Args
 * @return if {@link AbstractMicronautLambdaRuntime#createHandler(String...)} or {@link AbstractMicronautLambdaRuntime#createRequestStreamHandler(String...)} are implemented, it returns the handler returned by those methods. If they are not, it attempts to instantiate the class
 * referenced by the environment variable {@link ReservedRuntimeEnvironmentVariables#HANDLER} via Introspection. Otherwise, it returns {@code null}.
 */
@Nullable
protected Object createHandler(String... args) {
    RequestHandler<HandlerRequestType, HandlerResponseType> requestHandler = createRequestHandler(args);
    if (requestHandler != null) {
        return requestHandler;
    }
    RequestStreamHandler requestStreamHandler = createRequestStreamHandler(args);
    if (requestStreamHandler != null) {
        return requestStreamHandler;
    }
    return createEnvironmentHandler();
}
 
Example #5
Source File: AmazonLambdaRecorder.java    From quarkus with Apache License 2.0 5 votes vote down vote up
public void setHandlerClass(Class<? extends RequestHandler<?, ?>> handler, BeanContainer container) {
    handlerClass = handler;
    beanContainer = container;
    ObjectMapper objectMapper = AmazonLambdaMapperRecorder.objectMapper;
    Method handlerMethod = discoverHandlerMethod(handlerClass);
    objectReader = objectMapper.readerFor(handlerMethod.getParameterTypes()[0]);
    objectWriter = objectMapper.writerFor(handlerMethod.getReturnType());
}
 
Example #6
Source File: SpringVoidRequestHandlerTest.java    From spring-aws-lambda with MIT License 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void handle() {
    final RequestHandler handler = mock(RequestHandler.class);
    when(mockApplicationContext.getBean(RequestHandler.class)).thenReturn(handler);
    final TestSpringVoidRequestHandler testHandler = new TestSpringVoidRequestHandler();
    final Context mockContext = mock(Context.class);
    final String input = "foo";
    testHandler.handleRequest(input, mockContext);
    verify(mockApplicationContext).getBean(RequestHandler.class);
    verify(handler).handleRequest(input, mockContext);
}
 
Example #7
Source File: SpringRequestHandler.java    From spring-aws-lambda with MIT License 4 votes vote down vote up
public SpringRequestHandler() {
    handler = getApplicationContext().getBean(RequestHandler.class);
}
 
Example #8
Source File: AbstractMicronautLambdaRuntime.java    From micronaut-aws with Apache License 2.0 2 votes vote down vote up
/**
 *
 * @param args Command Line Args
 * @return a {@link RequestHandler} or {@code null}.
 */
@Nullable
protected RequestHandler<HandlerRequestType, HandlerResponseType> createRequestHandler(String... args) {
    return null;
}
 
Example #9
Source File: JacksonSpringRequestHandler.java    From spring-aws-lambda with MIT License 2 votes vote down vote up
/**
 * To work around type erasure, pass the input type class here. AWS Lambda
 * requires a no-arg constructor for their handler classes however, so be
 * sure to provide one while calling this constructor in it.
 *
 * @param inputClass the class of this class's input type parameter, I.
 */
protected JacksonSpringRequestHandler(@NotNull final Class<I> inputClass) {
    this.inputClass = inputClass;
    this.handler = getApplicationContext().getBean(RequestHandler.class);
}