~ubuntu-branches/ubuntu/precise/xom/precise

« back to all changes in this revision

Viewing changes to src/nu/xom/samples/FibonacciSOAPClient.java

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2007-11-25 15:50:40 UTC
  • Revision ID: james.westby@ubuntu.com-20071125155040-r75ikcqf1vu0cei7
Tags: upstream-1.1
ImportĀ upstreamĀ versionĀ 1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2002, 2003 Elliotte Rusty Harold
 
2
   
 
3
   This library is free software; you can redistribute it and/or modify
 
4
   it under the terms of version 2.1 of the GNU Lesser General Public 
 
5
   License as published by the Free Software Foundation.
 
6
   
 
7
   This library is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 
10
   GNU Lesser General Public License for more details.
 
11
   
 
12
   You should have received a copy of the GNU Lesser General Public
 
13
   License along with this library; if not, write to the 
 
14
   Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
 
15
   Boston, MA 02111-1307  USA
 
16
   
 
17
   You can contact Elliotte Rusty Harold by sending e-mail to
 
18
   elharo@metalab.unc.edu. Please include the word "XOM" in the
 
19
   subject line. The XOM home page is located at http://www.xom.nu/
 
20
*/
 
21
 
 
22
package nu.xom.samples;
 
23
 
 
24
import java.io.IOException;
 
25
import java.io.InputStream;
 
26
import java.io.OutputStream;
 
27
import java.net.HttpURLConnection;
 
28
import java.net.URL;
 
29
import java.net.URLConnection;
 
30
 
 
31
import nu.xom.Attribute;
 
32
import nu.xom.Builder;
 
33
import nu.xom.Document;
 
34
import nu.xom.Element;
 
35
import nu.xom.Elements;
 
36
import nu.xom.ParsingException;
 
37
import nu.xom.Serializer;
 
38
 
 
39
/**
 
40
 * <p>
 
41
 * Demonstrates communication with a SOAP
 
42
 * server via the creation of an XML document,
 
43
 * transmission of that document over the network,
 
44
 * and reception and parsing of the server's response
 
45
 * document.
 
46
 * </p>
 
47
 * 
 
48
 * @author Elliotte Rusty Harold
 
49
 * @version 1.0
 
50
 *
 
51
 */
 
