if (actionType.equals("login") || actionType.equals("connect")) {
ConnectQos connectQos;
if (actionType.equals("connect")) {
String qos = Util.getParameter(req, "xmlBlaster.connectQos", null);
if (qos == null || qos.length() < 1)
throw new XmlBlasterException(glob, ErrorCode.USER_CONFIGURATION, ME, "Missing connect QoS. Pass xmlBlaster.connectQos='<qos> ... </qos>' with your URL in your POST in a hidden form field or in your cookie.");
connectQos = new ConnectQos(glob, glob.getConnectQosFactory().readObject(qos));
}
else {
String loginName = Util.getParameter(req, "xmlBlaster.loginName", null); // "Joe";
if (loginName == null || loginName.length() < 1)
throw new XmlBlasterException(glob, ErrorCode.USER_CONFIGURATION, ME, "Missing login name. Pass xmlBlaster.loginName=xy with your URL or in your cookie.");
String passwd = Util.getParameter(req, "xmlBlaster.passwd", null); // "secret";
if (passwd == null || passwd.length() < 1)
throw new XmlBlasterException(glob, ErrorCode.USER_CONFIGURATION, ME, "Missing passwd");
connectQos = new ConnectQos(glob, loginName, passwd);
}
ME = "BlasterHttpProxyServlet-" + req.getRemoteAddr() + "-" +
connectQos.getSessionName().getLoginName() + "-" + sessionId;
I_XmlBlasterAccess xmlBlasterAccess = glob.getXmlBlasterAccess();
HttpPushHandler pushHandler = new HttpPushHandler(req, res, sessionId,
connectQos.getSessionName().getRelativeName(),
xmlBlasterAccess);
xmlBlasterAccess.connect(connectQos, pushHandler);
if (!session.isNew()) {
pushHandler.startPing();
}
else {
log.info("Login action, browser has not yet joined this sessionId (cookie), so first pings pong may return an invalid sessionId");
pushHandler.startPing(); // This is too early here, we need to start the ping thread later?
}
BlasterHttpProxy.addHttpPushHandler( sessionId, pushHandler );
// Don't fall out of doGet() to keep the HTTP connection open
log.info("Waiting forever, permanent HTTP connection from " +
req.getRemoteHost() + "/" + req.getRemoteAddr() +
", sessionName=" + connectQos.getSessionName().getRelativeName() + " sessionId=" + sessionId +
"', protocol='" + req.getProtocol() +
"', agent='" + req.getHeader("User-Agent") +
"', referer='" + req.getHeader("Referer") +
"'.");
if (log.isLoggable(Level.FINE)) log.fine("user='" + req.getRemoteUser() +
"', serverPort='" + req.getServerPort() +
"', query='" + req.getQueryString() +
"', pathInfo='" + req.getPathInfo() +
"', pathTranslated='" + req.getPathTranslated() +
"', servletPath='" + req.getServletPath() +
"', documentRoot='" + getServletConfig().getServletContext().getRealPath("/") +
"', accept='" + req.getHeader("Accept") +
"', referer='" + req.getHeader("Referer") +
"', authorization='" + req.getHeader("Authorization") +
"'.");
pushHandler.setBrowserIsReady( true );
pushHandler.ping("loginSucceeded");
while (!pushHandler.closed()) {
try {
Thread.sleep(10000L);
}
catch (InterruptedException i) {
log.severe("Error in Thread handling, don't know what to do: "+i.toString());
pushHandler.cleanup();
break;
}
}
pushHandler = null;
log.info("Persistent HTTP connection lost, leaving doGet() ....");
/*
System.out.println("Currently consumed threads:");
System.out.println("===========================");
ThreadLister.listAllThreads(System.out);
*/
}
//------------------ first request from applet --------------------------
else if(actionType.equals("dummyToCreateASessionId")) { // I_XmlBlasterAccessRaw.CREATE_SESSIONID_NAME
log.info("doGet: dummyToCreateASessionId");
PrintWriter out = res.getWriter();
out.println(header+"<body text='white' bgcolor='white'>Empty response for your ActionType='dummyToCreateASessionId' " +
System.currentTimeMillis() + "</body></html>");
return;
}
//------------------ ready, browser processed last message --------------------------
// The HttpPushHandler adds javascript 'parent.browserReady();' which
// is invoked after the browser is ready.
else if(actionType.equals("browserReady")) {
try {
HttpPushHandler pushHandler = BlasterHttpProxy.getHttpPushHandler(sessionId);
if (log.isLoggable(Level.FINE)) log.fine("Received 'browserReady'");
pushHandler.setBrowserIsReady( true );
// Otherwise the browser (controlFrame) complains 'document contained no data'
PrintWriter out = res.getWriter();
out.println(header+"<body text='white' bgcolor='white'>Empty response for your ActionType='browserReady' " + System.currentTimeMillis() + "</body></html>");
return;
}
catch (XmlBlasterException e) {
log.severe("Caught XmlBlaster Exception for actionType '" + actionType + "': " + e.getMessage());
return;
}
}
//------------------ answer of a ping -----------------------------------------------
// The HttpPushHandler adds javascript 'parent.ping();' which
// pings the browser to hold the http connection.
// The browser responses with 'pong', to allow the servlet to
// detect if the browser is alive.
// Locally this works fine, but over the internet the second or third pong from the browser
// was never reaching this servlet. Adding some dynamic content/URL helped a bit,
// but after some ten pongs, the following pongs where lost.
// The browserReady request hasn't got this problem, why??
// So we do a pong on browserReady as well, which solved the problem (see HttpPushHandler.java)
else if(actionType.equals("pong")) {
try {
HttpPushHandler pushHandler = BlasterHttpProxy.getHttpPushHandler(sessionId);
pushHandler.pong();
// state is only for debugging and to avoid internet proxies to discard this content (since it has not changed)
if (log.isLoggable(Level.FINE)) log.fine("Received pong '" + Util.getParameter(req, "state", "noState") + "'");
// Otherwise the browser (controlFrame) complains 'document contained no data'
PrintWriter out = res.getWriter();
out.println(header+"<body text='white' bgcolor='white'>Empty response for your ActionType='pong' " + Util.getParameter(req, "state", "noState") + " " + System.currentTimeMillis() + "</body></html>");
return;
}
catch (XmlBlasterException e) {
log.severe("Caught XmlBlaster Exception for actionType '" + actionType + "': " + e.getMessage());
return;
}
}
else if (MethodName.PUBLISH.toString().equalsIgnoreCase(actionType)) { // "publish"
doPost(req, res);
}
else if (MethodName.SUBSCRIBE.toString().equalsIgnoreCase(actionType)) { // "subscribe"
doPost(req, res);
}
else if (MethodName.UNSUBSCRIBE.toString().equalsIgnoreCase(actionType)) { // "unSubscribe"
doPost(req, res);
}
else if (MethodName.GET.toString().equalsIgnoreCase(actionType)) { // "get"
doPost(req, res);
}
else if (MethodName.ERASE.toString().equalsIgnoreCase(actionType)) { // "erase"
doPost(req, res);
}
else if (MethodName.PING.toString().equalsIgnoreCase(actionType)) { // "ping"
doPost(req, res);
}
//------------------ logout ---------------------------------------------------------
else if (actionType.equals("logout")) {
log.info("Logout arrived ...");
try {
HttpPushHandler pc = BlasterHttpProxy.getHttpPushHandler(sessionId);
pc.cleanup();
} catch(XmlBlasterException e) {
log.severe(e.toString());
}
// Otherwise the browser (controlFrame) complains 'document contained no data'
PrintWriter out = res.getWriter();
out.println(header+" <body text='white' bgcolor='white'><script language='JavaScript' type='text/javascript'>top.close()</script></body></html>");
}
else {
String text = "Unknown ActionType '" + actionType + "', request for permanent http connection ignored";
throw new XmlBlasterException(glob, ErrorCode.USER_CONFIGURATION, ME, text);
}
} catch (XmlBlasterException e) {
log.severe("Caught XmlBlaster Exception: " + e.getMessage());