if (launchSession == null) {
throw new Exception("No launch session id " + launchSessionId);
}
final ApplicationShortcut shortcut = (ApplicationShortcut)launchSession.getResource();
launchSession.checkAccessRights(null, getSessionInfo(request));
ExtensionDescriptor app = ExtensionStore.getInstance().getExtensionDescriptor(shortcut.getApplication());
if (app == null) {
throw new Exception("No application named " + shortcut.getApplication() + ".");
}
if (!(app.getExtensionType() instanceof HtmlType)) {
throw new Exception(getClass().getName() + " only supports applications of type " + HtmlType.class + ".");
}
// Get the primary VPN client ticket
HtmlType type = (HtmlType) app.getExtensionType();
File file = new File(app.getApplicationBundle().getBaseDir(), type.getTemplate());
if (log.isDebugEnabled())
log.debug("Loading template " + file.getAbsolutePath());
InputStream in = null;
StringBuffer template = new StringBuffer((int) file.length());
try {
in = new FileInputStream(file);
String line = null;
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
while ((line = reader.readLine()) != null) {
if (template.length() != 0) {
template.append("\n");
}
template.append(line);
}
} finally {
Util.closeStream(in);
}
if (log.isDebugEnabled())
log.debug("Parsing parameters.");
for (Iterator i = shortcut.getParameters().entrySet().iterator(); i.hasNext();) {
Map.Entry entry = (Map.Entry) i.next();
String content = (String) entry.getValue();
VariableReplacement r = new VariableReplacement();
r.setApplicationShortcut(app, null);
r.setServletRequest(request);
r.setLaunchSession(launchSession);
entry.setValue(r.replace(content));
}
if (log.isDebugEnabled())
log.debug("Template loaded, doing standard replacements.");
VariableReplacement r = new VariableReplacement();
r.setApplicationShortcut(app, shortcut.getParameters());
r.setServletRequest(request);
r.setLaunchSession(launchSession);
String templateText = r.replace(template.toString());
ReplacementEngine engine = new ReplacementEngine();
String tunnels = request.getParameter("tunnels");
if (tunnels != null && !tunnels.equals("")) {
StringTokenizer t = new StringTokenizer(tunnels, ",");
while (t.hasMoreTokens()) {
String name = null;
String hostname = null;
int port = -1;
try {
String tunnel = t.nextToken();
StringTokenizer t2 = new StringTokenizer(tunnel, ":");
name = t2.nextToken();
hostname = t2.nextToken();
port = Integer.parseInt(t2.nextToken());
} catch (Exception e) {
throw new Exception("Failed to parse tunnels parameter '" + tunnels + "'.", e);
}
final ExtensionDescriptor.TunnelDescriptor tunnelDescriptor = app.getTunnel(name);
if (tunnelDescriptor == null) {
throw new Exception("No tunnel named " + name);
}
final String fHostname = hostname;
final int fPort = port;
String pattern = "\\$\\{tunnel:" + name + "\\.[^\\}]*\\}";
engine.addPattern(pattern, new Replacer() {
public String getReplacement(Pattern pattern, Matcher matcher, String sequence) {
String match = matcher.group();
if (match.equals("${tunnel:" + tunnelDescriptor.getName() + ".hostname}")) {
return fHostname;
} else if (match.equals("${tunnel:" + tunnelDescriptor.getName() + ".port}")) {
return String.valueOf(fPort);
} else {
return "";
}
}
}, null);
}
}
// Get the location of Adito as the client sees it
String url = request.getParameter("adito");
if (url != null) {
String host = request.getHeader(HttpConstants.HDR_HOST);
if (host != null) {
url = (request.isSecure() ? "https" : "http") + "://" + host;
} else {
throw new Exception("No adito parameter supplied.");
}
}
final URL aditoUrl = new URL(url);
engine.addPattern("\\$\\{adito:[^\\}]*\\}", new Replacer() {
public String getReplacement(Pattern pattern, Matcher matcher, String sequence) {
String match = matcher.group();
try {
String param = match.substring(14, match.length() - 1);
if (param.equals("host")) {
return aditoUrl.getHost();
} else if (param.equals("port")) {
return String.valueOf(aditoUrl.getPort() == -1 ? (aditoUrl.getProtocol().equals("https") ? 443
: 80) : aditoUrl.getPort());
} else if (param.equals("protocol")) {
return aditoUrl.getProtocol();
} else {
throw new Exception("Unknow variable.");
}
} catch (Throwable t) {
log.error("Failed to replace " + match + ".", t);
}
return "";
}
}, null);
String processed = engine.replace(templateText);
if (log.isDebugEnabled())
log.debug("Returning " + processed);
Util.noCache(response);
response.setContentType("text/html");
response.setContentLength(processed.length());
request.setAttribute(Constants.REQ_ATTR_COMPRESS, Boolean.FALSE);
OutputStream out = response.getOutputStream();
PrintWriter pw = new PrintWriter(new OutputStreamWriter(out));
pw.print(processed);
pw.flush();
Policy pol = PolicyDatabaseFactory.getInstance().getGrantingPolicyForUser(launchSession.getSession().getUser(), shortcut);
CoreServlet.getServlet().fireCoreEvent(new ResourceAccessEvent(this, ApplicationShortcutEventConstants.APPLICATION_SHORTCUT_LAUNCHED, shortcut, pol, launchSession.getSession(), CoreEvent.STATE_SUCCESSFUL)
.addAttribute(CoreAttributeConstants.EVENT_ATTR_APPLICATION_NAME, app.getName())
.addAttribute(CoreAttributeConstants.EVENT_ATTR_APPLICATION_ID, shortcut.getApplication()));
//////////////////////////////////////////////
return null;
}