Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
   *
   * Copyright (c) 2010-2012 Oracle and/or its affiliates. All rights reserved.
   *
   * The contents of this file are subject to the terms of either the GNU
   * General Public License Version 2 only ("GPL") or the Common Development
   * and Distribution License("CDDL") (collectively, the "License").  You
   * may not use this file except in compliance with the License.  You can
  * obtain a copy of the License at
  * http://glassfish.java.net/public/CDDL+GPL_1_1.html
  * or packager/legal/LICENSE.txt.  See the License for the specific
  * language governing permissions and limitations under the License.
  *
  * When distributing the software, include this License Header Notice in each
  * file and include the License file at packager/legal/LICENSE.txt.
  *
  * GPL Classpath Exception:
  * Oracle designates this particular file as subject to the "Classpath"
  * exception as provided by Oracle in the GPL Version 2 section of the License
  * file that accompanied this code.
  *
  * Modifications:
  * If applicable, add the following below the License Header, with the fields
  * enclosed by brackets [] replaced by your own identifying information:
  * "Portions Copyright [year] [name of copyright owner]"
  *
  * Contributor(s):
  * If you wish your version of this file to be governed by only the CDDL or
  * only the GPL Version 2, indicate your decision by adding "[Contributor]
  * elects to include this software in this distribution under the [CDDL or GPL
  * Version 2] license."  If you don't indicate a single choice of license, a
  * recipient has the option to distribute your version of this file under
  * either the CDDL, the GPL Version 2 or to extend the choice of license to
  * its licensees as provided above.  However, if you add GPL Version 2 code
  * and therefore, elected the GPL Version 2 license, then the option applies
  * only if the new code is made subject to such option by the copyright
  * holder.
  */
 
 package com.sun.jersey.spi.container.servlet;
 
 
 import java.net.URI;
 import java.util.List;
 import java.util.Map;
A javax.servlet.Servlet or javax.servlet.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 com.sun.jersey.api.core.ResourceConfig or javax.ws.rs.core.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 com.sun.jersey.api.core.PackagesResourceConfig.PROPERTY_PACKAGES) a new instance of com.sun.jersey.api.core.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.api.core.PackagesResourceConfig.PROPERTY_PACKAGES. Any additional initialization parameters are then added to the Map instance. Then that Map instance is passed to the constructor of com.sun.jersey.api.core.PackagesResourceConfig.

If none of the above resource configuration related initialization parameters are present a new instance of com.sun.jersey.api.core.servlet.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.api.core.ClasspathResourceConfig.PROPERTY_CLASSPATH. Any additional initialization parameters are then added to the Map instance. Then that Map instance is passed to the constructor of com.sun.jersey.api.core.servlet.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 com.sun.jersey.api.core.ResourceConfig.

A new com.sun.jersey.spi.container.WebApplication instance will be created and configured such that the following classes may be injected onto a root resource, provider and javax.ws.rs.core.Application classes using javax.ws.rs.core.Context: javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, javax.servlet.ServletContext, javax.servlet.ServletConfig and WebConfig. If this class is used as a Servlet then the javax.servlet.ServletConfig class may be injected. If this class is used as a Filter then the javax.servlet.FilterConfig class may be injected. WebConfig may be injected to abstract servlet or filter deployment.

A com.sun.jersey.core.spi.component.ioc.IoCComponentProviderFactory instance may be registered by extending this class and overriding the method initiate(com.sun.jersey.api.core.ResourceConfig,com.sun.jersey.spi.container.WebApplication) to initiate the com.sun.jersey.spi.container.WebApplication with the com.sun.jersey.core.spi.component.ioc.IoCComponentProviderFactory instance.

public class ServletContainer extends HttpServlet implements Filter {
    
The servlet initialization property whose boolean value determines if GlassFish default error pages will be returned or not.

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.

Since:
1.5
    public static final String GLASSFISH_DEFAULT_ERROR_PAGE_RESPONSE = "org.glassfish.web.isDefaultErrorPageEnabled";

    
The servlet initialization property whose value is a fully qualified class name of a class that implements com.sun.jersey.api.core.ResourceConfig or javax.ws.rs.core.Application.
    public static final String APPLICATION_CONFIG_CLASS =
            "javax.ws.rs.Application";

    
The servlet initialization property whose value is a fully qualified class name of a class that implements com.sun.jersey.api.core.ResourceConfig or javax.ws.rs.core.Application.
    public static final String RESOURCE_CONFIG_CLASS =
            "com.sun.jersey.config.property.resourceConfigClass";

    
The base path in the Web Pages where JSP templates, associated with viewables of resource classes, are located.

If this property is not set then the base path will be the root path of the Web Pages.

