KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > applications > types > HtmlType


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.applications.types;
21
22 import java.io.EOFException JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.regex.Matcher JavaDoc;
26 import java.util.regex.Pattern JavaDoc;
27
28 import javax.servlet.http.HttpServletRequest JavaDoc;
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 /**
58  * An implementation of an {@link com.sslexplorer.applications.ApplicationLauncherType}
59  * that allows launching of applications that may be embedded in a browser
60  * such as Java applets and ActiveX controls.
61  * <p>
62  * If the application extension that supports this launcher has been marked
63  * as <i>SSL-Explorer Aware</i>, this type will process and display the
64  * provided template page in a new browser window. If the application is
65  * not SSL-Explorer Aware then the browser will first contact the VPN client
66  * to set up any required tunnels before redirect back to the server to
67  * process and display the templates.
68  *
69  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
70  */

71 public class HtmlType implements ApplicationLauncherType {
72
73     static Log log = LogFactory.getLog(HtmlType.class);
74     
75     final static String JavaDoc TYPE = "html";
76
77     // Private instance variables
78
private String JavaDoc template;
79     private String JavaDoc window;
80     private boolean sslExplorerAware;
81
82     /* (non-Javadoc)
83      * @see com.sslexplorer.extensions.ExtensionType#start(com.sslexplorer.extensions.ExtensionDescriptor, org.jdom.Element)
84      */

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     /* (non-Javadoc)
98      * @see com.sslexplorer.extensions.ExtensionType#verifyRequiredElements()
99      */

100     public void verifyRequiredElements() throws ExtensionException {
101
102     }
103     
104     /**
105      * Get the template name to use for HTML processing
106      *
107      * @return template name
108      */

109     public String JavaDoc getTemplate() {
110         return template;
111     }
112     
113     /* (non-Javadoc)
114      * @see com.sslexplorer.extensions.ExtensionType#isHidden()
115      */

116     public boolean isHidden() {
117         return false;
118     }
119
120     /* (non-Javadoc)
121      * @see com.sslexplorer.extensions.ExtensionType#getType()
122      */

123     public String JavaDoc getType() {
124         return TYPE;
125     }
126
127     /* (non-Javadoc)
128      * @see com.sslexplorer.extensions.ExtensionType#stop()
129      */

130     public void stop() throws ExtensionException {
131     }
132
133     /* (non-Javadoc)
134      * @see com.sslexplorer.applications.ApplicationLauncherType#launch(java.util.Map, com.sslexplorer.extensions.ExtensionDescriptor, com.sslexplorer.applications.ApplicationShortcut, org.apache.struts.action.ActionMapping, com.sslexplorer.policyframework.LaunchSession, java.lang.String, javax.servlet.http.HttpServletRequest)
135      */

136     public ActionForward launch(Map JavaDoc<String JavaDoc, String JavaDoc> parameters, ExtensionDescriptor descriptor, final ApplicationShortcut shortcut, ActionMapping mapping, LaunchSession launchSession, String JavaDoc returnTo, HttpServletRequest JavaDoc request) throws ExtensionException {
137         
138         // Create the javascript to execute to launch the HTML application window
139

140         String JavaDoc windowParsed = null;
141         if (window != null) {
142             ReplacementEngine engine = new ReplacementEngine();
143             engine.addPattern("\\$\\{shortcut:[^}]*\\}", new Replacer() {
144                 public String JavaDoc getReplacement(Pattern JavaDoc pattern, Matcher JavaDoc matcher, String JavaDoc sequence) {
145                     String JavaDoc match = matcher.group();
146                     try {
147                         String JavaDoc param = match.substring(11, match.length() - 1);
148                         String JavaDoc val = shortcut.getParameters().get(param);
149                         val = val == null ? "" : val;
150                         return val;
151                     } catch (Throwable JavaDoc t) {
152                     }
153                     return "";
154                 }
155             }, null);
156             windowParsed = engine.replace(window);
157         }
158
159         String JavaDoc 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 JavaDoc tunnels = new StringBuffer JavaDoc();
170             
171             if (DefaultAgentManager.getInstance().hasActiveAgent(session)) {
172                 try {
173                     Request JavaDoc 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 JavaDoc name = baw.readString();
182                             String JavaDoc 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 JavaDoc eofe) {
196                     }
197                 } catch (ExtensionException ee) {
198                     throw ee;
199                 } catch (Exception JavaDoc 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     /* (non-Javadoc)
231      * @see com.sslexplorer.applications.ApplicationLauncherType#isAgentRequired(com.sslexplorer.applications.ApplicationShortcut, com.sslexplorer.extensions.ExtensionDescriptor)
232      */

233     public boolean isAgentRequired(ApplicationShortcut shortcut, ExtensionDescriptor descriptor) {
234         return !sslExplorerAware;
235     }
236
237     /* (non-Javadoc)
238      * @see com.sslexplorer.extensions.ExtensionType#activate()
239      */

240     public void activate() throws ExtensionException {
241     }
242
243     /* (non-Javadoc)
244      * @see com.sslexplorer.extensions.ExtensionType#canStop()
245      */

246     public boolean canStop() throws ExtensionException {
247         return true;
248     }
249
250     /* (non-Javadoc)
251      * @see com.sslexplorer.extensions.ExtensionType#descriptorCreated(org.jdom.Element)
252      */

253     public void descriptorCreated(Element element, SessionInfo session) throws IOException JavaDoc {
254     }
255
256     /* (non-Javadoc)
257      * @see com.sslexplorer.extensions.ExtensionType#getTypeBundle()
258      */

259     public String JavaDoc getTypeBundle() {
260         return "applications";
261     }
262
263     /* (non-Javadoc)
264      * @see com.sslexplorer.applications.ApplicationLauncherType#isServiceSide()
265      */

266     public boolean isServerSide() {
267         return false;
268     }
269 }
Popular Tags