52
public class FibonacciSOAPClient {
 
53
  
 
54
    public final static String defaultServer 
 
55
      = "http://www.elharo.com/fibonacci/SOAP";
 
56
    public final static String SOAP_ACTION 
 
57
      = "http://www.example.com/fibonacci";
 
58
      
 
59
    public static void main(String[] args) {
 
60
 
 
61
        if (args.length == 0) {
 
62
            System.out.println(
 
63
              "Usage: java nu.xom.samples.FibonacciSOAPClient index serverURL"
 
64
            ); 
 
65
            return;
 
66
        }
 
67
    
 
68
        String index = args[0];
 
69
        
 
70
        String server;
 
71
        if (args.length <= 1) server = defaultServer;
 
72
        else server = args[1];
 
73
          
 
74
        Document request = buildRequest(index);
 
75
        
 
76
        try {
 
77
            URL u = new URL(server);
 
78
            URLConnection uc = u.openConnection();
 
79
            HttpURLConnection connection = (HttpURLConnection) uc;
 
80
            connection.setDoOutput(true);
 
81
            connection.setDoInput(true); 
 
82
            connection.setRequestMethod("POST");
 
83
            connection.setRequestProperty("SOAPAction", SOAP_ACTION);
 
84
         
 
85
            OutputStream out = connection.getOutputStream();
 
86
            Serializer serializer = new Serializer(out, "US-ASCII");
 
87
            serializer.write(request);
 
88
            serializer.flush();
 
89
              
 
90
            InputStream in = connection.getInputStream();
 
91
              
 
92
            Builder parser = new Builder();
 
93
            Document response = parser.build(in);
 
94
            in.close();
 
95
            out.close();
 
96
            connection.disconnect();
 
97
 
 
98
        /* This is the response we expect:
 
99
         * <?xml version="1.0"?>
 
100
<SOAP-ENV:Envelope
 
101
 xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" />
 
102
  <SOAP-ENV:Body>
 
103
    <Fibonacci_Numbers 
 
104
      xmlns="http://namespaces.cafeconleche.org/xmljava/ch3/">
 
105
      <fibonacci index="1">1</fibonacci>
 
106
      <fibonacci index="2">1</fibonacci>
 
107
      <fibonacci index="3">2</fibonacci>
 
108
      <fibonacci index="4">3</fibonacci>
 
109
      <fibonacci index="5">5</fibonacci>
 
110
      <fibonacci index="6">8</fibonacci>
 
111
      <fibonacci index="7">13</fibonacci>
 
112
      <fibonacci index="8">21</fibonacci>
 
113
      <fibonacci index="9">34</fibonacci>
 
114
      <fibonacci index="10">55</fibonacci>
 
115
    </Fibonacci_Numbers>
 
116
  </SOAP-ENV:Body>
 
117
</SOAP-ENV:Envelope>
 
118
         * 
 
119
         */
 
120
 
 
121
            Element responseEnvelope = response.getRootElement();          
 
122
            Element responseBody = responseEnvelope.getFirstChildElement("Body", 
 
123
             "http://schemas.xmlsoap.org/soap/envelope/");
 
124
             
 
125
            // Check for fault
 
126
            Element fault = responseBody.getFirstChildElement(
 
127
             "Fault", "http://schemas.xmlsoap.org/soap/envelope/");
 
128
            if (fault == null) { // no fault
 
129
                Element responseNumbers = responseBody.getFirstChildElement(
 
130
                 "Fibonacci_Numbers", "http://namespaces.cafeconleche.org/xmljava/ch3/");
 
131
                Elements results = responseNumbers.getChildElements("fibonacci", 
 
132
                 "http://namespaces.cafeconleche.org/xmljava/ch3/");
 
133
                for (int i = 0; i < results.size(); i++) {
 
134
                    System.out.println(results.get(i).getValue());    
 
135
                }
 
136
            }
 
137
            else { 
 
138
                handleFault(fault);
 
139
            }   
 
140
          
 
141
        }
 
142
        catch (ParsingException ex) {
 
143
          System.err.println("Server sent malformed output"); 
 
144
          System.err.println(ex); 
 
145
        }
 
146
        catch (NullPointerException ex) {
 
147
            System.err.println(
 
148
              "Server sent invalid output without the expected content."
 
149
            ); 
 
150
            System.err.println(ex); 
 
151
        }
 
152
        catch (IOException ex) {
 
153
            System.err.println(ex); 
 
154
            ex.printStackTrace();
 
155
        }
 
156
  
 
157
    }
 
158
 
 
159
    private static void handleFault(Element fault) {
 
160
        
 
161
        Element faultcode = fault.getFirstChildElement("faultcode");
 
162
        Element faultstring = fault.getFirstChildElement("faultstring");
 
163
        Element faultactor = fault.getFirstChildElement("faultactor");
 
164
        Element detail = fault.getFirstChildElement("detail");
 
165
        
 
166
        String error = "Fault: \n";
 
167
        if (faultcode != null) {
 
168
            error += "Fault code: " + faultcode.getValue() + "\n";   
 
169
        }
 
170
        if (faultstring != null) {
 
171
            error += "Fault string: " + faultstring.getValue() + "\n";   
 
172
        }
 
173
        if (faultactor != null) {
 
174
            error += "Fault actor: " + faultactor.getValue() + "\n";   
 
175
        }
 
176
        if (detail != null) {
 
177
            error += "Details: " + detail.getValue() + "\n";   
 
178
        }
 
179
        
 
180
    }
 
181
 
 
182
    public static Document buildRequest(String index) {
 
183
        
 
184
        String SOAPNamespace = "http://schemas.xmlsoap.org/soap/envelope/";
 
185
        Element envelope = new Element("SOAP-ENV:Envelope",
 
186
         SOAPNamespace);
 
187
        Element body = new Element("SOAP-ENV:Body",
 
188
         SOAPNamespace);
 
189
        Element calculateFibonacci = new Element("calculateFibonacci",
 
190
         "http://namespaces.cafeconleche.org/xmljava/ch3/");
 
191
        calculateFibonacci.appendChild(index);
 
192
        calculateFibonacci.addNamespaceDeclaration("xsi", 
 
193
         "http://www.w3.org/2001/XMLSchema-instance");
 
194
        Attribute type = new Attribute("type", "xsi:positiveInteger");
 
195
        calculateFibonacci.addAttribute(type);
 
196
        
 
197
        envelope.appendChild(body);
 
198
        body.appendChild(calculateFibonacci);
 
199
        
 
200
        Document doc = new Document(envelope);
 
201
        return doc;
 
202
        
 
203
    }  
 
204
 
 
205
}
 
 
b'\\ No newline at end of file'