    public static final String JSP_TEMPLATES_BASE_PATH =
            "com.sun.jersey.config.property.JSPTemplatesBasePath";

    
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.

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 javax.servlet.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 String PROPERTY_WEB_PAGE_CONTENT_REGEX
            = "com.sun.jersey.config.property.WebPageContentRegex";

    
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. This enables another filter or the underlying servlet engine to process the request. Before the request is forwarded the response status is set to 200.

This property is only applicable when this class is used as a javax.servlet.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 String FEATURE_FILTER_FORWARD_ON_404
            = "com.sun.jersey.config.feature.FilterForwardOn404";

    
The 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 javax.servlet.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 String PROPERTY_FILTER_CONTEXT_PATH
            = "com.sun.jersey.config.feature.FilterContextPath";

    
A helper class for creating an injectable provider that supports javax.ws.rs.core.Context with a type and constant value.

Parameters:
<T> the type of the constant value.
    protected static class ContextInjectableProvider<T> extends
            SingletonTypeInjectableProvider<Context, T> {

        
Create a new instance.

Parameters:
type the type of the constant value.
instance the constant value.
        protected ContextInjectableProvider(Type type, T instance) {
            super(typeinstance);
        }
    }
    private transient WebComponent webComponent;
    private transient FilterConfig filterConfig;
    private transient Pattern staticContentPattern;
    private transient boolean forwardOn404;
    private class InternalWebComponent extends WebComponent {
        InternalWebComponent() {
        }
        InternalWebComponent(Application app) {
            super(app);
        }
        @Override
        protected WebApplication create() {
            return ServletContainer.this.create();
        }
        @Override
        protected void configure(WebConfig wcResourceConfig rcWebApplication wa) {
            super.configure(wcrcwa);
            ServletContainer.this.configure(wcrcwa);
        }
        @Override
        protected void initiate(ResourceConfig rcWebApplication wa) {
            ServletContainer.this.initiate(rcwa);
        }
        @Override
        protected ResourceConfig getDefaultResourceConfig(Map<StringObjectprops,
                WebConfig wcthrows ServletException  {
            return ServletContainer.this.getDefaultResourceConfig(propswc);
        }
    }
    private transient final Application app;
    public ServletContainer() {
        this. = null;
    }
    public ServletContainer(Class<? extends ApplicationappClass) {
        this. = new DeferredResourceConfig(appClass);
    }
    public ServletContainer(Application app) {
        this. = app;
    }
    // GenericServlet

    
Get the servlet context for the servlet or filter, depending on how this class is registered.

It is recommended that the WebConfig be utilized, see the method , to obtain the servlet context and initialization parameters for a servlet or filter.

Returns:
the servlet context for the servlet or filter.
    @Override
    public ServletContext getServletContext() {
        if ( != null)
            return .getServletContext();
        return super.getServletContext();
    }

    
Initiate the Web component.

Parameters:
webConfig the Web configuration.
Throws:
javax.servlet.ServletException in case of an initialization failure
    protected void init(WebConfig webConfigthrows ServletException {
         = ( == null)
                ? new InternalWebComponent()
                : new InternalWebComponent();
        .init(webConfig);
    }

    
Get the Web configuration.

Returns:
the Web configuration.
    protected WebConfig getWebConfig() {
        return .getWebConfig();
    }

    
    protected WebApplication create() {
        return WebApplicationFactory.createWebApplication();
    }

    
Get the default resource configuration if one is not declared in the web.xml.

This implementation returns an instance of com.sun.jersey.api.core.servlet.WebAppResourceConfig that scans in files and directories as declared by the com.sun.jersey.api.core.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.

Parameters:
props the properties to pass to the resource configuration.
wc the web configuration.
Returns:
the default resource configuration.
Throws:
javax.servlet.ServletException in case there was an error while retrieving the default resource config
            WebConfig wcthrows ServletException  {
        return .getWebAppResourceConfig(propswc);
    }

    
Configure the com.sun.jersey.api.core.ResourceConfig.

The com.sun.jersey.api.core.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 javax.ws.rs.core.Context: javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse , javax.servlet.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 com.sun.jersey.api.core.ResourceConfig to provide alternative or additional instances that are resource or provider classes or instances, and may modify the features and properties of the com.sun.jersey.api.core.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.

Parameters:
wc the Web configuration
rc the Resource configuration
wa the Web application
    protected void configure(WebConfig wcResourceConfig rcWebApplication wa) {
        if (getServletConfig() != null)
            configure(getServletConfig(), rcwa);
        else if ( != null)
            configure(rcwa);
        if (rc instanceof ReloadListener) {
            List<ContainerNotifiernotifiers = new ArrayList<ContainerNotifier>();
            if (o instanceof ContainerNotifier)
                notifiers.add((ContainerNotifiero);
            else if (o instanceof List)
                for (Object elem : (Listo)
                    if (elem instanceof ContainerNotifier)
                        notifiers.add((ContainerNotifierelem);
            for (ContainerNotifier cn : ServiceFinder.find(ContainerNotifier.class)) {
                notifiers.add(cn);
            }
            rc.getProperties().put(.notifiers);
        }
    }

    
Initiate the com.sun.jersey.spi.container.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.

Parameters:
rc the Resource configuration
wa the Web application
    protected void initiate(ResourceConfig rcWebApplication wa) {
        wa.initiate(rc);
    }

    
Load the Web application. This will create, configure and initiate the web application.
    public void load() {
        .load();
    }

    
Reload the Web application. This will create and initiate the web application using the same com.sun.jersey.api.core.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 com.sun.jersey.api.core.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 void reload() {
        .onReload();
    }

    
Dispatch client requests to a resource class.

Parameters:
baseUri the base URI of the request.
requestUri the URI of the request.
request the javax.servlet.http.HttpServletRequest object that contains the request the client made to the Web component.
response the javax.servlet.http.HttpServletResponse object that contains the response the Web component returns to the client.
Returns:
the status code of the response.
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.
	public int service(URI baseUriURI requestUrifinal HttpServletRequest requestHttpServletResponse response)
            throws ServletExceptionIOException {
        return .service(baseUrirequestUrirequestresponse);
    }

    
Destroy this Servlet or Filter.
    @Override
    public void destroy() {
        if ( != null) {
            .destroy();
        }
    }
    // Servlet
    @Override
    public void init() throws ServletException {
        init(new WebServletConfig(this));
    }

    
Get the default resource configuration if one is not declared in the web.xml.

This implementation returns an instance of com.sun.jersey.api.core.servlet.WebAppResourceConfig that scans in files and directories as declared by the com.sun.jersey.api.core.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.

Deprecated:
methods should implement .
Parameters:
props the properties to pass to the resource configuration.
servletConfig the servlet configuration.
Returns:
the default resource configuration.
Throws:
javax.servlet.ServletException in case there was an error while retrieving the default resource config
    @Deprecated
            ServletConfig servletConfigthrows ServletException  {
        return getDefaultResourceConfig(propsgetWebConfig());
    }

    
Configure the com.sun.jersey.api.core.ResourceConfig for a Servlet.

The com.sun.jersey.api.core.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 javax.ws.rs.core.Context: javax.servlet.ServletConfig.

An inheriting class may override this method to configure the com.sun.jersey.api.core.ResourceConfig to provide alternative or additional instances that are resource or provider classes or instances, and may modify the features and properties of the com.sun.jersey.api.core.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.

Parameters:
sc the Servlet configuration
rc the Resource configuration
wa the Web application
    protected void configure(final ServletConfig scResourceConfig rcWebApplication wa) {
                ServletConfig.classsc));
    }

    
Dispatches client requests to the method.

Parameters:
request the javax.servlet.http.HttpServletRequest object that contains the request the client made to the servlet.
response the javax.servlet.http.HttpServletResponse 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 request.
javax.servlet.ServletException if the HTTP request cannot be handled.
    @Override
    public void service(HttpServletRequest requestHttpServletResponse responsethrows ServletExceptionIOException {
        
There is an annoying edge case where the service method is invoked for the case when the URI is equal to the deployment URL minus the '/', for example http://locahost:8080/HelloWorldWebApp
        final String servletPath = request.getServletPath();
        String pathInfo = request.getPathInfo();
        StringBuffer requestURL = request.getRequestURL();
        String requestURI = request.getRequestURI();
        final boolean checkPathInfo = pathInfo == null || pathInfo.isEmpty() || pathInfo.equals("/");

        
The HttpServletRequest.getRequestURL() contains the complete URI minus the query and fragment components.
        UriBuilder absoluteUriBuilder = null;
        try {
            absoluteUriBuilder = UriBuilder.fromUri(requestURL.toString());
        } catch (IllegalArgumentException iae) {
            final Response.Status badRequest = ..;
            response.sendError(badRequest.getStatusCode(), badRequest.getReasonPhrase());
            return;
        }
        if (checkPathInfo && !request.getRequestURI().endsWith("/")) {
            // Only do this if the last segment of the servlet path does not contain '.'
            // This handles the case when the extension mapping is used with the servlet
            // see issue 506
            // This solution does not require parsing the deployment descriptor,
            // however still leaves it broken for the very rare case if a standard path
            // servlet mapping would include dot in the last segment (e.g. /.webresources/*)
            // and somebody would want to hit the root resource without the trailing slash
            int i = servletPath.lastIndexOf("/");
            if (servletPath.substring(i + 1).indexOf('.') < 0) {
                if (.getResourceConfig().getFeature(.)) {
                    URI l = absoluteUriBuilder.
                            path("/").
                            replaceQuery(request.getQueryString()).build();
                    response.setStatus(307);
                    response.setHeader("Location"l.toASCIIString());
                    return;
                } else {
                    pathInfo = "/";
                    requestURL.append("/");
                    requestURI += "/";
                }
            }
        }

        
The HttpServletRequest.getPathInfo() and HttpServletRequest.getServletPath() are in decoded form. On some servlet implementations the getPathInfo() removed contiguous '/' characters. This is problematic if URIs are embedded, for example as the last path segment. We need to work around this and not use getPathInfo for the decodedPath.
        final String decodedBasePath = (pathInfo != null)
                ? request.getContextPath() + servletPath + "/"
                : request.getContextPath() + "/";
        final String encodedBasePath = UriComponent.encode(decodedBasePath,
                ..);
        if (!decodedBasePath.equals(encodedBasePath)) {
            throw new ContainerException("The servlet context path and/or the " +
                    "servlet path contain characters that are percent encoded");
        }
        final URI baseUri = absoluteUriBuilder.replacePath(encodedBasePath).
                build();
        String queryParameters = request.getQueryString();
        if (queryParameters == null) {
            queryParameters = "";
        }
        final URI requestUri = absoluteUriBuilder.replacePath(requestURI).
                replaceQuery(queryParameters).
                build();
        service(baseUrirequestUrirequestresponse);
    }
    // Filter
    private String filterContextPath = null;
    public void init(FilterConfig filterConfigthrows ServletException {
        this. = filterConfig;
        init(new WebFilterConfig(filterConfig));
    }

    

Returns:
the java.util.regex.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.
    public Pattern getStaticContentPattern() {
        return ;
    }

    
Configure the com.sun.jersey.api.core.ResourceConfig for a Filter.

The com.sun.jersey.api.core.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 javax.ws.rs.core.Context: javax.servlet.FilterConfig.

An inheriting class may override this method to configure the com.sun.jersey.api.core.ResourceConfig to provide alternative or additional instances that are resource or provider classes or instances, and may modify the features and properties of the com.sun.jersey.api.core.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.

Parameters:
fc the Filter configuration
rc the Resource configuration
wa the Web application
    protected void configure(final FilterConfig fcResourceConfig rcWebApplication wa) {
                FilterConfig.classfc));
        if (regex != null && regex.length() > 0) {
            try {
                 = Pattern.compile(regex);
            } catch (PatternSyntaxException ex) {
                throw new ContainerException(
                        "The syntax is invalid for the regular expression, " + regex +
                        ", associated with the initialization parameter " + ex);
            }
        }
        if ( != null) {
            if (.isEmpty()) {
                 = null;
            } else {
                if (!.startsWith("/")) {
                     = '/' + ;
                }
                if (.endsWith("/")) {
                     = .substring(0, .length() - 1);
                }
            }
        }
    }

    
Dispatches client requests to the method.

Parameters:
request the javax.servlet.http.HttpServletRequest object that contains the request the client made to the servlet.
response the javax.servlet.http.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.
Throws:
java.io.IOException
javax.servlet.ServletException
    public void doFilter(ServletRequest requestServletResponse responseFilterChain chainthrows IOExceptionServletException {
        try {
            doFilter((HttpServletRequest)request, (HttpServletResponse)responsechain);
        } catch (ClassCastException e) {
            throw new ServletException("non-HTTP request or response");
        }
    }

    
Dispatches client requests to the 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.

Parameters:
request the javax.servlet.http.HttpServletRequest object that contains the request the client made to the servlet.
response the javax.servlet.http.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.
Throws:
java.io.IOException
javax.servlet.ServletException
    public void doFilter(HttpServletRequest requestHttpServletResponse responseFilterChain chainthrows IOExceptionServletException {
        if (request.getAttribute("javax.servlet.include.request_uri") != null) {
            final String includeRequestURI = (String)request.getAttribute("javax.servlet.include.request_uri");
            if (!includeRequestURI.equals(request.getRequestURI())) {
                doFilter(requestresponsechain,
                        includeRequestURI,
                        (String)request.getAttribute("javax.servlet.include.servlet_path"),
                        (String)request.getAttribute("javax.servlet.include.query_string"));
                return;
            }
        }

        
JERSEY-880 - WAS interprets HttpServletRequest#getServletPath() and HttpServletRequest#getPathInfo() differently when accessing a static resource.
        final String servletPath = request.getServletPath()
                + (request.getPathInfo() == null ? "" : request.getPathInfo());
        doFilter(requestresponsechain,
                request.getRequestURI(),
                servletPath,
                request.getQueryString());
    }
    private void doFilter(HttpServletRequest requestHttpServletResponse responseFilterChain chain,
            String requestURIString servletPathString queryStringthrows IOExceptionServletException {
        // if we match the static content regular expression lets delegate to
        // the filter chain to use the default container servlets & handlers
        final Pattern p = getStaticContentPattern();
        if (p != null && p.matcher(servletPath).matches()) {
            chain.doFilter(requestresponse);
            return;
        }
        if ( != null) {
            if (!servletPath.startsWith()) {
                throw new ContainerException("The servlet path, \"" + servletPath +
                        "\", does not start with the filter context path, \"" +  + "\"");
            } else if (servletPath.length() == .length()) {
                // Path does not end in a slash, may need to redirect
                if (.getResourceConfig().getFeature(.)) {
                    URI l = UriBuilder.fromUri(request.getRequestURL().toString()).
                            path("/").
                            replaceQuery(queryString).build();
                    response.setStatus(307);
                    response.setHeader("Location"l.toASCIIString());
                    return;
                } else {
                    requestURI += "/";
                }
            }
        }
        final UriBuilder absoluteUriBuilder = UriBuilder.fromUri(
                request.getRequestURL().toString());
        final URI baseUri = ( == null)
                ? absoluteUriBuilder.replacePath(request.getContextPath()).
                        path("/").
                        build()
                : absoluteUriBuilder.replacePath(request.getContextPath()).
                        path().
                        path("/").
                        build();
        final URI requestUri = absoluteUriBuilder.replacePath(requestURI).
                replaceQuery(queryString).
                build();
        final int status = service(baseUrirequestUrirequestresponse);
        // If forwarding is configured and response is a 404 with no entity
        // body then call the next filter in the chain
        if ( && status == 404 && !response.isCommitted()) {
            // lets clear the response to OK before we forward to the next in the chain
            // as OK is the default set by servlet containers before filters/servlets do any wor
            // so lets hide our footsteps and pretend we were never in the chain at all and let the
            // next filter or servlet return the 404 if they can't find anything to return
            //
            // We could add an optional flag to disable this step if anyone can ever find a case where
            // this causes a problem, though I suspect any problems will really be with downstream
            // servlets not correctly setting an error status if they cannot find something to return
            response.setStatus(.);
            chain.doFilter(requestresponse);
        }
    }
New to GrepCode? Check out our FAQ X