Package org.glassfish.jersey.servlet
Class ServletContainer
- java.lang.Object
-
- javax.servlet.GenericServlet
-
- javax.servlet.http.HttpServlet
-
- org.glassfish.jersey.servlet.ServletContainer
-
- All Implemented Interfaces:
java.io.Serializable
,javax.servlet.Filter
,javax.servlet.Servlet
,javax.servlet.ServletConfig
,Container
public class ServletContainer extends javax.servlet.http.HttpServlet implements javax.servlet.Filter, Container
AServlet
orFilter
for deploying root resource classes. 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 "javax.ws.rs.Application" (seeServletProperties.JAXRS_APPLICATION_CLASS
) and whose value is a fully qualified name of a class that implementsApplication
. 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 "javax.ws.rs.Application" is not present and a initialization parameter "jersey.config.server.provider.packages" is present (seeServerProperties.PROVIDER_PACKAGES
) a new instance ofResourceConfig
with this configuration is created. The initialization parameter "jersey.config.server.provider.packages" MUST be set to provide one or more package names. Each package name MUST be separated by ';'. If none of the above resource configuration related initialization parameters are present a new instance ofResourceConfig
withWebAppResourcesScanner
is created. The initialization parameter "jersey.config.server.provider.classpath" is present (seeServerProperties.PROVIDER_CLASSPATH
) MAY be set to provide one or more resource paths. Each path MUST be separated by ';'. 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 createdResourceConfig
. A newApplicationHandler
instance will be created and configured such that the following classes may be injected onto a root resource, provider andApplication
classes using@Context
annotation:HttpServletRequest
,HttpServletResponse
,ServletContext
,ServletConfig
andWebConfig
. If this class is used as a Servlet then theServletConfig
class may be injected. If this class is used as a servlet filter then theFilterConfig
class may be injected.WebConfig
may be injected to abstract servlet or filter deployment. Persistence units that may be injected must be configured in web.xml in the normal way plus an additional servlet parameter to enable the Jersey servlet to locate them in JNDI. E.g. with the following persistence unit configuration:
the Jersey servlet requires an additional servlet parameter as follows:<persistence-unit-ref> <persistence-unit-ref-name>persistence/widget</persistence-unit-ref-name> <persistence-unit-name>WidgetPU</persistence-unit-name> </persistence-unit-ref>
Given the above, Jersey will inject the<init-param> <param-name>unit:WidgetPU</param-name> <param-value>persistence/widget</param-value> </init-param>
EntityManagerFactory
found atjava:comp/env/persistence/widget
in JNDI when encountering a field or parameter annotated with@PersistenceUnit(unitName="WidgetPU")
.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description private ContainerLifecycleListener
containerListener
private javax.servlet.FilterConfig
filterConfig
private java.lang.String
filterContextPath
private java.util.List<java.lang.String>
filterUrlMappings
private static ExtendedLogger
LOGGER
private ResourceConfig
resourceConfig
private static long
serialVersionUID
private java.util.regex.Pattern
staticContentPattern
private WebComponent
webComponent
-
Fields inherited from interface org.glassfish.jersey.server.spi.Container
DEFAULT_HTTP_PORT, DEFAULT_HTTPS_PORT
-
-
Constructor Summary
Constructors Constructor Description ServletContainer()
Create Jersey Servlet container.ServletContainer(ResourceConfig resourceConfig)
Create Jersey Servlet container.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
destroy()
void
doFilter(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, javax.servlet.FilterChain chain)
Dispatches client requests to theservice(URI, URI, HttpServletRequest, HttpServletResponse)
method.private void
doFilter(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, javax.servlet.FilterChain chain, java.lang.String requestURI, java.lang.String servletPath, java.lang.String queryString)
void
doFilter(javax.servlet.ServletRequest servletRequest, javax.servlet.ServletResponse servletResponse, javax.servlet.FilterChain filterChain)
ApplicationHandler
getApplicationHandler()
Get the Jersey server-side application handler associated with the container.ResourceConfig
getConfiguration()
Return an immutable representation of the currentconfiguration
.private FilterUrlMappingsProvider
getFilterUrlMappingsProvider()
Resolve theFilterUrlMappingsProvider
service via hk2.javax.servlet.ServletContext
getServletContext()
Get the servlet context for the servlet or filter, depending on how this class is registered.protected java.util.regex.Pattern
getStaticContentPattern()
Get the static content path pattern.WebComponent
getWebComponent()
GetWebComponent
used by this servlet container.void
init()
void
init(javax.servlet.FilterConfig filterConfig)
protected void
init(WebConfig webConfig)
Initiate the Web component.private java.lang.String
pickUrlMapping(java.lang.String requestUri, java.util.List<java.lang.String> filterUrlMappings)
Picks the most suitable url mapping (in case more than one is defined) based on the request URI.void
reload()
Reload the hosted Jersey application using the currentconfiguration
.void
reload(ResourceConfig configuration)
Reload the hosted Jersey application using a newconfiguration
.Value<java.lang.Integer>
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.protected void
service(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
Receives standard HTTP requests from the publicservice
method and dispatches them to thedo
XXX methods defined in this class.void
service(javax.servlet.ServletRequest req, javax.servlet.ServletResponse res)
Dispatches client requests to the protectedservice
method.private ResponseWriter
serviceImpl(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 and returnsResponseWriter
, Servlet'sContainerResponseWriter
implementation.private void
setResponseForInvalidUri(javax.servlet.http.HttpServletResponse response, java.lang.Throwable throwable)
-
Methods inherited from class javax.servlet.http.HttpServlet
doDelete, doGet, doHead, doOptions, doPost, doPut, doTrace, getLastModified
-
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
LOGGER
private static final ExtendedLogger LOGGER
-
filterConfig
private transient javax.servlet.FilterConfig filterConfig
-
webComponent
private transient WebComponent webComponent
-
resourceConfig
private transient ResourceConfig resourceConfig
-
staticContentPattern
private transient java.util.regex.Pattern staticContentPattern
-
filterContextPath
private transient java.lang.String filterContextPath
-
filterUrlMappings
private transient java.util.List<java.lang.String> filterUrlMappings
-
containerListener
private transient volatile ContainerLifecycleListener containerListener
-
-
Constructor Detail
-
ServletContainer
public ServletContainer()
Create Jersey Servlet container.
-
ServletContainer
public ServletContainer(ResourceConfig resourceConfig)
Create Jersey Servlet container.- Parameters:
resourceConfig
- container configuration.
-
-
Method Detail
-
init
protected void init(WebConfig webConfig) throws javax.servlet.ServletException
Initiate the Web component.- Parameters:
webConfig
- the Web configuration.- Throws:
javax.servlet.ServletException
- in case of an initialization failure
-
service
public void service(javax.servlet.ServletRequest req, javax.servlet.ServletResponse res) throws javax.servlet.ServletException, java.io.IOException
Dispatches client requests to the protectedservice
method. There's no need to override this method.- Specified by:
service
in interfacejavax.servlet.Servlet
- Overrides:
service
in classjavax.servlet.http.HttpServlet
- Parameters:
req
- theHttpServletRequest
object that contains the request the client made of the servletres
- theHttpServletResponse
object that contains the response the servlet returns to the client- Throws:
java.io.IOException
- if an input or output error occurs while the servlet is handling the HTTP requestjavax.servlet.ServletException
- if the HTTP request cannot be handled- See Also:
Servlet.service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
-
service
protected void service(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException
Receives standard HTTP requests from the publicservice
method and dispatches them to thedo
XXX methods defined in this class. This method is an HTTP-specific version of theServlet.service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
method. There's no need to override this method.- Overrides:
service
in classjavax.servlet.http.HttpServlet
- Parameters:
request
- theHttpServletRequest
object that contains the request the client made of the servletresponse
- theHttpServletResponse
object that contains the response the servlet returns to the client- Throws:
java.io.IOException
- if an input or output error occurs while the servlet is handling the HTTP requestjavax.servlet.ServletException
- if the HTTP request cannot be handled- See Also:
Servlet.service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
-
setResponseForInvalidUri
private void setResponseForInvalidUri(javax.servlet.http.HttpServletResponse response, java.lang.Throwable throwable) throws java.io.IOException
- Throws:
java.io.IOException
-
destroy
public void destroy()
- Specified by:
destroy
in interfacejavax.servlet.Filter
- Specified by:
destroy
in interfacejavax.servlet.Servlet
- Overrides:
destroy
in classjavax.servlet.GenericServlet
-
init
public void init() throws javax.servlet.ServletException
- Overrides:
init
in classjavax.servlet.GenericServlet
- Throws:
javax.servlet.ServletException
-
service
public Value<java.lang.Integer> 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
Dispatch client requests to a resource class.- Parameters:
baseUri
- the base URI of the request.requestUri
- the URI of the request.request
- theHttpServletRequest
object that contains the request the client made to the Web component.response
- theHttpServletResponse
object that contains the response the Web component returns to the client.- Returns:
- lazily initialized response status code
value provider
. If not resolved in the moment of call toValue.get()
,-1
is returned. - Throws:
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.
-
serviceImpl
private ResponseWriter serviceImpl(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
Dispatch client requests to a resource class and returnsResponseWriter
, Servlet'sContainerResponseWriter
implementation.- Parameters:
baseUri
- the base URI of the request.requestUri
- the URI of the request.request
- theHttpServletRequest
object that contains the request the client made to the Web component.response
- theHttpServletResponse
object that contains the response the Web component returns to the client.- Returns:
- returns
ResponseWriter
, Servlet'sContainerResponseWriter
implementation, into which processed request response was written to. - Throws:
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.
-
init
public void init(javax.servlet.FilterConfig filterConfig) throws javax.servlet.ServletException
- Specified by:
init
in interfacejavax.servlet.Filter
- Throws:
javax.servlet.ServletException
-
doFilter
public void doFilter(javax.servlet.ServletRequest servletRequest, javax.servlet.ServletResponse servletResponse, javax.servlet.FilterChain filterChain) throws java.io.IOException, javax.servlet.ServletException
- Specified by:
doFilter
in interfacejavax.servlet.Filter
- Throws:
java.io.IOException
javax.servlet.ServletException
-
getServletContext
public javax.servlet.ServletContext getServletContext()
Get the servlet context for the servlet or filter, depending on how this class is registered.- Specified by:
getServletContext
in interfacejavax.servlet.ServletConfig
- Overrides:
getServletContext
in classjavax.servlet.GenericServlet
- Returns:
- the servlet context for the servlet or filter.
-
doFilter
public void doFilter(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, javax.servlet.FilterChain chain) throws java.io.IOException, javax.servlet.ServletException
Dispatches client requests to theservice(URI, URI, HttpServletRequest, HttpServletResponse)
method. If the servlet path matches the regular expression declared by the propertyServletProperties.FILTER_STATIC_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.- Parameters:
request
- theHttpServletRequest
object that contains the request the client made to the servlet.response
- theHttpServletResponse
object that contains the response the servlet returns to the client.chain
- the chain of filters from which the next filter can be invoked.- Throws:
java.io.IOException
- in case of an I/O error.javax.servlet.ServletException
- in case of an error while executing the filter chain.
-
doFilter
private void doFilter(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, javax.servlet.FilterChain chain, java.lang.String requestURI, java.lang.String servletPath, java.lang.String queryString) throws java.io.IOException, javax.servlet.ServletException
- Throws:
java.io.IOException
javax.servlet.ServletException
-
pickUrlMapping
private java.lang.String pickUrlMapping(java.lang.String requestUri, java.util.List<java.lang.String> filterUrlMappings)
Picks the most suitable url mapping (in case more than one is defined) based on the request URI.- Parameters:
requestUri
- String representation of the request URIfilterUrlMappings
- set of configured filter url-patterns- Returns:
- the most suitable context path, or
null
if empty
-
getFilterUrlMappingsProvider
private FilterUrlMappingsProvider getFilterUrlMappingsProvider()
Resolve theFilterUrlMappingsProvider
service via hk2. Will only work in Servlet 3 container, as the older API version does not provide access to the filter mapping structure.- Returns:
FilterContextPath
instance, if available,null
otherwise.
-
getStaticContentPattern
protected java.util.regex.Pattern getStaticContentPattern()
Get the static content path pattern.- Returns:
- the
Pattern
compiled from a regular expression that is the property value ofServletProperties.FILTER_STATIC_CONTENT_REGEX
. Anull
value will be returned if the property is not set or is an empty String.
-
getConfiguration
public ResourceConfig getConfiguration()
Description copied from interface:Container
Return an immutable representation of the currentconfiguration
.- Specified by:
getConfiguration
in interfaceContainer
- Returns:
- current configuration of the hosted Jersey application.
-
reload
public void reload()
Description copied from interface:Container
Reload the hosted Jersey application using the currentconfiguration
.
-
reload
public void reload(ResourceConfig configuration)
Description copied from interface:Container
Reload the hosted Jersey application using a newconfiguration
.
-
getApplicationHandler
public ApplicationHandler getApplicationHandler()
Description copied from interface:Container
Get the Jersey server-side application handler associated with the container.- Specified by:
getApplicationHandler
in interfaceContainer
- Returns:
- Jersey server-side application handler associated with the container.
-
getWebComponent
public WebComponent getWebComponent()
GetWebComponent
used by this servlet container.- Returns:
- The web component.
-
-