KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > samples > jaxm > DelayedStockQuote


1 /*
2  * Copyright 2002-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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 JavaDoc;
20 import javax.xml.soap.Name JavaDoc;
21 import javax.xml.soap.SOAPBody JavaDoc;
22 import javax.xml.soap.SOAPBodyElement JavaDoc;
23 import javax.xml.soap.SOAPConnection JavaDoc;
24 import javax.xml.soap.SOAPConnectionFactory JavaDoc;
25 import javax.xml.soap.SOAPElement JavaDoc;
26 import javax.xml.soap.SOAPEnvelope JavaDoc;
27 import javax.xml.soap.SOAPHeader JavaDoc;
28 import javax.xml.soap.SOAPMessage JavaDoc;
29 import javax.xml.soap.SOAPPart JavaDoc;
30 import java.util.Iterator JavaDoc;
31
32 public class DelayedStockQuote {
33     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
34         DelayedStockQuote stockQuote = new DelayedStockQuote();
35         System.out.print("The last price for SUNW is " + stockQuote.getStockQuote("SUNW"));
36     }
37
38     public String JavaDoc getStockQuote(String JavaDoc tickerSymbol) throws Exception JavaDoc {
39         SOAPConnectionFactory JavaDoc scFactory = SOAPConnectionFactory.newInstance();
40         SOAPConnection JavaDoc con = scFactory.createConnection();
41
42         MessageFactory JavaDoc factory = MessageFactory.newInstance();
43         SOAPMessage JavaDoc message = factory.createMessage();
44
45         SOAPPart JavaDoc soapPart = message.getSOAPPart();
46         SOAPEnvelope JavaDoc envelope = soapPart.getEnvelope();
47
48         SOAPHeader JavaDoc header = envelope.getHeader();
49         SOAPBody JavaDoc body = envelope.getBody();
50
51         header.detachNode();
52
53         Name JavaDoc bodyName = envelope.createName("getQuote", "n", "urn:xmethods-delayed-quotes");
54         SOAPBodyElement JavaDoc gltp = body.addBodyElement(bodyName);
55
56         Name JavaDoc name = envelope.createName("symbol");
57         SOAPElement JavaDoc symbol = gltp.addChildElement(name);
58         symbol.addTextNode(tickerSymbol);
59
60         URLEndpoint endpoint = new URLEndpoint("http://64.124.140.30/soap");
61         SOAPMessage JavaDoc response = con.call(message, endpoint);
62         con.close();
63
64         SOAPPart JavaDoc sp = response.getSOAPPart();
65         SOAPEnvelope JavaDoc se = sp.getEnvelope();
66         SOAPBody JavaDoc sb = se.getBody();
67         Iterator JavaDoc it = sb.getChildElements();
68         while (it.hasNext()) {
69             SOAPBodyElement JavaDoc bodyElement = (SOAPBodyElement JavaDoc) it.next();
70             Iterator JavaDoc it2 = bodyElement.getChildElements();
71             while (it2.hasNext()) {
72                 SOAPElement JavaDoc element2 = (SOAPElement JavaDoc) it2.next();
73                 return element2.getValue();
74             }
75         }
76         return null;
77     }
78 }
79
80
Popular Tags