KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nu > xom > samples > FibonacciXOMXMLRPCServlet


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 JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.OutputStream JavaDoc;
27 import java.math.BigInteger JavaDoc;
28
29 import javax.servlet.ServletException JavaDoc;
30 import javax.servlet.SingleThreadModel JavaDoc;
31 import javax.servlet.http.HttpServlet JavaDoc;
32 import javax.servlet.http.HttpServletRequest JavaDoc;
33 import javax.servlet.http.HttpServletResponse JavaDoc;
34
35 import nu.xom.Builder;
36 import nu.xom.Document;
37 import nu.xom.Element;
38 import nu.xom.Serializer;
39 import nu.xom.XMLException;
40
41 /**
42  * <p>
43  * Demonstrates a servlet that receives and
44  * responds to XML-RPC requests.
45  * </p>
46  *
47  * @author Elliotte Rusty Harold
48  * @version 1.0
49  *
50  */

51 public class FibonacciXOMXMLRPCServlet extends HttpServlet JavaDoc
52  implements SingleThreadModel JavaDoc {
53
54   // Fault codes
55
public final static int MALFORMED_REQUEST_DOCUMENT = 1;
56   public final static int INVALID_REQUEST_DOCUMENT = 2;
57   public final static int INDEX_MISSING = 3;
58   public final static int NON_POSITIVE_INDEX = 4;
59   public final static int BAD_INTEGER_FORMAT = 5;
60   public final static int UNEXPECTED_PROBLEM = 255;
61    
62   private transient Builder parser;
63   
64   // Load a parser, transformer, and implementation
65
public void init() throws ServletException JavaDoc {
66   
67     try {
68       this.parser = new Builder();
69     }
70     catch (Exception JavaDoc ex) {
71       throw new ServletException JavaDoc(
72        "Could not locate a JAXP parser", ex);
73     }
74     
75   }
76   
77   // Respond to an XML-RPC request
78
public void doPost(HttpServletRequest JavaDoc servletRequest,
79    HttpServletResponse JavaDoc servletResponse)
80    throws ServletException JavaDoc, IOException JavaDoc {
81     
82     servletResponse.setContentType("application/xml; charset=UTF-8");
83     OutputStream JavaDoc out = servletResponse.getOutputStream();
84     InputStream JavaDoc in = servletRequest.getInputStream();
85
86     Document request;
87     Document response;
88     try {
89         request = parser.build(in);
90         Element methodCall = request.getRootElement();
91         Element params = methodCall.getFirstChildElement("params");
92         String JavaDoc generations = params.getValue().trim();
93         int numberOfGenerations = Integer.parseInt(generations);
94         BigInteger JavaDoc result = calculateFibonacci(numberOfGenerations);
95         response = makeResponseDocument(result);
96     }
97     catch (XMLException ex) {
98       response = makeFaultDocument(MALFORMED_REQUEST_DOCUMENT, ex.getMessage());
99     }
100     catch (NullPointerException JavaDoc ex) {
101       response = makeFaultDocument(INDEX_MISSING, ex.getMessage());
102     }
103     catch (NumberFormatException JavaDoc ex) {
104       response = makeFaultDocument(BAD_INTEGER_FORMAT, ex.getMessage());
105     }
106     catch (Exception JavaDoc ex) {
107       response = makeFaultDocument(UNEXPECTED_PROBLEM, ex.getMessage());
108     }
109     
110     // Transform onto the OutputStream
111
try {
112       Serializer output = new Serializer(out, "US-ASCII");
113       output.write(response);
114       servletResponse.flushBuffer();
115       out.flush();
116     }
117     catch (Exception JavaDoc ex) {
118       // If we get an exception at this point, it's too late to
119
// switch over to an XML-RPC fault.
120
throw new ServletException JavaDoc(ex);
121     }
122     
123   }
124   
125   // If performance is an issue, this could be pre-built in the
126
// init() method and then cached. You'd just change one text
127
// node each time. This would only work in a SingleThreadModel
128
// servlet.
129
public Document makeResponseDocument(BigInteger JavaDoc result) {
130     
131     Element methodResponse = new Element("methodResponse");
132     Element params = new Element("params");
133     Element param = new Element("param");
134     Element value = new Element("value");
135     Element doubleElement = new Element("double");
136  
137     methodResponse.appendChild(params);
138     params.appendChild(param);
139     param.appendChild(value);
140     value.appendChild(doubleElement);
141     doubleElement.appendChild(result.toString());
142
143     return new Document(methodResponse);
144    
145   }
146   
147   public Document makeFaultDocument(int faultCode, String JavaDoc faultString) {
148     
149     Element methodResponse = new Element("methodResponse");
150     
151     Element fault = new Element("fault");
152     Element value = new Element("value");
153     Element struct = new Element("struct");
154     Element memberCode = new Element("member");
155     Element nameCode = new Element("name");
156     Element valueCode = new Element("value");
157     Element intCode = new Element("int");
158     Element memberString = new Element("member");
159     Element valueString = new Element("value");
160     Element stringString = new Element("string");
161
162     methodResponse.appendChild(fault);
163     fault.appendChild(value);
164     value.appendChild(struct);
165     struct.appendChild(memberCode);
166     struct.appendChild(memberString);
167     memberCode.appendChild(nameCode);
168     memberCode.appendChild(valueCode);
169     memberString.appendChild("name");
170     memberString.appendChild(valueString);
171     nameCode.appendChild("faultCode");
172     valueCode.appendChild(intCode);
173     valueString.appendChild(stringString);
174     intCode.appendChild(String.valueOf(faultCode));
175     stringString.appendChild(faultString);
176
177     Document faultDoc = new Document(methodResponse);
178     
179     return faultDoc;
180        
181   }
182   
183   public static BigInteger JavaDoc calculateFibonacci(int generations)
184    throws IndexOutOfBoundsException JavaDoc {
185     
186     if (generations < 1) {
187       throw new IndexOutOfBoundsException JavaDoc(
188        "Fibonacci numbers are not defined for " + generations
189        + "or any other number less than one.");
190     }
191     BigInteger JavaDoc low = BigInteger.ONE;
192     BigInteger JavaDoc high = BigInteger.ONE;
193     for (int i = 2; i <= generations; i++) {
194       BigInteger JavaDoc temp = high;
195       high = high.add(low);
196       low = temp;
197     }
198     return low;
199         
200   }
201
202 }
203
Popular Tags