Skip to content

Commit 7f4058b

Browse files
committed
Allow for org.glassfish.jersey.servlet.ServletContainer class name without init parameters in web.xml
Signed-off-by: Jan Supol <jan.supol@oracle.com>
1 parent 75ac1d1 commit 7f4058b

File tree

7 files changed

+286
-5
lines changed

7 files changed

+286
-5
lines changed

containers/jersey-servlet/src/main/java/org/glassfish/jersey/servlet/init/JerseyServletContainerInitializer.java

+29-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2019 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2020 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -128,12 +128,15 @@ public void onStartup(Set<Class<?>> classes, final ServletContext servletContext
128128
}
129129

130130
private void onStartupImpl(final Set<Class<?>> classes, final ServletContext servletContext) throws ServletException {
131+
final Set<ServletRegistration> registrationsWithApplication = new HashSet<>();
132+
131133
// first see if there are any application classes in the web app
132134
for (final Class<? extends Application> applicationClass : getApplicationClasses(classes)) {
133135
final ServletRegistration servletRegistration = servletContext.getServletRegistration(applicationClass.getName());
134136

135137
if (servletRegistration != null) {
136138
addServletWithExistingRegistration(servletContext, servletRegistration, applicationClass, classes);
139+
registrationsWithApplication.add(servletRegistration);
137140
} else {
138141
// Servlet is not registered with app name or the app name is used to register a different servlet
139142
// check if some servlet defines the app in init params
@@ -145,17 +148,21 @@ private void onStartupImpl(final Set<Class<?>> classes, final ServletContext ser
145148
if (sr instanceof ServletRegistration) {
146149
addServletWithExistingRegistration(servletContext, (ServletRegistration) sr,
147150
applicationClass, classes);
151+
registrationsWithApplication.add((ServletRegistration) sr);
148152
}
149153
}
150154
} else {
151155
// app not handled by any servlet/filter -> add it
152-
addServletWithApplication(servletContext, applicationClass, classes);
156+
final ServletRegistration sr = addServletWithApplication(servletContext, applicationClass, classes);
157+
if (sr != null) {
158+
registrationsWithApplication.add(sr);
159+
}
153160
}
154161
}
155162
}
156163

157164
// check for javax.ws.rs.core.Application registration
158-
addServletWithDefaultConfiguration(servletContext, classes);
165+
addServletWithDefaultConfiguration(servletContext, registrationsWithApplication, classes);
159166
}
160167

161168
/**
@@ -211,12 +218,14 @@ private static void collectJaxRsRegistrations(final Map<String, ? extends Regist
211218
* Enhance default servlet (named {@link Application}) configuration.
212219
*/
213220
private static void addServletWithDefaultConfiguration(final ServletContext context,
221+
final Set<ServletRegistration> registrationsWithApplication,
214222
final Set<Class<?>> classes) throws ServletException {
215223

216224
ServletRegistration registration = context.getServletRegistration(Application.class.getName());
217225

226+
final Set<Class<?>> appClasses = getRootResourceAndProviderClasses(classes);
227+
218228
if (registration != null) {
219-
final Set<Class<?>> appClasses = getRootResourceAndProviderClasses(classes);
220229
final ResourceConfig resourceConfig = ResourceConfig.forApplicationClass(ResourceConfig.class, appClasses)
221230
.addProperties(getInitParams(registration))
222231
.addProperties(Utils.getContextParams(context));
@@ -239,13 +248,26 @@ private static void addServletWithDefaultConfiguration(final ServletContext cont
239248
}
240249
}
241250
}
251+
252+
// make <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> without init params
253+
// work the same as <servlet-name>javax.ws.rs.Application</servlet-name>
254+
for (ServletRegistration servletRegistration : context.getServletRegistrations().values()) {
255+
if (isJerseyServlet(servletRegistration.getClassName())
256+
&& servletRegistration != registration
257+
&& !registrationsWithApplication.contains(servletRegistration)
258+
&& getInitParams(servletRegistration).isEmpty()) {
259+
final ResourceConfig resourceConfig = ResourceConfig.forApplicationClass(ResourceConfig.class, appClasses)
260+
.addProperties(Utils.getContextParams(context));
261+
Utils.store(resourceConfig, context, servletRegistration.getName());
262+
}
263+
}
242264
}
243265

