From bcf38a25c41cfcd8b09132e62f9b239cd0036a18 Mon Sep 17 00:00:00 2001 From: Bartosz Firyn Date: Thu, 6 Nov 2014 16:29:26 +0100 Subject: [PATCH] Register parent service locator in servlet context, closes JERSEY-2704 --- .gitignore | 1 + .../jersey/servlet/ServletContainer.java | 9 ++ .../jersey/servlet/ServletProperties.java | 14 ++ .../jersey/servlet/WebComponent.java | 15 ++- tests/integration/jersey-2704/pom.xml | 90 +++++++++++++ .../jersey2704/ServiceLocatorSetup.java | 70 ++++++++++ .../jersey2704/TestApplication.java | 55 ++++++++ .../integration/jersey2704/TestResource.java | 89 +++++++++++++ .../jersey2704/services/HappyService.java | 55 ++++++++ .../jersey2704/services/SadService.java | 55 ++++++++ .../src/main/webapp/WEB-INF/web.xml | 61 +++++++++ .../jersey2704/Jersey2704ITCase.java | 121 ++++++++++++++++++ tests/integration/pom.xml | 1 + 13 files changed, 635 insertions(+), 1 deletion(-) create mode 100644 tests/integration/jersey-2704/pom.xml create mode 100644 tests/integration/jersey-2704/src/main/java/org/glassfish/jersey/tests/integration/jersey2704/ServiceLocatorSetup.java create mode 100644 tests/integration/jersey-2704/src/main/java/org/glassfish/jersey/tests/integration/jersey2704/TestApplication.java create mode 100644 tests/integration/jersey-2704/src/main/java/org/glassfish/jersey/tests/integration/jersey2704/TestResource.java create mode 100644 tests/integration/jersey-2704/src/main/java/org/glassfish/jersey/tests/integration/jersey2704/services/HappyService.java create mode 100644 tests/integration/jersey-2704/src/main/java/org/glassfish/jersey/tests/integration/jersey2704/services/SadService.java create mode 100644 tests/integration/jersey-2704/src/main/webapp/WEB-INF/web.xml create mode 100644 tests/integration/jersey-2704/src/test/java/org/glassfish/jersey/tests/integration/jersey2704/Jersey2704ITCase.java diff --git a/.gitignore b/.gitignore index 88d0eabd7b..367c54d98f 100644 --- a/.gitignore +++ b/.gitignore @@ -28,3 +28,4 @@ nb-configuration.xml # Maven plugins noise dependency-reduced-pom.xml +pom.xml.versionsBackup diff --git a/containers/jersey-servlet-core/src/main/java/org/glassfish/jersey/servlet/ServletContainer.java b/containers/jersey-servlet-core/src/main/java/org/glassfish/jersey/servlet/ServletContainer.java index b46bae81b7..daa7ac79ae 100644 --- a/containers/jersey-servlet-core/src/main/java/org/glassfish/jersey/servlet/ServletContainer.java +++ b/containers/jersey-servlet-core/src/main/java/org/glassfish/jersey/servlet/ServletContainer.java @@ -588,4 +588,13 @@ public void reload(ResourceConfig configuration) { public ApplicationHandler getApplicationHandler() { return webComponent.appHandler; } + + /** + * Get {@link WebComponent} used by this servlet container. + * + * @return The web component. + */ + public WebComponent getWebComponent() { + return webComponent; + } } diff --git a/containers/jersey-servlet-core/src/main/java/org/glassfish/jersey/servlet/ServletProperties.java b/containers/jersey-servlet-core/src/main/java/org/glassfish/jersey/servlet/ServletProperties.java index fae0e3156b..f36b3daed6 100644 --- a/containers/jersey-servlet-core/src/main/java/org/glassfish/jersey/servlet/ServletProperties.java +++ b/containers/jersey-servlet-core/src/main/java/org/glassfish/jersey/servlet/ServletProperties.java @@ -39,6 +39,7 @@ */ package org.glassfish.jersey.servlet; +import org.glassfish.hk2.api.ServiceLocator; import org.glassfish.jersey.internal.util.PropertiesClass; /** @@ -147,6 +148,19 @@ public final class ServletProperties { */ public static final String PROVIDER_WEB_APP = "jersey.config.servlet.provider.webapp"; + /** + * Identifies the object that will be used as a parent {@link ServiceLocator} in the Jersey + * {@link WebComponent}. + *

