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

« back to all changes in this revision

Viewing changes to src/nu/xom/samples/FibonacciXOMSOAPServlet.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-2004 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.OutputStreamWriter;
 
27
import java.math.BigInteger;
 
28
 
 
29
import javax.servlet.ServletException;
 
30
import javax.servlet.SingleThreadModel;
 
31
import javax.servlet.http.HttpServlet;
 
32
import javax.servlet.http.HttpServletRequest;
 
33
import javax.servlet.http.HttpServletResponse;
 
34
 
 
35
import nu.xom.Builder;
 
36
import nu.xom.Document;
 
37
import nu.xom.Element;
 
38
import nu.xom.WellformednessException;
 
39
 
 
40
 
 
41
/**
 
42
 * <p>
 
43
 * Demonstrates a servlet that receives and
 
44
 * responds to SOAP requests.
 
45
 * </p>
 
46
 * 
 
47
 * @author Elliotte Rusty Harold
 
48
 * @version 1.0
 
49
 *
 
50
 */
 
51
public class FibonacciXOMSOAPServlet extends HttpServlet 
 
52
  implements SingleThreadModel {
 
53
 
 
54
    // Fault codes   
 
55
    public final static String MALFORMED_REQUEST_DOCUMENT 
 
56
      = "MalformedRequest";
 
57
    public final static String INVALID_REQUEST_DOCUMENT 
 
58
      = "InvalidRequest";
 
59
    public final static String INDEX_MISSING 
 
60
      = "IndexMissing";
 
61
    public final static String NON_POSITIVE_INDEX 
 
62
      = "NonPositiveIndex";
 
63
    public final static String BAD_INTEGER_FORMAT
 
64
      = "BadIntegerFormat";
 
65
    public final static String UNEXPECTED_PROBLEM
 
66
      = "UnexpectedProblem";    
 
67
    
 
68
    private transient Builder parser;
 
69
  
 
70
    // Load a parser, transformer, and implementation
 
71
    public void init() throws ServletException {  
 
72
  
 
73
        try {
 
74
          this.parser = new Builder();
 
75
        }
 
76
        catch (Exception ex) { 
 
77
          throw new ServletException(
 
78
           "Could not locate a XOM parser", ex); 
 
79
        }
 
80
 
 
81
    } 
 
82
  
 
83
  
 
84
    public void doPost(HttpServletRequest servletRequest,
 
85
      HttpServletResponse servletResponse)
 
86
      throws ServletException, IOException {
 
87
    
 
88
        servletResponse.setContentType("application/soap+xml; charset=UTF-8");               
 
89
        OutputStreamWriter out = new OutputStreamWriter(
 
90
          servletResponse.getOutputStream(), "UTF-8");
 
91
        InputStream in = servletRequest.getInputStream();
 
92
    
 
93
        Document request;
 
94
        Document response;
 
95
        String generations ="here";
 
96
        try {
 
97
            request = parser.build(in);
 
98
       
 
99
/* <?xml version="1.0"?>
 
100
<SOAP-ENV:Envelope
 
101
 xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
 
102
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
 
103
  <SOAP-ENV:Body>
 
104
    <calculateFibonacci 
 
105
      xmlns="http://namespaces.cafeconleche.org/xmljava/ch3/"
 
106
      type="xsi:positiveInteger">10</calculateFibonacci>
 
107
  </SOAP-ENV:Body>
 
108
</SOAP-ENV:Envelope> */       
 
109
       
 
110
            generations = request.getValue().trim();
 
111
            int numberOfGenerations = Integer.parseInt(generations);
 
112
            BigInteger result = calculateFibonacci(numberOfGenerations);
 
113
            response = makeResponseDocument(result);
 
114
        }
 
115
        catch (WellformednessException ex) {  
 
116
            response = makeFaultDocument(MALFORMED_REQUEST_DOCUMENT, 
 
117
              ex.getMessage());
 
118
        }
 
119
        catch (NullPointerException ex) {  
 
120
            response = makeFaultDocument(INDEX_MISSING, 
 
121
              ex.getMessage());
 
122
        }
 
123
        catch (NumberFormatException ex) {  
 
124
            response = makeFaultDocument(BAD_INTEGER_FORMAT, 
 
125
              generations + ex.getMessage());
 
126
        }
 
127
        catch (IndexOutOfBoundsException ex) {  
 
128
            response = makeFaultDocument(NON_POSITIVE_INDEX, 
 
129
              ex.getMessage());
 
130
        }
 
131
        catch (Exception ex) {  
 
132
            response = makeFaultDocument(UNEXPECTED_PROBLEM, 
 
133
              ex.getMessage());
 
134
        }
 
135
    
 
136
        // Transform onto the OutputStream
 
137
        try {
 
138
            out.write(response.toXML());
 
139
            servletResponse.flushBuffer();
 
140
            out.flush();
 
141
        }
 
142
        catch (IOException ex) {
 
143
            // If we get an exception at this point, it's too late to
 
144
            // switch over to a SOAP fault
 
145
            throw new ServletException(ex); 
 
146
        }
 
147
        finally {
 
148
            in.close();
 
149
            out.close();
 
150
        }
 
151
    
 
152
    }
 
153
 
 
154
    
 
155
    // The details of the formats and namespace URIs are likely to
 
156
    // change when SOAP 1.2 is released.
 
157
    public Document makeResponseDocument(BigInteger result) {
 
158
    
 
159
        Element envelope = new Element("SOAP-ENV:Envelope",
 
160
          "http://schemas.xmlsoap.org/soap/envelope/");
 
161
        Document response = new Document(envelope); 
 
162
        Element body = new Element("SOAP-ENV:Body", 
 
163
          "http://schemas.xmlsoap.org/soap/envelope/");
 
164
        envelope.appendChild(body);
 
165
     
 
166
        Element Fibonacci_Numbers = new Element("Fibonacci_Numbers",  
 
167
         "http://namespaces.cafeconleche.org/xmljava/ch3/");
 
168
        body.appendChild(Fibonacci_Numbers);
 
169
    
 
170
        Element fibonacci = new Element("fibonacci",
 
171
         "http://namespaces.cafeconleche.org/xmljava/ch3/");
 
172
        Fibonacci_Numbers.appendChild(fibonacci);
 
173
        fibonacci.appendChild(result.toString());
 
174
    
 
175
        return response;
 
176
   
 
177
    }
 
178
  
 
179
    
 
180
    public Document makeFaultDocument(String code, String message){
 
181
     
 
182
        Element envelope = new Element("SOAP-ENV:Envelope",
 
183
          "http://schemas.xmlsoap.org/soap/envelope/");
 
184
        Document faultDoc = new Document(envelope);
 
185
    
 
186
        Element body = new Element("SOAP-ENV:Body", 
 
187
          "http://schemas.xmlsoap.org/soap/envelope/");
 
188
        envelope.appendChild(body);
 
189
     
 
190
        Element fault = new Element("Fault", 
 
191
         "http://schemas.xmlsoap.org/soap/envelope/");
 
192
        body.appendChild(fault);
 
193
    
 
194
        Element faultCode = new Element("faultcode");
 
195
        fault.appendChild(faultCode);
 
196
    
 
197
        Element faultString = new Element("faultstring");
 
198
        fault.appendChild(faultString);
 
199
    
 
200
        faultCode.appendChild(code);
 
201
    
 
202
        faultString.appendChild(message);
 
203
        
 
204
        return faultDoc;
 
205
       
 
206
    } 
 
207
  
 
208
    
 
209
    public static BigInteger calculateFibonacci(int generations) 
 
210
      throws IndexOutOfBoundsException {
 
211
    
 
212
        if (generations < 1) {
 
213
            throw new IndexOutOfBoundsException(
 
214
              "Fibonacci numbers are not defined for " + generations 
 
215
              + "or any other number less than one.");
 
216
        }
 
217
        BigInteger low  = BigInteger.ONE;
 
218
        BigInteger high = BigInteger.ONE;      
 
219
        for (int i = 2; i <= generations; i++) {
 
220
            BigInteger temp = high;
 
221
            high = high.add(low);
 
222
            low = temp;
 
223
        }
 
224
        return low;   
 
225
        
 
226
    }
 
227
 
 
228
}