001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: *
021: * $Id: AppletUtils.java,v 1.2 2006-06-15 13:40:47 sinisa Exp $
022: */
023:
024: package com.lutris.appserver.server.httpPresentation;
025:
026: import com.lutris.applet.LBSConnection;
027:
028: /**
029: * This is the counterpart to com.lutris.applet.LBSConnection. This class
030: * creates <APPLET ...> tags with extra initialization parameters.
031: * LBSConnection reads those extra initialization parameters and uses them
032: * to establish a connection back to the Mutliserver.
033: * This file is used by a presentation object, while LBSConnection is
034: * included in the applet's jar file or classes directory. This way it
035: * does not need to include the com.lutris.appserver.server.* classes.
036: *
037: * @see com.lutris.applet.LBSConnection.
038: */
039: public class AppletUtils {
040:
041: /**
042: * Private constructor to prevent instantiation; use the static methods.
043: */
044: private AppletUtils() {
045: }
046:
047: /**
048: *
049: * This assumes that if cookies are being used, the name of the cookie
050: * is the application name. This is true if the StandardApplication is
051: * being used.
052: *
053: * @param comms
054: * HTTP communications object. Contains objects and interfaces to read
055: * the request and send a response.
056: * @param targetURL
057: * The URL of the presentation object the applet should connect to.
058: * For example: "/package/Presentaion.po". The server name and
059: * presentation path will automatically be added.
060: * @param options
061: * A list of various applet options to be included in the applet tag.
062: * For example: "code=myApplet.class width=400 height=100". These
063: * three are required options.
064: * @param parameterNames
065: * An array of parameter names, for example: {"X", "Y", "maxIterations"}.
066: * This is optional.
067: * @param parameterValues
068: * An array of parameter values, for example: {"24", "19", "2000"}.
069: * Either this an <CODE>parameterNames</CODE> must be null, or they
070: * must have the same number of elements. <CODE>parameterNames[i]</CODE>
071: * is matched up with <CODE>parameterValues[i]</CODE>.
072: * This is optional.
073: * @param alternateHtml Any HTML to display if the browser does not
074: * support applets. This is optional, and may be null.
075: * @return
076: * A string of the form "<APPLET code=... > <PARAM NAME=... VALUE=...>
077: * ..... </APPLET>".
078: */
079: public static String createAppletTag(HttpPresentationComms comms,
080: String targetURL, String options, String[] parameterNames,
081: String[] parameterValues, String alternateHtml) {
082: /*
083: * Check input.
084: */
085: if (targetURL == null)
086: targetURL = ""; // This is an error, but ignore for now.
087: if (options == null)
088: options = ""; // This is an error, but ignore for now.
089: if (alternateHtml == null)
090: alternateHtml = "";
091: /*
092: * Start off with the APPLET tag and the options.
093: */
094: StringBuffer result = new StringBuffer();
095: result.append("\n<APPLET ");
096: result.append(options);
097: result.append(">\n");
098: /*
099: * Add any user-supplied parameters.
100: */
101: if ((parameterNames != null) && (parameterValues != null))
102: for (int i = 0; i < parameterNames.length; i++) {
103: result.append("<PARAM NAME=");
104: result.append(parameterNames[i]);
105: result.append(" VALUE=");
106: result.append(parameterValues[i]);
107: result.append(">\n");
108: }
109: /*
110: * Figure out the cookie settings.
111: */
112: String cookieName = comms.application.getName();
113: String cookieValue = comms.session.getSessionKey();
114: /*
115: * Build up a complete URL for the target.
116: */
117: String server = comms.request.getServerName();
118: int port = comms.request.getServerPort();
119: String pp = "/";
120: try {
121: pp = comms.request.getApplicationPath();
122: } catch (HttpPresentationException e) {
123: }
124: if (!pp.startsWith("/"))
125: pp = "/" + pp;
126: if (!pp.endsWith("/"))
127: pp = pp + "/";
128: if (targetURL.startsWith("/"))
129: targetURL = targetURL.substring(1);
130: String fullURL = "http://" + server;
131: if (port != 80)
132: fullURL += ":" + port;
133: fullURL += pp + targetURL;
134: /*
135: * Add the target and the session cookie parameter.
136: * The applet will need this
137: * when it issues requests back to the server, to access the current
138: * session.
139: * See StandardAppUtil.bindSessionToClient().
140: */
141: result.append("<PARAM NAME=");
142: result.append(LBSConnection.nameParamName);
143: result.append(" VALUE=");
144: result.append(cookieName);
145: result.append(">\n");
146: result.append("<PARAM NAME=");
147: result.append(LBSConnection.valueParamName);
148: result.append(" VALUE=");
149: result.append(cookieValue);
150: result.append(">\n");
151: result.append("<PARAM NAME=");
152: result.append(LBSConnection.targetParamName);
153: result.append(" VALUE=");
154: result.append(fullURL);
155: result.append(">\n");
156: /*
157: * Finish off with the alternate html, then close the tag.
158: */
159: result.append(alternateHtml);
160: result.append("\n</APPLET>\n");
161: /*
162: * All done.
163: */
164: return result.toString();
165: }
166:
167: }
|