| 1 |
/* |
| 2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
| 3 |
* |
| 4 |
* Copyright (c) 2010-2013 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.BufferedInputStream; |
| 44 |
import java.io.IOException; |
| 45 |
import java.io.InputStream; |
| 46 |
import java.io.OutputStream; |
| 47 |
import java.lang.reflect.Constructor; |
| 48 |
import java.lang.reflect.Proxy; |
| 49 |
import java.lang.reflect.Type; |
| 50 |
import java.net.URI; |
| 51 |
import java.security.Principal; |
| 52 |
import java.util.ArrayList; |
| 53 |
import java.util.Arrays; |
| 54 |
import java.util.Enumeration; |
| 55 |
import java.util.HashMap; |
| 56 |
import java.util.Iterator; |
| 57 |
import java.util.LinkedList; |
| 58 |
import java.util.List; |
| 59 |
import java.util.Map; |
| 60 |
import java.util.logging.Level; |
| 61 |
import java.util.logging.Logger; |
| 62 |
|
| 63 |
import javax.ws.rs.WebApplicationException; |
| 64 |
import javax.ws.rs.core.Application; |
| 65 |
import javax.ws.rs.core.Context; |
| 66 |
import javax.ws.rs.core.GenericEntity; |
| 67 |
import javax.ws.rs.core.MediaType; |
| 68 |
import javax.ws.rs.core.MultivaluedMap; |
| 69 |
import javax.ws.rs.core.Response; |
| 70 |
import javax.ws.rs.core.SecurityContext; |
| 71 |
|
| 72 |
import javax.naming.NamingException; |
| 73 |
import javax.servlet.ServletContext; |
| 74 |
import javax.servlet.ServletException; |
| 75 |
import javax.servlet.http.HttpServletRequest; |
| 76 |
import javax.servlet.http.HttpServletResponse; |
| 77 |
|
| 78 |
import com.sun.jersey.api.container.ContainerException; |
| 79 |
import com.sun.jersey.api.container.MappableContainerException; |
| 80 |
import com.sun.jersey.api.core.ApplicationAdapter; |
| 81 |
import com.sun.jersey.api.core.ClasspathResourceConfig; |
| 82 |
import com.sun.jersey.api.core.PackagesResourceConfig; |
| 83 |
import com.sun.jersey.api.core.ResourceConfig; |
| 84 |
import com.sun.jersey.api.core.TraceInformation; |
| 85 |
import com.sun.jersey.api.core.servlet.WebAppResourceConfig; |
| 86 |
import com.sun.jersey.api.representation.Form; |
| 87 |
import com.sun.jersey.core.header.InBoundHeaders; |
| 88 |
import com.sun.jersey.core.header.MediaTypes; |
| 89 |
import com.sun.jersey.core.reflection.ReflectionHelper; |
| 90 |
import com.sun.jersey.core.util.ReaderWriter; |
| 91 |
import com.sun.jersey.server.impl.InitialContextHelper; |
| 92 |
import com.sun.jersey.server.impl.ThreadLocalInvoker; |
| 93 |
import com.sun.jersey.server.impl.application.DeferredResourceConfig; |
| 94 |
import com.sun.jersey.server.impl.cdi.CDIComponentProviderFactoryInitializer; |
| 95 |
import com.sun.jersey.server.impl.container.servlet.JSPTemplateProcessor; |
| 96 |
import com.sun.jersey.server.impl.ejb.EJBComponentProviderFactoryInitilizer; |
| 97 |
import com.sun.jersey.server.impl.managedbeans.ManagedBeanComponentProviderFactoryInitilizer; |
| 98 |
import com.sun.jersey.server.impl.model.method.dispatch.FormDispatchProvider; |
| 99 |
import com.sun.jersey.server.impl.monitoring.GlassFishMonitoringInitializer; |
| 100 |
import com.sun.jersey.server.probes.UriRuleProbeProvider; |
| 101 |
import com.sun.jersey.spi.container.ContainerListener; |
| 102 |
import com.sun.jersey.spi.container.ContainerNotifier; |
| 103 |
import com.sun.jersey.spi.container.ContainerRequest; |
| 104 |
import com.sun.jersey.spi.container.ContainerResponse; |
| 105 |
import com.sun.jersey.spi.container.ContainerResponseWriter; |
| 106 |
import com.sun.jersey.spi.container.ReloadListener; |
| 107 |
import com.sun.jersey.spi.container.WebApplication; |
| 108 |
import com.sun.jersey.spi.container.WebApplicationFactory; |
| 109 |
import com.sun.jersey.spi.inject.SingletonTypeInjectableProvider; |
| 110 |
import java.security.AccessController; |
| 111 |
import java.security.PrivilegedActionException; |
| 112 |
|
| 113 |
/** |
| 114 |
* An abstract Web component that may be extended a Servlet and/or |
| 115 |
* Filter implementation, or encapsulated by a Servlet or Filter implementation. |
| 116 |
* |
| 117 |
* @author Paul Sandoz (paul.sandoz at oracle.com) |
| 118 |
*/ |
| 119 |
public class WebComponent implements ContainerListener { |
| 120 |
/** |
| 121 |
* The servlet initialization property whose value is a fully qualified |
| 122 |
* class name of a class that implements {@link ResourceConfig} or |
| 123 |
* {@link Application}. |
| 124 |
*/ |
| 125 |
public static final String APPLICATION_CONFIG_CLASS = |
| 126 |
"javax.ws.rs.Application"; |
| 127 |
|
| 128 |
/** |
| 129 |
* The servlet initialization property whose value is a fully qualified |
| 130 |
* class name of a class that implements {@link ResourceConfig} or |
| 131 |
* {@link Application}. |
| 132 |
*/ |
| 133 |
public static final String RESOURCE_CONFIG_CLASS = |
| 134 |
"com.sun.jersey.config.property.resourceConfigClass"; |
| 135 |
|
| 136 |
/** |
| 137 |
* The base path in the Web Pages where JSP templates, associated with |
| 138 |
* viewables of resource classes, are located. |
| 139 |
* <p/> |
| 140 |
* If this property is not set then the base path will be the root path |
| 141 |
* of the Web Pages. |
| 142 |
*/ |
| 143 |
public static final String JSP_TEMPLATES_BASE_PATH = |
| 144 |
"com.sun.jersey.config.property.JSPTemplatesBasePath"; |
| 145 |
|
| 146 |
|
| 147 |
private static final Logger LOGGER = |
| 148 |
Logger.getLogger(WebComponent.class.getName()); |
| 149 |
|
| 150 |
private final ThreadLocalInvoker<HttpServletRequest> requestInvoker = |
| 151 |
new ThreadLocalInvoker<HttpServletRequest>(); |
| 152 |
|
| 153 |
private final ThreadLocalInvoker<HttpServletResponse> responseInvoker = |
| 154 |
new ThreadLocalInvoker<HttpServletResponse>(); |
| 155 |
|
| 156 |
|
| 157 |
private WebConfig config; |
| 158 |
|
| 159 |
private ResourceConfig resourceConfig; |
| 160 |
|
| 161 |
private WebApplication application; |
| 162 |
|
| 163 |
public WebComponent() { |
| 164 |
} |
| 165 |
|
| 166 |
public WebComponent(Application app) { |
| 167 |
if (app == null) |
| 168 |
throw new IllegalArgumentException(); |
| 169 |
|
| 170 |
if (app instanceof ResourceConfig) { |
| 171 |
resourceConfig = (ResourceConfig) app; |
| 172 |
} else { |
| 173 |
resourceConfig = new ApplicationAdapter(app); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Get the Web configuration. |
| 179 |
* |
| 180 |
* @return the Web configuration. |
| 181 |
*/ |
| 182 |
public WebConfig getWebConfig() { |
| 183 |
return config; |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Get the resource configuration. |
| 188 |
* |
| 189 |
* @return the resource configuration. |
| 190 |
*/ |
| 191 |
public ResourceConfig getResourceConfig() { |
| 192 |
return resourceConfig; |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Initiate the Web component. |
| 197 |
* |
| 198 |
* @param webConfig the Web configuration. |
| 199 |
* @throws javax.servlet.ServletException in case of any initialization error |
| 200 |
*/ |
| 201 |
public void init(WebConfig webConfig) throws ServletException { |
| 202 |
config = webConfig; |
| 203 |
|
| 204 |
if (resourceConfig == null) |
| 205 |
resourceConfig = createResourceConfig(config); |
| 206 |
|
| 207 |
load(); |
| 208 |
|
| 209 |
Object o = resourceConfig.getProperties().get( |
| 210 |
ResourceConfig.PROPERTY_CONTAINER_NOTIFIER); |
| 211 |
if (o instanceof List) { |
| 212 |
List list = (List) o; |
| 213 |
for (Object elem : list) { |
| 214 |
if (elem instanceof ContainerNotifier) { |
| 215 |
ContainerNotifier crf = (ContainerNotifier) elem; |
| 216 |
crf.addListener(this); |
| 217 |
} |
| 218 |
} |
| 219 |
} else if (o instanceof ContainerNotifier) { |
| 220 |
ContainerNotifier crf = (ContainerNotifier) o; |
| 221 |
crf.addListener(this); |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Destroy this Web component. |
| 227 |
* <p/> |
| 228 |
* This will destroy the Web application created by this this Web component. |
| 229 |
*/ |
| 230 |
public void destroy() { |
| 231 |
if (application != null) |
| 232 |
application.destroy(); |
| 233 |
} |
| 234 |
|
| 235 |
private final static class Writer extends OutputStream implements ContainerResponseWriter { |
| 236 |
final HttpServletResponse response; |
| 237 |
|
| 238 |
ContainerResponse cResponse; |
| 239 |
|
| 240 |
long contentLength; |
| 241 |
|
| 242 |
OutputStream out; |
| 243 |
|
| 244 |
boolean statusAndHeadersWritten = false; |
| 245 |
|
| 246 |
Writer(HttpServletResponse response) { |
| 247 |
this.response = response; |
| 248 |
} |
| 249 |
|
| 250 |
public OutputStream writeStatusAndHeaders(long contentLength, |
| 251 |
ContainerResponse cResponse) throws IOException { |
| 252 |
this.contentLength = contentLength; |
| 253 |
this.cResponse = cResponse; |
| 254 |
this.statusAndHeadersWritten = false; |
| 255 |
return this; |
| 256 |
} |
| 257 |
|
| 258 |
public void finish() throws IOException { |
| 259 |
if (statusAndHeadersWritten) |
| 260 |
return; |
| 261 |
|
| 262 |
// Note that the writing of headers MUST be performed before |
| 263 |
// the invocation of sendError as on some Servlet implementations |
| 264 |
// modification of the response headers will have no effect |
| 265 |
// after the invocation of sendError. |
| 266 |
writeHeaders(); |
| 267 |
|
| 268 |
|
| 269 |
writeStatus(); |
| 270 |
} |
| 271 |
|
| 272 |
private void writeStatus() { |
| 273 |
Response.StatusType statusType = cResponse.getStatusType(); |
| 274 |
final String reasonPhrase = statusType.getReasonPhrase(); |
| 275 |
|
| 276 |
if (reasonPhrase != null) { |
| 277 |
response.setStatus(statusType.getStatusCode(), reasonPhrase); |
| 278 |
} else { |
| 279 |
response.setStatus(statusType.getStatusCode()); |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
public void write(int b) throws IOException { |
| 284 |
initiate(); |
| 285 |
out.write(b); |
| 286 |
} |
| 287 |
|
| 288 |
@Override |
| 289 |
public void write(byte b[]) throws IOException { |
| 290 |
if (b.length > 0) { |
| 291 |
initiate(); |
| 292 |
out.write(b); |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
@Override |
| 297 |
public void write(byte b[], int off, int len) throws IOException { |
| 298 |
if (len > 0) { |
| 299 |
initiate(); |
| 300 |
out.write(b, off, len); |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
@Override |
| 305 |
public void flush() throws IOException { |
| 306 |
writeStatusAndHeaders(); |
| 307 |
if (out != null) |
| 308 |
out.flush(); |
| 309 |
} |
| 310 |
|
| 311 |
@Override |
| 312 |
public void close() throws IOException { |
| 313 |
initiate(); |
| 314 |
out.close(); |
| 315 |
} |
| 316 |
|
| 317 |
void initiate() throws IOException { |
| 318 |
if (out == null) { |
| 319 |
writeStatusAndHeaders(); |
| 320 |
out = response.getOutputStream(); |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
void writeStatusAndHeaders() { |
| 325 |
if (statusAndHeadersWritten) |
| 326 |
return; |
| 327 |
|
| 328 |
writeHeaders(); |
| 329 |
writeStatus(); |
| 330 |
statusAndHeadersWritten = true; |
| 331 |
} |
| 332 |
|
| 333 |
void writeHeaders() { |
| 334 |
if (contentLength != -1 && contentLength < Integer.MAX_VALUE) |
| 335 |
response.setContentLength((int) contentLength); |
| 336 |
|
| 337 |
MultivaluedMap<String, Object> headers = cResponse.getHttpHeaders(); |
| 338 |
for (Map.Entry<String, List<Object>> e : headers.entrySet()) { |
| 339 |
for (Object v : e.getValue()) { |
| 340 |
response.addHeader(e.getKey(), ContainerResponse.getHeaderValue(v)); |
| 341 |
} |
| 342 |
} |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
/** |
| 347 |
* Dispatch client requests to a resource class. |
| 348 |
* |
| 349 |
* @param baseUri the base URI of the request. |
| 350 |
* @param requestUri the URI of the request. |
| 351 |
* @param request the {@link HttpServletRequest} object that |
| 352 |
* contains the request the client made to |
| 353 |
* the Web component. |
| 354 |
* @param response the {@link HttpServletResponse} object that |
| 355 |
* contains the response the Web component returns |
| 356 |
* to the client. |
| 357 |
* @return the status code of the response. |
| 358 |
* @throws IOException if an input or output error occurs |
| 359 |
* while the Web component is handling the |
| 360 |
* HTTP request. |
| 361 |
* @throws ServletException if the HTTP request cannot |
| 362 |
* be handled. |
| 363 |
*/ |
| 364 |
public int service(URI baseUri, URI requestUri, |
| 365 |
final HttpServletRequest request, |
| 366 |
final HttpServletResponse response) |
| 367 |
throws ServletException, IOException { |
| 368 |
// Copy the application field to local instance to ensure that the |
| 369 |
// currently loaded web application is used to process |
| 370 |
// request |
| 371 |
final WebApplication _application = application; |
| 372 |
|
| 373 |
final ContainerRequest cRequest = createRequest( |
| 374 |
_application, |
| 375 |
request, |
| 376 |
baseUri, |
| 377 |
requestUri); |
| 378 |
|
| 379 |
cRequest.setSecurityContext(new SecurityContext() { |
| 380 |
public Principal getUserPrincipal() { |
| 381 |
return request.getUserPrincipal(); |
| 382 |
} |
| 383 |
|
| 384 |
public boolean isUserInRole(String role) { |
| 385 |
return request.isUserInRole(role); |
| 386 |
} |
| 387 |
|
| 388 |
public boolean isSecure() { |
| 389 |
return request.isSecure(); |
| 390 |
} |
| 391 |
|
| 392 |
public String getAuthenticationScheme() { |
| 393 |
return request.getAuthType(); |
| 394 |
} |
| 395 |
}); |
| 396 |
|
| 397 |
try { |
| 398 |
// Check if any servlet filters have consumed a request entity |
| 399 |
// of the media type application/x-www-form-urlencoded |
| 400 |
// This can happen if a filter calls request.getParameter(...) |
| 401 |
filterFormParameters(request, cRequest); |
| 402 |
|
| 403 |
UriRuleProbeProvider.requestStart(requestUri); |
| 404 |
|
| 405 |
requestInvoker.set(request); |
| 406 |
responseInvoker.set(response); |
| 407 |
|
| 408 |
final Writer w = new Writer(response); |
| 409 |
_application.handleRequest(cRequest, w); |
| 410 |
return w.cResponse.getStatus(); |
| 411 |
} catch (WebApplicationException ex) { |
| 412 |
final Response exResponse = ex.getResponse(); |
| 413 |
final String entity = exResponse.getEntity() != null ? exResponse.getEntity().toString() : null; |
| 414 |
|
| 415 |
response.sendError(exResponse.getStatus(), entity); |
| 416 |
|
| 417 |
return exResponse.getStatus(); |
| 418 |
} catch (MappableContainerException ex) { |
| 419 |
traceOnException(cRequest, response); |
| 420 |
throw new ServletException(ex.getCause()); |
| 421 |
} catch (ContainerException ex) { |
| 422 |
traceOnException(cRequest, response); |
| 423 |
throw new ServletException(ex); |
| 424 |
} catch (RuntimeException ex) { |
| 425 |
traceOnException(cRequest, response); |
| 426 |
throw ex; |
| 427 |
} finally { |
| 428 |
UriRuleProbeProvider.requestEnd(); |
| 429 |
requestInvoker.set(null); |
| 430 |
responseInvoker.set(null); |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
/** |
| 435 |
* Extension point for creating your custom container request. |
| 436 |
* |
| 437 |
* @param app the web app |
| 438 |
* @param request the current servlet api request |
| 439 |
* @param baseUri the base uri |
| 440 |
* @param requestUri the request uri |
| 441 |
* @return the request container |
| 442 |
* @throws IOException if any error occurs when getting the input stream |
| 443 |
*/ |
| 444 |
protected ContainerRequest createRequest(WebApplication app, |
| 445 |
HttpServletRequest request, URI baseUri, URI requestUri) throws IOException { |
| 446 |
return new ContainerRequest( |
| 447 |
app, |
| 448 |
request.getMethod(), |
| 449 |
baseUri, |
| 450 |
requestUri, |
| 451 |
getHeaders(request), |
| 452 |
request.getInputStream()); |
| 453 |
} |
| 454 |
|
| 455 |
private void traceOnException(final ContainerRequest cRequest, final HttpServletResponse response) { |
| 456 |
if (cRequest.isTracingEnabled()) { |
| 457 |
final TraceInformation ti = (TraceInformation) cRequest.getProperties(). |
| 458 |
get(TraceInformation.class.getName()); |
| 459 |
|
| 460 |
ti.addTraceHeaders(new TraceInformation.TraceHeaderListener() { |
| 461 |
public void onHeader(String name, String value) { |
| 462 |
response.addHeader(name, value); |
| 463 |
} |
| 464 |
}); |
| 465 |
} |
| 466 |
} |
| 467 |
|
| 468 |
/** |
| 469 |
* Create a new instance of a {@link WebApplication}. |
| 470 |
* |
| 471 |
* @return the {@link WebApplication} instance. |
| 472 |
*/ |
| 473 |
protected WebApplication create() { |
| 474 |
return WebApplicationFactory.createWebApplication(); |
| 475 |
} |
| 476 |
|
| 477 |
/** |
| 478 |
* A helper class for creating an injectable provider that supports |
| 479 |
* {@link Context} with a type and constant value. |
| 480 |
* |
| 481 |
* @param <T> the type of the constant value. |
| 482 |
*/ |
| 483 |
protected static class ContextInjectableProvider<T> extends |
| 484 |
SingletonTypeInjectableProvider<Context, T> { |
| 485 |
|
| 486 |
/** |
| 487 |
* Create a new instance. |
| 488 |
* |
| 489 |
* @param type the type of the constant value. |
| 490 |
* @param instance the constant value. |
| 491 |
*/ |
| 492 |
protected ContextInjectableProvider(Type type, T instance) { |
| 493 |
super(type, instance); |
| 494 |
} |
| 495 |
} |
| 496 |
|
| 497 |
/** |
| 498 |
* Configure the {@link ResourceConfig}. |
| 499 |
* <p/> |
| 500 |
* The {@link ResourceConfig} is configured such that the following classes |
| 501 |
* may be injected onto the field of a root resource class or a parameter |
| 502 |
* of a method of root resource class that is annotated with |
| 503 |
* {@link javax.ws.rs.core.Context}: {@link HttpServletRequest}, {@link HttpServletResponse} |
| 504 |
* , {@link ServletContext} and {@link WebConfig}. |
| 505 |
* <p/> |
| 506 |
* Any root resource class in registered in the resource configuration |
| 507 |
* that is an interface is processed as follows. |
| 508 |
* If the class is an interface and there exists a JNDI named object |
| 509 |
* with the fully qualified class name as the JNDI name then that named |
| 510 |
* object is added as a singleton root resource and the class is removed |
| 511 |
* from the set of root resource classes. |
| 512 |
* <p/> |
| 513 |
* An inheriting class may override this method to configure the |
| 514 |
* {@link ResourceConfig} to provide alternative or additional instances |
| 515 |
* that are resource or provider classes or instances, and may modify the |
| 516 |
* features and properties of the {@link ResourceConfig}. For an inheriting |
| 517 |
* class to extend configuration behaviour the overriding method MUST call |
| 518 |
* <code>super.configure(servletConfig, rc, wa)</code> as the first statement |
| 519 |
* of that method. |
| 520 |
* <p/> |
| 521 |
* This method will be called only once at initiation. Subsequent |
| 522 |
* reloads of the Web application will not result in subsequence calls to |
| 523 |
* this method. |
| 524 |
* |
| 525 |
* @param wc the Web configuration |
| 526 |
* @param rc the Resource configuration |
| 527 |
* @param wa the Web application |
| 528 |
*/ |
| 529 |
protected void configure(WebConfig wc, ResourceConfig rc, WebApplication wa) { |
| 530 |
configureJndiResources(rc); |
| 531 |
|
| 532 |
rc.getSingletons().add(new ContextInjectableProvider<HttpServletRequest>( |
| 533 |
HttpServletRequest.class, |
| 534 |
(HttpServletRequest) Proxy.newProxyInstance( |
| 535 |
this.getClass().getClassLoader(), |
| 536 |
new Class[]{HttpServletRequest.class}, |
| 537 |
requestInvoker))); |
| 538 |
|
| 539 |
rc.getSingletons().add(new ContextInjectableProvider<HttpServletResponse>( |
| 540 |
HttpServletResponse.class, |
| 541 |
(HttpServletResponse) Proxy.newProxyInstance( |
| 542 |
this.getClass().getClassLoader(), |
| 543 |
new Class[]{HttpServletResponse.class}, |
| 544 |
responseInvoker))); |
| 545 |
|
| 546 |
GenericEntity<ThreadLocal<HttpServletRequest>> requestThreadLocal = |
| 547 |
new GenericEntity<ThreadLocal<HttpServletRequest>>(requestInvoker.getImmutableThreadLocal()) { |
| 548 |
}; |
| 549 |
rc.getSingletons().add(new ContextInjectableProvider<ThreadLocal<HttpServletRequest>>( |
| 550 |
requestThreadLocal.getType(), requestThreadLocal.getEntity())); |
| 551 |
|
| 552 |
GenericEntity<ThreadLocal<HttpServletResponse>> responseThreadLocal = |
| 553 |
new GenericEntity<ThreadLocal<HttpServletResponse>>(responseInvoker.getImmutableThreadLocal()) { |
| 554 |
}; |
| 555 |
rc.getSingletons().add(new ContextInjectableProvider<ThreadLocal<HttpServletResponse>>( |
| 556 |
responseThreadLocal.getType(), responseThreadLocal.getEntity())); |
| 557 |
|
| 558 |
rc.getSingletons().add(new ContextInjectableProvider<ServletContext>( |
| 559 |
ServletContext.class, |
| 560 |
wc.getServletContext())); |
| 561 |
|
| 562 |
rc.getSingletons().add(new ContextInjectableProvider<WebConfig>( |
| 563 |
WebConfig.class, |
| 564 |
wc)); |
| 565 |
|
| 566 |
rc.getClasses().add(JSPTemplateProcessor.class); |
| 567 |
|
| 568 |
// TODO |
| 569 |
// If CDI is enabled should EJB injection be disabled? |
| 570 |
EJBComponentProviderFactoryInitilizer.initialize(rc); |
| 571 |
|
| 572 |
CDIComponentProviderFactoryInitializer.initialize(wc, rc, wa); |
| 573 |
|
| 574 |
// TODO |
| 575 |
// If CDI is enabled then no need to initialize managed beans |
| 576 |
ManagedBeanComponentProviderFactoryInitilizer.initialize(rc); |
| 577 |
|
| 578 |
GlassFishMonitoringInitializer.initialize(); |
| 579 |
} |
| 580 |
|
| 581 |
/** |
| 582 |
* Initiate the {@link WebApplication}. |
| 583 |
* <p/> |
| 584 |
* This method will be called once at initiation and for |
| 585 |
* each reload of the Web application. |
| 586 |
* <p/> |
| 587 |
* An inheriting class may override this method to initiate the |
| 588 |
* Web application with different parameters. |
| 589 |
* |
| 590 |
* @param rc the Resource configuration |
| 591 |
* @param wa the Web application |
| 592 |
*/ |
| 593 |
protected void initiate(ResourceConfig rc, WebApplication wa) { |
| 594 |
wa.initiate(rc); |
| 595 |
} |
| 596 |
|
| 597 |
|
| 598 |
/** |
| 599 |
* Load the Web application. This will create, configure and initiate |
| 600 |
* the web application. |
| 601 |
*/ |
| 602 |
public void load() { |
| 603 |
WebApplication _application = create(); |
| 604 |
configure(config, resourceConfig, _application); |
| 605 |
initiate(resourceConfig, _application); |
| 606 |
application = _application; |
| 607 |
} |
| 608 |
|
| 609 |
/** |
| 610 |
* Get the default resource configuration if one is not declared in the |
| 611 |
* web.xml. |
| 612 |
* <p/> |
| 613 |
* This implementation returns an instance of {@link WebAppResourceConfig} |
| 614 |
* that scans in files and directories as declared by the |
| 615 |
* {@link ClasspathResourceConfig#PROPERTY_CLASSPATH} if present, otherwise |
| 616 |
* in the "WEB-INF/lib" and "WEB-INF/classes" directories. |
| 617 |
* <p/> |
| 618 |
* An inheriting class may override this method to supply a different |
| 619 |
* default resource configuration implementation. |
| 620 |
* |
| 621 |
* @param props the properties to pass to the resource configuration. |
| 622 |
* @param wc the web configuration. |
| 623 |
* @return the default resource configuration. |
| 624 |
* @throws javax.servlet.ServletException in case of any issues with providing \ |
| 625 |
* the default resource configuration |
| 626 |
*/ |
| 627 |
protected ResourceConfig getDefaultResourceConfig(Map<String, Object> props, |
| 628 |
WebConfig wc) throws ServletException { |
| 629 |
return getWebAppResourceConfig(props, wc); |
| 630 |
} |
| 631 |
|
| 632 |
|
| 633 |
// ContainerListener |
| 634 |
|
| 635 |
/** |
| 636 |
* Reload the Web application. This will create and initiate the web |
| 637 |
* application using the same {@link ResourceConfig} implementation |
| 638 |
* that was used to load the Web application. |
| 639 |
* <p/> |
| 640 |
* This method may be called at runtime, more than once, to reload the |
| 641 |
* Web application. For example, if a {@link ResourceConfig} implementation |
| 642 |
* is capable of detecting changes to resource classes (addition or removal) |
| 643 |
* or providers then this method may be invoked to reload the web |
| 644 |
* application for such changes to take effect. |
| 645 |
* <p/> |
| 646 |
* If this method is called when there are pending requests then such |
| 647 |
* requests will be processed using the previously loaded web application. |
| 648 |
*/ |
| 649 |
public void onReload() { |
| 650 |
WebApplication oldApplication = application; |
| 651 |
WebApplication newApplication = create(); |
| 652 |
initiate(resourceConfig, newApplication); |
| 653 |
application = newApplication; |
| 654 |
|
| 655 |
if (resourceConfig instanceof ReloadListener) |
| 656 |
((ReloadListener) resourceConfig).onReload(); |
| 657 |
|
| 658 |
oldApplication.destroy(); |
| 659 |
|
| 660 |
} |
| 661 |
|
| 662 |
|
| 663 |
// |
| 664 |
|
| 665 |
/* package */ ResourceConfig getWebAppResourceConfig(Map<String, Object> props, |
| 666 |
WebConfig webConfig) throws ServletException { |
| 667 |
// Default to using Web app resource config |
| 668 |
return new WebAppResourceConfig(props, webConfig.getServletContext()); |
| 669 |
} |
| 670 |
|
| 671 |
private ResourceConfig createResourceConfig(WebConfig webConfig) |
| 672 |
throws ServletException { |
| 673 |
final Map<String, Object> props = getInitParams(webConfig); |
| 674 |
final ResourceConfig rc = createResourceConfig(webConfig, props); |
| 675 |
rc.setPropertiesAndFeatures(props); |
| 676 |
return rc; |
| 677 |
} |
| 678 |
|
| 679 |
private ResourceConfig createResourceConfig(WebConfig webConfig, Map<String, Object> props) |
| 680 |
throws ServletException { |
| 681 |
// Check if the resource config class property is present |
| 682 |
String resourceConfigClassName = webConfig.getInitParameter(RESOURCE_CONFIG_CLASS); |
| 683 |
// Otherwise check if the JAX-RS application config class property is |
| 684 |
// present |
| 685 |
if (resourceConfigClassName == null) |
| 686 |
resourceConfigClassName = webConfig.getInitParameter(APPLICATION_CONFIG_CLASS); |
| 687 |
|
| 688 |
// If no resource config class property is present |
| 689 |
if (resourceConfigClassName == null) { |
| 690 |
// If the packages property is present then |
| 691 |
// use the packages resource config |
| 692 |
String packages = webConfig.getInitParameter( |
| 693 |
PackagesResourceConfig.PROPERTY_PACKAGES); |
| 694 |
if (packages != null) { |
| 695 |
props.put(PackagesResourceConfig.PROPERTY_PACKAGES, packages); |
| 696 |
return new PackagesResourceConfig(props); |
| 697 |
} |
| 698 |
|
| 699 |
ResourceConfig defaultConfig = webConfig.getDefaultResourceConfig(props); |
| 700 |
if (defaultConfig != null) |
| 701 |
return defaultConfig; |
| 702 |
|
| 703 |
return getDefaultResourceConfig(props, webConfig); |
| 704 |
} |
| 705 |
|
| 706 |
try { |
| 707 |
Class<?> resourceConfigClass = AccessController.doPrivileged(ReflectionHelper. |
| 708 |
classForNameWithExceptionPEA(resourceConfigClassName)); |
| 709 |
|
| 710 |
// TODO add support for WebAppResourceConfig |
| 711 |
if (resourceConfigClass == ClasspathResourceConfig.class) { |
| 712 |
String[] paths = getPaths(webConfig.getInitParameter( |
| 713 |
ClasspathResourceConfig.PROPERTY_CLASSPATH)); |
| 714 |
props.put(ClasspathResourceConfig.PROPERTY_CLASSPATH, paths); |
| 715 |
return new ClasspathResourceConfig(props); |
| 716 |
} else if (ResourceConfig.class.isAssignableFrom(resourceConfigClass)) { |
| 717 |
try { |
| 718 |
Constructor constructor = resourceConfigClass.getConstructor(Map.class); |
| 719 |
if (ClasspathResourceConfig.class.isAssignableFrom(resourceConfigClass)) { |
| 720 |
String[] paths = getPaths(webConfig.getInitParameter( |
| 721 |
ClasspathResourceConfig.PROPERTY_CLASSPATH)); |
| 722 |
props.put(ClasspathResourceConfig.PROPERTY_CLASSPATH, paths); |
| 723 |
} |
| 724 |
return (ResourceConfig) constructor.newInstance(props); |
| 725 |
} catch (NoSuchMethodException ex) { |
| 726 |
// Pass through and try the default constructor |
| 727 |
} catch (Exception e) { |
| 728 |
throw new ServletException(e); |
| 729 |
} |
| 730 |
|
| 731 |
return new DeferredResourceConfig(resourceConfigClass.asSubclass(ResourceConfig.class)); |
| 732 |
} else if (Application.class.isAssignableFrom(resourceConfigClass)) { |
| 733 |
return new DeferredResourceConfig(resourceConfigClass.asSubclass(Application.class)); |
| 734 |
} else { |
| 735 |
String message = "Resource configuration class, " + resourceConfigClassName + |
| 736 |
", is not a super class of " + Application.class; |
| 737 |
throw new ServletException(message); |
| 738 |
} |
| 739 |
} catch (ClassNotFoundException e) { |
| 740 |
String message = "Resource configuration class, " + resourceConfigClassName + |
| 741 |
", could not be loaded"; |
| 742 |
throw new ServletException(message, e); |
| 743 |
} catch (PrivilegedActionException e) { |
| 744 |
String message = "Resource configuration class, " + resourceConfigClassName + |
| 745 |
", could not be loaded"; |
| 746 |
throw new ServletException(message, e.getCause()); |
| 747 |
} |
| 748 |
} |
| 749 |
|
| 750 |
private Map<String, Object> getInitParams(WebConfig webConfig) { |
| 751 |
Map<String, Object> props = new HashMap<String, Object>(); |
| 752 |
Enumeration names = webConfig.getInitParameterNames(); |
| 753 |
while (names.hasMoreElements()) { |
| 754 |
String name = (String) names.nextElement(); |
| 755 |
props.put(name, webConfig.getInitParameter(name)); |
| 756 |
} |
| 757 |
return props; |
| 758 |
} |
| 759 |
|
| 760 |
private String[] getPaths(String classpath) throws ServletException { |
| 761 |
final ServletContext context = config.getServletContext(); |
| 762 |
if (classpath == null) { |
| 763 |
String[] paths = { |
| 764 |
context.getRealPath("/WEB-INF/lib"), |
| 765 |
context.getRealPath("/WEB-INF/classes") |
| 766 |
}; |
| 767 |
if (paths[0] == null && paths[1] == null) { |
| 768 |
String message = "The default deployment configuration that scans for " + |
| 769 |
"classes in /WEB-INF/lib and /WEB-INF/classes is not supported " + |
| 770 |
"for the application server." + |
| 771 |
"Try using the package scanning configuration, see the JavaDoc for " + |
| 772 |
PackagesResourceConfig.class.getName() + " and the property " + |
| 773 |
PackagesResourceConfig.PROPERTY_PACKAGES + "."; |
| 774 |
throw new ServletException(message); |
| 775 |
} |
| 776 |
return paths; |
| 777 |
} else { |
| 778 |
String[] virtualPaths = classpath.split(";"); |
| 779 |
List<String> resourcePaths = new ArrayList<String>(); |
| 780 |
for (String virtualPath : virtualPaths) { |
| 781 |
virtualPath = virtualPath.trim(); |
| 782 |
if (virtualPath.length() == 0) continue; |
| 783 |
String path = context.getRealPath(virtualPath); |
| 784 |
if (path != null) resourcePaths.add(path); |
| 785 |
} |
| 786 |
if (resourcePaths.isEmpty()) { |
| 787 |
String message = "None of the declared classpath locations, " + |
| 788 |
classpath + |
| 789 |
", could be resolved. " + |
| 790 |
"This could be because the default deployment configuration that scans for " + |
| 791 |
"classes in classpath locations is not supported. " + |
| 792 |
"Try using the package scanning configuration, see the JavaDoc for " + |
| 793 |
PackagesResourceConfig.class.getName() + " and the property " + |
| 794 |
PackagesResourceConfig.PROPERTY_PACKAGES + "."; |
| 795 |
throw new ServletException(message); |
| 796 |
} |
| 797 |
return resourcePaths.toArray(new String[resourcePaths.size()]); |
| 798 |
} |
| 799 |
} |
| 800 |
|
| 801 |
private void configureJndiResources(ResourceConfig rc) { |
| 802 |
// Obtain any instances that are registered in JNDI |
| 803 |
// Assumes such instances are singletons |
| 804 |
// Registered classes have to be interfaces |
| 805 |
javax.naming.Context x = InitialContextHelper.getInitialContext(); |
| 806 |
if (x != null) { |
| 807 |
Iterator<Class<?>> i = rc.getClasses().iterator(); |
| 808 |
while (i.hasNext()) { |
| 809 |
Class<?> c = i.next(); |
| 810 |
if (!c.isInterface()) continue; |
| 811 |
|
| 812 |
try { |
| 813 |
Object o = x.lookup(c.getName()); |
| 814 |
if (o != null) { |
| 815 |
i.remove(); |
| 816 |
rc.getSingletons().add(o); |
| 817 |
LOGGER.log(Level.INFO, |
| 818 |
"An instance of the class " + c.getName() + |
| 819 |
" is found by JNDI look up using the class name as the JNDI name. " + |
| 820 |
"The instance will be registered as a singleton."); |
| 821 |
} |
| 822 |
} catch (NamingException ex) { |
| 823 |
LOGGER.log(Level.CONFIG, |
| 824 |
"JNDI lookup failed for Jersey application resource " + c.getName(), |
| 825 |
ex); |
| 826 |
} |
| 827 |
} |
| 828 |
} |
| 829 |
} |
| 830 |
|
| 831 |
private void filterFormParameters(HttpServletRequest servletRequest, ContainerRequest containerRequet) throws IOException { |
| 832 |
if (MediaTypes.typeEquals(MediaType.APPLICATION_FORM_URLENCODED_TYPE, containerRequet.getMediaType()) |
| 833 |
&& !isEntityPresent(containerRequet)) { |
| 834 |
Form f = new Form(); |
| 835 |
|
| 836 |
Enumeration e = servletRequest.getParameterNames(); |
| 837 |
while (e.hasMoreElements()) { |
| 838 |
String name = (String) e.nextElement(); |
| 839 |
String[] values = servletRequest.getParameterValues(name); |
| 840 |
|
| 841 |
f.put(name, Arrays.asList(values)); |
| 842 |
} |
| 843 |
|
| 844 |
if (!f.isEmpty()) { |
| 845 |
containerRequet.getProperties().put(FormDispatchProvider.FORM_PROPERTY, f); |
| 846 |
if (LOGGER.isLoggable(Level.WARNING)) { |
| 847 |
LOGGER.log(Level.WARNING, |
| 848 |
"A servlet request, to the URI " + containerRequet.getRequestUri() + ", " + |
| 849 |
"contains form parameters in " + |
| 850 |
"the request body but the request body has been consumed " + |
| 851 |
"by the servlet or a servlet filter accessing the request " + |
| 852 |
"parameters. Only resource methods using @FormParam " + |
| 853 |
"will work as expected. Resource methods consuming the " + |
| 854 |
"request body by other means will not work as expected."); |
| 855 |
} |
| 856 |
} |
| 857 |
} |
| 858 |
} |
| 859 |
|
| 860 |
private boolean isEntityPresent(ContainerRequest cr) throws IOException { |
| 861 |
InputStream in = cr.getEntityInputStream(); |
| 862 |
if (!in.markSupported()) { |
| 863 |
in = new BufferedInputStream(in, ReaderWriter.BUFFER_SIZE); |
| 864 |
cr.setEntityInputStream(in); |
| 865 |
} |
| 866 |
|
| 867 |
in.mark(1); |
| 868 |
if (in.read() == -1) |
| 869 |
return false; |
| 870 |
else { |
| 871 |
in.reset(); |
| 872 |
return true; |
| 873 |
} |
| 874 |
} |
| 875 |
|
| 876 |
private InBoundHeaders getHeaders(HttpServletRequest request) { |
| 877 |
InBoundHeaders rh = new InBoundHeaders(); |
| 878 |
|
| 879 |
for (Enumeration<String> names = request.getHeaderNames(); names.hasMoreElements();) { |
| 880 |
String name = names.nextElement(); |
| 881 |
List<String> valueList = new LinkedList<String>(); |
| 882 |
for (Enumeration<String> values = request.getHeaders(name); values.hasMoreElements();) { |
| 883 |
valueList.add(values.nextElement()); |
| 884 |
} |
| 885 |
rh.put(name, valueList); |
| 886 |
} |
| 887 |
|
| 888 |
return rh; |
| 889 |
} |
| 890 |
} |