KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > replacementproxy > replacers > FormReplacer


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.replacementproxy.replacers;
21
22 import java.net.MalformedURLException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.regex.Matcher JavaDoc;
27 import java.util.regex.Pattern JavaDoc;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31
32 import com.sslexplorer.boot.CaseInsensitiveMap;
33 import com.sslexplorer.boot.Replacer;
34 import com.sslexplorer.boot.Util;
35 import com.sslexplorer.policyframework.LaunchSession;
36 import com.sslexplorer.util.Utils;
37
38 public class FormReplacer implements Replacer {
39
40     private URL JavaDoc context;
41     private String JavaDoc ticket;
42     
43     static Log log = LogFactory.getLog(FormReplacer.class);
44     
45     public FormReplacer(URL JavaDoc context, String JavaDoc ticket) {
46         this.context = context;
47         this.ticket = ticket;
48     }
49
50     /*
51      * (non-Javadoc)
52      *
53      * @see com.sslexplorer.services.Replacer#getReplacement(java.util.regex.Pattern,
54      * java.util.regex.Matcher, java.lang.String)
55      */

56     public String JavaDoc getReplacement(Pattern JavaDoc pattern, Matcher JavaDoc matcher, String JavaDoc replacementPattern) {
57         String JavaDoc attrs = matcher.group(2);
58         return doRepl(attrs);
59     }
60
61     private String JavaDoc doRepl(String JavaDoc attrs) {
62         StringBuffer JavaDoc attrName = new StringBuffer JavaDoc();
63         char quote = ' ';
64         StringBuffer JavaDoc attrVal = new StringBuffer JavaDoc();
65         boolean doName = true;
66         boolean doVal = false;
67         CaseInsensitiveMap a = new CaseInsensitiveMap();
68         for (int i = 0; i < attrs.length(); i++) {
69             char ch = attrs.charAt(i);
70             if (ch == '\'' && quote == ' ') {
71                 quote = '\'';
72             } else if (ch == '"' && quote == ' ') {
73                 quote = '"';
74             } else if (((doName && (ch == '\r' || ch == '\n')) || (doVal && ch == ' ' && quote == ' ' && attrVal.length() > 0)
75                 || (doVal && ch == '\'' && quote == '\'') || (doVal && ch == '\"' && quote == '\"'))) {
76                 quote = ' ';
77                 String JavaDoc an = attrName.toString();
78                 if (!an.equals("")) {
79                     a.put(attrName.toString(), attrVal.toString());
80                 }
81                 attrName.setLength(0);
82                 attrVal.setLength(0);
83                 doVal = false;
84                 doName = true;
85             } else if (ch == '=' && doName) {
86                 doName = false;
87                 doVal = true;
88             } else {
89                 if (doName) {
90                     if ((ch != ' ' && ch != '\r' && ch != '\n') || attrName.length() > 0) {
91                         attrName.append(ch);
92                     }
93                 } else if (doVal) {
94                     attrVal.append(ch);
95                 }
96             }
97         }
98         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("<form");
99         String JavaDoc sslexUrl = context.toExternalForm();
100         if (a.containsKey("action")) {
101             try {
102                 String JavaDoc contextPath = context.toExternalForm();
103                 if (contextPath.endsWith("/")) {
104                     contextPath = contextPath.substring(0, contextPath.length() - 1);
105                 }
106                 String JavaDoc originalAction = a.get("action").toString();
107                 try {
108                     sslexUrl = new URL JavaDoc(originalAction).toExternalForm();
109                 }
110                 catch(MalformedURLException JavaDoc murle) {
111                     /**
112                      * LDP - Bug fix: the commented out code causes problems with relative
113                      * URLs used in the action parameter of a form
114                      *
115                      * Example:
116                      *
117                      * Context URL:
118                      * http://foobar/OA_HTML/AppsLocalLogin.jsp
119                      *
120                      * Action:
121                      * fndvald.jsp
122                      *
123                      * Results In:
124                      *
125                      * http://foobar/OA_HTML/AppsLocalLogin.jsp/fndvald.jsp
126                      *
127                      * Fixed code results in:
128                      *
129                      * http://foobar/OA_HTML/fndvald.jsp
130                      *
131                      * Slash should not be a problem because URL is intelligent enough to work out that
132                      * the slash means from the root of the URL. I have tested this case and it does indeed
133                      * work the same way regardless of the value of originalAction, in fact originalAction
134                      * can be a completely different URL for example http://3sp.com/fndvald.jsp
135                      *
136                      * Here are the test cases:
137                      *
138                      * new URL("http://foobar/OA_HTML/AppsLocalLogin.jsp/fndvald.jsp", "fndvald.jsp").toExternalForm();
139                      * == http://foobar/OA_HTML/AppsLocalLogin.jsp/fndvald.jsp
140                      *
141                      * new URL("http://foobar/OA_HTML/AppsLocalLogin.jsp/fndvald.jsp", "/fndvald.jsp").toExternalForm();
142                      * == http://foobar/fndvald.jsp
143                      *
144                      * new URL("http://foobar/OA_HTML/AppsLocalLogin.jsp/fndvald.jsp", "http://3sp.com/fndvald.jsp").toExternalForm();
145                      * == http://3sp.com/fndvald.jsp
146                      */

147                     //if(originalAction.startsWith("/")) {
148
sslexUrl = new URL JavaDoc(context, originalAction).toExternalForm();
149                     //}
150
//else {
151
// sslexUrl = new URL(DAVUtilities.concatenatePaths(contextPath, originalAction)).toExternalForm();
152
//}
153
}
154             } catch (MalformedURLException JavaDoc e) {
155                 log.error("Failed to process FORM action", e);
156             }
157         }
158         
159         a.put("action", "/replacementProxyEngine/" + ticket + "/" + Util.urlEncode(Utils.htmlunescape(sslexUrl)) );
160
161         for (Iterator JavaDoc i = a.entrySet().iterator(); i.hasNext();) {
162             buf.append(" ");
163             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
164             buf.append(entry.getKey().toString());
165             buf.append("=\"");
166             buf.append(entry.getValue());
167             buf.append("\"");
168         }
169         buf.append(">");
170
171         return buf.toString();
172     }
173 }
174
Popular Tags