01: /*
02: * Copyright 2002-2004 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package samples.jaxm;
17:
18: import javax.xml.messaging.URLEndpoint;
19: import javax.xml.soap.MessageFactory;
20: import javax.xml.soap.Name;
21: import javax.xml.soap.SOAPBody;
22: import javax.xml.soap.SOAPBodyElement;
23: import javax.xml.soap.SOAPConnection;
24: import javax.xml.soap.SOAPConnectionFactory;
25: import javax.xml.soap.SOAPElement;
26: import javax.xml.soap.SOAPEnvelope;
27: import javax.xml.soap.SOAPHeader;
28: import javax.xml.soap.SOAPMessage;
29: import javax.xml.soap.SOAPPart;
30: import java.util.Iterator;
31:
32: public class DelayedStockQuote {
33: public static void main(String[] args) throws Exception {
34: DelayedStockQuote stockQuote = new DelayedStockQuote();
35: System.out.print("The last price for SUNW is "
36: + stockQuote.getStockQuote("SUNW"));
37: }
38:
39: public String getStockQuote(String tickerSymbol) throws Exception {
40: SOAPConnectionFactory scFactory = SOAPConnectionFactory
41: .newInstance();
42: SOAPConnection con = scFactory.createConnection();
43:
44: MessageFactory factory = MessageFactory.newInstance();
45: SOAPMessage message = factory.createMessage();
46:
47: SOAPPart soapPart = message.getSOAPPart();
48: SOAPEnvelope envelope = soapPart.getEnvelope();
49:
50: SOAPHeader header = envelope.getHeader();
51: SOAPBody body = envelope.getBody();
52:
53: header.detachNode();
54:
55: Name bodyName = envelope.createName("getQuote", "n",
56: "urn:xmethods-delayed-quotes");
57: SOAPBodyElement gltp = body.addBodyElement(bodyName);
58:
59: Name name = envelope.createName("symbol");
60: SOAPElement symbol = gltp.addChildElement(name);
61: symbol.addTextNode(tickerSymbol);
62:
63: URLEndpoint endpoint = new URLEndpoint(
64: "http://64.124.140.30/soap");
65: SOAPMessage response = con.call(message, endpoint);
66: con.close();
67:
68: SOAPPart sp = response.getSOAPPart();
69: SOAPEnvelope se = sp.getEnvelope();
70: SOAPBody sb = se.getBody();
71: Iterator it = sb.getChildElements();
72: while (it.hasNext()) {
73: SOAPBodyElement bodyElement = (SOAPBodyElement) it.next();
74: Iterator it2 = bodyElement.getChildElements();
75: while (it2.hasNext()) {
76: SOAPElement element2 = (SOAPElement) it2.next();
77: return element2.getValue();
78: }
79: }
80: return null;
81: }
82: }
|