| 1 |
/* |
| 2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
| 3 |
* |
| 4 |
* Copyright (c) 2010-2014 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 |
|
| 41 |
package com.sun.jersey.spi.container.servlet; |
| 42 |
|
| 43 |
import java.io.IOException; |
| 44 |
import java.lang.reflect.Type; |
| 45 |
import java.net.URI; |
| 46 |
import java.util.ArrayList; |
| 47 |
import java.util.List; |
| 48 |
import java.util.Map; |
| 49 |
import java.util.regex.Pattern; |
| 50 |
import java.util.regex.PatternSyntaxException; |
| 51 |
|
| 52 |
import javax.ws.rs.core.Application; |
| 53 |
import javax.ws.rs.core.Context; |
| 54 |
import javax.ws.rs.core.Response; |
| 55 |
import javax.ws.rs.core.UriBuilder; |
| 56 |
|
| 57 |
import javax.servlet.Filter; |
| 58 |
import javax.servlet.FilterChain; |
| 59 |
import javax.servlet.FilterConfig; |
| 60 |
import javax.servlet.Servlet; |
| 61 |
import javax.servlet.ServletConfig; |
| 62 |
import javax.servlet.ServletContext; |
| 63 |
import javax.servlet.ServletException; |
| 64 |
import javax.servlet.ServletRequest; |
| 65 |
import javax.servlet.ServletResponse; |
| 66 |
import javax.servlet.http.HttpServlet; |
| 67 |
import javax.servlet.http.HttpServletRequest; |
| 68 |
import javax.servlet.http.HttpServletResponse; |
| 69 |
|
| 70 |
import com.sun.jersey.api.container.ContainerException; |
| 71 |
import com.sun.jersey.api.core.ClasspathResourceConfig; |
| 72 |
import com.sun.jersey.api.core.PackagesResourceConfig; |
| 73 |
import com.sun.jersey.api.core.ResourceConfig; |
| 74 |
import com.sun.jersey.api.core.servlet.WebAppResourceConfig; |
| 75 |
import com.sun.jersey.api.uri.UriComponent; |
| 76 |
import com.sun.jersey.core.spi.component.ioc.IoCComponentProviderFactory; |
| 77 |
import com.sun.jersey.server.impl.application.DeferredResourceConfig; |
| 78 |
import com.sun.jersey.spi.container.ContainerNotifier; |
| 79 |
import com.sun.jersey.spi.container.ReloadListener; |
| 80 |
import com.sun.jersey.spi.container.WebApplication; |
| 81 |
import com.sun.jersey.spi.container.WebApplicationFactory; |
| 82 |
import com.sun.jersey.spi.inject.SingletonTypeInjectableProvider; |
| 83 |
import com.sun.jersey.spi.service.ServiceFinder; |
| 84 |
|
| 85 |
|
| 86 |
/** |
| 87 |
* A {@link Servlet} or {@link Filter} for deploying root resource classes. |
| 88 |
* <p> |
| 89 |
* If this class is declared as a filter and the initialization parameter |
| 90 |
* {@link #PROPERTY_WEB_PAGE_CONTENT_REGEX} is not set |
| 91 |
* or {@link #FEATURE_FILTER_FORWARD_ON_404} is not set to true then the filter |
| 92 |
* must be declared at the last position in the filter chain as the filter will |
| 93 |
* not forward any request to a next filter (if any) in the chain. |
| 94 |
* <p> |
| 95 |
* The following sections make reference to initialization parameters. Unless |
| 96 |
* otherwise specified the initialization parameters apply to both server |
| 97 |
* and filter initialization parameters. |
| 98 |
* <p> |
| 99 |
* The servlet or filter may be configured to have an initialization |
| 100 |
* parameter "com.sun.jersey.config.property.resourceConfigClass" or |
| 101 |
* "javax.ws.rs.Application" and whose value is a |
| 102 |
* fully qualified name of a class that implements {@link ResourceConfig} or |
| 103 |
* {@link Application}. |
| 104 |
* If the concrete class has a constructor that takes a single parameter of the |
| 105 |
* type Map then the class is instantiated with that constructor and an instance |
| 106 |
* of Map that contains all the initialization parameters is passed as the |
| 107 |
* parameter. Otherwise, the class is instantiated as a singleton component |
| 108 |
* managed by the runtime, and injection may be performed (the artifacts that |
| 109 |
* may be injected are limited to injectable providers registered when |
| 110 |
* the servlet or filter is configured). |
| 111 |
* <p> |
| 112 |
* If the initialization parameter |
| 113 |
* "com.sun.jersey.config.property.resourceConfigClass" or |
| 114 |
* "javax.ws.rs.Application" is not present and a |
| 115 |
* initialization parameter "com.sun.jersey.config.property.packages" is present |
| 116 |
* (see {@link PackagesResourceConfig#PROPERTY_PACKAGES}) a new instance of |
| 117 |
* {@link PackagesResourceConfig} is created. The initialization parameter |
| 118 |
* "com.sun.jersey.config.property.packages" MUST be set to provide one or |
| 119 |
* more package names. Each package name MUST be separated by ';'. |
| 120 |
* |
| 121 |
* The package names are added as a property value to a Map instance using |
| 122 |
* the property name {@value com.sun.jersey.api.core.PackagesResourceConfig#PROPERTY_PACKAGES}. |
| 123 |
* Any additional initialization parameters are then added to the Map instance. |
| 124 |
* Then that Map instance is passed to the constructor of |
| 125 |
* {@link PackagesResourceConfig}. |
| 126 |
* |
| 127 |
* <p> |
| 128 |
* If none of the above resource configuration related initialization parameters |
| 129 |
* are present a new instance of {@link WebAppResourceConfig} is created. The |
| 130 |
* initialization parameter "com.sun.jersey.config.property.classpath" MAY be |
| 131 |
* set to provide one or more resource paths. Each path MUST be separated by ';'. |
| 132 |
* |
| 133 |
* The resource paths are added as a property value to a Map instance using |
| 134 |
* the property name {@value com.sun.jersey.api.core.ClasspathResourceConfig#PROPERTY_CLASSPATH}. |
| 135 |
* Any additional initialization parameters are then added to the Map instance. |
| 136 |
* Then that Map instance is passed to the constructor of |
| 137 |
* {@link WebAppResourceConfig}. |
| 138 |
* |
| 139 |
* If the initialization parameter is not present then the following resource |
| 140 |
* paths are utilized: |
| 141 |
* "/WEB-INF/lib" and "/WEB-INF/classes". |
| 142 |
* <p> |
| 143 |
* All initialization parameters are added as properties of the created |
| 144 |
* {@link ResourceConfig}. |
| 145 |
* <p> |
| 146 |
* A new {@link WebApplication} instance will be created and configured such |
| 147 |
* that the following classes may be injected onto a root resource, provider |
| 148 |
* and {@link Application} classes using {@link javax.ws.rs.core.Context}: |
| 149 |
* {@link HttpServletRequest}, {@link HttpServletResponse}, |
| 150 |
* {@link ServletContext}, {@link ServletConfig} and {@link WebConfig}. |
| 151 |
* If this class is used as a Servlet then the {@link ServletConfig} class may |
| 152 |
* be injected. If this class is used as a Filter then the {@link FilterConfig} |
| 153 |
* class may be injected. {@link WebConfig} may be injected to abstract |
| 154 |
* servlet or filter deployment. |
| 155 |
* <p> |
| 156 |
* A {@link IoCComponentProviderFactory} instance may be registered by extending this class |
| 157 |
* and overriding the method {@link #initiate(ResourceConfig, WebApplication)} |
| 158 |
* to initiate the {@link WebApplication} with the {@link IoCComponentProviderFactory} |
| 159 |
* instance. |
| 160 |
* |
| 161 |
*/ |
| 162 |
public class ServletContainer extends HttpServlet implements Filter { |
| 163 |
/** |
| 164 |
* The servlet initialization property whose boolean value determines |
| 165 |
* if GlassFish default error pages will be returned or not. |
| 166 |
* <p> |
| 167 |
* The default value is true. |
| 168 |
* <p> |
| 169 |
* If false then GlassFish will not return default error pages. |
| 170 |
* <p> |
| 171 |
* This property is supported on GlassFish version 3.1 or greater. |
| 172 |
* @since 1.5 |
| 173 |
*/ |
| 174 |
public static final String GLASSFISH_DEFAULT_ERROR_PAGE_RESPONSE = "org.glassfish.web.isDefaultErrorPageEnabled"; |
| 175 |
|
| 176 |
/** |
| 177 |
* The servlet initialization property whose value is a fully qualified |
| 178 |
* class name of a class that implements {@link ResourceConfig} or |
| 179 |
* {@link Application}. |
| 180 |
*/ |
| 181 |
public static final String APPLICATION_CONFIG_CLASS = |
| 182 |
"javax.ws.rs.Application"; |
| 183 |
|
| 184 |
/** |
| 185 |
* The servlet initialization property whose value is a fully qualified |
| 186 |
* class name of a class that implements {@link ResourceConfig} or |
| 187 |
* {@link Application}. |
| 188 |
*/ |
| 189 |
public static final String RESOURCE_CONFIG_CLASS = |
| 190 |
"com.sun.jersey.config.property.resourceConfigClass"; |
| 191 |
|
| 192 |
/** |
| 193 |
* The base path in the Web Pages where JSP templates, associated with |
| 194 |
* viewables of resource classes, are located. |
| 195 |
* <p> |
| 196 |
* If this property is not set then the base path will be the root path |
| 197 |
* of the Web Pages. |
| 198 |
*/ |
| 199 |
public static final String JSP_TEMPLATES_BASE_PATH = |
| 200 |
"com.sun.jersey.config.property.JSPTemplatesBasePath"; |
| 201 |
|
| 202 |
/** |
| 203 |
* If set the regular expression used to match an incoming servlet path URI |
| 204 |
* to some web page content such as static resources or JSPs to be handled |
| 205 |
* by the underlying servlet engine. |
| 206 |
* <p> |
| 207 |
* The type of this property must be a String and the value must be a valid |
| 208 |
* regular expression. |
| 209 |
* <p> |
| 210 |
* This property is only applicable when this class is used as a |
| 211 |
* {@link Filter}, otherwise this property will be ignored and not |
| 212 |
* processed. |
| 213 |
* <p> |
| 214 |
* If a servlet path matches this regular expression then the filter |
| 215 |
* forwards the request to the next filter in the filter chain so that the |
| 216 |
* underlying servlet engine can process the request otherwise Jersey |
| 217 |
* will process the request. |
| 218 |
* <p> |
| 219 |
* For example if you set the value to |
| 220 |
* <code>/(image|css)/.*</code> |
| 221 |
* then you can serve up images and CSS files for your Implicit or Explicit |
| 222 |
* Views while still processing your JAX-RS resources. |
| 223 |
*/ |
| 224 |
public static final String PROPERTY_WEB_PAGE_CONTENT_REGEX |
| 225 |
= "com.sun.jersey.config.property.WebPageContentRegex"; |
| 226 |
|
| 227 |
/** |
| 228 |
* If true and a 404 response with no entity body is returned from either |
| 229 |
* the runtime or the application then the runtime forwards the request to |
| 230 |
* the next filter in the filter chain. This enables another filter or |
| 231 |
* the underlying servlet engine to process the request. |
| 232 |
* Before the request is forwarded the response status is set to 200. |
| 233 |
* <p> |
| 234 |
* This property is only applicable when this class is used as a |
| 235 |
* {@link Filter}, otherwise this property will be ignored and not |
| 236 |
* processed. |
| 237 |
* <p> |
| 238 |
* Application code, such as methods corresponding to sub-resource locators |
| 239 |
* may be invoked when this feature is enabled. |
| 240 |
* <p> |
| 241 |
* This feature is an alternative to setting |
| 242 |
* {@link #PROPERTY_WEB_PAGE_CONTENT_REGEX} and requires less configuration. |
| 243 |
* However, application code, such as methods corresponding to sub-resource |
| 244 |
* locators, may be invoked when this feature is enabled. |
| 245 |
* <p> |
| 246 |
* The default value is false. |
| 247 |
*/ |
| 248 |
public static final String FEATURE_FILTER_FORWARD_ON_404 |
| 249 |
= "com.sun.jersey.config.feature.FilterForwardOn404"; |
| 250 |
|
| 251 |
/** |
| 252 |
* The filter context path. |
| 253 |
* <p> |
| 254 |
* If the URL pattern of a filter is set to a base path and a wildcard, |
| 255 |
* such as "/base/*", then this property can be used to declare a filter |
| 256 |
* context path that behaves in the same manner as the Servlet context |
| 257 |
* path for determining the base URI of the application. (Note that with |
| 258 |
* the Servlet 2.x API it is not possible to determine the URL pattern |
| 259 |
* without parsing the web.xml, hence why this property is necessary.) |
| 260 |
* <p> |
| 261 |
* This property is only applicable when this class is used as a |
| 262 |
* {@link Filter}, otherwise this property will be ignored and not |
| 263 |
* processed. |
| 264 |
* <p> |
| 265 |
* This property may consist of one or more path segments separate by '/'. |
| 266 |
*/ |
| 267 |
public static final String PROPERTY_FILTER_CONTEXT_PATH |
| 268 |
= "com.sun.jersey.config.feature.FilterContextPath"; |
| 269 |
|
| 270 |
/** |
| 271 |
* If set to true then the internal Jersey CDI component provider |
| 272 |
* will be used for managed bean types, |
| 273 |
* that are not annotated with {@link ManagedBean} annotation (neither with any other |
| 274 |
* bean defining annotation) and CDI bean manager |
| 275 |
* evaluates them as dependent scoped beans. |
| 276 |
* <p/> |
| 277 |
* This feature is only applicable when CDI processing is enabled, |
| 278 |
* i.e. when <code>beans.xml</code> file is present. |
| 279 |
* <p/> |
| 280 |
* This feature is an alternative to adding {@link ManagedBean} annotation |
| 281 |
* to managed bean types directly. |
| 282 |
* <p/> |
| 283 |
* The default value is false. |
| 284 |
*/ |
| 285 |
public static final String FEATURE_ALLOW_RAW_MANAGED_BEANS |
| 286 |
= "com.sun.jersey.config.feature.AllowRawManagedBeans"; |
| 287 |
|
| 288 |
/** |
| 289 |
* A helper class for creating an injectable provider that supports |
| 290 |
* {@link Context} with a type and constant value. |
| 291 |
* |
| 292 |
* @param <T> the type of the constant value. |
| 293 |
*/ |
| 294 |
protected static class ContextInjectableProvider<T> extends |
| 295 |
SingletonTypeInjectableProvider<Context, T> { |
| 296 |
|
| 297 |
/** |
| 298 |
* Create a new instance. |
| 299 |
* |
| 300 |
* @param type the type of the constant value. |
| 301 |
* @param instance the constant value. |
| 302 |
*/ |
| 303 |
protected ContextInjectableProvider(Type type, T instance) { |
| 304 |
super(type, instance); |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
private transient WebComponent webComponent; |
| 309 |
|
| 310 |
private transient FilterConfig filterConfig; |
| 311 |
|
| 312 |
private transient Pattern staticContentPattern; |
| 313 |
|
| 314 |
private transient boolean forwardOn404; |
| 315 |
|
| 316 |
private class InternalWebComponent extends WebComponent { |
| 317 |
|
| 318 |
InternalWebComponent() { |
| 319 |
} |
| 320 |
|
| 321 |
InternalWebComponent(Application app) { |
| 322 |
super(app); |
| 323 |
} |
| 324 |
|
| 325 |
@Override |
| 326 |
protected WebApplication create() { |
| 327 |
return ServletContainer.this.create(); |
| 328 |
} |
| 329 |
|
| 330 |
@Override |
| 331 |
protected void configure(WebConfig wc, ResourceConfig rc, WebApplication wa) { |
| 332 |
super.configure(wc, rc, wa); |
| 333 |
|
| 334 |
ServletContainer.this.configure(wc, rc, wa); |
| 335 |
} |
| 336 |
|
| 337 |
@Override |
| 338 |
protected void initiate(ResourceConfig rc, WebApplication wa) { |
| 339 |
ServletContainer.this.initiate(rc, wa); |
| 340 |
} |
| 341 |
|
| 342 |
@Override |
| 343 |
protected ResourceConfig getDefaultResourceConfig(Map<String, Object> props, |
| 344 |
WebConfig wc) throws ServletException { |
| 345 |
return ServletContainer.this.getDefaultResourceConfig(props, wc); |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
private transient final Application app; |
| 350 |
|
| 351 |
public ServletContainer() { |
| 352 |
this.app = null; |
| 353 |
} |
| 354 |
|
| 355 |
public ServletContainer(Class<? extends Application> appClass) { |
| 356 |
this.app = new DeferredResourceConfig(appClass); |
| 357 |
} |
| 358 |
|
| 359 |
public ServletContainer(Application app) { |
| 360 |
this.app = app; |
| 361 |
} |
| 362 |
|
| 363 |
// GenericServlet |
| 364 |
|
| 365 |
/** |
| 366 |
* Get the servlet context for the servlet or filter, depending on |
| 367 |
* how this class is registered. |
| 368 |
* <p> |
| 369 |
* It is recommended that the {@link WebConfig} be utilized, |
| 370 |
* see the method {@link #getWebConfig() }, to obtain the servlet context |
| 371 |
* and initialization parameters for a servlet or filter. |
| 372 |
* |
| 373 |
* @return the servlet context for the servlet or filter. |
| 374 |
*/ |
| 375 |
@Override |
| 376 |
public ServletContext getServletContext() { |
| 377 |
if (filterConfig != null) |
| 378 |
return filterConfig.getServletContext(); |
| 379 |
|
| 380 |
return super.getServletContext(); |
| 381 |
} |
| 382 |
|
| 383 |
/** |
| 384 |
* Initiate the Web component. |
| 385 |
* |
| 386 |
* @param webConfig the Web configuration. |
| 387 |
* |
| 388 |
* @throws javax.servlet.ServletException in case of an initialization failure |
| 389 |
*/ |
| 390 |
protected void init(WebConfig webConfig) throws ServletException { |
| 391 |
webComponent = (app == null) |
| 392 |
? new InternalWebComponent() |
| 393 |
: new InternalWebComponent(app); |
| 394 |
webComponent.init(webConfig); |
| 395 |
} |
| 396 |
|
| 397 |
/** |
| 398 |
* Get the Web configuration. |
| 399 |
* |
| 400 |
* @return the Web configuration. |
| 401 |
*/ |
| 402 |
protected WebConfig getWebConfig() { |
| 403 |
return webComponent.getWebConfig(); |
| 404 |
} |
| 405 |
|
| 406 |
/** |
| 407 |
* Create a new instance of a {@link WebApplication}. |
| 408 |
* |
| 409 |
* @return the {@link WebApplication} instance. |
| 410 |
*/ |
| 411 |
protected WebApplication create() { |
| 412 |
return WebApplicationFactory.createWebApplication(); |
| 413 |
} |
| 414 |
|
| 415 |
/** |
| 416 |
* Get the default resource configuration if one is not declared in the |
| 417 |
* web.xml. |
| 418 |
* <p> |
| 419 |
* This implementation returns an instance of {@link WebAppResourceConfig} |
| 420 |
* that scans in files and directories as declared by the |
| 421 |
* {@link ClasspathResourceConfig#PROPERTY_CLASSPATH} if present, otherwise |
| 422 |
* in the "WEB-INF/lib" and "WEB-INF/classes" directories. |
| 423 |
* <p> |
| 424 |
* An inheriting class may override this method to supply a different |
| 425 |
* default resource configuration implementation. |
| 426 |
* |
| 427 |
* @param props the properties to pass to the resource configuration. |
| 428 |
* @param wc the web configuration. |
| 429 |
* @return the default resource configuration. |
| 430 |
* |
| 431 |
* @throws javax.servlet.ServletException in case there was an error while retrieving the default resource config |
| 432 |
*/ |
| 433 |
protected ResourceConfig getDefaultResourceConfig(Map<String, Object> props, |
| 434 |
WebConfig wc) throws ServletException { |
| 435 |
return webComponent.getWebAppResourceConfig(props, wc); |
| 436 |
} |
| 437 |
|
| 438 |
/** |
| 439 |
* Configure the {@link ResourceConfig}. |
| 440 |
* <p> |
| 441 |
* The {@link ResourceConfig} is configured such that the following classes |
| 442 |
* may be injected onto the field of a root resource class or a parameter |
| 443 |
* of a method of root resource class that is annotated with |
| 444 |
* {@link javax.ws.rs.core.Context}: {@link HttpServletRequest}, {@link HttpServletResponse} |
| 445 |
* , {@link ServletContext} and {@link WebConfig}. |
| 446 |
* <p> |
| 447 |
* Any root resource class in registered in the resource configuration |
| 448 |
* that is an interface is processed as follows. |
| 449 |
* If the class is an interface and there exists a JNDI named object |
| 450 |
* with the fully qualified class name as the JNDI name then that named |
| 451 |
* object is added as a singleton root resource and the class is removed |
| 452 |
* from the set of root resource classes. |
| 453 |
* <p> |
| 454 |
* An inheriting class may override this method to configure the |
| 455 |
* {@link ResourceConfig} to provide alternative or additional instances |
| 456 |
* that are resource or provider classes or instances, and may modify the |
| 457 |
* features and properties of the {@link ResourceConfig}. For an inheriting |
| 458 |
* class to extend configuration behaviour the overriding method MUST call |
| 459 |
* <code>super.configure(servletConfig, rc, wa)</code> as the first statement |
| 460 |
* of that method. |
| 461 |
* <p> |
| 462 |
* This method will be called only once at initiation. Subsequent |
| 463 |
* reloads of the Web application will not result in subsequence calls to |
| 464 |
* this method. |
| 465 |
* |
| 466 |
* @param wc the Web configuration |
| 467 |
* @param rc the Resource configuration |
| 468 |
* @param wa the Web application |
| 469 |
*/ |
| 470 |
protected void configure(WebConfig wc, ResourceConfig rc, WebApplication wa) { |
| 471 |
if (getServletConfig() != null) |
| 472 |
configure(getServletConfig(), rc, wa); |
| 473 |
else if (filterConfig != null) |
| 474 |
configure(filterConfig, rc, wa); |
| 475 |
|
| 476 |
if (rc instanceof ReloadListener) { |
| 477 |
List<ContainerNotifier> notifiers = new ArrayList<ContainerNotifier>(); |
| 478 |
|
| 479 |
Object o = rc.getProperties().get(ResourceConfig.PROPERTY_CONTAINER_NOTIFIER); |
| 480 |
|
| 481 |
if (o instanceof ContainerNotifier) |
| 482 |
notifiers.add((ContainerNotifier) o); |
| 483 |
else if (o instanceof List) |
| 484 |
for (Object elem : (List) o) |
| 485 |
if (elem instanceof ContainerNotifier) |
| 486 |
notifiers.add((ContainerNotifier) elem); |
| 487 |
|
| 488 |
for (ContainerNotifier cn : ServiceFinder.find(ContainerNotifier.class)) { |
| 489 |
notifiers.add(cn); |
| 490 |
} |
| 491 |
|
| 492 |
rc.getProperties().put(ResourceConfig.PROPERTY_CONTAINER_NOTIFIER, notifiers); |
| 493 |
} |
| 494 |
} |
| 495 |
|
| 496 |
/** |
| 497 |
* Initiate the {@link WebApplication}. |
| 498 |
* <p> |
| 499 |
* This method will be called once at initiation and for |
| 500 |
* each reload of the Web application. |
| 501 |
* <p> |
| 502 |
* An inheriting class may override this method to initiate the |
| 503 |
* Web application with different parameters. |
| 504 |
* |
| 505 |
* @param rc the Resource configuration |
| 506 |
* @param wa the Web application |
| 507 |
*/ |
| 508 |
protected void initiate(ResourceConfig rc, WebApplication wa) { |
| 509 |
wa.initiate(rc); |
| 510 |
} |
| 511 |
|
| 512 |
/** |
| 513 |
* Load the Web application. This will create, configure and initiate |
| 514 |
* the web application. |
| 515 |
*/ |
| 516 |
public void load() { |
| 517 |
webComponent.load(); |
| 518 |
} |
| 519 |
|
| 520 |
/** |
| 521 |
* Reload the Web application. This will create and initiate the web |
| 522 |
* application using the same {@link ResourceConfig} implementation |
| 523 |
* that was used to load the Web application. |
| 524 |
* <p> |
| 525 |
* This method may be called at runtime, more than once, to reload the |
| 526 |
* Web application. For example, if a {@link ResourceConfig} implementation |
| 527 |
* is capable of detecting changes to resource classes (addition or removal) |
| 528 |
* or providers then this method may be invoked to reload the web |
| 529 |
* application for such changes to take effect. |
| 530 |
* <p> |
| 531 |
* If this method is called when there are pending requests then such |
| 532 |
* requests will be processed using the previously loaded web application. |
| 533 |
*/ |
| 534 |
public void reload() { |
| 535 |
webComponent.onReload(); |
| 536 |
} |
| 537 |
|
| 538 |
/** |
| 539 |
* Dispatch client requests to a resource class. |
| 540 |
* |
| 541 |
* @param baseUri the base URI of the request. |
| 542 |
* @param requestUri the URI of the request. |
| 543 |
* @param request the {@link HttpServletRequest} object that |
| 544 |
* contains the request the client made to |
| 545 |
* the Web component. |
| 546 |
* @param response the {@link HttpServletResponse} object that |
| 547 |
* contains the response the Web component returns |
| 548 |
* to the client. |
| 549 |
* @return the status code of the response. |
| 550 |
* @exception IOException if an input or output error occurs |
| 551 |
* while the Web component is handling the |
| 552 |
* HTTP request. |
| 553 |
* @exception ServletException if the HTTP request cannot |
| 554 |
* be handled. |
| 555 |
*/ |
| 556 |
public int service(URI baseUri, URI requestUri, final HttpServletRequest request, HttpServletResponse response) |
| 557 |
throws ServletException, IOException { |
| 558 |
return webComponent.service(baseUri, requestUri, request, response); |
| 559 |
} |
| 560 |
|
| 561 |
/** |
| 562 |
* Destroy this Servlet or Filter. |
| 563 |
* |
| 564 |
*/ |
| 565 |
@Override |
| 566 |
public void destroy() { |
| 567 |
if (webComponent != null) { |
| 568 |
webComponent.destroy(); |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
|
| 573 |
// Servlet |
| 574 |
|
| 575 |
@Override |
| 576 |
public void init() throws ServletException { |
| 577 |
init(new WebServletConfig(this)); |
| 578 |
} |
| 579 |
|
| 580 |
/** |
| 581 |
* Get the default resource configuration if one is not declared in the |
| 582 |
* web.xml. |
| 583 |
* <p> |
| 584 |
* This implementation returns an instance of {@link WebAppResourceConfig} |
| 585 |
* that scans in files and directories as declared by the |
| 586 |
* {@link ClasspathResourceConfig#PROPERTY_CLASSPATH} if present, otherwise |
| 587 |
* in the "WEB-INF/lib" and "WEB-INF/classes" directories. |
| 588 |
* <p> |
| 589 |
* An inheriting class may override this method to supply a different |
| 590 |
* default resource configuration implementation. |
| 591 |
* |
| 592 |
* @param props the properties to pass to the resource configuration. |
| 593 |
* @param servletConfig the servlet configuration. |
| 594 |
* @return the default resource configuration. |
| 595 |
* |
| 596 |
* @throws javax.servlet.ServletException in case there was an error while retrieving the default resource config |
| 597 |
* @deprecated methods should implement {@link #getDefaultResourceConfig(java.util.Map, com.sun.jersey.spi.container.servlet.WebConfig) }. |
| 598 |
*/ |
| 599 |
@Deprecated |
| 600 |
protected ResourceConfig getDefaultResourceConfig(Map<String, Object> props, |
| 601 |
ServletConfig servletConfig) throws ServletException { |
| 602 |
return getDefaultResourceConfig(props, getWebConfig()); |
| 603 |
} |
| 604 |
|
| 605 |
/** |
| 606 |
* Configure the {@link ResourceConfig} for a Servlet. |
| 607 |
* <p> |
| 608 |
* The {@link ResourceConfig} is configured such that the following classes |
| 609 |
* may be injected onto the field of a root resource class or a parameter |
| 610 |
* of a method of root resource class that is annotated with |
| 611 |
* {@link javax.ws.rs.core.Context}: {@link ServletConfig}. |
| 612 |
* <p> |
| 613 |
* An inheriting class may override this method to configure the |
| 614 |
* {@link ResourceConfig} to provide alternative or additional instances |
| 615 |
* that are resource or provider classes or instances, and may modify the |
| 616 |
* features and properties of the {@link ResourceConfig}. For an inheriting |
| 617 |
* class to extend configuration behaviour the overriding method MUST call |
| 618 |
* <code>super.configure(servletConfig, rc, wa)</code> as the first statement of that |
| 619 |
* method. |
| 620 |
* <p> |
| 621 |
* This method will be called only once at servlet initiation. Subsequent |
| 622 |
* reloads of the Web application will not result in subsequence calls to |
| 623 |
* this method. |
| 624 |
* |
| 625 |
* @param sc the Servlet configuration |
| 626 |
* @param rc the Resource configuration |
| 627 |
* @param wa the Web application |
| 628 |
*/ |
| 629 |
protected void configure(final ServletConfig sc, ResourceConfig rc, WebApplication wa) { |
| 630 |
rc.getSingletons().add(new ContextInjectableProvider<ServletConfig>( |
| 631 |
ServletConfig.class, sc)); |
| 632 |
} |
| 633 |
|
| 634 |
/** |
| 635 |
* Dispatches client requests to the {@link #service(java.net.URI, java.net.URI, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) } |
| 636 |
* method. |
| 637 |
* |
| 638 |
* @param request the {@link HttpServletRequest} object that |
| 639 |
* contains the request the client made to |
| 640 |
* the servlet. |
| 641 |
* @param response the {@link HttpServletResponse} object that |
| 642 |
* contains the response the servlet returns |
| 643 |
* to the client. |
| 644 |
* @exception IOException if an input or output error occurs |
| 645 |
* while the servlet is handling the |
| 646 |
* HTTP request. |
| 647 |
* @exception ServletException if the HTTP request cannot |
| 648 |
* be handled. |
| 649 |
*/ |
| 650 |
@Override |
| 651 |
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
| 652 |
/** |
| 653 |
* There is an annoying edge case where the service method is |
| 654 |
* invoked for the case when the URI is equal to the deployment URL |
| 655 |
* minus the '/', for example http://locahost:8080/HelloWorldWebApp |
| 656 |
*/ |
| 657 |
final String servletPath = request.getServletPath(); |
| 658 |
String pathInfo = request.getPathInfo(); |
| 659 |
StringBuffer requestURL = request.getRequestURL(); |
| 660 |
String requestURI = request.getRequestURI(); |
| 661 |
final boolean checkPathInfo = pathInfo == null || pathInfo.isEmpty() || pathInfo.equals("/"); |
| 662 |
|
| 663 |
/** |
| 664 |
* The HttpServletRequest.getRequestURL() contains the complete URI |
| 665 |
* minus the query and fragment components. |
| 666 |
*/ |
| 667 |
UriBuilder absoluteUriBuilder; |
| 668 |
try { |
| 669 |
absoluteUriBuilder = UriBuilder.fromUri(requestURL.toString()); |
| 670 |
} catch (IllegalArgumentException iae) { |
| 671 |
final Response.Status badRequest = Response.Status.BAD_REQUEST; |
| 672 |
response.sendError(badRequest.getStatusCode(), badRequest.getReasonPhrase()); |
| 673 |
return; |
| 674 |
} |
| 675 |
|
| 676 |
if (checkPathInfo && !request.getRequestURI().endsWith("/")) { |
| 677 |
// Only do this if the last segment of the servlet path does not contain '.' |
| 678 |
// This handles the case when the extension mapping is used with the servlet |
| 679 |
// see issue 506 |
| 680 |
// This solution does not require parsing the deployment descriptor, |
| 681 |
// however still leaves it broken for the very rare case if a standard path |
| 682 |
// servlet mapping would include dot in the last segment (e.g. /.webresources/*) |
| 683 |
// and somebody would want to hit the root resource without the trailing slash |
| 684 |
int i = servletPath.lastIndexOf("/"); |
| 685 |
if (servletPath.substring(i + 1).indexOf('.') < 0) { |
| 686 |
if (webComponent.getResourceConfig().getFeature(ResourceConfig.FEATURE_REDIRECT)) { |
| 687 |
URI l = absoluteUriBuilder. |
| 688 |
path("/"). |
| 689 |
replaceQuery(request.getQueryString()).build(); |
| 690 |
|
| 691 |
response.setStatus(307); |
| 692 |
response.setHeader("Location", l.toASCIIString()); |
| 693 |
return; |
| 694 |
} else { |
| 695 |
requestURL.append("/"); |
| 696 |
requestURI += "/"; |
| 697 |
} |
| 698 |
} |
| 699 |
} |
| 700 |
|
| 701 |
/** |
| 702 |
* The HttpServletRequest.getPathInfo() and |
| 703 |
* HttpServletRequest.getServletPath() are in decoded form. |
| 704 |
* |
| 705 |
* On some servlet implementations the getPathInfo() removed |
| 706 |
* contiguous '/' characters. This is problematic if URIs |
| 707 |
* are embedded, for example as the last path segment. |
| 708 |
* We need to work around this and not use getPathInfo |
| 709 |
* for the decodedPath. |
| 710 |
*/ |
| 711 |
final String decodedBasePath = request.getContextPath() + servletPath + "/"; |
| 712 |
|
| 713 |
final String encodedBasePath = UriComponent.encode(decodedBasePath, |
| 714 |
UriComponent.Type.PATH); |
| 715 |
|
| 716 |
if (!decodedBasePath.equals(encodedBasePath)) { |
| 717 |
throw new ContainerException("The servlet context path and/or the " + |
| 718 |
"servlet path contain characters that are percent encoded"); |
| 719 |
} |
| 720 |
|
| 721 |
final URI baseUri = absoluteUriBuilder.replacePath(encodedBasePath). |
| 722 |
build(); |
| 723 |
|
| 724 |
String queryParameters = request.getQueryString(); |
| 725 |
if (queryParameters == null) { |
| 726 |
queryParameters = ""; |
| 727 |
} |
| 728 |
|
| 729 |
final URI requestUri = absoluteUriBuilder.replacePath(requestURI). |
| 730 |
replaceQuery(queryParameters). |
| 731 |
build(); |
| 732 |
|
| 733 |
service(baseUri, requestUri, request, response); |
| 734 |
} |
| 735 |
|
| 736 |
|
| 737 |
// Filter |
| 738 |
|
| 739 |
private String filterContextPath = null; |
| 740 |
|
| 741 |
public void init(FilterConfig filterConfig) throws ServletException { |
| 742 |
this.filterConfig = filterConfig; |
| 743 |
|
| 744 |
init(new WebFilterConfig(filterConfig)); |
| 745 |
} |
| 746 |
|
| 747 |
/** |
| 748 |
* @return the {@link Pattern} compiled from a regular expression that is |
| 749 |
* the property value of {@link #PROPERTY_WEB_PAGE_CONTENT_REGEX}. |
| 750 |
* A <code>null</code> value will be returned if the property is not present |
| 751 |
* is or an empty String. |
| 752 |
*/ |
| 753 |
public Pattern getStaticContentPattern() { |
| 754 |
return staticContentPattern; |
| 755 |
} |
| 756 |
|
| 757 |
/** |
| 758 |
* Configure the {@link ResourceConfig} for a Filter. |
| 759 |
* <p> |
| 760 |
* The {@link ResourceConfig} is configured such that the following classes |
| 761 |
* may be injected onto the field of a root resource class or a parameter |
| 762 |
* of a method of root resource class that is annotated with |
| 763 |
* {@link javax.ws.rs.core.Context}: {@link FilterConfig}. |
| 764 |
* <p> |
| 765 |
* An inheriting class may override this method to configure the |
| 766 |
* {@link ResourceConfig} to provide alternative or additional instances |
| 767 |
* that are resource or provider classes or instances, and may modify the |
| 768 |
* features and properties of the {@link ResourceConfig}. For an inheriting |
| 769 |
* class to extend configuration behaviour the overriding method MUST call |
| 770 |
* <code>super.configure(servletConfig, rc, wa)</code> as the first statement of that |
| 771 |
* method. |
| 772 |
* <p> |
| 773 |
* This method will be called only once at servlet initiation. Subsequent |
| 774 |
* reloads of the Web application will not result in subsequence calls to |
| 775 |
* this method. |
| 776 |
* |
| 777 |
* @param fc the Filter configuration |
| 778 |
* @param rc the Resource configuration |
| 779 |
* @param wa the Web application |
| 780 |
*/ |
| 781 |
protected void configure(final FilterConfig fc, ResourceConfig rc, WebApplication wa) { |
| 782 |
rc.getSingletons().add(new ContextInjectableProvider<FilterConfig>( |
| 783 |
FilterConfig.class, fc)); |
| 784 |
|
| 785 |
String regex = (String)rc.getProperty(PROPERTY_WEB_PAGE_CONTENT_REGEX); |
| 786 |
if (regex != null && regex.length() > 0) { |
| 787 |
try { |
| 788 |
staticContentPattern = Pattern.compile(regex); |
| 789 |
} catch (PatternSyntaxException ex) { |
| 790 |
throw new ContainerException( |
| 791 |
"The syntax is invalid for the regular expression, " + regex + |
| 792 |
", associated with the initialization parameter " + PROPERTY_WEB_PAGE_CONTENT_REGEX, ex); |
| 793 |
} |
| 794 |
} |
| 795 |
|
| 796 |
forwardOn404 = rc.getFeature(FEATURE_FILTER_FORWARD_ON_404); |
| 797 |
|
| 798 |
this.filterContextPath = filterConfig.getInitParameter(PROPERTY_FILTER_CONTEXT_PATH); |
| 799 |
if (filterContextPath != null) { |
| 800 |
if (filterContextPath.isEmpty()) { |
| 801 |
filterContextPath = null; |
| 802 |
} else { |
| 803 |
if (!filterContextPath.startsWith("/")) { |
| 804 |
filterContextPath = '/' + filterContextPath; |
| 805 |
} |
| 806 |
if (filterContextPath.endsWith("/")) { |
| 807 |
filterContextPath = filterContextPath.substring(0, filterContextPath.length() - 1); |
| 808 |
} |
| 809 |
} |
| 810 |
} |
| 811 |
} |
| 812 |
|
| 813 |
/** |
| 814 |
* Dispatches client requests to the {@link #doFilter(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, javax.servlet.FilterChain) } |
| 815 |
* method. |
| 816 |
* |
| 817 |
* @param request the {@link HttpServletRequest} object that |
| 818 |
* contains the request the client made to |
| 819 |
* the servlet. |
| 820 |
* @param response the {@link HttpServletResponse} object that |
| 821 |
* contains the response the servlet returns |
| 822 |
* to the client. |
| 823 |
* @param chain the chain of filters from which the next filter can be invoked. |
| 824 |
* @throws java.io.IOException |
| 825 |
* @throws javax.servlet.ServletException |
| 826 |
*/ |
| 827 |
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { |
| 828 |
try { |
| 829 |
doFilter((HttpServletRequest)request, (HttpServletResponse)response, chain); |
| 830 |
} catch (ClassCastException e) { |
| 831 |
throw new ServletException("non-HTTP request or response"); |
| 832 |
} |
| 833 |
} |
| 834 |
|
| 835 |
/** |
| 836 |
* Dispatches client requests to the {@link #service(java.net.URI, java.net.URI, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) } |
| 837 |
* method. |
| 838 |
* <p> |
| 839 |
* If the servlet path matches the regular expression declared by the |
| 840 |
* property {@link #PROPERTY_WEB_PAGE_CONTENT_REGEX} then the request |
| 841 |
* is forwarded to the next filter in the filter chain so that the |
| 842 |
* underlying servlet engine can process the request otherwise Jersey |
| 843 |
* will process the request. |
| 844 |
* |
| 845 |
* @param request the {@link HttpServletRequest} object that |
| 846 |
* contains the request the client made to |
| 847 |
* the servlet. |
| 848 |
* @param response the {@link HttpServletResponse} object that |
| 849 |
* contains the response the servlet returns |
| 850 |
* to the client. |
| 851 |
* @param chain the chain of filters from which the next filter can be invoked. |
| 852 |
* @throws java.io.IOException |
| 853 |
* @throws javax.servlet.ServletException |
| 854 |
*/ |
| 855 |
public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException { |
| 856 |
if (request.getAttribute("javax.servlet.include.request_uri") != null) { |
| 857 |
final String includeRequestURI = (String)request.getAttribute("javax.servlet.include.request_uri"); |
| 858 |
|
| 859 |
if (!includeRequestURI.equals(request.getRequestURI())) { |
| 860 |
doFilter(request, response, chain, |
| 861 |
includeRequestURI, |
| 862 |
(String)request.getAttribute("javax.servlet.include.servlet_path"), |
| 863 |
(String)request.getAttribute("javax.servlet.include.query_string")); |
| 864 |
return; |
| 865 |
} |
| 866 |
} |
| 867 |
|
| 868 |
/** |
| 869 |
* JERSEY-880 - WAS interprets HttpServletRequest#getServletPath() and HttpServletRequest#getPathInfo() |
| 870 |
* differently when accessing a static resource. |
| 871 |
*/ |
| 872 |
final String servletPath = request.getServletPath() |
| 873 |
+ (request.getPathInfo() == null ? "" : request.getPathInfo()); |
| 874 |
|
| 875 |
doFilter(request, response, chain, |
| 876 |
request.getRequestURI(), |
| 877 |
servletPath, |
| 878 |
request.getQueryString()); |
| 879 |
} |
| 880 |
|
| 881 |
private void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain, |
| 882 |
String requestURI, String servletPath, String queryString) throws IOException, ServletException { |
| 883 |
// if we match the static content regular expression lets delegate to |
| 884 |
// the filter chain to use the default container servlets & handlers |
| 885 |
final Pattern p = getStaticContentPattern(); |
| 886 |
if (p != null && p.matcher(servletPath).matches()) { |
| 887 |
chain.doFilter(request, response); |
| 888 |
return; |
| 889 |
} |
| 890 |
|
| 891 |
if (filterContextPath != null) { |
| 892 |
if (!servletPath.startsWith(filterContextPath)) { |
| 893 |
throw new ContainerException("The servlet path, \"" + servletPath + |
| 894 |
"\", does not start with the filter context path, \"" + filterContextPath + "\""); |
| 895 |
} else if (servletPath.length() == filterContextPath.length()) { |
| 896 |
// Path does not end in a slash, may need to redirect |
| 897 |
if (webComponent.getResourceConfig().getFeature(ResourceConfig.FEATURE_REDIRECT)) { |
| 898 |
URI l = UriBuilder.fromUri(request.getRequestURL().toString()). |
| 899 |
path("/"). |
| 900 |
replaceQuery(queryString).build(); |
| 901 |
|
| 902 |
response.setStatus(307); |
| 903 |
response.setHeader("Location", l.toASCIIString()); |
| 904 |
return; |
| 905 |
} else { |
| 906 |
requestURI += "/"; |
| 907 |
} |
| 908 |
} |
| 909 |
} |
| 910 |
|
| 911 |
final UriBuilder absoluteUriBuilder = UriBuilder.fromUri( |
| 912 |
request.getRequestURL().toString()); |
| 913 |
|
| 914 |
final URI baseUri = (filterContextPath == null) |
| 915 |
? absoluteUriBuilder.replacePath(request.getContextPath()). |
| 916 |
path("/"). |
| 917 |
build() |
| 918 |
: absoluteUriBuilder.replacePath(request.getContextPath()). |
| 919 |
path(filterContextPath). |
| 920 |
path("/"). |
| 921 |
build(); |
| 922 |
|
| 923 |
final URI requestUri = absoluteUriBuilder.replacePath(requestURI). |
| 924 |
replaceQuery(queryString). |
| 925 |
build(); |
| 926 |
|
| 927 |
final int status = service(baseUri, requestUri, request, response); |
| 928 |
|
| 929 |
// If forwarding is configured and response is a 404 with no entity |
| 930 |
// body then call the next filter in the chain |
| 931 |
if (forwardOn404 && status == 404 && !response.isCommitted()) { |
| 932 |
// lets clear the response to OK before we forward to the next in the chain |
| 933 |
// as OK is the default set by servlet containers before filters/servlets do any wor |
| 934 |
// so lets hide our footsteps and pretend we were never in the chain at all and let the |
| 935 |
// next filter or servlet return the 404 if they can't find anything to return |
| 936 |
// |
| 937 |
// We could add an optional flag to disable this step if anyone can ever find a case where |
| 938 |
// this causes a problem, though I suspect any problems will really be with downstream |
| 939 |
// servlets not correctly setting an error status if they cannot find something to return |
| 940 |
response.setStatus(HttpServletResponse.SC_OK); |
| 941 |
chain.doFilter(request, response); |
| 942 |
} |
| 943 |
} |
| 944 |
} |