244266
/**
245267
* Add new servlet according to {@link Application} subclass with {@link ApplicationPath} annotation or existing
246268
* {@code servlet-mapping}.
247269
*/
248-
private static void addServletWithApplication(final ServletContext context,
270+
private static ServletRegistration addServletWithApplication(final ServletContext context,
249271
final Class<? extends Application> clazz,
250272
final Set<Class<?>> defaultClasses) throws ServletException {
251273
final ApplicationPath ap = clazz.getAnnotation(ApplicationPath.class);
@@ -266,7 +288,9 @@ private static void addServletWithApplication(final ServletContext context,
266288
} else {
267289
LOGGER.log(Level.WARNING, LocalizationMessages.JERSEY_APP_MAPPING_CONFLICT(clazz.getName(), mapping));
268290
}
291+
return dsr;
269292
}
293+
return null;
270294
}
271295

272296
/**

tests/integration/pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
<module>servlet-3-init-6</module>
123123
<module>servlet-3-init-7</module>
124124
<module>servlet-3-init-8</module>
125+
<module>servlet-3-init-9</module>
125126
<module>servlet-3-init-provider</module>
126127
<module>servlet-3-params</module>
127128
<module>servlet-3-sse-1</module>
+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
5+
6+
This program and the accompanying materials are made available under the
7+
terms of the Eclipse Public License v. 2.0, which is available at
8+
http://www.eclipse.org/legal/epl-2.0.
9+
10+
This Source Code may also be made available under the following Secondary
11+
Licenses when the conditions for such availability set forth in the
12+
Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
13+
version 2 with the GNU Classpath Exception, which is available at
14+
https://www.gnu.org/software/classpath/license.html.
15+
16+
SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
17+
18+
-->
19+
20+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
21+
<modelVersion>4.0.0</modelVersion>
22+
23+
<parent>
24+
<groupId>org.glassfish.jersey.tests.integration</groupId>
25+
<artifactId>project</artifactId>
26+
<version>2.33-SNAPSHOT</version>
27+
</parent>
28+
29+
<artifactId>servlet-3-init-9</artifactId>
30+
<packaging>war</packaging>
31+
<name>jersey-tests-integration-servlet-3-init-9</name>
32+
33+
<description>Servlet integration test - servlet-3-init-9 - no Application subclass is present;
34+
dynamically add a servlet for class-name=org.glassfish.jersey.servlet.ServletContainer
35+
servlet-mapping in web.xml</description>
36+
37+
<dependencies>
38+
<dependency>
39+
<groupId>org.glassfish.jersey.containers</groupId>
40+
<artifactId>jersey-container-servlet</artifactId>
41+
</dependency>
42+
<dependency>
43+
<groupId>javax.servlet</groupId>
44+
<artifactId>javax.servlet-api</artifactId>
45+
<version>${servlet3.version}</version>
46+
<scope>provided</scope>
47+
</dependency>
48+
49+
<dependency>
50+
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
51+
<artifactId>jersey-test-framework-provider-external</artifactId>
52+
<scope>test</scope>
53+
</dependency>
54+
</dependencies>
55+
56+
<build>
57+
<plugins>
58+
<plugin>
59+
<groupId>org.apache.maven.plugins</groupId>
60+
<artifactId>maven-compiler-plugin</artifactId>
61+
</plugin>
62+
<plugin>
63+
<groupId>org.apache.maven.plugins</groupId>
64+
<artifactId>maven-failsafe-plugin</artifactId>
65+
</plugin>
66+
<plugin>
67+
<groupId>org.mortbay.jetty</groupId>
68+
<artifactId>jetty-maven-plugin</artifactId>
69+
</plugin>
70+
</plugins>
71+
</build>
72+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0, which is available at
6+
* http://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* This Source Code may also be made available under the following Secondary
9+
* Licenses when the conditions for such availability set forth in the
10+
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11+
* version 2 with the GNU Classpath Exception, which is available at
12+
* https://www.gnu.org/software/classpath/license.html.
13+
*
14+
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15+
*/
16+
17+
package org.glassfish.jersey.tests.integration.servlet_3_init_9;
18+
19+
import javax.ws.rs.GET;
20+
import javax.ws.rs.Path;
21+
import javax.ws.rs.Produces;
22+
23+
@Path("helloworld")
24+
public class HelloWorldResource {
25+
26+
@GET
27+
@Produces("text/plain")
28+
public Hello get() {
29+
return new Hello();
30+
}
31+
32+
public static class Hello {
33+
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0, which is available at
6+
* http://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* This Source Code may also be made available under the following Secondary
9+
* Licenses when the conditions for such availability set forth in the
10+
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11+
* version 2 with the GNU Classpath Exception, which is available at
12+
* https://www.gnu.org/software/classpath/license.html.
13+
*
14+
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15+
*/
16+
17+
package org.glassfish.jersey.tests.integration.servlet_3_init_9;
18+
19+
import java.io.IOException;
20+
import java.io.OutputStream;
21+
import java.lang.annotation.Annotation;
22+
import java.lang.reflect.Type;
23+
24+
import javax.ws.rs.WebApplicationException;
25+
import javax.ws.rs.core.MediaType;
26+
import javax.ws.rs.core.MultivaluedMap;
27+
import javax.ws.rs.ext.MessageBodyWriter;
28+
import javax.ws.rs.ext.Provider;
29+
30+
@Provider
31+
public class HelloWriter implements MessageBodyWriter<HelloWorldResource.Hello> {
32+
33+
@Override
34+
public boolean isWriteable(final Class<?> type, final Type genericType, final Annotation[] annotations,
35+
final MediaType mediaType) {
36+
return type.equals(HelloWorldResource.Hello.class);
37+
}
38+
39+
@Override
40+
public long getSize(final HelloWorldResource.Hello hello, final Class<?> type, final Type genericType,
41+
final Annotation[] annotations, final MediaType mediaType) {
42+
return -1;
43+
}
44+
45+
@Override
46+
public void writeTo(final HelloWorldResource.Hello hello,
47+
final Class<?> type,
48+
final Type genericType,
49+
final Annotation[] annotations,
50+
final MediaType mediaType,
51+
final MultivaluedMap<String, Object> httpHeaders,
52+
final OutputStream entityStream) throws IOException, WebApplicationException {
53+
entityStream.write(("Hello World! " + this.getClass().getPackage().getName()).getBytes());
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
5+
6+
This program and the accompanying materials are made available under the
7+
terms of the Eclipse Public License v. 2.0, which is available at
8+
http://www.eclipse.org/legal/epl-2.0.
9+
10+
This Source Code may also be made available under the following Secondary
11+
Licenses when the conditions for such availability set forth in the
12+
Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
13+
version 2 with the GNU Classpath Exception, which is available at
14+
https://www.gnu.org/software/classpath/license.html.
15+
16+
SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
17+
18+
-->
19+
20+
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
21+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
22+
version="3.0">
23+
<servlet>
24+
<servlet-name>myApplication</servlet-name>
25+
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
26+
</servlet>
27+
<servlet-mapping>
28+
<servlet-name>myApplication</servlet-name>
29+
<url-pattern>/a/*</url-pattern>
30+
</servlet-mapping>
31+
32+
<servlet>
33+
<servlet-name>myApplication2</servlet-name>
34+
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
35+
</servlet>
36+
<servlet-mapping>
37+
<servlet-name>myApplication2</servlet-name>
38+
<url-pattern>/b/*</url-pattern>
39+
</servlet-mapping>
40+
</web-app>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (c) 2014, 2019 Oracle and/or its affiliates. All rights reserved.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0, which is available at
6+
* http://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* This Source Code may also be made available under the following Secondary
9+
* Licenses when the conditions for such availability set forth in the
10+
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11+
* version 2 with the GNU Classpath Exception, which is available at
12+
* https://www.gnu.org/software/classpath/license.html.
13+
*
14+
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15+
*/
16+
17+
package org.glassfish.jersey.tests.integration.servlet_3_init_9;
18+
19+
import org.glassfish.jersey.server.ResourceConfig;
20+
import org.glassfish.jersey.test.JerseyTest;
21+
import org.glassfish.jersey.test.TestProperties;
22+
import org.glassfish.jersey.test.external.ExternalTestContainerFactory;
23+
import org.glassfish.jersey.test.spi.TestContainerException;
24+
import org.glassfish.jersey.test.spi.TestContainerFactory;
25+
26+
import org.junit.Test;
27+
import static org.junit.Assert.assertTrue;
28+
29+
public class HelloWorldResourceITCase extends JerseyTest {
30+
31+
@Override
32+
protected ResourceConfig configure() {
33+
enable(TestProperties.LOG_TRAFFIC);
34+
35+
return new ResourceConfig(HelloWorldResource.class);
36+
}
37+
38+
@Override
39+
protected TestContainerFactory getTestContainerFactory() throws TestContainerException {
40+
return new ExternalTestContainerFactory();
41+
}
42+
43+
@Test
44+
public void testHelloWorldA() throws Exception {
45+
String s = target().path("a").path("helloworld").request().get(String.class);
46+
assertTrue(s.equals("Hello World! " + this.getClass().getPackage().getName()));
47+
}
48+
49+
@Test
50+
public void testHelloWorldB() throws Exception {
51+
String s = target().path("b").path("helloworld").request().get(String.class);
52+
assertTrue(s.equals("Hello World! " + this.getClass().getPackage().getName()));
53+
}
54+
}

0 commit comments

Comments
 (0)