KickJava   Java API By Example, From Geeks To Geeks.

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


1 /******************************************************************************
2  * ResponderCONNECT.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 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 JavaDoc 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     /**
44      * Get current number of active connections.
45      */

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 JavaDoc username)
55         throws IOException
56     {
57         mLogger.debug("respondImpl(username=" + username + ")");
58
59         // This case is annoying
60
String JavaDoc 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         // ...create the connection...
74
HTTPConnection connection = null;
75         HTTPConnection prevConnection = app.getConnection(req.getParameter("i"));
76
77         // for reconnects!
78
String JavaDoc 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                 // Log that reconnection request has lost previous session.
87
// This can happen if client takes longer than the reconnection
88
// request wait interval and tries to make a reconnect request.
89
mLogger.warn("tried reconnecting but there was no previous connection");
90             }
91         }
92
93         if (doConnect) {
94             // ...pad connection headers with empty bytes if MSIE...
95
String JavaDoc 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             // ...and finally register. Check to see if we had a previous
106
// connection. If so, disconnect it after we register the new one.
107
//
108
// Registering the new connection and disconnecting the old one *must*
109
// be an atomic operation.
110
app.register(connection);
111             if (prevConnection != null)
112                 prevConnection.disconnect(true);
113
114             ++mSTAT_activeCount[0];
115         }
116
117         try {
118             // this call blocks until a new connection comes in or is
119
// disconnected
120
connection.connect();
121         } catch (IOException e) {
122             mLogger.debug("ioexception: " + e.getMessage());
123         } catch (Exception JavaDoc 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         // Send disconnect message when it isn't reconnect
134
if ( ! connection.toBeReconnected() && app.doSendUserDisconnect() ) {
135             String JavaDoc 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             // Send a byte to flush out socket. The flash player keeps its
145
// socket open for a few seconds after disconnect unless we do
146
// this. See bug 1447 for more info.
147
byte[] buf = new byte[1];
148             buf[0] = (byte) 0; // sending a buch of end tags (code 0).
149

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