
samples.common.StockQuoteHandler Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package samples.common;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.om.xpath.AXIOMXPath;
import javax.xml.namespace.QName;
import java.util.Random;
import java.util.List;
import java.util.Iterator;
/**
* A class that can create messages to, and parse replies from our sample StockQuote service
*/
public class StockQuoteHandler {
private static final Random RANDOM = new Random();
/**
* Create a new custom quote request with a body as follows
*
* symbol
*
* @param symbol the stock symbol
* @return OMElement for SOAP body
*/
public static OMElement createCustomQuoteRequest(String symbol) {
OMFactory factory = OMAbstractFactory.getOMFactory();
OMNamespace ns = factory.createOMNamespace(
"http://services.samples", "m0");
OMElement chkPrice = factory.createOMElement("CheckPriceRequest", ns);
OMElement code = factory.createOMElement("Code", ns);
chkPrice.addChild(code);
code.setText(symbol);
return chkPrice;
}
/**
* Create a new quote request with a body as follows
*
*
* IBM
*
*
* @param symbol the stock symbol
* @return OMElement for SOAP body
*/
public static OMElement createStandardQuoteRequest(String symbol, int itrCount) {
OMFactory factory = OMAbstractFactory.getOMFactory();
OMNamespace ns = factory.createOMNamespace("http://services.samples", "m0");
OMElement getQuote = factory.createOMElement("getQuote", ns);
for (int i =0; i
*
* IBM
*
*
* @param symbol the stock symbol
* @return OMElement for SOAP body
*/
public static OMElement createFullQuoteRequest(String symbol) {
OMFactory factory = OMAbstractFactory.getOMFactory();
OMNamespace ns = factory.createOMNamespace("http://services.samples", "m0");
OMElement getQuote = factory.createOMElement("getFullQuote", ns);
OMElement request = factory.createOMElement("request", ns);
OMElement symb = factory.createOMElement("symbol", ns);
request.addChild(symb);
getQuote.addChild(request);
symb.setText(symbol);
return getQuote;
}
/**
* Create a new market activity request with a body as follows
*
*
* IBM
* ...
* MSFT
*
*
* @return OMElement for SOAP body
*/
public static OMElement createMarketActivityRequest() {
OMFactory factory = OMAbstractFactory.getOMFactory();
OMNamespace ns = factory.createOMNamespace("http://services.samples", "m0");
OMElement getQuote = factory.createOMElement("getMarketActivity", ns);
OMElement request = factory.createOMElement("request", ns);
OMElement symb = null;
for (int i=0; i<100; i++) {
symb = factory.createOMElement("symbols", ns);
symb.setText(randomString(3));
request.addChild(symb);
}
getQuote.addChild(request);
return getQuote;
}
/**
* Create a new order for a quantiry of a stock at a given price
*
*
* 3.141593E0
* 4
* IBM
*
*
*
* @param purchPrice the purchase price
* @param qty the quantiry
* @param symbol the stock
* @return an OMElement payload for the order
*/
public static OMElement createPlaceOrderRequest(double purchPrice, int qty, String symbol) {
OMFactory factory = OMAbstractFactory.getOMFactory();
OMNamespace ns = factory.createOMNamespace("http://services.samples", "m0");
OMElement placeOrder= factory.createOMElement("placeOrder", ns);
OMElement order = factory.createOMElement("order", ns);
OMElement price = factory.createOMElement("price", ns);
OMElement quantity = factory.createOMElement("quantity", ns);
OMElement symb = factory.createOMElement("symbol", ns);
price.setText(Double.toString(purchPrice));
quantity.setText(Integer.toString(qty));
symb.setText(symbol);
order.addChild(price);
order.addChild(quantity);
order.addChild(symb);
placeOrder.addChild(order);
return placeOrder;
}
/**
* Digests the standard StockQuote response and extracts the last trade price
* @param result
* @return
* @throws javax.xml.stream.XMLStreamException
*
*
*
* -2.3238706829151026
* ...
* IBM
* 17949
*
*
*/
public static String parseStandardQuoteResponse(OMElement result) throws Exception {
AXIOMXPath xPath = new AXIOMXPath("//ns:last");
xPath.addNamespace("ns","http://services.samples");
OMElement last = (OMElement) xPath.selectSingleNode(result);
if (last != null) {
return last.getText();
} else {
throw new Exception("Unexpected response : " + result);
}
}
/**
*
0
-2.367492989603466
13.14956711287784
-155.58844623078153
157.47582716569198
Mon Apr 16 23:29:58 LKT 2007
-155.31924118819015
6373750.467022192
IBM Company
-154.84071720443495
-17.353258031353164
-1.3910235348298898
170.1979104108393
IBM
8935
1
3.794122022240518
-8.656536789776045
176.77136802352928
170.28677783945102
Mon Apr 16 23:29:58 LKT 2007
-166.64126635049223
-6112014.916847887
IBM Company
-168.30884678174925
-18.644628475049693
-2.29678289479374
-165.19288918603885
IBM
5825
...
*
* @param result
* @return
* @throws Exception
*/
public static String parseFullQuoteResponse(OMElement result) throws Exception {
AXIOMXPath xPath = new AXIOMXPath("//ns:last");
xPath.addNamespace("ns","http://services.samples/xsd");
List lastNodes = xPath.selectNodes(result);
if (lastNodes == null) {
throw new Exception("Unexpected response : " + result);
}
double total = 0;
int count = 0;
Iterator iter = lastNodes.iterator();
while (iter.hasNext()) {
OMElement last = (OMElement) iter.next();
total += Double.parseDouble(last.getText());
count++;
}
return Double.toString(total/count);
}
/**
*
4.183958555301184
-8.585281368244686
-158.70528805517333
160.83784480071603
Tue Apr 17 02:21:30 LKT 2007
-157.4950051860593
5.9907588733164035E7
EHM Company
-160.18368223376558
24.0926205053427
-2.6141745708181374
-160.04893483420904
EHM
6319
....
7613
...
* @param result
* @return the average last price for each stock symbol
* @throws Exception
*/
public static String parseMarketActivityResponse(OMElement result) throws Exception {
AXIOMXPath xPath = new AXIOMXPath("//ns:last");
xPath.addNamespace("ns","http://services.samples/xsd");
List lastNodes = xPath.selectNodes(result);
if (lastNodes == null) {
throw new Exception("Unexpected response : " + result);
}
double total = 0;
int count = 0;
Iterator iter = lastNodes.iterator();
while (iter.hasNext()) {
OMElement last = (OMElement) iter.next();
total += Double.parseDouble(last.getText());
count++;
}
return Double.toString(total/count);
}
/**
* Digests the custom quote response and extracts the last trade price
* @param result
* @return
* @throws javax.xml.stream.XMLStreamException
*
*
* IBM
* 82.90
*
*/
public static String parseCustomQuoteResponse(OMElement result) throws Exception {
AXIOMXPath xPath = new AXIOMXPath("//ns:Price");
xPath.addNamespace("ns","http://services.samples/xsd");
OMElement price = (OMElement) xPath.selectSingleNode(result);
if (price != null) {
return price.getText();
} else {
throw new Exception("Unexpected response : " + result);
}
}
/**
* Return a random String of letters
* @param count number of letters
* @return the random string
*/
public static String randomString(int count) {
int end = 'Z' + 1;
int start = 'A';
StringBuffer buffer = new StringBuffer();
int gap = end - start;
while (count-- != 0) {
char ch;
ch = (char) (RANDOM.nextInt(gap) + start);
if (Character.isLetter(ch)) {
buffer.append(ch);
} else {
count++;
}
}
return buffer.toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy