Skip to content

Commit

Permalink
[RESTEASY-1287, JBEAP-2963] RESTEasy-Spring is unable to set mediaTyp…
Browse files Browse the repository at this point in the history
…eMappings in ResteasyDeployment by bean settings
  • Loading branch information
xstefank committed Aug 4, 2016
1 parent cfeb425 commit 1911ac0
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ public void contextInitialized(ServletContextEvent event)
deployment = config.createDeployment();
deployment.start();

servletContext.setAttribute(ResteasyProviderFactory.class.getName(), deployment.getProviderFactory());
servletContext.setAttribute(Dispatcher.class.getName(), deployment.getDispatcher());
servletContext.setAttribute(Registry.class.getName(), deployment.getRegistry());
servletContext.setAttribute(ResteasyDeployment.class.getName(), deployment);
}

public void contextDestroyed(ServletContextEvent event)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,34 @@ public void start()
}
}

public void merge(ResteasyDeployment other)
{
scannedResourceClasses.addAll(other.getScannedResourceClasses());
scannedProviderClasses.addAll(other.getScannedProviderClasses());
scannedJndiComponentResources.addAll(other.getScannedJndiComponentResources());

jndiComponentResources.addAll(other.getJndiComponentResources());
providerClasses.addAll(other.getProviderClasses());
actualProviderClasses.addAll(other.getActualProviderClasses());
providers.addAll(other.getProviders());

jndiResources.addAll(other.getJndiResources());
resourceClasses.addAll(other.getResourceClasses());
unwrappedExceptions.addAll(other.getUnwrappedExceptions());
actualResourceClasses.addAll(other.getActualResourceClasses());
resourceFactories.addAll(other.getResourceFactories());
resources.addAll(other.getResources());

mediaTypeMappings.putAll(other.getMediaTypeMappings());
languageExtensions.putAll(other.getLanguageExtensions());
interceptorPrecedences.addAll(other.getInterceptorPrecedences());
interceptorBeforePrecedences.putAll(other.getInterceptorBeforePrecedences());
interceptorAfterPrecedences.putAll(other.getInterceptorAfterPrecedences());

defaultContextObjects.putAll(other.getDefaultContextObjects());
constructedDefaultContextObjects.putAll(other.getConstructedDefaultContextObjects());
}

public static Application createApplication(String applicationClass, Dispatcher dispatcher, ResteasyProviderFactory providerFactory)
{
Class<?> clazz = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public class SpringBeanProcessor implements BeanFactoryPostProcessor, SmartAppli
protected Registry registry;
protected ResteasyProviderFactory providerFactory;
protected Dispatcher dispatcher;
protected ResteasyDeployment deployment;

protected Set<String> resourceFactoryNames;
protected Map<String, SpringResourceFactory> resourceFactories = new HashMap<String, SpringResourceFactory>();
Expand Down Expand Up @@ -92,12 +93,14 @@ public Object postProcessBeforeInitialization(Object bean, String beanName)
* <ol>
* <p/>
* <li>RESTEasy injection of singleton @Providers, as well as @Provider
* registration
* registration</li>
* <p/>
* <li>either singleton or request/prototype RESTeasy injection... but not
* registration. The RESTEasy registration happens in the
* onApplicationEvent() below, which happens at the end of the Spring
* life-cycle
* life-cycle</li>
* <p/>
* <li>merges the {@link ResteasyDeployment} bean with the user deployment</li>
* <p/>
* </ol>
*
Expand All @@ -122,7 +125,13 @@ else if(registrations.contains(beanName) && bean instanceof ResteasyRegistration
SpringResourceFactory resourceFactory = new SpringResourceFactory(registeredBeanName, beanFactory, beanClass);
resourceFactory.setContext(registration.getContext());
resourceFactories.put(registeredBeanName, resourceFactory);
}
}

else if (bean instanceof ResteasyDeployment) {
ResteasyDeployment beanDeployment = (ResteasyDeployment) bean;
deployment.merge(beanDeployment);
deployment.start();
}

