org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher Maven / Gradle / Ivy
package org.jboss.resteasy.plugins.server.servlet;
import org.jboss.resteasy.core.Dispatcher;
import org.jboss.resteasy.core.SynchronousDispatcher;
import org.jboss.resteasy.core.ThreadLocalResteasyProviderFactory;
import org.jboss.resteasy.resteasy_jaxrs.i18n.LogMessages;
import org.jboss.resteasy.resteasy_jaxrs.i18n.Messages;
import org.jboss.resteasy.specimpl.ResteasyHttpHeaders;
import org.jboss.resteasy.spi.HttpRequest;
import org.jboss.resteasy.spi.HttpResponse;
import org.jboss.resteasy.spi.Registry;
import org.jboss.resteasy.spi.ResteasyDeployment;
import org.jboss.resteasy.spi.ResteasyProviderFactory;
import org.jboss.resteasy.spi.ResteasyUriInfo;
import org.jboss.resteasy.util.GetRestful;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.SecurityContext;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
/**
* Helper/delegate class to unify Servlet and Filter dispatcher implementations
*
* @author Bill Burke
* @version $Revision: 1 $
*/
public class ServletContainerDispatcher
{
protected Dispatcher dispatcher;
protected ResteasyProviderFactory providerFactory;
private String servletMappingPrefix = "";
protected ResteasyDeployment deployment = null;
protected HttpRequestFactory requestFactory;
protected HttpResponseFactory responseFactory;
public Dispatcher getDispatcher()
{
return dispatcher;
}
@SuppressWarnings(value = "unchecked")
public void init(ServletContext servletContext, ConfigurationBootstrap bootstrap, HttpRequestFactory requestFactory, HttpResponseFactory responseFactory) throws ServletException
{
this.requestFactory = requestFactory;
this.responseFactory = responseFactory;
ResteasyDeployment ctxDeployment = (ResteasyDeployment) servletContext.getAttribute(ResteasyDeployment.class.getName());
ResteasyProviderFactory globalFactory = (ResteasyProviderFactory) servletContext.getAttribute(ResteasyProviderFactory.class.getName());
if (globalFactory == null && ctxDeployment != null) {
globalFactory = ctxDeployment.getProviderFactory();
}
Dispatcher globalDispatcher = (Dispatcher) servletContext.getAttribute(Dispatcher.class.getName());
if (globalDispatcher == null && ctxDeployment != null) {
globalDispatcher = ctxDeployment.getDispatcher();
}
String application = bootstrap.getInitParameter("javax.ws.rs.Application");
String useGlobalStr = bootstrap.getInitParameter("resteasy.servlet.context.deployment");
boolean useGlobal = globalFactory != null;
if (useGlobalStr != null) useGlobal = Boolean.parseBoolean(useGlobalStr);
// use global is backward compatible with 2.3.x and earlier and will store and/or use the dispatcher and provider factory
// in the servlet context
if (useGlobal)
{
providerFactory = globalFactory;
dispatcher = globalDispatcher;
if ((providerFactory != null && dispatcher == null) || (providerFactory == null && dispatcher != null))
{
throw new ServletException(Messages.MESSAGES.unknownStateListener());
}
// We haven't been initialized by an external entity so bootstrap ourselves
if (providerFactory == null)
{
deployment = bootstrap.createDeployment();
deployment.start();
servletContext.setAttribute(ResteasyProviderFactory.class.getName(), deployment.getProviderFactory());
servletContext.setAttribute(Dispatcher.class.getName(), deployment.getDispatcher());
servletContext.setAttribute(Registry.class.getName(), deployment.getRegistry());
dispatcher = deployment.getDispatcher();
providerFactory = deployment.getProviderFactory();
}
else
{
// ResteasyBootstrap inited us. Check to see if the servlet defines an Application class
if (application != null)
{
try
{
Map contextDataMap = ResteasyProviderFactory.getContextDataMap();
contextDataMap.putAll(dispatcher.getDefaultContextObjects());
Application app = ResteasyDeployment.createApplication(application.trim(), dispatcher, providerFactory);
// push context data so we can inject it
processApplication(app);
}
finally
{
ResteasyProviderFactory.removeContextDataLevel();
}
}
}
servletMappingPrefix = bootstrap.getParameter(ResteasyContextParameters.RESTEASY_SERVLET_MAPPING_PREFIX);
if (servletMappingPrefix == null) servletMappingPrefix = "";
servletMappingPrefix = servletMappingPrefix.trim();
}
else
{
deployment = bootstrap.createDeployment();
deployment.start();
dispatcher = deployment.getDispatcher();
providerFactory = deployment.getProviderFactory();
servletMappingPrefix = bootstrap.getParameter(ResteasyContextParameters.RESTEASY_SERVLET_MAPPING_PREFIX);
if (servletMappingPrefix == null) servletMappingPrefix = "";
servletMappingPrefix = servletMappingPrefix.trim();
}
}
public void destroy()
{
if (deployment != null)
{
deployment.stop();
}
}
protected void processApplication(Application config)
{
LogMessages.LOGGER.deployingApplication(Application.class.getName(), config.getClass());
ArrayList actualResourceClasses = new ArrayList();
ArrayList actualProviderClasses = new ArrayList();
ArrayList