1
/* Copyright 2002-2004 Elliotte Rusty Harold
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.
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.
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
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/
22
package nu.xom.samples;
24
import java.io.IOException;
25
import java.io.InputStream;
26
import java.io.OutputStreamWriter;
27
import java.math.BigInteger;
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;
35
import nu.xom.Builder;
36
import nu.xom.Document;
37
import nu.xom.Element;
38
import nu.xom.WellformednessException;
43
* Demonstrates a servlet that receives and
44
* responds to SOAP requests.
47
* @author Elliotte Rusty Harold
51
public class FibonacciXOMSOAPServlet extends HttpServlet
52
implements SingleThreadModel {
55
public final static String MALFORMED_REQUEST_DOCUMENT
57
public final static String INVALID_REQUEST_DOCUMENT
59
public final static String INDEX_MISSING
61
public final static String NON_POSITIVE_INDEX
63
public final static String BAD_INTEGER_FORMAT
65
public final static String UNEXPECTED_PROBLEM
66
= "UnexpectedProblem";
68
private transient Builder parser;
70
// Load a parser, transformer, and implementation
71
public void init() throws ServletException {
74
this.parser = new Builder();
76
catch (Exception ex) {
77
throw new ServletException(
78
"Could not locate a XOM parser", ex);
84
public void doPost(HttpServletRequest servletRequest,
85
HttpServletResponse servletResponse)
86
throws ServletException, IOException {
88
servletResponse.setContentType("application/soap+xml; charset=UTF-8");
89
OutputStreamWriter out = new OutputStreamWriter(
90
servletResponse.getOutputStream(), "UTF-8");
91
InputStream in = servletRequest.getInputStream();
95
String generations ="here";
97
request = parser.build(in);
99
/* <?xml version="1.0"?>
101
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
102
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
105
xmlns="http://namespaces.cafeconleche.org/xmljava/ch3/"
106
type="xsi:positiveInteger">10</calculateFibonacci>
108
</SOAP-ENV:Envelope> */
110
generations = request.getValue().trim();
111
int numberOfGenerations = Integer.parseInt(generations);
112
BigInteger result = calculateFibonacci(numberOfGenerations);
113
response = makeResponseDocument(result);
115
catch (WellformednessException ex) {
116
response = makeFaultDocument(MALFORMED_REQUEST_DOCUMENT,
119
catch (NullPointerException ex) {
120
response = makeFaultDocument(INDEX_MISSING,
123
catch (NumberFormatException ex) {
124
response = makeFaultDocument(BAD_INTEGER_FORMAT,
125
generations + ex.getMessage());
127
catch (IndexOutOfBoundsException ex) {
128
response = makeFaultDocument(NON_POSITIVE_INDEX,
131
catch (Exception ex) {
132
response = makeFaultDocument(UNEXPECTED_PROBLEM,
136
// Transform onto the OutputStream
138
out.write(response.toXML());
139
servletResponse.flushBuffer();
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);
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) {
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);
166
Element Fibonacci_Numbers = new Element("Fibonacci_Numbers",
167
"http://namespaces.cafeconleche.org/xmljava/ch3/");
168
body.appendChild(Fibonacci_Numbers);
170
Element fibonacci = new Element("fibonacci",
171
"http://namespaces.cafeconleche.org/xmljava/ch3/");
172
Fibonacci_Numbers.appendChild(fibonacci);
173
fibonacci.appendChild(result.toString());
180
public Document makeFaultDocument(String code, String message){
182
Element envelope = new Element("SOAP-ENV:Envelope",
183
"http://schemas.xmlsoap.org/soap/envelope/");
184
Document faultDoc = new Document(envelope);
186
Element body = new Element("SOAP-ENV:Body",
187
"http://schemas.xmlsoap.org/soap/envelope/");
188
envelope.appendChild(body);
190
Element fault = new Element("Fault",
191
"http://schemas.xmlsoap.org/soap/envelope/");
192
body.appendChild(fault);
194
Element faultCode = new Element("faultcode");
195
fault.appendChild(faultCode);
197
Element faultString = new Element("faultstring");
198
fault.appendChild(faultString);
200
faultCode.appendChild(code);
202
faultString.appendChild(message);
209
public static BigInteger calculateFibonacci(int generations)
210
throws IndexOutOfBoundsException {
212
if (generations < 1) {
213
throw new IndexOutOfBoundsException(
214
"Fibonacci numbers are not defined for " + generations
215
+ "or any other number less than one.");
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);