📄 xmlblasteraccessrawbase.java
字号:
package org.xmlBlaster.client.protocol.http.common;import java.io.ByteArrayInputStream;import java.io.IOException;import java.util.Hashtable;import java.util.Enumeration;import java.util.Vector;/** * A java client implementation to access xmlBlaster using a persistent http connection * for instant callback messages. * <p> * You can control logging with the Applet PARAM tag, logging output is put to the Java console * of your browser: * </p> * <pre> * <PARAM name="xmlBlaster/logLevels" value="ERROR,WARN"> * with more logging: * <PARAM name="xmlBlaster/logLevels" value="ERROR,WARN,INFO,DEBUG"> * </pre> * See the example applet {@link http.applet.HelloWorld3} on how to use it. * @author <a href="mailto:xmlBlaster@marcelruff.info">Marcel Ruff</a> * @see http.applet.HelloWorld3 */public abstract class XmlBlasterAccessRawBase implements I_XmlBlasterAccessRaw{ /** Typically "http://localhost:8080/xmlBlaster/AppletServlet" */ protected String xmlBlasterServletUrl; private PersistentRequest persistentHttpConnection; private I_CallbackRaw callback; public static final boolean ONEWAY = true; private boolean isConnected = false; protected Hashtable properties = new Hashtable(); protected I_Log logListener; protected String logLevels = "ERROR,WARN,INFO"; private Hashtable cookie; private static int staticInstanceCounter; protected int instanceCount; private static Object mutexer = new Object(); /** * Provides access to xmlBlaster server. * @see #parseAppletParameter * @param properties "xmlBlaster/servletUrl", "xmlBlaster/logLevels" (locally used) * and additional properties which are send to the servlet.<br /> * The addtional properties must start with "servlet/xyz=someValue". The "servlet/" will * be stripped away and in the web-servlet will arrive "xyz=someValue". * The key/values are send in the URL. */ public XmlBlasterAccessRawBase(Hashtable properties) { synchronized (mutexer) { staticInstanceCounter++; this.instanceCount = staticInstanceCounter; } this.properties = properties; this.xmlBlasterServletUrl = (String)this.properties.get("xmlBlaster/servletUrl"); //param from html page if (this.properties.get("xmlBlaster/logLevels") != null) logLevels = (String)this.properties.get("xmlBlaster/logLevels"); log("DEBUG", new StringBuffer("constructor - ").append(getXmlBlasterServletUrl()).toString()); } public String getInstanceId() { return ""+getInstanceCount(); } /** * Access the unique counter of this object instance. */ public int getInstanceCount() { return this.instanceCount; } /** * Register to receive the logging output * @param listener If null we log to the java console of the browser */ public synchronized void setLogListener(I_Log logListener) { this.logListener = logListener; } /** * @see I_XmlBlasterAccessRaw#getHtmlProperties */ public Hashtable getHtmlProperties() { return this.properties; } /** * Log to java console of the browser of the logListener if any is registered */ protected void log(String level, String text) { log("XmlBlasterAccess", level, text); } /** * @see I_XmlBlasterAccessRaw#log(String, String, String) */ public synchronized void log(String location, String level, String text) { if (this.logListener != null) { this.logListener.log(location, level, text); } if (logLevels.indexOf(level) != -1) System.out.println(new StringBuffer(location).append(" #").append(this.instanceCount).append(" [").append(level).append("]: ").append(text).toString()); } /** * Access the URL of the xmlBlaster servlet. * @return Typically "http://localhost:8080/xmlBlaster/AppletServlet&appletInstanceCount=1" */ public String getXmlBlasterServletUrl() { String url = this.xmlBlasterServletUrl; if (url != null) { StringBuffer tmp = new StringBuffer(); if (url.indexOf("?") >= 0) tmp.append(url).append("&appletInstanceCount=").append(this.instanceCount); else tmp.append(url).append("?appletInstanceCount=").append(this.instanceCount); // Add additional paramaters and send to the servlet Hashtable props = getHtmlProperties(); Enumeration it = props.keys(); while (it.hasMoreElements()) { String key = (String)it.nextElement(); String value = (String)props.get(key); if (key.startsWith("servlet/")) { key = key.substring("servlet/".length()); tmp.append("&").append(key).append("=").append(value); } } url = tmp.toString(); } log("DEBUG", new StringBuffer("URL=").append(url).toString()); return url; } public void isConnected(boolean isConnected) { this.isConnected = isConnected; log("INFO", new StringBuffer("isConnected(").append(isConnected).append(")").toString()); } public boolean isConnected() { return this.isConnected; } private String startPersistentHttpConnection() throws Exception { this.persistentHttpConnection.start(); log("DEBUG", "Waiting for connect() to establish ..."); int num = 100; int i; for (i=0; i<num; i++) { if (this.isConnected) { break; } try { Thread.sleep(500); } catch(java.lang.InterruptedException e){ log("WARN", e.toString()); } } if (i >= num) { log("ERROR", "Can't login to xmlBlaster, timed out."); throw new Exception("Can't login to xmlBlaster, timed out."); } log("INFO", new StringBuffer("Successfully connected to xmlBlaster '").append(getXmlBlasterServletUrl()).append("'").toString()); return this.persistentHttpConnection.getConnectReturnQos(); } /** * @see I_XmlBlasterAccessRaw#connect(String, I_CallbackRaw) */ public String connect(String qos, I_CallbackRaw callback) throws Exception { this.callback = callback; // We pass getHtmlProperties() to the servlet to be used to initialize Global // currently this is attached to the URL (not very nice) if (qos == null) { String loginName = (String)this.properties.get("xmlBlaster/loginName"); String passwd = (String)this.properties.get("xmlBlaster/passwd"); if (loginName != null && passwd != null) { log("INFO", new StringBuffer("Using loginName = ").append(loginName).append(" as configured in your HTML page to connect to xmlBlaster").toString()); this.persistentHttpConnection = new PersistentRequest(this, getXmlBlasterServletUrl(), loginName, passwd); } else qos = "<qos/>"; // Servlet does authentication (can be a security issue!) } if (qos != null) { this.persistentHttpConnection = new PersistentRequest(this, getXmlBlasterServletUrl(), qos); } return startPersistentHttpConnection(); } /** * @see I_XmlBlasterAccessRaw#connect(String, I_CallbackRaw) */ public String sendXmlScript(String xmlRequest) throws Exception { log("DEBUG", new StringBuffer("xmlScript(xmlRequest=").append(xmlRequest).append(")").toString()); return (String)postRequest("xmlScript", xmlRequest, null, null, !ONEWAY); } public Hashtable ping(java.lang.String qos) throws Exception { log("DEBUG", new StringBuffer("ping(qos=").append(qos).append(")").toString()); return (Hashtable)postRequest(PING_NAME, null, qos, null, !ONEWAY); } public Hashtable subscribe(java.lang.String xmlKey, java.lang.String qos) throws Exception { log("DEBUG", new StringBuffer("subscribe(key=").append(xmlKey).append(")").toString()); return (Hashtable)postRequest(SUBSCRIBE_NAME, xmlKey, qos, null, !ONEWAY); } public Msg[] get(java.lang.String xmlKey, java.lang.String qos) throws Exception { log("DEBUG", new StringBuffer("get(key=").append(xmlKey).append(")").toString()); //String keyEnc = encode(xmlKey, "UTF-8"); //String qosEnc = encode(qos, "UTF-8"); Vector list = (Vector)postRequest(GET_NAME, xmlKey, qos, null, !ONEWAY); Msg[] msgs = new Msg[list.size()/3]; for (int i=0; i<list.size()/3; i++) { log("DEBUG", "Synchronous get is not implented"); Hashtable qosRet = (Hashtable)list.elementAt(i*3); Hashtable keyRet = (Hashtable)list.elementAt(i*3+1); byte[] content = (byte[])list.elementAt(i*3+2); msgs[i] = new Msg(keyRet, content, qosRet); } return msgs; } public Hashtable[] unSubscribe(java.lang.String xmlKey, java.lang.String qos) throws Exception { log("DEBUG", new StringBuffer("unSubscribe(key=").append(xmlKey).append(")").toString()); return (Hashtable[])postRequest(UNSUBSCRIBE_NAME, xmlKey, qos, null, !ONEWAY); } public Hashtable publish(String xmlKey, byte[] content, String qos) throws Exception { log("DEBUG", new StringBuffer("publish(key=").append(xmlKey).append(")").toString()); return (Hashtable)postRequest(PUBLISH_NAME, xmlKey, qos, content, !ONEWAY); } public Hashtable[] erase(java.lang.String xmlKey, java.lang.String qos) throws Exception { log("DEBUG", new StringBuffer("erase(key=").append(xmlKey).append(")").toString()); return (Hashtable[])postRequest(ERASE_NAME, xmlKey, qos, null, !ONEWAY); } public void disconnect(String qos) { log("DEBUG", "disconnect()"); try { postRequest("disconnect", null, qos, null, !ONEWAY); log("INFO", "Successfully disconnected from xmlBlaster"); } catch (Exception e) { log("WARN", new StringBuffer("Ignoring unexpected exception during disconnect: ").append(e.toString()).toString()); } } /** * The format: * oid + \0 + key + '\0' + qos + '\0' + content: length = oid + key + qos + content + 3 * @param conn * @param actionType * @param key * @param qos * @param content */ static void writeRequest(I_Connection conn, String actionType, String key, String qos, byte[] content) throws IOException { conn.setRequestProperty("ActionType", actionType); conn.setRequestProperty("BinaryProtocol", "true"); int length = ObjectOutputStreamMicro.getMessageLength(null, key, qos, content); // this is needed since J2ME does not set Content-Length (don't know why) conn.setRequestProperty("Data-Length", String.valueOf(length)); ObjectOutputStreamMicro.writeMessage(conn.getOutputStream(), null, key, qos, content); //conn.getOutputStream().close(); } /** * Send a http request to the servlet. * @param request The request string without the URL prefix, e.g. "?XmlBlasterAccessRawType=pong" * @param doPost if true POST else GET * @param oneway true for requests returning void * @return The returned value for the given request, "" on error or for oneway messages */ /* Object postRequestOld(String actionType, String key, String qos, byte[] content, boolean oneway) throws Exception { String request = "ActionType=" + actionType; try { boolean doPost = true; // applet.getAppletContext().showDocument(URL url, String target); //String url = (doPost) ? this.xmlBlasterServletUrl : this.xmlBlasterServletUrl + request; if ("xmlScript".equals(actionType)) { if (key != null) request += "&xmlRequest=" + encode(key, "UTF-8"); } else { if (key != null) request += "&key=" + encode(key, "UTF-8"); if (qos != null) request += "&qos=" + encode(qos, "UTF-8"); if (content != null) request += "&content=" + encode(new String(content), "UTF-8"); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -