1 /*------------------------------------------------------------------------------
2 Name: StreamCallback.java
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 ------------------------------------------------------------------------------*/
6
7 package org.xmlBlaster.client.script;
8
9 import java.io.IOException;
10 import java.io.OutputStream;
11
12 import java.util.logging.Logger;
13 import java.util.logging.Level;
14 import org.xmlBlaster.client.I_Callback;
15 import org.xmlBlaster.client.key.UpdateKey;
16 import org.xmlBlaster.client.qos.UpdateQos;
17 import org.xmlBlaster.util.Global;
18 import org.xmlBlaster.util.XmlBlasterException;
19 import org.xmlBlaster.util.def.ErrorCode;
20
21 /**
22 * StreamCallback is a sample implementation of the I_Callback interface which
23 * provides basic functionality as a callback to the XmlScriptInterpreter. It writes
24 * the information it gets from the update method to the output stream in an xml formatted
25 * way.
26 * <p/>
27 * If you want another behavior (for example by outputting the content in base64) you
28 * can overwrite the writeContent method.
29 *
30 * @author <a href="mailto:michele@laghi.eu">Michele Laghi</a>
31 */
32 public class StreamCallback implements I_Callback {
33
34 private final String ME = "StreamCallback";
35 private Global global;
36 private static Logger log = Logger.getLogger(StreamCallback.class.getName());
37 private OutputStream out;
38 private String offset = "";
39
40 /**
41 * The constructor
42 * @param global The global
43 * @param out the output stream to which you want to send the information coming to the
44 * update method.
45 * @param offset the offset to use.
46 */
47 public StreamCallback(Global global, OutputStream out, String offset) {
48 this.global = global;
49
50 this.out = out;
51 if (offset != null) this.offset = offset;
52 }
53
54
55 /**
56 * Invoked in the update method to write out the content in an xml
57 * formatted way.
58 *
59 * @param content the content to write
60 * @param buf the StringBuffer object to fill with the content
61 */
62 protected void writeContent(String contentStr, StringBuffer buf) {
63 buf.append(this.offset).append(" ").append("<content>");
64 buf.append("<![CDATA[").append(contentStr).append("]]>"); // here you can change according to your needs
65 buf.append("</content>");
66 }
67
68 /**
69 * Enforced by I_Callback
70 */
71 public String update(String cbSessionId, UpdateKey updateKey, byte[] content, UpdateQos updateQos) throws XmlBlasterException {
72 if (this.out == null) return "OK";
73 StringBuffer buf = new StringBuffer();
74 buf.append("\n<!-- ___________________________________ update ________________________________ -->");
75 buf.append("\n").append(this.offset).append("<update>\n");
76 buf.append(this.offset).append("<sessionId>").append(cbSessionId).append("</sessionId>");
77 buf.append(updateKey.toXml(this.offset + " ")).append("\n");
78 writeContent(updateQos.getContentStr(content), buf);
79 buf.append(updateQos.toXml(this.offset + " ")).append("\n");
80 buf.append(this.offset).append("</update>\n");
81 synchronized (this.out) {
82 try {
83 this.out.write(buf.toString().getBytes());
84 }
85 catch (IOException ex) {
86 throw new XmlBlasterException(updateKey.getGlobal(), ErrorCode.USER_CLIENTCODE, ME + ".update");
87 }
88 }
89 return "OK";
90 }
91
92 }
syntax highlighted by Code2HTML, v. 0.9.1