1 19 20 package com.sslexplorer.core.stringreplacement; 21 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.io.OutputStream ; 25 import java.util.Map ; 26 27 import javax.servlet.http.HttpServletRequest ; 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 VARIABLE_PATTERN = "\\$\\{[^}]*\\}"; 46 47 private ExtensionBundle extensionBundle; 48 private ExtensionDescriptor extensionDescriptor; 49 private Map parameters; 50 private RequestHandlerRequest request; 51 private HttpServletRequest servletRequest; 52 private SessionInfo session; 53 Policy policy; 54 private ReplacementEngine.Encoder encoder; 55 private String username; 56 private int realm; 57 58 public void setUsernameAndRealm(String username, int Realm) { 59 this.username = username; 60 this.realm = realm; 61 } 62 63 public void setApplicationShortcut(ExtensionDescriptor extensionDescriptor, Map 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 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 servletRequest) { 94 this.servletRequest = servletRequest; 95 } 96 97 public void setSession(SessionInfo session) { 98 this.session = session; 99 } 100 101 public long replace(InputStream in, OutputStream out, String charset) throws IOException { 102 if (log.isDebugEnabled()) 103 log.debug("Replacing using streams, reading stream into memory"); 104 StringBuffer str = new StringBuffer (4096); 105 byte[] buf = new byte[32768]; 106 int read; 107 while ((read = in.read(buf)) > -1) { 108 str.append(charset == null ? new String (buf, 0, read) : new String (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 replace(String input) { 122 ReplacementEngine engine = new ReplacementEngine(); 123 if (encoder != null) { 124 engine.setEncoder(encoder); 125 } 126 127 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 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 |