1 4 5 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 DEFAULT_AUTHENTICATOR = 27 "org.openlaszlo.auth.HTTPAuthentication"; 28 private static final String ANONYMOUS_AUTHENTICATOR = 29 "org.openlaszlo.auth.NullAuthentication"; 30 31 private static String mDefaultAuthClass = null; 32 33 private static String mDefaultUserName = null; 34 private static Properties mLPSProperties = null; 35 private static String 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 username) 43 throws IOException; 44 45 46 synchronized public void init(String 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 fileName, HttpServletRequest req, 69 HttpServletResponse res) 70 throws IOException 71 { 72 try { 73 String path = req.getServletPath(); 74 String 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 String username = mDefaultUserName; 85 Authentication authenticator = app.getAuthenticator(); 86 87 String 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 int serial = 0; 102 try { 103 String serialStr = req.getParameter("s"); 104 if (serialStr != null) { 105 serial = Integer.parseInt(serialStr); 106 } 107 } catch (NumberFormatException 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 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 path = req.getServletPath(); 134 135 app = Application.getApplication(path, false); 136 if (app != null && mCMOption.equals("never")) 137 return app; 138 139 String 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 long lmt = getLastModified(filename, req); 153 if (lmt != app.getLastModifiedTime()) { 154 155 String authClass = canvas.getAuthenticator(); 156 Authentication authenticator; 157 try { 158 authenticator = ( authClass == null 159 ? getAuthenticator(mDefaultAuthClass) 160 : getAuthenticator(authClass) ); 161 } catch (ClassNotFoundException e) { 162 if (app != null) Application.removeApplication(app); 163 throw new AuthenticationException("ClassNotFoundException: " + e.getMessage()); 164 } catch (InstantiationException e) { 165 if (app != null) Application.removeApplication(app); 166 throw new AuthenticationException("InstantiationException: " + e.getMessage()); 167 } catch (IllegalAccessException 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 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 authParam = req.getParameter("authparam"); 210 if (authParam != null) { 211 authParam = URLDecoder.decode(authParam); 212 String [] params = StringUtils.split(authParam, "&"); 213 for (int i=0; i < params.length; i++) { 214 String [] 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 k = (String ) iter.next(); 225 mLogger.debug(k + ": " + map.get(k)); 226 } 227 } 228 229 return map; 230 } 231 232 synchronized private Authentication getAuthenticator(String className) 233 throws AuthenticationException, InstantiationException , 234 IllegalAccessException , ClassNotFoundException 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 |