KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lutris > appserver > server > httpPresentation > AppletUtils


1
2 /*
3  * Enhydra Java Application Server Project
4  *
5  * The contents of this file are subject to the Enhydra Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License on
8  * the Enhydra web site ( http://www.enhydra.org/ ).
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific terms governing rights and limitations
13  * under the License.
14  *
15  * The Initial Developer of the Enhydra Application Server is Lutris
16  * Technologies, Inc. The Enhydra Application Server and portions created
17  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
18  * All Rights Reserved.
19  *
20  * Contributor(s):
21  *
22  * $Id: AppletUtils.java,v 1.2 2005/03/24 10:51:16 slobodan Exp $
23  */

24
25
26
27
28
29 package com.lutris.appserver.server.httpPresentation;
30
31 import com.lutris.applet.LBSConnection;
32
33
34 /**
35  * This is the counterpart to com.lutris.applet.LBSConnection. This class
36  * creates <APPLET ...> tags with extra initialization parameters.
37  * LBSConnection reads those extra initialization parameters and uses them
38  * to establish a connection back to the Mutliserver.
39  * This file is used by a presentation object, while LBSConnection is
40  * included in the applet's jar file or classes directory. This way it
41  * does not need to include the com.lutris.appserver.server.* classes.
42  *
43  * @see com.lutris.applet.LBSConnection.
44  */

45 public class AppletUtils {
46
47     /**
48      * Private constructor to prevent instantiation; use the static methods.
49      */

50     private AppletUtils() {
51     }
52
53
54     /**
55      *
56      * This assumes that if cookies are being used, the name of the cookie
57      * is the application name. This is true if the StandardApplication is
58      * being used.
59      *
60      * @param comms
61      * HTTP communications object. Contains objects and interfaces to read
62      * the request and send a response.
63      * @param targetURL
64      * The URL of the presentation object the applet should connect to.
65      * For example: "/package/Presentaion.po". The server name and
66      * presentation path will automatically be added.
67      * @param options
68      * A list of various applet options to be included in the applet tag.
69      * For example: "code=myApplet.class width=400 height=100". These
70      * three are required options.
71      * @param parameterNames
72      * An array of parameter names, for example: {"X", "Y", "maxIterations"}.
73      * This is optional.
74      * @param parameterValues
75      * An array of parameter values, for example: {"24", "19", "2000"}.
76      * Either this an <CODE>parameterNames</CODE> must be null, or they
77      * must have the same number of elements. <CODE>parameterNames[i]</CODE>
78      * is matched up with <CODE>parameterValues[i]</CODE>.
79      * This is optional.
80      * @param alternateHtml Any HTML to display if the browser does not
81      * support applets. This is optional, and may be null.
82      * @return
83      * A string of the form "<APPLET code=... > <PARAM NAME=... VALUE=...>
84      * ..... </APPLET>".
85      */

86     public static String JavaDoc createAppletTag(
87                              HttpPresentationComms comms,
88                              String JavaDoc targetURL,
89                              String JavaDoc options,
90                              String JavaDoc[] parameterNames,
91                              String JavaDoc[] parameterValues,
92                              String JavaDoc alternateHtml) {
93         /*
94          * Check input.
95          */

96         if (targetURL == null)
97             targetURL = ""; // This is an error, but ignore for now.
98
if (options == null)
99             options = ""; // This is an error, but ignore for now.
100
if (alternateHtml == null)
101             alternateHtml = "";
102         /*
103          * Start off with the APPLET tag and the options.
104          */

105         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
106         result.append("\n<APPLET ");
107         result.append(options);
108         result.append(">\n");
109         /*
110          * Add any user-supplied parameters.
111          */

112         if ((parameterNames != null) && (parameterValues != null))
113             for (int i=0; i<parameterNames.length; i++) {
114                 result.append("<PARAM NAME=");
115                 result.append(parameterNames[i]);
116                 result.append(" VALUE=");
117                 result.append(parameterValues[i]);
118                 result.append(">\n");
119             }
120         /*
121          * Figure out the cookie settings.
122          */

123         String JavaDoc cookieName = comms.application.getName();
124         String JavaDoc cookieValue = comms.session.getSessionKey();
125         /*
126          * Build up a complete URL for the target.
127          */

128         String JavaDoc server = comms.request.getServerName();
129         int port = comms.request.getServerPort();
130         String JavaDoc pp = "/";
131         try {
132             pp = comms.request.getApplicationPath();
133         } catch (HttpPresentationException e) {
134         }
135         if (!pp.startsWith("/"))
136             pp = "/" + pp;
137         if (!pp.endsWith("/"))
138             pp = pp + "/";
139         if (targetURL.startsWith("/"))
140             targetURL = targetURL.substring(1);
141         String JavaDoc fullURL = "http://" + server;
142         if (port != 80)
143             fullURL += ":" + port;
144         fullURL += pp + targetURL;
145         /*
146          * Add the target and the session cookie parameter.
147          * The applet will need this
148          * when it issues requests back to the server, to access the current
149          * session.
150          * See StandardAppUtil.bindSessionToClient().
151          */

152         result.append("<PARAM NAME=");
153         result.append(LBSConnection.nameParamName);
154         result.append(" VALUE=");
155         result.append(cookieName);
156         result.append(">\n");
157         result.append("<PARAM NAME=");
158         result.append(LBSConnection.valueParamName);
159         result.append(" VALUE=");
160         result.append(cookieValue);
161         result.append(">\n");
162         result.append("<PARAM NAME=");
163         result.append(LBSConnection.targetParamName);
164         result.append(" VALUE=");
165         result.append(fullURL);
166         result.append(">\n");
167         /*
168          * Finish off with the alternate html, then close the tag.
169          */

170         result.append(alternateHtml);
171         result.append("\n</APPLET>\n");
172         /*
173          * All done.
174          */

175         return result.toString();
176     }
177
178
179 }
180
Popular Tags