1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 20 package samples.common; 21 22 import org.apache.axiom.om.OMAbstractFactory; 23 import org.apache.axiom.om.OMElement; 24 import org.apache.axiom.om.OMFactory; 25 import org.apache.axiom.om.OMNamespace; 26 import org.apache.axiom.om.xpath.AXIOMXPath; 27 28 import javax.xml.namespace.QName; 29 import java.util.Random; 30 import java.util.List; 31 import java.util.Iterator; 32 33 /** 34 * A class that can create messages to, and parse replies from our sample StockQuote service 35 */ 36 public class StockQuoteHandler { 37 38 private static final Random RANDOM = new Random(); 39 40 /** 41 * Create a new custom quote request with a body as follows 42 * <m0:CheckPriceRequest xmlns:m0="http://services.samples/xsd"> 43 * <m0:Code>symbol</m0:Code> 44 * </m0:CheckPriceRequest> 45 * @param symbol the stock symbol 46 * @return OMElement for SOAP body 47 */ 48 public static OMElement createCustomQuoteRequest(String symbol) { 49 OMFactory factory = OMAbstractFactory.getOMFactory(); 50 OMNamespace ns = factory.createOMNamespace( 51 "http://services.samples/xsd", "m0"); 52 OMElement chkPrice = factory.createOMElement("CheckPriceRequest", ns); 53 OMElement code = factory.createOMElement("Code", ns); 54 chkPrice.addChild(code); 55 code.setText(symbol); 56 return chkPrice; 57 } 58 59 /** 60 * Create a new quote request with a body as follows 61 * <m:GetQuote xmlns:m="http://services.samples/xsd"> 62 * <m:request> 63 * <m:symbol>IBM</m:symbol> 64 * </m:request> 65 * </m:GetQuote> 66 * @param symbol the stock symbol 67 * @return OMElement for SOAP body 68 */ 69 public static OMElement createStandardQuoteRequest(String symbol, int itrCount) { 70 OMFactory factory = OMAbstractFactory.getOMFactory(); 71 OMNamespace ns = factory.createOMNamespace("http://services.samples/xsd", "m0"); 72 OMElement getQuote = factory.createOMElement("getQuote", ns); 73 for (int i =0; i<itrCount; i++) { 74 OMElement request = factory.createOMElement("request", ns); 75 OMElement symb = factory.createOMElement("symbol", ns); 76 request.addChild(symb); 77 getQuote.addChild(request); 78 symb.setText(symbol); 79 } 80 return getQuote; 81 } 82 83 /** 84 * Create a new full quote request with a body as follows 85 * <m:GetFullQuote xmlns:m="http://services.samples/xsd"> 86 * <m:request> 87 * <m:symbol>IBM</m:symbol> 88 * </m:request> 89 * </m:GetFullQuote> 90 * @param symbol the stock symbol 91 * @return OMElement for SOAP body 92 */ 93 public static OMElement createFullQuoteRequest(String symbol) { 94 OMFactory factory = OMAbstractFactory.getOMFactory(); 95 OMNamespace ns = factory.createOMNamespace("http://services.samples/xsd", "m0"); 96 OMElement getQuote = factory.createOMElement("getFullQuote", ns); 97 OMElement request = factory.createOMElement("request", ns); 98 OMElement symb = factory.createOMElement("symbol", ns); 99 request.addChild(symb); 100 getQuote.addChild(request); 101 symb.setText(symbol); 102 return getQuote; 103 } 104 105 /** 106 * Create a new market activity request with a body as follows 107 * <m:getMarketActivity xmlns:m="http://services.samples/xsd"> 108 * <m:request> 109 * <m:symbol>IBM</m:symbol> 110 * ... 111 * <m:symbol>MSFT</m:symbol> 112 * </m:request> 113 * </m:getMarketActivity> 114 * @return OMElement for SOAP body 115 */ 116 public static OMElement createMarketActivityRequest() { 117 OMFactory factory = OMAbstractFactory.getOMFactory(); 118 OMNamespace ns = factory.createOMNamespace("http://services.samples/xsd", "m0"); 119 OMElement getQuote = factory.createOMElement("getMarketActivity", ns); 120 OMElement request = factory.createOMElement("request", ns); 121 122 OMElement symb = null; 123 for (int i=0; i<100; i++) { 124 symb = factory.createOMElement("symbols", ns); 125 symb.setText(randomString(3)); 126 request.addChild(symb); 127 } 128 129 getQuote.addChild(request); 130 return getQuote; 131 } 132 133 /** 134 * Create a new order for a quantiry of a stock at a given price 135 * <m:placeOrder xmlns:m="http://services.samples/xsd"> 136 * <m:order> 137 * <m:price>3.141593E0</m:price> 138 * <m:quantity>4</m:quantity> 139 * <m:symbol>IBM</m:symbol> 140 * </m:order> 141 * </m:placeOrder> 142 * 143 * @param purchPrice the purchase price 144 * @param qty the quantiry 145 * @param symbol the stock 146 * @return an OMElement payload for the order 147 */ 148 public static OMElement createPlaceOrderRequest(double purchPrice, int qty, String symbol) { 149 OMFactory factory = OMAbstractFactory.getOMFactory(); 150 OMNamespace ns = factory.createOMNamespace("http://services.samples/xsd", "m0"); 151 OMElement placeOrder= factory.createOMElement("placeOrder", ns); 152 OMElement order = factory.createOMElement("order", ns); 153 OMElement price = factory.createOMElement("price", ns); 154 OMElement quantity = factory.createOMElement("quantity", ns); 155 OMElement symb = factory.createOMElement("symbol", ns); 156 price.setText(Double.toString(purchPrice)); 157 quantity.setText(Integer.toString(qty)); 158 symb.setText(symbol); 159 order.addChild(price); 160 order.addChild(quantity); 161 order.addChild(symb); 162 placeOrder.addChild(order); 163 return placeOrder; 164 } 165 166 /** 167 * Digests the standard StockQuote response and extracts the last trade price 168 * @param result 169 * @return 170 * @throws javax.xml.stream.XMLStreamException 171 * 172 * <ns:getQuoteResponse xmlns:ns="http://services.samples/xsd"> 173 * <ns:return> 174 * <ns:change>-2.3238706829151026</ns:change> 175 * ... 176 * <ns:symbol>IBM</ns:symbol> 177 * <ns:volume>17949</ns:volume> 178 * </ns:return> 179 * </ns:getQuoteResponse> 180 */ 181 public static String parseStandardQuoteResponse(OMElement result) throws Exception { 182 183 AXIOMXPath xPath = new AXIOMXPath("//ns:last"); 184 xPath.addNamespace("ns","http://services.samples/xsd"); 185 OMElement last = (OMElement) xPath.selectSingleNode(result); 186 if (last != null) { 187 return last.getText(); 188 } else { 189 throw new Exception("Unexpected response : " + result); 190 } 191 } 192 193 /** 194 * <ns:getFullQuoteResponse xmlns:ns="http://services.samples/xsd"> 195 <ns:return> 196 <tradeHistory xmlns="http://services.samples/xsd"> 197 <day>0</day> 198 <quote> 199 <change>-2.367492989603466</change> 200 <earnings>13.14956711287784</earnings> 201 <high>-155.58844623078153</high> 202 <last>157.47582716569198</last> 203 <lastTradeTimestamp>Mon Apr 16 23:29:58 LKT 2007</lastTradeTimestamp> 204 <low>-155.31924118819015</low> 205 <marketCap>6373750.467022192</marketCap> 206 <name>IBM Company</name> 207 <open>-154.84071720443495</open> 208 <peRatio>-17.353258031353164</peRatio> 209 <percentageChange>-1.3910235348298898</percentageChange> 210 <prevClose>170.1979104108393</prevClose> 211 <symbol>IBM</symbol> 212 <volume>8935</volume> 213 </quote> 214 </tradeHistory> 215 <tradeHistory xmlns="http://services.samples/xsd"> 216 <day>1</day> 217 <quote> 218 <change>3.794122022240518</change> 219 <earnings>-8.656536789776045</earnings> 220 <high>176.77136802352928</high> 221 <last>170.28677783945102</last> 222 <lastTradeTimestamp>Mon Apr 16 23:29:58 LKT 2007</lastTradeTimestamp> 223 <low>-166.64126635049223</low> 224 <marketCap>-6112014.916847887</marketCap> 225 <name>IBM Company</name> 226 <open>-168.30884678174925</open> 227 <peRatio>-18.644628475049693</peRatio> 228 <percentageChange>-2.29678289479374</percentageChange> 229 <prevClose>-165.19288918603885</prevClose> 230 <symbol>IBM</symbol> 231 <volume>5825</volume> 232 </quote> 233 </tradeHistory> 234 ... 235 </ns:return> 236 </ns:getFullQuoteResponse> 237 * 238 * @param result 239 * @return 240 * @throws Exception 241 */ 242 public static String parseFullQuoteResponse(OMElement result) throws Exception { 243 244 AXIOMXPath xPath = new AXIOMXPath("//ns:last"); 245 xPath.addNamespace("ns","http://services.samples/xsd"); 246 List lastNodes = xPath.selectNodes(result); 247 248 if (lastNodes == null) { 249 throw new Exception("Unexpected response : " + result); 250 } 251 252 double total = 0; 253 int count = 0; 254 255 Iterator iter = lastNodes.iterator(); 256 while (iter.hasNext()) { 257 OMElement last = (OMElement) iter.next(); 258 total += Double.parseDouble(last.getText()); 259 count++; 260 } 261 262 return Double.toString(total/count); 263 } 264 265 /** 266 * <ns:getMarketActivityResponse xmlns:ns="http://services.samples/xsd"> 267 <ns:return> 268 <quotes xmlns="http://services.samples/xsd"> 269 <change>4.183958555301184</change> 270 <earnings>-8.585281368244686</earnings> 271 <high>-158.70528805517333</high> 272 <last>160.83784480071603</last> 273 <lastTradeTimestamp>Tue Apr 17 02:21:30 LKT 2007</lastTradeTimestamp> 274 <low>-157.4950051860593</low> 275 <marketCap>5.9907588733164035E7</marketCap> 276 <name>EHM Company</name> 277 <open>-160.18368223376558</open> 278 <peRatio>24.0926205053427</peRatio> 279 <percentageChange>-2.6141745708181374</percentageChange> 280 <prevClose>-160.04893483420904</prevClose> 281 <symbol>EHM</symbol> 282 <volume>6319</volume> 283 </quotes> 284 <quotes xmlns="http://services.samples/xsd"> 285 .... 286 <volume>7613</volume> 287 </quotes> 288 ... 289 </ns:return> 290 <ns:getMarketActivityResponse> 291 * @param result 292 * @return the average last price for each stock symbol 293 * @throws Exception 294 */ 295 public static String parseMarketActivityResponse(OMElement result) throws Exception { 296 297 AXIOMXPath xPath = new AXIOMXPath("//ns:last"); 298 xPath.addNamespace("ns","http://services.samples/xsd"); 299 List lastNodes = xPath.selectNodes(result); 300 301 if (lastNodes == null) { 302 throw new Exception("Unexpected response : " + result); 303 } 304 305 double total = 0; 306 int count = 0; 307 308 Iterator iter = lastNodes.iterator(); 309 while (iter.hasNext()) { 310 OMElement last = (OMElement) iter.next(); 311 total += Double.parseDouble(last.getText()); 312 count++; 313 } 314 315 return Double.toString(total/count); 316 } 317 318 /** 319 * Digests the custom quote response and extracts the last trade price 320 * @param result 321 * @return 322 * @throws javax.xml.stream.XMLStreamException 323 * 324 * <CheckPriceResponse xmlns="http://ws.invesbot.com/" > 325 * <Code>IBM</Code> 326 * <Price>82.90</Price> 327 * </CheckPriceResponse> 328 */ 329 public static String parseCustomQuoteResponse(OMElement result) throws Exception { 330 331 AXIOMXPath xPath = new AXIOMXPath("//ns:Price"); 332 xPath.addNamespace("ns","http://services.samples/xsd"); 333 OMElement price = (OMElement) xPath.selectSingleNode(result); 334 if (price != null) { 335 return price.getText(); 336 } else { 337 throw new Exception("Unexpected response : " + result); 338 } 339 } 340 341 /** 342 * Return a random String of letters 343 * @param count number of letters 344 * @return the random string 345 */ 346 public static String randomString(int count) { 347 int end = 'Z' + 1; 348 int start = 'A'; 349 350 StringBuffer buffer = new StringBuffer(); 351 int gap = end - start; 352 353 while (count-- != 0) { 354 char ch; 355 ch = (char) (RANDOM.nextInt(gap) + start); 356 if (Character.isLetter(ch)) { 357 buffer.append(ch); 358 } else { 359 count++; 360 } 361 } 362 return buffer.toString(); 363 } 364 365 }