KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > servlets > responders > ResponderConnection


1 /******************************************************************************
2  * ResponderConnection.java
3  * ****************************************************************************/

4
5 /* J_LZ_COPYRIGHT_BEGIN *******************************************************
6 * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
7 * Use is subject to license terms. *
8 * J_LZ_COPYRIGHT_END *********************************************************/

9
10 package org.openlaszlo.servlets.responders;
11
12 import java.io.*;
13 import java.util.*;
14 import java.net.*;
15 import javax.servlet.*;
16 import javax.servlet.http.*;
17 import org.openlaszlo.auth.*;
18 import org.openlaszlo.compiler.*;
19 import org.openlaszlo.connection.*;
20 import org.openlaszlo.utils.*;
21 import org.apache.log4j.*;
22
23
24 public abstract class ResponderConnection extends ResponderCompile
25 {
26     private static final String JavaDoc DEFAULT_AUTHENTICATOR =
27         "org.openlaszlo.auth.HTTPAuthentication";
28     private static final String JavaDoc ANONYMOUS_AUTHENTICATOR =
29         "org.openlaszlo.auth.NullAuthentication";
30
31     private static String JavaDoc mDefaultAuthClass = null;
32
33     private static String JavaDoc mDefaultUserName = null;
34     private static Properties mLPSProperties = null;
35     private static String JavaDoc mCMOption = "check";
36     private static boolean mIsInitialized = false;
37     private static Logger mLogger = Logger.getLogger(ResponderConnection.class);
38
39     private static Hashtable mAuthenticators = new Hashtable();
40
41     protected abstract void respondImpl(HttpServletRequest req, HttpServletResponse res,
42                                         Application app, int serial, String JavaDoc username)
43         throws IOException;
44
45
46     synchronized public void init(String JavaDoc reqName, ServletConfig config, Properties prop)
47         throws ServletException, IOException
48     {
49         super.init(reqName, config, prop);
50
51         if (! mIsInitialized) {
52
53             HTTPConnection.init(prop);
54             mLPSProperties = prop;
55             mDefaultUserName =
56                 prop.getProperty("connection.none-authenticator.username",
57                                  NullAuthentication.DEFAULT_USERNAME);
58             mDefaultAuthClass =
59                 prop.getProperty("connection.default.authenticator",
60                                  DEFAULT_AUTHENTICATOR);
61             mCMOption = prop.getProperty("compMgrDependencyOption", "check");
62
63             mIsInitialized = true;
64         }
65     }
66
67
68     protected final void respondImpl(String JavaDoc fileName, HttpServletRequest req,
69                                      HttpServletResponse res)
70         throws IOException
71     {
72         try {
73             String JavaDoc path = req.getServletPath();
74             String JavaDoc info = null;
75
76             Application app = getConnectedApplication(fileName, req);
77             if (app == null) {
78                 info = "application does not allow persistent connection calls";
79                 respondWithErrorSWF(res, info);
80                 return;
81             }
82
83             // Session is valid if a username is returned
84
String JavaDoc username = mDefaultUserName;
85             Authentication authenticator = app.getAuthenticator();
86
87             // Kludge: skip authentication if lzt=connectionlogin.
88
String JavaDoc lzt = req.getParameter("lzt");
89             if (! lzt.equals("connectionlogin") && authenticator != null ) {
90                 username = authenticator.getUsername(req, res, getAuthParam(req));
91                 if (username == null) {
92                     respondWithErrorSWF(res, "invalid user or session");
93                     return;
94                 }
95             }
96
97             // s: serial number of request; this number needs to be echoed
98
// back for successful response as an attribute of the resultset
99
// tag. See xml response in respondWithStatusSWF() for more
100
// information.
101
int serial = 0;
102             try {
103                 String JavaDoc serialStr = req.getParameter("s");
104                 if (serialStr != null) {
105                     serial = Integer.parseInt(serialStr);
106                 }
107             } catch (NumberFormatException JavaDoc e) {
108                 respondWithErrorSWF(res, "'s' is not a number");
109                 return;
110             }
111
112             respondImpl(req, res, app, serial, username);
113
114         } catch (AuthenticationException e) {
115
116             respondWithExceptionSWF(res, e);
117
118         }
119     }
120
121
122     synchronized private
123         Application getConnectedApplication(String JavaDoc filename, HttpServletRequest req)
124         throws IOException, AuthenticationException
125     {
126         mLogger.debug("checkConnection(filename,req)");
127
128         if (filename.endsWith(".lzo")) {
129             filename = filename.substring(0, filename.length() - 1) + "x";
130         }
131
132         Application app = null;
133         String JavaDoc path = req.getServletPath();
134
135         app = Application.getApplication(path, false);
136         if (app != null && mCMOption.equals("never"))
137             return app;
138
139         // FIXME: [2003-11-03 pkang] should have another object that connection and
140
// connectionlogin inherit from. This is the safer option for now.
141
String JavaDoc lzt = req.getParameter("lzt");
142         if (! lzt.equals("connect") && ! lzt.equals("connectionlogin") )
143             return app;
144
145         Canvas canvas = getCanvas(filename, req);
146         if (canvas.isConnected()) {
147
148             app = Application.getApplication(path);
149
150             // Fetching lmt should be low impact, since the file should already
151
// be cached.
152
long lmt = getLastModified(filename, req);
153             if (lmt != app.getLastModifiedTime()) {
154
155                 String JavaDoc authClass = canvas.getAuthenticator();
156                 Authentication authenticator;
157                 try {
158                     authenticator = ( authClass == null
159                                       ? getAuthenticator(mDefaultAuthClass)
160                                       : getAuthenticator(authClass) );
161                 } catch (ClassNotFoundException JavaDoc e) {
162                     if (app != null) Application.removeApplication(app);
163                     throw new AuthenticationException("ClassNotFoundException: " + e.getMessage());
164                 } catch (InstantiationException JavaDoc e) {
165                     if (app != null) Application.removeApplication(app);
166                     throw new AuthenticationException("InstantiationException: " + e.getMessage());
167                 } catch (IllegalAccessException JavaDoc e) {
168                     if (app != null) Application.removeApplication(app);
169                     throw new AuthenticationException("IllegalAccessException: " + e.getMessage());
170                 }
171
172                 long heartbeat = canvas.getHeartbeat();
173                 boolean sendUserDisconnect = canvas.doSendUserDisconnect();
174
175                 String JavaDoc grName = canvas.getGroup();
176                 if (grName == null)
177                     grName = path;
178                 ConnectionGroup group = ConnectionGroup.getGroup(grName);
179
180                 app.setAgents(canvas.getAgents());
181
182                 app.setConnectionGroup(group);
183                 app.setAuthenticator(authenticator);
184                 app.setHeartbeat(heartbeat);
185                 app.setLastModifiedTime(lmt);
186                 app.setSendUserDisconnect(sendUserDisconnect);
187
188                 mLogger.debug("connected app settings " +
189                               "- authenticator: " + authenticator +
190                               ", senduserdisconnect: " + sendUserDisconnect +
191                               ", heartbeat: " + heartbeat +
192                               ", lmt: " + lmt);
193             }
194
195         } else if (app != null) {
196
197             Application.removeApplication(app);
198
199             mLogger.debug("Removed " + path + " as a connected application");
200         }
201
202         return app;
203     }
204
205
206     protected HashMap getAuthParam(HttpServletRequest req)
207     {
208         HashMap map = new HashMap();
209         String JavaDoc authParam = req.getParameter("authparam");
210         if (authParam != null) {
211             authParam = URLDecoder.decode(authParam);
212             String JavaDoc[] params = StringUtils.split(authParam, "&");
213             for (int i=0; i < params.length; i++) {
214                 String JavaDoc[] kv = StringUtils.split(params[i], "=");
215                 if (kv.length == 2) {
216                     map.put(kv[0], kv[1]);
217                 }
218             }
219         }
220
221         if (mLogger.isDebugEnabled()) {
222             Iterator iter=map.keySet().iterator();
223             while (iter.hasNext()) {
224                 String JavaDoc k = (String JavaDoc) iter.next();
225                 mLogger.debug(k + ": " + map.get(k));
226             }
227         }
228
229         return map;
230     }
231
232     synchronized private Authentication getAuthenticator(String JavaDoc className)
233         throws AuthenticationException, InstantiationException JavaDoc,
234                IllegalAccessException JavaDoc, ClassNotFoundException JavaDoc
235     {
236         if (className.equals("anonymous"))
237             className = ANONYMOUS_AUTHENTICATOR;
238
239         Authentication auth = (Authentication)mAuthenticators.get(className);
240         if (auth == null) {
241             auth = (Authentication)Class.forName(className).newInstance();
242             auth.init(mLPSProperties);
243             mAuthenticators.put(className, auth);
244         }
245         return auth;
246     }
247
248     public final int getMimeType()
249     {
250         return MIME_TYPE_SWF;
251     }
252 }
253
Popular Tags