1 19 20 package com.sslexplorer.applications.types; 21 22 import java.io.EOFException ; 23 import java.io.IOException ; 24 import java.util.Map ; 25 import java.util.regex.Matcher ; 26 import java.util.regex.Pattern ; 27 28 import javax.servlet.http.HttpServletRequest ; 29 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 import org.apache.struts.Globals; 33 import org.apache.struts.action.ActionForward; 34 import org.apache.struts.action.ActionMapping; 35 import org.apache.struts.action.ActionMessages; 36 import org.jdom.Element; 37 38 import com.maverick.multiplex.Request; 39 import com.maverick.util.ByteArrayReader; 40 import com.sslexplorer.agent.AgentTunnel; 41 import com.sslexplorer.agent.DefaultAgentManager; 42 import com.sslexplorer.applications.ApplicationLauncherType; 43 import com.sslexplorer.applications.ApplicationService; 44 import com.sslexplorer.applications.ApplicationShortcut; 45 import com.sslexplorer.applications.ApplicationsPlugin; 46 import com.sslexplorer.boot.ReplacementEngine; 47 import com.sslexplorer.boot.Replacer; 48 import com.sslexplorer.boot.Util; 49 import com.sslexplorer.core.BundleActionMessage; 50 import com.sslexplorer.core.RedirectWithMessages; 51 import com.sslexplorer.extensions.ExtensionDescriptor; 52 import com.sslexplorer.extensions.ExtensionException; 53 import com.sslexplorer.policyframework.LaunchSession; 54 import com.sslexplorer.security.Constants; 55 import com.sslexplorer.security.SessionInfo; 56 57 71 public class HtmlType implements ApplicationLauncherType { 72 73 static Log log = LogFactory.getLog(HtmlType.class); 74 75 final static String TYPE = "html"; 76 77 private String template; 79 private String window; 80 private boolean sslExplorerAware; 81 82 85 public void start(ExtensionDescriptor descriptor, Element element) throws ExtensionException { 86 if (element.getName().equals(TYPE)) { 87 sslExplorerAware = "true".equalsIgnoreCase(element.getAttributeValue("sslexplorerAware")); 88 window = element.getAttributeValue("window"); 89 template = element.getAttributeValue("template"); 90 if (template == null) { 91 throw new ExtensionException(ExtensionException.FAILED_TO_PROCESS_DESCRIPTOR, "The <html> tag requires the template attribute."); 92 } 93 } 94 95 } 96 97 100 public void verifyRequiredElements() throws ExtensionException { 101 102 } 103 104 109 public String getTemplate() { 110 return template; 111 } 112 113 116 public boolean isHidden() { 117 return false; 118 } 119 120 123 public String getType() { 124 return TYPE; 125 } 126 127 130 public void stop() throws ExtensionException { 131 } 132 133 136 public ActionForward launch(Map <String , String > parameters, ExtensionDescriptor descriptor, final ApplicationShortcut shortcut, ActionMapping mapping, LaunchSession launchSession, String returnTo, HttpServletRequest request) throws ExtensionException { 137 138 140 String windowParsed = null; 141 if (window != null) { 142 ReplacementEngine engine = new ReplacementEngine(); 143 engine.addPattern("\\$\\{shortcut:[^}]*\\}", new Replacer() { 144 public String getReplacement(Pattern pattern, Matcher matcher, String sequence) { 145 String match = matcher.group(); 146 try { 147 String param = match.substring(11, match.length() - 1); 148 String val = shortcut.getParameters().get(param); 149 val = val == null ? "" : val; 150 return val; 151 } catch (Throwable t) { 152 } 153 return ""; 154 } 155 }, null); 156 windowParsed = engine.replace(window); 157 } 158 159 String script = ""; 160 if (sslExplorerAware) { 161 script = "myRef = window.open('" + "/getHTMLApplication.do?" 162 + LaunchSession.LAUNCH_ID + "=" + launchSession.getId() 163 + "&sslexplorer=' + escape(window.location.protocol + '//' + " 164 + "window.location.host + ':' + window.location.port)," + "'_blank'," + 165 "'" + (windowParsed == null ? "" : windowParsed) + "');myRef.focus();"; 166 } else { 167 168 SessionInfo session = launchSession.getSession(); 169 StringBuffer tunnels = new StringBuffer (); 170 171 if (DefaultAgentManager.getInstance().hasActiveAgent(session)) { 172 try { 173 Request agentRequest = ((ApplicationService) DefaultAgentManager.getInstance().getService(ApplicationService.class)).launchApplication(launchSession); 174 AgentTunnel agent = DefaultAgentManager.getInstance().getAgentBySession(launchSession.getSession()); 175 if (!agent.sendRequest(agentRequest, true, 60000)) { 176 throw new ExtensionException(ExtensionException.AGENT_REFUSED_LAUNCH); 177 } 178 ByteArrayReader baw = new ByteArrayReader(agentRequest.getRequestData()); 179 try { 180 while(true) { 181 String name = baw.readString(); 182 String hostname = baw.readString(); 183 long port = baw.readInt(); 184 if(tunnels.length() > 0) { 185 tunnels.append(","); 186 } 187 tunnels.append(name); 188 tunnels.append(":"); 189 tunnels.append(hostname); 190 tunnels.append(":"); 191 tunnels.append(port); 192 log.info("Got temporary tunnel '" + name + "' = " + port + " (" + hostname + ")"); 193 } 194 } 195 catch(EOFException eofe) { 196 } 197 } catch (ExtensionException ee) { 198 throw ee; 199 } catch (Exception e) { 200 throw new ExtensionException(ExtensionException.INTERNAL_ERROR, e); 201 } 202 203 } 204 else { 205 throw new ExtensionException(ExtensionException.NO_AGENT); 206 } 207 208 script = "myRef = window.open('" + "/getHTMLApplication.do?" 209 + LaunchSession.LAUNCH_ID + "=" + launchSession.getId() 210 + "&tunnels=" + Util.urlEncode(tunnels.toString()) 211 + "&sslexplorer=' + escape(window.location.protocol + '//' + " 212 + "window.location.host + ':' + window.location.port)," + "'_blank'," + 213 "'" + (windowParsed == null ? "" : windowParsed) + "');myRef.focus();"; 214 } 215 216 ActionMessages msgs = new ActionMessages(); 217 msgs.add(Globals.MESSAGE_KEY, new BundleActionMessage(ApplicationsPlugin.MESSAGE_RESOURCES_KEY, "launchApplication.launched", shortcut.getResourceName())); 218 219 if(request != null) { 220 request.setAttribute(Constants.REQ_ATTR_FORWARD_TO, 221 RedirectWithMessages.addMessages(request, Globals.MESSAGE_KEY, msgs, returnTo) ); 222 request.setAttribute(Constants.REQ_ATTR_FOLDER, ""); 223 request.setAttribute(Constants.REQ_ATTR_TARGET, ""); 224 request.setAttribute(Constants.REQ_ATTR_EXEC_ON_LOAD, script); 225 } 226 227 return mapping.findForward("redirect"); 228 } 229 230 233 public boolean isAgentRequired(ApplicationShortcut shortcut, ExtensionDescriptor descriptor) { 234 return !sslExplorerAware; 235 } 236 237 240 public void activate() throws ExtensionException { 241 } 242 243 246 public boolean canStop() throws ExtensionException { 247 return true; 248 } 249 250 253 public void descriptorCreated(Element element, SessionInfo session) throws IOException { 254 } 255 256 259 public String getTypeBundle() { 260 return "applications"; 261 } 262 263 266 public boolean isServerSide() { 267 return false; 268 } 269 } | Popular Tags |