Java Web Services Message Client Redux

What’s the simplest thing that …

I’m sure that my colleagues are tired of me asking “What’s the simplest thing …”. But simple things are focused on just one thing. And when the simple thing fails, then there is just one thing to check. In furtherance of the “simplest thing”, here is an updated Java Web Services Message Client pointed at a publicly available web service. Thanks to Sean Collins of XML Me for allowing us to use their web service.

This client reads an input document from standard input, invokes a web service, and then prints the web service response. It has been updated to support the SOAPActionURI required by the Shakespeare web service and to force a newline between XML elements (changes are shown in bold). The results shown here use Java 1.4.2_11 and Axis 1.3.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.constants.Style;
import org.apache.axis.Message;
import org.apache.axis.message.SOAPEnvelope;


public class Invoke {

public static void main ( String[] args )
throws Exception
{

String endpoint = "http://www.xmlme.com/WSShakespeare.asmx";
String action = "http://xmlme.com/WebServices/GetSpeech";
/*
* Two optional command line arguments:
* the web service uri
* the web action uri
*/

switch(args.length) {
case 0:
break;
case 1:
endpoint = args[0];
action = null;
break;
case 2:
endpoint = args[0];
action = args[1];
break;
default:
System.out.println( "Usage java -cp $AXISCLASSPATH Invoke [WebServiceUri] [WebActionUri] < data.xml" );
System.exit(2);
}
/*
* Read input
*/

String document = streamToString( System.in );
System.out.println( document );
System.out.println( "Invoking " + endpoint + "\n" );
/*
* Pass to web service
*/

Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress( new java.net.URL(endpoint) );
call.setOperationStyle( org.apache.axis.constants.Style.MESSAGE );
if ( action != null ) {
call.setProperty(Call.SOAPACTION_USE_PROPERTY,Boolean.TRUE);
call.setProperty(Call.SOAPACTION_URI_PROPERTY,action);
}
SOAPEnvelope envelope = call.invoke( new Message(document) );
/*
* Print the Web Service return
*/

System.out.println( envelope.getBody().toString().replaceAll("><",">\n<") );
}

/**
* Utility to read textual data from in InputStream to a String
*/

public static String streamToString( java.io.InputStream is )
throws java.io.IOException
{

java.io.InputStreamReader ir = new java.io.InputStreamReader(is);
java.io.BufferedReader in = new java.io.BufferedReader(ir);

StringBuffer buffer = new StringBuffer();
{
String line = null;
while( (line=in.readLine()) != null ) {
buffer.append( line );
buffer.append( '\n' );
}
}
return buffer.toString();
}
}

And here is the client in action. For brevity, I’ve requested a Shakespeare speech containing those famous words “Hello, World”. You may want to request something more along the lines of: “dogs of war” or “unto the breach”.

1
12 $ javac -classpath $AXISCLASSPATH Invoke.java
13 $ java -classpath $AXISCLASSPATH Invoke < data/speech.xml
<?xml version="1.0" standalone="yes" ?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
               xmlns:ns="http://xmlme.com/WebServices">
  <soap:Body>
    <ns:GetSpeech>
      <ns:Request>Hello, World</ns:Request>
    </ns:GetSpeech>
  </soap:Body>
</soap:Envelope>

Invoking http://www.xmlme.com/WSShakespeare.asmx

<soapenv:Body xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<GetSpeechResponse xmlns="http://xmlme.com/WebServices">
<GetSpeechResult>&lt;MESSAGE&gt;Speech not found.&lt;/MESSAGE&gt;</GetSpeechResult>
</GetSpeechResponse>
</soapenv:Body>
14 $

9 Feb: Examing your Java app with a debugging proxy.