Skip to content

Commit

Permalink
[RESTEASY-2266] Fix storage of ServletConfig when useGlobal applies.
Browse files Browse the repository at this point in the history
  • Loading branch information
ronsigal committed Jun 17, 2019
1 parent 0f4606d commit 4714904
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 2 deletions.
Expand Up @@ -37,10 +37,9 @@ public void init(ServletConfig servletConfig) throws ServletException
Map<Class<?>, Object> map = ResteasyContext.getContextDataMap();
map.put(ServletContext.class, servletConfig.getServletContext());
map.put(ServletConfig.class, servletConfig);
servletContainerDispatcher = new ServletContainerDispatcher();
servletContainerDispatcher = new ServletContainerDispatcher(servletConfig);
ServletBootstrap bootstrap = new ServletBootstrap(servletConfig);
servletContainerDispatcher.init(servletConfig.getServletContext(), bootstrap, this, this);
servletContainerDispatcher.getDispatcher().getDefaultContextObjects().put(ServletConfig.class, servletConfig);
}

@Override
Expand Down
Expand Up @@ -4,6 +4,7 @@
import java.util.ArrayList;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -44,6 +45,18 @@ public class ServletContainerDispatcher
protected HttpRequestFactory requestFactory;
protected HttpResponseFactory responseFactory;

protected ServletConfig servletConfig;

public ServletContainerDispatcher(final ServletConfig servletConfig)
{
this.servletConfig = servletConfig;
ResteasyContext.pushContext(ServletConfig.class, servletConfig);
}

public ServletContainerDispatcher()
{
}

public Dispatcher getDispatcher()
{
return dispatcher;
Expand Down Expand Up @@ -229,6 +242,8 @@ public void service(String httpMethod, HttpServletRequest request, HttpServletRe
ResteasyContext.pushContext(HttpServletResponse.class, response);

ResteasyContext.pushContext(SecurityContext.class, new ServletSecurityContext(request));
ResteasyContext.pushContext(ServletConfig.class, servletConfig);

if (handleNotFound)
{
dispatcher.invoke(in, theResponse);
Expand Down
@@ -0,0 +1,73 @@
package org.jboss.resteasy.test.microprofile.config;

import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.Response;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.test.microprofile.config.resource.MicroProfileConfigUseGlobalApplication1;
import org.jboss.resteasy.test.microprofile.config.resource.MicroProfileConfigUseGlobalApplication2;
import org.jboss.resteasy.test.microprofile.config.resource.MicroProfileConfigUseGlobalResource;
import org.jboss.resteasy.utils.PortProviderUtil;
import org.jboss.resteasy.utils.TestUtil;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
* @tpSubChapter MicroProfile Config: ServletConfig with useGlobal and multiple servlets
* @tpChapter Integration tests
* @tpTestCaseDetails Regression tests for RESTEASY-2266
* @tpSince RESTEasy 4.1.0
*/
@RunWith(Arquillian.class)
@RunAsClient
public class MicroProfileConfigUseGlobalTest {

static ResteasyClient client;

@Deployment
public static Archive<?> deploy() {
WebArchive war = TestUtil.prepareArchive(MicroProfileConfigUseGlobalTest.class.getSimpleName())
.addClass(MicroProfileConfigUseGlobalApplication1.class)
.addClass(MicroProfileConfigUseGlobalApplication2.class)
.setWebXML(MicroProfileConfigUseGlobalTest.class.getPackage(), "web_use_global.xml")
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
return TestUtil.finishContainerPrepare(war, null, MicroProfileConfigUseGlobalResource.class);
}

@BeforeClass
public static void before() throws Exception {
client = (ResteasyClient)ClientBuilder.newClient();
}

@AfterClass
public static void after() throws Exception {
client.close();
}

private String generateURL(String path) {
return PortProviderUtil.generateURL(path, MicroProfileConfigUseGlobalTest.class.getSimpleName());
}

/**
* @tpTestDetails
* @tpSince RESTEasy 4.1.0
*/
@Test
public void testMultipleAppsUseGlobal() throws Exception {
Response response = client.target(generateURL("/app1/prefix")).request().get();
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals("/app1", response.readEntity(String.class));
response = client.target(generateURL("/app2/prefix")).request().get();
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals("/app2", response.readEntity(String.class));
}
}
@@ -0,0 +1,9 @@
package org.jboss.resteasy.test.microprofile.config.resource;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("app1")
public class MicroProfileConfigUseGlobalApplication1 extends Application {

}
@@ -0,0 +1,9 @@
package org.jboss.resteasy.test.microprofile.config.resource;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("app2")
public class MicroProfileConfigUseGlobalApplication2 extends Application {

}
@@ -0,0 +1,23 @@
package org.jboss.resteasy.test.microprofile.config.resource;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.ConfigProvider;

@Path("/")
public class MicroProfileConfigUseGlobalResource {

@Inject Config config;

@GET
@Produces("text/plain")
@Path("prefix")
public String prefix() {
String p = ConfigProvider.getConfig().getOptionalValue("resteasy.servlet.mapping.prefix", String.class).orElse("d'oh");
return p;
}
}
@@ -0,0 +1,11 @@
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

<listener>
<listener-class>
org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
</listener-class>
</listener>

</web-app>

0 comments on commit 4714904

Please sign in to comment.