KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > core > stringreplacement > VariableReplacement


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.core.stringreplacement;
21
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import javax.servlet.http.HttpServletRequest JavaDoc;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31
32 import com.sslexplorer.boot.ReplacementEngine;
33 import com.sslexplorer.boot.Replacer;
34 import com.sslexplorer.boot.RequestHandlerRequest;
35 import com.sslexplorer.extensions.ExtensionBundle;
36 import com.sslexplorer.extensions.ExtensionDescriptor;
37 import com.sslexplorer.policyframework.LaunchSession;
38 import com.sslexplorer.policyframework.Policy;
39 import com.sslexplorer.security.SessionInfo;
40
41 public class VariableReplacement {
42     final static Log log = LogFactory.getLog(VariableReplacement.class);
43
44     final static Replacer GLOBAL_REPLACER = new GlobalReplacer();
45     public final static String JavaDoc VARIABLE_PATTERN = "\\$\\{[^}]*\\}";
46
47     private ExtensionBundle extensionBundle;
48     private ExtensionDescriptor extensionDescriptor;
49     private Map JavaDoc parameters;
50     private RequestHandlerRequest request;
51     private HttpServletRequest JavaDoc servletRequest;
52     private SessionInfo session;
53     Policy policy;
54     private ReplacementEngine.Encoder encoder;
55     private String JavaDoc username;
56     private int realm;
57
58     public void setUsernameAndRealm(String JavaDoc username, int Realm) {
59         this.username = username;
60         this.realm = realm;
61     }
62
63     public void setApplicationShortcut(ExtensionDescriptor extensionDescriptor, Map JavaDoc parameters) {
64         this.extensionDescriptor = extensionDescriptor;
65         this.parameters = parameters;
66     }
67
68     public void setEncoder(ReplacementEngine.Encoder encoder) {
69         this.encoder = encoder;
70     }
71
72     public void setExtensionBundle(ExtensionBundle extensionBundle) {
73         this.extensionBundle = extensionBundle;
74     }
75
76     public void setParameters(Map JavaDoc parameters) {
77         this.parameters = parameters;
78     }
79
80     public void setLaunchSession(LaunchSession launchSession) {
81         this.policy = launchSession.getPolicy();
82         this.session = launchSession.getSession();
83     }
84
85     public void setPolicy(Policy policy) {
86         this.policy = policy;
87     }
88
89     public void setRequest(RequestHandlerRequest request) {
90         this.request = request;
91     }
92
93     public void setServletRequest(HttpServletRequest JavaDoc servletRequest) {
94         this.servletRequest = servletRequest;
95     }
96
97     public void setSession(SessionInfo session) {
98         this.session = session;
99     }
100
101     public long replace(InputStream JavaDoc in, OutputStream JavaDoc out, String JavaDoc charset) throws IOException JavaDoc {
102         if (log.isDebugEnabled())
103             log.debug("Replacing using streams, reading stream into memory");
104         StringBuffer JavaDoc str = new StringBuffer JavaDoc(4096);
105         byte[] buf = new byte[32768];
106         int read;
107         while ((read = in.read(buf)) > -1) {
108             str.append(charset == null ? new String JavaDoc(buf, 0, read) : new String JavaDoc(buf, 0, read, charset));
109             if (log.isDebugEnabled())
110                 log.debug("Got block of " + read + ", waiting for next one");
111         }
112         if (log.isDebugEnabled())
113             log.debug("Read all blocks, performing replacement");
114         byte[] b = charset == null ? replace(str.toString()).getBytes() : replace(str.toString()).getBytes(charset);
115         if (log.isDebugEnabled())
116             log.debug("Writing replaced content back (" + b.length + " bytes)");
117         out.write(b);
118         return b.length;
119     }
120
121     public String JavaDoc replace(String JavaDoc input) {
122         ReplacementEngine engine = new ReplacementEngine();
123         if (encoder != null) {
124             engine.setEncoder(encoder);
125         }
126         
127         // User attributes and policy attributes can nest replacement variables so these must
128
// be done first
129
if (session != null) {
130             engine.addPattern(VARIABLE_PATTERN, new UserAttributesReplacer(session.getUser().getPrincipalName(), session.getUser()
131                             .getRealm().getResourceId()), null);
132         } else {
133             if (username != null) {
134                 engine.addPattern(VARIABLE_PATTERN, new UserAttributesReplacer(username, realm), null);
135             }
136         }
137         
138         if (policy != null) {
139             engine.addPattern(VARIABLE_PATTERN, new PolicyAttributesReplacer(policy), null);
140         }
141         
142         // Now do the variable patterns that may be nested in attributes
143

144         engine.addPattern(VARIABLE_PATTERN, GLOBAL_REPLACER, null);
145         if (extensionBundle != null)
146             engine.addPattern(VARIABLE_PATTERN, new ExtensionBundleReplacer(extensionBundle), null);
147         if (extensionDescriptor != null) {
148             engine.addPattern(VARIABLE_PATTERN, new ExtensionDescriptorReplacer(extensionDescriptor, parameters), null);
149         }
150         if (request != null) {
151             engine.addPattern(VARIABLE_PATTERN, new RequestHandlerRequestReplacer(request), null);
152         }
153         if (servletRequest != null) {
154             engine.addPattern(VARIABLE_PATTERN, new ServletRequestReplacer(servletRequest), null);
155         }
156         if (session != null) {
157             engine.addPattern(VARIABLE_PATTERN, new SessionInfoReplacer(session), null);
158         }
159         return engine.replace(input);
160     }
161 }
Popular Tags