Commit 0df12e2a authored by Mathijs den Burger's avatar Mathijs den Burger

CMS-10437 Configure proxies for a specific resource servlet only

The 'from' part in the proxies configuration should now include
the jar path of the resource servlet so only the servlet with that
specific jar path actually proxies (instead of all resource
servlets). The 'from' part is automatically appended to the 'to'
part so the system property get slightly less verbose.

Example config:
- resource.proxies = angular/hippo-cm@to=http://localhost:9090/cms
- jarPath = /angular

Results in forwarding "/hippo-cm/somefile.js"
to "http://localhost:9090/cms/angular/hippo-cm/somefile.js"
parent 0ae2244a
...@@ -304,17 +304,28 @@ public class ResourceServlet extends HttpServlet { ...@@ -304,17 +304,28 @@ public class ResourceServlet extends HttpServlet {
} }
} }
proxies = initProxies(jarPathPrefix);
}
private static HashMap<String, String> initProxies(final String jarPathPrefix) {
final HashMap<String, String> proxies = new HashMap<>();
final String proxiesAsString = System.getProperty(PROXIES_SYSTEM_PROPERTY); final String proxiesAsString = System.getProperty(PROXIES_SYSTEM_PROPERTY);
proxies = new HashMap<>();
if (StringUtils.isNotBlank(proxiesAsString)) { if (StringUtils.isNotBlank(proxiesAsString)) {
Arrays.stream(StringUtils.split(proxiesAsString, PROXY_SEPARATOR)) Arrays.stream(StringUtils.split(proxiesAsString, PROXY_SEPARATOR))
.filter(StringUtils::isNotBlank) .filter(StringUtils::isNotBlank)
.map(proxyLine -> StringUtils.split(proxyLine, PROXY_FROM_TO_SEPARATOR)) .map(proxyLine -> StringUtils.split(proxyLine, PROXY_FROM_TO_SEPARATOR))
.forEach(fromTo -> { .forEach(fromTo -> {
if (fromTo.length > 1) { if (fromTo.length > 1) {
final String from = "/" + StringUtils.trim(fromTo[0]) + "/"; // Example config: from=angular/hippo-cm, to=http://localhost:9090/cms, jarPath=/angular
final String to = StringUtils.trim(fromTo[1]) + "/"; // Results in mapping "/hippo-cm/" -> "http://localhost:9090/cms/angular/hippo-cm/"
proxies.put(from, to); String from = "/" + StringUtils.trim(fromTo[0]) + "/";
String to = StringUtils.trim(fromTo[1]);
if (from.startsWith(jarPathPrefix + "/")) {
to = to + from;
from = StringUtils.removeStart(from, jarPathPrefix);
proxies.put(from, to);
}
} }
}); });
} }
...@@ -326,6 +337,8 @@ public class ResourceServlet extends HttpServlet { ...@@ -326,6 +337,8 @@ public class ResourceServlet extends HttpServlet {
logBorderedMessage(messages); logBorderedMessage(messages);
} }
return proxies;
} }
@Override @Override
......
...@@ -39,8 +39,9 @@ public class ResourceServletProxyTest { ...@@ -39,8 +39,9 @@ public class ResourceServletProxyTest {
@Test @Test
public void testProxy() throws Exception { public void testProxy() throws Exception {
final ProxyServlet proxyServlet = new ProxyServlet("hippo-proxy@classpath:org/onehippo/cms7/utilities/servlet"); final ProxyServlet proxyServlet = new ProxyServlet("/servlet",
final HttpServletResponse response = proxyServlet.get("/hippo-proxy/index.js"); "servlet/test@classpath:org/onehippo/cms7/utilities");
final HttpServletResponse response = proxyServlet.get("/test/index.js");
assertEquals(200, response.getStatus()); assertEquals(200, response.getStatus());
assertEquals("text/javascript", response.getContentType()); assertEquals("text/javascript", response.getContentType());
...@@ -48,23 +49,24 @@ public class ResourceServletProxyTest { ...@@ -48,23 +49,24 @@ public class ResourceServletProxyTest {
@Test @Test
public void testProxies() throws Exception { public void testProxies() throws Exception {
final ProxyServlet proxyServlet = new ProxyServlet("proxy1@classpath:org/onehippo/cms7/utilities/servlet," + final ProxyServlet proxyServlet = new ProxyServlet("/servlet",
"proxy2@classpath:META-INF/test"); "servlet/test@classpath:org/onehippo/cms7/utilities," +
"servlet/test2@classpath:META-INF");
final HttpServletResponse response1 = proxyServlet.get("/proxy1/index.js"); final HttpServletResponse response1 = proxyServlet.get("/test/index.js");
assertEquals(200, response1.getStatus()); assertEquals(200, response1.getStatus());
assertEquals("text/javascript", response1.getContentType()); assertEquals("text/javascript", response1.getContentType());
final HttpServletResponse response2 = proxyServlet.get("/proxy2/onehippo.gif"); final HttpServletResponse response2 = proxyServlet.get("/test2/onehippo.gif");
assertEquals(200, response2.getStatus()); assertEquals(200, response2.getStatus());
assertEquals("image/gif", response2.getContentType()); assertEquals("image/gif", response2.getContentType());
} }
@Test @Test
public void testProxyDisabled() throws Exception { public void testProxyDisabled() throws Exception {
final ProxyServlet proxyServlet = new ProxyServlet(""); final ProxyServlet proxyServlet = new ProxyServlet("/servlet", "");
final HttpServletResponse response = proxyServlet.get("/hippo-cm/index.js"); final HttpServletResponse response = proxyServlet.get("/sub/index.js");
assertEquals(404, response.getStatus()); assertEquals(404, response.getStatus());
} }
...@@ -87,11 +89,15 @@ public class ResourceServletProxyTest { ...@@ -87,11 +89,15 @@ public class ResourceServletProxyTest {
final ResourceServlet servlet; final ResourceServlet servlet;
ProxyServlet(final String resourceProxies) throws ServletException { ProxyServlet(final String jarPathPrefix, final String resourceProxies) throws ServletException {
System.setProperty("resource.proxies", resourceProxies); System.setProperty("resource.proxies", resourceProxies);
servlet = new ResourceServlet(); servlet = new ResourceServlet();
servlet.init(new MockServletConfig());
final MockServletConfig config = new MockServletConfig();
config.addInitParameter("jarPathPrefix", jarPathPrefix);
servlet.init(config);
} }
HttpServletResponse get(final String path) throws ServletException, IOException { HttpServletResponse get(final String path) throws ServletException, IOException {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment