Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.dievision.sinicum</groupId>
<artifactId>sinicum-server-parent</artifactId>
<version>0.10.3</version>
<version>0.10.5</version>
<packaging>pom</packaging>

<name>sinicum-server-parent</name>
Expand Down
2 changes: 1 addition & 1 deletion sinicum-server-magnolia-5/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.dievision.sinicum</groupId>
<artifactId>sinicum-server-parent</artifactId>
<version>0.10.3</version>
<version>0.10.5</version>
</parent>

<artifactId>sinicum-server-magnolia-5</artifactId>
Expand Down
7 changes: 1 addition & 6 deletions sinicum-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.dievision.sinicum</groupId>
<artifactId>sinicum-server-parent</artifactId>
<version>0.10.3</version>
<version>0.10.5</version>
</parent>

<artifactId>sinicum-server</artifactId>
Expand All @@ -23,11 +23,6 @@


<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>${jerseyVersion}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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());
Expand All @@ -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) {
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,76 @@

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<ProxyEntry> entries = new ArrayList<ProxyEntry>();
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<ProxyEntry> 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() {
if (instance == null) {
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;
}
}
}
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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,15 @@ 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());
}
if (template != null) {
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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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);

Expand All @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand All @@ -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"));
}

}