diff --git a/pom.xml b/pom.xml index c13d52b..5a6c304 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ com.dievision.sinicum sinicum-server-parent - 0.10.3 + 0.10.5 pom sinicum-server-parent diff --git a/sinicum-server-magnolia-5/pom.xml b/sinicum-server-magnolia-5/pom.xml index a98ab7f..721e162 100644 --- a/sinicum-server-magnolia-5/pom.xml +++ b/sinicum-server-magnolia-5/pom.xml @@ -6,7 +6,7 @@ com.dievision.sinicum sinicum-server-parent - 0.10.3 + 0.10.5 sinicum-server-magnolia-5 diff --git a/sinicum-server/pom.xml b/sinicum-server/pom.xml index 934a033..12b0cd4 100644 --- a/sinicum-server/pom.xml +++ b/sinicum-server/pom.xml @@ -6,7 +6,7 @@ com.dievision.sinicum sinicum-server-parent - 0.10.3 + 0.10.5 sinicum-server @@ -23,11 +23,6 @@ - - org.glassfish.jersey.containers - jersey-container-servlet - ${jerseyVersion} - org.glassfish.jersey.containers jersey-container-servlet diff --git a/sinicum-server/src/main/java/com/dievision/sinicum/server/filters/ProxyEntry.java b/sinicum-server/src/main/java/com/dievision/sinicum/server/filters/ProxyEntry.java new file mode 100644 index 0000000..b0a86c5 --- /dev/null +++ b/sinicum-server/src/main/java/com/dievision/sinicum/server/filters/ProxyEntry.java @@ -0,0 +1,54 @@ +package com.dievision.sinicum.server.filters; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.net.URI; +import java.net.URISyntaxException; +import java.util.regex.Pattern; + +public class ProxyEntry { + private Pattern proxyPattern; + private URI proxyTargetUri; + private static final String DEFAULT_PATH_PATTERN = "/.*"; + private static final String DEFAULT_PROXY_TARGET_URI = "http://localhost:3000"; + private static final Logger logger = LoggerFactory.getLogger(ProxyEntry.class); + + public ProxyEntry() { + this(DEFAULT_PATH_PATTERN, DEFAULT_PROXY_TARGET_URI); + } + + public ProxyEntry(String proxyPattern, String proxyTargetUri) { + this.proxyPattern = Pattern.compile(proxyPattern); + try { + this.proxyTargetUri = new URI(proxyTargetUri); + } catch (URISyntaxException e) { + logger.error("Error setting sinicum-server default proxy target: " + + e.toString()); + } + } + + public boolean matchesPath(String path) { + if (path == null) { + return false; + } else { + return proxyPattern.matcher(path).matches(); + } + } + + public Pattern getProxyPattern() { + return proxyPattern; + } + + public void setProxyPattern(String proxyPattern) { + this.proxyPattern = Pattern.compile(proxyPattern); + } + + public URI getProxyTargetUri() { + return proxyTargetUri; + } + + public void setProxyTargetUri(URI proxyTargetUri) { + this.proxyTargetUri = proxyTargetUri; + } +} diff --git a/sinicum-server/src/main/java/com/dievision/sinicum/server/filters/ProxyFilter.java b/sinicum-server/src/main/java/com/dievision/sinicum/server/filters/ProxyFilter.java index f53069b..b8e82e3 100644 --- a/sinicum-server/src/main/java/com/dievision/sinicum/server/filters/ProxyFilter.java +++ b/sinicum-server/src/main/java/com/dievision/sinicum/server/filters/ProxyFilter.java @@ -138,9 +138,9 @@ private void performProxyRequest(HttpServletRequest request, HttpClient proxyClient = createNewClient(); try { // Execute the request - HttpResponse proxyResponse = - proxyClient.execute(URIUtils.extractHost( - ProxyFilterConfig.getInstance().getProxyTargetUri()), proxyRequest); + HttpResponse proxyResponse = proxyClient.execute(URIUtils.extractHost( + ProxyFilterConfig.getInstance().getProxyTargetUri( + request.getRequestURI())), proxyRequest); // Process the response int statusCode = proxyResponse.getStatusLine().getStatusCode(); @@ -297,7 +297,8 @@ private void copyRequestHeaders(HttpServletRequest servletRequest, HttpRequest p // the correct virtual server if (headerName.equalsIgnoreCase(HttpHeaders.HOST)) { HttpHost host = URIUtils.extractHost( - ProxyFilterConfig.getInstance().getProxyTargetUri()); + ProxyFilterConfig.getInstance().getProxyTargetUri( + servletRequest.getRequestURI())); headerValue = host.getHostName(); if (host.getPort() != -1) { headerValue += ":" + host.getPort(); @@ -339,7 +340,8 @@ private void copyResponseEntity(HttpResponse proxyResponse, HttpServletResponse private String rewriteUrlFromRequest(HttpServletRequest request) { StringBuilder uri = new StringBuilder(500); - uri.append(ProxyFilterConfig.getInstance().getProxyTargetUri().toString()); + uri.append(ProxyFilterConfig.getInstance().getProxyTargetUri( + request.getRequestURI()).toString()); // Handle the path given to the servlet if (request.getRequestURI() != null) { //ex: /my/path.html uri.append(request.getRequestURI()); @@ -362,7 +364,8 @@ private String rewriteUrlFromRequest(HttpServletRequest request) { private String rewriteUrlFromResponse(HttpServletRequest request, String theUrl) { //TODO document example paths - if (theUrl.startsWith(ProxyFilterConfig.getInstance().getProxyTargetUri().toString())) { + if (theUrl.startsWith(ProxyFilterConfig.getInstance().getProxyTargetUri( + request.getRequestURI()).toString())) { String curUrl = request.getRequestURL().toString(); //no query String pathInfo = request.getRequestURI(); if (pathInfo != null) { @@ -371,7 +374,7 @@ private String rewriteUrlFromResponse(HttpServletRequest request, String theUrl) curUrl = curUrl.substring(0, curUrl.length() - pathInfo.length()); } theUrl = curUrl + theUrl.substring(ProxyFilterConfig.getInstance() - .getProxyTargetUri().toString().length()); + .getProxyTargetUri(request.getRequestURI()).toString().length()); } return theUrl; } diff --git a/sinicum-server/src/main/java/com/dievision/sinicum/server/filters/ProxyFilterConfig.java b/sinicum-server/src/main/java/com/dievision/sinicum/server/filters/ProxyFilterConfig.java index 6727b13..32e6243 100644 --- a/sinicum-server/src/main/java/com/dievision/sinicum/server/filters/ProxyFilterConfig.java +++ b/sinicum-server/src/main/java/com/dievision/sinicum/server/filters/ProxyFilterConfig.java @@ -2,41 +2,48 @@ import java.net.URI; import java.net.URISyntaxException; -import java.util.regex.Pattern; +import java.util.ArrayList; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import javax.jcr.Node; +import javax.jcr.RepositoryException; + public final class ProxyFilterConfig { private static ProxyFilterConfig instance = null; - private Pattern proxyPattern; - private URI proxyTargetUri; - private static final String DEFAULT_PATH_PATTERN = "/.*"; - private static final String DEFAULT_PROXY_TARGET_URI = "http://localhost:3000"; + private ArrayList entries = new ArrayList(); private static final Logger logger = LoggerFactory.getLogger(ProxyFilterConfig.class); private ProxyFilterConfig() { // nothing } - public boolean matchesPath(String path) { - if (path == null) { - return false; - } else { - return proxyPattern.matcher(path).matches(); + public boolean matchesPath(String proxyPath) { + boolean performProxying = false; + for (ProxyEntry entry : getProxyEntries()) { + if (entry.getProxyPattern().matcher(proxyPath).matches()) { + performProxying = true; + } } + return performProxying; } - public URI getProxyTargetUri() { - return proxyTargetUri; + public void addProxyEntry(ProxyEntry entry) { + entries.add(entry); } - public void setProxyTargetUri(URI proxyTargetUri) { - this.proxyTargetUri = proxyTargetUri; + public ArrayList getProxyEntries() { + return entries; } - public void setProxyPattern(String pattern) { - proxyPattern = Pattern.compile(pattern); + public URI getProxyTargetUri(String proxyPath) { + for (ProxyEntry entry : getProxyEntries()) { + if (entry.getProxyPattern().matcher(proxyPath).matches()) { + return entry.getProxyTargetUri(); + } + } + return null; } public static ProxyFilterConfig getInstance() { @@ -44,13 +51,7 @@ public static ProxyFilterConfig getInstance() { synchronized (ProxyFilterConfig.class) { if (instance == null) { ProxyFilterConfig proxyFilterConfig = new ProxyFilterConfig(); - proxyFilterConfig.setProxyPattern(DEFAULT_PATH_PATTERN); - try { - proxyFilterConfig.setProxyTargetUri(new URI(DEFAULT_PROXY_TARGET_URI)); - } catch (URISyntaxException e) { - logger.error("Error setting sinicum-server default proxy target: " - + e.toString()); - } + instance = proxyFilterConfig; } } @@ -58,4 +59,19 @@ public static ProxyFilterConfig getInstance() { return instance; } + public static ProxyEntry buildProxyEntry(Node node) throws RepositoryException { + ProxyEntry entry = new ProxyEntry(); + if (node.hasProperty("proxyPathPattern")) { + entry.setProxyPattern(node.getProperty("proxyPathPattern").getString()); + } + if (node.hasProperty("proxyTargetUri")) { + try { + entry.setProxyTargetUri(new URI(node.getProperty("proxyTargetUri").getString())); + } catch (URISyntaxException e) { + logger.error("Error setting new sinicum-server proxy target: " + e.toString()); + } + } + return entry; + } + } diff --git a/sinicum-server/src/main/java/com/dievision/sinicum/server/jcr/templating/AreaInitializer.java b/sinicum-server/src/main/java/com/dievision/sinicum/server/jcr/templating/AreaInitializer.java index 7349e9c..4763d48 100644 --- a/sinicum-server/src/main/java/com/dievision/sinicum/server/jcr/templating/AreaInitializer.java +++ b/sinicum-server/src/main/java/com/dievision/sinicum/server/jcr/templating/AreaInitializer.java @@ -95,6 +95,7 @@ protected Node getAreaNode() { String template = null; try { template = getTemplateNameForNode(pageNode); + logger.error("Template is => " + template); } catch (RepositoryException e) { logger.error("Error finding template name: " + e.toString()); } @@ -102,7 +103,7 @@ protected Node getAreaNode() { ComponentId componentId = new ComponentId(template); try { Session configSession = MgnlContextAdapter.getJcrSession("config"); - String stmt = "/jcr:root/modules//templates/" + String stmt = "/jcr:root/modules/" + componentId.getComponent() + "/templates/" + componentId.parentPathElement() + "/" + componentId.getPath() + "/areas" + "/" + areaName; Query query = configSession.getWorkspace().getQueryManager() diff --git a/sinicum-server/src/main/java/com/dievision/sinicum/server/magnolia/SinicumServerModuleBase.java b/sinicum-server/src/main/java/com/dievision/sinicum/server/magnolia/SinicumServerModuleBase.java index 53a74fe..65fa3a8 100644 --- a/sinicum-server/src/main/java/com/dievision/sinicum/server/magnolia/SinicumServerModuleBase.java +++ b/sinicum-server/src/main/java/com/dievision/sinicum/server/magnolia/SinicumServerModuleBase.java @@ -1,22 +1,18 @@ package com.dievision.sinicum.server.magnolia; -import java.net.URI; -import java.net.URISyntaxException; - -import javax.jcr.Node; -import javax.jcr.PathNotFoundException; -import javax.jcr.RepositoryException; -import javax.jcr.Session; - +import com.dievision.sinicum.server.filters.ProxyEntry; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.dievision.sinicum.server.filters.ProxyFilterConfig; import com.dievision.sinicum.server.jcr.caching.CacheObserverManager; -/** - * - */ +import javax.jcr.Node; +import javax.jcr.NodeIterator; +import javax.jcr.PathNotFoundException; +import javax.jcr.RepositoryException; +import javax.jcr.Session; + public class SinicumServerModuleBase { private static final Logger logger = LoggerFactory.getLogger(SinicumServerModuleBase.class); @@ -28,18 +24,21 @@ protected void start(String moduleName, Session session) { private void readModuleConfiguration(String moduleName, Session session) { logger.info("Configuring sinicum-server module..."); try { - Node node = (Node) session.getItem("/modules/" + moduleName - + "/config/proxy-servlet/default"); - if (node.hasProperty("proxyPathPattern")) { - ProxyFilterConfig.getInstance().setProxyPattern( - node.getProperty("proxyPathPattern").getString()); - } - if (node.hasProperty("proxyTargetUri")) { - try { - ProxyFilterConfig.getInstance().setProxyTargetUri( - new URI(node.getProperty("proxyTargetUri").getString())); - } catch (URISyntaxException e) { - logger.error("Error setting new sinicum-server proxy target: " + e.toString()); + if (session.nodeExists("/modules/" + moduleName + + "/config/proxy-servlet/default")) { + Node node = (Node) session.getItem("/modules/" + moduleName + + "/config/proxy-servlet/default"); + ProxyEntry entry = ProxyFilterConfig.buildProxyEntry(node); + ProxyFilterConfig.getInstance().addProxyEntry(entry); + } else { + Node parent = session.getNode("/modules/" + moduleName + "/config/proxy-servlet"); + if (parent.hasNodes()) { + NodeIterator it = parent.getNodes(); + while (it.hasNext()) { + Node node = it.nextNode(); + ProxyEntry entry = ProxyFilterConfig.buildProxyEntry(node); + ProxyFilterConfig.getInstance().addProxyEntry(entry); + } } } logger.info("Success"); diff --git a/sinicum-server/src/test/java/com/dievision/sinicum/server/filters/ProxyFilterConfigTest.java b/sinicum-server/src/test/java/com/dievision/sinicum/server/filters/ProxyFilterConfigTest.java index afb2938..5b6802a 100644 --- a/sinicum-server/src/test/java/com/dievision/sinicum/server/filters/ProxyFilterConfigTest.java +++ b/sinicum-server/src/test/java/com/dievision/sinicum/server/filters/ProxyFilterConfigTest.java @@ -4,21 +4,21 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.net.URI; +import java.net.URISyntaxException; + import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; public class ProxyFilterConfigTest { private static final Logger logger = LoggerFactory.getLogger(ProxyFilterConfigTest.class); @Test - public void testDefaultPattern() { - assertTrue(ProxyFilterConfig.getInstance().matchesPath("/.*")); - assertTrue(ProxyFilterConfig.getInstance().matchesPath("/something")); - } + public void testSetNewPattern() throws URISyntaxException { + ProxyEntry entry = new ProxyEntry("/something.*", "http://www.example.com"); - @Test - public void testSetNewPattern() { - ProxyFilterConfig.getInstance().setProxyPattern("/something.*"); + ProxyFilterConfig.getInstance().addProxyEntry(entry); ProxyFilterConfig config = ProxyFilterConfig.getInstance(); assertTrue(config.matchesPath("/something")); assertTrue(config.matchesPath("/somethingnew")); @@ -29,4 +29,23 @@ public void testSetNewPattern() { assertFalse(config.matchesPath("/")); } + @Test + public void testMultipleEntries() throws URISyntaxException { + ProxyEntry entry = new ProxyEntry("/something.*", "http://www.example.com"); + ProxyFilterConfig.getInstance().addProxyEntry(entry); + ProxyEntry entry2 = new ProxyEntry("/otherpath.*", "http://www.dievision.de"); + ProxyFilterConfig.getInstance().addProxyEntry(entry2); + ProxyEntry entry3 = new ProxyEntry("/.*", "http://www.the-rest.de"); + ProxyFilterConfig.getInstance().addProxyEntry(entry3); + + ProxyFilterConfig config = ProxyFilterConfig.getInstance(); + assertTrue(config.matchesPath("/something")); + assertTrue(config.matchesPath("/otherpath")); + + assertEquals(config.getProxyTargetUri("/something/more"), + new URI("http://www.example.com")); + assertEquals(config.getProxyTargetUri("/otherpath"), new URI("http://www.dievision.de")); + assertEquals(config.getProxyTargetUri("/123123123"), new URI("http://www.the-rest.de")); + } + }