| 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; |
| 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 " + stockQuote.getStockQuote("SUNW")); |
| 36 |
} |
| 37 |
|
| 38 |
public String getStockQuote(String tickerSymbol) throws Exception { |
| 39 |
SOAPConnectionFactory scFactory = SOAPConnectionFactory.newInstance(); |
| 40 |
SOAPConnection con = scFactory.createConnection(); |
| 41 |
|
| 42 |
MessageFactory factory = MessageFactory.newInstance(); |
| 43 |
SOAPMessage message = factory.createMessage(); |
| 44 |
|
| 45 |
SOAPPart soapPart = message.getSOAPPart(); |
| 46 |
SOAPEnvelope envelope = soapPart.getEnvelope(); |
| 47 |
|
| 48 |
SOAPHeader header = envelope.getHeader(); |
| 49 |
SOAPBody body = envelope.getBody(); |
| 50 |
|
| 51 |
header.detachNode(); |
| 52 |
|
| 53 |
Name bodyName = envelope.createName("getQuote", "n", "urn:xmethods-delayed-quotes"); |
| 54 |
SOAPBodyElement gltp = body.addBodyElement(bodyName); |
| 55 |
|
| 56 |
Name name = envelope.createName("symbol"); |
| 57 |
SOAPElement symbol = gltp.addChildElement(name); |
| 58 |
symbol.addTextNode(tickerSymbol); |
| 59 |
|
| 60 |
URLEndpoint endpoint = new URLEndpoint("http://64.124.140.30/soap"); |
| 61 |
SOAPMessage response = con.call(message, endpoint); |
| 62 |
con.close(); |
| 63 |
|
| 64 |
SOAPPart sp = response.getSOAPPart(); |
| 65 |
SOAPEnvelope se = sp.getEnvelope(); |
| 66 |
SOAPBody sb = se.getBody(); |
| 67 |
Iterator it = sb.getChildElements(); |
| 68 |
while (it.hasNext()) { |
| 69 |
SOAPBodyElement bodyElement = (SOAPBodyElement) it.next(); |
| 70 |
Iterator it2 = bodyElement.getChildElements(); |
| 71 |
while (it2.hasNext()) { |
| 72 |
SOAPElement element2 = (SOAPElement) it2.next(); |
| 73 |
return element2.getValue(); |
| 74 |
} |
| 75 |
} |
| 76 |
return null; |
| 77 |
} |
| 78 |
} |
| 79 |
|