+ * This property gives a possibility to use HK2 services that are registered and/or created + * outside of the Jersey server context. + *

+ * By default this property is not set. + *

+ * The name of the configuration property is {@value}. + */ + public static final String SERVICE_LOCATOR = "jersey.config.servlet.context.serviceLocator"; + private ServletProperties() { // prevents instantiation } diff --git a/containers/jersey-servlet-core/src/main/java/org/glassfish/jersey/servlet/WebComponent.java b/containers/jersey-servlet-core/src/main/java/org/glassfish/jersey/servlet/WebComponent.java index 40ce64b623..fd73b3f3fc 100644 --- a/containers/jersey-servlet-core/src/main/java/org/glassfish/jersey/servlet/WebComponent.java +++ b/containers/jersey-servlet-core/src/main/java/org/glassfish/jersey/servlet/WebComponent.java @@ -297,7 +297,9 @@ public void dispose(final WebConfig instance) { * resource configuration. */ public WebComponent(final WebConfig webConfig, ResourceConfig resourceConfig) throws ServletException { + this.webConfig = webConfig; + if (resourceConfig == null) { resourceConfig = createResourceConfig(webConfig); } @@ -308,7 +310,9 @@ public WebComponent(final WebConfig webConfig, ResourceConfig resourceConfig) th final AbstractBinder webComponentBinder = new WebComponentBinder(resourceConfig.getProperties()); resourceConfig.register(webComponentBinder); - this.appHandler = new ApplicationHandler(resourceConfig, webComponentBinder); + ServiceLocator locator = (ServiceLocator) webConfig.getServletContext().getAttribute(ServletProperties.SERVICE_LOCATOR); + + this.appHandler = new ApplicationHandler(resourceConfig, webComponentBinder, locator); this.asyncExtensionDelegate = getAsyncExtensionDelegate(); this.forwardOn404 = webConfig.getConfigType().equals(WebConfig.ConfigType.FilterConfig) @@ -562,4 +566,13 @@ private void filterFormParameters(final HttpServletRequest servletRequest, final } } } + + /** + * Get {@link ApplicationHandler} used by this web component. + * + * @return The application handler + */ + public ApplicationHandler getAppHandler() { + return appHandler; + } } diff --git a/tests/integration/jersey-2704/pom.xml b/tests/integration/jersey-2704/pom.xml new file mode 100644 index 0000000000..2f4e43b27a --- /dev/null +++ b/tests/integration/jersey-2704/pom.xml @@ -0,0 +1,90 @@ + + + + 4.0.0 + + + org.glassfish.jersey.tests.integration + project + 2.14-SNAPSHOT + + + jersey-2704 + war + jersey-tests-integration-jersey-2704 + + + This test is to verify if ServiceLocator can be configured within the server context + in such a way that Jersey can use it as a parent ServiceLocator in the WebComponent. + More details can be found in JERSEY-2704. + + + + + org.glassfish.jersey.containers + jersey-container-servlet-core + + + org.glassfish.jersey.test-framework.providers + jersey-test-framework-provider-external + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + org.apache.maven.plugins + maven-failsafe-plugin + + + org.mortbay.jetty + jetty-maven-plugin + + + + + diff --git a/tests/integration/jersey-2704/src/main/java/org/glassfish/jersey/tests/integration/jersey2704/ServiceLocatorSetup.java b/tests/integration/jersey-2704/src/main/java/org/glassfish/jersey/tests/integration/jersey2704/ServiceLocatorSetup.java new file mode 100644 index 0000000000..c366c38475 --- /dev/null +++ b/tests/integration/jersey-2704/src/main/java/org/glassfish/jersey/tests/integration/jersey2704/ServiceLocatorSetup.java @@ -0,0 +1,70 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright (c) 2012-2014 Oracle and/or its affiliates. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common Development + * and Distribution License("CDDL") (collectively, the "License"). You + * may not use this file except in compliance with the License. You can + * obtain a copy of the License at + * http://glassfish.java.net/public/CDDL+GPL_1_1.html + * or packager/legal/LICENSE.txt. See the License for the specific + * language governing permissions and limitations under the License. + * + * When distributing the software, include this License Header Notice in each + * file and include the License file at packager/legal/LICENSE.txt. + * + * GPL Classpath Exception: + * Oracle designates this particular file as subject to the "Classpath" + * exception as provided by Oracle in the GPL Version 2 section of the License + * file that accompanied this code. + * + * Modifications: + * If applicable, add the following below the License Header, with the fields + * enclosed by brackets [] replaced by your own identifying information: + * "Portions Copyright [year] [name of copyright owner]" + * + * Contributor(s): + * If you wish your version of this file to be governed by only the CDDL or + * only the GPL Version 2, indicate your decision by adding "[Contributor] + * elects to include this software in this distribution under the [CDDL or GPL + * Version 2] license." If you don't indicate a single choice of license, a + * recipient has the option to distribute your version of this file under + * either the CDDL, the GPL Version 2 or to extend the choice of license to + * its licensees as provided above. However, if you add GPL Version 2 code + * and therefore, elected the GPL Version 2 license, then the option applies + * only if the new code is made subject to such option by the copyright + * holder. + */ +package org.glassfish.jersey.tests.integration.jersey2704; + +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; + +import org.glassfish.hk2.api.ServiceLocator; +import org.glassfish.hk2.utilities.ServiceLocatorUtilities; +import org.glassfish.jersey.servlet.ServletProperties; +import org.glassfish.jersey.tests.integration.jersey2704.services.HappyService; + + +/** + * This class is to listen for {@link ServletContextEvent} generated whenever context + * is initialized and set {@link ServletProperties#SERVICE_LOCATOR} attribute to point + * {@link ServiceLocator} pre-populated with {@link HappyService} instance. + * + * @author Bartosz Firyn (sarxos) + */ +public class ServiceLocatorSetup implements ServletContextListener { + + @Override + public void contextInitialized(ServletContextEvent event) { + ServiceLocator locator = ServiceLocatorUtilities.createAndPopulateServiceLocator(); + ServiceLocatorUtilities.addOneConstant(locator, new HappyService()); + event.getServletContext().setAttribute(ServletProperties.SERVICE_LOCATOR, locator); + } + + @Override + public void contextDestroyed(ServletContextEvent event) { + } +} diff --git a/tests/integration/jersey-2704/src/main/java/org/glassfish/jersey/tests/integration/jersey2704/TestApplication.java b/tests/integration/jersey-2704/src/main/java/org/glassfish/jersey/tests/integration/jersey2704/TestApplication.java new file mode 100644 index 0000000000..23d4e34f58 --- /dev/null +++ b/tests/integration/jersey-2704/src/main/java/org/glassfish/jersey/tests/integration/jersey2704/TestApplication.java @@ -0,0 +1,55 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright (c) 2012-2014 Oracle and/or its affiliates. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common Development + * and Distribution License("CDDL") (collectively, the "License"). You + * may not use this file except in compliance with the License. You can + * obtain a copy of the License at + * http://glassfish.java.net/public/CDDL+GPL_1_1.html + * or packager/legal/LICENSE.txt. See the License for the specific + * language governing permissions and limitations under the License. + * + * When distributing the software, include this License Header Notice in each + * file and include the License file at packager/legal/LICENSE.txt. + * + * GPL Classpath Exception: + * Oracle designates this particular file as subject to the "Classpath" + * exception as provided by Oracle in the GPL Version 2 section of the License + * file that accompanied this code. + * + * Modifications: + * If applicable, add the following below the License Header, with the fields + * enclosed by brackets [] replaced by your own identifying information: + * "Portions Copyright [year] [name of copyright owner]" + * + * Contributor(s): + * If you wish your version of this file to be governed by only the CDDL or + * only the GPL Version 2, indicate your decision by adding "[Contributor] + * elects to include this software in this distribution under the [CDDL or GPL + * Version 2] license." If you don't indicate a single choice of license, a + * recipient has the option to distribute your version of this file under + * either the CDDL, the GPL Version 2 or to extend the choice of license to + * its licensees as provided above. However, if you add GPL Version 2 code + * and therefore, elected the GPL Version 2 license, then the option applies + * only if the new code is made subject to such option by the copyright + * holder. + */ +package org.glassfish.jersey.tests.integration.jersey2704; + +import org.glassfish.jersey.server.ResourceConfig; + + +/** + * Jersey application. + * + * @author Bartosz Firyn (bartoszfiryn at gmail.com) + */ +public class TestApplication extends ResourceConfig { + + public TestApplication() { + register(TestResource.class); + } +} diff --git a/tests/integration/jersey-2704/src/main/java/org/glassfish/jersey/tests/integration/jersey2704/TestResource.java b/tests/integration/jersey-2704/src/main/java/org/glassfish/jersey/tests/integration/jersey2704/TestResource.java new file mode 100644 index 0000000000..a2ac44b283 --- /dev/null +++ b/tests/integration/jersey-2704/src/main/java/org/glassfish/jersey/tests/integration/jersey2704/TestResource.java @@ -0,0 +1,89 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright (c) 2012-2014 Oracle and/or its affiliates. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common Development + * and Distribution License("CDDL") (collectively, the "License"). You + * may not use this file except in compliance with the License. You can + * obtain a copy of the License at + * http://glassfish.java.net/public/CDDL+GPL_1_1.html + * or packager/legal/LICENSE.txt. See the License for the specific + * language governing permissions and limitations under the License. + * + * When distributing the software, include this License Header Notice in each + * file and include the License file at packager/legal/LICENSE.txt. + * + * GPL Classpath Exception: + * Oracle designates this particular file as subject to the "Classpath" + * exception as provided by Oracle in the GPL Version 2 section of the License + * file that accompanied this code. + * + * Modifications: + * If applicable, add the following below the License Header, with the fields + * enclosed by brackets [] replaced by your own identifying information: + * "Portions Copyright [year] [name of copyright owner]" + * + * Contributor(s): + * If you wish your version of this file to be governed by only the CDDL or + * only the GPL Version 2, indicate your decision by adding "[Contributor] + * elects to include this software in this distribution under the [CDDL or GPL + * Version 2] license." If you don't indicate a single choice of license, a + * recipient has the option to distribute your version of this file under + * either the CDDL, the GPL Version 2 or to extend the choice of license to + * its licensees as provided above. However, if you add GPL Version 2 code + * and therefore, elected the GPL Version 2 license, then the option applies + * only if the new code is made subject to such option by the copyright + * holder. + */ +package org.glassfish.jersey.tests.integration.jersey2704; + +import javax.inject.Inject; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.Response; + +import org.glassfish.hk2.api.ServiceLocator; + + +/** + * This resource is used to test if specific service class instance is available in the + * {@link ServiceLocator} that comes from Jersey context. + * + * @author Bartosz Firyn (bartoszfiryn at gmail.com) + */ +@Path("test") +public class TestResource { + + ServiceLocator locator; + + /** + * Inject {@link ServiceLocator} from Jersey context. + * + * @param locator the {@link ServiceLocator} + */ + @Inject + public TestResource(ServiceLocator locator) { + this.locator = locator; + } + + /** + * This method will test given class by checking if it is available in {@link ServiceLocator} + * that has been injected from the Jersey context. + * + * @param clazz the service class name to check + * @return {@link Response} with status code 200 if service is available, 600 otherwise + * @throws Exception in case when there are any error (e.g. class not exist) + */ + @GET + @Path("{clazz}") + @Produces("text/plain") + public Response test(@PathParam("clazz") String clazz) throws Exception { + return Response + .status(locator.getService(Class.forName(clazz)) != null ? 200 : 600) + .build(); + } +} diff --git a/tests/integration/jersey-2704/src/main/java/org/glassfish/jersey/tests/integration/jersey2704/services/HappyService.java b/tests/integration/jersey-2704/src/main/java/org/glassfish/jersey/tests/integration/jersey2704/services/HappyService.java new file mode 100644 index 0000000000..b8e52f6cb4 --- /dev/null +++ b/tests/integration/jersey-2704/src/main/java/org/glassfish/jersey/tests/integration/jersey2704/services/HappyService.java @@ -0,0 +1,55 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright (c) 2012-2014 Oracle and/or its affiliates. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common Development + * and Distribution License("CDDL") (collectively, the "License"). You + * may not use this file except in compliance with the License. You can + * obtain a copy of the License at + * http://glassfish.java.net/public/CDDL+GPL_1_1.html + * or packager/legal/LICENSE.txt. See the License for the specific + * language governing permissions and limitations under the License. + * + * When distributing the software, include this License Header Notice in each + * file and include the License file at packager/legal/LICENSE.txt. + * + * GPL Classpath Exception: + * Oracle designates this particular file as subject to the "Classpath" + * exception as provided by Oracle in the GPL Version 2 section of the License + * file that accompanied this code. + * + * Modifications: + * If applicable, add the following below the License Header, with the fields + * enclosed by brackets [] replaced by your own identifying information: + * "Portions Copyright [year] [name of copyright owner]" + * + * Contributor(s): + * If you wish your version of this file to be governed by only the CDDL or + * only the GPL Version 2, indicate your decision by adding "[Contributor] + * elects to include this software in this distribution under the [CDDL or GPL + * Version 2] license." If you don't indicate a single choice of license, a + * recipient has the option to distribute your version of this file under + * either the CDDL, the GPL Version 2 or to extend the choice of license to + * its licensees as provided above. However, if you add GPL Version 2 code + * and therefore, elected the GPL Version 2 license, then the option applies + * only if the new code is made subject to such option by the copyright + * holder. + */ +package org.glassfish.jersey.tests.integration.jersey2704.services; + +import org.glassfish.hk2.api.ServiceLocator; +import org.jvnet.hk2.annotations.Service; + + +/** + * This service is registered in the {@link ServiceLocator} and therefore + * can be used in Jersey resources. + * + * @author Bartosz Firyn (bartoszfiryn at gmail.com) + */ +@Service +public class HappyService { + +} diff --git a/tests/integration/jersey-2704/src/main/java/org/glassfish/jersey/tests/integration/jersey2704/services/SadService.java b/tests/integration/jersey-2704/src/main/java/org/glassfish/jersey/tests/integration/jersey2704/services/SadService.java new file mode 100644 index 0000000000..7572c93167 --- /dev/null +++ b/tests/integration/jersey-2704/src/main/java/org/glassfish/jersey/tests/integration/jersey2704/services/SadService.java @@ -0,0 +1,55 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright (c) 2012-2014 Oracle and/or its affiliates. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common Development + * and Distribution License("CDDL") (collectively, the "License"). You + * may not use this file except in compliance with the License. You can + * obtain a copy of the License at + * http://glassfish.java.net/public/CDDL+GPL_1_1.html + * or packager/legal/LICENSE.txt. See the License for the specific + * language governing permissions and limitations under the License. + * + * When distributing the software, include this License Header Notice in each + * file and include the License file at packager/legal/LICENSE.txt. + * + * GPL Classpath Exception: + * Oracle designates this particular file as subject to the "Classpath" + * exception as provided by Oracle in the GPL Version 2 section of the License + * file that accompanied this code. + * + * Modifications: + * If applicable, add the following below the License Header, with the fields + * enclosed by brackets [] replaced by your own identifying information: + * "Portions Copyright [year] [name of copyright owner]" + * + * Contributor(s): + * If you wish your version of this file to be governed by only the CDDL or + * only the GPL Version 2, indicate your decision by adding "[Contributor] + * elects to include this software in this distribution under the [CDDL or GPL + * Version 2] license." If you don't indicate a single choice of license, a + * recipient has the option to distribute your version of this file under + * either the CDDL, the GPL Version 2 or to extend the choice of license to + * its licensees as provided above. However, if you add GPL Version 2 code + * and therefore, elected the GPL Version 2 license, then the option applies + * only if the new code is made subject to such option by the copyright + * holder. + */ +package org.glassfish.jersey.tests.integration.jersey2704.services; + +import org.glassfish.hk2.api.ServiceLocator; +import org.jvnet.hk2.annotations.Service; + + +/** + * This service is not registered in {@link ServiceLocator} and therefore cannot + * be used in the Jersey resources. + * + * @author Bartosz Firyn (bartoszfiryn at gmail.com) + */ +@Service +public class SadService { + +} diff --git a/tests/integration/jersey-2704/src/main/webapp/WEB-INF/web.xml b/tests/integration/jersey-2704/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000000..ca47f1e9f2 --- /dev/null +++ b/tests/integration/jersey-2704/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,61 @@ + + + + + + org.glassfish.jersey.tests.integration.jersey2704.ServiceLocatorSetup + + + test + org.glassfish.jersey.servlet.ServletContainer + + javax.ws.rs.Application + org.glassfish.jersey.tests.integration.jersey2704.TestApplication + + 1 + + + test + /* + + diff --git a/tests/integration/jersey-2704/src/test/java/org/glassfish/jersey/tests/integration/jersey2704/Jersey2704ITCase.java b/tests/integration/jersey-2704/src/test/java/org/glassfish/jersey/tests/integration/jersey2704/Jersey2704ITCase.java new file mode 100644 index 0000000000..27cf22b7d7 --- /dev/null +++ b/tests/integration/jersey-2704/src/test/java/org/glassfish/jersey/tests/integration/jersey2704/Jersey2704ITCase.java @@ -0,0 +1,121 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright (c) 2012-2014 Oracle and/or its affiliates. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common Development + * and Distribution License("CDDL") (collectively, the "License"). You + * may not use this file except in compliance with the License. You can + * obtain a copy of the License at + * http://glassfish.java.net/public/CDDL+GPL_1_1.html + * or packager/legal/LICENSE.txt. See the License for the specific + * language governing permissions and limitations under the License. + * + * When distributing the software, include this License Header Notice in each + * file and include the License file at packager/legal/LICENSE.txt. + * + * GPL Classpath Exception: + * Oracle designates this particular file as subject to the "Classpath" + * exception as provided by Oracle in the GPL Version 2 section of the License + * file that accompanied this code. + * + * Modifications: + * If applicable, add the following below the License Header, with the fields + * enclosed by brackets [] replaced by your own identifying information: + * "Portions Copyright [year] [name of copyright owner]" + * + * Contributor(s): + * If you wish your version of this file to be governed by only the CDDL or + * only the GPL Version 2, indicate your decision by adding "[Contributor] + * elects to include this software in this distribution under the [CDDL or GPL + * Version 2] license." If you don't indicate a single choice of license, a + * recipient has the option to distribute your version of this file under + * either the CDDL, the GPL Version 2 or to extend the choice of license to + * its licensees as provided above. However, if you add GPL Version 2 code + * and therefore, elected the GPL Version 2 license, then the option applies + * only if the new code is made subject to such option by the copyright + * holder. + */ +package org.glassfish.jersey.tests.integration.jersey2704; + +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.URL; + +import javax.ws.rs.core.Application; + +import org.glassfish.hk2.api.ServiceLocator; +import org.glassfish.jersey.test.JerseyTest; +import org.glassfish.jersey.test.external.ExternalTestContainerFactory; +import org.glassfish.jersey.test.spi.TestContainerException; +import org.glassfish.jersey.test.spi.TestContainerFactory; +import org.glassfish.jersey.tests.integration.jersey2704.services.HappyService; +import org.glassfish.jersey.tests.integration.jersey2704.services.SadService; +import org.junit.Assert; +import org.junit.Test; + + +/** + * This test case is to cover enhancement implemented in JERSEY-2704. The goal of this enhancement + * is to give users possibility to register main {@link ServiceLocator} in the servlet context, so + * it can be later used by Jersey. This creates the opportunity to wire Jersey-specific classes with + * the services created outside the Jersey context. + * + * @author Bartosz Firyn (bartoszfiryn at gmail.com) + */ +public class Jersey2704ITCase extends JerseyTest { + + @Override + protected Application configure() { + return new TestApplication(); + } + + @Override + protected TestContainerFactory getTestContainerFactory() throws TestContainerException { + return new ExternalTestContainerFactory(); + } + + /** + * Invokes REST endpoint to check whether specific class service is registered in the + * {@link ServiceLocator}. + * + * @param service the service class + * @return HTTP status code, 200 when service is available and 600 otherwise + * @throws IOException in case of problems with HTTP communication + */ + private int test(Class service) throws IOException { + + String name = service.getCanonicalName(); + String path = getBaseUri().toString() + "test/" + name; + + HttpURLConnection connection = (HttpURLConnection) new URL(path).openConnection(); + connection.setRequestMethod("GET"); + connection.connect(); + connection.disconnect(); + + return connection.getResponseCode(); + } + + /** + * Test to cover sunny day scenario, i.e. specific service has been registered in the parent + * {@link ServiceLocator} so it will be available in the one that is used in Jersey context. + * + * @throws IOException + */ + @Test + public void testCorrectInjection() throws IOException { + Assert.assertEquals(200, test(HappyService.class)); + } + + /** + * Test to cover rainy day scenario, i.e. specific service has not been registered in the + * parent {@link ServiceLocator} so it cannot be used to wire Jersey classes. + * + * @throws IOException + */ + @Test + public void testMisingInjection() throws IOException { + Assert.assertEquals(600, test(SadService.class)); + } +} diff --git a/tests/integration/pom.xml b/tests/integration/pom.xml index 6fd46dca2c..1fcaf74055 100644 --- a/tests/integration/pom.xml +++ b/tests/integration/pom.xml @@ -87,6 +87,7 @@ jersey-2421 jersey-2551 jersey-2612 + jersey-2704 j-59-ejb-lib j-59-cdi-war j-59-ear