📄 appletservlet.java
字号:
// The applet responses with 'pong', to allow the servlet to // detect if the applet is alive. try { PushHandler pushHandler = getPushHandler(req); pushHandler.pong(); if (log.isLoggable(Level.FINE)) log.fine("Received pong"); writeResponse(res, I_XmlBlasterAccessRaw.PONG_NAME, "OK-"+System.currentTimeMillis()); return; } catch (XmlBlasterException e) { log.severe("Caught XmlBlaster Exception for actionType '" + actionType + "': " + e.getMessage()); return; } } else if (actionType.equals(I_XmlBlasterAccessRaw.DISCONNECT_NAME)) { // "disconnect" log.info("Logout arrived ..."); try { PushHandler pc = getPushHandler(req); pc.cleanup(); } catch(XmlBlasterException e) { log.severe(e.toString()); } writeResponse(res, I_XmlBlasterAccessRaw.DISCONNECT_NAME, "<qos/>"); } else { String text = "Unknown ActionType '" + actionType + "', request for permanent http connection ignored"; throw new XmlBlasterException(this.initialGlobal, ErrorCode.USER_CONFIGURATION, ME, text); } } catch (XmlBlasterException e) { log.warning("Caught XmlBlaster Exception: " + e.getMessage()); writeResponse(res, I_XmlBlasterAccessRaw.EXCEPTION_NAME, e.getMessage()); } catch (Exception e) { log.severe("Caught Exception: " + e.toString()); e.printStackTrace(); writeResponse(res, I_XmlBlasterAccessRaw.EXCEPTION_NAME, e.toString()); } } private final String decode(String in, String encoding) { //return new String(Base64.decodeBase64(in.getBytes())); return Global.decode(in, encoding); } private byte[] readBodyContent(HttpServletRequest req) { try { int length = req.getContentLength(); if (length < 0) { String tmp = req.getHeader("Data-Length"); if (tmp != null) length = Integer.parseInt(tmp); } //System.out.println("readBodyContent: Length=" + length); byte[] ret = new byte[length]; DataInputStream in = new DataInputStream(req.getInputStream()); in.readFully(ret); return ret; } catch (Exception ex) { ex.printStackTrace(); return new byte[0]; } } /** * This method is used by the binary protocol. It passes all information in * the http body. * @param req * @param res * @throws IOException * @return null if it was no binary protocol, otherwise an Object[3] = { (String)key, (String)qos, byte[] content } * where every one of the three elements could be null. */ private MsgHolder readBinaryProtocol(HttpServletRequest req, HttpServletResponse res) throws IOException { try { //System.out.println("entering readMessageFromContent"); byte[] contentAsBytes = readBodyContent(req); String tmp = req.getHeader("BinaryProtocol"); //System.out.println("BinaryProtocol=" + tmp); if (tmp == null || tmp.equalsIgnoreCase("false")) return null; // then it was not set ... //String actionType = req.getHeader("ActionType"); //ServletInputStream in = req.getInputStream(); //int length = req.getContentLength(); //System.out.println("Content-Length=" + length); MsgHolder msg = ObjectInputStreamMicro.readMessage(contentAsBytes); //System.out.println("msg: ActionType='" + actionType + "' length='" + length + "'"); //System.out.println(" - key : '" + msg.key + "'"); //System.out.println(" - qos : '" + msg.qos + "'"); //String tmp1 = "null"; //if (msg.content != null) tmp1 = new String(msg.content); //System.out.println(" - content: '" + tmp1 + "'"); //System.out.println("============================"); //req.getInputStream().close(); return msg; } catch (IOException ex) { ex.printStackTrace(); throw ex; } catch (Throwable ex) { ex.printStackTrace(); throw new IOException(ex.getMessage()); } } protected MsgHolder extractMessage(String ME, Logger log, HttpServletRequest req, MsgHolder binaryMsg) { if (binaryMsg != null) return binaryMsg; String oid = getParameter(req, "key.oid", (String)null); if (log.isLoggable(Level.FINE)) log.fine("encoded oid=" + oid); if (oid != null) oid = this.decode(oid, ENCODING); String key = getParameter(req, "key", (String)null); if (log.isLoggable(Level.FINE)) log.fine("encoded key=" + key); if (key != null) { key = this.decode(key, ENCODING); if (log.isLoggable(Level.FINEST)) log.finest("key=\n'" + key + "'"); } byte[] content; String contentStr = getParameter(req, "content", (String)null); if (contentStr != null) { content = this.decode(contentStr, ENCODING).getBytes(); //content = Base64.decodeBase64(contentStr.getBytes()); } else content = new byte[0]; if (log.isLoggable(Level.FINEST)) log.finest("content=\n'" + new String(content) + "'"); String qos = getParameter(req, "qos", (String)null); if (log.isLoggable(Level.FINE)) log.fine("encoded qos=" + qos); if (qos != null) { qos = this.decode(qos, ENCODING); } else qos = ""; if (log.isLoggable(Level.FINEST)) log.finest("qos=\n'" + qos + "'"); // See http://www.xmlblaster.org/xmlBlaster/doc/requirements/client.script.html String xmlRequest = getParameter(req, "xmlRequest", (String)null); if (log.isLoggable(Level.FINE)) log.fine("encoded xmlRequest=" + xmlRequest); if (xmlRequest != null) { xmlRequest = this.decode(xmlRequest, ENCODING); if (log.isLoggable(Level.FINEST)) log.finest("xmlRequest=\n'" + xmlRequest + "'"); } return new MsgHolder(oid, key, qos, content); } /** * This method is supported just for cases where the servlet is contacted * directy from a browser in which case it is easy for the user to pass * the data directly via the url. */ public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { doPost(req, res); } /** * POST request from the applet. * <p> * Handles all requests coming from the applet. It reads the * passed parameters either from the url (in which case they are * encoded) or directly from the input stream (the body of the request). * In the latter case they are binary data which is not encoded and * is refered to as binary protocol. * * The asynchronous updates are pushed back using PushHandler.java * * @param req Data from browser * @param res Response of the servlet */ public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { String ME; // null if the message was not passed via the binary protocol MsgHolder binaryMsg = readBinaryProtocol(req, res); String actionType = req.getHeader("ActionType"); if (actionType == null) { actionType = getParameter(req, "ActionType", "NONE"); } synchronized(AppletServlet.class) { requestCounter++; // For logging only String appletInstanceCount = getParameter(req, "appletInstanceCount", "0"); ME = this.getClass().getName() + "-" + req.getRemoteAddr() + "-#" + appletInstanceCount + "req" + requestCounter + "-" + actionType; } //String appletInstanceCount = getParameter(req, "appletInstanceCount", "0"); if (actionType.equalsIgnoreCase(I_XmlBlasterAccessRaw.CONNECT_NAME) || actionType.equalsIgnoreCase(I_XmlBlasterAccessRaw.DISCONNECT_NAME) || actionType.equalsIgnoreCase(I_XmlBlasterAccessRaw.PONG_NAME) || actionType.equalsIgnoreCase(I_XmlBlasterAccessRaw.CREATE_SESSIONID_NAME)) { // "connect", "disconnect" doGetFake(ME, req, res, actionType, binaryMsg); return; } res.setContentType("text/plain"); //HttpSession session = req.getSession(false); String sessionId = req.getRequestedSessionId(); ME += "-" + sessionId; if (log.isLoggable(Level.FINE)) log.fine("Entering servlet doPost() ..."); Global glob = null; I_XmlBlasterAccess xmlBlaster = null; PushHandler pushHandler = null; Object returnObject = null; try { pushHandler = getPushHandler(req); xmlBlaster = pushHandler.getXmlBlasterAccess(); glob = xmlBlaster.getGlobal(); } catch (XmlBlasterException e) { log.warning("Caught XmlBlaster Exception: " + e.getMessage()); writeResponse(res, I_XmlBlasterAccessRaw.EXCEPTION_NAME, e.getMessage()); return; } try { // Extract the message data MsgHolder msg = extractMessage(ME, log, req, binaryMsg); String oid = msg.getOid(); String key = msg.getKey(); String qos = msg.getQos(); String xmlRequest = msg.getKey(); // in case of xmlScript the request is sent as the key (all other are null) byte[] content = msg.getContent(); if (actionType.equals(I_XmlBlasterAccessRaw.PING_NAME)) { // "ping" if (log.isLoggable(Level.FINE)) log.fine("ping arrived, qos=" + qos); Hashtable map = new Hashtable(); if (xmlBlaster.isAlive()) { map.put("/qos/state/@id", Constants.STATE_OK); map.put("/qos/state/@info", ConnectionStateEnum.ALIVE.toString()); // "ALIVE" } else if (xmlBlaster.isPolling()) { map.put("/qos/state/@id", Constants.STATE_OK); map.put("/qos/state/@info", ConnectionStateEnum.POLLING.toString()); // "POLLING" } else { map.put("/qos/state/@id", Constants.STATE_WARN); map.put("/qos/state/@info", ConnectionStateEnum.DEAD.toString()); // "DEAD" } returnObject = map; } else if (actionType.equals(I_XmlBlasterAccessRaw.SUBSCRIBE_NAME)) { // "subscribe" if (log.isLoggable(Level.FINE)) log.fine("subscribe arrived ... oid=" + oid + ", key=" + key + ", qos=" + qos); if (oid != null) { SubscribeKey xmlKey = new SubscribeKey(glob, oid); SubscribeReturnQos ret = xmlBlaster.subscribe(xmlKey.toXml(), qos); returnObject = ret.getData().toJXPath(); if (log.isLoggable(Level.FINE)) log.fine("Subscribed to simple key.oid=" + oid + ": " + ret.getSubscriptionId()); } else if (key != null) { SubscribeReturnQos ret = xmlBlaster.subscribe(key, qos); returnObject = ret.getData().toJXPath(); if (log.isLoggable(Level.FINE)) log.fine("Subscribed to " + key + ": SubscriptionId=" + ret.getSubscriptionId() + " qos=" + qos + " returnObject=" + returnObject.getClass().getName()); } else { String str = "Please call servlet with some 'key.oid=...' or 'key=<key ...' when subscribing"; log.warning(str); throw new XmlBlasterException(this.initialGlobal, ErrorCode.USER_CONFIGURATION, ME, str);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -