Commit 379412c1 authored by Mathijs den Burger's avatar Mathijs den Burger

CMS-10585 Reintegrate bugfix/CMS-10585

parents 63b77c40 488bc953
/*
* Copyright 2011-2016 Hippo B.V. (http://www.onehippo.com)
* Copyright 2011-2017 Hippo B.V. (http://www.onehippo.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -307,6 +307,18 @@ public class ResourceServlet extends HttpServlet {
proxies = initProxies(jarPathPrefix);
}
/**
* Examples configurations and mapping
* - jarPath=/angular
* - from=angular/hippo-cm
* - to=http://localhost:9090
* Results in mapping "/hippo-cm/" -> "http://localhost:9090/"
*
* - jarPath=/angular
* - from=angular/hippo-cm
* - to=http://localhost:9090/cms/angular/hippo-cm
* Results in mapping "/hippo-cm/" -> "http://localhost:9090/cms/angular/hippo-cm/"
*/
private static HashMap<String, String> initProxies(final String jarPathPrefix) {
final HashMap<String, String> proxies = new HashMap<>();
final String proxiesAsString = System.getProperty(PROXIES_SYSTEM_PROPERTY);
......@@ -317,12 +329,12 @@ public class ResourceServlet extends HttpServlet {
.map(proxyLine -> StringUtils.split(proxyLine, PROXY_FROM_TO_SEPARATOR))
.forEach(fromTo -> {
if (fromTo.length > 1) {
// Example config: from=angular/hippo-cm, to=http://localhost:9090/cms, jarPath=/angular
// Results in mapping "/hippo-cm/" -> "http://localhost:9090/cms/angular/hippo-cm/"
String from = "/" + StringUtils.trim(fromTo[0]) + "/";
String from = StringUtils.trim(fromTo[0]);
from = prependIfMissing(from, "/");
from = appendIfMissing(from, "/");
String to = StringUtils.trim(fromTo[1]);
to = appendIfMissing(to, "/");
if (from.startsWith(jarPathPrefix + "/")) {
to = to + from;
from = StringUtils.removeStart(from, jarPathPrefix);
proxies.put(from, to);
}
......@@ -344,26 +356,25 @@ public class ResourceServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String resourcePath = StringUtils.substringBefore(request.getPathInfo(), ";");
final String resourcePath = StringUtils.substringBefore(request.getPathInfo(), ";");
String queryParams = request.getQueryString();
queryParams = StringUtils.isEmpty(queryParams) ? "" : "?" + queryParams;
if (log.isDebugEnabled()) {
log.debug("Processing request for resource {}{}.", resourcePath, queryParams);
}
URL resource = getResourceURL(resourcePath, queryParams);
log.debug("Processing request for resource {}{}.", resourcePath, queryParams);
final URL resource = getResourceURL(resourcePath, queryParams);
if (resource == null) {
if (log.isDebugEnabled()) {
log.debug("Resource not found: {}", resourcePath);
}
response.sendError(HttpServletResponse.SC_NOT_FOUND);
notFound(response, resourcePath);
return;
}
long modifiedSince = request.getDateHeader(HTTP_IF_MODIFIED_SINCE_HEADER);
final URLConnection conn = resource.openConnection();
if (conn == null) {
notFound(response, resourcePath);
return;
}
final long modifiedSince = request.getDateHeader(HTTP_IF_MODIFIED_SINCE_HEADER);
if (!proxies.isEmpty() && conn instanceof HttpURLConnection) {
......@@ -371,11 +382,10 @@ public class ResourceServlet extends HttpServlet {
conn.setIfModifiedSince(modifiedSince);
}
HttpURLConnection httpConn = (HttpURLConnection) conn;
final HttpURLConnection httpConn = (HttpURLConnection) conn;
try {
if (httpConn.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
log.debug("Resource not found: {}", resourcePath);
response.sendError(HttpServletResponse.SC_NOT_FOUND);
notFound(response, resourcePath);
return;
}
} catch (ConnectException e) {
......@@ -384,17 +394,15 @@ public class ResourceServlet extends HttpServlet {
}
}
long lastModified = conn.getLastModified();
final long lastModified = conn.getLastModified();
if (modifiedSince >= lastModified) {
if (log.isDebugEnabled()) {
log.debug("Resource: {} Not Modified.", resourcePath);
}
log.debug("Resource: {} Not Modified.", resourcePath);
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
return;
}
int contentLength = conn.getContentLength();
final int contentLength = conn.getContentLength();
prepareResponse(response, resource, lastModified, contentLength);
......@@ -409,6 +417,11 @@ public class ResourceServlet extends HttpServlet {
}
}
private void notFound(final HttpServletResponse response, final String resourcePath) throws IOException {
log.debug("Resource not found: {}", resourcePath);
response.sendError(HttpServletResponse.SC_NOT_FOUND);
}
private Set<Pattern> initPatterns(final String paramName, Set<Pattern> defaultPatterns) {
final String param = getInitParameter(paramName, null);
......@@ -591,6 +604,20 @@ public class ResourceServlet extends HttpServlet {
});
}
private static String prependIfMissing(final String str, final String prefix) {
if (str == null || StringUtils.isEmpty(prefix) || StringUtils.startsWith(str, prefix)) {
return str;
}
return prefix + str;
}
private static String appendIfMissing(final String str, final String suffix) {
if (str == null || StringUtils.isEmpty(suffix) || StringUtils.endsWith(str, suffix)) {
return str;
}
return str + suffix;
}
private class GZIPResponseStream extends ServletOutputStream {
private ByteArrayOutputStream byteStream = null;
......
/*
* Copyright 2016 Hippo B.V. (http://www.onehippo.com)
* Copyright 2016-2017 Hippo B.V. (http://www.onehippo.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -40,8 +40,8 @@ public class ResourceServletProxyTest {
@Test
public void testProxy() throws Exception {
final ProxyServlet proxyServlet = new ProxyServlet("/servlet",
"servlet/test@classpath:org/onehippo/cms7/utilities");
final HttpServletResponse response = proxyServlet.get("/test/index.js");
"servlet/path@classpath:org/onehippo/cms7/utilities/servlet/test");
final HttpServletResponse response = proxyServlet.get("/path/index.js");
assertEquals(200, response.getStatus());
assertEquals("text/javascript", response.getContentType());
......@@ -50,18 +50,27 @@ public class ResourceServletProxyTest {
@Test
public void testProxies() throws Exception {
final ProxyServlet proxyServlet = new ProxyServlet("/servlet",
"servlet/test@classpath:org/onehippo/cms7/utilities," +
"servlet/test2@classpath:META-INF");
"servlet/path@classpath:org/onehippo/cms7/utilities/servlet/test," +
"servlet/path2@classpath:org/onehippo/cms7/utilities/servlet/test2");
final HttpServletResponse response1 = proxyServlet.get("/test/index.js");
final HttpServletResponse response1 = proxyServlet.get("/path/index.js");
assertEquals(200, response1.getStatus());
assertEquals("text/javascript", response1.getContentType());
final HttpServletResponse response2 = proxyServlet.get("/test2/onehippo.gif");
final HttpServletResponse response2 = proxyServlet.get("/path2/onehippo.gif");
assertEquals(200, response2.getStatus());
assertEquals("image/gif", response2.getContentType());
}
@Test
public void testProxyFileNotFound() throws Exception {
final ProxyServlet proxyServlet = new ProxyServlet("/servlet",
"servlet/path@classpath:org/onehippo/cms7/utilities/servlet/test");
final HttpServletResponse response = proxyServlet.get("/path/non-existing.js");
assertEquals(404, response.getStatus());
}
@Test
public void testProxyDisabled() throws Exception {
final ProxyServlet proxyServlet = new ProxyServlet("/servlet", "");
......
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