From 95e6e536a25394c3e839304cf5bfb33830a954d0 Mon Sep 17 00:00:00 2001 From: Johannes Wilden Date: Tue, 6 Aug 2013 11:07:07 +0200 Subject: [PATCH 01/28] added repos, dep-management, scm to pom.xml --- pom.xml | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/pom.xml b/pom.xml index 9602d04..d8e74d0 100644 --- a/pom.xml +++ b/pom.xml @@ -111,4 +111,43 @@ http://localhost:8080 + + + scm:git:git@github.com:lat-lon/j2ep.git + scm:git:git@github.com:lat-lon/j2ep.git + https://github.com/lat-lon/j2ep.git + + + + + latlon-repo + http://repo.lat-lon/nexus/content/groups/public/ + + + j2ep-releases + http://repo.lat-lon/nexus/content/repositories/j2ep-release/ + + + j2ep-snapshots + http://repo.lat-lon/nexus/content/repositories/j2ep-snapshots/ + + + + + + j2ep-releases + http://repo.lat-lon/nexus/content/repositories/j2ep-release/ + + + j2ep-snapshots + http://repo.lat-lon/nexus/content/repositories/j2ep-snapshots/ + + + + + + latlon-repo + http://repo.lat-lon/nexus/content/groups/public/ + + \ No newline at end of file From 229337aaa0c4338ab7a25d4e14edfdee3b210f5f Mon Sep 17 00:00:00 2001 From: Johannes Wilden Date: Tue, 6 Aug 2013 11:15:22 +0200 Subject: [PATCH 02/28] added sonar config --- pom.xml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pom.xml b/pom.xml index d8e74d0..162f1ec 100644 --- a/pom.xml +++ b/pom.xml @@ -110,6 +110,9 @@ http://localhost:8080 + + http://vento.lat-lon:9000 + jdbc:postgresql://habub/sonar From eb948f4badb439b37fe61ff9d1118b0a6ed37ee6 Mon Sep 17 00:00:00 2001 From: aerben Date: Tue, 6 Aug 2013 11:40:04 +0200 Subject: [PATCH 03/28] Changed behaviour of directory rule url rewrite - now does not append a trailing slash --- src/main/java/net/sf/j2ep/rules/DirectoryRule.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/net/sf/j2ep/rules/DirectoryRule.java b/src/main/java/net/sf/j2ep/rules/DirectoryRule.java index c8e80c1..b9837d6 100644 --- a/src/main/java/net/sf/j2ep/rules/DirectoryRule.java +++ b/src/main/java/net/sf/j2ep/rules/DirectoryRule.java @@ -49,9 +49,9 @@ public void setDirectory(String directory) { if (!directory.startsWith("/")) { directory = "/" + directory; } - if (!directory.endsWith(("/"))) { - directory += "/"; - } +// if (!directory.endsWith(("/"))) { +// directory += "/"; +// } this.directory = directory; } } From d53a233bee5cd89ddc33d719965c254f5cda4fa7 Mon Sep 17 00:00:00 2001 From: aerben Date: Tue, 6 Aug 2013 11:46:30 +0200 Subject: [PATCH 04/28] Set project specific version and changed artifact type to jar --- pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 162f1ec..83b9ee2 100644 --- a/pom.xml +++ b/pom.xml @@ -3,8 +3,8 @@ 4.0.0 de.latlon j2ep - 1.0.0-SNAPSHOT - war + 1.0.0-blackbridge-SNAPSHOT + jar j2ep Fork of jEasy Extensible Proxy - svn://svn.code.sf.net/p/j2ep/code/trunk From a7a7e1e7583d203befd8fb66414f1440de683ea8 Mon Sep 17 00:00:00 2001 From: aerben Date: Tue, 6 Aug 2013 11:55:08 +0200 Subject: [PATCH 05/28] Set version to branch name --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 83b9ee2..543ca1f 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 de.latlon j2ep - 1.0.0-blackbridge-SNAPSHOT + 1.0.0-securityproxy-SNAPSHOT jar j2ep Fork of jEasy Extensible Proxy - svn://svn.code.sf.net/p/j2ep/code/trunk From a171974c66746fc59fc61fd6b7ca8988fcc14798 Mon Sep 17 00:00:00 2001 From: aerben Date: Tue, 6 Aug 2013 11:40:04 +0200 Subject: [PATCH 06/28] Changed behaviour of directory rule url rewrite - now does not append a trailing slash --- src/main/java/net/sf/j2ep/rules/DirectoryRule.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/net/sf/j2ep/rules/DirectoryRule.java b/src/main/java/net/sf/j2ep/rules/DirectoryRule.java index c8e80c1..b9837d6 100644 --- a/src/main/java/net/sf/j2ep/rules/DirectoryRule.java +++ b/src/main/java/net/sf/j2ep/rules/DirectoryRule.java @@ -49,9 +49,9 @@ public void setDirectory(String directory) { if (!directory.startsWith("/")) { directory = "/" + directory; } - if (!directory.endsWith(("/"))) { - directory += "/"; - } +// if (!directory.endsWith(("/"))) { +// directory += "/"; +// } this.directory = directory; } } From 89671be7f4d38acbaf45ebadcad091afe97894ab Mon Sep 17 00:00:00 2001 From: aerben Date: Tue, 6 Aug 2013 14:45:19 +0200 Subject: [PATCH 07/28] Made append of trailing slash configurable --- .../java/net/sf/j2ep/rules/DirectoryRule.java | 49 ++++++++++++++++--- 1 file changed, 43 insertions(+), 6 deletions(-) diff --git a/src/main/java/net/sf/j2ep/rules/DirectoryRule.java b/src/main/java/net/sf/j2ep/rules/DirectoryRule.java index b9837d6..885021e 100644 --- a/src/main/java/net/sf/j2ep/rules/DirectoryRule.java +++ b/src/main/java/net/sf/j2ep/rules/DirectoryRule.java @@ -35,6 +35,32 @@ public class DirectoryRule extends BaseRule { */ private String directory; + /** + * The originally set directory structure before appending and prepending slashes + */ + private String originalDirectory; + + private boolean isAppendTrailingSlash = true; + + /** + * Decide whether a trailing slash should be appended to directory settings without a trailing slash. + * @param isAppendTrailingSlash + */ + public void setIsAppendTrailingSlash(String isAppendTrailingSlash) { + if (isAppendTrailingSlash != null && isAppendTrailingSlash.equals("false")) { + this.isAppendTrailingSlash = false; + directory = prependSlashIfNeccessary(originalDirectory); + } + } + + /** + * Return the trailing slash setting + * @return true if slashes are appended to directory settings that do not end with a slash, false if not + */ + public boolean getAppendTrailingSlash () { + return isAppendTrailingSlash; + } + /** * Sets the directory structure that will * be mapped to a specified server. @@ -46,16 +72,27 @@ public void setDirectory(String directory) { throw new IllegalArgumentException( "The directory string cannot be null."); } else { - if (!directory.startsWith("/")) { - directory = "/" + directory; - } -// if (!directory.endsWith(("/"))) { -// directory += "/"; -// } + originalDirectory = directory; + directory = prependSlashIfNeccessary(directory); + directory = appendSlashIfNeccessary(directory); this.directory = directory; } } + private String appendSlashIfNeccessary(String directory) { + if (!directory.endsWith(("/")) && isAppendTrailingSlash) { + directory += "/"; + } + return directory; + } + + private String prependSlashIfNeccessary(String directory) { + if (!directory.startsWith("/")) { + directory = "/" + directory; + } + return directory; + } + /** * Returns the directory structure that * this rule will match on. From 3b8c007db6ecfaf4840a21d33f34dc7146284f7a Mon Sep 17 00:00:00 2001 From: aerben Date: Tue, 6 Aug 2013 15:29:33 +0200 Subject: [PATCH 08/28] Externalized configuration of j2ep by introduction a servlet init param with an environment variable for the config location --- src/main/java/net/sf/j2ep/ProxyFilter.java | 35 +++++++++++++++++----- src/main/webapp/WEB-INF/web.xml | 4 +-- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/main/java/net/sf/j2ep/ProxyFilter.java b/src/main/java/net/sf/j2ep/ProxyFilter.java index 8d57642..029d5c8 100644 --- a/src/main/java/net/sf/j2ep/ProxyFilter.java +++ b/src/main/java/net/sf/j2ep/ProxyFilter.java @@ -54,7 +54,11 @@ */ public class ProxyFilter implements Filter { - /** + private static final String CONFIG_XML = "/config.xml"; + + private static final String DEFAULT_PATH = "/WEB-INF/config/data.xml"; + + /** * The server chain, will be traversed to find a matching server. */ private ServerChain serverChain; @@ -196,20 +200,37 @@ public void init(FilterConfig filterConfig) throws ServletException { httpClient.getParams().setBooleanParameter(HttpClientParams.USE_EXPECT_CONTINUE, false); httpClient.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES); - String data = filterConfig.getInitParameter("dataUrl"); - if (data == null) { + String proxyConfigEnv = filterConfig.getInitParameter("proxyConfigEnv"); + if (proxyConfigEnv == null) { serverChain = null; } else { try { - File dataFile = new File(filterConfig.getServletContext().getRealPath(data)); - ConfigParser parser = new ConfigParser(dataFile); - serverChain = parser.getServerChain(); + String path = buildConfigFilePath(proxyConfigEnv); + processConfiguration(path); } catch (Exception e) { - throw new ServletException(e); + try { + String path = filterConfig.getServletContext().getRealPath(DEFAULT_PATH); + processConfiguration(path); + } catch (Exception e1) { + throw new ServletException(e); + } } } } + private void processConfiguration(String path) { + File dataFile = new File(path); + ConfigParser parser = new ConfigParser(dataFile); + serverChain = parser.getServerChain(); + } + + private String buildConfigFilePath(String proxyConfigEnv) { + String path = System.getenv(proxyConfigEnv); + if (!path.endsWith("/")) path += "/"; + path += CONFIG_XML; + return path; + } + /** * Called when this filter is destroyed. * Releases the fields. diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml index 20dbb1a..5cf3c17 100644 --- a/src/main/webapp/WEB-INF/web.xml +++ b/src/main/webapp/WEB-INF/web.xml @@ -14,8 +14,8 @@ Proxy net.sf.j2ep.ProxyFilter - dataUrl - /WEB-INF/config/data.xml + proxyConfigEnv + PROXY_CONFIG From 4711173e8a5de9d02ec2f75b29ab73183ad5a8dc Mon Sep 17 00:00:00 2001 From: Johannes Wilden Date: Thu, 8 Aug 2013 17:20:57 +0200 Subject: [PATCH 09/28] set version to branch version --- pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 543ca1f..c5ded0b 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 de.latlon j2ep - 1.0.0-securityproxy-SNAPSHOT + 1.0.0-securityproxy-master-SNAPSHOT jar j2ep Fork of jEasy Extensible Proxy - svn://svn.code.sf.net/p/j2ep/code/trunk @@ -153,4 +153,4 @@ http://repo.lat-lon/nexus/content/groups/public/ - \ No newline at end of file + From 1d36df4ca712dfe53e85ae52712940d89f0a1458 Mon Sep 17 00:00:00 2001 From: aerben Date: Thu, 8 Aug 2013 17:37:55 +0200 Subject: [PATCH 10/28] Fixed bug in DirectoryRule that caused wrong characters to be included in the proxy url --- .../java/net/sf/j2ep/rules/DirectoryRule.java | 117 +++++++++--------- 1 file changed, 58 insertions(+), 59 deletions(-) diff --git a/src/main/java/net/sf/j2ep/rules/DirectoryRule.java b/src/main/java/net/sf/j2ep/rules/DirectoryRule.java index 4ccfa07..a6833de 100644 --- a/src/main/java/net/sf/j2ep/rules/DirectoryRule.java +++ b/src/main/java/net/sf/j2ep/rules/DirectoryRule.java @@ -19,122 +19,121 @@ import javax.servlet.http.HttpServletRequest; /** - * A rule that will check the start of the URI for a specifed - * starting directory/directories. If the directory is at the start this - * rule matches. The process method will then remove this directory - * from the URI, making it easy to map various servers to directory - * structures. - * If one needs some more advanced types of rewriting use the RewriteRule. - * + * A rule that will check the start of the URI for a specifed starting directory/directories. If the directory is at the + * start this rule matches. The process method will then remove this directory from the URI, making it easy to map + * various servers to directory structures. If one needs some more advanced types of rewriting use the RewriteRule. + * * @author Anders Nyman */ public class DirectoryRule extends BaseRule { - /** + /** * The directory structure. */ private String directory; - + /** * The originally set directory structure before appending and prepending slashes */ private String originalDirectory; - + private boolean isAppendTrailingSlash = true; - + /** * Decide whether a trailing slash should be appended to directory settings without a trailing slash. + * * @param isAppendTrailingSlash */ - public void setIsAppendTrailingSlash(String isAppendTrailingSlash) { - if (isAppendTrailingSlash != null && isAppendTrailingSlash.equals("false")) { - this.isAppendTrailingSlash = false; - directory = prependSlashIfNeccessary(originalDirectory); + public void setIsAppendTrailingSlash( String isAppendTrailingSlash ) { + if ( isAppendTrailingSlash != null && isAppendTrailingSlash.equals( "false" ) ) { + this.isAppendTrailingSlash = false; + directory = prependSlashIfNeccessary( originalDirectory ); } } - + /** * Return the trailing slash setting + * * @return true if slashes are appended to directory settings that do not end with a slash, false if not */ - public boolean getAppendTrailingSlash () { - return isAppendTrailingSlash; + public boolean getAppendTrailingSlash() { + return isAppendTrailingSlash; } - + /** - * Sets the directory structure that will - * be mapped to a specified server. - * - * @param directory The directory string + * Sets the directory structure that will be mapped to a specified server. + * + * @param directory + * The directory string */ - public void setDirectory(String directory) { - if (directory == null) { - throw new IllegalArgumentException( - "The directory string cannot be null."); + public void setDirectory( String directory ) { + if ( directory == null ) { + throw new IllegalArgumentException( "The directory string cannot be null." ); } else { - if (!directory.startsWith("/")) { + if ( !directory.startsWith( "/" ) ) { directory = "/" + directory; } originalDirectory = directory; - directory = prependSlashIfNeccessary(directory); - directory = appendSlashIfNeccessary(directory); + directory = prependSlashIfNeccessary( directory ); + directory = appendSlashIfNeccessary( directory ); this.directory = directory; } } - private String appendSlashIfNeccessary(String directory) { - if (!directory.endsWith(("/")) && isAppendTrailingSlash) { - directory += "/"; - } - return directory; - } + private String appendSlashIfNeccessary( String directory ) { + if ( !directory.endsWith( ( "/" ) ) && isAppendTrailingSlash ) { + directory += "/"; + } + return directory; + } - private String prependSlashIfNeccessary(String directory) { - if (!directory.startsWith("/")) { - directory = "/" + directory; - } - return directory; - } + private String prependSlashIfNeccessary( String directory ) { + if ( !directory.startsWith( "/" ) ) { + directory = "/" + directory; + } + return directory; + } /** - * Returns the directory structure that - * this rule will match on. - * + * Returns the directory structure that this rule will match on. + * * @return The directory string */ public String getDirectory() { return directory; } - + /** - * Will see if the directory for the incoming URI is the same - * as this rule is set to match on. + * Will see if the directory for the incoming URI is the same as this rule is set to match on. * * @see net.sf.j2ep.model.Rule#matches(javax.servlet.http.HttpServletRequest) */ - public boolean matches(HttpServletRequest request) { + public boolean matches( HttpServletRequest request ) { String uri = request.getServletPath(); - return (uri.startsWith(directory)); + return ( uri.startsWith( directory ) ); } - + /** * Removes the specified mapping directory from the URI. * * @see net.sf.j2ep.model.Rule#process(java.lang.String) */ - public String process(String uri) { - return uri.substring(directory.length()-1); + public String process( String uri ) { + if ( !isAppendTrailingSlash ) + return uri.substring( directory.length() ); + else + return uri.substring( directory.length() - 1 ); } - + /** - * Does the opposite of process. revert(String URI) will add the directory - * specified to the start of the incoming URI. + * Does the opposite of process. revert(String URI) will add the directory specified to the start of the incoming + * URI. * * @see net.sf.j2ep.model.Rule#revert(java.lang.String) */ - public String revert(String uri) { - if (uri.startsWith("/")) { - return directory + uri.substring(1); + public String revert( String uri ) { + if ( uri.startsWith( "/" ) ) { + return directory + uri.substring( 1 ); } else { return uri; } From be972751768e01559db68c4536f52b9b38f722d9 Mon Sep 17 00:00:00 2001 From: Dirk Stenger Date: Fri, 9 Aug 2013 11:07:46 +0200 Subject: [PATCH 11/28] Fixed logging path --- src/main/resources/logging.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/logging.properties b/src/main/resources/logging.properties index f732ee5..751b3b7 100644 --- a/src/main/resources/logging.properties +++ b/src/main/resources/logging.properties @@ -6,7 +6,7 @@ handlers = org.apache.juli.FileHandler ############################################################ org.apache.juli.FileHandler.level = FINEST -org.apache.juli.FileHandler.directory = /logs +org.apache.juli.FileHandler.directory = ${user.home}/logs org.apache.juli.FileHandler.prefix = j2ep. .level=INFO From 13c97cc80b30ba952822f6d9fca02aaaf65c4187 Mon Sep 17 00:00:00 2001 From: lat/lon Date: Mon, 12 Aug 2013 13:46:04 +0200 Subject: [PATCH 12/28] [maven-release-plugin] prepare release 1.0.0-securityproxy-master --- pom.xml | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/pom.xml b/pom.xml index c5ded0b..c6965e7 100644 --- a/pom.xml +++ b/pom.xml @@ -1,9 +1,8 @@ - + 4.0.0 de.latlon j2ep - 1.0.0-securityproxy-master-SNAPSHOT + 1.0-securityproxy-master jar j2ep Fork of jEasy Extensible Proxy - svn://svn.code.sf.net/p/j2ep/code/trunk @@ -109,7 +108,7 @@ http://localhost:8080 - + http://vento.lat-lon:9000 jdbc:postgresql://habub/sonar From 4e338ae83b9c340a0722db49a630dab6ba20e2c8 Mon Sep 17 00:00:00 2001 From: lat/lon Date: Mon, 12 Aug 2013 13:46:11 +0200 Subject: [PATCH 13/28] [maven-release-plugin] prepare for next development iteration --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index c6965e7..5e17c59 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 de.latlon j2ep - 1.0-securityproxy-master + 1.1-securityproxy-master-SNAPSHOT jar j2ep Fork of jEasy Extensible Proxy - svn://svn.code.sf.net/p/j2ep/code/trunk From 90d504f53d77fe990fe450be8d86a6cacf5efd18 Mon Sep 17 00:00:00 2001 From: aerben Date: Wed, 14 Aug 2013 10:09:43 +0200 Subject: [PATCH 14/28] Removed file log creation from apache commons logging configuration --- src/main/resources/logging.properties | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/main/resources/logging.properties b/src/main/resources/logging.properties index 751b3b7..393446e 100644 --- a/src/main/resources/logging.properties +++ b/src/main/resources/logging.properties @@ -1,14 +1,8 @@ -handlers = org.apache.juli.FileHandler - ############################################################ # Handler specific properties. # Describes specific configuration info for Handlers. ############################################################ -org.apache.juli.FileHandler.level = FINEST -org.apache.juli.FileHandler.directory = ${user.home}/logs -org.apache.juli.FileHandler.prefix = j2ep. - .level=INFO httpclient.wire.header.level=INFO From c39ee9c8833dc1902a69fbf2392a18c1bee4a6ca Mon Sep 17 00:00:00 2001 From: lat/lon Date: Thu, 15 Aug 2013 10:00:50 +0200 Subject: [PATCH 15/28] [maven-release-plugin] prepare release j2ep-1.0.1-securityproxy-master --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 5e17c59..6acc0e6 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 de.latlon j2ep - 1.1-securityproxy-master-SNAPSHOT + 1.0.1-securityproxy-master jar j2ep Fork of jEasy Extensible Proxy - svn://svn.code.sf.net/p/j2ep/code/trunk From ea8b2f1a3f750e8c1ac986dd691fc8d521e13ad6 Mon Sep 17 00:00:00 2001 From: lat/lon Date: Thu, 15 Aug 2013 10:00:56 +0200 Subject: [PATCH 16/28] [maven-release-plugin] prepare for next development iteration --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 6acc0e6..5e17c59 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 de.latlon j2ep - 1.0.1-securityproxy-master + 1.1-securityproxy-master-SNAPSHOT jar j2ep Fork of jEasy Extensible Proxy - svn://svn.code.sf.net/p/j2ep/code/trunk From 68e6b2f03eacce4063a906757ac1256fa76dfb28 Mon Sep 17 00:00:00 2001 From: Dirk Stenger Date: Tue, 28 Jan 2014 11:48:40 +0100 Subject: [PATCH 17/28] Extended j2ep to support dynamic forwarding --- src/main/java/net/sf/j2ep/ProxyFilter.java | 74 ++++++++++++++++++++-- 1 file changed, 69 insertions(+), 5 deletions(-) diff --git a/src/main/java/net/sf/j2ep/ProxyFilter.java b/src/main/java/net/sf/j2ep/ProxyFilter.java index 029d5c8..5ffd887 100644 --- a/src/main/java/net/sf/j2ep/ProxyFilter.java +++ b/src/main/java/net/sf/j2ep/ProxyFilter.java @@ -32,6 +32,8 @@ import net.sf.j2ep.model.ResponseHandler; import net.sf.j2ep.model.Server; +import net.sf.j2ep.rules.DirectoryRule; +import net.sf.j2ep.servers.BaseServer; import org.apache.commons.httpclient.*; import org.apache.commons.httpclient.cookie.CookiePolicy; import org.apache.commons.httpclient.params.HttpClientParams; @@ -58,7 +60,13 @@ public class ProxyFilter implements Filter { private static final String DEFAULT_PATH = "/WEB-INF/config/data.xml"; - /** + private static final String REQUEST_ATTRIBUTE_SERVICE_URL = "net.sf.j2ep.serviceurl"; + + private static final String BASE_SERVER_IS_REWRITING = "true"; + + private static final String DIRECTORY_RULE_IS_APPENDING_TRAILING_SLASH = "false"; + + /** * The server chain, will be traversed to find a matching server. */ private ServerChain serverChain; @@ -88,9 +96,19 @@ public void doFilter(ServletRequest request, ServletResponse response, Server server = (Server) httpRequest.getAttribute("proxyServer"); if (server == null) { - server = serverChain.evaluate(httpRequest); + Object requestAttributeServiceUrl = request.getAttribute( REQUEST_ATTRIBUTE_SERVICE_URL ); + if (requestAttributeServiceUrl != null) { + String urlWithoutProtocol = retrieveUrlWithoutProtocol( requestAttributeServiceUrl ); + int divideDomainNameAndPathIndex = urlWithoutProtocol.indexOf( "/" ); + String domainName = urlWithoutProtocol.substring( 0, divideDomainNameAndPathIndex ); + String path = retrievePath( urlWithoutProtocol, divideDomainNameAndPathIndex ); + String directoryPath = retrieveDirectoryPath( httpRequest ); + server = createServer( domainName, path, directoryPath ); + } else { + server = serverChain.evaluate(httpRequest); + } } - + if (server == null) { filterChain.doFilter(request, response); } else { @@ -128,7 +146,7 @@ public void doFilter(ServletRequest request, ServletResponse response, } } } - + /** * Will build a URI but including the Query String. That means that it really * isn't a URI, but quite near. @@ -242,4 +260,50 @@ public void destroy() { httpClient = null; serverChain = null; } -} \ No newline at end of file + + private String retrieveUrlWithoutProtocol( Object requestAttributeServiceUrl ) { + String serviceUrl = requestAttributeServiceUrl.toString(); + int protocolIndex = serviceUrl.indexOf( "//" ); + return serviceUrl.substring( protocolIndex + 2 ); + } + + private String retrievePath( String urlWithoutProtocol, int divideDomainNameAndPathIndex ) { + String path = urlWithoutProtocol.substring( divideDomainNameAndPathIndex ); + if (path.endsWith( "?" )) + return path.substring( 0, path.length() - 1 ); + else + return path; + } + + private String retrieveDirectoryPath( HttpServletRequest httpRequest ) { + String directoryPath = httpRequest.getServletPath(); + if ( directoryPath.contains("/")){ + int directoryPathIndex = directoryPath.lastIndexOf( "/" ); + return directoryPath.substring( directoryPathIndex ); + } else { + return directoryPath; + } + } + + private Server createServer( String domainName, String path, String directoryPath ) { + BaseServer baseServer = createBaseServer( domainName, path ); + DirectoryRule directoryRule = createDirectoryRule( directoryPath ); + baseServer.setRule( directoryRule ); + return baseServer; + } + + private BaseServer createBaseServer( String domainName, String path ) { + BaseServer baseServer = new BaseServer(); + baseServer.setDomainName( domainName ); + baseServer.setPath( path ); + baseServer.setIsRewriting( BASE_SERVER_IS_REWRITING ); + return baseServer; + } + + private DirectoryRule createDirectoryRule( String directoryPath ) { + DirectoryRule directoryRule = new DirectoryRule(); + directoryRule.setDirectory( directoryPath ); + directoryRule.setIsAppendTrailingSlash( DIRECTORY_RULE_IS_APPENDING_TRAILING_SLASH ); + return directoryRule; + } +} From e516bf00f111c338449fc6dd4278b2a70bda54f0 Mon Sep 17 00:00:00 2001 From: Dirk Stenger Date: Tue, 28 Jan 2014 15:10:23 +0100 Subject: [PATCH 18/28] Extracted creation of base-service from url to BaseServerFromUrlCreator --- src/main/java/net/sf/j2ep/ProxyFilter.java | 59 +------------ .../servers/BaseServerFromUrlCreator.java | 82 +++++++++++++++++++ 2 files changed, 85 insertions(+), 56 deletions(-) create mode 100644 src/main/java/net/sf/j2ep/servers/BaseServerFromUrlCreator.java diff --git a/src/main/java/net/sf/j2ep/ProxyFilter.java b/src/main/java/net/sf/j2ep/ProxyFilter.java index 5ffd887..815fbfb 100644 --- a/src/main/java/net/sf/j2ep/ProxyFilter.java +++ b/src/main/java/net/sf/j2ep/ProxyFilter.java @@ -34,6 +34,7 @@ import net.sf.j2ep.rules.DirectoryRule; import net.sf.j2ep.servers.BaseServer; +import net.sf.j2ep.servers.BaseServerFromUrlCreator; import org.apache.commons.httpclient.*; import org.apache.commons.httpclient.cookie.CookiePolicy; import org.apache.commons.httpclient.params.HttpClientParams; @@ -62,10 +63,6 @@ public class ProxyFilter implements Filter { private static final String REQUEST_ATTRIBUTE_SERVICE_URL = "net.sf.j2ep.serviceurl"; - private static final String BASE_SERVER_IS_REWRITING = "true"; - - private static final String DIRECTORY_RULE_IS_APPENDING_TRAILING_SLASH = "false"; - /** * The server chain, will be traversed to find a matching server. */ @@ -98,12 +95,8 @@ public void doFilter(ServletRequest request, ServletResponse response, if (server == null) { Object requestAttributeServiceUrl = request.getAttribute( REQUEST_ATTRIBUTE_SERVICE_URL ); if (requestAttributeServiceUrl != null) { - String urlWithoutProtocol = retrieveUrlWithoutProtocol( requestAttributeServiceUrl ); - int divideDomainNameAndPathIndex = urlWithoutProtocol.indexOf( "/" ); - String domainName = urlWithoutProtocol.substring( 0, divideDomainNameAndPathIndex ); - String path = retrievePath( urlWithoutProtocol, divideDomainNameAndPathIndex ); - String directoryPath = retrieveDirectoryPath( httpRequest ); - server = createServer( domainName, path, directoryPath ); + BaseServerFromUrlCreator baseServerCreator = new BaseServerFromUrlCreator(); + server = baseServerCreator.createServer(requestAttributeServiceUrl.toString(), request); } else { server = serverChain.evaluate(httpRequest); } @@ -260,50 +253,4 @@ public void destroy() { httpClient = null; serverChain = null; } - - private String retrieveUrlWithoutProtocol( Object requestAttributeServiceUrl ) { - String serviceUrl = requestAttributeServiceUrl.toString(); - int protocolIndex = serviceUrl.indexOf( "//" ); - return serviceUrl.substring( protocolIndex + 2 ); - } - - private String retrievePath( String urlWithoutProtocol, int divideDomainNameAndPathIndex ) { - String path = urlWithoutProtocol.substring( divideDomainNameAndPathIndex ); - if (path.endsWith( "?" )) - return path.substring( 0, path.length() - 1 ); - else - return path; - } - - private String retrieveDirectoryPath( HttpServletRequest httpRequest ) { - String directoryPath = httpRequest.getServletPath(); - if ( directoryPath.contains("/")){ - int directoryPathIndex = directoryPath.lastIndexOf( "/" ); - return directoryPath.substring( directoryPathIndex ); - } else { - return directoryPath; - } - } - - private Server createServer( String domainName, String path, String directoryPath ) { - BaseServer baseServer = createBaseServer( domainName, path ); - DirectoryRule directoryRule = createDirectoryRule( directoryPath ); - baseServer.setRule( directoryRule ); - return baseServer; - } - - private BaseServer createBaseServer( String domainName, String path ) { - BaseServer baseServer = new BaseServer(); - baseServer.setDomainName( domainName ); - baseServer.setPath( path ); - baseServer.setIsRewriting( BASE_SERVER_IS_REWRITING ); - return baseServer; - } - - private DirectoryRule createDirectoryRule( String directoryPath ) { - DirectoryRule directoryRule = new DirectoryRule(); - directoryRule.setDirectory( directoryPath ); - directoryRule.setIsAppendTrailingSlash( DIRECTORY_RULE_IS_APPENDING_TRAILING_SLASH ); - return directoryRule; - } } diff --git a/src/main/java/net/sf/j2ep/servers/BaseServerFromUrlCreator.java b/src/main/java/net/sf/j2ep/servers/BaseServerFromUrlCreator.java new file mode 100644 index 0000000..84d03e2 --- /dev/null +++ b/src/main/java/net/sf/j2ep/servers/BaseServerFromUrlCreator.java @@ -0,0 +1,82 @@ +package net.sf.j2ep.servers; + +import net.sf.j2ep.model.Server; +import net.sf.j2ep.rules.DirectoryRule; + +import javax.servlet.ServletRequest; +import javax.servlet.http.HttpServletRequest; + +/** + * Creates a BaseServer-object from passed url. + * + * @author Dirk Stenger + * @author last edited by: $Author: stenger $ + * @version $Revision: $, $Date: $ + */ +public class BaseServerFromUrlCreator { + + private static final String BASE_SERVER_IS_REWRITING = "true"; + + private static final String DIRECTORY_RULE_IS_APPENDING_TRAILING_SLASH = "false"; + + /** + * Create a BaseServer-object from url and request. + * + * @param url, never null + * @param request, never null + * @return Server + */ + public Server createServer( String url, ServletRequest request ) { + String urlWithoutProtocol = retrieveUrlWithoutProtocol( url ); + int divideDomainNameAndPathIndex = urlWithoutProtocol.indexOf( "/" ); + String domainName = urlWithoutProtocol.substring( 0, divideDomainNameAndPathIndex ); + String path = retrievePath( urlWithoutProtocol, divideDomainNameAndPathIndex ); + String directoryPath = retrieveDirectoryPath( request ); + return createServer( domainName, path, directoryPath ); + } + + private String retrieveUrlWithoutProtocol( String url ) { + int protocolIndex = url.indexOf( "//" ); + return url.substring( protocolIndex + 2 ); + } + + private String retrievePath( String urlWithoutProtocol, int divideDomainNameAndPathIndex ) { + String path = urlWithoutProtocol.substring( divideDomainNameAndPathIndex ); + if ( path.endsWith( "?" ) ) + return path.substring( 0, path.length() - 1 ); + else + return path; + } + + private String retrieveDirectoryPath( ServletRequest request ) { + String directoryPath = ( (HttpServletRequest) request ).getServletPath(); + if ( directoryPath.contains( "/" ) ) { + int directoryPathIndex = directoryPath.lastIndexOf( "/" ); + return directoryPath.substring( directoryPathIndex ); + } else { + return directoryPath; + } + } + + private Server createServer( String domainName, String path, String directoryPath ) { + BaseServer baseServer = createBaseServer( domainName, path ); + DirectoryRule directoryRule = createDirectoryRule( directoryPath ); + baseServer.setRule( directoryRule ); + return baseServer; + } + + private BaseServer createBaseServer( String domainName, String path ) { + BaseServer baseServer = new BaseServer(); + baseServer.setDomainName( domainName ); + baseServer.setPath( path ); + baseServer.setIsRewriting( BASE_SERVER_IS_REWRITING ); + return baseServer; + } + + private DirectoryRule createDirectoryRule( String directoryPath ) { + DirectoryRule directoryRule = new DirectoryRule(); + directoryRule.setDirectory( directoryPath ); + directoryRule.setIsAppendTrailingSlash( DIRECTORY_RULE_IS_APPENDING_TRAILING_SLASH ); + return directoryRule; + } +} From d4a6d11316eec2476ee8932a4b1436ddd9fc01a1 Mon Sep 17 00:00:00 2001 From: Dirk Stenger Date: Tue, 28 Jan 2014 17:00:46 +0100 Subject: [PATCH 19/28] Added tests for BaseServerFromUrlCreator and did some minor fixes in the BaseServerFromUrlCreator --- pom.xml | 6 ++ .../servers/BaseServerFromUrlCreator.java | 16 ++- .../servers/BaseServerFromUrlCreatorTest.java | 98 +++++++++++++++++++ 3 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 src/test/java/net/sf/j2ep/servers/BaseServerFromUrlCreatorTest.java diff --git a/pom.xml b/pom.xml index 5e17c59..37bceb9 100644 --- a/pom.xml +++ b/pom.xml @@ -93,6 +93,12 @@ 3.8.1 test + + org.mockito + mockito-core + 1.9.5 + test + cactus cactus diff --git a/src/main/java/net/sf/j2ep/servers/BaseServerFromUrlCreator.java b/src/main/java/net/sf/j2ep/servers/BaseServerFromUrlCreator.java index 84d03e2..9070e5d 100644 --- a/src/main/java/net/sf/j2ep/servers/BaseServerFromUrlCreator.java +++ b/src/main/java/net/sf/j2ep/servers/BaseServerFromUrlCreator.java @@ -27,6 +27,7 @@ public class BaseServerFromUrlCreator { * @return Server */ public Server createServer( String url, ServletRequest request ) { + checkRequiredParameters( url, request ); String urlWithoutProtocol = retrieveUrlWithoutProtocol( url ); int divideDomainNameAndPathIndex = urlWithoutProtocol.indexOf( "/" ); String domainName = urlWithoutProtocol.substring( 0, divideDomainNameAndPathIndex ); @@ -36,8 +37,12 @@ public Server createServer( String url, ServletRequest request ) { } private String retrieveUrlWithoutProtocol( String url ) { - int protocolIndex = url.indexOf( "//" ); - return url.substring( protocolIndex + 2 ); + if ( url.contains( "//" ) ) { + int protocolIndex = url.indexOf( "//" ); + return url.substring( protocolIndex + 2 ); + } else { + return url; + } } private String retrievePath( String urlWithoutProtocol, int divideDomainNameAndPathIndex ) { @@ -79,4 +84,11 @@ private DirectoryRule createDirectoryRule( String directoryPath ) { directoryRule.setIsAppendTrailingSlash( DIRECTORY_RULE_IS_APPENDING_TRAILING_SLASH ); return directoryRule; } + + private void checkRequiredParameters( String url, ServletRequest request ) { + if ( url == null ) + throw new IllegalArgumentException( "Url must not be null!" ); + if ( request == null ) + throw new IllegalArgumentException( "Request must not be null!" ); + } } diff --git a/src/test/java/net/sf/j2ep/servers/BaseServerFromUrlCreatorTest.java b/src/test/java/net/sf/j2ep/servers/BaseServerFromUrlCreatorTest.java new file mode 100644 index 0000000..cb7d7c4 --- /dev/null +++ b/src/test/java/net/sf/j2ep/servers/BaseServerFromUrlCreatorTest.java @@ -0,0 +1,98 @@ +package net.sf.j2ep.servers; + +import junit.framework.TestCase; +import net.sf.j2ep.model.Server; +import net.sf.j2ep.rules.DirectoryRule; + +import javax.servlet.ServletRequest; +import javax.servlet.http.HttpServletRequest; + +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; + +/** + * Tests for BaseServerFromUrlCreator. + * + * @author Dirk Stenger + * @author last edited by: $Author: stenger $ + * @version $Revision: $, $Date: $ + */ +public class BaseServerFromUrlCreatorTest extends TestCase { + + private static final String URL = "http://www.test.de/url/j2ep?"; + + private static final String URL_WITHOUT_PROTOCOL = "www.test.de/url/j2ep?"; + + private static final String URL_WITHOUT_QUESTION_MARK = "http://www.test.de/url/j2ep"; + + private static final String REQUEST_SERVLET_PATH = "/path"; + + private static final String EXPECTED_DOMAIN_NAME = "www.test.de"; + + private static final String EXPECTED_PATH = "/url/j2ep"; + + private static final String EXPECTED_DIRECTORY = "/path"; + + private final BaseServerFromUrlCreator serverCreator = new BaseServerFromUrlCreator(); + + public void testCreateServer() { + ServletRequest request = mockServletRequest(); + Server server = serverCreator.createServer( URL, request ); + + assertEquals( EXPECTED_DOMAIN_NAME, server.getDomainName() ); + assertEquals( EXPECTED_PATH, server.getPath() ); + DirectoryRule rule = (DirectoryRule) server.getRule(); + assertEquals( EXPECTED_DIRECTORY, rule.getDirectory() ); + } + + public void testCreateServerWithoutProtocol() { + ServletRequest request = mockServletRequest(); + Server server = serverCreator.createServer( URL_WITHOUT_PROTOCOL, request ); + + assertEquals( EXPECTED_DOMAIN_NAME, server.getDomainName() ); + assertEquals( EXPECTED_PATH, server.getPath() ); + DirectoryRule rule = (DirectoryRule) server.getRule(); + assertEquals( EXPECTED_DIRECTORY, rule.getDirectory() ); + } + + public void testCreateServerWithoutQuestionMark() { + ServletRequest request = mockServletRequest(); + Server server = serverCreator.createServer( URL_WITHOUT_QUESTION_MARK, request ); + + assertEquals( EXPECTED_DOMAIN_NAME, server.getDomainName() ); + assertEquals( EXPECTED_PATH, server.getPath() ); + DirectoryRule rule = (DirectoryRule) server.getRule(); + assertEquals( EXPECTED_DIRECTORY, rule.getDirectory() ); + } + + public void testCreateServerWithNullValues() { + try { + serverCreator.createServer( null, null ); + fail( "Expected an IllegalArugmentException to be thrown" ); + } catch ( IllegalArgumentException exception ) { + } + } + + public void testCreateServerWithUrlNullValue() { + ServletRequest request = mockServletRequest(); + try { + serverCreator.createServer( null, request ); + fail( "Expected an IllegalArugmentException to be thrown" ); + } catch ( IllegalArgumentException exception ) { + } + } + + public void testCreateServerWithRequestNullValue() { + try { + serverCreator.createServer( URL, null ); + fail( "Expected an IllegalArugmentException to be thrown" ); + } catch ( IllegalArgumentException exception ) { + } + } + + private ServletRequest mockServletRequest() { + HttpServletRequest request = mock( HttpServletRequest.class ); + doReturn( REQUEST_SERVLET_PATH ).when( request ).getServletPath(); + return request; + } +} From 2349aaaee2ee26afc69e16755e10111808da8b93 Mon Sep 17 00:00:00 2001 From: Dirk Stenger Date: Tue, 28 Jan 2014 17:12:19 +0100 Subject: [PATCH 20/28] Added maven-compiler-plugin to pom to ensure compiling with java 6 --- pom.xml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pom.xml b/pom.xml index 37bceb9..ca8f7f3 100644 --- a/pom.xml +++ b/pom.xml @@ -30,6 +30,15 @@ + + org.apache.maven.plugins + maven-compiler-plugin + 3.1 + + 1.6 + 1.6 + + From cf91e5477f340dd6a2dc24bd721ea9c3436dcaca Mon Sep 17 00:00:00 2001 From: lat/lon Date: Wed, 29 Jan 2014 13:56:58 +0100 Subject: [PATCH 21/28] [maven-release-plugin] prepare release j2ep-1.1-securityproxy-master --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index ca8f7f3..b63c22f 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 de.latlon j2ep - 1.1-securityproxy-master-SNAPSHOT + 1.1-securityproxy-master jar j2ep Fork of jEasy Extensible Proxy - svn://svn.code.sf.net/p/j2ep/code/trunk From f4a676fc7d7e8be62b6bf594b2a1f9479104cbc5 Mon Sep 17 00:00:00 2001 From: lat/lon Date: Wed, 29 Jan 2014 13:57:03 +0100 Subject: [PATCH 22/28] [maven-release-plugin] prepare for next development iteration --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index b63c22f..18688a9 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 de.latlon j2ep - 1.1-securityproxy-master + 1.2-securityproxy-master-SNAPSHOT jar j2ep Fork of jEasy Extensible Proxy - svn://svn.code.sf.net/p/j2ep/code/trunk From 51537408f66059edb73ee3a19d48a8b95eb0e92c Mon Sep 17 00:00:00 2001 From: Lyn Goltz Date: Thu, 24 Apr 2014 15:29:04 +0200 Subject: [PATCH 23/28] added eclipse files --- .gitignore | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index acd1ac3..de32679 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,7 @@ *.class - +.classpath +.project +.settings # Package Files # *.jar *.war From 15db37b5f2a4b7245f76f9112b270129d1bba371 Mon Sep 17 00:00:00 2001 From: Lyn Goltz Date: Thu, 24 Apr 2014 15:42:35 +0200 Subject: [PATCH 24/28] read config from classpath resource --- src/main/java/net/sf/j2ep/ConfigParser.java | 51 +++- src/main/java/net/sf/j2ep/ProxyFilter.java | 283 +++++++++++--------- 2 files changed, 203 insertions(+), 131 deletions(-) diff --git a/src/main/java/net/sf/j2ep/ConfigParser.java b/src/main/java/net/sf/j2ep/ConfigParser.java index 9aa2e31..555ee8d 100644 --- a/src/main/java/net/sf/j2ep/ConfigParser.java +++ b/src/main/java/net/sf/j2ep/ConfigParser.java @@ -17,6 +17,10 @@ package net.sf.j2ep; import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; import java.util.Iterator; import java.util.LinkedList; @@ -53,11 +57,45 @@ public class ConfigParser { * mappings included. * * @param data The config file containing the XML data structure + * @throws FileNotFoundException */ - public ConfigParser(File data) { + public ConfigParser( File data ) { + createServerChain( data ); + } + + /** + * Standard constructor only specifying the input file. The constructor will + * parse the config and build a corresponding rule chain with the server + * mappings included. + * + * @param data The config file containing the XML data structure + */ + public ConfigParser(InputStream data) { + createServerChain( data ); + } + + + private void createServerChain( File data ) { + log = LogFactory.getLog( ConfigParser.class ); + FileInputStream fileInputStream = null; + try { + fileInputStream = new FileInputStream( data ); + createServerChain( fileInputStream ); + } catch ( Exception e ) { + throw new RuntimeException( e ); + } finally { + if ( fileInputStream != null ) + try { + fileInputStream.close(); + } catch ( IOException e ) { + } + } + } + + private void createServerChain( InputStream data ) { log = LogFactory.getLog(ConfigParser.class); try { - LinkedList serverContainer = createServerList(data); + LinkedList serverContainer = createServerList(data); if (log.isDebugEnabled()) { debugServers(serverContainer); } @@ -66,7 +104,6 @@ public ConfigParser(File data) { throw new RuntimeException(e); } } - /** * Returns the parsed server chain. * @@ -81,7 +118,7 @@ public ServerChain getServerChain() { * * @return The rules all put into a rule chain */ - private LinkedList createServerList(File data) throws Exception { + private LinkedList createServerList(InputStream data) throws Exception { Digester digester = new Digester(); digester.setUseContextClassLoader(true); @@ -133,7 +170,7 @@ private LinkedList createServerList(File data) throws Exception { // Add server to list digester.addSetNext("config/cluster-server", "add"); - return (LinkedList) digester.parse(data); + return (LinkedList) digester.parse(data); } /** @@ -142,8 +179,8 @@ private LinkedList createServerList(File data) throws Exception { * * @param servers The server to debug */ - private void debugServers(LinkedList servers) { - Iterator itr = servers.iterator(); + private void debugServers(LinkedList servers) { + Iterator itr = servers.iterator(); while (itr.hasNext()) { ServerContainer container = (ServerContainer) itr.next(); diff --git a/src/main/java/net/sf/j2ep/ProxyFilter.java b/src/main/java/net/sf/j2ep/ProxyFilter.java index 815fbfb..8d627b2 100644 --- a/src/main/java/net/sf/j2ep/ProxyFilter.java +++ b/src/main/java/net/sf/j2ep/ProxyFilter.java @@ -18,9 +18,15 @@ import java.io.File; import java.io.IOException; +import java.io.InputStream; import java.net.UnknownHostException; -import javax.servlet.*; +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -31,11 +37,14 @@ import net.sf.j2ep.model.RequestHandler; import net.sf.j2ep.model.ResponseHandler; import net.sf.j2ep.model.Server; - -import net.sf.j2ep.rules.DirectoryRule; -import net.sf.j2ep.servers.BaseServer; import net.sf.j2ep.servers.BaseServerFromUrlCreator; -import org.apache.commons.httpclient.*; + +import org.apache.commons.httpclient.Header; +import org.apache.commons.httpclient.HttpClient; +import org.apache.commons.httpclient.HttpException; +import org.apache.commons.httpclient.HttpMethod; +import org.apache.commons.httpclient.HttpMethodBase; +import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; import org.apache.commons.httpclient.cookie.CookiePolicy; import org.apache.commons.httpclient.params.HttpClientParams; import org.apache.commons.logging.Log; @@ -44,14 +53,12 @@ /** * A reverse proxy using a set of Rules to identify which resource to proxy. * - * At first the rule chain is traversed trying to find a matching rule. - * When the rule is found it is given the option to rewrite the URL. - * The rewritten URL is then sent to a Server creating a Response Handler - * that can be used to process the response with streams and headers. + * At first the rule chain is traversed trying to find a matching rule. When the rule is found it is given the option to + * rewrite the URL. The rewritten URL is then sent to a Server creating a Response Handler that can be used to process + * the response with streams and headers. * - * The rules and servers are created dynamically and are specified in the - * XML data file. This allows the proxy to be easily extended by creating - * new rules and new servers. + * The rules and servers are created dynamically and are specified in the XML data file. This allows the proxy to be + * easily extended by creating new rules and new servers. * * @author Anders Nyman */ @@ -59,7 +66,7 @@ public class ProxyFilter implements Filter { private static final String CONFIG_XML = "/config.xml"; - private static final String DEFAULT_PATH = "/WEB-INF/config/data.xml"; + private static final String DEFAULT_PATH = "/WEB-INF/config/data.xml"; private static final String REQUEST_ATTRIBUTE_SERVICE_URL = "net.sf.j2ep.serviceurl"; @@ -67,73 +74,72 @@ public class ProxyFilter implements Filter { * The server chain, will be traversed to find a matching server. */ private ServerChain serverChain; - - /** + + /** * Logging element supplied by commons-logging. */ private static Log log; - - /** + + /** * The httpclient used to make all connections with, supplied by commons-httpclient. */ private HttpClient httpClient; /** - * Implementation of a reverse-proxy. All request go through here. This is - * the main class where are handling starts. + * Implementation of a reverse-proxy. All request go through here. This is the main class where are handling starts. * - * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, - * javax.servlet.ServletResponse, javax.servlet.FilterChain) + * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, + * javax.servlet.FilterChain) */ - public void doFilter(ServletRequest request, ServletResponse response, - FilterChain filterChain) throws IOException, ServletException { + public void doFilter( ServletRequest request, ServletResponse response, FilterChain filterChain ) + throws IOException, ServletException { HttpServletResponse httpResponse = (HttpServletResponse) response; HttpServletRequest httpRequest = (HttpServletRequest) request; - Server server = (Server) httpRequest.getAttribute("proxyServer"); - if (server == null) { + Server server = (Server) httpRequest.getAttribute( "proxyServer" ); + if ( server == null ) { Object requestAttributeServiceUrl = request.getAttribute( REQUEST_ATTRIBUTE_SERVICE_URL ); - if (requestAttributeServiceUrl != null) { + if ( requestAttributeServiceUrl != null ) { BaseServerFromUrlCreator baseServerCreator = new BaseServerFromUrlCreator(); - server = baseServerCreator.createServer(requestAttributeServiceUrl.toString(), request); + server = baseServerCreator.createServer( requestAttributeServiceUrl.toString(), request ); } else { - server = serverChain.evaluate(httpRequest); + server = serverChain.evaluate( httpRequest ); } } - if (server == null) { - filterChain.doFilter(request, response); + if ( server == null ) { + filterChain.doFilter( request, response ); } else { - String uri = server.getRule().process(getURI(httpRequest)); + String uri = server.getRule().process( getURI( httpRequest ) ); String url = request.getScheme() + "://" + server.getDomainName() + server.getPath() + uri; - log.debug("Connecting to " + url); - + log.debug( "Connecting to " + url ); + ResponseHandler responseHandler = null; - + try { - httpRequest = server.preExecute(httpRequest); - responseHandler = executeRequest(httpRequest, url); - httpResponse = server.postExecute(httpResponse); - - responseHandler.process(httpResponse); - } catch (HttpException e) { - log.error("Problem while connecting to server", e); - httpResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); - server.setConnectionExceptionRecieved(e); - } catch (UnknownHostException e) { - log.error("Could not connection to the host specified", e); - httpResponse.setStatus(HttpServletResponse.SC_GATEWAY_TIMEOUT); - server.setConnectionExceptionRecieved(e); - } catch (IOException e) { - log.error( "Problem probably with the input being send, either with a Header or the Stream", e); - httpResponse .setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); - } catch (MethodNotAllowedException e) { - log.error("Incoming method could not be handled", e); - httpResponse.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED); - httpResponse.setHeader("Allow", e.getAllowedMethods()); + httpRequest = server.preExecute( httpRequest ); + responseHandler = executeRequest( httpRequest, url ); + httpResponse = server.postExecute( httpResponse ); + + responseHandler.process( httpResponse ); + } catch ( HttpException e ) { + log.error( "Problem while connecting to server", e ); + httpResponse.setStatus( HttpServletResponse.SC_INTERNAL_SERVER_ERROR ); + server.setConnectionExceptionRecieved( e ); + } catch ( UnknownHostException e ) { + log.error( "Could not connection to the host specified", e ); + httpResponse.setStatus( HttpServletResponse.SC_GATEWAY_TIMEOUT ); + server.setConnectionExceptionRecieved( e ); + } catch ( IOException e ) { + log.error( "Problem probably with the input being send, either with a Header or the Stream", e ); + httpResponse.setStatus( HttpServletResponse.SC_INTERNAL_SERVER_ERROR ); + } catch ( MethodNotAllowedException e ) { + log.error( "Incoming method could not be handled", e ); + httpResponse.setStatus( HttpServletResponse.SC_METHOD_NOT_ALLOWED ); + httpResponse.setHeader( "Allow", e.getAllowedMethods() ); } finally { - if (responseHandler != null) { + if ( responseHandler != null ) { responseHandler.close(); } } @@ -141,110 +147,139 @@ public void doFilter(ServletRequest request, ServletResponse response, } /** - * Will build a URI but including the Query String. That means that it really - * isn't a URI, but quite near. + * Will build a URI but including the Query String. That means that it really isn't a URI, but quite near. * - * @param httpRequest Request to get the URI and query string from + * @param httpRequest + * Request to get the URI and query string from * @return The URI for this request including the query string */ - private String getURI(HttpServletRequest httpRequest) { + private String getURI( HttpServletRequest httpRequest ) { String contextPath = httpRequest.getContextPath(); - String uri = httpRequest.getRequestURI().substring(contextPath.length()); - if (httpRequest.getQueryString() != null) { + String uri = httpRequest.getRequestURI().substring( contextPath.length() ); + if ( httpRequest.getQueryString() != null ) { uri += "?" + httpRequest.getQueryString(); } return uri; } /** - * Will create the method and execute it. After this the method - * is sent to a ResponseHandler that is returned. + * Will create the method and execute it. After this the method is sent to a ResponseHandler that is returned. * - * @param httpRequest Request we are receiving from the client - * @param url The location we are proxying to + * @param httpRequest + * Request we are receiving from the client + * @param url + * The location we are proxying to * @return A ResponseHandler that can be used to write the response - * @throws MethodNotAllowedException If the method specified by the request isn't handled - * @throws IOException When there is a problem with the streams - * @throws HttpException The httpclient can throw HttpExcetion when executing the method + * @throws MethodNotAllowedException + * If the method specified by the request isn't handled + * @throws IOException + * When there is a problem with the streams + * @throws HttpException + * The httpclient can throw HttpExcetion when executing the method */ - private ResponseHandler executeRequest(HttpServletRequest httpRequest, - String url) throws MethodNotAllowedException, IOException, - HttpException { - RequestHandler requestHandler = RequestHandlerFactory - .createRequestMethod(httpRequest.getMethod()); + private ResponseHandler executeRequest( HttpServletRequest httpRequest, String url ) + throws MethodNotAllowedException, IOException, HttpException { + RequestHandler requestHandler = RequestHandlerFactory.createRequestMethod( httpRequest.getMethod() ); - HttpMethod method = requestHandler.process(httpRequest, url); - method.setFollowRedirects(false); + HttpMethod method = requestHandler.process( httpRequest, url ); + method.setFollowRedirects( false ); /* - * Why does method.validate() return true when the method has been - * aborted? I mean, if validate returns true the API says that means - * that the method is ready to be executed. - * TODO I don't like doing type casting here, see above. + * Why does method.validate() return true when the method has been aborted? I mean, if validate returns true the + * API says that means that the method is ready to be executed. TODO I don't like doing type casting here, see + * above. */ - if (!((HttpMethodBase) method).isAborted()) { - httpClient.executeMethod(method); + if ( !( (HttpMethodBase) method ).isAborted() ) { + httpClient.executeMethod( method ); - if (method.getStatusCode() == 405) { - Header allow = method.getResponseHeader("allow"); + if ( method.getStatusCode() == 405 ) { + Header allow = method.getResponseHeader( "allow" ); String value = allow.getValue(); - throw new MethodNotAllowedException( - "Status code 405 from server", AllowedMethodHandler - .processAllowHeader(value)); + throw new MethodNotAllowedException( "Status code 405 from server", + AllowedMethodHandler.processAllowHeader( value ) ); } } - return ResponseHandlerFactory.createResponseHandler(method); + return ResponseHandlerFactory.createResponseHandler( method ); } /** - * Called upon initialization, Will create the ConfigParser and get the - * RuleChain back. Will also configure the httpclient. + * Called upon initialization, Will create the ConfigParser and get the RuleChain back. Will also configure the + * httpclient. * * @see javax.servlet.Filter#init(javax.servlet.FilterConfig) */ - public void init(FilterConfig filterConfig) throws ServletException { - log = LogFactory.getLog(ProxyFilter.class); - AllowedMethodHandler.setAllowedMethods("OPTIONS,GET,HEAD,POST,PUT,DELETE,TRACE"); - - httpClient = new HttpClient(new MultiThreadedHttpConnectionManager()); - httpClient.getParams().setBooleanParameter(HttpClientParams.USE_EXPECT_CONTINUE, false); - httpClient.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES); - - String proxyConfigEnv = filterConfig.getInitParameter("proxyConfigEnv"); - if (proxyConfigEnv == null) { - serverChain = null; + public void init( FilterConfig filterConfig ) + throws ServletException { + log = LogFactory.getLog( ProxyFilter.class ); + AllowedMethodHandler.setAllowedMethods( "OPTIONS,GET,HEAD,POST,PUT,DELETE,TRACE" ); + + httpClient = new HttpClient( new MultiThreadedHttpConnectionManager() ); + httpClient.getParams().setBooleanParameter( HttpClientParams.USE_EXPECT_CONTINUE, false ); + httpClient.getParams().setCookiePolicy( CookiePolicy.IGNORE_COOKIES ); + + String proxyConfigFile = filterConfig.getInitParameter( "proxyConfigFile" ); + String proxyConfigEnv = filterConfig.getInitParameter( "proxyConfigEnv" ); + if ( proxyConfigFile != null ) { + parseConfigFromFileInClasspath( proxyConfigFile ); + } else if ( proxyConfigEnv != null ) { + parseConfigFileFromSystemEnvDirectory( proxyConfigEnv ); } else { - try { - String path = buildConfigFilePath(proxyConfigEnv); - processConfiguration(path); - } catch (Exception e) { - try { - String path = filterConfig.getServletContext().getRealPath(DEFAULT_PATH); - processConfiguration(path); - } catch (Exception e1) { - throw new ServletException(e); - } - } + parseConfigFileFromDefaultPath( filterConfig ); } } - private void processConfiguration(String path) { - File dataFile = new File(path); - ConfigParser parser = new ConfigParser(dataFile); - serverChain = parser.getServerChain(); - } + private void parseConfigFromFileInClasspath( String proxyConfigFile ) + throws ServletException { + try { + InputStream resourceAsStream = ProxyFilter.class.getResourceAsStream( proxyConfigFile ); + ConfigParser parser = new ConfigParser( resourceAsStream ); + serverChain = parser.getServerChain(); + } catch ( Exception e ) { + log.error( "j2ep configuration could not be read from classpath resource!", e ); + throw new ServletException( e ); + } + } + + private void parseConfigFileFromSystemEnvDirectory( String proxyConfigEnv ) + throws ServletException { + try { + String path = buildConfigFilePath( proxyConfigEnv ); + processConfiguration( path ); + } catch ( Exception e ) { + log.error( "j2ep configuration could not be read from system environment variable!", e ); + throw new ServletException( e ); + } + } - private String buildConfigFilePath(String proxyConfigEnv) { - String path = System.getenv(proxyConfigEnv); - if (!path.endsWith("/")) path += "/"; - path += CONFIG_XML; - return path; - } + private void parseConfigFileFromDefaultPath( FilterConfig filterConfig ) + throws ServletException { + String path = null; + try { + path = filterConfig.getServletContext().getRealPath( DEFAULT_PATH ); + processConfiguration( path ); + } catch ( Exception e ) { + log.error( "j2ep configuration could not be read from default path " + path, e ); + throw new ServletException( e ); + } + } + + private void processConfiguration( String path ) { + File dataFile = new File( path ); + ConfigParser parser = new ConfigParser( dataFile ); + serverChain = parser.getServerChain(); + } + + private String buildConfigFilePath( String proxyConfigEnv ) { + String path = System.getenv( proxyConfigEnv ); + if ( !path.endsWith( "/" ) ) + path += "/"; + path += CONFIG_XML; + return path; + } /** - * Called when this filter is destroyed. - * Releases the fields. + * Called when this filter is destroyed. Releases the fields. * * @see javax.servlet.Filter#destroy() */ From 618a4b35fe4f90a63b9d49d9f7da38b3eb6d0364 Mon Sep 17 00:00:00 2001 From: Lyn Goltz Date: Thu, 24 Apr 2014 15:45:09 +0200 Subject: [PATCH 25/28] renamed init param --- src/main/java/net/sf/j2ep/ProxyFilter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/net/sf/j2ep/ProxyFilter.java b/src/main/java/net/sf/j2ep/ProxyFilter.java index 8d627b2..964bbed 100644 --- a/src/main/java/net/sf/j2ep/ProxyFilter.java +++ b/src/main/java/net/sf/j2ep/ProxyFilter.java @@ -218,7 +218,7 @@ public void init( FilterConfig filterConfig ) httpClient.getParams().setBooleanParameter( HttpClientParams.USE_EXPECT_CONTINUE, false ); httpClient.getParams().setCookiePolicy( CookiePolicy.IGNORE_COOKIES ); - String proxyConfigFile = filterConfig.getInitParameter( "proxyConfigFile" ); + String proxyConfigFile = filterConfig.getInitParameter( "proxyConfigResource" ); String proxyConfigEnv = filterConfig.getInitParameter( "proxyConfigEnv" ); if ( proxyConfigFile != null ) { parseConfigFromFileInClasspath( proxyConfigFile ); From a8e45622a9f1f604d11f4eaa2b534c4b07484ad0 Mon Sep 17 00:00:00 2001 From: lat/lon Date: Wed, 7 May 2014 11:18:31 +0200 Subject: [PATCH 26/28] [maven-release-plugin] prepare release j2ep-1.2-securityproxy-master --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 18688a9..c92e5f6 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 de.latlon j2ep - 1.2-securityproxy-master-SNAPSHOT + 1.2-securityproxy-master jar j2ep Fork of jEasy Extensible Proxy - svn://svn.code.sf.net/p/j2ep/code/trunk From 3c9e7d714e64d22ad3c31e77a21071db9501f856 Mon Sep 17 00:00:00 2001 From: lat/lon Date: Wed, 7 May 2014 11:18:36 +0200 Subject: [PATCH 27/28] [maven-release-plugin] prepare for next development iteration --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index c92e5f6..232a081 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 de.latlon j2ep - 1.2-securityproxy-master + 1.3-securityproxy-master-SNAPSHOT jar j2ep Fork of jEasy Extensible Proxy - svn://svn.code.sf.net/p/j2ep/code/trunk From 657c92959d74b09761fac298497e2738a4ef0307 Mon Sep 17 00:00:00 2001 From: Torsten Friebe Date: Tue, 26 Feb 2019 21:08:16 +0100 Subject: [PATCH 28/28] Updated plugin and various dependencies to run on Java 8 and resolve owasp vulnerability findings --- pom.xml | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/pom.xml b/pom.xml index 232a081..506fbb0 100644 --- a/pom.xml +++ b/pom.xml @@ -2,8 +2,8 @@ 4.0.0 de.latlon j2ep - 1.3-securityproxy-master-SNAPSHOT jar + 1.2-securityproxy-master j2ep Fork of jEasy Extensible Proxy - svn://svn.code.sf.net/p/j2ep/code/trunk @@ -16,7 +16,7 @@ org.apache.maven.plugins maven-surefire-plugin - 2.15 + 2.22.1 **/PostTest* @@ -33,20 +33,19 @@ org.apache.maven.plugins maven-compiler-plugin - 3.1 + 3.8.0 - 1.6 - 1.6 + 1.8 + 1.8 - commons-digester commons-digester - 1.7 + 1.8.1 commons-logging @@ -57,12 +56,12 @@ commons-codec commons-codec - 1.3 + 1.11 commons-beanutils commons-beanutils - 1.5 + 1.9.3 commons-logging @@ -88,18 +87,18 @@ commons-logging commons-logging - 1.0.3 + 1.1.3 javax.servlet servlet-api - 2.4 + 2.5 provided junit junit - 3.8.1 + 3.8.2 test @@ -115,18 +114,15 @@ test - aspectj + org.aspectj aspectjrt - 1.2.1 + 1.5.4 test http://localhost:8080 - - - http://vento.lat-lon:9000 - jdbc:postgresql://habub/sonar +