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-2013 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;
 
 
 
An abstract Web component that may be extended a Servlet and/or Filter implementation, or encapsulated by a Servlet or Filter implementation.

Author(s):
Paul Sandoz (paul.sandoz at oracle.com)
public class WebComponent implements ContainerListener {
    
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";
    private static final Logger LOGGER =
            Logger.getLogger(WebComponent.class.getName());
            new ThreadLocalInvoker<HttpServletRequest>();
            new ThreadLocalInvoker<HttpServletResponse>();
    private WebConfig config;
    private boolean useSetStatusOn404 = false;
    private WebApplication application;
    public WebComponent() {
    }
    public WebComponent(Application app) {
        if (app == null)
            throw new IllegalArgumentException();
        if (app instanceof ResourceConfig) {
             = (ResourceConfigapp;
        } else {
             = new ApplicationAdapter(app);
        }
    }

    
Get the Web configuration.

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

    
Get the resource configuration.

Returns:
the resource configuration.
    public ResourceConfig getResourceConfig() {
        return ;
    }

    
Initiate the Web component.

Parameters:
webConfig the Web configuration.
Throws:
javax.servlet.ServletException in case of any initialization error
    public void init(WebConfig webConfigthrows ServletException {
         = webConfig;
        if ( == null)
             = createResourceConfig();
        if (.getConfigType() == .. &&
             = true;
        }
        load();
        Object o = .getProperties().get(
                .);
        if (o instanceof List) {
            List list = (Listo;
            for (Object elem : list) {
                if (elem instanceof ContainerNotifier) {
                    ContainerNotifier crf = (ContainerNotifierelem;
                    crf.addListener(this);
                }
            }
        } else if (o instanceof ContainerNotifier) {
            ContainerNotifier crf = (ContainerNotifiero;
            crf.addListener(this);
        }
    }

    
Destroy this Web component.

This will destroy the Web application created by this this Web component.

    public void destroy() {
        if ( != null)
            .destroy();
    }
    private final static class Writer extends OutputStream implements ContainerResponseWriter {
        final HttpServletResponse response;
        final boolean useSetStatusOn404;
        long contentLength;
        OutputStream out;
        boolean statusAndHeadersWritten = false;
        Writer(boolean useSetStatusOn404HttpServletResponse response) {
            this. = useSetStatusOn404;
            this. = response;
        }
        public OutputStream writeStatusAndHeaders(long contentLength,
                                                  ContainerResponse cResponsethrows IOException {
            this. = contentLength;
            this. = cResponse;
            this. = false;
            return this;
        }
        public void finish() throws IOException {
            if ()
                return;
            // Note that the writing of headers MUST be performed before
            // the invocation of sendError as on some Servlet implementations
            // modification of the response headers will have no effect
            // after the invocation of sendError.
            writeHeaders();
            if (.getStatus() >= 400) {
                if ( && .getStatus() == 404) {
                    .setStatus(.getStatus());
                } else {
                    final String reason = .getStatusType().getReasonPhrase();
                    if (reason == null || reason.isEmpty()) {
                        .sendError(.getStatus());
                    } else {
                        .sendError(.getStatus(), reason);
                    }
                }
            } else {
                .setStatus(.getStatus());
            }
        }
        public void write(int bthrows IOException {
            initiate();
            .write(b);
        }
        @Override
        public void write(byte b[]) throws IOException {
            if (b.length > 0) {
                initiate();
                .write(b);
            }
        }
        @Override
        public void write(byte b[], int offint lenthrows IOException {
            if (len > 0) {
                initiate();
                .write(bofflen);
            }
        }
        @Override
        public void flush() throws IOException {
            writeStatusAndHeaders();
            if ( != null)
                .flush();
        }
        @Override
        public void close() throws IOException {
            initiate();
            .close();
        }
        void initiate() throws IOException {
            if ( == null) {
                writeStatusAndHeaders();
                 = .getOutputStream();
            }
        }
        void writeStatusAndHeaders() {
            if ()
                return;
            writeHeaders();
            .setStatus(.getStatus());
             = true;
        }
        void writeHeaders() {
            if ( != -1 &&  < .)
                .setContentLength((int);
            MultivaluedMap<StringObjectheaders = .getHttpHeaders();
            for (Map.Entry<StringList<Object>> e : headers.entrySet()) {
                for (Object v : e.getValue()) {
                    .addHeader(e.getKey(), ContainerResponse.getHeaderValue(v));
                }
            }
        }
    }

    
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 requestUri,
                       final HttpServletRequest request,
                       final HttpServletResponse response)
            throws ServletExceptionIOException {
        // Copy the application field to local instance to ensure that the
        // currently loaded web application is used to process
        // request
        final WebApplication _application = ;
        final ContainerRequest cRequest = createRequest(
                _application,
                request,
                baseUri,
                requestUri);
        cRequest.setSecurityContext(new SecurityContext() {
            public Principal getUserPrincipal() {
                return request.getUserPrincipal();
            }
            public boolean isUserInRole(String role) {
                return request.isUserInRole(role);
            }
            public boolean isSecure() {
                return request.isSecure();
            }
            public String getAuthenticationScheme() {
                return request.getAuthType();
            }
        });
        // Check if any servlet filters have consumed a request entity
        // of the media type application/x-www-form-urlencoded
        // This can happen if a filter calls request.getParameter(...)
        filterFormParameters(requestcRequest);
        try {
            UriRuleProbeProvider.requestStart(requestUri);
            .set(request);
            .set(response);
            final Writer w = new Writer(response);
            _application.handleRequest(cRequestw);
            return w.cResponse.getStatus();
        } catch (MappableContainerException ex) {
            traceOnException(cRequestresponse);
            throw new ServletException(ex.getCause());
        } catch (ContainerException ex) {
            traceOnException(cRequestresponse);
            throw new ServletException(ex);
        } catch (RuntimeException ex) {
            traceOnException(cRequestresponse);
            throw ex;
        } finally {
            UriRuleProbeProvider.requestEnd();
            .set(null);
            .set(null);
        }
    }

    
Extension point for creating your custom container request.

Parameters:
app the web app
request the current servlet api request
baseUri the base uri
requestUri the request uri
Returns:
the request container
Throws:
java.io.IOException if any error occurs when getting the input stream
                                             HttpServletRequest requestURI baseUriURI requestUrithrows IOException {
        return new ContainerRequest(
                app,
                request.getMethod(),
                baseUri,
                requestUri,
                getHeaders(request),
                request.getInputStream());
    }
    private void traceOnException(final ContainerRequest cRequestfinal HttpServletResponse response) {
        if (cRequest.isTracingEnabled()) {
            final TraceInformation ti = (TraceInformationcRequest.getProperties().
                    get(TraceInformation.class.getName());
            ti.addTraceHeaders(new TraceInformation.TraceHeaderListener() {
                public void onHeader(String nameString value) {
                    response.addHeader(namevalue);
                }
            });
        }
    }

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

    
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);
        }
    }

    
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) {
        configureJndiResources(rc);
                HttpServletRequest.class,
                (HttpServletRequest) Proxy.newProxyInstance(
                        this.getClass().getClassLoader(),
                        new Class[]{HttpServletRequest.class},
                        )));
                HttpServletResponse.class,
                (HttpServletResponse) Proxy.newProxyInstance(
                        this.getClass().getClassLoader(),
                        new Class[]{HttpServletResponse.class},
                        )));
        GenericEntity<ThreadLocal<HttpServletRequest>> requestThreadLocal =
                };
                requestThreadLocal.getType(), requestThreadLocal.getEntity()));
        GenericEntity<ThreadLocal<HttpServletResponse>> responseThreadLocal =
                };
                responseThreadLocal.getType(), responseThreadLocal.getEntity()));
                ServletContext.class,
                wc.getServletContext()));
                WebConfig.class,
                wc));
        rc.getClasses().add(JSPTemplateProcessor.class);
        // TODO
        // If CDI is enabled should EJB injection be disabled?
        EJBComponentProviderFactoryInitilizer.initialize(rc);
        CDIComponentProviderFactoryInitializer.initialize(wcrcwa);
        // TODO
        // If CDI is enabled then no need to initialize managed beans
        ManagedBeanComponentProviderFactoryInitilizer.initialize(rc);
        GlassFishMonitoringInitializer.initialize();
    }

    
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() {
        WebApplication _application = create();
        configure(_application);
        initiate(_application);
         = _application;
    }

    
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 of any issues with providing \ the default resource configuration
                                                      WebConfig wcthrows ServletException {
        return getWebAppResourceConfig(propswc);
    }
    // ContainerListener

    
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 onReload() {
        WebApplication oldApplication = ;
        WebApplication newApplication = create();
        initiate(newApplication);
         = newApplication;
        if ( instanceof ReloadListener)
            ((ReloadListener).onReload();
        oldApplication.destroy();
    }
    //
    /* package */ ResourceConfig getWebAppResourceConfig(Map<StringObjectprops,
                                                         WebConfig webConfigthrows ServletException {
        // Default to using Web app resource config
        return new WebAppResourceConfig(propswebConfig.getServletContext());
    }
    private ResourceConfig createResourceConfig(WebConfig webConfig)
            throws ServletException {
        final Map<StringObjectprops = getInitParams(webConfig);
        final ResourceConfig rc = createResourceConfig(webConfigprops);
        rc.setPropertiesAndFeatures(props);
        return rc;
    }
    private ResourceConfig createResourceConfig(WebConfig webConfigMap<StringObjectprops)
            throws ServletException {
        // Check if the resource config class property is present
        String resourceConfigClassName = webConfig.getInitParameter();
        // Otherwise check if the JAX-RS application config class property is
        // present
        if (resourceConfigClassName == null)
            resourceConfigClassName = webConfig.getInitParameter();
        // If no resource config class property is present
        if (resourceConfigClassName == null) {
            // If the packages property is present then
            // use the packages resource config
            String packages = webConfig.getInitParameter(
                    .);
            if (packages != null) {
                props.put(.packages);
                return new PackagesResourceConfig(props);
            }
            ResourceConfig defaultConfig = webConfig.getDefaultResourceConfig(props);
            if (defaultConfig != null)
                return defaultConfig;
            return getDefaultResourceConfig(propswebConfig);
        }
        try {
            Class<?> resourceConfigClass = ReflectionHelper.
                    classForNameWithException(resourceConfigClassName);
            // TODO add support for WebAppResourceConfig
            if (resourceConfigClass == ClasspathResourceConfig.class) {
                String[] paths = getPaths(webConfig.getInitParameter(
                        .));
                props.put(.paths);
                return new ClasspathResourceConfig(props);
            } else if (ResourceConfig.class.isAssignableFrom(resourceConfigClass)) {
                try {
                    Constructor constructor = resourceConfigClass.getConstructor(Map.class);
                    if (ClasspathResourceConfig.class.isAssignableFrom(resourceConfigClass)) {
                        String[] paths = getPaths(webConfig.getInitParameter(
                                .));
                        props.put(.paths);
                    }
                    return (ResourceConfigconstructor.newInstance(props);
                } catch (NoSuchMethodException ex) {
                    // Pass through and try the default constructor
                } catch (Exception e) {
                    throw new ServletException(e);
                }
                return new DeferredResourceConfig(resourceConfigClass.asSubclass(ResourceConfig.class));
            } else if (Application.class.isAssignableFrom(resourceConfigClass)) {
                return new DeferredResourceConfig(resourceConfigClass.asSubclass(Application.class));
            } else {
                String message = "Resource configuration class, " + resourceConfigClassName +
                        ", is not a super class of " + Application.class;
                throw new ServletException(message);
            }
        } catch (ClassNotFoundException e) {
            String message = "Resource configuration class, " + resourceConfigClassName +
                    ", could not be loaded";
            throw new ServletException(messagee);
        }
    }
    private Map<StringObjectgetInitParams(WebConfig webConfig) {
        Map<StringObjectprops = new HashMap<StringObject>();
        Enumeration names = webConfig.getInitParameterNames();
        while (names.hasMoreElements()) {
            String name = (Stringnames.nextElement();
            props.put(namewebConfig.getInitParameter(name));
        }
        return props;
    }
    private String[] getPaths(String classpaththrows ServletException {
        final ServletContext context = .getServletContext();
        if (classpath == null) {
            String[] paths = {
                    context.getRealPath("/WEB-INF/lib"),
                    context.getRealPath("/WEB-INF/classes")
            };
            if (paths[0] == null && paths[1] == null) {
                String message = "The default deployment configuration that scans for " +
                        "classes in /WEB-INF/lib and /WEB-INF/classes is not supported " +
                        "for the application server." +
                        "Try using the package scanning configuration, see the JavaDoc for " +
                        PackagesResourceConfig.class.getName() + " and the property " +
                        . + ".";
                throw new ServletException(message);
            }
            return paths;
        } else {
            String[] virtualPaths = classpath.split(";");
            List<StringresourcePaths = new ArrayList<String>();
            for (String virtualPath : virtualPaths) {
                virtualPath = virtualPath.trim();
                if (virtualPath.length() == 0) continue;
                String path = context.getRealPath(virtualPath);
                if (path != nullresourcePaths.add(path);
            }
            if (resourcePaths.isEmpty()) {
                String message = "None of the declared classpath locations, " +
                        classpath +
                        ", could be resolved. " +
                        "This could be because the default deployment configuration that scans for " +
                        "classes in classpath locations is not supported. " +
                        "Try using the package scanning configuration, see the JavaDoc for " +
                        PackagesResourceConfig.class.getName() + " and the property " +
                        . + ".";
                throw new ServletException(message);
            }
            return resourcePaths.toArray(new String[resourcePaths.size()]);
        }
    }
    private void configureJndiResources(ResourceConfig rc) {
        // Obtain any instances that are registered in JNDI
        // Assumes such instances are singletons
        // Registered classes have to be interfaces
        javax.naming.Context x = InitialContextHelper.getInitialContext();
        if (x != null) {
            Iterator<Class<?>> i = rc.getClasses().iterator();
            while (i.hasNext()) {
                Class<?> c = i.next();
                if (!c.isInterface()) continue;
                try {
                    Object o = x.lookup(c.getName());
                    if (o != null) {
                        i.remove();
                        rc.getSingletons().add(o);
                        .log(.,
                                "An instance of the class " + c.getName() +
                                        " is found by JNDI look up using the class name as the JNDI name. " +
                                        "The instance will be registered as a singleton.");
                    }
                } catch (NamingException ex) {
                    .log(.,
                            "JNDI lookup failed for Jersey application resource " + c.getName(),
                            ex);
                }
            }
        }
    }
    private void filterFormParameters(HttpServletRequest hsrContainerRequest crthrows IOException {
                && !isEntityPresent(cr)) {
            Form f = new Form();
            Enumeration e = hsr.getParameterNames();
            while (e.hasMoreElements()) {
                String name = (Stringe.nextElement();
                String[] values = hsr.getParameterValues(name);
                f.put(name, Arrays.asList(values));
            }
            if (!f.isEmpty()) {
                cr.getProperties().put(.f);
                if (.isLoggable(.)) {
                    .log(.,
                            "A servlet request, to the URI " + cr.getRequestUri() + ", " +
                                    "contains form parameters in " +
                                    "the request body but the request body has been consumed " +
                                    "by the servlet or a servlet filter accessing the request " +
                                    "parameters. Only resource methods using @FormParam " +
                                    "will work as expected. Resource methods consuming the " +
                                    "request body by other means will not work as expected.");
                }
            }
        }
    }
    private boolean isEntityPresent(ContainerRequest crthrows IOException {
        InputStream in = cr.getEntityInputStream();
        if (!in.markSupported()) {
            in = new BufferedInputStream(in.);
            cr.setEntityInputStream(in);
        }
        in.mark(1);
        if (in.read() == -1)
            return false;
        else {
            in.reset();
            return true;
        }
    }
    private InBoundHeaders getHeaders(HttpServletRequest request) {
        InBoundHeaders rh = new InBoundHeaders();
        for (Enumeration<Stringnames = request.getHeaderNames(); names.hasMoreElements();) {
            String name = names.nextElement();
            List<StringvalueList = new LinkedList<String>();
            for (Enumeration<Stringvalues = request.getHeaders(name); values.hasMoreElements();) {
                valueList.add(values.nextElement());
            }
            rh.put(namevalueList);
        }
        return rh;
    }
New to GrepCode? Check out our FAQ X