public class ServletContainer
extends javax.servlet.http.HttpServlet
implements javax.servlet.Filter
Servlet
or Filter
for deploying root resource classes.
If this class is declared as a filter and the initialization parameter
PROPERTY_WEB_PAGE_CONTENT_REGEX
is not set
or FEATURE_FILTER_FORWARD_ON_404
is not set to true then the filter
must be declared at the last position in the filter chain as the filter will
not forward any request to a next filter (if any) in the chain.
The following sections make reference to initialization parameters. Unless otherwise specified the initialization parameters apply to both server and filter initialization parameters.
The servlet or filter may be configured to have an initialization
parameter "com.sun.jersey.config.property.resourceConfigClass" or
"javax.ws.rs.Application" and whose value is a
fully qualified name of a class that implements ResourceConfig
or
Application
.
If the concrete class has a constructor that takes a single parameter of the
type Map then the class is instantiated with that constructor and an instance
of Map that contains all the initialization parameters is passed as the
parameter. Otherwise, the class is instantiated as a singleton component
managed by the runtime, and injection may be performed (the artifacts that
may be injected are limited to injectable providers registered when
the servlet or filter is configured).
If the initialization parameter
"com.sun.jersey.config.property.resourceConfigClass" or
"javax.ws.rs.Application" is not present and a
initialization parameter "com.sun.jersey.config.property.packages" is present
(see PackagesResourceConfig.PROPERTY_PACKAGES
) a new instance of
PackagesResourceConfig
is created. The initialization parameter
"com.sun.jersey.config.property.packages" MUST be set to provide one or
more package names. Each package name MUST be separated by ';'.
The package names are added as a property value to a Map instance using
the property name "com.sun.jersey.config.property.packages".
Any additional initialization parameters are then added to the Map instance.
Then that Map instance is passed to the constructor of
PackagesResourceConfig
.
If none of the above resource configuration related initialization parameters
are present a new instance of WebAppResourceConfig
is created. The
initialization parameter "com.sun.jersey.config.property.classpath" MAY be
set to provide one or more resource paths. Each path MUST be separated by ';'.
The resource paths are added as a property value to a Map instance using
the property name "com.sun.jersey.config.property.classpath".
Any additional initialization parameters are then added to the Map instance.
Then that Map instance is passed to the constructor of
WebAppResourceConfig
.
If the initialization parameter is not present then the following resource
paths are utilized:
"/WEB-INF/lib" and "/WEB-INF/classes".
All initialization parameters are added as properties of the created
ResourceConfig
.
A new WebApplication
instance will be created and configured such
that the following classes may be injected onto a root resource, provider
and Application
classes using Context
:
HttpServletRequest
, HttpServletResponse
,
ServletContext
, ServletConfig
and WebConfig
.
If this class is used as a Servlet then the ServletConfig
class may
be injected. If this class is used as a Filter then the FilterConfig
class may be injected. WebConfig
may be injected to abstract
servlet or filter deployment.
A IoCComponentProviderFactory
instance may be registered by extending this class
and overriding the method initiate(ResourceConfig, WebApplication)
to initiate the WebApplication
with the IoCComponentProviderFactory
instance.
Modifier and Type | Class and Description |
---|---|
protected static class |
ServletContainer.ContextInjectableProvider<T>
A helper class for creating an injectable provider that supports
Context with a type and constant value. |
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
APPLICATION_CONFIG_CLASS
The servlet initialization property whose value is a fully qualified
class name of a class that implements
ResourceConfig or
Application . |
static java.lang.String |
FEATURE_ALLOW_RAW_MANAGED_BEANS
If set to true then the internal Jersey CDI component provider
will be used for managed bean types,
that are not annotated with
ManagedBean annotation (neither with any other
bean defining annotation) and CDI bean manager
evaluates them as dependent scoped beans. |
static java.lang.String |
FEATURE_FILTER_FORWARD_ON_404
If true and a 404 response with no entity body is returned from either
the runtime or the application then the runtime forwards the request to
the next filter in the filter chain.
|
static java.lang.String |
GLASSFISH_DEFAULT_ERROR_PAGE_RESPONSE
The servlet initialization property whose boolean value determines
if GlassFish default error pages will be returned or not.
|
static java.lang.String |
JSP_TEMPLATES_BASE_PATH
The base path in the Web Pages where JSP templates, associated with
viewables of resource classes, are located.
|
static java.lang.String |
PROPERTY_FILTER_CONTEXT_PATH
The filter context path.
|
static java.lang.String |
PROPERTY_WEB_PAGE_CONTENT_REGEX
If set the regular expression used to match an incoming servlet path URI
to some web page content such as static resources or JSPs to be handled
by the underlying servlet engine.
|
static java.lang.String |
RESOURCE_CONFIG_CLASS
The servlet initialization property whose value is a fully qualified
class name of a class that implements
ResourceConfig or
Application . |
Constructor and Description |
---|
ServletContainer() |
ServletContainer(Application app) |
ServletContainer(java.lang.Class<? extends Application> appClass) |
Modifier and Type | Method and Description |
---|---|
protected void |
configure(javax.servlet.FilterConfig fc,
ResourceConfig rc,
WebApplication wa)
Configure the
ResourceConfig for a Filter. |
protected void |
configure(javax.servlet.ServletConfig sc,
ResourceConfig rc,
WebApplication wa)
Configure the
ResourceConfig for a Servlet. |
protected void |
configure(WebConfig wc,
ResourceConfig rc,
WebApplication wa)
Configure the
ResourceConfig . |
protected WebApplication |
create()
Create a new instance of a
WebApplication . |
void |
destroy()
Destroy this Servlet or Filter.
|
void |
doFilter(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response,
javax.servlet.FilterChain chain)
Dispatches client requests to the
service(java.net.URI, java.net.URI, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
method. |
void |
doFilter(javax.servlet.ServletRequest request,
javax.servlet.ServletResponse response,
javax.servlet.FilterChain chain)
Dispatches client requests to the
doFilter(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, javax.servlet.FilterChain)
method. |
protected ResourceConfig |
getDefaultResourceConfig(java.util.Map<java.lang.String,java.lang.Object> props,
javax.servlet.ServletConfig servletConfig)
Deprecated.
methods should implement
getDefaultResourceConfig(java.util.Map, com.sun.jersey.spi.container.servlet.WebConfig) . |
protected ResourceConfig |
getDefaultResourceConfig(java.util.Map<java.lang.String,java.lang.Object> props,
WebConfig wc)
Get the default resource configuration if one is not declared in the
web.xml.
|
javax.servlet.ServletContext |
getServletContext()
Get the servlet context for the servlet or filter, depending on
how this class is registered.
|
java.util.regex.Pattern |
getStaticContentPattern() |
protected WebConfig |
getWebConfig()
Get the Web configuration.
|
void |
init() |
void |
init(javax.servlet.FilterConfig filterConfig) |
protected void |
init(WebConfig webConfig)
Initiate the Web component.
|
protected void |
initiate(ResourceConfig rc,
WebApplication wa)
Initiate the
WebApplication . |
void |
load()
Load the Web application.
|
void |
reload()
Reload the Web application.
|
void |
service(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
Dispatches client requests to the
service(java.net.URI, java.net.URI, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
method. |
int |
service(java.net.URI baseUri,
java.net.URI requestUri,
javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
Dispatch client requests to a resource class.
|
doDelete, doGet, doHead, doOptions, doPost, doPut, doTrace, getLastModified, service
public static final java.lang.String GLASSFISH_DEFAULT_ERROR_PAGE_RESPONSE
The default value is true.
If false then GlassFish will not return default error pages.
This property is supported on GlassFish version 3.1 or greater.
public static final java.lang.String APPLICATION_CONFIG_CLASS
ResourceConfig
or
Application
.public static final java.lang.String RESOURCE_CONFIG_CLASS
ResourceConfig
or
Application
.public static final java.lang.String JSP_TEMPLATES_BASE_PATH
If this property is not set then the base path will be the root path of the Web Pages.
public static final java.lang.String PROPERTY_WEB_PAGE_CONTENT_REGEX
The type of this property must be a String and the value must be a valid regular expression.
This property is only applicable when this class is used as a
Filter
, otherwise this property will be ignored and not
processed.
If a servlet path matches this regular expression then the filter forwards the request to the next filter in the filter chain so that the underlying servlet engine can process the request otherwise Jersey will process the request.
For example if you set the value to
/(image|css)/.*
then you can serve up images and CSS files for your Implicit or Explicit
Views while still processing your JAX-RS resources.
public static final java.lang.String FEATURE_FILTER_FORWARD_ON_404
This property is only applicable when this class is used as a
Filter
, otherwise this property will be ignored and not
processed.
Application code, such as methods corresponding to sub-resource locators may be invoked when this feature is enabled.
This feature is an alternative to setting
PROPERTY_WEB_PAGE_CONTENT_REGEX
and requires less configuration.
However, application code, such as methods corresponding to sub-resource
locators, may be invoked when this feature is enabled.
The default value is false.
public static final java.lang.String PROPERTY_FILTER_CONTEXT_PATH
If the URL pattern of a filter is set to a base path and a wildcard, such as "/base/*", then this property can be used to declare a filter context path that behaves in the same manner as the Servlet context path for determining the base URI of the application. (Note that with the Servlet 2.x API it is not possible to determine the URL pattern without parsing the web.xml, hence why this property is necessary.)
This property is only applicable when this class is used as a
Filter
, otherwise this property will be ignored and not
processed.
This property may consist of one or more path segments separate by '/'.
public static final java.lang.String FEATURE_ALLOW_RAW_MANAGED_BEANS
ManagedBean
annotation (neither with any other
bean defining annotation) and CDI bean manager
evaluates them as dependent scoped beans.
This feature is only applicable when CDI processing is enabled,
i.e. when beans.xml
file is present.
This feature is an alternative to adding ManagedBean
annotation
to managed bean types directly.
The default value is false.public ServletContainer()
public ServletContainer(java.lang.Class<? extends Application> appClass)
public ServletContainer(Application app)
public javax.servlet.ServletContext getServletContext()
It is recommended that the WebConfig
be utilized,
see the method getWebConfig()
, to obtain the servlet context
and initialization parameters for a servlet or filter.
getServletContext
in interface javax.servlet.ServletConfig
getServletContext
in class javax.servlet.GenericServlet
protected void init(WebConfig webConfig) throws javax.servlet.ServletException
webConfig
- the Web configuration.javax.servlet.ServletException
- in case of an initialization failureprotected WebConfig getWebConfig()
protected WebApplication create()
WebApplication
.WebApplication
instance.protected ResourceConfig getDefaultResourceConfig(java.util.Map<java.lang.String,java.lang.Object> props, WebConfig wc) throws javax.servlet.ServletException
This implementation returns an instance of WebAppResourceConfig
that scans in files and directories as declared by the
ClasspathResourceConfig.PROPERTY_CLASSPATH
if present, otherwise
in the "WEB-INF/lib" and "WEB-INF/classes" directories.
An inheriting class may override this method to supply a different default resource configuration implementation.
props
- the properties to pass to the resource configuration.wc
- the web configuration.javax.servlet.ServletException
- in case there was an error while retrieving the default resource configprotected void configure(WebConfig wc, ResourceConfig rc, WebApplication wa)
ResourceConfig
.
The ResourceConfig
is configured such that the following classes
may be injected onto the field of a root resource class or a parameter
of a method of root resource class that is annotated with
Context
: HttpServletRequest
, HttpServletResponse
, ServletContext
and WebConfig
.
Any root resource class in registered in the resource configuration that is an interface is processed as follows. If the class is an interface and there exists a JNDI named object with the fully qualified class name as the JNDI name then that named object is added as a singleton root resource and the class is removed from the set of root resource classes.
An inheriting class may override this method to configure the
ResourceConfig
to provide alternative or additional instances
that are resource or provider classes or instances, and may modify the
features and properties of the ResourceConfig
. For an inheriting
class to extend configuration behaviour the overriding method MUST call
super.configure(servletConfig, rc, wa)
as the first statement
of that method.
This method will be called only once at initiation. Subsequent reloads of the Web application will not result in subsequence calls to this method.
wc
- the Web configurationrc
- the Resource configurationwa
- the Web applicationprotected void initiate(ResourceConfig rc, WebApplication wa)
WebApplication
.
This method will be called once at initiation and for each reload of the Web application.
An inheriting class may override this method to initiate the Web application with different parameters.
rc
- the Resource configurationwa
- the Web applicationpublic void load()
public void reload()
ResourceConfig
implementation
that was used to load the Web application.
This method may be called at runtime, more than once, to reload the
Web application. For example, if a ResourceConfig
implementation
is capable of detecting changes to resource classes (addition or removal)
or providers then this method may be invoked to reload the web
application for such changes to take effect.
If this method is called when there are pending requests then such requests will be processed using the previously loaded web application.
public int service(java.net.URI baseUri, java.net.URI requestUri, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException
baseUri
- the base URI of the request.requestUri
- the URI of the request.request
- the HttpServletRequest
object that
contains the request the client made to
the Web component.response
- the HttpServletResponse
object that
contains the response the Web component returns
to the client.java.io.IOException
- if an input or output error occurs
while the Web component is handling the
HTTP request.javax.servlet.ServletException
- if the HTTP request cannot
be handled.public void destroy()
destroy
in interface javax.servlet.Filter
destroy
in interface javax.servlet.Servlet
destroy
in class javax.servlet.GenericServlet
public void init() throws javax.servlet.ServletException
init
in class javax.servlet.GenericServlet
javax.servlet.ServletException
@Deprecated protected ResourceConfig getDefaultResourceConfig(java.util.Map<java.lang.String,java.lang.Object> props, javax.servlet.ServletConfig servletConfig) throws javax.servlet.ServletException
getDefaultResourceConfig(java.util.Map, com.sun.jersey.spi.container.servlet.WebConfig)
.
This implementation returns an instance of WebAppResourceConfig
that scans in files and directories as declared by the
ClasspathResourceConfig.PROPERTY_CLASSPATH
if present, otherwise
in the "WEB-INF/lib" and "WEB-INF/classes" directories.
An inheriting class may override this method to supply a different default resource configuration implementation.
props
- the properties to pass to the resource configuration.servletConfig
- the servlet configuration.javax.servlet.ServletException
- in case there was an error while retrieving the default resource configprotected void configure(javax.servlet.ServletConfig sc, ResourceConfig rc, WebApplication wa)
ResourceConfig
for a Servlet.
The ResourceConfig
is configured such that the following classes
may be injected onto the field of a root resource class or a parameter
of a method of root resource class that is annotated with
Context
: ServletConfig
.
An inheriting class may override this method to configure the
ResourceConfig
to provide alternative or additional instances
that are resource or provider classes or instances, and may modify the
features and properties of the ResourceConfig
. For an inheriting
class to extend configuration behaviour the overriding method MUST call
super.configure(servletConfig, rc, wa)
as the first statement of that
method.
This method will be called only once at servlet initiation. Subsequent reloads of the Web application will not result in subsequence calls to this method.
sc
- the Servlet configurationrc
- the Resource configurationwa
- the Web applicationpublic void service(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException
service(java.net.URI, java.net.URI, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
method.service
in class javax.servlet.http.HttpServlet
request
- the HttpServletRequest
object that
contains the request the client made to
the servlet.response
- the HttpServletResponse
object that
contains the response the servlet returns
to the client.java.io.IOException
- if an input or output error occurs
while the servlet is handling the
HTTP request.javax.servlet.ServletException
- if the HTTP request cannot
be handled.public void init(javax.servlet.FilterConfig filterConfig) throws javax.servlet.ServletException
init
in interface javax.servlet.Filter
javax.servlet.ServletException
public java.util.regex.Pattern getStaticContentPattern()
Pattern
compiled from a regular expression that is
the property value of PROPERTY_WEB_PAGE_CONTENT_REGEX
.
A null
value will be returned if the property is not present
is or an empty String.protected void configure(javax.servlet.FilterConfig fc, ResourceConfig rc, WebApplication wa)
ResourceConfig
for a Filter.
The ResourceConfig
is configured such that the following classes
may be injected onto the field of a root resource class or a parameter
of a method of root resource class that is annotated with
Context
: FilterConfig
.
An inheriting class may override this method to configure the
ResourceConfig
to provide alternative or additional instances
that are resource or provider classes or instances, and may modify the
features and properties of the ResourceConfig
. For an inheriting
class to extend configuration behaviour the overriding method MUST call
super.configure(servletConfig, rc, wa)
as the first statement of that
method.
This method will be called only once at servlet initiation. Subsequent reloads of the Web application will not result in subsequence calls to this method.
fc
- the Filter configurationrc
- the Resource configurationwa
- the Web applicationpublic void doFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain chain) throws java.io.IOException, javax.servlet.ServletException
doFilter(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, javax.servlet.FilterChain)
method.doFilter
in interface javax.servlet.Filter
request
- the HttpServletRequest
object that
contains the request the client made to
the servlet.response
- the HttpServletResponse
object that
contains the response the servlet returns
to the client.chain
- the chain of filters from which the next filter can be invoked.java.io.IOException
javax.servlet.ServletException
public void doFilter(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, javax.servlet.FilterChain chain) throws java.io.IOException, javax.servlet.ServletException
service(java.net.URI, java.net.URI, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
method.
If the servlet path matches the regular expression declared by the
property PROPERTY_WEB_PAGE_CONTENT_REGEX
then the request
is forwarded to the next filter in the filter chain so that the
underlying servlet engine can process the request otherwise Jersey
will process the request.
request
- the HttpServletRequest
object that
contains the request the client made to
the servlet.response
- the HttpServletResponse
object that
contains the response the servlet returns
to the client.chain
- the chain of filters from which the next filter can be invoked.java.io.IOException
javax.servlet.ServletException
Copyright © 2016 Oracle Corporation. All Rights Reserved.