KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > replacementproxy > ProxyReplacer


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;
21
22 import java.net.MalformedURLException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.regex.Matcher JavaDoc;
25 import java.util.regex.Pattern JavaDoc;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30 import com.sslexplorer.boot.Replacer;
31 import com.sslexplorer.boot.Util;
32 import com.sslexplorer.policyframework.LaunchSession;
33 import com.sslexplorer.util.Utils;
34
35 /**
36  * @author Brett Smith <brett@3sp.com>
37  */

38 public class ProxyReplacer implements Replacer {
39
40     final static Log log = LogFactory.getLog(ProxyReplacer.class);
41
42     private RequestProcessor requestProcessor;
43     private BaseSearch baseSearch;
44
45     /**
46      *
47      */

48     public ProxyReplacer(RequestProcessor requestProcessor, BaseSearch baseSearch) {
49         super();
50         this.requestProcessor = requestProcessor;
51         this.baseSearch = baseSearch;
52     }
53
54     public String JavaDoc getProxiedPath(URL JavaDoc context, String JavaDoc path, String JavaDoc base) {
55         // TODO leave alone?
56
if (path.startsWith("#")) {
57             return path;
58         }
59
60         // Some types we do not want to proxy at all
61
String JavaDoc lc = path.toLowerCase();
62         if (lc.startsWith("mailto:") || lc.startsWith("javascript:") || lc.startsWith("ftp:") || lc.startsWith("news")) {
63             return path;
64         }
65
66         // Strip the reference of the path (either as a URL or a relative path)
67
// and
68
// store it for
69
// later appending to the proxied path
70
URL JavaDoc pathURL = null;
71         String JavaDoc ref = "";
72         try {
73             pathURL = new URL JavaDoc(path);
74             if (pathURL.getRef() != null) {
75                 ref = "#" + pathURL.getRef();
76                 int idx = path.lastIndexOf('#');
77                 path = path.substring(0, idx);
78                 pathURL = new URL JavaDoc(path);
79             }
80         } catch (MalformedURLException JavaDoc murle) {
81
82             int idx = path.lastIndexOf('#');
83             if (idx != -1) {
84                 ref = "#" + path.substring(idx + 1);
85                 path = path.substring(0, idx);
86             }
87         }
88
89         // The web forward may restrict access to the target URL
90
// if (requestProcessor.getWebForward().getRestrictToURL()) {
91
// try {
92
// URL webForwardURL = new URL(requestProcessor.getWebForward().getDestinationURL());
93
// if (!(InetAddress.getByName(pathURL.getHost()).equals(InetAddress.getByName(webForwardURL.getHost())))) {
94
// throw new Exception("Restricted.");
95
// }
96
// } catch (Exception e) {
97
// return "javascript: void();";
98
// }
99
//
100
// }
101

102         /**
103          * LDP - We have to unescape any HTML entities because our client will not process them
104          * otherwise when a request is received.
105          */

106         String JavaDoc newPath = null;
107         try {
108             if (base != null) {
109                 if (pathURL != null) {
110                     //newPath = "/replacementProxyEngine?" + LaunchSession.LONG_LAUNCH_ID + "=" + requestProcessor.getLaunchId() + "&sslex_url=" + Util.urlEncode(Utils.htmlunescape(pathURL.toExternalForm())) + ref;
111
newPath = "/replacementProxyEngine/" + requestProcessor.getLaunchId() + "/" + Util.urlEncode(Utils.htmlunescape(pathURL.toExternalForm())) + ref;
112                 } else {
113                     // Relative so we need to prepend the base
114
if (path.startsWith("./")) {
115                         path = path.substring(2);
116                     }
117                     try {
118                         URL JavaDoc baseURL = new URL JavaDoc(base);
119                         URL JavaDoc actual = new URL JavaDoc(baseURL, path);
120                         //newPath = "/replacementProxyEngine?" + LaunchSession.LONG_LAUNCH_ID + "=" + requestProcessor.getLaunchId() + "&sslex_url=" + Util.urlEncode(Utils.htmlunescape(actual.toExternalForm())) + ref;
121
newPath = "/replacementProxyEngine/" + requestProcessor.getLaunchId() + "/" + Util.urlEncode(Utils.htmlunescape(actual.toExternalForm())) + ref;
122                     } catch (MalformedURLException JavaDoc murle) {
123                         log.error("Invalidate base URL.", murle);
124                     }
125                 }
126             } else {
127                 URL JavaDoc actual = new URL JavaDoc(context, path);
128                 //newPath = "/replacementProxyEngine?" + LaunchSession.LONG_LAUNCH_ID + "=" + requestProcessor.getLaunchId() + "&sslex_url=" + Util.urlEncode(Utils.htmlunescape(actual.toExternalForm())) + ref;
129
newPath = "/replacementProxyEngine/" + requestProcessor.getLaunchId() + "/" + Util.urlEncode(Utils.htmlunescape(actual.toExternalForm())) + ref;
130             }
131         } catch (MalformedURLException JavaDoc ex) {
132             log.error("Could not convert path from '" + path + "' using " + context.toExternalForm(), ex);
133             newPath = path;
134         }
135         if (newPath == null) {
136             log.warn("Failed to proxy path " + path + ", using original.");
137             newPath = path;
138         }
139         if (log.isDebugEnabled())
140             log.debug("Created proxy path " + newPath);
141         
142         // Unescape the last encoded path
143
int idx = newPath.lastIndexOf("%2F");
144         newPath = newPath.substring(0, idx) + "/" + newPath.substring(idx + 3);
145         
146         return newPath;
147     }
148
149     public String JavaDoc getReplacement(Pattern JavaDoc pattern, Matcher JavaDoc matcher, String JavaDoc replacementPattern) {
150         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
151         char ch;
152         boolean esc = false;
153         try {
154             for (int i = 0; i < replacementPattern.length(); i++) {
155                 ch = replacementPattern.charAt(i);
156                 if (esc) {
157                     buf.append(ch);
158                     esc = false;
159                 } else {
160                     if (ch == '\\') {
161                         esc = true;
162                     } else if (ch == '%') {
163                         i++;
164                         ch = replacementPattern.charAt(i);
165                         int group = Integer.parseInt(String.valueOf(ch));
166                         String JavaDoc groupText = matcher.group(group);
167                         if (groupText != null && !groupText.equals("")) {
168                             buf.append(getProxiedPath(requestProcessor.getRequestParameters().getProxiedURLBase(), groupText, baseSearch.getBase()));
169                         }
170                     } else if (ch == '$') {
171                         i++;
172                         ch = replacementPattern.charAt(i);
173                         int group = Integer.parseInt(String.valueOf(ch));
174                         String JavaDoc groupText = matcher.group(group);
175                         if (groupText != null && !groupText.equals("")) {
176                             buf.append(groupText);
177                         }
178                     } else if (ch == '^') {
179                         i++;
180                         ch = replacementPattern.charAt(i);
181                         if(ch == 'T') {
182                             buf.append(requestProcessor.getLaunchId());
183                         }
184                         else {
185                             int group = Integer.parseInt(String.valueOf(ch));
186                             String JavaDoc groupText = matcher.group(group);
187                             if (groupText != null && !groupText.equals("")) {
188                                 buf.append(new URL JavaDoc(requestProcessor.getRequestParameters().getProxiedURLBase(), groupText).toExternalForm());
189                             }
190                         }
191                     } else if(ch == '~') {
192                         i++;
193                         ch = replacementPattern.charAt(i);
194                         int group = Integer.parseInt(String.valueOf(ch));
195                         String JavaDoc groupText = matcher.group(group);
196                         if (groupText != null && !groupText.equals("")) {
197                             groupText = groupText.replaceAll("\\\\", "");
198                             buf.append(getProxiedPath(requestProcessor.getRequestParameters().getProxiedURLBase(), groupText, baseSearch.getBase()));
199                         }
200                     }else {
201                         buf.append(ch);
202                     }
203                 }
204             }
205             return buf.toString();
206         } catch (Throwable JavaDoc t) {
207             log.warn("Invalid replacement pattern " + replacementPattern, t);
208         }
209         return replacementPattern;
210     }
211
212 }
213
Popular Tags