1 // xmlBlaster/demo/javaclients/HelloWorldMime.java
  2 package javaclients;
  3 
  4 import java.util.logging.Logger;
  5 import java.util.logging.Level;
  6 import org.xmlBlaster.util.Global;
  7 import org.xmlBlaster.util.XmlBlasterException;
  8 import org.xmlBlaster.util.MsgUnit;
  9 import org.xmlBlaster.util.qos.AccessFilterQos;
 10 import org.xmlBlaster.client.I_Callback;
 11 import org.xmlBlaster.client.key.GetKey;
 12 import org.xmlBlaster.client.key.SubscribeKey;
 13 import org.xmlBlaster.client.key.UnSubscribeKey;
 14 import org.xmlBlaster.client.key.PublishKey;
 15 import org.xmlBlaster.client.key.UpdateKey;
 16 import org.xmlBlaster.client.key.EraseKey;
 17 import org.xmlBlaster.client.qos.ConnectQos;
 18 import org.xmlBlaster.client.qos.ConnectReturnQos;
 19 import org.xmlBlaster.client.qos.DisconnectQos;
 20 import org.xmlBlaster.client.qos.GetQos;
 21 import org.xmlBlaster.client.qos.PublishQos;
 22 import org.xmlBlaster.client.qos.PublishReturnQos;
 23 import org.xmlBlaster.client.qos.UpdateQos;
 24 import org.xmlBlaster.client.qos.SubscribeQos;
 25 import org.xmlBlaster.client.qos.SubscribeReturnQos;
 26 import org.xmlBlaster.client.qos.UnSubscribeQos;
 27 import org.xmlBlaster.client.qos.UnSubscribeReturnQos;
 28 import org.xmlBlaster.client.qos.EraseQos;
 29 import org.xmlBlaster.client.qos.EraseReturnQos;
 30 import org.xmlBlaster.client.I_XmlBlasterAccess;
 31 
 32 
 33 /**
 34  * This client connects to xmlBlaster and invokes all available methods, further we
 35  * show how to do a full text message filtering by looking into the xml message content
 36  * and filter with XPath.
 37  * <p />
 38  * We use java client helper classes to generate the raw xml strings, e.g.:
 39  * <pre>
 40  *   PublishKey pk = new PublishKey(glob, "HelloWorldMime", "text/xml");
 41  * 
 42  * generates:
 43  *
 44  *   &lt;key oid='HelloWorldMime' contentMime='text/xml'/>
 45  * </pre>
 46  *
 47  * Invoke: java javaclients.HelloWorldMime
 48  * <p />
 49  * Invoke: java javaclients.HelloWorldMime -session.name joe -passwd secret
 50  * @see <a href="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/interface.html" target="others">xmlBlaster interface</a>
 51  * @see <a href="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/mime.plugin.access.xpath.html" target="others">xmlBlaster mime.plugin.access.xpath requirement</a>
 52  */
 53 public class HelloWorldMime implements I_Callback
 54 {
 55    private static Logger log = Logger.getLogger(HelloWorldMime.class.getName());
 56 
 57    public HelloWorldMime(Global glob) {
 58 
 59       try {
 60          I_XmlBlasterAccess con = glob.getXmlBlasterAccess();
 61 
 62          ConnectQos qos = new ConnectQos(glob); // name, passwd can be set on command line, try -help
 63          con.connect(qos, this);  // Login to xmlBlaster, register for updates
 64 
 65 
 66          PublishKey pk = new PublishKey(glob, "HelloWorldMime", "text/xml");
 67          pk.setClientTags("<org.xmlBlaster><demo/></org.xmlBlaster>");
 68          PublishQos pq = new PublishQos(glob);
 69          MsgUnit msgUnit = new MsgUnit(pk, "<news type='sport'/>".getBytes(), pq);
 70          con.publish(msgUnit);
 71 
 72 
 73          GetKey gk = new GetKey(glob, "HelloWorldMime");
 74          GetQos gq = new GetQos(glob);
 75          gq.addAccessFilter(new AccessFilterQos(glob, "XPathFilter", "1.0", "/news[@type='sport']"));
 76          MsgUnit[] msgs = con.get(gk, gq);
 77 
 78          log.info("Accessed xmlBlaster message synchronous with get() with content '" + new String(msgs[0].getContent()) + "'");
 79 
 80 
 81          SubscribeKey sk = new SubscribeKey(glob, "HelloWorldMime");
 82          SubscribeQos sq = new SubscribeQos(glob);
 83          sq.addAccessFilter(new AccessFilterQos(glob, "XPathFilter", "1.0", "/news[@type='fishing']"));
 84          SubscribeReturnQos subRet = con.subscribe(sk, sq);
 85 
 86 
 87          msgUnit = new MsgUnit(pk, "<news type='fishing'/>".getBytes(), pq);
 88          con.publish(msgUnit);
 89 
 90 
 91          try { Thread.currentThread().sleep(1000); } 
 92          catch( InterruptedException i) {} // wait a second to receive update()
 93 
 94 
 95          UnSubscribeKey uk = new UnSubscribeKey(glob, subRet.getSubscriptionId());
 96          UnSubscribeQos uq = new UnSubscribeQos(glob);
 97          con.unSubscribe(uk, uq);
 98 
 99          EraseKey ek = new EraseKey(glob, "HelloWorldMime");
100          EraseQos eq = new EraseQos(glob);
101          EraseReturnQos[] eraseArr = con.erase(ek, eq);
102 
103          DisconnectQos dq = new DisconnectQos(glob);
104          con.disconnect(dq);
105       }
106       catch (Exception e) {
107          log.severe(e.getMessage());
108       }
109    }
110 
111    public String update(String cbSessionId, UpdateKey updateKey, byte[] content,
112                         UpdateQos updateQos)
113    {
114       if (updateKey.isInternal()) {
115          log.info("Received unexpected internal message '" +
116               updateKey.getOid() + " from xmlBlaster");
117          return "";
118       }
119 
120       log.info("Received asynchronous message '" + updateKey.getOid() +
121                    "' state=" + updateQos.getState() +
122                    " content=" + new String(content) + " from xmlBlaster");
123       return "";
124    }
125 
126    /**
127     * Try
128     * <pre>
129     *   java javaclients.HelloWorldMime -help
130     * </pre>
131     * for usage help
132     */
133    public static void main(String args[]) {
134       Global glob = new Global();
135       
136       if (glob.init(args) != 0) { // Get help with -help
137          System.out.println(glob.usage());
138          System.err.println("Example: java javaclients.HelloWorldMime -session.name Jeff\n");
139          System.exit(1);
140       }
141 
142       new HelloWorldMime(glob);
143    }
144 }


syntax highlighted by Code2HTML, v. 0.9.1