1 19 20 package com.sslexplorer.applications.actions; 21 22 import java.io.BufferedReader ; 23 import java.io.File ; 24 import java.io.FileInputStream ; 25 import java.io.InputStream ; 26 import java.io.InputStreamReader ; 27 import java.io.OutputStream ; 28 import java.io.OutputStreamWriter ; 29 import java.io.PrintWriter ; 30 import java.net.URL ; 31 import java.util.Iterator ; 32 import java.util.Map ; 33 import java.util.StringTokenizer ; 34 import java.util.regex.Matcher ; 35 import java.util.regex.Pattern ; 36 37 import javax.servlet.http.HttpServletRequest ; 38 import javax.servlet.http.HttpServletResponse ; 39 40 import org.apache.commons.logging.Log; 41 import org.apache.commons.logging.LogFactory; 42 import org.apache.struts.action.ActionForm; 43 import org.apache.struts.action.ActionForward; 44 import org.apache.struts.action.ActionMapping; 45 46 import com.sslexplorer.applications.ApplicationShortcut; 47 import com.sslexplorer.applications.ApplicationShortcutEventConstants; 48 import com.sslexplorer.applications.types.HtmlType; 49 import com.sslexplorer.boot.HttpConstants; 50 import com.sslexplorer.boot.ReplacementEngine; 51 import com.sslexplorer.boot.Replacer; 52 import com.sslexplorer.boot.Util; 53 import com.sslexplorer.core.CoreAttributeConstants; 54 import com.sslexplorer.core.CoreEvent; 55 import com.sslexplorer.core.CoreServlet; 56 import com.sslexplorer.core.actions.AuthenticatedAction; 57 import com.sslexplorer.core.stringreplacement.VariableReplacement; 58 import com.sslexplorer.extensions.ExtensionDescriptor; 59 import com.sslexplorer.extensions.store.ExtensionStore; 60 import com.sslexplorer.policyframework.LaunchSession; 61 import com.sslexplorer.policyframework.LaunchSessionFactory; 62 import com.sslexplorer.policyframework.Policy; 63 import com.sslexplorer.policyframework.PolicyDatabaseFactory; 64 import com.sslexplorer.policyframework.ResourceAccessEvent; 65 import com.sslexplorer.security.Constants; 66 import com.sslexplorer.security.SessionInfo; 67 68 91 public class GetHTMLApplicationAction extends AuthenticatedAction { 92 93 final static Log log = LogFactory.getLog(GetHTMLApplicationAction.class); 94 final static String VARIABLE_PATTERN = "\\$\\{[^}]*\\}"; 95 96 99 public GetHTMLApplicationAction() { 100 super(); 101 } 102 103 106 public ActionForward onExecute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 107 throws Exception { 108 String launchSessionId = request.getParameter(LaunchSession.LAUNCH_ID); 109 LaunchSession launchSession = LaunchSessionFactory.getInstance().getLaunchSession(launchSessionId); 110 if (launchSession == null) { 111 throw new Exception ("No launch session id " + launchSessionId); 112 } 113 final ApplicationShortcut shortcut = (ApplicationShortcut)launchSession.getResource(); 114 launchSession.checkAccessRights(null, getSessionInfo(request)); 115 ExtensionDescriptor app = ExtensionStore.getInstance().getExtensionDescriptor(shortcut.getApplication()); 116 if (app == null) { 117 throw new Exception ("No application named " + shortcut.getApplication() + "."); 118 } 119 120 if (!(app.getExtensionType() instanceof HtmlType)) { 121 throw new Exception (getClass().getName() + " only supports applications of type " + HtmlType.class + "."); 122 } 123 124 126 HtmlType type = (HtmlType) app.getExtensionType(); 127 File file = new File (app.getApplicationBundle().getBaseDir(), type.getTemplate()); 128 if (log.isDebugEnabled()) 129 log.debug("Loading template " + file.getAbsolutePath()); 130 131 InputStream in = null; 132 StringBuffer template = new StringBuffer ((int) file.length()); 133 try { 134 in = new FileInputStream (file); 135 String line = null; 136 BufferedReader reader = new BufferedReader (new InputStreamReader (in)); 137 while ((line = reader.readLine()) != null) { 138 if (template.length() != 0) { 139 template.append("\n"); 140 } 141 template.append(line); 142 } 143 } finally { 144 Util.closeStream(in); 145 } 146 147 if (log.isDebugEnabled()) 148 log.debug("Parsing parameters."); 149 for (Iterator i = shortcut.getParameters().entrySet().iterator(); i.hasNext();) { 150 Map.Entry entry = (Map.Entry ) i.next(); 151 String content = (String ) entry.getValue(); 152 153 VariableReplacement r = new VariableReplacement(); 154 r.setApplicationShortcut(app, null); 155 r.setServletRequest(request); 156 r.setLaunchSession(launchSession); 157 158 entry.setValue(r.replace(content)); 159 } 160 161 if (log.isDebugEnabled()) 162 log.debug("Template loaded, doing standard replacements."); 163 164 VariableReplacement r = new VariableReplacement(); 165 r.setApplicationShortcut(app, shortcut.getParameters()); 166 r.setServletRequest(request); 167 r.setLaunchSession(launchSession); 168 String templateText = r.replace(template.toString()); 169 170 ReplacementEngine engine = new ReplacementEngine(); 171 172 String tunnels = request.getParameter("tunnels"); 173 if (tunnels != null && !tunnels.equals("")) { 174 StringTokenizer t = new StringTokenizer (tunnels, ","); 175 while (t.hasMoreTokens()) { 176 String name = null; 177 String hostname = null; 178 int port = -1; 179 try { 180 String tunnel = t.nextToken(); 181 StringTokenizer t2 = new StringTokenizer (tunnel, ":"); 182 name = t2.nextToken(); 183 hostname = t2.nextToken(); 184 port = Integer.parseInt(t2.nextToken()); 185 } catch (Exception e) { 186 throw new Exception ("Failed to parse tunnels parameter '" + tunnels + "'.", e); 187 } 188 final ExtensionDescriptor.TunnelDescriptor tunnelDescriptor = app.getTunnel(name); 189 if (tunnelDescriptor == null) { 190 throw new Exception ("No tunnel named " + name); 191 } 192 final String fHostname = hostname; 193 final int fPort = port; 194 String pattern = "\\$\\{tunnel:" + name + "\\.[^\\}]*\\}"; 195 engine.addPattern(pattern, new Replacer() { 196 public String getReplacement(Pattern pattern, Matcher matcher, String sequence) { 197 String match = matcher.group(); 198 if (match.equals("${tunnel:" + tunnelDescriptor.getName() + ".hostname}")) { 199 return fHostname; 200 } else if (match.equals("${tunnel:" + tunnelDescriptor.getName() + ".port}")) { 201 return String.valueOf(fPort); 202 } else { 203 return ""; 204 } 205 } 206 }, null); 207 208 } 209 } 210 211 String url = request.getParameter("sslexplorer"); 213 if (url != null) { 214 String host = request.getHeader(HttpConstants.HDR_HOST); 215 if (host != null) { 216 url = (request.isSecure() ? "https" : "http") + "://" + host; 217 } else { 218 219 throw new Exception ("No sslexplorer parameter supplied."); 220 } 221 } 222 final URL sslexplorerUrl = new URL (url); 223 engine.addPattern("\\$\\{sslexplorer:[^\\}]*\\}", new Replacer() { 224 public String getReplacement(Pattern pattern, Matcher matcher, String sequence) { 225 String match = matcher.group(); 226 try { 227 String param = match.substring(14, match.length() - 1); 228 if (param.equals("host")) { 229 return sslexplorerUrl.getHost(); 230 } else if (param.equals("port")) { 231 return String.valueOf(sslexplorerUrl.getPort() == -1 ? (sslexplorerUrl.getProtocol().equals("https") ? 443 232 : 80) : sslexplorerUrl.getPort()); 233 } else if (param.equals("protocol")) { 234 return sslexplorerUrl.getProtocol(); 235 } else { 236 throw new Exception ("Unknow variable."); 237 } 238 } catch (Throwable t) { 239 log.error("Failed to replace " + match + ".", t); 240 } 241 return ""; 242 } 243 }, null); 244 245 String processed = engine.replace(templateText); 246 if (log.isDebugEnabled()) 247 log.debug("Returning " + processed); 248 249 Util.noCache(response); 250 251 response.setContentType("text/html"); 252 response.setContentLength(processed.length()); 253 request.setAttribute(Constants.REQ_ATTR_COMPRESS, Boolean.FALSE); 254 255 OutputStream out = response.getOutputStream(); 256 PrintWriter pw = new PrintWriter (new OutputStreamWriter (out)); 257 pw.print(processed); 258 pw.flush(); 259 260 Policy pol = PolicyDatabaseFactory.getInstance().getGrantingPolicyForUser(launchSession.getSession().getUser(), shortcut); 261 CoreServlet.getServlet().fireCoreEvent(new ResourceAccessEvent(this, ApplicationShortcutEventConstants.APPLICATION_SHORTCUT_LAUNCHED, shortcut, pol, launchSession.getSession(), CoreEvent.STATE_SUCCESSFUL) 262 .addAttribute(CoreAttributeConstants.EVENT_ATTR_APPLICATION_NAME, app.getName()) 263 .addAttribute(CoreAttributeConstants.EVENT_ATTR_APPLICATION_ID, shortcut.getApplication())); 264 266 return null; 267 } 268 269 272 public int getNavigationContext(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { 273 return SessionInfo.MANAGEMENT_CONSOLE_CONTEXT | SessionInfo.USER_CONSOLE_CONTEXT; 274 } 275 } 276 | Popular Tags |