else
{
Expand Down Expand Up @@ -178,6 +187,7 @@ private boolean isSingleton(String beanName)
public SpringBeanProcessor(ResteasyDeployment deployment)
{
this(deployment.getDispatcher(), deployment.getRegistry(), deployment.getProviderFactory());
this.deployment = deployment;
}

public SpringBeanProcessor(Dispatcher dispatcher)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.jboss.resteasy.core.Dispatcher;
import org.jboss.resteasy.plugins.spring.i18n.Messages;
import org.jboss.resteasy.spi.Registry;
import org.jboss.resteasy.spi.ResteasyDeployment;
import org.jboss.resteasy.spi.ResteasyProviderFactory;
import org.springframework.web.context.ConfigurableWebApplicationContext;

Expand Down Expand Up @@ -40,19 +41,14 @@ public class SpringContextLoaderSupport
{
public void customizeContext(ServletContext servletContext, ConfigurableWebApplicationContext configurableWebApplicationContext)
{
ResteasyProviderFactory providerFactory = (ResteasyProviderFactory) servletContext.getAttribute(ResteasyProviderFactory.class.getName());
if (providerFactory == null)
throw new RuntimeException(Messages.MESSAGES.providerFactoryIsNull());
SpringBeanProcessor processor = null;

Registry registry = (Registry) servletContext.getAttribute(Registry.class.getName());
if (registry == null)
throw new RuntimeException(Messages.MESSAGES.registryIsNull());
ResteasyDeployment deployment = (ResteasyDeployment) servletContext.getAttribute(ResteasyDeployment.class.getName());
if (deployment == null) {
throw new RuntimeException(Messages.MESSAGES.deploymentIsNull());
}

Dispatcher dispatcher = (Dispatcher) servletContext.getAttribute(Dispatcher.class.getName());
if (dispatcher == null)
throw new RuntimeException(Messages.MESSAGES.dispatcherIsNull());

SpringBeanProcessor processor = new SpringBeanProcessor(dispatcher, registry, providerFactory);
processor = new SpringBeanProcessor(deployment);
configurableWebApplicationContext.addBeanFactoryPostProcessor(processor);
configurableWebApplicationContext.addApplicationListener(processor);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,7 @@ public interface Messages

@Message(id = BASE + 90, value = "Starting up Jetty")
String startingUpJetty();

@Message(id = BASE + 95, value = "RESTeasy Deployment is null, do you have the ResteasyBootstrap listener configured?")
String deploymentIsNull();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.jboss.resteasy.core.ResourceMethodRegistry;
import org.jboss.resteasy.mock.MockDispatcherFactory;
import org.jboss.resteasy.spi.Registry;
import org.jboss.resteasy.spi.ResteasyDeployment;
import org.jboss.resteasy.spi.ResteasyProviderFactory;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -29,36 +30,11 @@ public void setupEditor() {
}

/**
* @tpTestDetails Tests that ProviderFactory is required for customizeContext() of SpringContextLoader
* @tpSince RESTEasy 3.0.16
* @tpTestDetails Tests that ResteasyDeployment is required for customizeContext() of SpringContextLoader
*/
@Test(expected = RuntimeException.class)
public void testThatProviderFactoryIsRequired() {
contextLoader.customizeContext(
mockServletContext(null, someRegistry(), someDispatcher()),
mockWebApplicationContext());
}

/**
* @tpTestDetails Tests that Registry is required for customizeContext() of SpringContextLoader
* @tpSince RESTEasy 3.0.16
*/
@Test(expected = RuntimeException.class)
public void testThatRegistryIsRequired() {
contextLoader.customizeContext(
mockServletContext(someProviderFactory(), null, someDispatcher()),
mockWebApplicationContext());
}

/**
* @tpTestDetails Tests that Dispatcher is required for customizeContext() of SpringContextLoader
* @tpSince RESTEasy 3.0.16
*/
@Test(expected = RuntimeException.class)
public void testThatDispatcherIsRequired() {
contextLoader.customizeContext(
mockServletContext(someProviderFactory(), someRegistry(), null),
mockWebApplicationContext());
public void testThatDeploymentIsRequired() {
contextLoader.customizeContext(mockServletContext(null), mockWebApplicationContext());
}

/**
Expand All @@ -69,9 +45,7 @@ public void testThatDispatcherIsRequired() {
public void testThatWeAddedAnApplicationListener() {
StaticWebApplicationContext context = mockWebApplicationContext();
int numListeners = context.getApplicationListeners().size();
contextLoader.customizeContext(
mockServletContext(someProviderFactory(), someRegistry(), someDispatcher()),
context);
contextLoader.customizeContext(mockServletContext(someDeployment()), context);
int numListenersNow = context.getApplicationListeners().size();
assertEquals("Expected to add exactly one new listener; in fact added " + (numListenersNow - numListeners),
numListeners + 1, numListenersNow);
Expand All @@ -81,36 +55,17 @@ private StaticWebApplicationContext mockWebApplicationContext() {
return new StaticWebApplicationContext();
}

private ServletContext mockServletContext(
ResteasyProviderFactory providerFactory,
Registry registry,
Dispatcher dispatcher) {
private ServletContext mockServletContext(ResteasyDeployment deployment) {
MockServletContext context = new MockServletContext();

if (providerFactory != null) {
context.setAttribute(ResteasyProviderFactory.class.getName(), providerFactory);
}

if (registry != null) {
context.setAttribute(Registry.class.getName(), registry);
}

if (dispatcher != null) {
context.setAttribute(Dispatcher.class.getName(), dispatcher);
if (deployment != null) {
context.setAttribute(ResteasyDeployment.class.getName(), deployment);
}

return context;
}

private Registry someRegistry() {
return new ResourceMethodRegistry(someProviderFactory());
}

private ResteasyProviderFactory someProviderFactory() {
return new ResteasyProviderFactory();
}

private Dispatcher someDispatcher() {
return MockDispatcherFactory.createDispatcher();
private ResteasyDeployment someDeployment() {
return new ResteasyDeployment();
}
}

0 comments on commit 1911ac0

Please sign in to comment.