From d6f1f08532e8f4d9f7738ae4fb4b70367ce47fc4 Mon Sep 17 00:00:00 2001 From: Torsten Date: Mon, 5 Aug 2013 23:03:33 +0200 Subject: [PATCH] fix path structure --- j2ep_git/.gitignore | 1 - j2ep_git/LICENSE | 176 -------- j2ep_git/build.properties.default | 21 - j2ep_git/build.xml | 425 ------------------ j2ep_git/pom.xml | 93 ---- .../main/java/net/sf/j2ep/ConfigParser.java | 153 ------- .../main/java/net/sf/j2ep/ProxyFilter.java | 224 --------- .../main/java/net/sf/j2ep/RewriteFilter.java | 130 ------ .../main/java/net/sf/j2ep/ServerChain.java | 165 ------- .../net/sf/j2ep/UrlRewritingOutputStream.java | 227 ---------- .../sf/j2ep/UrlRewritingResponseWrapper.java | 271 ----------- .../factories/MethodNotAllowedException.java | 56 --- .../j2ep/factories/RequestHandlerFactory.java | 89 ---- .../factories/ResponseHandlerFactory.java | 78 ---- .../sf/j2ep/model/AllowedMethodHandler.java | 111 ----- .../net/sf/j2ep/model/RequestHandler.java | 45 -- .../net/sf/j2ep/model/ResponseHandler.java | 53 --- .../src/main/java/net/sf/j2ep/model/Rule.java | 68 --- .../main/java/net/sf/j2ep/model/Server.java | 87 ---- .../net/sf/j2ep/model/ServerContainer.java | 71 --- .../requesthandlers/BasicRequestHandler.java | 63 --- .../EntityEnclosingRequestHandler.java | 66 --- .../MaxForwardRequestHandler.java | 94 ---- .../requesthandlers/RequestHandlerBase.java | 151 ------- .../BasicResponseHandler.java | 56 --- .../DeleteResponseHandler.java | 38 -- .../responsehandlers/GetResponseHandler.java | 38 -- .../responsehandlers/HeadResponseHandler.java | 51 --- .../OptionsResponseHandler.java | 110 ----- .../responsehandlers/PostResponseHandler.java | 38 -- .../responsehandlers/PutResponseHandler.java | 37 -- .../responsehandlers/ResponseHandlerBase.java | 147 ------ .../TraceResponseHandler.java | 97 ---- .../sf/j2ep/rules/AcceptEverythingRule.java | 41 -- .../main/java/net/sf/j2ep/rules/BaseRule.java | 63 --- .../java/net/sf/j2ep/rules/CompositeRule.java | 135 ------ .../java/net/sf/j2ep/rules/DirectoryRule.java | 103 ----- .../main/java/net/sf/j2ep/rules/IPRule.java | 139 ------ .../java/net/sf/j2ep/rules/RewriteRule.java | 192 -------- .../main/java/net/sf/j2ep/rules/TimeRule.java | 87 ---- .../java/net/sf/j2ep/servers/BaseServer.java | 156 ------- .../net/sf/j2ep/servers/ClusterContainer.java | 321 ------------- .../j2ep/servers/ClusterRequestWrapper.java | 108 ----- .../j2ep/servers/ClusterResponseWrapper.java | 112 ----- .../sf/j2ep/servers/RoundRobinCluster.java | 78 ---- .../sf/j2ep/servers/ServerContainerBase.java | 49 -- .../sf/j2ep/servers/ServerStatusChecker.java | 200 --------- .../sf/j2ep/servers/ServerStatusListener.java | 49 -- .../src/main/webapp/WEB-INF/cactus-web.xml | 23 - .../src/main/webapp/WEB-INF/config/data.xml | 20 - j2ep_git/src/main/webapp/WEB-INF/web.xml | 27 -- .../src/main/webapp/WEB-INF/web_rewriting.xml | 38 -- .../net/sf/j2ep/test/AllowHeaderTest.java | 54 --- .../java/net/sf/j2ep/test/ClusterTest.java | 111 ----- .../net/sf/j2ep/test/CompositeRuleTest.java | 78 ---- .../test/java/net/sf/j2ep/test/CycleTest.java | 56 --- .../sf/j2ep/test/DirectoryMappingTest.java | 121 ----- .../net/sf/j2ep/test/DirectoryRuleTest.java | 115 ----- .../test/java/net/sf/j2ep/test/GetTest.java | 159 ------- .../java/net/sf/j2ep/test/IPRuleTest.java | 151 ------- .../sf/j2ep/test/MethodWrappingRequest.java | 34 -- .../sf/j2ep/test/MockHttpServletRequest.java | 253 ----------- .../java/net/sf/j2ep/test/OptionTest.java | 118 ----- .../src/test/java/net/sf/j2ep/test/POSTdata | 1 - .../test/java/net/sf/j2ep/test/PostTest.java | 98 ---- .../net/sf/j2ep/test/RewriteRuleTest.java | 80 ---- .../net/sf/j2ep/test/StatusCheckerTest.java | 118 ----- .../java/net/sf/j2ep/test/TimeRuleTest.java | 64 --- .../test/java/net/sf/j2ep/test/TraceTest.java | 97 ---- .../test/java/net/sf/j2ep/test/testData.xml | 96 ---- 70 files changed, 7275 deletions(-) delete mode 100644 j2ep_git/.gitignore delete mode 100644 j2ep_git/LICENSE delete mode 100644 j2ep_git/build.properties.default delete mode 100644 j2ep_git/build.xml delete mode 100644 j2ep_git/pom.xml delete mode 100644 j2ep_git/src/main/java/net/sf/j2ep/ConfigParser.java delete mode 100644 j2ep_git/src/main/java/net/sf/j2ep/ProxyFilter.java delete mode 100644 j2ep_git/src/main/java/net/sf/j2ep/RewriteFilter.java delete mode 100644 j2ep_git/src/main/java/net/sf/j2ep/ServerChain.java delete mode 100644 j2ep_git/src/main/java/net/sf/j2ep/UrlRewritingOutputStream.java delete mode 100644 j2ep_git/src/main/java/net/sf/j2ep/UrlRewritingResponseWrapper.java delete mode 100644 j2ep_git/src/main/java/net/sf/j2ep/factories/MethodNotAllowedException.java delete mode 100644 j2ep_git/src/main/java/net/sf/j2ep/factories/RequestHandlerFactory.java delete mode 100644 j2ep_git/src/main/java/net/sf/j2ep/factories/ResponseHandlerFactory.java delete mode 100644 j2ep_git/src/main/java/net/sf/j2ep/model/AllowedMethodHandler.java delete mode 100644 j2ep_git/src/main/java/net/sf/j2ep/model/RequestHandler.java delete mode 100644 j2ep_git/src/main/java/net/sf/j2ep/model/ResponseHandler.java delete mode 100644 j2ep_git/src/main/java/net/sf/j2ep/model/Rule.java delete mode 100644 j2ep_git/src/main/java/net/sf/j2ep/model/Server.java delete mode 100644 j2ep_git/src/main/java/net/sf/j2ep/model/ServerContainer.java delete mode 100644 j2ep_git/src/main/java/net/sf/j2ep/requesthandlers/BasicRequestHandler.java delete mode 100644 j2ep_git/src/main/java/net/sf/j2ep/requesthandlers/EntityEnclosingRequestHandler.java delete mode 100644 j2ep_git/src/main/java/net/sf/j2ep/requesthandlers/MaxForwardRequestHandler.java delete mode 100644 j2ep_git/src/main/java/net/sf/j2ep/requesthandlers/RequestHandlerBase.java delete mode 100644 j2ep_git/src/main/java/net/sf/j2ep/responsehandlers/BasicResponseHandler.java delete mode 100644 j2ep_git/src/main/java/net/sf/j2ep/responsehandlers/DeleteResponseHandler.java delete mode 100644 j2ep_git/src/main/java/net/sf/j2ep/responsehandlers/GetResponseHandler.java delete mode 100644 j2ep_git/src/main/java/net/sf/j2ep/responsehandlers/HeadResponseHandler.java delete mode 100644 j2ep_git/src/main/java/net/sf/j2ep/responsehandlers/OptionsResponseHandler.java delete mode 100644 j2ep_git/src/main/java/net/sf/j2ep/responsehandlers/PostResponseHandler.java delete mode 100644 j2ep_git/src/main/java/net/sf/j2ep/responsehandlers/PutResponseHandler.java delete mode 100644 j2ep_git/src/main/java/net/sf/j2ep/responsehandlers/ResponseHandlerBase.java delete mode 100644 j2ep_git/src/main/java/net/sf/j2ep/responsehandlers/TraceResponseHandler.java delete mode 100644 j2ep_git/src/main/java/net/sf/j2ep/rules/AcceptEverythingRule.java delete mode 100644 j2ep_git/src/main/java/net/sf/j2ep/rules/BaseRule.java delete mode 100644 j2ep_git/src/main/java/net/sf/j2ep/rules/CompositeRule.java delete mode 100644 j2ep_git/src/main/java/net/sf/j2ep/rules/DirectoryRule.java delete mode 100644 j2ep_git/src/main/java/net/sf/j2ep/rules/IPRule.java delete mode 100644 j2ep_git/src/main/java/net/sf/j2ep/rules/RewriteRule.java delete mode 100644 j2ep_git/src/main/java/net/sf/j2ep/rules/TimeRule.java delete mode 100644 j2ep_git/src/main/java/net/sf/j2ep/servers/BaseServer.java delete mode 100644 j2ep_git/src/main/java/net/sf/j2ep/servers/ClusterContainer.java delete mode 100644 j2ep_git/src/main/java/net/sf/j2ep/servers/ClusterRequestWrapper.java delete mode 100644 j2ep_git/src/main/java/net/sf/j2ep/servers/ClusterResponseWrapper.java delete mode 100644 j2ep_git/src/main/java/net/sf/j2ep/servers/RoundRobinCluster.java delete mode 100644 j2ep_git/src/main/java/net/sf/j2ep/servers/ServerContainerBase.java delete mode 100644 j2ep_git/src/main/java/net/sf/j2ep/servers/ServerStatusChecker.java delete mode 100644 j2ep_git/src/main/java/net/sf/j2ep/servers/ServerStatusListener.java delete mode 100644 j2ep_git/src/main/webapp/WEB-INF/cactus-web.xml delete mode 100644 j2ep_git/src/main/webapp/WEB-INF/config/data.xml delete mode 100644 j2ep_git/src/main/webapp/WEB-INF/web.xml delete mode 100644 j2ep_git/src/main/webapp/WEB-INF/web_rewriting.xml delete mode 100644 j2ep_git/src/test/java/net/sf/j2ep/test/AllowHeaderTest.java delete mode 100644 j2ep_git/src/test/java/net/sf/j2ep/test/ClusterTest.java delete mode 100644 j2ep_git/src/test/java/net/sf/j2ep/test/CompositeRuleTest.java delete mode 100644 j2ep_git/src/test/java/net/sf/j2ep/test/CycleTest.java delete mode 100644 j2ep_git/src/test/java/net/sf/j2ep/test/DirectoryMappingTest.java delete mode 100644 j2ep_git/src/test/java/net/sf/j2ep/test/DirectoryRuleTest.java delete mode 100644 j2ep_git/src/test/java/net/sf/j2ep/test/GetTest.java delete mode 100644 j2ep_git/src/test/java/net/sf/j2ep/test/IPRuleTest.java delete mode 100644 j2ep_git/src/test/java/net/sf/j2ep/test/MethodWrappingRequest.java delete mode 100644 j2ep_git/src/test/java/net/sf/j2ep/test/MockHttpServletRequest.java delete mode 100644 j2ep_git/src/test/java/net/sf/j2ep/test/OptionTest.java delete mode 100644 j2ep_git/src/test/java/net/sf/j2ep/test/POSTdata delete mode 100644 j2ep_git/src/test/java/net/sf/j2ep/test/PostTest.java delete mode 100644 j2ep_git/src/test/java/net/sf/j2ep/test/RewriteRuleTest.java delete mode 100644 j2ep_git/src/test/java/net/sf/j2ep/test/StatusCheckerTest.java delete mode 100644 j2ep_git/src/test/java/net/sf/j2ep/test/TimeRuleTest.java delete mode 100644 j2ep_git/src/test/java/net/sf/j2ep/test/TraceTest.java delete mode 100644 j2ep_git/src/test/java/net/sf/j2ep/test/testData.xml diff --git a/j2ep_git/.gitignore b/j2ep_git/.gitignore deleted file mode 100644 index ea8c4bf..0000000 --- a/j2ep_git/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/target diff --git a/j2ep_git/LICENSE b/j2ep_git/LICENSE deleted file mode 100644 index d9a10c0..0000000 --- a/j2ep_git/LICENSE +++ /dev/null @@ -1,176 +0,0 @@ - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS diff --git a/j2ep_git/build.properties.default b/j2ep_git/build.properties.default deleted file mode 100644 index dc9287c..0000000 --- a/j2ep_git/build.properties.default +++ /dev/null @@ -1,21 +0,0 @@ -#The version -app.version=custom - -# Tomcat specifics -catalina.home= installation dir for tomcat -manager.username= username that can use the manager -manager.password= password for user above - -#Directory having the libs that will be copied to build -#Change this to something that suits you and run the -#download task before running anything else. -external.libs=${catalina.home}/webapps/j2ep/lib/ - -#Here are the libs downloaded from -download.libs=http://j2ep.sourceforge.net/lib - -#server -server.url = http://localhost:8080 - - - diff --git a/j2ep_git/build.xml b/j2ep_git/build.xml deleted file mode 100644 index 8d32f1c..0000000 --- a/j2ep_git/build.xml +++ /dev/null @@ -1,425 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/j2ep_git/pom.xml b/j2ep_git/pom.xml deleted file mode 100644 index 7a9abd8..0000000 --- a/j2ep_git/pom.xml +++ /dev/null @@ -1,93 +0,0 @@ - - 4.0.0 - de.latlon - j2ep - 1.0.0-SNAPSHOT - war - j2ep - Fork of jEasy Extensible Proxy - svn://svn.code.sf.net/p/j2ep/code/trunk - - - - cactus - cactus-maven - 1.7.2 - - - org.apache.maven.plugins - maven-surefire-plugin - 2.15 - - - **/PostTest* - **/TraceTest* - **/DirectoryMappingTest* - **/ClusterTest* - **/CycleTest* - **/GetTest* - **/OptionTest* - **/StatusCheckerTest* - - - - - - - - - commons-digester - commons-digester - 1.7 - - - commons-codec - commons-codec - 1.3 - - - commons-beanutils - commons-beanutils - 1.5 - - - commons-httpclient - commons-httpclient - 3.0.1 - - - junit - junit - - - - - javax.servlet - servlet-api - 2.4 - provided - - - junit - junit - 3.8.1 - test - - - cactus - cactus - 13-1.7.2 - test - - - aspectj - aspectjrt - 1.2.1 - test - - - - http://localhost:8080 - - - \ No newline at end of file diff --git a/j2ep_git/src/main/java/net/sf/j2ep/ConfigParser.java b/j2ep_git/src/main/java/net/sf/j2ep/ConfigParser.java deleted file mode 100644 index 9aa2e31..0000000 --- a/j2ep_git/src/main/java/net/sf/j2ep/ConfigParser.java +++ /dev/null @@ -1,153 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep; - -import java.io.File; -import java.util.Iterator; -import java.util.LinkedList; - -import net.sf.j2ep.model.ServerContainer; - -import org.apache.commons.digester.Digester; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -/** - * The config parser uses Digester to parse the config file. A rule chain with - * links to the servers will be constructed. - * - * Based on the work by Yoav Shapira for the balancer webapp distributed with - * Tomcat. - * - * @author Anders Nyman, Yoav Shapira - */ -public class ConfigParser { - - /** - * The resulting server chain. - */ - private ServerChain serverChain; - - /** - * A logging instance supplied by commons-logging. - */ - private static Log log; - - /** - * 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(File data) { - log = LogFactory.getLog(ConfigParser.class); - try { - LinkedList serverContainer = createServerList(data); - if (log.isDebugEnabled()) { - debugServers(serverContainer); - } - serverChain = new ServerChain(serverContainer); - } catch (Exception e) { - throw new RuntimeException(e); - } - } - - /** - * Returns the parsed server chain. - * - * @return The resulting ServerChain - */ - public ServerChain getServerChain() { - return serverChain; - } - - /** - * Creates the rules. - * - * @return The rules all put into a rule chain - */ - private LinkedList createServerList(File data) throws Exception { - Digester digester = new Digester(); - digester.setUseContextClassLoader(true); - - // Construct server list - digester.addObjectCreate("config", LinkedList.class); - - // Create servers - digester.addObjectCreate("config/server", null, "className"); - digester.addSetProperties("config/server"); - // Create rule - digester.addObjectCreate("config/server/rule", null, "className"); - digester.addSetProperties("config/server/rule"); - digester.addSetNext("config/server/rule", "setRule"); - // Create composite rule - digester.addObjectCreate("config/server/composite-rule", null, - "className"); - digester.addSetProperties("config/server/composite-rule"); - digester.addObjectCreate("config/server/composite-rule/rule", null, - "className"); - digester.addSetProperties("config/server/composite-rule/rule"); - digester.addSetNext("config/server/composite-rule/rule", "addRule"); - digester.addSetNext("config/server/composite-rule", "setRule"); - // Add server to list - digester.addSetNext("config/server", "add"); - - // Create cluster servers - digester.addObjectCreate("config/cluster-server", null, "className"); - digester.addSetProperties("config/cluster-server"); - // Create the servers in this cluster - digester.addCallMethod("config/cluster-server/server", "addServer", 2); - digester.addCallParam("config/cluster-server/server", 0, "domainName"); - digester.addCallParam("config/cluster-server/server", 1, "path"); - // Create rule - digester.addObjectCreate("config/cluster-server/rule", null, - "className"); - digester.addSetProperties("config/cluster-server/rule"); - digester.addSetNext("config/cluster-server/rule", "setRule"); - // Create composite rule - digester.addObjectCreate("config/cluster-server/composite-rule", null, - "className"); - digester.addSetProperties("config/cluster-server/composite-rule"); - digester.addObjectCreate("config/cluster-server/composite-rule/rule", - null, "className"); - digester.addSetProperties("config/cluster-server/composite-rule/rule"); - digester.addSetNext("config/cluster-server/composite-rule/rule", - "addRule"); - digester.addSetNext("config/cluster-server/composite-rule", "setRule"); - - // Add server to list - digester.addSetNext("config/cluster-server", "add"); - - return (LinkedList) digester.parse(data); - } - - /** - * Will iterate over the server and print out the mappings between servers - * and rules. - * - * @param servers The server to debug - */ - private void debugServers(LinkedList servers) { - Iterator itr = servers.iterator(); - - while (itr.hasNext()) { - ServerContainer container = (ServerContainer) itr.next(); - log.debug(container + " mapped to --> " + container.getRule()); - } - } -} diff --git a/j2ep_git/src/main/java/net/sf/j2ep/ProxyFilter.java b/j2ep_git/src/main/java/net/sf/j2ep/ProxyFilter.java deleted file mode 100644 index 8d57642..0000000 --- a/j2ep_git/src/main/java/net/sf/j2ep/ProxyFilter.java +++ /dev/null @@ -1,224 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep; - -import java.io.File; -import java.io.IOException; -import java.net.UnknownHostException; - -import javax.servlet.*; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import net.sf.j2ep.factories.MethodNotAllowedException; -import net.sf.j2ep.factories.RequestHandlerFactory; -import net.sf.j2ep.factories.ResponseHandlerFactory; -import net.sf.j2ep.model.AllowedMethodHandler; -import net.sf.j2ep.model.RequestHandler; -import net.sf.j2ep.model.ResponseHandler; -import net.sf.j2ep.model.Server; - -import org.apache.commons.httpclient.*; -import org.apache.commons.httpclient.cookie.CookiePolicy; -import org.apache.commons.httpclient.params.HttpClientParams; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -/** - * 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. - * - * 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 - */ -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. - * - * @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 { - - HttpServletResponse httpResponse = (HttpServletResponse) response; - HttpServletRequest httpRequest = (HttpServletRequest) request; - - Server server = (Server) httpRequest.getAttribute("proxyServer"); - if (server == null) { - server = serverChain.evaluate(httpRequest); - } - - if (server == null) { - filterChain.doFilter(request, response); - } else { - String uri = server.getRule().process(getURI(httpRequest)); - String url = request.getScheme() + "://" + server.getDomainName() + server.getPath() + uri; - 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()); - } finally { - if (responseHandler != null) { - responseHandler.close(); - } - } - } - } - - /** - * 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 - * @return The URI for this request including the query string - */ - private String getURI(HttpServletRequest httpRequest) { - String contextPath = httpRequest.getContextPath(); - 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. - * - * @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 - */ - 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); - - /* - * 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 (method.getStatusCode() == 405) { - Header allow = method.getResponseHeader("allow"); - String value = allow.getValue(); - throw new MethodNotAllowedException( - "Status code 405 from server", AllowedMethodHandler - .processAllowHeader(value)); - } - } - - return ResponseHandlerFactory.createResponseHandler(method); - } - - /** - * 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 data = filterConfig.getInitParameter("dataUrl"); - if (data == null) { - serverChain = null; - } else { - try { - File dataFile = new File(filterConfig.getServletContext().getRealPath(data)); - ConfigParser parser = new ConfigParser(dataFile); - serverChain = parser.getServerChain(); - } catch (Exception e) { - throw new ServletException(e); - } - } - } - - /** - * Called when this filter is destroyed. - * Releases the fields. - * - * @see javax.servlet.Filter#destroy() - */ - public void destroy() { - log = null; - httpClient = null; - serverChain = null; - } -} \ No newline at end of file diff --git a/j2ep_git/src/main/java/net/sf/j2ep/RewriteFilter.java b/j2ep_git/src/main/java/net/sf/j2ep/RewriteFilter.java deleted file mode 100644 index 610ebc5..0000000 --- a/j2ep_git/src/main/java/net/sf/j2ep/RewriteFilter.java +++ /dev/null @@ -1,130 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep; - -import java.io.File; -import java.io.IOException; - -import javax.servlet.*; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import net.sf.j2ep.model.Server; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -/** - * A filter that will locate the appropriate Rule - * and use it to rewrite any incoming request to - * get the server targeted. Responses sent back - * are also rewritten. - * - * @author Anders Nyman - */ -public class RewriteFilter implements Filter { - - /** - * Logging element supplied by commons-logging. - */ - private static Log log; - - /** - * The server chain, will be traversed to find a matching server. - */ - private ServerChain serverChain; - - - /** - * Rewrites the outgoing stream to make sure URLs and headers - * are correct. The incoming request is first processed to - * identify what resource we want to proxy. - * - * @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 { - - if (response.isCommitted()) { - log.info("Not proxying, already committed."); - return; - } else if (!(request instanceof HttpServletRequest)) { - log.info("Request is not HttpRequest, will only handle HttpRequests."); - return; - } else if (!(response instanceof HttpServletResponse)) { - log.info("Request is not HttpResponse, will only handle HttpResponses."); - return; - } else { - HttpServletResponse httpResponse = (HttpServletResponse) response; - HttpServletRequest httpRequest = (HttpServletRequest) request; - - Server server = serverChain.evaluate(httpRequest); - if (server == null) { - log.info("Could not find a rule for this request, will not do anything."); - filterChain.doFilter(request, response); - } else { - httpRequest.setAttribute("proxyServer", server); - - String ownHostName = request.getServerName() + ":" + request.getServerPort(); - UrlRewritingResponseWrapper wrappedResponse; - wrappedResponse = new UrlRewritingResponseWrapper(httpResponse, server, ownHostName, httpRequest.getContextPath(), serverChain); - - filterChain.doFilter(httpRequest, wrappedResponse); - - wrappedResponse.processStream(); - } - } - } - - - - /** - * Initialize. - * - * @see javax.servlet.Filter#init(javax.servlet.FilterConfig) - */ - public void init(FilterConfig filterConfig) throws ServletException { - log = LogFactory.getLog(RewriteFilter.class); - - String data = filterConfig.getInitParameter("dataUrl"); - if (data == null) { - throw new ServletException("dataUrl is required."); - } else { - try { - File dataFile = new File(filterConfig.getServletContext().getRealPath(data)); - ConfigParser parser = new ConfigParser(dataFile); - serverChain = parser.getServerChain(); - } catch (Exception e) { - throw new ServletException(e); - } - } - - } - - /** - * Release resources. - * - * @see javax.servlet.Filter#destroy() - */ - public void destroy() { - log = null; - serverChain = null; - } - - - -} diff --git a/j2ep_git/src/main/java/net/sf/j2ep/ServerChain.java b/j2ep_git/src/main/java/net/sf/j2ep/ServerChain.java deleted file mode 100644 index 3a1e1e6..0000000 --- a/j2ep_git/src/main/java/net/sf/j2ep/ServerChain.java +++ /dev/null @@ -1,165 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep; - -import java.util.Iterator; -import java.util.List; - -import javax.servlet.http.HttpServletRequest; - -import net.sf.j2ep.model.Rule; -import net.sf.j2ep.model.Server; -import net.sf.j2ep.model.ServerContainer; - - -/** - * A ServerChain is a list of server - * considered in order. The first - * server with a rule that successfully matches - * tops the evaluation of servers - * - * This is only a slightly modified version of the - * RuleChain used with the balancer webapp shipped - * with tomcat. - * - * @author Anders Nyman, Yoav Shapira - */ -public class ServerChain{ - - /** - * The list of servers to evaluate. - */ - private List serverContainers; - - /** - * Constructor. - */ - public ServerChain(List serverContainers) { - this.serverContainers = serverContainers; - } - - /** - * Returns the list of servers - * to evaluate. - * - * @return The servers - */ - protected List getServers() { - return serverContainers; - } - - /** - * Returns an iterator over - * the list of servers to evaluate. - * - * @return The iterator - */ - protected Iterator getServerIterator() { - return getServers().iterator(); - } - - /** - * Adds a server to evaluate. - * - * @param theServer The server to add - */ - public void addServer(Server theServer) { - if (theServer == null) { - throw new IllegalArgumentException("The rule cannot be null."); - } else { - getServers().add(theServer); - } - } - - /** - * Evaluates the given request to see if - * any of the rules matches. Returns the - * the server linked to the first matching rule. - * - * @param request The request - * @return The first matching server, null if no rule matched the request - * @see Rule#matches(HttpServletRequest) - */ - public Server evaluate(HttpServletRequest request) { - Iterator itr = getServerIterator(); - ServerContainer currentContainer = null; - boolean currentMatches = false; - - while (itr.hasNext() && !currentMatches) { - currentContainer = (ServerContainer) itr.next(); - currentMatches = currentContainer.getRule().matches(request); - } - - if (currentMatches) { - return currentContainer.getServer(request); - } else { - return null; - } - } - - /** - * Finds a server with the full path specified by the - * location sent in. This is used when we want to find a - * server that can handle a request. - * - * @param location The location we want a server for. - * @return The matching server, if no server is found null is returned - * @see ServerContainer#getServerMapped(String) - */ - public Server getServerMapped(String location) { - Iterator itr = getServerIterator(); - Server match = null; - - while (itr.hasNext() && match == null) { - ServerContainer container = (ServerContainer) itr.next(); - Server next = container.getServerMapped(location); - if (next != null) { - match = next; - } - } - return match; - } - - /** - * Returns a String representation of this object. - * - * @return A string representation - */ - public String toString() { - StringBuffer buffer = new StringBuffer(); - - buffer.append("["); - buffer.append(getClass().getName()); - buffer.append(": "); - - Iterator iter = getServerIterator(); - Server currentServer = null; - - while (iter.hasNext()) { - currentServer = (Server) iter.next(); - buffer.append(currentServer); - - if (iter.hasNext()) { - buffer.append(", "); - } - } - - buffer.append("]"); - - return buffer.toString(); - } -} diff --git a/j2ep_git/src/main/java/net/sf/j2ep/UrlRewritingOutputStream.java b/j2ep_git/src/main/java/net/sf/j2ep/UrlRewritingOutputStream.java deleted file mode 100644 index 4c87971..0000000 --- a/j2ep_git/src/main/java/net/sf/j2ep/UrlRewritingOutputStream.java +++ /dev/null @@ -1,227 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import javax.servlet.ServletOutputStream; - -import net.sf.j2ep.model.Server; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -/** - * A wrapper for the default output stream. This class will - * make sure all data being sent is cached by the stream and - * can rewrite all the links. - * - * @author Anders Nyman - */ -public final class UrlRewritingOutputStream extends ServletOutputStream { - - /** - * The stream we are wrapping, is the original response stream. - */ - private ServletOutputStream originalStream; - - /** - * Stream that is written to, works as a buffer for the response stream. - */ - private ByteArrayOutputStream stream; - - /** - * The server, needed when we rewrite absolute links. - */ - private String ownHostName; - - /** - * The contextPath, needed when we rewrite links. - */ - private String contextPath; - - /** - * The servers. - */ - private ServerChain serverChain; - - /** - * Regex matching links in the HTML. - */ - private static Pattern linkPattern = Pattern.compile("\\b(href=|src=|action=|url\\()([\"\'])(([^/]+://)([^/<>]+))?([^\"\'>]*)[\"\']", Pattern.CASE_INSENSITIVE | Pattern.CANON_EQ); - - /** - * Logging element supplied by commons-logging. - */ - private static Log log; - - /** - * Basic constructor. - * - * @param originalStream The stream we are wrapping - */ - public UrlRewritingOutputStream(ServletOutputStream originalStream, String ownHostName, String contextPath, ServerChain serverChain) { - this.originalStream = originalStream; - this.ownHostName = ownHostName; - this.contextPath = contextPath; - this.serverChain = serverChain; - log = LogFactory.getLog(UrlRewritingOutputStream.class); - - stream = new ByteArrayOutputStream(); - } - - /** - * @see java.io.OutputStream#write(int) - */ - public void write(int b) throws IOException { - stream.write(b); - } - - /** - * @see java.io.OutputStream#write(byte[], int, int) - */ - public void write(byte[] b, int off, int len) throws IOException { - stream.write(b, off, len); - } - - /** - * @see java.io.OutputStream#write(byte[]) - */ - public void write(byte[] b) throws IOException { - stream.write(b); - } - - /** - * Processes the stream looking for links, all links - * found are rewritten. After this the stream is written - * to the response. - * - * @param server The server that we are using for this request. - * @throws IOException Is thrown when there is a problem with the streams - */ - public void rewrite(Server server) throws IOException { - /* - * Using regex can be quite harsh sometimes so here is how - * the regex trying to find links works - * - * \\b(href=|src=|action=|url\\()([\"\']) - * This part is the identification of links, matching - * something like href=", href=' and href= - * - * (([^/]+://)([^/<>]+))? - * This is to identify absolute paths. A link doesn't have - * to be absolute therefor there is a ?. - * - * ([^\"\'>]*) - * This is the link - * - * [\"\'] - * Ending " or ' - * - * $1 - link type, e.g. href= - * $2 - ", ' or whitespace - * $3 - The entire http://www.server.com if present - * $4 - The protocol, e.g http:// or ftp:// - * $5 - The host name, e.g. www.server.com - * $6 - The link - */ - StringBuffer page = new StringBuffer(); - - Matcher matcher = linkPattern.matcher(stream.toString()); - while (matcher.find()) { - - String link = matcher.group(6).replaceAll("\\$", "\\\\\\$"); - if (link.length() == 0) { - link = "/"; - } - - String rewritten = null; - if (matcher.group(4) != null) { - rewritten = handleExternalLink(matcher, link); - } else if (link.startsWith("/")) { - rewritten = handleLocalLink(server, matcher, link); - } - - if (rewritten != null) { - if (log.isDebugEnabled()) { - log.debug("Found link " + link + " >> " + rewritten); - } - matcher.appendReplacement(page, rewritten); - } - } - - matcher.appendTail(page); - originalStream.print(page.toString()); - } - - - /** - * Rewrites a absolute path starting with a protocol e.g. - * http://www.server.com/index.html - * - * @param matcher The matcher used for this link - * @param link The part of the link after the domain name - * @return The link now rewritten - */ - private String handleExternalLink(Matcher matcher, String link) { - String location = matcher.group(5) + link; - Server matchingServer = serverChain.getServerMapped(location); - - if (matchingServer != null) { - link = link.substring(matchingServer.getPath().length()); - link = matchingServer.getRule().revert(link); - String type = matcher.group(1); - String separator = matcher.group(2); - String protocol = matcher.group(4); - return type+separator+protocol+ ownHostName + contextPath + link + separator; - } else { - return null; - } - } - - /** - * - * @param server The current server we are using for this page - * @param matcher The matcher used for this link - * @param link The original link - * @return The rewritten link - */ - private String handleLocalLink(Server server, Matcher matcher, String link) { - String serverDir = server.getPath(); - - if (serverDir.equals("") || link.startsWith(serverDir + "/")) { - link = server.getRule().revert(link.substring(serverDir.length())); - String type = matcher.group(1); - String separator = matcher.group(2); - return type + separator + contextPath + link + separator; - } else { - return null; - } - } - - - /** - * @see java.io.Closeable#close() - */ - public void close() throws IOException { - stream.close(); - } - -} diff --git a/j2ep_git/src/main/java/net/sf/j2ep/UrlRewritingResponseWrapper.java b/j2ep_git/src/main/java/net/sf/j2ep/UrlRewritingResponseWrapper.java deleted file mode 100644 index 6de8456..0000000 --- a/j2ep_git/src/main/java/net/sf/j2ep/UrlRewritingResponseWrapper.java +++ /dev/null @@ -1,271 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep; - -import java.io.IOException; -import java.io.PrintWriter; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import javax.servlet.ServletOutputStream; -import javax.servlet.http.HttpServletResponse; -import javax.servlet.http.HttpServletResponseWrapper; - -import net.sf.j2ep.model.Server; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -/** - * A wrapper for the normal HttpServletResponse, based - * on the content-type either the normal output stream - * of a wrapped stream will be returned. The wrapped stream - * can handle rewrite of links found in the source. - * - * This class also handles rewriting of the headers Location - * and Set-Cookie. - * - * @author Anders Nyman - */ -public final class UrlRewritingResponseWrapper extends HttpServletResponseWrapper{ - - /** - * Stream we are using for the response. - */ - private UrlRewritingOutputStream outStream; - - /** - * Writer we are using for the response. - */ - private PrintWriter outWriter; - - /** - * Writer that writes to the underlying stream. - */ - private PrintWriter originalWriter; - - /** - * Server used for this page - */ - private Server server; - - /** - * The location for this server, used when we rewrite absolute URIs - */ - private String ownHostName; - - /** - * The contextPath, needed when we rewrite links. - */ - private String contextPath; - - /** - * The servers. - */ - private ServerChain serverChain; - - /** - * Regex to find absolute links. - */ - private static Pattern linkPattern = Pattern.compile("\\b([^/]+://)([^/]+)([\\w/]*)", Pattern.CASE_INSENSITIVE | Pattern.CANON_EQ); - - /** - * Regex to find the path in Set-Cookie headers. - */ - private static Pattern pathAndDomainPattern = Pattern.compile("\\b(path=|domain=)([^;\\s]+);?", Pattern.CASE_INSENSITIVE | Pattern.CANON_EQ); - - /** - * Logging element supplied by commons-logging. - */ - private static Log log; - - /** - * Basic constructor. - * - * @param response The response we are wrapping - * @param server The server that was matched - * @param ownHostName String we are rewriting servers to - * @throws IOException When there is a problem with the streams - */ - public UrlRewritingResponseWrapper(HttpServletResponse response, Server server, String ownHostName, String contextPath, ServerChain serverChain) throws IOException { - super(response); - this.server = server; - this.ownHostName = ownHostName; - this.contextPath = contextPath; - this.serverChain = serverChain; - - log = LogFactory.getLog(UrlRewritingResponseWrapper.class); - outStream = new UrlRewritingOutputStream(response.getOutputStream(), ownHostName, contextPath, serverChain); - outWriter = new PrintWriter(outStream); - originalWriter = new PrintWriter(response.getOutputStream()); - } - - /** - * Checks if we have to rewrite the header and - * if so will rewrite it. - * - * @see javax.servlet.http.HttpServletResponse#addHeader(java.lang.String, java.lang.String) - */ - public void addHeader(String name, String originalValue) { - String value; - if (name.equalsIgnoreCase("location")) { - value = rewriteLocation(originalValue); - } else if (name.equalsIgnoreCase("set-cookie")) { - value = rewriteSetCookie(originalValue); - } else { - value = originalValue; - } - super.addHeader(name, value); - } - - /** - * Checks if we have to rewrite the header and - * if so will rewrite it. - * - * @see javax.servlet.http.HttpServletResponse#setHeader(java.lang.String, java.lang.String) - */ - public void setHeader(String name, String originalValue) { - String value; - if (name.equalsIgnoreCase("location")) { - value = rewriteLocation(originalValue); - } else if (name.equalsIgnoreCase("set-cookie")) { - value = rewriteSetCookie(originalValue); - } - else { - value = originalValue; - } - super.setHeader(name, value); - } - - - /** - * Rewrites the location header. - * Will first locate any links in the header and then rewrite them. - * - * @param value The header value we are to rewrite - * @return A rewritten header - */ - private String rewriteLocation(String value) { - StringBuffer header = new StringBuffer(); - - Matcher matcher = linkPattern.matcher(value); - while (matcher.find()) { - - String link = matcher.group(3).replaceAll("\\$", "\\\\$"); - if (link.length() == 0) { - link = "/"; - } - String location = matcher.group(2) + link; - - Server matchingServer = serverChain.getServerMapped(location); - if (matchingServer != null) { - link = link.substring(matchingServer.getPath().length()); - link = matchingServer.getRule().revert(link); - matcher.appendReplacement(header, "$1" + ownHostName + contextPath + link); - } - } - matcher.appendTail(header); - log.debug("Location header rewritten " + value + " >> " + header.toString()); - return header.toString(); - } - - /** - * Rewrites the header Set-Cookie so that path and domain - * is correct. - * - * @param value The original header - * @return The rewritten header - */ - private String rewriteSetCookie(String value) { - StringBuffer header = new StringBuffer(); - - Matcher matcher = pathAndDomainPattern.matcher(value); - while (matcher.find()) { - if (matcher.group(1).equalsIgnoreCase("path=")) { - String path = server.getRule().revert(matcher.group(2)); - matcher.appendReplacement(header, "$1" + contextPath + path + ";"); - } else { - matcher.appendReplacement(header, ""); - } - - } - matcher.appendTail(header); - log.debug("Set-Cookie header rewritten \"" + value + "\" >> " + header.toString()); - return header.toString(); - } - - /** - * Based on the value in the content-type header we either - * return the default stream or our own stream that can rewrite - * links. - * - * @see javax.servlet.ServletResponse#getOutputStream() - */ - public ServletOutputStream getOutputStream() throws IOException { - if (getContentType() != null && shouldRewrite(getContentType())) { - return outStream; - } else { - return super.getOutputStream(); - } - } - - /** - * Based on the value in the content-type header we either - * return the default writer or our own writer. Our own - * writer will write to the stream that can rewrite links. - * - * @see javax.servlet.ServletResponse#getWriter() - */ - public PrintWriter getWriter() throws IOException { - if (getContentType() != null && shouldRewrite(getContentType())) { - return outWriter; - } else { - return originalWriter; - } - } - - /** - * Rewrites the output stream to change any links. Also closes all the - * streams and writers. We need the user to flush and close the streams himself - * as usual but we can't be sure that the writers created are used by the client - * and therefor we close them here. - * - * @throws IOException Is thrown when there is a problem with the streams - */ - public void processStream() throws IOException { - if (getContentType() != null && shouldRewrite(getContentType())) { - outStream.rewrite(server); - } - super.getOutputStream().flush(); - super.getOutputStream().close(); - outStream.close(); - originalWriter.close(); - outWriter.close(); - } - - /** - * Checks the contentType to evaluate if we should do - * link rewriting for this content. - * - * @param contentType The Content-Type header - * @return true if we need to rewrite links, false otherwise - */ - private boolean shouldRewrite(String contentType) { - String lowerCased = contentType.toLowerCase(); - return (lowerCased.indexOf("html")>-1 || lowerCased.indexOf("css")>-1 || lowerCased.indexOf("javascript")>-1); - } -} diff --git a/j2ep_git/src/main/java/net/sf/j2ep/factories/MethodNotAllowedException.java b/j2ep_git/src/main/java/net/sf/j2ep/factories/MethodNotAllowedException.java deleted file mode 100644 index d4e9281..0000000 --- a/j2ep_git/src/main/java/net/sf/j2ep/factories/MethodNotAllowedException.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep.factories; - -/** - * An exception thrown when a factory can't handle the incoming method. - * - * @author Anders Nyman - */ -public class MethodNotAllowedException extends Exception { - - /** - * Our id - */ - private static final long serialVersionUID = 4149736397823198286L; - - /** - * List of methods that are being allowed by the factory. - */ - private String allowedMethods; - - /** - * Basic constructor creating a exception. - * @param message The exception message - * @param allowedMethods The allowed methods - */ - public MethodNotAllowedException(String message, String allowedMethods) { - super(message); - this.allowedMethods = allowedMethods; - } - - /** - * Returns which methods that are allowed by the instance throwing - * this exception. - * - * @return The allowed methods - */ - public String getAllowedMethods() { - return allowedMethods; - } - -} diff --git a/j2ep_git/src/main/java/net/sf/j2ep/factories/RequestHandlerFactory.java b/j2ep_git/src/main/java/net/sf/j2ep/factories/RequestHandlerFactory.java deleted file mode 100644 index 0950f03..0000000 --- a/j2ep_git/src/main/java/net/sf/j2ep/factories/RequestHandlerFactory.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep.factories; - -import java.util.HashMap; - -import net.sf.j2ep.model.AllowedMethodHandler; -import net.sf.j2ep.model.RequestHandler; -import net.sf.j2ep.requesthandlers.BasicRequestHandler; -import net.sf.j2ep.requesthandlers.EntityEnclosingRequestHandler; -import net.sf.j2ep.requesthandlers.MaxForwardRequestHandler; -import net.sf.j2ep.requesthandlers.RequestHandlerBase; - -/** - * A factory creating RequestHandlers. - * This factory is used to get the handler for each request, it has - * a list of methods it can handle and will throw a MethodNotAllowedException - * when it can't handle a method. - * - * @author Anders Nyman - */ -public class RequestHandlerFactory { - - /** - * The RequestHandlers to return. - */ - private static HashMap requestHandlers; - - /** - * These methods are handled by this factory. - */ - private static final String handledMethods = "OPTIONS,GET,HEAD,POST,PUT,DELETE,TRACE"; - - /** - * List of banned headers that should not be set. - */ - private static final String bannedHeaders = "connection,accept-encoding,via,x-forwarded-for,x-forwarded-host,x-forwarded-server"; - - static { - RequestHandlerBase.addBannedHeaders(bannedHeaders); - - requestHandlers = new HashMap(); - MaxForwardRequestHandler optionsAndTrace = new MaxForwardRequestHandler(); - BasicRequestHandler basic = new BasicRequestHandler(); - EntityEnclosingRequestHandler entityEnclosing = new EntityEnclosingRequestHandler(); - - requestHandlers.put("OPTIONS", optionsAndTrace); - requestHandlers.put("GET", basic); - requestHandlers.put("HEAD", basic); - requestHandlers.put("POST", entityEnclosing); - requestHandlers.put("PUT", entityEnclosing); - requestHandlers.put("DELETE", basic); - requestHandlers.put("TRACE", optionsAndTrace); - } - - /** - * Selects one suitable RequestMethod from the HashMap. - * - * @param method The method of this request - * @return A RequestHandler that can handle this request - * @throws MethodNotAllowedException If there is no RequestHandler available an exception will be thrown - */ - public static RequestHandler createRequestMethod(String method) throws MethodNotAllowedException{ - if (!AllowedMethodHandler.methodAllowed(method)) { - throw new MethodNotAllowedException("The method " + method + " is not in the AllowedHeaderHandler's list of allowed methods.", AllowedMethodHandler.getAllowHeader()); - } - - RequestHandler handler = (RequestHandler) requestHandlers.get(method.toUpperCase()); - if (handler == null) { - throw new MethodNotAllowedException("The method " + method + " was allowed by the AllowedMethodHandler, not by the factory.", handledMethods); - } else { - return handler; - } - } -} \ No newline at end of file diff --git a/j2ep_git/src/main/java/net/sf/j2ep/factories/ResponseHandlerFactory.java b/j2ep_git/src/main/java/net/sf/j2ep/factories/ResponseHandlerFactory.java deleted file mode 100644 index bee6bc8..0000000 --- a/j2ep_git/src/main/java/net/sf/j2ep/factories/ResponseHandlerFactory.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep.factories; - -import net.sf.j2ep.model.AllowedMethodHandler; -import net.sf.j2ep.model.ResponseHandler; -import net.sf.j2ep.responsehandlers.*; - -import org.apache.commons.httpclient.HttpMethod; -import org.apache.commons.httpclient.methods.*; - -/** - * A factory creating ResponseHandlers. - * This factory is used to get the handler for each request, it has - * a list of methods it can handle and will throw a MethodNotAllowedException - * when it can't handle a method. - * - * @author Anders Nyman - */ -public class ResponseHandlerFactory { - - /** - * These methods are handled by this factory. - */ - private static final String handledMethods = "OPTIONS,GET,HEAD,POST,PUT,DELETE,TRACE"; - - /** - * Checks the method being received and created a - * suitable ResponseHandler for this method. - * - * @param method Method to handle - * @return The handler for this response - * @throws MethodNotAllowedException If no method could be choose this exception is thrown - */ - public static ResponseHandler createResponseHandler(HttpMethod method) throws MethodNotAllowedException { - if (!AllowedMethodHandler.methodAllowed(method)) { - throw new MethodNotAllowedException("The method " + method.getName() + " is not in the AllowedHeaderHandler's list of allowed methods.", AllowedMethodHandler.getAllowHeader()); - } - - ResponseHandler handler = null; - if (method.getName().equals("OPTIONS")) { - handler = new OptionsResponseHandler((OptionsMethod) method); - } else if (method.getName().equals("GET")) { - handler = new GetResponseHandler((GetMethod) method); - } else if (method.getName().equals("HEAD")) { - handler = new HeadResponseHandler((HeadMethod) method); - } else if (method.getName().equals("POST")) { - handler = new PostResponseHandler((PostMethod) method); - } else if (method.getName().equals("PUT")) { - handler = new PutResponseHandler((PutMethod) method); - } else if (method.getName().equals("DELETE")) { - handler = new DeleteResponseHandler((DeleteMethod) method); - } else if (method.getName().equals("TRACE")) { - handler = new TraceResponseHandler((TraceMethod) method); - } else { - throw new MethodNotAllowedException("The method " + method.getName() + " was allowed by the AllowedMethodHandler, not by the factory.", handledMethods); - } - - return handler; - } - - - -} diff --git a/j2ep_git/src/main/java/net/sf/j2ep/model/AllowedMethodHandler.java b/j2ep_git/src/main/java/net/sf/j2ep/model/AllowedMethodHandler.java deleted file mode 100644 index 4b24d55..0000000 --- a/j2ep_git/src/main/java/net/sf/j2ep/model/AllowedMethodHandler.java +++ /dev/null @@ -1,111 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep.model; - -import java.util.HashSet; -import java.util.StringTokenizer; - -import org.apache.commons.httpclient.HttpMethod; - -/** - * A utility class that will be used throughout the - * project to process headers to filter out those - * not allowed by the proxy. - * - * @author Anders Nyman - */ -public class AllowedMethodHandler { - - /** - * The methods handled by the proxy. - */ - private static String allowString; - - /** - * A set of the HTTP methods allowed by the proxy. - */ - private static HashSet allowedMethods; - - /** - * Will go through all the methods sent in - * checking to see that the method is allowed. - * If it's allowed it will be included - * in the returned value. - * - * @param allowSent The header returned by the server - * @return The allowed headers for this request - */ - public static String processAllowHeader(String allowSent) { - StringBuffer allowToSend = new StringBuffer(""); - StringTokenizer tokenizer = new StringTokenizer(allowSent, ","); - while (tokenizer.hasMoreTokens()) { - String token = tokenizer.nextToken().trim().toUpperCase(); - if (allowedMethods.contains(token)) { - allowToSend.append(token).append(","); - } - } - - return allowToSend.toString(); - } - - /** - * Returns the allow methods for this proxy. - * - * @return Allowed methods - */ - public static String getAllowHeader() { - return allowString; - } - - /** - * Will check if the specified method is allowed by - * looking if it is included in the allowedMethods. - * - * @param method The method that is checked - * @return true if the method is allowed, false otherwise - */ - public static boolean methodAllowed(String method) { - return allowedMethods.contains(method.toUpperCase()); - } - - /** - * Checks the method to see if it's allowed - * - * @param method The method that is checked - * @return true if the method is allowed, false otherwise - * @see AllowedMethodHandler#methodAllowed(String) - */ - public static boolean methodAllowed(HttpMethod method) { - return methodAllowed(method.getName()); - } - - /** - * Will set the allowed methods, both by setting the string - * and also by adding all the methods to the set of allowed. - * @param allowed The list of allowed methods, should be comma separated - */ - public synchronized static void setAllowedMethods(String allowed) { - allowedMethods = new HashSet(); - allowString = allowed; - StringTokenizer tokenizer = new StringTokenizer(allowed, ","); - while (tokenizer.hasMoreTokens()) { - String token = tokenizer.nextToken().trim().toUpperCase(); - allowedMethods.add(token); - } - } - -} diff --git a/j2ep_git/src/main/java/net/sf/j2ep/model/RequestHandler.java b/j2ep_git/src/main/java/net/sf/j2ep/model/RequestHandler.java deleted file mode 100644 index 7ccef4d..0000000 --- a/j2ep_git/src/main/java/net/sf/j2ep/model/RequestHandler.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep.model; - -import java.io.IOException; - -import javax.servlet.http.HttpServletRequest; - -import org.apache.commons.httpclient.HttpMethod; - -/** - * A handler for all requests. Will set the headers, process any - * input stream and do any method specific actions. - * - * @author Anders Nyman - */ -public interface RequestHandler { - - /** - * Creates a new HttpMethod for this request. Will then - * set the headers and any other information needed for this - * request. - * - * @param request The request we are processing - * @param url URL to bind the method to - * @return The method we have created - * @throws IOException An exception is thrown when there is a problem with the input supplied by the request - */ - public HttpMethod process(HttpServletRequest request, String url) throws IOException; - -} diff --git a/j2ep_git/src/main/java/net/sf/j2ep/model/ResponseHandler.java b/j2ep_git/src/main/java/net/sf/j2ep/model/ResponseHandler.java deleted file mode 100644 index 227047f..0000000 --- a/j2ep_git/src/main/java/net/sf/j2ep/model/ResponseHandler.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep.model; - -import java.io.IOException; - -import javax.servlet.http.HttpServletResponse; - -/** - * A handler for all responses. Will set the headers, process any - * output stream and do any method specific actions. - * - * @author Anders Nyman - */ -public interface ResponseHandler { - - /** - * Will process the response to set headers and streams for - * it. Each implementation of ResponseHandler can also add - * it's one method specific actions to the process method. - * - * @param response The response to process - * @throws IOException An exception is thrown when there is a problem with writing the output - */ - public void process(HttpServletResponse response) throws IOException; - - /** - * Returns the HTTP status code we received from the server - * - * @return The status code - */ - public int getStatusCode(); - - /** - * Do any actions needed when we wont need the ResponseHandler any more. - */ - public void close(); - -} diff --git a/j2ep_git/src/main/java/net/sf/j2ep/model/Rule.java b/j2ep_git/src/main/java/net/sf/j2ep/model/Rule.java deleted file mode 100644 index 9017dcb..0000000 --- a/j2ep_git/src/main/java/net/sf/j2ep/model/Rule.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package net.sf.j2ep.model; - -import javax.servlet.http.HttpServletRequest; - - -/** - * The Rule interface is implemented by - * the rules for the reverse proxy. - * Based on the work by Yoav Shapira - * for the balancer webapp. - * - * @author Anders Nyman - */ -public interface Rule { - - /** - * Determines if the given request - * matches the rule. - * - * @param request The request - * @return true if the request is matched, otherwise false - */ - boolean matches(HttpServletRequest request); - - /** - * Returns the redirect URI for - * requests that match this rule. - * Process is used in order to let the rules - * do rewriting of the URI before being handled - * by the server. - * - * The rule will not any check that a URI is matched - * before processing it. If you want this type of - * control make make sure the calling class checks that - * there is a match before processing. - * - * @param uri URI to be processed - * @return The final URI - */ - String process(String uri); - - /** - * Returns the reverted URI, this means - * that if a URI is processed and then reverted - * it should be the same URI. - * revert(process("some random string")) should - * return "some random string". - * - * @param uri URI to be reverted - * @return The reverted URI - */ - String revert(String uri); -} \ No newline at end of file diff --git a/j2ep_git/src/main/java/net/sf/j2ep/model/Server.java b/j2ep_git/src/main/java/net/sf/j2ep/model/Server.java deleted file mode 100644 index c3b0c93..0000000 --- a/j2ep_git/src/main/java/net/sf/j2ep/model/Server.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep.model; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - - -/** - * A representation of the server. Its main use it to get the - * domain and path for the underlying proxied server. - * - * @author Anders Nyman - */ -public interface Server { - - /** - * Can do any handling needed of a request before - * the HttpMethod is executed. Example of handling - * is to wrap the request. - * - * @param request The request we are receiving - * @return Eventual modified HttpServletRequest - */ - HttpServletRequest preExecute(HttpServletRequest request); - - /** - * Can do handling of the response, if needed the - * server can also return a new HttpServletResponse - * if a wrapper of the response is needed. - * - * @param response The response we are receiving - * @return Eventual modified HttpServletResponse - */ - HttpServletResponse postExecute(HttpServletResponse response); - - /** - * Used to notify the server that there is a problem using the data this - * server supplied to the user. Maybe the domainName isn't working or the - * directory doesn't exist on the underlying server, in any case this method - * can be used to let the server know about the problems. - * - * The server should try to analyze the problem and if possible fix it so - * that the next request to this server will succeed. - * - * @param e The exception received when trying to use this servers data - */ - void setConnectionExceptionRecieved(Exception e); - - /** - * Returns the host name and port for this server. - * @return Host name and port - */ - String getDomainName(); - - /** - * Returns the path that we are mapping to. Starting from the - * servers root the path starts with a / but doesn't end with - * a /. A mapping to the root results in - * path being an empty string "". - * - * @return The path - */ - String getPath(); - - - /** - * Returns the mapped rule so we can rewrite links. - * - * @return The rule we are mapped to - */ - Rule getRule(); -} diff --git a/j2ep_git/src/main/java/net/sf/j2ep/model/ServerContainer.java b/j2ep_git/src/main/java/net/sf/j2ep/model/ServerContainer.java deleted file mode 100644 index 35503ca..0000000 --- a/j2ep_git/src/main/java/net/sf/j2ep/model/ServerContainer.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep.model; - -import javax.servlet.http.HttpServletRequest; - - -/** - * A server container is a object can can hold a server. It - * is used to do initial processing of the data in a request - * before a server is created. This can be used to make some - * decisions on the server creating based on the request to - * allow more advanced server. - * - * Usually the server can implement both this interface and - * the server interface since the usage is rather linked. - * - * @author Anders Nyman - */ -public interface ServerContainer { - - /** - * Do any processing needed before this server can be used. - * Specifically important for cluster servers that needs - * to choose which server to use for the current request. - * - * @return The server that is finished to be used. - */ - Server getServer(HttpServletRequest request); - - /** - * Returns the mapped rule. This method must return the same - * rule as a call to the underlying servers getRule(). - * - * @return The rule we are mapped to - * @see Server#getRule() - */ - Rule getRule(); - - /** - * Sets the rule that is mapped for this server. Will - * be used when we rewrite links to know how a absolute - * path should be rewritten. - * - * @param rule The rule - */ - void setRule(Rule rule); - - /** - * Finds a server with the full path specified by the - * location sent in. - * - * @param link The start of a link that a server is mapped to - * @return The server that can handle the link, null if no server is found - */ - Server getServerMapped(String link); -} diff --git a/j2ep_git/src/main/java/net/sf/j2ep/requesthandlers/BasicRequestHandler.java b/j2ep_git/src/main/java/net/sf/j2ep/requesthandlers/BasicRequestHandler.java deleted file mode 100644 index 56f4fad..0000000 --- a/j2ep_git/src/main/java/net/sf/j2ep/requesthandlers/BasicRequestHandler.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep.requesthandlers; - -import javax.servlet.http.HttpServletRequest; - - -import org.apache.commons.httpclient.HttpException; -import org.apache.commons.httpclient.HttpMethod; -import org.apache.commons.httpclient.HttpMethodBase; -import org.apache.commons.httpclient.methods.DeleteMethod; -import org.apache.commons.httpclient.methods.GetMethod; -import org.apache.commons.httpclient.methods.HeadMethod; - -/** - * A handler for GET, HEAD, DELETE. Since these methods basically - * only will need the headers set they can be handled by the same - * handler. - * - * @author Anders Nyman - */ -public class BasicRequestHandler extends RequestHandlerBase { - - /** - * Will only set the headers. - * @throws HttpException - * - * @see net.sf.j2ep.model.RequestHandler#process(javax.servlet.http.HttpServletRequest, java.lang.String) - */ - public HttpMethod process(HttpServletRequest request, String url) throws HttpException { - - HttpMethodBase method = null; - - if (request.getMethod().equalsIgnoreCase("GET")) { - method = new GetMethod(url); - } else if (request.getMethod().equalsIgnoreCase("HEAD")) { - method = new HeadMethod(url); - } else if (request.getMethod().equalsIgnoreCase("DELETE")) { - method = new DeleteMethod(url); - } else { - return null; - } - - setHeaders(method, request); - return method; - } - - -} diff --git a/j2ep_git/src/main/java/net/sf/j2ep/requesthandlers/EntityEnclosingRequestHandler.java b/j2ep_git/src/main/java/net/sf/j2ep/requesthandlers/EntityEnclosingRequestHandler.java deleted file mode 100644 index fba41f9..0000000 --- a/j2ep_git/src/main/java/net/sf/j2ep/requesthandlers/EntityEnclosingRequestHandler.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep.requesthandlers; - -import java.io.IOException; - -import javax.servlet.http.HttpServletRequest; - - -import org.apache.commons.httpclient.HttpMethod; -import org.apache.commons.httpclient.methods.EntityEnclosingMethod; -import org.apache.commons.httpclient.methods.InputStreamRequestEntity; -import org.apache.commons.httpclient.methods.PostMethod; -import org.apache.commons.httpclient.methods.PutMethod; - -/** - * Handler for POST and PUT methods. - * - * @author Anders Nyman - */ -public class EntityEnclosingRequestHandler extends RequestHandlerBase { - - /** - * Will set the input stream and the Content-Type header to match this request. - * Will also set the other headers send in the request. - * - * @throws IOException An exception is throws when there is a problem getting the input stream - * @see net.sf.j2ep.model.RequestHandler#process(javax.servlet.http.HttpServletRequest, java.lang.String) - */ - public HttpMethod process(HttpServletRequest request, String url) throws IOException { - - EntityEnclosingMethod method = null; - - if (request.getMethod().equalsIgnoreCase("POST")) { - method = new PostMethod(url); - } else if (request.getMethod().equalsIgnoreCase("PUT")) { - method = new PutMethod(url); - } - - setHeaders(method, request); - - InputStreamRequestEntity stream; - stream = new InputStreamRequestEntity(request.getInputStream()); - method.setRequestEntity(stream); - method.setRequestHeader("Content-type", request.getContentType()); - - return method; - - } - - -} diff --git a/j2ep_git/src/main/java/net/sf/j2ep/requesthandlers/MaxForwardRequestHandler.java b/j2ep_git/src/main/java/net/sf/j2ep/requesthandlers/MaxForwardRequestHandler.java deleted file mode 100644 index 2528caf..0000000 --- a/j2ep_git/src/main/java/net/sf/j2ep/requesthandlers/MaxForwardRequestHandler.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep.requesthandlers; - -import java.io.IOException; -import java.util.Enumeration; - -import javax.servlet.http.HttpServletRequest; - -import org.apache.commons.httpclient.HttpMethod; -import org.apache.commons.httpclient.HttpMethodBase; -import org.apache.commons.httpclient.methods.OptionsMethod; -import org.apache.commons.httpclient.methods.TraceMethod; - - -/** - * Handler for the OPTIONS and TRACE method. - * - * @author Anders Nyman - */ -public class MaxForwardRequestHandler extends RequestHandlerBase { - - /** - * Sets the headers and does some checking for if this request - * is meant for the server or for the proxy. This check is done - * by looking at the Max-Forwards header. - * - * @see net.sf.j2ep.model.RequestHandler#process(javax.servlet.http.HttpServletRequest, java.lang.String) - */ - public HttpMethod process(HttpServletRequest request, String url) throws IOException { - HttpMethodBase method = null; - - if (request.getMethod().equalsIgnoreCase("OPTIONS")) { - method = new OptionsMethod(url); - } else if (request.getMethod().equalsIgnoreCase("TRACE")) { - method = new TraceMethod(url); - } else { - return null; - } - - try { - int max = request.getIntHeader("Max-Forwards"); - if (max == 0 || request.getRequestURI().equals("*")) { - setAllHeaders(method, request); - method.abort(); - } else if (max != -1) { - setHeaders(method, request); - method.setRequestHeader("Max-Forwards", "" + max--); - } else { - setHeaders(method, request); - } - } catch (NumberFormatException e) {} - - return method; - } - - /** - * Will write all the headers included in the request to the method. - * The difference between this method and setHeaders in RequestHandlerBase - * is that the BasicRequestHandler will also add Via, x-forwarded-for, etc. - * These "special" headers should not be added when the proxy is target - * directly with a Max-Forwards: 0 headers. - * @param method The method to write to - * @param request The incoming request - * @see RequestHandlerBase#setHeaders(HttpMethod, HttpServletRequest) - */ - private void setAllHeaders(HttpMethod method, HttpServletRequest request) { - Enumeration headers = request.getHeaderNames(); - - while (headers.hasMoreElements()) { - String name = (String) headers.nextElement(); - Enumeration value = request.getHeaders(name); - - while (value.hasMoreElements()) { - method.addRequestHeader(name, (String) value.nextElement()); - } - - } - } -} diff --git a/j2ep_git/src/main/java/net/sf/j2ep/requesthandlers/RequestHandlerBase.java b/j2ep_git/src/main/java/net/sf/j2ep/requesthandlers/RequestHandlerBase.java deleted file mode 100644 index 7f09b32..0000000 --- a/j2ep_git/src/main/java/net/sf/j2ep/requesthandlers/RequestHandlerBase.java +++ /dev/null @@ -1,151 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep.requesthandlers; - -import java.io.IOException; -import java.net.InetAddress; -import java.net.UnknownHostException; -import java.util.Enumeration; -import java.util.HashSet; -import java.util.Set; -import java.util.StringTokenizer; - -import javax.servlet.http.HttpServletRequest; - -import net.sf.j2ep.model.RequestHandler; - -import org.apache.commons.httpclient.HttpException; -import org.apache.commons.httpclient.HttpMethod; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -/** - * A basic implementation of the RequestHandler. - * Includes a method to set the headers with. - * - * @author Anders Nyman - */ -public abstract class RequestHandlerBase implements RequestHandler { - - /** - * A set of headers that are not to be set in the request, - * these headers are for example Connection. - */ - private static Set bannedHeaders = new HashSet(); - - /** - * @see net.sf.j2ep.model.RequestHandler#process(javax.servlet.http.HttpServletRequest, java.lang.String) - */ - public abstract HttpMethod process(HttpServletRequest request, String url) throws IOException; - - /** - * Logging element supplied by commons-logging. - */ - private static Log log = LogFactory.getLog(RequestHandlerBase.class); - - /** - * Will write all request headers stored in the request to the method that - * are not in the set of banned headers. - * The Accept-Endocing header is also changed to allow compressed content - * connection to the server even if the end client doesn't support that. - * A Via headers is created as well in compliance with the RFC. - * - * @param method The HttpMethod used for this connection - * @param request The incoming request - * @throws HttpException - */ - protected void setHeaders(HttpMethod method, HttpServletRequest request) throws HttpException { - Enumeration headers = request.getHeaderNames(); - String connectionToken = request.getHeader("connection"); - - while (headers.hasMoreElements()) { - String name = (String) headers.nextElement(); - boolean isToken = (connectionToken != null && name.equalsIgnoreCase(connectionToken)); - - if (!isToken && !bannedHeaders.contains(name.toLowerCase())) { - Enumeration value = request.getHeaders(name); - while (value.hasMoreElements()) { - method.addRequestHeader(name, (String) value.nextElement()); - } - } - } - - setProxySpecificHeaders(method, request); - } - - /** - * Will write the proxy specific headers such as Via and x-forwarded-for. - * - * @param method Method to write the headers to - * @param request The incoming request, will need to get virtual host. - * @throws HttpException - */ - private void setProxySpecificHeaders(HttpMethod method, HttpServletRequest request) throws HttpException { - String serverHostName = "jEasyExtensibleProxy"; - try { - serverHostName = InetAddress.getLocalHost().getHostName(); - } catch (UnknownHostException e) { - log.error("Couldn't get the hostname needed for headers x-forwarded-server and Via", e); - } - - String originalVia = request.getHeader("via"); - StringBuffer via = new StringBuffer(""); - if (originalVia != null) { - if (originalVia.indexOf(serverHostName) != -1) { - log.error("This proxy has already handled the request, will abort."); - throw new HttpException("Request has a cyclic dependency on this proxy."); - } else { - via.append(originalVia).append(", "); - } - } - via.append(request.getProtocol()).append(" ").append(serverHostName); - - method.setRequestHeader("via", via.toString()); - method.setRequestHeader("x-forwarded-for", request.getRemoteAddr()); - method.setRequestHeader("x-forwarded-host", request.getServerName()); - method.setRequestHeader("x-forwarded-server", serverHostName); - - method.setRequestHeader("accept-encoding", ""); - } - - /** - * Adds a headers to the list of banned headers. - * - * @param header The header to add - */ - public static void addBannedHeader(String header) { - bannedHeaders.add(header); - } - - /** - * Adds all the headers in the input to the list - * of banned headers. The input string should be - * comma separated e.g. "Server,Connection,Via" - * - * This method is normally called by the factory that - * is using this request handler. - * - * @param headers The headers that are banned - */ - public static void addBannedHeaders(String headers) { - StringTokenizer tokenizer = new StringTokenizer(headers, ","); - while (tokenizer.hasMoreTokens()) { - bannedHeaders.add(tokenizer.nextToken().trim().toLowerCase()); - } - } - -} diff --git a/j2ep_git/src/main/java/net/sf/j2ep/responsehandlers/BasicResponseHandler.java b/j2ep_git/src/main/java/net/sf/j2ep/responsehandlers/BasicResponseHandler.java deleted file mode 100644 index 548bf01..0000000 --- a/j2ep_git/src/main/java/net/sf/j2ep/responsehandlers/BasicResponseHandler.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep.responsehandlers; - -import java.io.IOException; - -import javax.servlet.http.HttpServletResponse; - - -import org.apache.commons.httpclient.HttpMethod; - -/** - * A simple ResponseHandler used for all those times - * when all you have to do is write the Headers and - * process the stream. - * - * @author Anders Nyman - */ -public class BasicResponseHandler extends ResponseHandlerBase { - - /** - * Default constructor, will only call the super-constructor - * for ResponseHandlerBase. - * - * @param method The method used for this response - */ - public BasicResponseHandler(HttpMethod method) { - super(method); - } - - /** - * Sets the headers, writes the stream and sets the status code. - * - * @see net.sf.j2ep.model.ResponseHandler#process(javax.servlet.http.HttpServletResponse) - */ - public void process(HttpServletResponse response) throws IOException{ - setHeaders(response); - response.setStatus(getStatusCode()); - sendStreamToClient(response); - } - -} diff --git a/j2ep_git/src/main/java/net/sf/j2ep/responsehandlers/DeleteResponseHandler.java b/j2ep_git/src/main/java/net/sf/j2ep/responsehandlers/DeleteResponseHandler.java deleted file mode 100644 index aaaa6e4..0000000 --- a/j2ep_git/src/main/java/net/sf/j2ep/responsehandlers/DeleteResponseHandler.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep.responsehandlers; - -import org.apache.commons.httpclient.methods.DeleteMethod; - -/** - * Handler for the DELETE method. - * - * @author Anders Nyman - */ -public class DeleteResponseHandler extends BasicResponseHandler { - - /** - * Default constructor, will only call the super-constructor - * for BasicResponseHandler. - * - * @param method The method used for this response - */ - public DeleteResponseHandler(DeleteMethod method) { - super(method); - } - -} diff --git a/j2ep_git/src/main/java/net/sf/j2ep/responsehandlers/GetResponseHandler.java b/j2ep_git/src/main/java/net/sf/j2ep/responsehandlers/GetResponseHandler.java deleted file mode 100644 index c9c3bec..0000000 --- a/j2ep_git/src/main/java/net/sf/j2ep/responsehandlers/GetResponseHandler.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep.responsehandlers; - -import org.apache.commons.httpclient.methods.GetMethod; - -/** - * Handler for the GET method. - * - * @author Anders Nyman - */ -public class GetResponseHandler extends BasicResponseHandler { - - /** - * Default constructor, will only call the super-constructor - * for BasicResponseHandler. - * - * @param method The method used for this response - */ - public GetResponseHandler(GetMethod method) { - super(method); - } - -} diff --git a/j2ep_git/src/main/java/net/sf/j2ep/responsehandlers/HeadResponseHandler.java b/j2ep_git/src/main/java/net/sf/j2ep/responsehandlers/HeadResponseHandler.java deleted file mode 100644 index 629fe26..0000000 --- a/j2ep_git/src/main/java/net/sf/j2ep/responsehandlers/HeadResponseHandler.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep.responsehandlers; - -import javax.servlet.http.HttpServletResponse; - - -import org.apache.commons.httpclient.methods.HeadMethod; - -/** - * Handler for the HEAD method. - * - * @author Anders Nyman - */ -public class HeadResponseHandler extends ResponseHandlerBase { - - /** - * Default constructor, will only call the super-constructor - * for BasicResponseHandler. - * - * @param method The method used for this response - */ - public HeadResponseHandler(HeadMethod method) { - super(method); - } - - /** - * Will only set the headers and status code, no response is sent. - * - * @see net.sf.j2ep.model.ResponseHandler#process(javax.servlet.http.HttpServletResponse) - */ - public void process(HttpServletResponse response) { - setHeaders(response); - response.setStatus(getStatusCode()); - } - -} diff --git a/j2ep_git/src/main/java/net/sf/j2ep/responsehandlers/OptionsResponseHandler.java b/j2ep_git/src/main/java/net/sf/j2ep/responsehandlers/OptionsResponseHandler.java deleted file mode 100644 index 22c09a8..0000000 --- a/j2ep_git/src/main/java/net/sf/j2ep/responsehandlers/OptionsResponseHandler.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep.responsehandlers; - -import java.io.IOException; - -import javax.servlet.http.HttpServletResponse; - -import net.sf.j2ep.model.AllowedMethodHandler; - -import org.apache.commons.httpclient.Header; -import org.apache.commons.httpclient.methods.OptionsMethod; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -/** - * Handler for the OPTIONS method. - * Will process the Allow header so that - * no methods that the backing server can handle - * but we can't are being sent to the client. - * - * @author Anders Nyman - */ -public class OptionsResponseHandler extends ResponseHandlerBase { - - /** - * The logger. - */ - private static Log log = LogFactory.getLog(OptionsResponseHandler.class); - - /** - * Set a construction to indicate if the request is directed to the - * proxy directly by using Max-Forwards: 0 or using URI *. - */ - private boolean useOwnAllow; - - /** - * Constructor checking if we should handle the Allow header - * ourself or respond with the backing servers header. - * - * @param method The method for this response - */ - public OptionsResponseHandler(OptionsMethod method) { - super(method); - useOwnAllow = !method.hasBeenUsed(); - } - - /** - * Will check if we are to handle this request, if so - * the http methods allowed by this proxy is returned in the - * Allow header. - * If it is a request meant for the backing server its - * allowed method will be returned. - * - * @see net.sf.j2ep.model.ResponseHandler#process(javax.servlet.http.HttpServletResponse) - */ - public void process(HttpServletResponse response) { - if (useOwnAllow) { - response.setStatus(HttpServletResponse.SC_OK); - response.setHeader("allow", AllowedMethodHandler.getAllowHeader()); - response.setHeader("Connection", "close"); - response.setHeader("content-length", "0"); - } else { - setHeaders(response); - response.setStatus(getStatusCode()); - String allow = method.getResponseHeader("allow").getValue(); - response.setHeader("allow", AllowedMethodHandler.processAllowHeader(allow)); - Header contentLength = method.getResponseHeader("Content-Length"); - if (contentLength == null || contentLength.getValue().equals("0")) { - response.setHeader("Content-Length", "0"); - } else { - try { - sendStreamToClient(response); - } catch (IOException e) { - log.error("Problem with writing response stream, solving by setting Content-Length=0", e); - response.setHeader("Content-Length", "0"); - } - } - } - } - - /** - * Returns 200 if the request is targeted to the proxy - * otherwise the normal status code is returned. - * - * @see net.sf.j2ep.model.ResponseHandler#getStatusCode() - */ - public int getStatusCode() { - if (useOwnAllow) { - return 200; - } else { - return super.getStatusCode(); - } - } - -} diff --git a/j2ep_git/src/main/java/net/sf/j2ep/responsehandlers/PostResponseHandler.java b/j2ep_git/src/main/java/net/sf/j2ep/responsehandlers/PostResponseHandler.java deleted file mode 100644 index 5378a0a..0000000 --- a/j2ep_git/src/main/java/net/sf/j2ep/responsehandlers/PostResponseHandler.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep.responsehandlers; - -import org.apache.commons.httpclient.methods.PostMethod; - -/** - * Handler for the POST method. - * - * @author Anders Nyman - */ -public class PostResponseHandler extends BasicResponseHandler { - - /** - * Default constructor, will only call the super-constructor - * for ResponseHandlerBase. - * - * @param method The method used for this response - */ - public PostResponseHandler(PostMethod method) { - super(method); - } - -} diff --git a/j2ep_git/src/main/java/net/sf/j2ep/responsehandlers/PutResponseHandler.java b/j2ep_git/src/main/java/net/sf/j2ep/responsehandlers/PutResponseHandler.java deleted file mode 100644 index 1adb74c..0000000 --- a/j2ep_git/src/main/java/net/sf/j2ep/responsehandlers/PutResponseHandler.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep.responsehandlers; - -import org.apache.commons.httpclient.methods.PutMethod; - -/** - * Handler for the PUT method. - * - * @author Anders Nyman - */ -public class PutResponseHandler extends BasicResponseHandler { - - /** - * Default constructor, will only call the super-constructor - * for ResponseHandlerBase. - * - * @param method The method used for this response - */ - public PutResponseHandler(PutMethod method) { - super(method); - } -} diff --git a/j2ep_git/src/main/java/net/sf/j2ep/responsehandlers/ResponseHandlerBase.java b/j2ep_git/src/main/java/net/sf/j2ep/responsehandlers/ResponseHandlerBase.java deleted file mode 100644 index fcb2d46..0000000 --- a/j2ep_git/src/main/java/net/sf/j2ep/responsehandlers/ResponseHandlerBase.java +++ /dev/null @@ -1,147 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep.responsehandlers; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.net.InetAddress; -import java.net.UnknownHostException; - -import javax.servlet.ServletResponse; -import javax.servlet.http.HttpServletResponse; - -import net.sf.j2ep.model.ResponseHandler; -import net.sf.j2ep.requesthandlers.RequestHandlerBase; - -import org.apache.commons.httpclient.Header; -import org.apache.commons.httpclient.HttpMethod; -import org.apache.commons.logging.LogFactory; - -/** - * Basic implementation of a Response Handler. This class - * can write the headers and process the output stream. - * - * @author Anders Nyman - */ -public abstract class ResponseHandlerBase implements ResponseHandler{ - - /** - * Method we are using for this request. - */ - protected HttpMethod method; - - /** - * Basic constructor only setting the method. - * - * @param method The method we are using - */ - public ResponseHandlerBase(HttpMethod method) { - this.method = method; - } - - /** - * @see net.sf.j2ep.model.ResponseHandler#process(javax.servlet.http.HttpServletResponse) - */ - public abstract void process(HttpServletResponse response) throws IOException; - - /** - * Will release the connection for the method. - * - * @see net.sf.j2ep.model.ResponseHandler#close() - */ - public void close() { - method.releaseConnection(); - } - - /** - * @see net.sf.j2ep.model.ResponseHandler#getStatusCode() - */ - public int getStatusCode() { - return method.getStatusCode(); - } - - /** - * Writes the entire stream from the method to the response - * stream. - * - * @param response Response to send data to - * @throws IOException An IOException is thrown when we are having problems with reading the streams - */ - protected void sendStreamToClient(ServletResponse response) throws IOException { - InputStream streamFromServer = method.getResponseBodyAsStream(); - OutputStream responseStream = response.getOutputStream(); - - if (streamFromServer != null) { - byte[] buffer = new byte[1024]; - int read = streamFromServer.read(buffer); - while (read > 0) { - responseStream.write(buffer, 0, read); - read = streamFromServer.read(buffer); - } - streamFromServer.close(); - } - responseStream.flush(); - responseStream.close(); - } - - /** - * Will write all response headers received in the method to the response. - * One header, connection, is however omitted since we will only want the - * client to keep his connection to the proxy not to the backing server. - * - * @param response The response that will have headers written to it - */ - protected void setHeaders(HttpServletResponse response) { - Header[] headers = method.getResponseHeaders(); - - for (int i=0; i < headers.length; i++) { - Header header = headers[i]; - String name = header.getName(); - boolean contentLength = name.equalsIgnoreCase("content-length"); - boolean connection = name.equalsIgnoreCase("connection"); - - if (!contentLength && !connection) { - response.addHeader(name, header.getValue()); - } - } - - setViaHeader(response); - } - - /** - * Will set the via header with this proxies data to the response. - * @param response The response we set the header for - */ - private void setViaHeader(HttpServletResponse response) { - String serverHostName = "jEasyReverseProxy"; - try { - serverHostName = InetAddress.getLocalHost().getHostName(); - } catch (UnknownHostException e) { - LogFactory.getLog(RequestHandlerBase.class).error("Couldn't get the hostname needed for header Via", e); - } - - Header originalVia = method.getResponseHeader("via"); - StringBuffer via = new StringBuffer(""); - if (originalVia != null) { - via.append(originalVia.getValue()).append(", "); - } - via.append(method.getStatusLine().getHttpVersion()).append(" ").append(serverHostName); - - response.setHeader("via", via.toString()); - } -} diff --git a/j2ep_git/src/main/java/net/sf/j2ep/responsehandlers/TraceResponseHandler.java b/j2ep_git/src/main/java/net/sf/j2ep/responsehandlers/TraceResponseHandler.java deleted file mode 100644 index 9dd2469..0000000 --- a/j2ep_git/src/main/java/net/sf/j2ep/responsehandlers/TraceResponseHandler.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep.responsehandlers; - -import java.io.IOException; -import java.io.PrintWriter; - -import javax.servlet.http.HttpServletResponse; - -import org.apache.commons.httpclient.Header; -import org.apache.commons.httpclient.methods.TraceMethod; - -/** - * A handler for the TRACE method. This handler will make - * a check if the trace is directed to the proxy or a underlying - * server. If the trace directed to the proxy the response will - * be made in compliance to HTTP 1.1 RFC. - * - * @author Anders Nyman - */ -public class TraceResponseHandler extends ResponseHandlerBase { - - /** - * Set a construction to indicate if the request is directed to the - * proxy directly by using Max-Forwards: 0 or using URI *. - */ - private boolean proxyTargeted; - - /** - * Basic constructor setting the method, and also checks if - * the request is targeted to the proxy or the underlying server. - * @param method The http method - */ - public TraceResponseHandler(TraceMethod method) { - super(method); - proxyTargeted = !method.hasBeenUsed(); - } - - /** - * Will either respond with data from the underlying server - * or the proxy's own data. - * @see net.sf.j2ep.model.ResponseHandler#process(javax.servlet.http.HttpServletResponse) - */ - public void process(HttpServletResponse response) throws IOException { - - if (proxyTargeted) { - response.setStatus(HttpServletResponse.SC_OK); - response.setHeader("content-type", "message/http"); - response.setHeader("Connection", "close"); - - String path = method.getPath(); - String protocol = method.getParams().getVersion().toString(); - PrintWriter writer = response.getWriter(); - writer.println("TRACE " + path + " " + protocol); - Header[] headers = method.getRequestHeaders(); - for (int i=0; i < headers.length; i++) { - writer.print(headers[i]); - } - writer.flush(); - writer.close(); - - } else { - setHeaders(response); - response.setStatus(getStatusCode()); - sendStreamToClient(response); - } - } - - /** - * Returns 200 if the request is targeted to the proxy - * otherwise the normal status code is returned. - * - * @see net.sf.j2ep.model.ResponseHandler#getStatusCode() - */ - public int getStatusCode() { - if (proxyTargeted) { - return 200; - } else { - return super.getStatusCode(); - } - } - -} diff --git a/j2ep_git/src/main/java/net/sf/j2ep/rules/AcceptEverythingRule.java b/j2ep_git/src/main/java/net/sf/j2ep/rules/AcceptEverythingRule.java deleted file mode 100644 index 4689417..0000000 --- a/j2ep_git/src/main/java/net/sf/j2ep/rules/AcceptEverythingRule.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep.rules; - -import javax.servlet.http.HttpServletRequest; - - -/** - * This rule matches every request - * passed to it, making it suitable - * for use as a catch-all or last - * rule in a chain. - * - * @author Yoav Shapira - */ -public class AcceptEverythingRule extends BaseRule { - - /** - * This implementation always matches. - * - * @see net.sf.j2ep.model.Rule#matches(HttpServletRequest) - */ - public boolean matches(HttpServletRequest request) { - return true; - } - -} \ No newline at end of file diff --git a/j2ep_git/src/main/java/net/sf/j2ep/rules/BaseRule.java b/j2ep_git/src/main/java/net/sf/j2ep/rules/BaseRule.java deleted file mode 100644 index fc7794e..0000000 --- a/j2ep_git/src/main/java/net/sf/j2ep/rules/BaseRule.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - - -package net.sf.j2ep.rules; - -import net.sf.j2ep.model.Rule; - -/** - * The BaseRule is an empty rule - * implementation which can be - * extended. - * This class is based on the work by Yoav Shapira - * for the balancer webapp supplied with Tomcat. - * - * @author Anders Nyman - */ -public abstract class BaseRule implements Rule { - - /** - * @see net.sf.j2ep.model.Rule#process(java.lang.String) - */ - public String process(String uri) { - return uri; - } - - /** - * @see net.sf.j2ep.model.Rule#revert(java.lang.String) - */ - public String revert(String uri) { - return uri; - } - - /** - * Returns a String representation of this object. - * - * @return The string representation - */ - public String toString() { - StringBuffer buffer = new StringBuffer(); - - buffer.append("["); - buffer.append(getClass().getName()); - buffer.append(": "); - - buffer.append("]"); - - return buffer.toString(); - } -} diff --git a/j2ep_git/src/main/java/net/sf/j2ep/rules/CompositeRule.java b/j2ep_git/src/main/java/net/sf/j2ep/rules/CompositeRule.java deleted file mode 100644 index 6b599fe..0000000 --- a/j2ep_git/src/main/java/net/sf/j2ep/rules/CompositeRule.java +++ /dev/null @@ -1,135 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep.rules; - -import java.util.Iterator; -import java.util.LinkedList; -import java.util.ListIterator; - -import javax.servlet.http.HttpServletRequest; - -import net.sf.j2ep.model.Rule; - -/** - * This rule consists of many other rules using the - * composite design pattern. The rule is matches if - * all the included rules are matched. - * - * @author Anders Nyman - */ -public class CompositeRule extends BaseRule { - - /** - * The list of rules. - */ - private LinkedList rules; - - /** - * Empty constructor, will only create the list of rules. - */ - public CompositeRule() { - rules = new LinkedList(); - } - - /** - * Used to add a rule to the list. - * @param rule The rule to be added - */ - public void addRule(Rule rule) { - if (rule == null) { - throw new IllegalArgumentException("Rule to add cannot be null."); - } else { - rules.add(rule); - } - } - - /** - * Iterates over all the rules in the list checking that they all match. - * - * @return true if all the rules match or there are no rules, false otherwise - * @see net.sf.j2ep.model.Rule#matches(javax.servlet.http.HttpServletRequest) - */ - public boolean matches(HttpServletRequest request) { - Iterator itr = rules.iterator(); - boolean matches = true; - while (itr.hasNext() && matches) { - Rule rule = (Rule) itr.next(); - matches = rule.matches(request); - } - - return matches; - } - - /** - * Process all the rules in the list, allowing them all to change - * the URI. - * - * @see net.sf.j2ep.model.Rule#process(java.lang.String) - */ - public String process(String uri) { - String returnString = uri; - Iterator itr = rules.iterator(); - while (itr.hasNext()) { - Rule rule = (Rule) itr.next(); - returnString = rule.process(returnString); - } - - return returnString; - } - - /** - * Will do the opposite of process, that is revert all URIs to there default - * value. This method will call all rules in the rule list and call revert on them. - * Rules are called in a reversed order in comparison with process. - * - * @see net.sf.j2ep.model.Rule#revert(java.lang.String) - */ - public String revert(String uri) { - String returnString = uri; - ListIterator itr = rules.listIterator(rules.indexOf(rules.getLast())); - while (itr.hasPrevious()) { - Rule rule = (Rule) itr.previous(); - returnString = rule.revert(returnString); - } - return returnString; - } - - /** - * Returns a String representation of this object. - * - * @return A string representation - */ - public String toString() { - StringBuffer buffer = new StringBuffer(); - - buffer.append("["); - buffer.append("CompositeRule containing "); - - Iterator itr = rules.iterator(); - while (itr.hasNext()) { - Rule rule = (Rule) itr.next(); - buffer.append("("); - buffer.append(rule.getClass().getName()); - buffer.append(") "); - } - - buffer.append(": "); - buffer.append("]"); - return buffer.toString(); - } - -} diff --git a/j2ep_git/src/main/java/net/sf/j2ep/rules/DirectoryRule.java b/j2ep_git/src/main/java/net/sf/j2ep/rules/DirectoryRule.java deleted file mode 100644 index c8e80c1..0000000 --- a/j2ep_git/src/main/java/net/sf/j2ep/rules/DirectoryRule.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep.rules; - -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. - * - * @author Anders Nyman - */ -public class DirectoryRule extends BaseRule { - - /** - * The directory structure. - */ - private String directory; - - /** - * 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."); - } else { - if (!directory.startsWith("/")) { - directory = "/" + directory; - } - if (!directory.endsWith(("/"))) { - directory += "/"; - } - this.directory = directory; - } - } - - /** - * 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. - * - * @see net.sf.j2ep.model.Rule#matches(javax.servlet.http.HttpServletRequest) - */ - public boolean matches(HttpServletRequest request) { - String uri = request.getServletPath(); - 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); - } - - /** - * 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); - } else { - return uri; - } - } - -} diff --git a/j2ep_git/src/main/java/net/sf/j2ep/rules/IPRule.java b/j2ep_git/src/main/java/net/sf/j2ep/rules/IPRule.java deleted file mode 100644 index 05dfc7a..0000000 --- a/j2ep_git/src/main/java/net/sf/j2ep/rules/IPRule.java +++ /dev/null @@ -1,139 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep.rules; - -import java.util.StringTokenizer; - -import javax.servlet.http.HttpServletRequest; - -/** - * This rule will check the IP for the remote user - * allowing the user if his IP is in the specified range. - * - * @author Anders Nyman - */ -public class IPRule extends BaseRule { - - /** - * The starting IP range. - */ - private String startRange; - - /** - * The ending IP range. - */ - private String endRange; - - /** - * Checks the IP for the remote user, if it's in the specified - * range it's a match. - * - * @see net.sf.j2ep.model.Rule#matches(javax.servlet.http.HttpServletRequest) - */ - public boolean matches(HttpServletRequest request) { - boolean match = false; - if (getStartRange() != null && getEndRange() != null) { - String ip = request.getRemoteAddr(); - match = (ip.compareTo(getStartRange()) >= 0 && ip - .compareTo(getEndRange()) <= 0); - } - return match; - } - - /** - * Checks to see is a specified IP range is OK. OK means - * that it's in the range 0.0.0.0 to 255.255.255.255 and - * that the value is a correct IP address. - * - * @param range The value to check - * @return Will give true if the range is OK - */ - private boolean validRange(String range) { - StringTokenizer tokenizer = new StringTokenizer(range, "."); - boolean correct = true; - - if (tokenizer.countTokens() != 4) { - correct = false; - } - try { - while (tokenizer.hasMoreTokens() && correct) { - int tokenValue = Integer.parseInt(tokenizer.nextToken()); - correct = (tokenValue >= 0 && tokenValue <= 255); - } - } catch (NumberFormatException e) { - correct = false; - } - - return correct; - } - - /** - * Sets the beginning range that a connection has to be made from to be - * triggered by this rule. - * - * @param startRange The start of the IP range - */ - public void setStartRange(String startRange) { - if(startRange == null) { - throw new IllegalArgumentException("The startRange cannot be null."); - } else if(!validRange(startRange)) { - throw new IllegalArgumentException("IP range has to be between \"0.0.0.0\" and \"255.255.255.255\"."); - } else if(getEndRange() != null && startRange.compareTo(getEndRange()) > 0) { - throw new IllegalArgumentException("Starting range has to come before the ending range."); - } - this.startRange = startRange; - } - - /** - * Returns the beginning range that a connection has to be made from - * to be triggered by this rule. - * - * @return The start of the IP range - */ - protected String getStartRange() { - return startRange; - } - - /** - * Sets the ending range that a connection has to be made from - * to be triggered by this rule. - * - * @param endRange The end of the IP range - */ - public void setEndRange(String endRange) { - if(endRange == null) { - throw new IllegalArgumentException("The endRange cannot be null."); - } else if(!validRange(endRange)) { - throw new IllegalArgumentException("IP range has to be between \"0.0.0.0\" and \"255.255.255.255\"."); - } else if(getStartRange() != null && endRange.compareTo(getStartRange()) < 0) { - throw new IllegalArgumentException("Ending range has to come after the starting range."); - - } - this.endRange = endRange; - } - - /** - * Returns the ending range that a connection has to be made from - * to be triggered by this rule. - * - * @return The end of the IP range - */ - protected String getEndRange() { - return endRange; - } - -} diff --git a/j2ep_git/src/main/java/net/sf/j2ep/rules/RewriteRule.java b/j2ep_git/src/main/java/net/sf/j2ep/rules/RewriteRule.java deleted file mode 100644 index f5e1e05..0000000 --- a/j2ep_git/src/main/java/net/sf/j2ep/rules/RewriteRule.java +++ /dev/null @@ -1,192 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep.rules; - -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import javax.servlet.http.HttpServletRequest; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -/** - * A rule using regular expressions to rewrite the - * URI. At first the expression will have to match the - * URI, after that the groups from the expression can - * be used to rewrite the URI. - * - * @author Anders Nyman - */ -public class RewriteRule extends BaseRule { - - /** - * Pattern we match the URI on, - */ - private Pattern matchPattern; - - /** - * The string we rewrite to. - */ - private String rewriteTo; - - /** - * Pattern to match when we rewrite links found in HTML. - */ - private Pattern revertPattern; - - /** - * The string we revert links to. - */ - private String revertTo; - - /** - * Marks if we should rewrite incoming links. - */ - private boolean isRewriting; - - /** - * Marks if we are rewriting outgoing links found in HTML. - */ - private boolean isReverting; - - /** - * Logging element supplied by commons-logging. - */ - private static Log log; - - /** - * Basic constructor. - */ - public RewriteRule() { - isRewriting = false; - log = LogFactory.getLog(RewriteRule.class); - } - - /** - * Will check if the URI matches the pattern we have set up. - * - * @see net.sf.j2ep.model.Rule#matches(javax.servlet.http.HttpServletRequest) - */ - public boolean matches(HttpServletRequest request) { - String uri = getURI(request); - Matcher matcher = matchPattern.matcher(uri); - return matcher.matches(); - } - - /** - * Will use the pattern and the rewriteTo string to - * rewrite the URI before using it to connection to - * the end server. - * - * @see net.sf.j2ep.model.Rule#process(java.lang.String) - */ - public String process(String uri) { - String rewritten = uri; - if (isRewriting) { - Matcher matcher = matchPattern.matcher(uri); - rewritten = matcher.replaceAll(rewriteTo); - log.debug("Rewriting URI: " + uri + " >> " + rewritten); - } - return rewritten; - } - - /** - * @see net.sf.j2ep.model.Rule#revert(java.lang.String) - */ - public String revert(String uri) { - String rewritten = uri; - if (isReverting) { - Matcher matcher = revertPattern.matcher(uri); - rewritten = matcher.replaceAll(revertTo); - log.debug("Reverting URI: " + uri + " >> " + rewritten); - } - return rewritten; - } - - - /** - * Sets the regex we will match incoming URIs on. - * - * @param regex The regex - */ - public void setFrom(String regex) { - if (regex == null) { - throw new IllegalArgumentException("From pattern cannot be null."); - } else { - matchPattern = Pattern.compile(regex); - } - } - - /** - * Sets the string we will rewrite incoming URIs to. - * - * @param to The string we rewrite to - */ - public void setTo(String to) { - if (to == null) { - throw new IllegalArgumentException("To string cannot be null."); - } else { - rewriteTo = to; - isRewriting = true; - } - } - - /** - * Sets the regex we use to match outgoing links found. - * - * @param regex The regex - */ - public void setRevertFrom(String regex) { - if (regex == null) { - throw new IllegalArgumentException("Revert pattern cannot be null."); - } else { - revertPattern = Pattern.compile(regex); - } - } - - /** - * Sets the string we rewrite outgoing links to. - * - * @param to The string we rewrite to - */ - public void setRevertTo(String to) { - if (to == null) { - throw new IllegalArgumentException("To string cannot be null."); - } else { - revertTo = to; - isReverting = true; - } - } - - /** - * 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 - * @return The URI for this request including the query string - */ - private String getURI(HttpServletRequest httpRequest) { - String contextPath = httpRequest.getContextPath(); - String uri = httpRequest.getRequestURI().substring(contextPath.length()); - if (httpRequest.getQueryString() != null) { - uri += "?" + httpRequest.getQueryString(); - } - return uri; - } - -} diff --git a/j2ep_git/src/main/java/net/sf/j2ep/rules/TimeRule.java b/j2ep_git/src/main/java/net/sf/j2ep/rules/TimeRule.java deleted file mode 100644 index dc0a519..0000000 --- a/j2ep_git/src/main/java/net/sf/j2ep/rules/TimeRule.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep.rules; - -import java.util.Calendar; - -import javax.servlet.http.HttpServletRequest; - -/** - * A simple rule that checks the hour. If the hour - * is in the specified range we will allow the - * request. - * - * @author Anders Nyman - */ -public class TimeRule extends BaseRule { - - /** - * The start hour - */ - private int startTime; - - /** - * The end hour - */ - private int endTime; - - /** - * Making check to see that the time lies between the start and end - * time. If the time crosses a date barrier, e.g. 23-01 the time will - * be checked correctly. Note the difference between 01-23 and 23-01. - * @see net.sf.j2ep.model.Rule#matches(javax.servlet.http.HttpServletRequest) - */ - public boolean matches(HttpServletRequest request) { - int currentTime = Calendar.getInstance().get(Calendar.HOUR_OF_DAY); - if (startTime > endTime) { - if (currentTime < startTime) { - currentTime += 24; - } - return (currentTime >= startTime && currentTime <= endTime+24); - } else { - return (currentTime >= startTime && currentTime <= endTime); - } - - } - - /** - * Sets the start hour that requests will be allowed. - * - * @param time The hour - */ - public void setStartTime(String time) { - if (time == null) { - throw new IllegalArgumentException("The start time cannot be null"); - } else { - startTime = Integer.parseInt(time)%24; - } - } - - /** - * Sets the end hour that request will be allowed. - * - * @param time The hour - */ - public void setEndTime(String time) { - if (time == null) { - throw new IllegalArgumentException("The end time cannot be null"); - } else { - endTime = Integer.parseInt(time)%24; - } - } - -} diff --git a/j2ep_git/src/main/java/net/sf/j2ep/servers/BaseServer.java b/j2ep_git/src/main/java/net/sf/j2ep/servers/BaseServer.java deleted file mode 100644 index 5b55590..0000000 --- a/j2ep_git/src/main/java/net/sf/j2ep/servers/BaseServer.java +++ /dev/null @@ -1,156 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep.servers; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import net.sf.j2ep.model.Server; - -/** - * A basic implementation of the Server interface using a single host name to map - * all connections. Can be easily extended to create a server that gets it host - * name in some other way. For instance a server fetching the host name from the - * request could be made to change the proxy into a forwarding proxy. - * - * @author Anders Nyman - */ -/** - * - * - * @author Anders Nyman - */ -public class BaseServer extends ServerContainerBase implements Server { - - /** - * Marks if this rule server will do any - * rewriting of links. - */ - private boolean isRewriting; - - /** - * The host and port for this server - */ - private String domainName; - - /** - * The path for this server - */ - private String path; - - /** - * Basic constructor that will initialize - * the directory to "". - */ - public BaseServer() { - path = ""; - isRewriting = false; - } - - /** - * @see net.sf.j2ep.model.ServerContainer#getServer(javax.servlet.http.HttpServletRequest) - */ - public Server getServer(HttpServletRequest request) { - return this; - } - - /** - * Will no do any handling - * @see net.sf.j2ep.model.Server#preExecute(javax.servlet.http.HttpServletRequest) - */ - public HttpServletRequest preExecute(HttpServletRequest request) { - return request; - } - - /** - * Will no do any handling - * @see net.sf.j2ep.model.Server#postExecute(javax.servlet.http.HttpServletResponse) - */ - public HttpServletResponse postExecute(HttpServletResponse response) { - return response; - } - - /** - * @see net.sf.j2ep.model.Server#getDomainName() - */ - public String getDomainName() { - return domainName; - } - - /** - * @see net.sf.j2ep.model.Server#getPath() - */ - public String getPath() { - return path; - } - - /** - * @see net.sf.j2ep.model.ServerContainer#getServerMapped(java.lang.String) - */ - public Server getServerMapped(String location) { - String fullPath = getDomainName() + getPath() + "/"; - if (location.startsWith(fullPath) && isRewriting) { - return this; - } else { - return null; - } - } - - /** - * Set if this server wants absolute links mapped - * for this server to be rewritten. - * - * @param rewrite Should be true if we want to do rewriting - */ - public void setIsRewriting(String rewrite) { - if (rewrite != null && rewrite.equals("true")) { - isRewriting = true; - } - } - - /** - * Sets the host and port we are mapping to. - * - * @param domainName Value to set - */ - public void setDomainName(String domainName) { - if (domainName == null) { - throw new IllegalArgumentException( - "The hostAndPort string cannot be null."); - } else { - this.domainName = domainName; - } - } - - /** - * Sets the path we are mapping to. - * @param path The path - */ - public void setPath(String path) { - if (path == null) { - path = ""; - } else { - this.path = path; - } - } - - /** - * @see net.sf.j2ep.model.Server#setConnectionExceptionRecieved(java.lang.Exception) - */ - public void setConnectionExceptionRecieved(Exception e) { - } -} diff --git a/j2ep_git/src/main/java/net/sf/j2ep/servers/ClusterContainer.java b/j2ep_git/src/main/java/net/sf/j2ep/servers/ClusterContainer.java deleted file mode 100644 index 20a1ebb..0000000 --- a/j2ep_git/src/main/java/net/sf/j2ep/servers/ClusterContainer.java +++ /dev/null @@ -1,321 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep.servers; - -import java.util.HashMap; -import java.util.Iterator; - -import javax.servlet.http.Cookie; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -import net.sf.j2ep.model.Rule; -import net.sf.j2ep.model.Server; - -/** - * A ServerContainer implementation that have multiple domains to choose from. - * When a request is received one server is chosen to handle the request. If the - * request is linked to a session this server will make sure that it's the - * domain that created the session that will process this request. - * - * @author Anders Nyman - */ -public abstract class ClusterContainer extends ServerContainerBase implements ServerStatusListener { - - /** - * Logging element supplied by commons-logging. - */ - private static Log log; - - /** - * The servers in our cluster, - */ - protected HashMap servers; - - /** - * Class that will check if our servers are online or offline. - */ - private ServerStatusChecker statusChecker; - - /** - * Basic constructor - */ - public ClusterContainer() { - servers = new HashMap(); - statusChecker = new ServerStatusChecker(this, 5*60*1000); - statusChecker.start(); - log = LogFactory.getLog(ClusterContainer.class); - } - - /** - * Will create a new server based on the domainName and the directory. - * @param domainName The domain - * @param directory The directory - * @return The created server - */ - protected abstract ClusteredServer createNewServer(String domainName, String directory); - - /** - * Returns the next server in out cluster. - * Is used when we can't get a server from the requests session. - * @return The next server - */ - protected abstract ClusteredServer getNextServer(); - - /** - * Checks the request for any session. If there is a session created we - * make sure that the server returned is the one the issued the session. - * If no session is included in the request we will choose the next server - * in a round-robin fashion. - * - * @see net.sf.j2ep.model.ServerContainer#getServer(javax.servlet.http.HttpServletRequest) - */ - public Server getServer(HttpServletRequest request) { - String serverId = getServerIdFromCookie(request.getCookies()); - ClusteredServer server = (ClusteredServer) servers.get(serverId); - if (server == null || !server.online()) { - server = getNextServer(); - } else { - log.debug("Server found in session"); - } - - if (server.online()) { - log.debug("Using id " + server.getServerId() + " for this request"); - } else { - log.error("All the servers in this cluster are offline. Using id " + server.getServerId() + ", will probably not work"); - } - return server; - } - - /** - * Locates any specification of which server that issued a - * session. If there is no session or the session isn't mapped - * to a specific server null is returned. - * - * @param cookies The cookies so look for a session in - * @return the server's ID or null if no server is found - */ - private String getServerIdFromCookie(Cookie[] cookies) { - String serverId = null; - if (cookies != null) { - for (int i=0; i < cookies.length; i++) { - Cookie cookie = cookies[i]; - if ( isSessionCookie(cookie.getName()) ) { - String value = cookie.getValue(); - String id = value.substring(value.indexOf(".")+1); - if (id.startsWith("server")) { - serverId = id; - } - } - } - } - return serverId; - } - - /** - * Checks if the supplied name of a cookie is known to be a - * session. - * - * @param name The cookies name - * @return true if this cookie is specifying a session - */ - private boolean isSessionCookie(String name) { - return name.equalsIgnoreCase("JSESSIONID") - || name.equalsIgnoreCase("PHPSESSID") - || name.equalsIgnoreCase("ASPSESSIONID") - || name.equalsIgnoreCase("ASP.NET_SessionId"); - } - - /** - * @see net.sf.j2ep.model.ServerContainer#getServerMapped(java.lang.String) - */ - public Server getServerMapped(String location) { - Iterator itr = servers.values().iterator(); - Server match = null; - - while (itr.hasNext() && match == null) { - Server server = (Server) itr.next(); - String fullPath = server.getDomainName() + server.getPath() + "/"; - if (location.startsWith(fullPath)) { - match = server; - } - } - return match; - } - - /** - * Sets the server to offline status. - * Will only handle servers that are ClusteredServers - * @see net.sf.j2ep.servers.ServerStatusListener#serverOffline(net.sf.j2ep.model.Server) - */ - public void serverOffline(Server server) { - if (server instanceof ClusteredServer) { - ((ClusteredServer) server).setOnline(false); - } - } - - /** - * Sets the server to online status. - * Will only handle servers that are ClusteredServers - * @see net.sf.j2ep.servers.ServerStatusListener#serverOnline(net.sf.j2ep.model.Server) - */ - public void serverOnline(Server server) { - if (server instanceof ClusteredServer) { - ((ClusteredServer) server).setOnline(true); - } - } - - /** - * Will create a new ClusteredServer and add it to the hash map. - * - * @param domainName The domain name for the new server - * @param directory The director for the new server. - */ - public synchronized void addServer(String domainName, String directory) { - if (domainName == null) { - throw new IllegalArgumentException("The domainName cannot be null"); - } - if (directory == null) { - directory = ""; - } - - ClusteredServer server = createNewServer(domainName, directory); - servers.put(server.getServerId(), server); - statusChecker.addServer(server); - log.debug("Added server " + domainName + directory + " to the cluster on id " + server.getServerId()); - } - - /** - * A server in the cluster. Will have access to the encapsulating Cluster - * so that we can use its methods to get the rule and such. - * - * @author Anders Nyman - */ - protected class ClusteredServer implements Server { - - /** - * The domain name mapping - */ - private String domainName; - - /** - * The path mapping - */ - private String path; - - /** - * This servers id - */ - private String serverId; - - /** - * The status of this server - */ - private boolean online; - - /** - * Basic constructor that sets the domain name and directory. - * - * @param domainName The domain name - * @param path The directory - */ - public ClusteredServer(String domainName, String path, String serverId) { - this.domainName = domainName; - this.path = path; - this.serverId = serverId; - this.online = true; - } - - /** - * Will wrap the request so the tailing .something, - * identifying the server, is removed from the request. - * - * @see net.sf.j2ep.model.Server#preExecute(javax.servlet.http.HttpServletRequest) - */ - public HttpServletRequest preExecute(HttpServletRequest request) { - return new ClusterRequestWrapper(request); - } - - /** - * Will wrap the response so that sessions are rewritten to - * remove the tailing .something that indicated which server - * the session is linked to. - * @see net.sf.j2ep.model.Server#postExecute(javax.servlet.http.HttpServletResponse) - */ - public HttpServletResponse postExecute(HttpServletResponse response) { - return new ClusterResponseWrapper(response, serverId); - } - - /** - * Notifies the server status checker that a server - * might have gone offline. - * @see net.sf.j2ep.model.Server#setConnectionExceptionRecieved(java.lang.Exception) - */ - public void setConnectionExceptionRecieved(Exception e) { - ClusterContainer.this.statusChecker.interrupt(); - } - - /** - * @see net.sf.j2ep.model.Server#getDomainName() - */ - public String getDomainName() { - return domainName; - } - - /** - * @see net.sf.j2ep.model.Server#getPath() - */ - public String getPath() { - return path; - } - - /** - * Returns the online status of this server - * @return true if the server is online, otherwise false - */ - public boolean online() { - return online; - } - - /** - * Marks if this server should be considered online or - * offline. - * @param online The status of the server - */ - public void setOnline(boolean online) { - this.online = online; - } - - /** - * @see net.sf.j2ep.model.Server#getRule() - */ - public Rule getRule() { - return ClusterContainer.this.getRule(); - } - - /** - * Returns this servers ID. - * @return The server ID - */ - public String getServerId() { - return serverId; - } - } -} diff --git a/j2ep_git/src/main/java/net/sf/j2ep/servers/ClusterRequestWrapper.java b/j2ep_git/src/main/java/net/sf/j2ep/servers/ClusterRequestWrapper.java deleted file mode 100644 index bfeb048..0000000 --- a/j2ep_git/src/main/java/net/sf/j2ep/servers/ClusterRequestWrapper.java +++ /dev/null @@ -1,108 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep.servers; - -import java.util.Enumeration; -import java.util.Vector; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletRequestWrapper; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -/** - * A wrapper for requests that will create it's own set - * of headers. The headers are the same except for - * cookies with a JSESSIONID that has a mark for a specific - * server. More information about this can be found in the - * ClusterServer - * - * @author Anders Nyman - * @see ClusterContainer - */ -public class ClusterRequestWrapper extends HttpServletRequestWrapper { - - /** - * The cookies for this request. - */ - private Vector cookies; - - /** - * Regex to find session in cookies. - */ - private static Pattern sessionPattern = Pattern.compile("((JSESSIONID=|PHPSESSID=|ASPSESSIONID=|ASP.NET_SessionId=)[a-z0-9]+)(\\.[^;\\s]+)", Pattern.CASE_INSENSITIVE | Pattern.CANON_EQ); - - /** - * Logging element supplied by commons-logging. - */ - private static Log log = LogFactory.getLog(ClusterResponseWrapper.class); - - /** - * Constructor, will check all cookies if they include - * JSESSIONID. If they do any extra information about - * which server this session was created for is removed. - * - * @param request The request we wrap. - */ - public ClusterRequestWrapper(HttpServletRequest request) { - super(request); - cookies = new Vector(); - - Enumeration reqCookies = request.getHeaders("Cookie"); - while (reqCookies.hasMoreElements()) { - String value = (String) reqCookies.nextElement(); - Matcher matcher = sessionPattern.matcher(value); - String replaced = matcher.replaceAll("$1"); - if (log.isDebugEnabled() && !replaced.equals(value)) { - log.debug("Session processed, serverId removed \"" + value + "\" >> " + replaced); - } - cookies.add(replaced); - } - } - - /** - * Will return the default request's header unless we are requesting - * a cookie. If it's a cookie we want we will use our own. - * - * @see javax.servlet.http.HttpServletRequest#getHeader(java.lang.String) - */ - public String getHeader(String name) { - if (name.equalsIgnoreCase("cookie")) { - return (String) cookies.firstElement(); - } else { - return super.getHeader(name); - } - } - - /** - * Will return the default request's headers unless we are requesting - * a cookie. If it's a cookie we want we will use our own vector. - * - * @see javax.servlet.http.HttpServletRequest#getHeaders(java.lang.String) - */ - public Enumeration getHeaders(String name) { - if (name.equalsIgnoreCase("cookie")) { - return cookies.elements(); - } else { - return super.getHeaders(name); - } - } - -} diff --git a/j2ep_git/src/main/java/net/sf/j2ep/servers/ClusterResponseWrapper.java b/j2ep_git/src/main/java/net/sf/j2ep/servers/ClusterResponseWrapper.java deleted file mode 100644 index 27b6276..0000000 --- a/j2ep_git/src/main/java/net/sf/j2ep/servers/ClusterResponseWrapper.java +++ /dev/null @@ -1,112 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep.servers; - -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import javax.servlet.http.HttpServletResponse; -import javax.servlet.http.HttpServletResponseWrapper; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -/** - * A wrapper that will make sure sessions are rewritten so - * that the server can be derived from the session. - * - * @author Anders Nyman - */ -public class ClusterResponseWrapper extends HttpServletResponseWrapper { - - /** - * The id of the server we are adding to the session. - */ - private String serverId; - - /** - * Logging element supplied by commons-logging. - */ - private static Log log = LogFactory.getLog(ClusterResponseWrapper.class); - - /** - * Regex to find sessions in cookies. - */ - private static Pattern sessionPattern = Pattern.compile("(JSESSIONID=|PHPSESSID=|ASPSESSIONID=|ASP.NET_SessionId=)([^;\\s\\.]+)", Pattern.CASE_INSENSITIVE | Pattern.CANON_EQ); - - /** - * Basic constructor, will set the id that we should add to add - * the sessions to make sure that the session is tracked to a specific - * server. - * - * @param response The response we wrapp - * @param serverId The id of the server - */ - public ClusterResponseWrapper(HttpServletResponse response, String serverId) { - super(response); - this.serverId = "." + serverId; - } - - /** - * Checks for the set-cookie header. This header will have to be - * rewritten (if it is marking a session) - * - * @see javax.servlet.http.HttpServletResponse#addHeader(java.lang.String, java.lang.String) - */ - public void addHeader(String name, String originalValue) { - String value; - if (name.equalsIgnoreCase("set-cookie")) { - value = rewriteSetCookie(originalValue); - } else { - value = originalValue; - } - super.addHeader(name, value); - } - - /** - * Checks for the set-cookie header. This header will have to be - * rewritten (if it is marking a session) - * - * @see javax.servlet.http.HttpServletResponse#setHeader(java.lang.String, java.lang.String) - */ - public void setHeader(String name, String originalValue) { - String value; - if (name.equalsIgnoreCase("set-cookie")) { - value = rewriteSetCookie(originalValue); - } else { - value = originalValue; - } - super.setHeader(name, value); - } - - /** - * Rewrites the cookie so that we store the server this session - * is linked to. The check making sure that this is a session cookie - * is performed by the regex. - * - * @param value The original header - * @return The rewritten header - */ - private String rewriteSetCookie(String value) { - Matcher matcher = sessionPattern.matcher(value); - String rewritten = matcher.replaceAll("$1$2" + serverId); - if (log.isDebugEnabled() && !rewritten.equals(value)) { - log.debug("Session found and rewritten \"" + value + "\" >> " + rewritten); - } - return rewritten; - } -} diff --git a/j2ep_git/src/main/java/net/sf/j2ep/servers/RoundRobinCluster.java b/j2ep_git/src/main/java/net/sf/j2ep/servers/RoundRobinCluster.java deleted file mode 100644 index 686b7d4..0000000 --- a/j2ep_git/src/main/java/net/sf/j2ep/servers/RoundRobinCluster.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep.servers; - - -/** - * A cluster using round-robin to get the next server in the - * cluster. - * - * @author Anders Nyman - */ -public class RoundRobinCluster extends ClusterContainer { - - /** - * The current number of servers, only used at when the servers are added to - * the hash map. It is assumed that this variable is only modified in a - * single threaded environment. - */ - private int numberOfServers; - - /** - * The currentServer we are using. - */ - private int currentServerNumber; - - /** - * Creates a new round-robin cluster - */ - public RoundRobinCluster() { - super(); - currentServerNumber = 0; - numberOfServers = 0; - } - - /** - * Returns the next in the cluster. The server if found - * using round-robin and checking that the server is marked - * as online. - * - * @return The next server - */ - protected ClusteredServer getNextServer() { - ClusteredServer server; - int start = currentServerNumber; - int current = start; - do { - current = (current + 1) % numberOfServers; - server = (ClusteredServer) servers.get("server" + current); - } while (!server.online() && start != current); - - currentServerNumber = current; - return server; - } - - /** - * @see net.sf.j2ep.servers.ClusterContainer#createNewServer(java.lang.String, java.lang.String) - */ - protected ClusteredServer createNewServer(String domainName, String directory) { - String id = "server" + numberOfServers; - numberOfServers++; - ClusteredServer server = new ClusteredServer(domainName, directory, id); - return server; - } -} diff --git a/j2ep_git/src/main/java/net/sf/j2ep/servers/ServerContainerBase.java b/j2ep_git/src/main/java/net/sf/j2ep/servers/ServerContainerBase.java deleted file mode 100644 index 60663a3..0000000 --- a/j2ep_git/src/main/java/net/sf/j2ep/servers/ServerContainerBase.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep.servers; - -import net.sf.j2ep.model.Rule; -import net.sf.j2ep.model.ServerContainer; - -/** - * A base container that will only handle the setting and getting of - * rules. - * - * @author Anders Nyman - */ -public abstract class ServerContainerBase implements ServerContainer { - - /** - * The rule we are mapped to. - */ - private Rule rule; - - /** - * @see net.sf.j2ep.model.ServerContainer#getRule() - */ - public Rule getRule() { - return rule; - } - - /** - * @see net.sf.j2ep.model.ServerContainer#setRule(net.sf.j2ep.model.Rule) - */ - public void setRule(Rule rule) { - this.rule = rule; - } - -} diff --git a/j2ep_git/src/main/java/net/sf/j2ep/servers/ServerStatusChecker.java b/j2ep_git/src/main/java/net/sf/j2ep/servers/ServerStatusChecker.java deleted file mode 100644 index 8d3b5f4..0000000 --- a/j2ep_git/src/main/java/net/sf/j2ep/servers/ServerStatusChecker.java +++ /dev/null @@ -1,200 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep.servers; - -import java.util.Iterator; -import java.util.LinkedList; - -import net.sf.j2ep.model.Server; - -import org.apache.commons.httpclient.HttpClient; -import org.apache.commons.httpclient.cookie.CookiePolicy; -import org.apache.commons.httpclient.methods.GetMethod; -import org.apache.commons.httpclient.params.HttpClientParams; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -/** - * A class that will check if servers are online and - * notify a listener when servers goes down and comes - * back online again. - * - * @author Anders Nyman - */ -public class ServerStatusChecker extends Thread { - - /** - * The online servers. - */ - private LinkedList online; - - /** - * The offline servers. - */ - private LinkedList offline; - - /** - * Client used to make the connections. - */ - private HttpClient httpClient; - - /** - * The listener we notify when a servers status changes. - */ - private ServerStatusListener listener; - - /** - * The time we wait between checking the servers status. - */ - private long pollingTime; - - /** - * Logging element supplied by commons-logging. - */ - private static Log log = LogFactory.getLog(ServerStatusChecker.class); - - /** - * Basic constructor sets the listener to notify when - * servers goes down/up. Also sets the polling time - * which decides how long we wait between doing checks. - * - * @param listener The listener - * @param pollingTime The time we wait between checks, in milliseconds - */ - public ServerStatusChecker(ServerStatusListener listener, long pollingTime) { - this.listener = listener; - this.pollingTime = Math.max(30*1000, pollingTime); - setPriority(Thread.NORM_PRIORITY-1); - setDaemon(true); - - online = new LinkedList(); - offline = new LinkedList(); - httpClient = new HttpClient(); - httpClient.getParams().setBooleanParameter(HttpClientParams.USE_EXPECT_CONTINUE, false); - httpClient.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES); - } - - /** - * Runs the tests - * @see java.lang.Runnable#run() - */ - public void run() { - for(;;) { - checkOnlineServers(); - checkOfflineServers(); - try { - sleep(pollingTime); - } catch (InterruptedException ie) { - - } - } - } - - /** - * Checks all the servers marked as being online - * if they still are online. - */ - private synchronized void checkOnlineServers() { - Iterator itr; - itr = online.listIterator(); - while (itr.hasNext()) { - Server server = (Server) itr.next(); - String url = getServerURL(server); - GetMethod get = new GetMethod(url); - get.setFollowRedirects(false); - - try { - httpClient.executeMethod(get); - if (!okServerResponse(get.getStatusCode())) { - offline.add(server); - itr.remove(); - log.debug("Server going OFFLINE! " + getServerURL(server)); - listener.serverOffline(server); - } - } catch (Exception e) { - offline.add(server); - itr.remove(); - log.debug("Server going OFFLINE! " + getServerURL(server)); - listener.serverOffline(server); - } finally { - get.releaseConnection(); - } - } - } - - /** - * Checks if the offline servers has come back online - * again. - */ - private synchronized void checkOfflineServers() { - Iterator itr = offline.listIterator(); - while (itr.hasNext()) { - Server server = (Server) itr.next(); - String url = getServerURL(server); - GetMethod get = new GetMethod(url); - get.setFollowRedirects(false); - - try { - httpClient.executeMethod(get); - if (okServerResponse(get.getStatusCode())) { - online.add(server); - itr.remove(); - log.debug("Server back online " + getServerURL(server)); - listener.serverOnline(server); - } - } catch (Exception e) { - listener.serverOffline(server); - } finally { - get.releaseConnection(); - } - } - } - - /** - * Returns the URL to the server - * @param server The server we are connection to - * @return The URL - */ - private String getServerURL(Server server) { - String url = "http://" + server.getDomainName() + server.getPath() + "/"; - return url; - } - - /** - * Checks the status code received from the server and - * validates if this server should be considered online - * or offline. - * - * @param statusCode The status code received - * @return true if the server if online, otherwise false - */ - private boolean okServerResponse(int statusCode) { - return !(statusCode/100 == 5); - } - - /** - * Adds a server that we will check for it's status. - * The server is added to the offline list and will first - * come online when we have managed to make a connection - * to it. - * - * @param server The server to add - */ - public synchronized void addServer(Server server) { - offline.add(server); - } -} \ No newline at end of file diff --git a/j2ep_git/src/main/java/net/sf/j2ep/servers/ServerStatusListener.java b/j2ep_git/src/main/java/net/sf/j2ep/servers/ServerStatusListener.java deleted file mode 100644 index ca3e811..0000000 --- a/j2ep_git/src/main/java/net/sf/j2ep/servers/ServerStatusListener.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep.servers; - -import java.util.EventListener; - -import net.sf.j2ep.model.Server; - -/** - * A listener for status messages for the servers. - * Classes implementing this interface can recieve - * information when a server goes down and when it comes - * back online. - * - * @author Anders Nyman - */ -public interface ServerStatusListener extends EventListener { - - /** - * Notifies that a servers has gone down - * - * @param server Server that is now offline - */ - public void serverOffline(Server server); - - /** - * Notifies that a server is online. This - * means that the server previously has been marked - * as being offline. - * - * @param server Server that is now online - */ - public void serverOnline(Server server); - -} diff --git a/j2ep_git/src/main/webapp/WEB-INF/cactus-web.xml b/j2ep_git/src/main/webapp/WEB-INF/cactus-web.xml deleted file mode 100644 index bdf933e..0000000 --- a/j2ep_git/src/main/webapp/WEB-INF/cactus-web.xml +++ /dev/null @@ -1,23 +0,0 @@ - - - - - j2ep-test - - A J2EE application implementing a reverse proxy. - - - - FilterRedirector - org.apache.cactus.server.FilterTestRedirector - - - FilterRedirector - /FilterRedirector - - - - diff --git a/j2ep_git/src/main/webapp/WEB-INF/config/data.xml b/j2ep_git/src/main/webapp/WEB-INF/config/data.xml deleted file mode 100644 index 5320fc8..0000000 --- a/j2ep_git/src/main/webapp/WEB-INF/config/data.xml +++ /dev/null @@ -1,20 +0,0 @@ - - - - - - - - - - - - - - \ No newline at end of file diff --git a/j2ep_git/src/main/webapp/WEB-INF/web.xml b/j2ep_git/src/main/webapp/WEB-INF/web.xml deleted file mode 100644 index 20dbb1a..0000000 --- a/j2ep_git/src/main/webapp/WEB-INF/web.xml +++ /dev/null @@ -1,27 +0,0 @@ - - - - - j2ep - - A J2EE application implementing a reverse proxy. - - - - Proxy - net.sf.j2ep.ProxyFilter - - dataUrl - /WEB-INF/config/data.xml - - - - - Proxy - /* - - - diff --git a/j2ep_git/src/main/webapp/WEB-INF/web_rewriting.xml b/j2ep_git/src/main/webapp/WEB-INF/web_rewriting.xml deleted file mode 100644 index 008aa1e..0000000 --- a/j2ep_git/src/main/webapp/WEB-INF/web_rewriting.xml +++ /dev/null @@ -1,38 +0,0 @@ - - - - - j2ep - - A J2EE application implementing a reverse proxy. - - - - Rewriter - net.sf.j2ep.RewriteFilter - - dataUrl - /WEB-INF/config/data.xml - - - - - Rewriter - /* - - - - Proxy - net.sf.j2ep.ProxyFilter - - - - Proxy - /* - - - - \ No newline at end of file diff --git a/j2ep_git/src/test/java/net/sf/j2ep/test/AllowHeaderTest.java b/j2ep_git/src/test/java/net/sf/j2ep/test/AllowHeaderTest.java deleted file mode 100644 index bb545bb..0000000 --- a/j2ep_git/src/test/java/net/sf/j2ep/test/AllowHeaderTest.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep.test; - -import junit.framework.TestCase; -import net.sf.j2ep.model.AllowedMethodHandler; - -import org.apache.commons.httpclient.methods.GetMethod; -import org.apache.commons.httpclient.methods.OptionsMethod; - -public class AllowHeaderTest extends TestCase { - - protected void setUp() throws Exception { - super.setUp(); - } - - public void testSetAllowed() { - String allow = "OPTIONS,PROPFIND,OP,PUT"; - AllowedMethodHandler.setAllowedMethods(allow); - assertEquals("Checking that allow is set", allow, AllowedMethodHandler.getAllowHeader()); - - assertTrue("Checking that OPTIONS is allowed", AllowedMethodHandler.methodAllowed("OPTIONS")); - assertTrue("Checking that PROPFIND is allowed", AllowedMethodHandler.methodAllowed("PROPFIND")); - assertTrue("Checking that OP is allowed", AllowedMethodHandler.methodAllowed("OP")); - assertTrue("Checking that PUT is allowed", AllowedMethodHandler.methodAllowed("PUT")); - assertFalse("Checking that PROP isn't allowed", AllowedMethodHandler.methodAllowed("PROP")); - - assertTrue("Checking OPTIONS method", AllowedMethodHandler.methodAllowed(new OptionsMethod())); - assertFalse("Checking GET method", AllowedMethodHandler.methodAllowed(new GetMethod())); - } - - public void testprocessAllowHeader() { - String allow = "OPTIONS,PROPFIND,OP,PUT"; - String correct = "OPTIONS,PUT,"; - AllowedMethodHandler.setAllowedMethods("OPTIONS,GET,PUT,HEJ"); - String returned = AllowedMethodHandler.processAllowHeader(allow); - assertEquals("Checking factory implementation for allow header", correct, returned); - } - -} diff --git a/j2ep_git/src/test/java/net/sf/j2ep/test/ClusterTest.java b/j2ep_git/src/test/java/net/sf/j2ep/test/ClusterTest.java deleted file mode 100644 index 72b62ab..0000000 --- a/j2ep_git/src/test/java/net/sf/j2ep/test/ClusterTest.java +++ /dev/null @@ -1,111 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep.test; - -import java.io.IOException; - -import javax.servlet.ServletException; - -import net.sf.j2ep.ProxyFilter; - -import org.apache.cactus.FilterTestCase; -import org.apache.cactus.WebRequest; -import org.apache.cactus.WebResponse; - -public class ClusterTest extends FilterTestCase { - - private ProxyFilter proxyFilter; - - public void setUp() { - proxyFilter = new ProxyFilter(); - config.setInitParameter("dataUrl", "/WEB-INF/classes/net/sf/j2ep/test/testData.xml"); - try { - proxyFilter.init(config); - } catch (ServletException e) { - fail("Problem with init, error given was " + e.getMessage()); - } - } - - public void beginFirstServer(WebRequest theRequest) { - theRequest.setURL("localhost:8080", "/test", "/testCluster/serverId.jsp", null, null); - theRequest.addHeader("Cookie", "JSESSIONID=somesessionid.server0"); - } - - public void testFirstServer() throws IOException, ServletException { - proxyFilter.doFilter(request, response, filterChain); - } - - public void endFirstServer(WebResponse theResponse) { - assertEquals("Targeting first server", 0, Integer.parseInt(theResponse.getText())); - } - - public void beginSecondServer(WebRequest theRequest) { - theRequest.setURL("localhost:8080", "/test", "/testCluster/serverId.jsp", null, null); - theRequest.addHeader("Cookie", "JSESSIONID=somesessionid.server1"); - } - - public void testSecondServer() throws IOException, ServletException { - proxyFilter.doFilter(request, response, filterChain); - } - - public void endSecondServer(WebResponse theResponse) { - assertEquals("Targeting second server", 1, Integer.parseInt(theResponse.getText())); - } - - public void beginSessionRewriting(WebRequest theRequest) { - theRequest.setURL("localhost:8080", "/test", "/testCluster/createSession.jsp", null, null); - } - - public void testSessionRewriting() throws IOException, ServletException { - proxyFilter.doFilter(request, response, filterChain); - } - - public void endSessionRewriting(WebResponse theResponse) { - String session = theResponse.getCookie("JSESSIONID").getValue(); - assertTrue("Checking that we have rewritten the session", session.indexOf(".server")>-1); - } - - public void beginServerRemoving(WebRequest theRequest) { - theRequest.setURL("localhost:8080", "/test", "/testCluster/viewSession.jsp", null, null); - theRequest.addHeader("Cookie", "JSESSIONID=somesessionid.server1"); - } - - public void testServerRemoving() throws IOException, ServletException { - proxyFilter.doFilter(request, response, filterChain); - } - - public void endServerRemoving(WebResponse theResponse) { - assertEquals("Checking that the session is included", "somesessionid", theResponse.getText().trim()); - } - - public void beginNonExistingServer(WebRequest theRequest) { - theRequest.setURL("localhost:8080", "/test", "/testCluster/serverId.jsp", null, null); - theRequest.addHeader("Cookie", "JSESSIONID=somesessionid.server54"); - } - - public void testNonExistingServer() throws IOException, ServletException { - proxyFilter.doFilter(request, response, filterChain); - } - - public void endNonExistingServer(WebResponse theResponse) { - int id = Integer.parseInt(theResponse.getText()); - String session = theResponse.getCookie("JSESSIONID").getValue(); - assertTrue("Checking that the cluster did send the response to some server anyways", id == 0 || id == 1); - assertTrue("Checking that we now have a new server", session.endsWith(".server"+id)); - } - -} diff --git a/j2ep_git/src/test/java/net/sf/j2ep/test/CompositeRuleTest.java b/j2ep_git/src/test/java/net/sf/j2ep/test/CompositeRuleTest.java deleted file mode 100644 index e4b5b29..0000000 --- a/j2ep_git/src/test/java/net/sf/j2ep/test/CompositeRuleTest.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep.test; - -import junit.framework.TestCase; - -import net.sf.j2ep.rules.CompositeRule; -import net.sf.j2ep.rules.DirectoryRule; -import net.sf.j2ep.rules.IPRule; - -public class CompositeRuleTest extends TestCase { - - private CompositeRule rule; - - protected void setUp() throws Exception { - super.setUp(); - rule = new CompositeRule(); - } - - /* - * Test method for - * 'net.sf.j2ep.rules.CompositeRule.matches(HttpServletRequest)' - */ - public void testMatches() { - IPRule ipRule = new IPRule(); - ipRule.setStartRange("10.0.0.1"); - ipRule.setEndRange("11.0.0.25"); - DirectoryRule dirRule = new DirectoryRule(); - dirRule.setDirectory("/test/"); - - rule.addRule(ipRule); - rule.addRule(dirRule); - - assertTrue("This request should match both rules", rule - .matches(new MockHttpServletRequest() { - public String getRemoteAddr() { - return "11.0.0.0"; - } - - public String getServletPath() { - return "/test/hejsan.html"; - } - })); - - assertFalse("This request shouldn't match the DirectoryRule", rule - .matches(new MockHttpServletRequest() { - public String getRemoteAddr() { - return "11.0.0.14"; - } - - public String getServletPath() { - return "/tesssst/index.html"; - } - })); - } - - public void testEmptyRule() { - assertTrue( - "An empty compositeRule should return true since all the rules are infact matched", - rule.matches(new MockHttpServletRequest() { - })); - } - -} diff --git a/j2ep_git/src/test/java/net/sf/j2ep/test/CycleTest.java b/j2ep_git/src/test/java/net/sf/j2ep/test/CycleTest.java deleted file mode 100644 index d3b5f8b..0000000 --- a/j2ep_git/src/test/java/net/sf/j2ep/test/CycleTest.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep.test; - -import java.io.IOException; - -import javax.servlet.ServletException; - -import net.sf.j2ep.ProxyFilter; - -import org.apache.cactus.FilterTestCase; -import org.apache.cactus.WebRequest; -import org.apache.cactus.WebResponse; - -public class CycleTest extends FilterTestCase { - - private ProxyFilter proxyFilter; - - public void setUp() { - proxyFilter = new ProxyFilter(); - - config.setInitParameter("dataUrl", - "/WEB-INF/classes/net/sf/j2ep/test/testData.xml"); - try { - proxyFilter.init(config); - } catch (ServletException e) { - fail("Problem with init, error given was " + e.getMessage()); - } - } - - public void beginCycle(WebRequest theRequest) { - theRequest.setURL("localhost:8080", "/test", "/testCycle/", null, null); - } - - public void testCycle() throws ServletException, IOException { - proxyFilter.doFilter(request, response, filterChain); - } - - public void endCycle(WebResponse theResponse) { - assertEquals("Checking that the server responded with 500 error", 500, theResponse.getStatusCode()); - } -} \ No newline at end of file diff --git a/j2ep_git/src/test/java/net/sf/j2ep/test/DirectoryMappingTest.java b/j2ep_git/src/test/java/net/sf/j2ep/test/DirectoryMappingTest.java deleted file mode 100644 index 7283da6..0000000 --- a/j2ep_git/src/test/java/net/sf/j2ep/test/DirectoryMappingTest.java +++ /dev/null @@ -1,121 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep.test; - -import java.io.IOException; - -import javax.servlet.FilterChain; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; - -import net.sf.j2ep.ProxyFilter; -import net.sf.j2ep.RewriteFilter; - -import org.apache.cactus.FilterTestCase; -import org.apache.cactus.WebRequest; -import org.apache.cactus.WebResponse; - -public class DirectoryMappingTest extends FilterTestCase { - - private RewriteFilter rewriteFilter; - private FilterChain mockFilterChain; - - public void setUp() { - rewriteFilter = new RewriteFilter(); - - mockFilterChain = new FilterChain() { - ProxyFilter proxyFilter = new ProxyFilter(); - - public void doFilter(ServletRequest theRequest, ServletResponse theResponse) throws IOException, ServletException { - proxyFilter.init(config); - proxyFilter.doFilter(theRequest, theResponse, this); - } - }; - - config.setInitParameter("dataUrl", "/WEB-INF/classes/net/sf/j2ep/test/testData.xml"); - try { - rewriteFilter.init(config); - } catch (ServletException e) { - fail("Problem with init, error given was " + e.getMessage()); - } - } - - public void beginBasicMapping(WebRequest theRequest) { - theRequest.setURL("localhost:8080", "/test", "/testDirectoryMapping/main.jsp", null, null); - } - - public void testBasicMapping() throws IOException, ServletException { - rewriteFilter.doFilter(request, response, mockFilterChain); - } - - public void endBasicMapping(WebResponse theResponse) { - assertEquals("The response code should be 200", 200, theResponse.getStatusCode()); - assertEquals("Test that we got the right page", "/GET/main.jsp", theResponse.getText()); - } - - public void beginRewrite(WebRequest theRequest) { - theRequest.setURL("localhost:8080", "/test", "/testDirectoryMapping/links.jsp", null, null); - } - - public void testRewrite() throws IOException, ServletException { - rewriteFilter.doFilter(request, response, mockFilterChain); - } - - public void endRewrite(WebResponse theResponse) { - assertEquals("The response code should be 200", 200, theResponse.getStatusCode()); - assertTrue("Test no \" path", theResponse.getText().indexOf("Test2")>-1); - assertTrue("Test ' path", theResponse.getText().indexOf("Test2")>-1); - assertTrue("Test path not in directory", theResponse.getText().indexOf("")>-1); - assertTrue("Test relative path", theResponse.getText().indexOf("")>-1); - assertTrue("Test mixed containers", theResponse.getText().indexOf("src=\"/test/testDirectoryMapping/test11.jsp\"")>-1); - assertTrue("Test path included $", theResponse.getText().indexOf("src=\"/test/testDirectoryMapping/$HOME\"")>-1); - } - - public void beginRewriteAbsolute(WebRequest theRequest) { - theRequest.setURL("localhost:8080", "/test", "/testDirectoryMapping/absolute.jsp", null, null); - } - - public void testRewriteAbsolute() throws IOException, ServletException { - rewriteFilter.doFilter(request, response, mockFilterChain); - } - - public void endRewriteAbsolute(WebResponse theResponse) { - assertEquals("The response code should be 200", 200, theResponse.getStatusCode()); - assertTrue("Test absolute path", theResponse.getText().indexOf("http://localhost:8080/test/testDirectoryMapping/test.jsp")>-1); - assertTrue("Test absolute path not mapped", theResponse.getText().indexOf("http://localhost:8080/test/GETT/test6.jsp")>-1); - assertTrue("Test absolute path on different server", theResponse.getText().indexOf("http://localhost:8080/test/testRewriteAbsoluteOther/hej.jsp")>-1); - assertTrue("Test absolute path on different server not mapped", theResponse.getText().indexOf("http://www.test.com/anotherfolder/test")>-1); - assertTrue("Test short absolute path", theResponse.getText().indexOf("http://localhost:8080/test/testRewriteAbsoluteShort/")>-1); - } - - public void beginCSS(WebRequest theRequest) { - theRequest.setURL("localhost:8080", "/test", "/testDirectoryMapping/links.css", null, null); - } - - public void testCSS() throws IOException, ServletException { - rewriteFilter.doFilter(request, response, mockFilterChain); - } - - public void endCSS(WebResponse theResponse) { - assertEquals("The response code should be 200", 200, theResponse.getStatusCode()); - assertTrue("The content is css", theResponse.getConnection().getContentType().indexOf("css")>-1); - assertTrue("Test absolute path", theResponse.getText().indexOf("url(\"/test/testDirectoryMapping/test7.jsp\")")>-1); - assertTrue("Test only rewrite with container", theResponse.getText().indexOf("url(http://localhost:8080/test-response/GET/test8.jsp")>-1); - assertTrue("Test only rewrite with container", theResponse.getText().indexOf("url(\"http://localhost:8080/test/testDirectoryMapping/test9.jsp\")")>-1); - } -} diff --git a/j2ep_git/src/test/java/net/sf/j2ep/test/DirectoryRuleTest.java b/j2ep_git/src/test/java/net/sf/j2ep/test/DirectoryRuleTest.java deleted file mode 100644 index 2223f51..0000000 --- a/j2ep_git/src/test/java/net/sf/j2ep/test/DirectoryRuleTest.java +++ /dev/null @@ -1,115 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep.test; - -import junit.framework.TestCase; - -import net.sf.j2ep.rules.DirectoryRule; - -public class DirectoryRuleTest extends TestCase { - - private DirectoryRule dirRule; - - protected void setUp() throws Exception { - dirRule = new DirectoryRule(); - } - - public void testSetDirectory() { - dirRule.setDirectory("/test/"); - assertEquals("The directory didn't get saves properly", "/test/", dirRule.getDirectory()); - - dirRule.setDirectory("/test"); - assertEquals("The DirectoryRule should add a slash at the end if there isn't one in the input", "/test/", dirRule.getDirectory()); - - dirRule.setDirectory("test"); - assertEquals("The DirectoryRule should add slash at start and end", "/test/", dirRule.getDirectory()); - - dirRule.setDirectory(""); - assertEquals("The DirectoryRule should make this '/'", "/", dirRule.getDirectory()); - - try { - dirRule.setDirectory(null); - fail("Should throw exceptions, directory can't be null"); - } catch (IllegalArgumentException e) { - - } - } - - public void testMatch() { - dirRule.setDirectory("/"); - - assertTrue("This URI should be matched", dirRule.matches( new MockHttpServletRequest() { - public String getServletPath() { - return "/test/hej.html"; - } - })); - - dirRule.setDirectory("test/"); - - assertTrue("This URI should be matched", dirRule.matches( new MockHttpServletRequest() { - public String getServletPath() { - return "/test/hej.html"; - } - })); - - assertFalse("This URI shouldn't be matched", dirRule.matches( new MockHttpServletRequest() { - public String getServletPath() { - return "../test/hej.html"; - } - })); - - assertFalse("This URI shouldn't be matched", dirRule.matches( new MockHttpServletRequest() { - public String getServletPath() { - return "test/.html"; - } - })); - } - - public void testProcess() { - /* In this test it is assumed that the input to process is indeed matched. - * This means that I will not send in illegal input to process. - */ - dirRule.setDirectory("/"); - assertEquals("The URI should be the same as before", "/test/hej.html", dirRule.process("/test/hej.html")); - - dirRule.setDirectory("/test/"); - assertEquals("The initial directory should be removed", "/hej.html", dirRule.process("/test/hej.html")); - - dirRule.setDirectory("/test/hej/"); - assertEquals("The initial directory should be removed", "/hej.html", dirRule.process("/test/hej/hej.html")); - - dirRule.setDirectory("/../test/hej/"); - assertEquals("The initial directory should be removed", "/hej.html", dirRule.process("/../test/hej/hej.html")); - } - - public void testRevert() { - dirRule.setDirectory("/"); - assertEquals("The URI should be the same as before", "hej.html", dirRule.revert("hej.html")); - assertEquals("The URI should be the same as before", "/test/hej.html", dirRule.revert("/test/hej.html")); - - dirRule.setDirectory("/test/"); - assertEquals("The URI should be the same as before", "http://www.hej.com", dirRule.revert("http://www.hej.com")); - assertEquals("The URI should be the same as before", "C:/windows", dirRule.revert("C:/windows")); - assertEquals("The URI should be the same as before", "hej.html", dirRule.revert("hej.html")); - assertEquals("The URI should be the same as before", "../hej.html", dirRule.revert("../hej.html")); - assertEquals("The URI should be rewritten", "/test/hej.html", dirRule.revert("/hej.html")); - - dirRule.setDirectory("/test/test2/"); - assertEquals("The URI should be rewritten", "/test/test2/hum/hej.html", dirRule.revert("/hum/hej.html")); - } - -} diff --git a/j2ep_git/src/test/java/net/sf/j2ep/test/GetTest.java b/j2ep_git/src/test/java/net/sf/j2ep/test/GetTest.java deleted file mode 100644 index d25ef50..0000000 --- a/j2ep_git/src/test/java/net/sf/j2ep/test/GetTest.java +++ /dev/null @@ -1,159 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep.test; - -import java.io.IOException; -import java.net.InetAddress; -import java.net.UnknownHostException; - -import javax.servlet.ServletException; - -import net.sf.j2ep.ProxyFilter; - -import org.apache.cactus.FilterTestCase; -import org.apache.cactus.WebRequest; -import org.apache.cactus.WebResponse; - -/** - * Tests the GetHandler. Is the main testclass for this program since it will - * test some simple default behavior. - * - * @author Anders Nyman - */ -public class GetTest extends FilterTestCase { - - private ProxyFilter proxyFilter; - - public void setUp() { - proxyFilter = new ProxyFilter(); - - config.setInitParameter("dataUrl", "/WEB-INF/classes/net/sf/j2ep/test/testData.xml"); - try { - proxyFilter.init(config); - } catch (ServletException e) { - fail("Problem with init, error given was " + e.getMessage()); - } - } - - public void beginNormalRequest(WebRequest theRequest) { - theRequest.setURL("localhost:8080", "/test", "/GET/main.jsp", null, null); - } - - public void testNormalRequest() throws IOException, ServletException { - proxyFilter.doFilter(request, response, filterChain); - } - - public void endNormalRequest(WebResponse theResponse) { - assertEquals("The response code should be 200", 200, theResponse.getStatusCode()); - assertEquals("Checking for correct page", "/GET/main.jsp", theResponse.getText()); - } - - public void begin404(WebRequest theRequest) { - theRequest.setURL("localhost:8080", "/test", "/GET/nonexistant.jsp", null, null); - } - - public void test404() throws IOException, ServletException { - proxyFilter.doFilter(request, response, filterChain); - } - - public void end404(WebResponse theResponse) { - assertEquals("The response code should be 404", 404, theResponse.getStatusCode()); - } - - public void beginNonExistentServer(WebRequest theRequest) { - theRequest.setURL("localhost:8080", "/test", "/testNonExistentServer/", null, null); - } - - public void testNonExistentServer() throws IOException, ServletException { - proxyFilter.doFilter(request, response, filterChain); - } - - public void endNonExistentServer(WebResponse theResponse) { - assertEquals("The response code should be 504", 504, theResponse.getStatusCode()); - } - - public void beginConditional(WebRequest theRequest) { - theRequest.setURL("localhost:8080", "/test", "/GET/image.gif", null, null); - theRequest.addHeader("If-Unmodified-Since", "Wed, 20 Jul 2000 15:00:00 GMT"); - } - - public void testConditional() throws IOException, ServletException { - proxyFilter.doFilter(request, response, filterChain); - } - - public void endConditional(WebResponse theResponse) { - assertEquals("The response code should be 412", 412, theResponse.getStatusCode()); - } - - public void beginUnhandledMethod(WebRequest theRequest) { - theRequest.setURL("localhost:8080", "/test", "/", null, null); - } - - public void testUnhandledMethod() throws ServletException, IOException { - - MethodWrappingRequest req = new MethodWrappingRequest(request, "JDFJDSJSN"); - proxyFilter.doFilter(req, response, filterChain); - - } - - public void endUnhandledMethod(WebResponse theResponse) { - assertEquals("Checking that we got a 405 response", 405, theResponse.getStatusCode()); - assertEquals("Correct options not returned", - "OPTIONS,GET,HEAD,POST,PUT,DELETE,TRACE", theResponse.getConnection() - .getHeaderField("Allow")); - } - - public void begin405(WebRequest theRequest) { - theRequest.setURL("localhost:8080", "/test", "/GET/405.jsp", null, null); - } - - public void test405() throws IOException, ServletException { - proxyFilter.doFilter(request, response, filterChain); - } - - public void end405(WebResponse theResponse) { - assertEquals("The response code should be 405", 405, theResponse.getStatusCode()); - String allow = theResponse.getConnection().getHeaderField("Allow"); - - assertTrue("Should include OPTIONS", allow.indexOf("OPTIONS")>-1); - assertTrue("Should include GET", allow.indexOf("GET")>-1); - assertFalse("Shouldn't include MYOWNHEADER", allow.indexOf("MYOWNHEADER")>-1); - assertFalse("Shouldn't include PROPFIND", allow.indexOf("PROPFIND")>-1); - } - - public void beginVia(WebRequest theRequest) { - theRequest.setURL("localhost:8080", "/test", "/GET/main.jsp", null, null); - } - - public void testVia() throws IOException, ServletException { - proxyFilter.doFilter(request, response, filterChain); - } - - public void endVia(WebResponse theResponse) { - assertEquals("The response code should be 200", 200, theResponse.getStatusCode()); - String via = theResponse.getConnection().getHeaderField("Via").trim(); - - try { - String serverHostName = InetAddress.getLocalHost().getHostName(); - String expected = "HTTP/1.1 " + serverHostName; - assertEquals("Checking that the via header is included", expected, via); - } catch (UnknownHostException e) { - fail("Couldn't get the hostname needed for header Via"); - } - } - -} diff --git a/j2ep_git/src/test/java/net/sf/j2ep/test/IPRuleTest.java b/j2ep_git/src/test/java/net/sf/j2ep/test/IPRuleTest.java deleted file mode 100644 index 10efaf7..0000000 --- a/j2ep_git/src/test/java/net/sf/j2ep/test/IPRuleTest.java +++ /dev/null @@ -1,151 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep.test; - -import junit.framework.TestCase; - -import net.sf.j2ep.rules.IPRule; - -public class IPRuleTest extends TestCase { - - private MockIPRule ipRule; - - protected void setUp() throws Exception { - ipRule = new MockIPRule(); - super.setUp(); - } - - /* - * Test method for - * 'net.sf.j2ep.rules.IPRule.setStartRange(String)' - */ - public void testSetStartRange() { - try { - ipRule.setStartRange("46548754.22.22.22"); - fail("Should throw Exception, this is not a correct IP"); - } catch (IllegalArgumentException e) { - - } - try { - ipRule.setStartRange(null); - fail("Should throw Exception, this is not a correct IP"); - } catch (IllegalArgumentException e) { - - } - try { - ipRule.setEndRange("10.0.0.0"); - ipRule.setStartRange("10.0.0.1"); - fail("Should throw Exception, startRange has to come before endRange"); - } catch (IllegalArgumentException e) { - - } - } - - /* - * Test method for - * 'org.apache.webapp.reverseproxy.rules.IPRule.getStartRange()' - */ - public void testGetStartRange() { - ipRule.setStartRange("12.0.4.0"); - assertEquals("The given startRange wasn't stored", "12.0.4.0", ipRule - .getStartRange()); - } - - /* - * Test method for - * 'org.apache.webapp.reverseproxy.rules.IPRule.setEndRange(String)' - */ - public void testSetEndRange() { - try { - ipRule.setStartRange("hej.22.22.22"); - fail("Should throw Exception, this is not a correct IP"); - } catch (IllegalArgumentException e) { - - } - try { - ipRule.setStartRange(null); - fail("Should throw Exception, this is not a correct IP"); - } catch (IllegalArgumentException e) { - - } - try { - ipRule.setEndRange("192.45.244.255"); - ipRule.setStartRange("192.45.245.2"); - fail("Should throw Exception, endRange has to come after startRange"); - } catch (IllegalArgumentException e) { - - } - - } - - /* - * Test method for - * 'org.apache.webapp.reverseproxy.rules.IPRule.matches(HttpServletRequest - * request)' - */ - public void testMatches() { - class IPTest extends MockHttpServletRequest { - private String addr; - - public IPTest(String addr) { - this.addr = addr; - } - - public String getRemoteAddr() { - return addr; - } - } - ipRule.setStartRange("10.0.0.1"); - ipRule.setEndRange("10.0.0.5"); - - assertTrue("10.0.0.1 should be allowed", ipRule.matches(new IPTest( - "10.0.0.1"))); - assertTrue("10.0.0.5 should be allowed", ipRule.matches(new IPTest( - "10.0.0.5"))); - assertTrue("10.0.0.3 should be allowed", ipRule.matches(new IPTest( - "10.0.0.3"))); - assertFalse("10.0.0.0 shouldn't be allowed", ipRule.matches(new IPTest( - "10.0.0.0"))); - assertFalse("10.0.0.6 shouldn't be allowed", ipRule.matches(new IPTest( - "10.0.0.6"))); - } - - /* - * Test method for - * 'org.apache.webapp.reverseproxy.rules.IPRule.getEndRange()' - */ - public void testGetEndRange() { - ipRule.setEndRange("12.0.4.0"); - assertEquals("The given endRange wasn't stored", "12.0.4.0", ipRule - .getEndRange()); - } - - /* - * Needed since getStartRange and getEndRange are - * private in the IPRule. - */ - private static class MockIPRule extends IPRule { - public String getStartRange() { - return super.getStartRange(); - } - - public String getEndRange() { - return super.getEndRange(); - } - } - -} diff --git a/j2ep_git/src/test/java/net/sf/j2ep/test/MethodWrappingRequest.java b/j2ep_git/src/test/java/net/sf/j2ep/test/MethodWrappingRequest.java deleted file mode 100644 index f2024a1..0000000 --- a/j2ep_git/src/test/java/net/sf/j2ep/test/MethodWrappingRequest.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep.test; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletRequestWrapper; - -public class MethodWrappingRequest extends HttpServletRequestWrapper { - - private String method; - - public MethodWrappingRequest(HttpServletRequest request, String method) { - super(request); - this.method = method; - } - - public String getMethod() { - return method; - } -} diff --git a/j2ep_git/src/test/java/net/sf/j2ep/test/MockHttpServletRequest.java b/j2ep_git/src/test/java/net/sf/j2ep/test/MockHttpServletRequest.java deleted file mode 100644 index 7541c10..0000000 --- a/j2ep_git/src/test/java/net/sf/j2ep/test/MockHttpServletRequest.java +++ /dev/null @@ -1,253 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep.test; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.security.Principal; -import java.util.Enumeration; -import java.util.Locale; -import java.util.Map; - -import javax.servlet.RequestDispatcher; -import javax.servlet.ServletInputStream; -import javax.servlet.http.Cookie; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpSession; - -public abstract class MockHttpServletRequest implements HttpServletRequest { - - public String getAuthType() { - return null; - } - - public Cookie[] getCookies() { - return null; - } - - public long getDateHeader(String arg0) { - return 0; - } - - public String getHeader(String arg0) { - return null; - } - - public Enumeration getHeaders(String arg0) { - return null; - } - - public Enumeration getHeaderNames() { - return null; - } - - public int getIntHeader(String arg0) throws NumberFormatException { - return 0; - } - - public String getMethod() { - return null; - } - - public String getPathInfo() { - return null; - } - - public String getPathTranslated() { - return null; - } - - public String getContextPath() { - return ""; - } - - public String getQueryString() { - return null; - } - - public String getRemoteUser() { - return null; - } - - public boolean isUserInRole(String arg0) { - return false; - } - - public Principal getUserPrincipal() { - return null; - } - - public String getRequestedSessionId() { - return null; - } - - public String getRequestURI() { - return null; - } - - public StringBuffer getRequestURL() { - return null; - } - - public String getServletPath() { - return null; - } - - public HttpSession getSession(boolean arg0) { - return null; - } - - public HttpSession getSession() { - return null; - } - - public boolean isRequestedSessionIdValid() { - return false; - } - - public boolean isRequestedSessionIdFromCookie() { - return false; - } - - public boolean isRequestedSessionIdFromURL() { - return false; - } - - public boolean isRequestedSessionIdFromUrl() { - return false; - } - - public Object getAttribute(String name) { - return null; - } - - public Enumeration getAttributeNames() { - return null; - } - - public String getCharacterEncoding() { - - return null; - } - - public void setCharacterEncoding(String arg0) - throws UnsupportedEncodingException { - - } - - public int getContentLength() { - return 0; - } - - public String getContentType() { - return null; - } - - public ServletInputStream getInputStream() throws IOException { - return null; - } - - public String getParameter(String arg0) { - return null; - } - - public Enumeration getParameterNames() { - return null; - } - - public String[] getParameterValues(String arg0) { - return null; - } - - public Map getParameterMap() { - return null; - } - - public String getProtocol() { - return null; - } - - public String getScheme() { - return null; - } - - public String getServerName() { - return null; - } - - public int getServerPort() { - return 0; - } - - public BufferedReader getReader() throws IOException { - return null; - } - - public String getRemoteAddr() { - return null; - } - - public String getRemoteHost() { - return null; - } - - public void setAttribute(String arg0, Object arg1) { - - } - - public void removeAttribute(String arg0) { - - } - - public Locale getLocale() { - return null; - } - - public Enumeration getLocales() { - return null; - } - - public boolean isSecure() { - return false; - } - - public RequestDispatcher getRequestDispatcher(String arg0) { - return null; - } - - public String getRealPath(String arg0) { - return null; - } - - public int getRemotePort() { - return 0; - } - - public String getLocalName() { - return null; - } - - public String getLocalAddr() { - return null; - } - - public int getLocalPort() { - return 0; - } - -} diff --git a/j2ep_git/src/test/java/net/sf/j2ep/test/OptionTest.java b/j2ep_git/src/test/java/net/sf/j2ep/test/OptionTest.java deleted file mode 100644 index 7baecbc..0000000 --- a/j2ep_git/src/test/java/net/sf/j2ep/test/OptionTest.java +++ /dev/null @@ -1,118 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep.test; - -import java.io.IOException; - -import javax.servlet.ServletException; - -import net.sf.j2ep.ProxyFilter; - -import org.apache.cactus.FilterTestCase; -import org.apache.cactus.WebRequest; -import org.apache.cactus.WebResponse; - -/** - * Tests the OptionsHandler - * - * @author Anders Nyman - */ -public class OptionTest extends FilterTestCase { - - private ProxyFilter proxyFilter; - - public void setUp() { - proxyFilter = new ProxyFilter(); - - config.setInitParameter("dataUrl", "/WEB-INF/classes/net/sf/j2ep/test/testData.xml"); - try { - proxyFilter.init(config); - } catch (ServletException e) { - fail("Problem with init, error given was " + e.getMessage()); - } - } - - public void beginNoMaxFowards(WebRequest theRequest) { - theRequest.setURL("localhost:8080", "/test", "/", null, null); - } - - public void testNoMaxFowards() throws ServletException, IOException { - MethodWrappingRequest req = new MethodWrappingRequest(request, "OPTIONS"); - proxyFilter.doFilter(req, response, filterChain); - - } - - public void endNoMaxFowards(WebResponse theResponse) { - assertEquals("Correct options not returned", - "GET,HEAD,POST,PUT,DELETE,TRACE,OPTIONS,", theResponse.getConnection() - .getHeaderField("Allow")); - } - - public void beginMaxForwards(WebRequest theRequest) { - theRequest.setURL("localhost:8080", "/test", "/maxForwards", null, null); - theRequest.addHeader("Max-Forwards", "0"); - } - - public void testMaxForwards() throws ServletException, - IOException { - MethodWrappingRequest req = new MethodWrappingRequest(request, "OPTIONS"); - proxyFilter.doFilter(req, response, filterChain); - } - - public void endMaxForwards(WebResponse theResponse) { - assertEquals("Status code check", 200, theResponse.getStatusCode()); - String allow = theResponse.getConnection().getHeaderField("allow"); - /* - * Reason we have to check all and not just compare to string is - * that the order of the methods returned cannot is unknown and unimportant. - */ - assertTrue("Should include OPTIONS", allow.indexOf("OPTIONS")>-1); - assertTrue("Should include GET", allow.indexOf("GET")>-1); - assertTrue("Should include HEAD", allow.indexOf("HEAD")>-1); - assertTrue("Should include POST", allow.indexOf("POST")>-1); - assertTrue("Should include PUT", allow.indexOf("PUT")>-1); - assertTrue("Should include DELETE", allow.indexOf("DELETE")>-1); - assertTrue("Should include TRACE", allow.indexOf("TRACE")>-1); - assertEquals("Content Length should be 0", "0", theResponse.getConnection().getHeaderField("Content-Length")); - } - - public void beginServerWithUnsupportedMethods(WebRequest theRequest) { - theRequest.setURL("localhost:8080", "/test", "/testUnsupportedMethods", null, null); - } - - public void testServerWithUnsupportedMethods() throws ServletException, IOException { - MethodWrappingRequest req = new MethodWrappingRequest(request, "OPTIONS"); - proxyFilter.doFilter(req, response, filterChain); - } - - public void endServerWithUnsupportedMethods(WebResponse theResponse) { - String allow = theResponse.getConnection().getHeaderField("Allow"); - - assertTrue("Should include OPTIONS", allow.indexOf("OPTIONS")>-1); - assertTrue("Should include GET", allow.indexOf("GET")>-1); - assertTrue("Should include HEAD", allow.indexOf("HEAD")>-1); - assertTrue("Should include POST", allow.indexOf("POST")>-1); - assertTrue("Should include DELETE", allow.indexOf("DELETE")>-1); - assertTrue("Should include TRACE", allow.indexOf("TRACE")>-1); - assertFalse("Shouldn't include PROPPATCH", allow.indexOf("PROPPATCH")>-1); - assertFalse("Shouldn't include COPY", allow.indexOf("COPY")>-1); - assertFalse("Shouldn't include MOVE", allow.indexOf("MOVE")>-1); - assertFalse("Shouldn't include LOCK", allow.indexOf("LOCK")>-1); - assertFalse("Shouldn't include UNLOCK", allow.indexOf("UNLOCK")>-1); - assertFalse("Shouldn't include PROPFIND", allow.indexOf("PROPFIND")>-1); - } -} \ No newline at end of file diff --git a/j2ep_git/src/test/java/net/sf/j2ep/test/POSTdata b/j2ep_git/src/test/java/net/sf/j2ep/test/POSTdata deleted file mode 100644 index 228d26d..0000000 --- a/j2ep_git/src/test/java/net/sf/j2ep/test/POSTdata +++ /dev/null @@ -1 +0,0 @@ -here is some data that will be sent using multipart POST diff --git a/j2ep_git/src/test/java/net/sf/j2ep/test/PostTest.java b/j2ep_git/src/test/java/net/sf/j2ep/test/PostTest.java deleted file mode 100644 index e0bc93f..0000000 --- a/j2ep_git/src/test/java/net/sf/j2ep/test/PostTest.java +++ /dev/null @@ -1,98 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep.test; - -import java.io.*; - -import javax.servlet.ServletException; - -import net.sf.j2ep.ProxyFilter; - -import org.apache.cactus.FilterTestCase; -import org.apache.cactus.WebRequest; -import org.apache.cactus.WebResponse; -import org.apache.commons.httpclient.methods.PostMethod; -import org.apache.commons.httpclient.methods.multipart.FilePart; -import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; -import org.apache.commons.httpclient.methods.multipart.Part; -import org.apache.commons.httpclient.methods.multipart.StringPart; - -public class PostTest extends FilterTestCase { - - private ProxyFilter proxyFilter; - - public void setUp() { - proxyFilter = new ProxyFilter(); - - config.setInitParameter("dataUrl", "/WEB-INF/classes/net/sf/j2ep/test/testData.xml"); - try { - proxyFilter.init(config); - } catch (ServletException e) { - fail("Problem with init, error given was " + e.getMessage()); - } - } - - - public void beginSendParam(WebRequest theRequest) { - theRequest.setURL("localhost:8080", "/test", "/POST/param.jsp", null, null); - theRequest.addParameter("testParam", "myValue", WebRequest.POST_METHOD); - } - - public void testSendParam() throws IOException, ServletException { - proxyFilter.doFilter(request, response, filterChain); - } - - public void endSendParam(WebResponse theResponse) { - assertEquals("Checking output", "myValue", theResponse.getText()); - } - - public void beginSendMultipart(WebRequest theRequest) { - theRequest.setURL("localhost:8080", "/test", "/POST/multipart.jsp", null, null); - theRequest.addParameter("tmp", "", WebRequest.POST_METHOD); - - try { - PostMethod post = new PostMethod(); - FilePart filePart = new FilePart("theFile", new File("WEB-INF/classes/net/sf/j2ep/test/POSTdata")); - StringPart stringPart = new StringPart("testParam", "123456"); - Part[] parts = new Part[2]; - parts[0] = stringPart; - parts[1] = filePart; - MultipartRequestEntity reqEntitiy = new MultipartRequestEntity(parts, post.getParams()); - - ByteArrayOutputStream outStream = new ByteArrayOutputStream(); - reqEntitiy.writeRequest(outStream); - - theRequest.setUserData(new ByteArrayInputStream(outStream.toByteArray())); - theRequest.addHeader("content-type", reqEntitiy.getContentType()); - } catch (FileNotFoundException e) { - fail("File was not found " + e.getMessage()); - } catch (IOException e) { - fail("IOException"); - e.printStackTrace(); - } - } - - public void testSendMultipart() throws IOException, ServletException { - proxyFilter.doFilter(request, response, filterChain); - } - - public void endSendMultipart(WebResponse theResponse) { - assertTrue("Checking for the param", theResponse.getText().indexOf("123456")>-1); - assertTrue("Checking for the file data", theResponse.getText().indexOf("here is some data that will be sent using multipart POST")>-1); - } - -} diff --git a/j2ep_git/src/test/java/net/sf/j2ep/test/RewriteRuleTest.java b/j2ep_git/src/test/java/net/sf/j2ep/test/RewriteRuleTest.java deleted file mode 100644 index 20df8fc..0000000 --- a/j2ep_git/src/test/java/net/sf/j2ep/test/RewriteRuleTest.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep.test; - -import junit.framework.TestCase; -import net.sf.j2ep.rules.RewriteRule; - -public class RewriteRuleTest extends TestCase { - - private RewriteRule rule; - - protected void setUp() throws Exception { - rule = new RewriteRule(); - } - - public void testMatch() { - rule.setFrom("/blogs/([0-9]+)/([0-9]+)[/\\w]*"); - assertTrue("Checking for match", rule.matches(new MockHttpServletRequest() { - public String getRequestURI() { - return "/blogs/050505/12/"; - } - })); - - assertTrue("Should get match here too", rule.matches(new MockHttpServletRequest() { - public String getRequestURI() { - return "/blogs/050505/1231"; - } - })); - - - rule.setFrom("^/~(([a-z])[a-z0-9]+)(.*)"); - assertTrue("Should get match here", rule.matches(new MockHttpServletRequest() { - public String getRequestURI() { - return "/~j2ep/docs/api/"; - } - })); - - assertFalse("No match here", rule.matches(new MockHttpServletRequest() { - public String getRequestURI() { - return "/~22ep/docs/api/"; - } - })); - - } - - public void testRewrite() { - rule.setFrom("/blogs/([0-9]+)/([0-9]+)[/\\w]*"); - rule.setTo("/blog?date=$1&id=$2"); - assertEquals("Testing the rewrite", "/blog?date=010101&id=10", rule.process("/blogs/010101/10afd")); - - rule.setFrom("^/~(([a-z])[a-z0-9]+)(.*)"); - rule.setTo("/home/$2/$1/.www$3"); - assertEquals("Testing the rewrite", "/home/j/j2ep/.www/docs/api/", rule.process("/~j2ep/docs/api/")); - } - - public void testRevert() { - rule.setRevertFrom("/blog\\?date=([0-9]+)&id=([0-9]+)"); - rule.setRevertTo("/blogs/$1/$2/"); - assertEquals("Testing the rewrite", "/blogs/051212/1/", rule.revert("/blog?date=051212&id=1")); - - rule.setRevertFrom("^/home/[a-z]/([a-z0-9]+)/.www(.*)$"); - rule.setRevertTo("/~$1$2"); - assertEquals("Testing the rewrite", "/~j2ep/docs/api/", rule.revert("/home/j/j2ep/.www/docs/api/")); - } - -} diff --git a/j2ep_git/src/test/java/net/sf/j2ep/test/StatusCheckerTest.java b/j2ep_git/src/test/java/net/sf/j2ep/test/StatusCheckerTest.java deleted file mode 100644 index a507679..0000000 --- a/j2ep_git/src/test/java/net/sf/j2ep/test/StatusCheckerTest.java +++ /dev/null @@ -1,118 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep.test; - -import junit.framework.TestCase; -import net.sf.j2ep.model.Server; -import net.sf.j2ep.servers.BaseServer; -import net.sf.j2ep.servers.ServerStatusChecker; -import net.sf.j2ep.servers.ServerStatusListener; - -public class StatusCheckerTest extends TestCase { - - private ServerStatusChecker statusChecker; - private TestStatusListener listener; - private BaseServer server; - - protected void setUp() throws Exception { - listener = new TestStatusListener(); - statusChecker = new ServerStatusChecker(listener, 1); - statusChecker.start(); - server = new BaseServer(); - } - - public void testAddServer() { - server.setDomainName("localhost:8080"); - server.setPath("/test-response"); - listener.makeReady(); - statusChecker.addServer(server); - while (!listener.gotResponse()) { - try { - Thread.sleep(100); - } catch (InterruptedException e) { - return; - } - } - assertEquals("Should be getting the expected server", server, listener.getNextOnline()); - - //Taking server offline - listener.makeReady(); - server.setDomainName("locallkjlkjlkjhost:8080"); - statusChecker.interrupt(); - while (!listener.gotResponse()) { - try { - Thread.sleep(100); - } catch (InterruptedException e) { - return; - } - } - assertEquals("Should be getting the expected server", server, listener.getNextOffline()); - - // Taking server online - listener.makeReady(); - server.setDomainName("localhost:8080"); - statusChecker.interrupt(); - while (!listener.gotResponse()) { - try { - Thread.sleep(100); - } catch (InterruptedException e) { - return; - } - } - assertEquals("Should be getting the expected server", server, listener.getNextOnline()); - } - - protected void tearDown() { - statusChecker = null; - } - - private class TestStatusListener implements ServerStatusListener { - - private Server offline; - private Server online; - private volatile boolean gotResponse; - - public synchronized void serverOnline(Server theServer) { - online = theServer; - gotResponse = true; - } - - public synchronized void serverOffline(Server theServer) { - offline = theServer; - gotResponse = true; - } - - public Server getNextOffline() { - return offline; - } - - public Server getNextOnline() { - return online; - } - - public boolean gotResponse() { - return gotResponse; - } - - public void makeReady() { - gotResponse = false; - online = null; - offline = null; - } - - } -} diff --git a/j2ep_git/src/test/java/net/sf/j2ep/test/TimeRuleTest.java b/j2ep_git/src/test/java/net/sf/j2ep/test/TimeRuleTest.java deleted file mode 100644 index dd77000..0000000 --- a/j2ep_git/src/test/java/net/sf/j2ep/test/TimeRuleTest.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep.test; - -import java.util.Calendar; - -import junit.framework.TestCase; -import net.sf.j2ep.rules.TimeRule; - -public class TimeRuleTest extends TestCase { - - TimeRule rule; - int now; - - protected void setUp() throws Exception { - rule = new TimeRule(); - now = Calendar.getInstance().get(Calendar.HOUR_OF_DAY); - } - - public void testMatch() { - rule.setStartTime(""+(now-1)); - rule.setEndTime(""+(now+1)); - assertTrue("Current should be between the start and end", rule.matches(new MockHttpServletRequest() {})); - - rule.setStartTime(""+now); - rule.setEndTime(""+now); - assertTrue("Current should be between the start and end", rule.matches(new MockHttpServletRequest() {})); - - rule.setStartTime(""+(now+24)); - rule.setEndTime(""+(now-23)); - assertTrue("Current should be between the start and end", rule.matches(new MockHttpServletRequest() {})); - } - - public void testNoMatches() { - rule.setStartTime(""+(now+1)); - rule.setEndTime(""+(now+1)); - assertFalse("Current shoudn't be covered", rule.matches(new MockHttpServletRequest() {})); - - rule.setStartTime(""+(now-1)); - rule.setEndTime(""+(now-1)); - assertFalse("Current shoudn't be covered", rule.matches(new MockHttpServletRequest() {})); - } - - public void testDateCrossed() { - rule.setStartTime("02"); - rule.setEndTime("01"); - assertTrue("Current should be between the start and end", rule.matches(new MockHttpServletRequest() {})); - } - -} diff --git a/j2ep_git/src/test/java/net/sf/j2ep/test/TraceTest.java b/j2ep_git/src/test/java/net/sf/j2ep/test/TraceTest.java deleted file mode 100644 index a446dda..0000000 --- a/j2ep_git/src/test/java/net/sf/j2ep/test/TraceTest.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Copyright 1999-2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.sf.j2ep.test; - -import java.io.IOException; -import java.net.InetAddress; -import java.net.UnknownHostException; - -import javax.servlet.ServletException; - -import net.sf.j2ep.ProxyFilter; - -import org.apache.cactus.FilterTestCase; -import org.apache.cactus.WebRequest; -import org.apache.cactus.WebResponse; - -public class TraceTest extends FilterTestCase { - - private ProxyFilter proxyFilter; - - public void setUp() { - proxyFilter = new ProxyFilter(); - - config.setInitParameter("dataUrl", - "/WEB-INF/classes/net/sf/j2ep/test/testData.xml"); - try { - proxyFilter.init(config); - } catch (ServletException e) { - fail("Problem with init, error given was " + e.getMessage()); - } - } - - public void beginNoMaxFowards(WebRequest theRequest) { - theRequest.setURL("localhost:8080", "/test", "/", null, null); - theRequest.addHeader("Via", "HTTP/1.0 fakeserver.com"); - } - - public void testNoMaxFowards() throws ServletException, IOException { - MethodWrappingRequest req = new MethodWrappingRequest(request, "TRACE"); - proxyFilter.doFilter(req, response, filterChain); - } - - public void endNoMaxFowards(WebResponse theResponse) { - String serverHostName = null; - try { - serverHostName = InetAddress.getLocalHost().getHostName(); - } catch (UnknownHostException e) { - fail("Couldn't get the hostname needed for header Via"); - } - - String contentType = theResponse.getConnection().getContentType(); - assertEquals("Checking content-type", "message/http", contentType); - - String expectedVia = "HTTP/1.0 fakeserver.com, " + "HTTP/1.1 " + serverHostName; - assertTrue("Checking that the via header is included", theResponse.getText().indexOf(expectedVia)>-1); - - String expectedUserAgent = "Jakarta Commons-HttpClient/3.0-rc3"; - assertTrue("Checking that user-agent is included", theResponse.getText().indexOf(expectedUserAgent)>-1); - } - - public void beginMaxForwards(WebRequest theRequest) { - theRequest.setURL("localhost:8080", "/test", "/maxForwards", null, null); - theRequest.addHeader("Via", "HTTP/1.0 fakeserver.com"); - theRequest.addHeader("Max-Forwards", "0"); - } - - public void testMaxForwards() throws ServletException, - IOException { - MethodWrappingRequest req = new MethodWrappingRequest(request, "TRACE"); - proxyFilter.doFilter(req, response, filterChain); - } - - public void endMaxForwards(WebResponse theResponse) { - String contentType = theResponse.getConnection().getContentType(); - if (contentType.indexOf(";") != -1) { - contentType = contentType.substring(0, contentType.indexOf(";")); - } - assertEquals("Checking content-type", "message/http", contentType); - - String expectedVia = "HTTP/1.0 fakeserver.com"; - assertTrue("Checking that the via header is included", theResponse.getText().indexOf(expectedVia)>-1); - } -} diff --git a/j2ep_git/src/test/java/net/sf/j2ep/test/testData.xml b/j2ep_git/src/test/java/net/sf/j2ep/test/testData.xml deleted file mode 100644 index cbfaaab..0000000 --- a/j2ep_git/src/test/java/net/sf/j2ep/test/testData.xml +++ /dev/null @@ -1,96 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -