1 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 51 public class FibonacciXOMSOAPServlet extends HttpServlet 52 implements SingleThreadModel { 53 54 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 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 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 try { 138 out.write(response.toXML()); 139 servletResponse.flushBuffer(); 140 out.flush(); 141 } 142 catch (IOException ex) { 143 throw new ServletException (ex); 146 } 147 finally { 148 in.close(); 149 out.close(); 150 } 151 152 } 153 154 155 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 } 229 | Popular Tags |