1 4 5 9 10 package org.openlaszlo.servlets.responders; 11 12 import java.io.*; 13 import java.util.*; 14 import javax.servlet.*; 15 import javax.servlet.http.*; 16 import org.openlaszlo.connection.*; 17 import org.openlaszlo.server.*; 18 import org.openlaszlo.utils.*; 19 import org.openlaszlo.servlets.LoadCount; 20 import org.apache.log4j.*; 21 22 public final class ResponderCONNECT extends ResponderConnection 23 { 24 private static boolean mIsInitialized = false; 25 private static boolean mEmitConnectHeaders = false; 26 27 private static int[] mSTAT_activeCount = new int[1]; 28 29 private static Logger mLogger = Logger.getLogger(ResponderCONNECT.class); 30 31 synchronized public void init(String reqName, ServletConfig config, 32 Properties prop) 33 throws ServletException, IOException 34 { 35 super.init(reqName, config, prop); 36 if (! mIsInitialized) { 37 mEmitConnectHeaders = 38 prop.getProperty("emitConnectHeaders", "false").intern() == "true"; 39 mIsInitialized = true; 40 } 41 } 42 43 46 public static int getActiveCount() 47 { 48 synchronized (mSTAT_activeCount) { 49 return mSTAT_activeCount[0]; 50 } 51 } 52 53 protected void respondImpl(HttpServletRequest req, HttpServletResponse res, 54 Application app, int serial, String username) 55 throws IOException 56 { 57 mLogger.debug("respondImpl(username=" + username + ")"); 58 59 String ua = req.getHeader(LZHttpUtils.USER_AGENT); 61 if (ua == null) { 62 respondWithErrorSWF(res, "request has no user-agent header"); 63 mLogger.warn("request has no user-agent header"); 64 return; 65 } 66 67 if (! LPS.configuration.optionAllows("connection-user-agent", ua)) { 68 respondWithErrorSWF(res, "forbidden user-agent: " + ua); 69 mLogger.warn("forbidden user-agent: " + ua); 70 return; 71 } 72 73 HTTPConnection connection = null; 75 HTTPConnection prevConnection = app.getConnection(req.getParameter("i")); 76 77 String type = req.getParameter("type"); 79 boolean isReconnect = (type!=null && type.equals("r")); 80 boolean doConnect = true; 81 if (isReconnect) { 82 if (prevConnection != null) { 83 doConnect = false; 84 connection = new HTTPConnection(res, prevConnection); 85 } else { 86 mLogger.warn("tried reconnecting but there was no previous connection"); 90 } 91 } 92 93 if (doConnect) { 94 String userAgent = req.getHeader("User-Agent"); 96 boolean doPad = (userAgent!=null && userAgent.indexOf("MSIE") != -1); 97 98 connection = new HTTPConnection(res, username, mSWFVersionNum) 99 .setHeartbeatInterval(app.getHeartbeat()) 100 .setEmitConnectHeaders(mEmitConnectHeaders) 101 .setDoPad(doPad); 102 } 103 104 synchronized (mSTAT_activeCount) { 105 app.register(connection); 111 if (prevConnection != null) 112 prevConnection.disconnect(true); 113 114 ++mSTAT_activeCount[0]; 115 } 116 117 try { 118 connection.connect(); 121 } catch (IOException e) { 122 mLogger.debug("ioexception: " + e.getMessage()); 123 } catch (Exception e) { 124 mLogger.debug("exception: " + e.getMessage()); 125 } finally { 126 app.unregister(connection); 127 synchronized (mSTAT_activeCount) { 128 --mSTAT_activeCount[0]; 129 } 130 } 131 132 133 if ( ! connection.toBeReconnected() && app.doSendUserDisconnect() ) { 135 String disconnectInfo = 136 HTTPConnection.getConnectionInfoXML("__LPSUSERDISCONNECT", 137 connection.getUsername()); 138 ConnectionGroup group = app.getConnectionGroup(); 139 group.sendMessage("*", disconnectInfo, "all", null); 140 } 141 142 try { 143 144 byte[] buf = new byte[1]; 148 buf[0] = (byte) 0; 150 ServletOutputStream out = res.getOutputStream(); 151 out.write(buf); 152 out.close(); 153 154 } catch (IOException e) { 155 mLogger.debug(e.getMessage()); 156 } 157 } 158 } 159 | Popular Tags |