1 /*------------------------------------------------------------------------------
2 Name: XmlRpcHttpClientRaw.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: XmlRpcHttpClientRaw.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.io.*;
13 import java.net.URL;
14 import java.net.HttpURLConnection;
15 import java.net.MalformedURLException;
16
17 import java.util.logging.Logger;
18 import org.xmlBlaster.util.Global;
19 import org.xmlBlaster.util.XmlBlasterException;
20
21 /**
22 * Raw demo showing how to implement a client which connects to xmlBlaster via
23 * xml-rpc.
24 * <br />
25 * This is a demo showing what happens 'low level', used with 'demo.xml',
26 * sending the method calls directly in xml style.
27 * <br />
28 * You must be careful when sending xml strings.
29 * The "<" character inside these strings must be
30 * converted to "<", otherwise the xmlrpc parser will erroneously try to
31 * parse xmlBlaster specific stuff, resulting in a parsing error (see demo.xml
32 * on this directory).
33 * <p/>
34 * This demo first reads "raw" procedure calls from the standard input.
35 *
36 * @author "Michele Laghi" <michele@laghi.eu>
37 * @see org.xmlBlaster.client.protocol.xmlrpc.XmlRpcConnection
38 */
39 public class XmlRpcHttpClientRaw
40 {
41 private static final String ME = "XmlRpcHttpClientRaw";
42 private final Global glob;
43 private static Logger log = Logger.getLogger(XmlRpcHttpClientRaw.class.getName());
44
45 /**
46 * Constructor.
47 */
48 public XmlRpcHttpClientRaw (Global glob)
49 {
50 this.glob = glob;
51
52 }
53
54
55 /**
56 * executes a 'raw' xml-rpc method call through the HTTP protocol.
57 * @param inputString the xml-rpc string to execute (xml literal)
58 * @return a string containing the result of the remote procedure call.
59 */
60 public String execute(String urlStr, String inputString) throws XmlBlasterException
61 {
62 StringBuffer ret = null;
63 try {
64 // connect to the server
65 URL url = new URL(urlStr);
66 HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
67
68 // send a POST
69 urlConnection.setRequestMethod("POST");
70 urlConnection.setDoOutput(true);
71 OutputStream outStream = urlConnection.getOutputStream();
72 byte[] conversionHelper = inputString.getBytes();
73 outStream.write(conversionHelper);
74 outStream.flush();
75
76 // read the answer of the server and store it in a string
77 ret = new StringBuffer();
78 InputStream inStream = urlConnection.getInputStream();
79 int bytes = 0, deltaBytes = 0;
80 int maxBytes = urlConnection.getContentLength();
81
82 // while ( (bytes = inStream.available()) > 0) {
83 while (bytes < maxBytes) {
84 deltaBytes = inStream.available();
85 byte buffer[] = new byte[deltaBytes];
86 inStream.read(buffer);
87 bytes += deltaBytes;
88 ret.append(new String(buffer));
89 }
90 }
91
92 catch (MalformedURLException ex1) {
93 log.severe(ex1.toString());
94 throw new XmlBlasterException(ME, ex1.toString());
95 }
96 catch (IOException ex2) {
97 log.severe(ex2.toString());
98 throw new XmlBlasterException(ME, ex2.toString());
99 }
100
101 return ret.toString();
102 }
103
104
105 /**
106 * Invokes the methods as described in demo.xml
107 */
108 private void testRaw()
109 {
110 try {
111 String host = glob.getProperty().get("dispatch/connection/plugin/xmlrpc/hostname", "localhost");
112 int port = glob.getProperty().get("dispatch/connection/plugin/xmlrpc/port", 8080);
113 // int cb_port = glob.getProperty().get("dispatch/callback/plugin/xmlrpc/port", 8081);
114 String urlStr = "http://" + host + ":" + port;
115
116 log.info("Connected to xmlBlaster using XMLRPC");
117
118 // reads from the standard input stream. Ignores the lines until the
119 // first comment line containing the word COMMAND
120
121 BufferedReader reader = new BufferedReader( new InputStreamReader(System.in) );
122 String line = "";
123
124 String cmd = "";
125 boolean hasStarted = false;
126 log.info("Processing data from stdin ...");
127 while ( (line = reader.readLine()) != null) {
128 if (line.indexOf("COMMAND") == -1) {
129 if (hasStarted) cmd += line + "\n";
130 }
131
132 else {
133 if (cmd.length() > 0) {
134 System.out.println("THE COMMAND IS: " + cmd + "\nEND OF COMMAND");
135 System.out.println(execute(urlStr, cmd));
136 }
137 cmd = "";
138 hasStarted = true;
139 }
140
141 }
142 }
143 catch (XmlBlasterException ex) {
144 log.severe("exception: " + ex);
145 }
146 catch (IOException ex1) {
147 log.severe("exception:" + ex1);
148 }
149 }
150
151
152 /**
153 * Only for testing purposes.
154 * <pre>
155 * java javaclients.xmlrpc.XmlRpcHttpClientRaw < demo.xml -logging FINE
156 * </pre>
157 */
158 public static void main (String args[])
159 {
160 Global glob = new Global();
161 if (glob.init(args) != 0) {
162 usage();
163 System.exit(1);
164 }
165 XmlRpcHttpClientRaw client = new XmlRpcHttpClientRaw(glob);
166 client.testRaw();
167 }
168
169
170 /**
171 * Command line usage.
172 */
173 private static void usage()
174 {
175 System.out.println("----------------------------------------------------------");
176 System.out.println("java javaclients.xmlrpc.XmlRpcHttpClientRaw < demo.xml <options>");
177 System.out.println("----------------------------------------------------------");
178 System.out.println(" -h Show the complete usage.");
179 System.out.println(" -dispatch/connection/plugin/xmlrpc/hostname The XMLRPC web server host [localhost].");
180 System.out.println(" -dispatch/connection/plugin/xmlrpc/port The XMLRPC web server port [8080].");
181 //System.out.println(" -xmlrpc.cb_host My XMLRPC callback web server host (e.g. for multi homed hosts) [localhost].");
182 //System.out.println(" -xmlrpc.cb_port My XMLRPC callback web server port [8081].");
183 System.out.println(Global.instance().usage());
184 System.out.println("----------------------------------------------------------");
185 System.out.println("");
186 }
187 }
syntax highlighted by Code2HTML, v. 0.9.1