| 1 |
/* |
| 2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
| 3 |
* |
| 4 |
* Copyright (c) 2010-2011 Oracle and/or its affiliates. All rights reserved. |
| 5 |
* |
| 6 |
* The contents of this file are subject to the terms of either the GNU |
| 7 |
* General Public License Version 2 only ("GPL") or the Common Development |
| 8 |
* and Distribution License("CDDL") (collectively, the "License"). You |
| 9 |
* may not use this file except in compliance with the License. You can |
| 10 |
* obtain a copy of the License at |
| 11 |
* http://glassfish.java.net/public/CDDL+GPL_1_1.html |
| 12 |
* or packager/legal/LICENSE.txt. See the License for the specific |
| 13 |
* language governing permissions and limitations under the License. |
| 14 |
* |
| 15 |
* When distributing the software, include this License Header Notice in each |
| 16 |
* file and include the License file at packager/legal/LICENSE.txt. |
| 17 |
* |
| 18 |
* GPL Classpath Exception: |
| 19 |
* Oracle designates this particular file as subject to the "Classpath" |
| 20 |
* exception as provided by Oracle in the GPL Version 2 section of the License |
| 21 |
* file that accompanied this code. |
| 22 |
* |
| 23 |
* Modifications: |
| 24 |
* If applicable, add the following below the License Header, with the fields |
| 25 |
* enclosed by brackets [] replaced by your own identifying information: |
| 26 |
* "Portions Copyright [year] [name of copyright owner]" |
| 27 |
* |
| 28 |
* Contributor(s): |
| 29 |
* If you wish your version of this file to be governed by only the CDDL or |
| 30 |
* only the GPL Version 2, indicate your decision by adding "[Contributor] |
| 31 |
* elects to include this software in this distribution under the [CDDL or GPL |
| 32 |
* Version 2] license." If you don't indicate a single choice of license, a |
| 33 |
* recipient has the option to distribute your version of this file under |
| 34 |
* either the CDDL, the GPL Version 2 or to extend the choice of license to |
| 35 |
* its licensees as provided above. However, if you add GPL Version 2 code |
| 36 |
* and therefore, elected the GPL Version 2 license, then the option applies |
| 37 |
* only if the new code is made subject to such option by the copyright |
| 38 |
* holder. |
| 39 |
*/ |
| 40 |
package com.sun.jersey.server.impl.container.servlet; |
| 41 |
|
| 42 |
import com.sun.jersey.api.core.DefaultResourceConfig; |
| 43 |
import com.sun.jersey.api.core.ResourceConfig; |
| 44 |
import com.sun.jersey.server.impl.application.DeferredResourceConfig; |
| 45 |
import com.sun.jersey.spi.container.servlet.ServletContainer; |
| 46 |
|
| 47 |
import javax.servlet.ServletContainerInitializer; |
| 48 |
import javax.servlet.ServletContext; |
| 49 |
import javax.servlet.ServletRegistration; |
| 50 |
import javax.servlet.annotation.HandlesTypes; |
| 51 |
import javax.ws.rs.ApplicationPath; |
| 52 |
import javax.ws.rs.Path; |
| 53 |
import javax.ws.rs.core.Application; |
| 54 |
import javax.ws.rs.ext.Provider; |
| 55 |
import java.util.ArrayList; |
| 56 |
import java.util.Collections; |
| 57 |
import java.util.HashMap; |
| 58 |
import java.util.LinkedHashSet; |
| 59 |
import java.util.List; |
| 60 |
import java.util.Map; |
| 61 |
import java.util.Set; |
| 62 |
import java.util.logging.Logger; |
| 63 |
|
| 64 |
/* |
| 65 |
It is RECOMMENDED that implementations support the Servlet 3 framework |
| 66 |
pluggability mechanism to enable portability between containers and to avail |
| 67 |
themselves of container-supplied class scanning facilities. |
| 68 |
When using the pluggability mechanism the following conditions MUST be met: |
| 69 |
|
| 70 |
- If no Application subclass is present the added servlet MUST be |
| 71 |
named "javax.ws.rs.core.Application" and all root resource classes and |
| 72 |
providers packaged in the web application MUST be included in the published |
| 73 |
JAX-RS application. The application MUST be packaged with a web.xml that |
| 74 |
specifies a servlet mapping for the added servlet. |
| 75 |
|
| 76 |
- If an Application subclass is present and there is already a servlet defined |
| 77 |
that has a servlet initialization parameter named "javax.ws.rs.Application" |
| 78 |
whose value is the fully qualified name of the Application subclass then no |
| 79 |
servlet should be added by the JAX-RS implementation's ContainerInitializer |
| 80 |
since the application is already being handled by an existing servlet. |
| 81 |
|
| 82 |
- If an application subclass is present that is not being handled by an |
| 83 |
existing servlet then the servlet added by the ContainerInitializer MUST be |
| 84 |
named with the fully qualified name of the Application subclass. If the |
| 85 |
Application subclass is annotated with @PathPrefix and no servlet-mapping |
| 86 |
exists for the added servlet then a new servlet mapping is added with the |
| 87 |
value of the @PathPrefix annotation with "/*" appended otherwise the existing |
| 88 |
mapping is used. If the Application subclass is not annotated with @PathPrefix |
| 89 |
then the application MUST be packaged with a web.xml that specifies a servlet |
| 90 |
mapping for the added servlet. It is an error for more than one Application |
| 91 |
to be deployed at the same effective servlet mapping. |
| 92 |
|
| 93 |
In either of the latter two cases, if both Application#getClasses and |
| 94 |
Application#getSingletons return an empty list then all root resource classes |
| 95 |
and providers packaged in the web application MUST be included in the |
| 96 |
published JAX-RS application. If either getClasses or getSingletons return a |
| 97 |
non-empty list then only those classes or singletons returned MUST be included |
| 98 |
in the published JAX-RS application. |
| 99 |
|
| 100 |
If not using the Servlet 3 framework pluggability mechanism |
| 101 |
(e.g. in a pre-Servet 3.0 container), the servlet-class or filter-class |
| 102 |
element of the web.xml descriptor SHOULD name the JAX-RS |
| 103 |
implementation-supplied Servlet or Filter class respectively. The |
| 104 |
application-supplied subclass of Application SHOULD be identied using an |
| 105 |
init-param with a param-name of javax.ws.rs.Application. |
| 106 |
|
| 107 |
*/ |
| 108 |
/** |
| 109 |
* |
| 110 |
* @author [email protected] |
| 111 |
*/ |
| 112 |
@HandlesTypes({Path.class, Provider.class, Application.class, ApplicationPath.class}) |
| 113 |
public class JerseyServletContainerInitializer implements ServletContainerInitializer { |
| 114 |
|
| 115 |
private static final Logger LOGGER = |
| 116 |
Logger.getLogger(JerseyServletContainerInitializer.class.getName()); |
| 117 |
|
| 118 |
@Override |
| 119 |
public void onStartup(Set<Class<?>> classes, ServletContext sc) { |
| 120 |
if (classes == null) { |
| 121 |
classes = Collections.<Class<?>>emptySet(); |
| 122 |
} |
| 123 |
final int nOfRegisterations = sc.getServletRegistrations().size(); |
| 124 |
for (Class<? extends Application> a : getApplicationClasses(classes)) { |
| 125 |
final ServletRegistration appReg = sc.getServletRegistration(a.getName()); |
| 126 |
if (appReg != null) { |
| 127 |
// Servlet is registered with app name |
| 128 |
|
| 129 |
addServletWithExistingRegistration(sc, appReg, a, classes); |
| 130 |
} else { |
| 131 |
// Servlet is not registered with app name |
| 132 |
|
| 133 |
final List<ServletRegistration> srs = getInitParamDeclaredRegistrations(sc, a); |
| 134 |
if (!srs.isEmpty()) { |
| 135 |
// List of servlets registered with app name in init param |
| 136 |
|
| 137 |
for (ServletRegistration sr : srs) { |
| 138 |
addServletWithExistingRegistration(sc, sr, a, classes); |
| 139 |
} |
| 140 |
} else { |
| 141 |
addServletWithApplication(sc, a, classes); |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
if (nOfRegisterations == sc.getServletRegistrations().size()) { |
| 147 |
// No app was registered |
| 148 |
addServletWithDefaultConfiguration(sc, classes); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
private List<ServletRegistration> getInitParamDeclaredRegistrations(ServletContext sc, Class<? extends Application> a) { |
| 153 |
final List<ServletRegistration> srs = new ArrayList<ServletRegistration>(1); |
| 154 |
for (ServletRegistration sr : sc.getServletRegistrations().values()) { |
| 155 |
|
| 156 |
Map<String, String> ips = sr.getInitParameters(); |
| 157 |
if (ips.containsKey(ServletContainer.APPLICATION_CONFIG_CLASS)) { |
| 158 |
if (ips.get(ServletContainer.APPLICATION_CONFIG_CLASS).equals(a.getName())) { |
| 159 |
if (sr.getClassName() == null) { |
| 160 |
srs.add(sr); |
| 161 |
} |
| 162 |
} |
| 163 |
} else if (ips.containsKey(ServletContainer.RESOURCE_CONFIG_CLASS)) { |
| 164 |
if (ips.get(ServletContainer.RESOURCE_CONFIG_CLASS).equals(a.getName())) { |
| 165 |
if (sr.getClassName() == null) { |
| 166 |
srs.add(sr); |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
return srs; |
| 173 |
} |
| 174 |
|
| 175 |
private void addServletWithDefaultConfiguration(ServletContext sc, Set<Class<?>> classes) { |
| 176 |
ServletRegistration appReg = sc.getServletRegistration(Application.class.getName()); |
| 177 |
if (appReg != null && appReg.getClassName() == null) { |
| 178 |
final Set<Class<?>> x = getRootResourceAndProviderClasses(classes); |
| 179 |
final ServletContainer s = new ServletContainer( |
| 180 |
new DefaultResourceConfig(x)); |
| 181 |
appReg = sc.addServlet(appReg.getName(), s); |
| 182 |
|
| 183 |
if (appReg.getMappings().isEmpty()) { |
| 184 |
// Error |
| 185 |
LOGGER.severe("The Jersey servlet application, named " + |
| 186 |
appReg.getName() + |
| 187 |
", has no servlet mapping"); |
| 188 |
} else { |
| 189 |
LOGGER.info("Registering the Jersey servlet application, named " + |
| 190 |
appReg.getName() + |
| 191 |
", with the following root resource and provider classes: " + x); |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
private void addServletWithApplication(ServletContext sc, |
| 197 |
Class<? extends Application> a, Set<Class<?>> classes) { |
| 198 |
final ApplicationPath ap = a.getAnnotation(ApplicationPath.class); |
| 199 |
if (ap != null) { |
| 200 |
// App is annotated with ApplicationPath |
| 201 |
|
| 202 |
final ServletContainer s = new ServletContainer( |
| 203 |
new DeferredResourceConfig(a, getRootResourceAndProviderClasses(classes))); |
| 204 |
|
| 205 |
final String mapping = createMappingPath(ap); |
| 206 |
if (!mappingExists(sc, mapping)) { |
| 207 |
sc.addServlet(a.getName(), s). |
| 208 |
addMapping(mapping); |
| 209 |
|
| 210 |
LOGGER.info("Registering the Jersey servlet application, named " + |
| 211 |
a.getName() + |
| 212 |
", at the servlet mapping, " |
| 213 |
+ mapping + |
| 214 |
", with the Application class of the same name"); |
| 215 |
} else { |
| 216 |
LOGGER.severe("Mapping conflict. " + |
| 217 |
"A Servlet declaration exists with same mapping as the Jersey servlet application, named " + |
| 218 |
a.getName() + |
| 219 |
", at the servlet mapping, " |
| 220 |
+ mapping + |
| 221 |
". The Jersey servlet is not deployed."); |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
private void addServletWithExistingRegistration(ServletContext sc, ServletRegistration sr, |
| 227 |
Class<? extends Application> a, Set<Class<?>> classes) { |
| 228 |
if (sr.getClassName() == null) { |
| 229 |
|
| 230 |
final ResourceConfig rc = new DeferredResourceConfig(a, getRootResourceAndProviderClasses(classes)); |
| 231 |
final Map<String, Object> initParams = new HashMap<String, Object>(); |
| 232 |
for(Map.Entry<String, String> entry : sr.getInitParameters().entrySet()) |
| 233 |
initParams.put(entry.getKey(), entry.getValue()); |
| 234 |
|
| 235 |
rc.setPropertiesAndFeatures(initParams); |
| 236 |
|
| 237 |
final ServletContainer s = new ServletContainer(rc); |
| 238 |
sr = sc.addServlet(a.getName(), s); |
| 239 |
if (sr.getMappings().isEmpty()) { |
| 240 |
final ApplicationPath ap = a.getAnnotation(ApplicationPath.class); |
| 241 |
if (ap != null) { |
| 242 |
final String mapping = createMappingPath(ap); |
| 243 |
if (!mappingExists(sc, mapping)) { |
| 244 |
sr.addMapping(mapping); |
| 245 |
|
| 246 |
LOGGER.info("Registering the Jersey servlet application, named " + |
| 247 |
a.getName() + |
| 248 |
", at the servlet mapping, " |
| 249 |
+ mapping + |
| 250 |
", with the Application class of the same name"); |
| 251 |
} else { |
| 252 |
LOGGER.severe("Mapping conflict. " + |
| 253 |
"A Servlet registration exists with same mapping as the Jersey servlet application, named " + |
| 254 |
a.getName() + |
| 255 |
", at the servlet mapping, " |
| 256 |
+ mapping + |
| 257 |
". The Jersey servlet is not deployed."); |
| 258 |
} |
| 259 |
} else { |
| 260 |
// Error |
| 261 |
LOGGER.severe("The Jersey servlet application, named " + |
| 262 |
a.getName() + |
| 263 |
", is not annotated with " + ApplicationPath.class.getSimpleName() + " " + |
| 264 |
"and has no servlet mapping"); |
| 265 |
} |
| 266 |
} else { |
| 267 |
LOGGER.info("Registering the Jersey servlet application, named " + |
| 268 |
a.getName() + |
| 269 |
", with the Application class of the same name"); |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
private boolean mappingExists(ServletContext sc, String mapping) { |
| 275 |
for (ServletRegistration sr : sc.getServletRegistrations().values()) { |
| 276 |
for (String declaredMapping : sr.getMappings()) { |
| 277 |
if (mapping.equals(declaredMapping)) { |
| 278 |
return true; |
| 279 |
} |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
return false; |
| 284 |
} |
| 285 |
|
| 286 |
private String createMappingPath(ApplicationPath ap) { |
| 287 |
String path = ap.value(); |
| 288 |
if (!path.startsWith("/")) { |
| 289 |
path = "/" + path; |
| 290 |
} |
| 291 |
|
| 292 |
if (!path.endsWith("/*")) { |
| 293 |
if (path.endsWith("/")) { |
| 294 |
path += "*"; |
| 295 |
} else { |
| 296 |
path += "/*"; |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
return path; |
| 301 |
} |
| 302 |
|
| 303 |
private Set<Class<? extends Application>> getApplicationClasses(Set<Class<?>> classes) { |
| 304 |
Set<Class<? extends Application>> s = new LinkedHashSet<Class<? extends Application>>(); |
| 305 |
for (Class<?> c : classes) { |
| 306 |
if (Application.class != c && Application.class.isAssignableFrom(c)) { |
| 307 |
s.add(c.asSubclass(Application.class)); |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
return s; |
| 312 |
} |
| 313 |
|
| 314 |
private Set<Class<?>> getRootResourceAndProviderClasses(Set<Class<?>> classes) { |
| 315 |
// TODO filter out any classes from the Jersey jars |
| 316 |
Set<Class<?>> s = new LinkedHashSet<Class<?>>(); |
| 317 |
for (Class<?> c : classes) { |
| 318 |
if (c.isAnnotationPresent(Path.class) || c.isAnnotationPresent(Provider.class)) |
| 319 |
s.add(c); |
| 320 |
} |
| 321 |
|
| 322 |
return s; |
| 323 |
} |
| 324 |
|
| 325 |
} |