001: /*
002: * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved. U.S.
003: * Government Rights - Commercial software. Government users are subject
004: * to the Sun Microsystems, Inc. standard license agreement and
005: * applicable provisions of the FAR and its supplements. Use is subject
006: * to license terms.
007: *
008: * This distribution may include materials developed by third parties.
009: * Sun, Sun Microsystems, the Sun logo, Java and J2EE are trademarks
010: * or registered trademarks of Sun Microsystems, Inc. in the U.S. and
011: * other countries.
012: *
013: * Copyright (c) 2005 Sun Microsystems, Inc. Tous droits reserves.
014: *
015: * Droits du gouvernement americain, utilisateurs gouvernementaux - logiciel
016: * commercial. Les utilisateurs gouvernementaux sont soumis au contrat de
017: * licence standard de Sun Microsystems, Inc., ainsi qu'aux dispositions
018: * en vigueur de la FAR (Federal Acquisition Regulations) et des
019: * supplements a celles-ci. Distribue par des licences qui en
020: * restreignent l'utilisation.
021: *
022: * Cette distribution peut comprendre des composants developpes par des
023: * tierces parties. Sun, Sun Microsystems, le logo Sun, Java et J2EE
024: * sont des marques de fabrique ou des marques deposees de Sun
025: * Microsystems, Inc. aux Etats-Unis et dans d'autres pays.
026: */
027:
028: package mypackage;
029:
030: import javax.xml.soap.*;
031: import java.net.*;
032: import java.util.*;
033: import java.io.*;
034:
035: public class UddiPing {
036: public static void main(String[] args) {
037: try {
038: // two arguments are passed in from build.xml
039: if (args.length != 2) {
040: System.err.println("Usage: asant run "
041: + "-Dbusiness-name=<name>");
042: System.exit(1);
043: }
044:
045: // Retrieve settings from uddi.properties file,
046: // add to system properties
047: Properties myprops = new Properties();
048: myprops.load(new FileInputStream(args[0]));
049:
050: Properties props = System.getProperties();
051:
052: Enumeration propNames = myprops.propertyNames();
053: while (propNames.hasMoreElements()) {
054: String s = (String) propNames.nextElement();
055: props.setProperty(s, myprops.getProperty(s));
056: }
057:
058: // Create connection, message factory, and
059: // SOAP factory
060: SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory
061: .newInstance();
062: SOAPConnection connection = soapConnectionFactory
063: .createConnection();
064: MessageFactory messageFactory = MessageFactory
065: .newInstance();
066: SOAPFactory soapFactory = SOAPFactory.newInstance();
067:
068: // Create a message
069: SOAPMessage message = messageFactory.createMessage();
070:
071: // Get the SOAP header from the message and remove it
072: SOAPHeader header = message.getSOAPHeader();
073: header.detachNode();
074:
075: // Get the SOAP body from the message
076: SOAPBody body = message.getSOAPBody();
077:
078: // Create a UDDI v2 find_business body element
079: SOAPBodyElement findBusiness = body
080: .addBodyElement(soapFactory.createName(
081: "find_business", "", "urn:uddi-org:api_v2"));
082: findBusiness.addAttribute(
083: soapFactory.createName("generic"), "2.0");
084: findBusiness.addAttribute(
085: soapFactory.createName("maxRows"), "100");
086:
087: SOAPElement businessName = findBusiness
088: .addChildElement(soapFactory.createName("name"));
089: businessName.addTextNode(args[1]);
090:
091: message.saveChanges();
092:
093: // Display the message you are sending
094: System.out.println("\n----- Request Message ----\n");
095: message.writeTo(System.out);
096:
097: // Retrieve the endpoint from system properties
098: URL endpoint = new URL(System.getProperties().getProperty(
099: "URL"));
100:
101: // Send message and get reply
102: SOAPMessage reply = connection.call(message, endpoint);
103:
104: System.out.println("\n\nReceived reply from: " + endpoint);
105: System.out.println("\n------ Reply Message -----\n");
106:
107: // Display the reply
108: reply.writeTo(System.out);
109:
110: // Now parse the reply
111: SOAPBody replyBody = reply.getSOAPBody();
112:
113: /*
114: * The response to a find_business query is a
115: * businessList conformant to the UDDI V2 Data
116: * Structure specifications. For further details,
117: * see
118: * http://uddi.org/pubs/DataStructure-V2.03-Published-20020719.htm#_Toc25130802
119: */
120: System.out.println("\n\nContent extracted from "
121: + "the reply message:\n");
122:
123: Iterator businessListIterator = replyBody
124: .getChildElements(soapFactory.createName(
125: "businessList", "", "urn:uddi-org:api_v2"));
126:
127: /*
128: * businessList is a mandatory element in a
129: * successful response and appears only once.
130: */
131: SOAPBodyElement businessList = (SOAPBodyElement) businessListIterator
132: .next();
133:
134: /*
135: * Get the businessInfos element. This contains
136: * the business entries retrieved that match the
137: * criteria specified in the find_business
138: * request.
139: */
140: Iterator businessInfosIterator = businessList
141: .getChildElements(soapFactory.createName(
142: "businessInfos", "", "urn:uddi-org:api_v2"));
143:
144: /*
145: * businessInfos, too is a mandatory element in
146: * a successful response and appears only once.
147: */
148: SOAPElement businessInfos = (SOAPElement) businessInfosIterator
149: .next();
150:
151: /*
152: * The businessInfos contains zero or more
153: * businessInfo structures. Each businessInfo
154: * structure contains the company name and
155: * optional description data.
156: */
157: Iterator businessInfoIterator = businessInfos
158: .getChildElements(soapFactory.createName(
159: "businessInfo", "", "urn:uddi-org:api_v2"));
160:
161: if (!businessInfoIterator.hasNext()) {
162: System.out.println("No businesses found "
163: + "matching the name \"" + args[1] + "\".");
164: } else {
165: while (businessInfoIterator.hasNext()) {
166: SOAPElement businessInfo = (SOAPElement) businessInfoIterator
167: .next();
168:
169: // Extract name and description from the
170: // businessInfo
171: Iterator nameIterator = businessInfo
172: .getChildElements(soapFactory.createName(
173: "name", "", "urn:uddi-org:api_v2"));
174:
175: while (nameIterator.hasNext()) {
176: businessName = (SOAPElement) nameIterator
177: .next();
178: System.out.println("Company name: "
179: + businessName.getValue());
180: }
181:
182: Iterator descriptionIterator = businessInfo
183: .getChildElements(soapFactory.createName(
184: "description", "",
185: "urn:uddi-org:api_v2"));
186:
187: while (descriptionIterator.hasNext()) {
188: SOAPElement businessDescription = (SOAPElement) descriptionIterator
189: .next();
190: System.out.println("Description: "
191: + businessDescription.getValue());
192: }
193:
194: System.out.println("");
195: }
196: }
197:
198: // Close the connection
199: connection.close();
200: } catch (Exception ex) {
201: ex.printStackTrace();
202: }
203: }
204: }
|