1
1
/*
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.
3
3
*
4
4
* This program and the accompanying materials are made available under the
5
5
* 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
128
128
}
129
129
130
130
private void onStartupImpl (final Set <Class <?>> classes , final ServletContext servletContext ) throws ServletException {
131
+ final Set <ServletRegistration > registrationsWithApplication = new HashSet <>();
132
+
131
133
// first see if there are any application classes in the web app
132
134
for (final Class <? extends Application > applicationClass : getApplicationClasses (classes )) {
133
135
final ServletRegistration servletRegistration = servletContext .getServletRegistration (applicationClass .getName ());
134
136
135
137
if (servletRegistration != null ) {
136
138
addServletWithExistingRegistration (servletContext , servletRegistration , applicationClass , classes );
139
+ registrationsWithApplication .add (servletRegistration );
137
140
} else {
138
141
// Servlet is not registered with app name or the app name is used to register a different servlet
139
142
// check if some servlet defines the app in init params
@@ -145,17 +148,21 @@ private void onStartupImpl(final Set<Class<?>> classes, final ServletContext ser
145
148
if (sr instanceof ServletRegistration ) {
146
149
addServletWithExistingRegistration (servletContext , (ServletRegistration ) sr ,
147
150
applicationClass , classes );
151
+ registrationsWithApplication .add ((ServletRegistration ) sr );
148
152
}
149
153
}
150
154
} else {
151
155
// 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
+ }
153
160
}
154
161
}
155
162
}
156
163
157
164
// check for javax.ws.rs.core.Application registration
158
- addServletWithDefaultConfiguration (servletContext , classes );
165
+ addServletWithDefaultConfiguration (servletContext , registrationsWithApplication , classes );
159
166
}
160
167
161
168
/**
@@ -211,12 +218,14 @@ private static void collectJaxRsRegistrations(final Map<String, ? extends Regist
211
218
* Enhance default servlet (named {@link Application}) configuration.
212
219
*/
213
220
private static void addServletWithDefaultConfiguration (final ServletContext context ,
221
+ final Set <ServletRegistration > registrationsWithApplication ,
214
222
final Set <Class <?>> classes ) throws ServletException {
215
223
216
224
ServletRegistration registration = context .getServletRegistration (Application .class .getName ());
217
225
226
+ final Set <Class <?>> appClasses = getRootResourceAndProviderClasses (classes );
227
+
218
228
if (registration != null ) {
219
- final Set <Class <?>> appClasses = getRootResourceAndProviderClasses (classes );
220
229
final ResourceConfig resourceConfig = ResourceConfig .forApplicationClass (ResourceConfig .class , appClasses )
221
230
.addProperties (getInitParams (registration ))
222
231
.addProperties (Utils .getContextParams (context ));
@@ -239,13 +248,26 @@ private static void addServletWithDefaultConfiguration(final ServletContext cont
239
248
}
240
249
}
241
250
}
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
+ }
242
264
}
243
265
244
266
/**
245
267
* Add new servlet according to {@link Application} subclass with {@link ApplicationPath} annotation or existing
246
268
* {@code servlet-mapping}.
247
269
*/
248
- private static void addServletWithApplication (final ServletContext context ,
270
+ private static ServletRegistration addServletWithApplication (final ServletContext context ,
249
271
final Class <? extends Application > clazz ,
250
272
final Set <Class <?>> defaultClasses ) throws ServletException {
251
273
final ApplicationPath ap = clazz .getAnnotation (ApplicationPath .class );
@@ -266,7 +288,9 @@ private static void addServletWithApplication(final ServletContext context,
266
288
} else {
267
289
LOGGER .log (Level .WARNING , LocalizationMessages .JERSEY_APP_MAPPING_CONFLICT (clazz .getName (), mapping ));
268
290
}
291
+ return dsr ;
269
292
}
293
+ return null ;
270
294
}
271
295
272
296
/**
0 commit comments