diff --git a/aws-serverless-java-container-jersey/src/main/java/com/amazonaws/serverless/proxy/jersey/JerseyLambdaContainerHandler.java b/aws-serverless-java-container-jersey/src/main/java/com/amazonaws/serverless/proxy/jersey/JerseyLambdaContainerHandler.java index 576d2b66c..dd9166f7d 100644 --- a/aws-serverless-java-container-jersey/src/main/java/com/amazonaws/serverless/proxy/jersey/JerseyLambdaContainerHandler.java +++ b/aws-serverless-java-container-jersey/src/main/java/com/amazonaws/serverless/proxy/jersey/JerseyLambdaContainerHandler.java @@ -34,6 +34,7 @@ import com.amazonaws.services.lambda.runtime.Context; import org.glassfish.jersey.internal.inject.AbstractBinder; +import org.glassfish.jersey.internal.inject.InjectionManager; import org.glassfish.jersey.process.internal.RequestScoped; import org.glassfish.jersey.server.ResourceConfig; @@ -196,4 +197,12 @@ public void initialize() { Timer.stop("JERSEY_COLD_START_INIT"); initialized = true; } + + + public InjectionManager getInjectionManager() { + if (!initialized) { + initialize(); + } + return jerseyFilter.getApplicationHandler().getInjectionManager(); + } } diff --git a/aws-serverless-java-container-jersey/src/test/java/com/amazonaws/serverless/proxy/jersey/JerseyInjectionTest.java b/aws-serverless-java-container-jersey/src/test/java/com/amazonaws/serverless/proxy/jersey/JerseyInjectionTest.java new file mode 100644 index 000000000..0af2785ee --- /dev/null +++ b/aws-serverless-java-container-jersey/src/test/java/com/amazonaws/serverless/proxy/jersey/JerseyInjectionTest.java @@ -0,0 +1,59 @@ +/* + * Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance + * with the License. A copy of the License is located at + * + * http://aws.amazon.com/apache2.0/ + * + * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES + * OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions + * and limitations under the License. + */ +package com.amazonaws.serverless.proxy.jersey; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import javax.inject.Singleton; + +import org.glassfish.jersey.internal.inject.AbstractBinder; +import org.glassfish.jersey.media.multipart.MultiPartFeature; +import org.glassfish.jersey.server.ResourceConfig; +import org.junit.Test; + +import com.amazonaws.serverless.proxy.model.AwsProxyRequest; +import com.amazonaws.serverless.proxy.model.AwsProxyResponse; + +/** + * Test that one can access the Jersey injection manager + */ +public class JerseyInjectionTest { + + // Test ressource binder + private static class ResourceBinder extends AbstractBinder { + + @Override + protected void configure() { + bind(new JerseyInjectionTest()).to(JerseyInjectionTest.class).in(Singleton.class); + } + + } + + private static ResourceConfig app = new ResourceConfig().register(MultiPartFeature.class) + .register(new ResourceBinder()); + + private static JerseyLambdaContainerHandler handler = JerseyLambdaContainerHandler.getAwsProxyHandler( + app); + + @Test + public void can_get_injected_resources() throws Exception { + + JerseyInjectionTest instance1 = handler.getInjectionManager().getInstance(JerseyInjectionTest.class); + assertNotNull(instance1); + + JerseyInjectionTest instance2 = handler.getInjectionManager().getInstance(JerseyInjectionTest.class); + assertEquals(instance1, instance2); + + } +}