1 /*------------------------------------------------------------------------------
2 Name: XmlRpcHttpClient.java
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 Comment: Code to post a xml-rpc message thru the HTTP protocol
6 Version: $Id: XmlRpcHttpClient.java 16476 2007-09-06 22:36:52Z laghi $
7 Author: Michele Laghi (michele@laghi.eu)
8 ------------------------------------------------------------------------------*/
9
10 package javaclients.xmlrpc;
11
12 import java.util.logging.Logger;
13 import org.xmlBlaster.util.Global;
14 import org.xmlBlaster.util.XmlBlasterException;
15 import org.xmlBlaster.client.qos.ConnectQos;
16 import org.xmlBlaster.client.I_XmlBlasterAccess;
17
18 import org.xmlBlaster.client.key.PublishKey;
19 import org.xmlBlaster.util.MsgUnit;
20
21 /**
22 * Demo showing how to implement a client which connects to xmlBlaster via
23 * xml-rpc.
24 * Calls are made through the I_XmlBlasterAccess client helper class.
25 *
26 * When using this class as a client, the xmlrpc
27 * protocol is completely transparent, even the Callback server is created for you.
28 * <p />
29 * Invoke example (this client forces XMLRPC as protocol hard coded):
30 * <pre>
31 * java -cp lib/xmlBlaster.jar javaclients.xmlrpc.XmlRpcHttpClient
32 * </pre>
33 *
34 * <p />
35 * NOTE: Any java client using I_XmlBlasterAccess helper class will switch
36 * to XMLRPC if the command line parameter -protocol is specified as follows:
37 * <br />
38 * <pre>
39 * java -cp lib/xmlBlaster.jar javaclients.ClientSub -dispatch/connection/protocol XMLRPC
40 * </pre>
41 *
42 * @author "Michele Laghi" <michele@laghi.eu>
43 * @see org.xmlBlaster.client.protocol.xmlrpc.XmlRpcConnection
44 */
45 public class XmlRpcHttpClient {
46 private static Logger log = Logger.getLogger(XmlRpcHttpClient.class.getName());
47
48
49 /**
50 * Constructor.
51 */
52 public XmlRpcHttpClient ()
53 {
54 }
55
56
57 /**
58 * Here come the methods to invoke if you want to use the xml-rpc
59 * protocol transparently ...
60 * <br />
61 */
62 private void testWithHelperClasses(Global glob)
63 {
64
65 try {
66 // force XMLRPC protocol:
67 glob.getProperty().set("client.protocol", "XMLRPC");
68
69 I_XmlBlasterAccess client = glob.getXmlBlasterAccess();
70
71 log.info("Going to invoke xmlBlaster using XmlRpc-I_XmlBlasterAccess");
72 ConnectQos connectQos = new ConnectQos(glob, "LunaMia", "silence");
73 client.connect(connectQos, null);
74 log.info("Connection successful");
75
76 String contentString = "This is a simple Test Message for the xml-rpc Protocol";
77 byte[] content = contentString.getBytes();
78
79 PublishKey xmlKey = new PublishKey(glob, "", "text/plain", null);
80
81 MsgUnit msgUnit = new MsgUnit(xmlKey.toXml(), content, "<qos><forceUpdate /></qos>");
82 client.publish(msgUnit);
83 log.info("Published a message");
84
85 client.disconnect(null);
86 }
87 catch (XmlBlasterException ex) {
88 log.severe("exception: " + ex.toString());
89 }
90 catch (Throwable ex1) {
91 System.err.println("exception:" + ex1);
92 }
93 }
94
95
96 /**
97 * Only for testing purposes.
98 * <pre>
99 * java javaclients.xmlrpc.XmlRpcHttpClient < demo.xml -logging FINE
100 * </pre>
101 */
102 public static void main (String args[])
103 {
104 final String ME = "XmlRpcHttpClient";
105
106 Global glob = new Global();
107 if (glob.init(args) != 0) {
108 usage();
109 System.exit(1);
110 }
111
112 XmlRpcHttpClient client = new XmlRpcHttpClient();
113
114 client.testWithHelperClasses(glob);
115 }
116
117
118 /**
119 * Command line usage.
120 */
121 private static void usage()
122 {
123 System.out.println("----------------------------------------------------------");
124 System.out.println("java javaclients.xmlrpc.XmlRpcHttpClient < demo.xml <options>");
125 System.out.println("----------------------------------------------------------");
126 System.out.println(Global.instance().usage());
127 System.out.println("----------------------------------------------------------");
128 System.out.println("");
129 }
130 }
syntax highlighted by Code2HTML, v. 0.9.1