001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036: package com.sun.xml.ws.mex.client;
037:
038: import java.io.IOException;
039: import java.io.InputStream;
040: import java.util.ArrayList;
041: import java.util.List;
042: import java.util.logging.Level;
043: import java.util.logging.Logger;
044:
045: import javax.xml.bind.JAXBContext;
046: import javax.xml.bind.JAXBException;
047: import javax.xml.bind.Unmarshaller;
048: import javax.xml.namespace.QName;
049: import javax.xml.stream.XMLInputFactory;
050: import javax.xml.stream.XMLStreamException;
051: import javax.xml.stream.XMLStreamReader;
052:
053: import com.sun.istack.NotNull;
054: import com.sun.xml.ws.mex.MessagesMessages;
055: import com.sun.xml.ws.mex.client.schema.Metadata;
056: import com.sun.xml.ws.mex.client.schema.MetadataReference;
057: import com.sun.xml.ws.mex.client.schema.MetadataSection;
058:
059: import org.w3c.dom.NamedNodeMap;
060: import org.w3c.dom.Node;
061: import org.w3c.dom.NodeList;
062:
063: import static com.sun.xml.ws.mex.MetadataConstants.ERROR_LOG_LEVEL;
064: import static com.sun.xml.ws.mex.MetadataConstants.WSDL_DIALECT;
065: import static com.sun.xml.ws.mex.MetadataConstants.SCHEMA_DIALECT;
066:
067: /**
068: * Class used for retrieving metadata at runtime. The intended usage is:
069: * <p>
070: * <code>MetadataClient mClient = new MetadataClient();</code><br>
071: * <code>Metadata mData = mClient.retrieveMetadata(someAddress);</code><br>
072: * <p>
073: * Utility methods will be added for common usages of the metadata. For
074: * instance, the service and port QNames from the endpoint can be
075: * retrieved from the metadata with:
076: * <p>
077: * <code>Map<QName, List<PortInfo>> names = mClient.getServiceAndPortNames(mData);</code>
078: */
079: public class MetadataClient {
080:
081: enum Protocol {
082: SOAP_1_2, SOAP_1_1
083: };
084:
085: private final String[] suffixes = { "", "/mex" };
086: private final MetadataUtil mexUtil;
087: private static final JAXBContext jaxbContext;
088:
089: private static final Logger logger = Logger
090: .getLogger(MetadataClient.class.getName());
091:
092: static {
093: try {
094: jaxbContext = JAXBContext
095: .newInstance("com.sun.xml.ws.mex.client.schema");
096: } catch (JAXBException jaxbE) {
097: throw new AssertionError(jaxbE);
098: }
099: }
100:
101: /**
102: * Default constructor.
103: */
104: public MetadataClient() {
105: mexUtil = new MetadataUtil();
106: }
107:
108: /**
109: * Method used to load the metadata from the endpoint. First
110: * soap 1.2 is used, then 1.1. If both attempts fail, the
111: * client will try again adding "/mex" to the address.
112: * <P>
113: * If any wsdl or schema import elements are found with
114: * empty location attributes, these attributes are removed.
115: * In the case of data returned to JAX-WS through
116: * ServiceDescriptorImpl, these attributes are added back
117: * in with appropriate location information.
118: *
119: * @see com.sun.xml.ws.mex.client.ServiceDescriptorImpl
120: * @param address The address used to query for Metadata
121: * @return The metadata object, or null if no metadata could
122: * be obtained from the service
123: */
124: public Metadata retrieveMetadata(@NotNull
125: final String address) {
126: for (String suffix : suffixes) {
127: final String newAddress = address.concat(suffix);
128: for (Protocol p : Protocol.values()) {
129: InputStream responseStream = null;
130: try {
131: responseStream = mexUtil.getMetadata(newAddress, p);
132: return createMetadata(responseStream);
133: } catch (IOException e) {
134: logger.log(ERROR_LOG_LEVEL, MessagesMessages
135: .MEX_0006_RETRIEVING_MDATA_FAILURE(p,
136: newAddress));
137: continue;
138: } catch (Exception e) {
139: logger.log(Level.WARNING, MessagesMessages
140: .MEX_0008_PARSING_MDATA_FAILURE(p,
141: newAddress));
142: continue;
143: }
144: }
145: }
146: logger.log(ERROR_LOG_LEVEL, MessagesMessages
147: .MEX_0007_RETURNING_NULL_MDATA());
148: return null;
149: }
150:
151: /**
152: * Currently only supports Get requests (not Get Metadata),
153: * so we only need the reference's address. Any metadata
154: * about the request is ignored.
155: *
156: * @see #retrieveMetadata(String)
157: */
158: public Metadata retrieveMetadata(@NotNull
159: final MetadataReference reference) {
160:
161: final List nodes = reference.getAny();
162: for (Object o : nodes) {
163: final Node node = (Node) o;
164: if (node.getLocalName().equals("Address")) {
165: final String address = node.getFirstChild()
166: .getNodeValue();
167: return retrieveMetadata(address);
168: }
169: }
170: return null;
171: }
172:
173: /**
174: * Used to retrieve the service and port names and port addresses
175: * from metadata. If there is more than one wsdl section in the metadata,
176: * only the first is parsed by this method.
177: *
178: * @see com.sun.xml.ws.mex.client.PortInfo
179: * @return A list of PortInfo objects
180: */
181: public List<PortInfo> getServiceInformation(@NotNull
182: final Metadata data) {
183: for (MetadataSection section : data.getMetadataSection()) {
184: if (section.getDialect().equals(WSDL_DIALECT)) {
185: if (section.getAny() != null) {
186: return getServiceInformationFromNode(section
187: .getAny());
188: }
189: if (section.getMetadataReference() != null) {
190: final Metadata newMetadata = retrieveMetadata(section
191: .getMetadataReference());
192: return getServiceInformation(newMetadata);
193: }
194: if (section.getLocation() != null) {
195: final Metadata newMetadata = retrieveMetadata(section
196: .getLocation());
197: return getServiceInformation(newMetadata);
198: }
199: }
200: }
201: return null;
202: }
203:
204: private List<PortInfo> getServiceInformationFromNode(
205: final Object node) {
206: if (node == null) {
207: return null;
208: }
209: final List<PortInfo> portInfos = new ArrayList<PortInfo>();
210: final Node wsdlNode = (Node) node;
211: final String namespace = getAttributeValue(wsdlNode,
212: "targetNamespace");
213: final NodeList nodes = wsdlNode.getChildNodes();
214: for (int i = 0; i < nodes.getLength(); i++) {
215: final Node serviceNode = nodes.item(i);
216: if (serviceNode.getLocalName() != null
217: && serviceNode.getLocalName().equals("service")) {
218:
219: final Node nameAtt = wsdlNode.getAttributes()
220: .getNamedItem("name");
221: final QName serviceName = new QName(namespace, nameAtt
222: .getNodeValue());
223: final NodeList portNodes = serviceNode.getChildNodes();
224: for (int j = 0; j < portNodes.getLength(); j++) {
225: final Node portNode = portNodes.item(j);
226: if (portNode.getLocalName() != null
227: && portNode.getLocalName().equals("port")) {
228:
229: final QName portName = new QName(namespace,
230: getAttributeValue(portNode, "name"));
231: final String address = getPortAddress(portNode);
232: portInfos.add(new PortInfo(serviceName,
233: portName, address));
234: }
235: }
236: }
237: }
238: return portInfos;
239: }
240:
241: /*
242: * Create Metadata object from output stream. Need to remove
243: * the metadata from soap wrapper. If the metadata contains
244: * metadata refernces or HTTP GET location elements, these
245: * are dereferenced later.
246: */
247: private Metadata createMetadata(final InputStream stream)
248: throws XMLStreamException, JAXBException {
249:
250: final XMLInputFactory factory = XMLInputFactory.newInstance();
251: final XMLStreamReader reader = factory
252: .createXMLStreamReader(stream);
253: int state = 0;
254: do {
255: state = reader.next();
256: } while (state != reader.START_ELEMENT
257: || !reader.getLocalName().equals("Metadata"));
258:
259: final Unmarshaller uMarhaller = jaxbContext
260: .createUnmarshaller();
261: final Metadata mData = (Metadata) uMarhaller.unmarshal(reader);
262: cleanData(mData);
263: return mData;
264: }
265:
266: /*
267: * Get the value of an attribute from a given node.
268: */
269: private String getAttributeValue(final Node node,
270: final String attName) {
271: return node.getAttributes().getNamedItem(attName)
272: .getNodeValue();
273: }
274:
275: /*
276: * Get the port address from a port node. Returns null
277: * if there is not one (which would be an error).
278: */
279: private String getPortAddress(final Node portNode) {
280: final NodeList portDetails = portNode.getChildNodes();
281: for (int i = 0; i < portDetails.getLength(); i++) {
282: final Node addressNode = portDetails.item(i);
283: if (addressNode.getLocalName() != null
284: && addressNode.getLocalName().equals("address")) {
285:
286: return getAttributeValue(addressNode, "location");
287: }
288: }
289: logger.warning(MessagesMessages
290: .MEX_0009_ADDRESS_NOT_FOUND_FOR_PORT(portNode));
291: return null;
292: }
293:
294: /*
295: * This is a workaround for a bug in some indigo wsdls
296: * that are being returned with schema or wsdl imports
297: * containing empty location attributes.
298: *
299: * If getAny() returns null, the metadata section contains
300: * a metadata reference or location rather than the wsdl.
301: */
302: private void cleanData(final Metadata mData) {
303: for (MetadataSection section : mData.getMetadataSection()) {
304: if (section.getDialect().equals(WSDL_DIALECT)
305: && section.getAny() != null) {
306: cleanWSDLNode((Node) section.getAny());
307: } else if (section.getDialect().equals(SCHEMA_DIALECT)
308: && section.getAny() != null) {
309: cleanSchemaNode((Node) section.getAny());
310: }
311: }
312: }
313:
314: /*
315: * This should be passed the top level wsdl:definitions node.
316: */
317: private void cleanWSDLNode(final Node wsdlNode) {
318: final NodeList nodes = wsdlNode.getChildNodes();
319: for (int i = 0; i < nodes.getLength(); i++) {
320: final Node node = nodes.item(i);
321: if (node.getLocalName() != null) {
322: if (node.getLocalName().equals("types")) {
323: final NodeList schemaNodes = node.getChildNodes();
324: for (int j = 0; j < schemaNodes.getLength(); j++) {
325: final Node schemaNode = schemaNodes.item(j);
326: if (schemaNode.getLocalName() != null
327: && schemaNode.getLocalName().equals(
328: "schema")) {
329:
330: cleanSchemaNode(schemaNode);
331: }
332: }
333: } else if (node.getLocalName().equals("import")) {
334: cleanImport(node);
335: }
336: }
337: }
338: }
339:
340: private void cleanSchemaNode(final Node schemaNode) {
341: final NodeList children = schemaNode.getChildNodes();
342: for (int i = 0; i < children.getLength(); i++) {
343: final Node importNode = children.item(i);
344: if (importNode.getLocalName() != null
345: && importNode.getLocalName().equals("import")) {
346: cleanImport(importNode);
347: }
348: }
349: }
350:
351: /*
352: * Takes the import node itself and removes any empty
353: * schemaLocation or location attributes. There will
354: * only be one or the other, so the method returns if
355: * it finds a schema location.
356: */
357: private void cleanImport(final Node importNode) {
358: final NamedNodeMap atts = importNode.getAttributes();
359: Node location = atts.getNamedItem("schemaLocation");
360: if (location != null && location.getNodeValue().equals("")) {
361: atts.removeNamedItem("schemaLocation");
362: return;
363: }
364: location = atts.getNamedItem("location");
365: if (location != null && location.getNodeValue().equals("")) {
366: atts.removeNamedItem("location");
367: }
368: }
369:
370: }
|