📄 xmlblasteraccessrawbase.java
字号:
String url = (doPost) ? this.xmlBlasterServletUrl + "?" + request : this.xmlBlasterServletUrl + request; I_Connection conn = createConnection(url); // conn.setUseCaches(false); writeCookie(conn); log("DEBUG", "doPost=" + doPost + ", sending '" + url + "' with request '" + request + "' ..."); if(doPost){ // for HTTP-POST, e.g. for publish(), subscribe() conn.setDoOutput(true); conn.setPostMethod(); conn.setDoInput(true); conn.setUseCaches(false); writeRequest(conn, actionType, key, qos, content); } conn.connect(); readCookie(conn); if (oneway) { return ""; } // Read the return value ... BufferedInputStreamMicro dataInput = new BufferedInputStreamMicro(conn.getInputStream()); String line; Object returnObject = null; StringBuffer ret = new StringBuffer(1024); while ((line = dataInput.readLine()) != null){ log("DEBUG", "Return value for '" + request + "' = '" + line + "'"); if (line == null || line.length() < 1) continue; if (true) { // doPost) { // All POST is returned Base64 encoded, all GET as ordinary string byte[] serial = decodeBase64(line.getBytes()); log("DEBUG", "Parsing now: <" + new String(serial) + ">"); ByteArrayInputStream bais = new ByteArrayInputStream(serial); ObjectInputStreamMicro ois = new ObjectInputStreamMicro(bais); String method = (String)ois.readObject(); // e.g. "subscribe" if (PUBLISH_NAME.equals(method)) { Hashtable returnQos = (Hashtable)ois.readObject(); returnObject = returnQos; } else if (GET_NAME.equals(method)) { Vector returnQos = (Vector)ois.readObject(); returnObject = returnQos; } else if (SUBSCRIBE_NAME.equals(method)) { Hashtable returnQos = (Hashtable)ois.readObject(); returnObject = returnQos; } else if (UNSUBSCRIBE_NAME.equals(method)) { Hashtable[] returnQos = (Hashtable[])ois.readObject(); returnObject = returnQos; } else if (ERASE_NAME.equals(method)) { Hashtable[] returnQos = (Hashtable[])ois.readObject(); returnObject = returnQos; } else if ("xmlScript".equals(method)) { returnObject = (String)ois.readObject(); } else if (DISCONNECT_NAME.equals(method)) { returnObject = ois.readObject(); } else if (EXCEPTION_NAME.equals(method)) { String err = (String)ois.readObject(); log("INFO", "Caught XmlBlasterException: " + err); throw new Exception(err); } else if (CREATE_SESSIONID_NAME.equals(method)) { returnObject = (String)ois.readObject(); } else if (PONG_NAME.equals(method)) { returnObject = (String)ois.readObject(); } else { log("ERROR", "Unknown method=" + method); returnObject = line; } } else returnObject = line; } return returnObject; } catch (java.lang.ClassNotFoundException e) { e.printStackTrace(); log("ERROR", "request(" + request + ") failed: " + e.toString()); } catch (IOException e) { e.printStackTrace(); log("ERROR", "request(" + request + ") failed: " + e.toString()); } return ""; } */ Object postRequest(String actionType, String key, String qos, byte[] content, boolean oneway) throws Exception { String request = new StringBuffer("ActionType=").append(actionType).toString(); try { boolean doPost = true; String url = getXmlBlasterServletUrl(); I_Connection conn = createConnection(url); // conn.setUseCaches(false); writeCookie(conn); log("DEBUG", new StringBuffer("doPost=").append(doPost).append(", sending '").append(url).append("' with request '").append(request).append("' ...").toString()); if(doPost){ // for HTTP-POST, e.g. for publish(), subscribe() conn.setDoOutput(true); conn.setPostMethod(); conn.setDoInput(true); conn.setUseCaches(false); writeRequest(conn, actionType, key, qos, content); } conn.connect(); readCookie(conn); if (oneway) { return ""; } // Read the return value ... BufferedInputStreamMicro dataInput = new BufferedInputStreamMicro(conn.getInputStream()); String line; Object returnObject = null; while ((line = dataInput.readLine()) != null){ log("DEBUG", new StringBuffer("Return value for '").append(request).append("' = '").append(line).append("'").toString()); if (line == null || line.length() < 1) continue; if (true) { // doPost) { // All POST is returned Base64 encoded, all GET as ordinary string byte[] serial = decodeBase64(line.getBytes()); //log("DEBUG", "Parsing now: <" + new String(serial) + ">"); ByteArrayInputStream bais = new ByteArrayInputStream(serial); ObjectInputStreamMicro ois = new ObjectInputStreamMicro(bais); String method = (String)ois.readObject(); // e.g. "subscribe" if (PUBLISH_NAME.equals(method)) { Hashtable returnQos = (Hashtable)ois.readObject(); returnObject = returnQos; } else if (GET_NAME.equals(method)) { Vector returnQos = (Vector)ois.readObject(); returnObject = returnQos; } else if (PING_NAME.equals(method)) { Hashtable returnQos = (Hashtable)ois.readObject(); returnObject = returnQos; } else if (SUBSCRIBE_NAME.equals(method)) { Hashtable returnQos = (Hashtable)ois.readObject(); returnObject = returnQos; } else if (UNSUBSCRIBE_NAME.equals(method)) { Hashtable[] returnQos = (Hashtable[])ois.readObject(); returnObject = returnQos; } else if (ERASE_NAME.equals(method)) { Hashtable[] returnQos = (Hashtable[])ois.readObject(); returnObject = returnQos; } else if ("xmlScript".equals(method)) { returnObject = (String)ois.readObject(); } else if (DISCONNECT_NAME.equals(method)) { returnObject = ois.readObject(); } else if (EXCEPTION_NAME.equals(method)) { String err = (String)ois.readObject(); log("INFO", new StringBuffer("Caught XmlBlasterException: ").append(err).toString()); throw new Exception(err); } else if (CREATE_SESSIONID_NAME.equals(method)) { returnObject = (String)ois.readObject(); } else if (PONG_NAME.equals(method)) { returnObject = (String)ois.readObject(); } else { log("ERROR", new StringBuffer("Unknown method=").append(method).toString()); returnObject = line; } } else returnObject = line; } return returnObject; } catch (java.lang.ClassNotFoundException e) { e.printStackTrace(); log("ERROR", new StringBuffer("request(").append(request).append(") failed: ").append(e.toString()).toString()); } catch (IOException e) { e.printStackTrace(); log("ERROR", new StringBuffer("request(").append(request).append(") failed: ").append(e.toString()).toString()); } return ""; } /** * see I_CallbackRaw#update */ public String update(String cbSessionId, Hashtable updateKey, byte[] content, Hashtable updateQos) throws Exception { if (this.callback == null) { String text = new StringBuffer("Receiving unexpected update message '").append(updateKey.get("/key/@oid")).append("', no callback handle available").toString(); log("WARN", text); throw new Exception(text); } log("DEBUG", new StringBuffer("Receiving update message '").append(updateKey.get("/key/@oid")).append("' state=").append(updateQos.get("/qos/state/@id")).toString()); return this.callback.update(cbSessionId, updateKey, content, updateQos); } /** * Converts a string containing all cookies to an hashtable containing * all cookies as key/value pairs. * * @param cookieTxt The string from which to extract the cookies * @return an hashtable containing all found cookies as key/value pairs */ protected Hashtable extractCookies(String cookieTxt) { Hashtable ret = new Hashtable(); //StringTokenizer token = new StringTokenizer(cookieTxt.trim(), ";"); char ch = ';'; Vector token = new Vector(); cookieTxt = cookieTxt.trim(); while (true) { int pos=cookieTxt.indexOf(ch); if (pos < 0) { if (cookieTxt.length() > 0) token.addElement(cookieTxt); break; } token.addElement(cookieTxt.substring(0, pos)); cookieTxt = cookieTxt.substring(pos+1).trim(); } for (int i=0; i < token.size(); i++) { String prop = (String)token.elementAt(i); int pos = prop.indexOf("="); if (pos < 0) continue; String key = prop.substring(0, pos); String val = prop.substring(pos+1); log("DEBUG", new StringBuffer(" extractCookies: ").append("(key='").append(key).append("', val='").append(val).append("')").toString()); ret.put(key, val); } return ret; } /** * reads the cookie and stores it. * @param conn */ public void readCookie(I_Connection conn) { //conn.setRequestProperty("cookie", "JSESSIONID=" + this.sessionId); log("DEBUG", new StringBuffer(" readCookie: Cookie : ").append(conn.getHeaderField("Cookie")).toString()); log("DEBUG", new StringBuffer(" readCookie: Set-Cookie : ").append(conn.getHeaderField("Set-Cookie")).toString()); String setCookie = conn.getHeaderField("Set-Cookie"); if (setCookie != null) this.cookie = extractCookies(setCookie); } public void writeCookie(I_Connection conn) { //conn.setRequestProperty("cookie", "JSESSIONID=" + this.sessionId); log("DEBUG", new StringBuffer("writeCookie: original cookie: ").append(this.cookie).toString()); if (this.cookie == null) { conn.setRequestProperty("cookie", ""); } else conn.setRequestProperty("cookie", new StringBuffer("JSESSIONID=").append((String)this.cookie.get("JSESSIONID")).toString()); conn.setRequestProperty("Cache-Control", "no-cache"); conn.setRequestProperty("Pragma", "no-cache"); conn.setRequestProperty("Connection", "keep-alive"); } /** * Url encodes the string * @param s * @param enc * @return */ //public abstract String encode(String s, String enc); public abstract byte[] encodeBase64(byte[] data); /** * decodes binary data to Base64. The returned bytes are all * text characters. * * @param data * @return */ public abstract byte[] decodeBase64(byte[] data); public abstract I_Connection createConnection(String urlString) throws Exception;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -