PageRenderTime 27ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/jWebSocket/libs/J2SE/Javolution/src/javolution/xml/ws/WebServiceClient.java

http://jwebsocket.googlecode.com/
Java | 235 lines | 131 code | 17 blank | 87 comment | 13 complexity | b89fa6d7307a7bc06f2d2d894cf4db1c MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
  3. * Copyright (C) 2007 - Javolution (http://javolution.org/)
  4. * All rights reserved.
  5. *
  6. * Permission to use, copy, modify, and distribute this software is
  7. * freely granted, provided that this notice is preserved.
  8. */
  9. package javolution.xml.ws;
  10. import java.io.IOException;
  11. import java.lang.CharSequence;
  12. import java.lang.UnsupportedOperationException;
  13. import javolution.io.AppendableWriter;
  14. import javolution.io.UTF8StreamWriter;
  15. import javolution.text.Text;
  16. import javolution.text.TextBuilder;
  17. import javolution.xml.XMLObjectReader;
  18. import javolution.xml.XMLObjectWriter;
  19. import javolution.xml.stream.XMLStreamException;
  20. import javolution.xml.stream.XMLStreamReader;
  21. import javolution.xml.stream.XMLStreamWriter;
  22. /**
  23. * <p> This class provides a simple web service client capable of leveraging
  24. * Javolution {@link javolution.xml XML marshalling/unmarshalling}.</p>
  25. *
  26. * <p> Sub-classes may work from WSDL files, {@link javolution.xml.XMLFormat
  27. * XMLFormat} or directly with the XML streams (StAX). For example:[code]
  28. * private static class HelloWorld extends WebServiceClient {
  29. * protected void writeRequest(XMLObjectWriter out) throws XMLStreamException {
  30. * XMLStreamWriter xml = out.getStreamWriter();
  31. * xml.writeDefaultNamespace("http://www.openuri.org/");
  32. * xml.writeEmptyElement("helloWorld"); // Operation name.
  33. * }
  34. * protected void readResponse(XMLObjectReader in) throws XMLStreamException {
  35. * XMLStreamReader xml = in.getStreamReader();
  36. * xml.require(START_ELEMENT, "http://www.openuri.org/", "string");
  37. * xml.next(); // Move to character content.
  38. * System.out.println(xml.getText());
  39. * }
  40. * }
  41. * WebServiceClient ws = new HelloWorld().setAddress("http://acme.com:80/HelloWorld.jws");
  42. * ws.invoke();
  43. *
  44. * > Hello World!
  45. * [/code]</p>
  46. *
  47. * <p><b>Note:</b> At this moment, this class is supported only on the J2SE
  48. * platform. Soon, it will also be supported on mobile devices
  49. * through the CLDC/MIDP Generic Connection framework.</p>
  50. *
  51. * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
  52. * @version 5.2, September 16, 2007
  53. */
  54. public abstract class WebServiceClient {
  55. /**
  56. * Holds standard SOAP envelope prefix.
  57. */
  58. public static final String ENVELOPE_PREFIX = "env";
  59. /**
  60. * Holds standard SOAP envelope namespace.
  61. */
  62. public static final String ENVELOPE_URI = "http://schemas.xmlsoap.org/soap/envelope/";
  63. /**
  64. * Holds the URL (J2SE).
  65. */
  66. Object _url;
  67. /**
  68. * Default constructor (address not set).
  69. */
  70. public WebServiceClient() {
  71. /**/
  72. if (true) return;
  73. /**/
  74. throw new UnsupportedOperationException("J2ME Not Supported Yet");
  75. }
  76. /**
  77. * Sets the address of this web service.
  78. *
  79. * @param address the service full address.
  80. */
  81. public WebServiceClient setAddress(String address) {
  82. /**/
  83. try {
  84. _url = new java.net.URL(address);
  85. } catch (java.net.MalformedURLException e) {
  86. throw new IllegalArgumentException("Malformed URL: " + address);
  87. }
  88. /**/
  89. return this;
  90. }
  91. /**
  92. * Invokes the web service.
  93. */
  94. public void invoke() throws IOException, XMLStreamException {
  95. try {
  96. // Formats the request message (we cannot write directly to
  97. // the output stream because the http request requires the length.
  98. _out.setOutput(_buffer);
  99. _writer.setOutput(_out);
  100. final XMLStreamWriter xmlOut = _writer.getStreamWriter();
  101. xmlOut.setPrefix(csq(ENVELOPE_PREFIX), csq(ENVELOPE_URI));
  102. xmlOut.writeStartElement(csq(ENVELOPE_URI), csq("Envelope"));
  103. xmlOut.writeNamespace(csq(ENVELOPE_PREFIX), csq(ENVELOPE_URI));
  104. xmlOut.writeStartElement(csq(ENVELOPE_URI), csq("Header"));
  105. xmlOut.writeEndElement();
  106. xmlOut.writeStartElement(csq(ENVELOPE_URI), csq("Body"));
  107. writeRequest(_writer);
  108. _writer.close();
  109. // Sends the request.
  110. if (_url == null)
  111. throw new IOException("URL not set");
  112. /**/
  113. java.net.HttpURLConnection http = (java.net.HttpURLConnection)
  114. ((java.net.URL)_url).openConnection();
  115. http.setRequestProperty("Content-Length", String.valueOf(_buffer
  116. .length()));
  117. http.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
  118. // httpConn.setRequestProperty("SOAPAction", "");
  119. http.setRequestMethod("POST");
  120. http.setDoOutput(true);
  121. http.setDoInput(true);
  122. _utf8Writer.setOutput(http.getOutputStream());
  123. /**/
  124. _buffer.print(_utf8Writer);
  125. _utf8Writer.close();
  126. // Reads the response.
  127. /**/
  128. _reader.setInput(http.getInputStream());
  129. /**/
  130. final XMLStreamReader xmlIn = _reader.getStreamReader();
  131. while (xmlIn.hasNext()) {
  132. if ((xmlIn.next() == XMLStreamReader.START_ELEMENT)
  133. && xmlIn.getLocalName().equals("Body")
  134. && xmlIn.getNamespaceURI().equals(ENVELOPE_URI)) {
  135. // Found body, position reader to next element.
  136. xmlIn.next();
  137. readResponse(_reader);
  138. break;
  139. }
  140. }
  141. } finally {
  142. _reader.close();
  143. _writer.reset();
  144. _out.reset();
  145. _buffer.reset();
  146. _utf8Writer.reset();
  147. _reader.reset();
  148. }
  149. }
  150. private final TextBuilder _buffer = new TextBuilder();
  151. private final AppendableWriter _out = new AppendableWriter();
  152. private final XMLObjectWriter _writer = new XMLObjectWriter();
  153. private final UTF8StreamWriter _utf8Writer = new UTF8StreamWriter();
  154. private final XMLObjectReader _reader = new XMLObjectReader();
  155. /**/
  156. /**
  157. * Writes the web service request (SOAP body).
  158. *
  159. * @param out the XML object writer.
  160. */
  161. protected abstract void writeRequest(XMLObjectWriter out)
  162. throws XMLStreamException;
  163. /**
  164. * Reads the web service response (SOAP body). The default implementation
  165. * writes the body XML events to <code>System.out</code>.
  166. *
  167. * @param in the XML object reader.
  168. */
  169. protected void readResponse(XMLObjectReader in) throws XMLStreamException {
  170. final XMLStreamReader xml = in.getStreamReader();
  171. while (xml.hasNext()) {
  172. switch (xml.next()) {
  173. case XMLStreamReader.START_DOCUMENT:
  174. System.out.println("Start Document");
  175. break;
  176. case XMLStreamReader.END_DOCUMENT:
  177. System.out.println("End Document.");
  178. break;
  179. case XMLStreamReader.START_ELEMENT:
  180. System.out.println("Start Element: " + xml.getLocalName() + "("
  181. + xml.getNamespaceURI() + ")");
  182. for (int i = 0, n = xml.getAttributeCount(); i < n; i++) {
  183. System.out.println(" Attribute: "
  184. + xml.getAttributeLocalName(i) + "("
  185. + xml.getAttributeNamespace(i) + "), Value: "
  186. + xml.getAttributeValue(i));
  187. }
  188. break;
  189. case XMLStreamReader.END_ELEMENT:
  190. if (xml.getLocalName().equals("Body")
  191. && xml.getNamespaceURI().equals(ENVELOPE_URI))
  192. return; // End body.
  193. System.out.println("End Element: " + xml.getLocalName() + "("
  194. + xml.getNamespaceURI() + ")");
  195. break;
  196. case XMLStreamReader.CHARACTERS:
  197. System.out.println("Characters: " + xml.getText());
  198. break;
  199. case XMLStreamReader.CDATA:
  200. System.out.println("CDATA: " + xml.getText());
  201. break;
  202. case XMLStreamReader.COMMENT:
  203. System.out.println("Comment: " + xml.getText());
  204. break;
  205. case XMLStreamReader.SPACE:
  206. System.out.println("Space");
  207. break;
  208. default:
  209. System.out.println(xml);
  210. }
  211. }
  212. }
  213. // For J2ME compatiblity.
  214. private static final CharSequence csq(Object string) {
  215. return (string instanceof CharSequence) ? (CharSequence) string:
  216. Text.valueOf(string);
  217. }
  218. }