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.server;
037:
038: import java.io.IOException;
039: import java.util.Iterator;
040: import java.util.logging.Level;
041: import java.util.logging.Logger;
042:
043: import javax.xml.stream.XMLStreamException;
044: import javax.xml.stream.XMLStreamWriter;
045: import javax.xml.ws.WebServiceException;
046: import javax.xml.namespace.QName;
047:
048: import com.sun.xml.ws.api.message.Packet;
049: import com.sun.xml.ws.api.server.DocumentAddressResolver;
050: import com.sun.xml.ws.api.server.PortAddressResolver;
051: import com.sun.xml.ws.api.server.SDDocument;
052: import com.sun.xml.ws.api.server.ServiceDefinition;
053: import com.sun.xml.ws.api.server.WSEndpoint;
054:
055: import com.sun.xml.ws.mex.MessagesMessages;
056:
057: import static com.sun.xml.ws.mex.MetadataConstants.MEX_NAMESPACE;
058: import static com.sun.xml.ws.mex.MetadataConstants.MEX_PREFIX;
059: import static com.sun.xml.ws.mex.MetadataConstants.SCHEMA_DIALECT;
060: import static com.sun.xml.ws.mex.MetadataConstants.WSDL_DIALECT;
061:
062: /**
063: * This class is used to add the endpoint's metadata to
064: * the mex MetadataResponse element.
065: *
066: * @author WS Development Team
067: */
068: public class WSDLRetriever {
069:
070: private final WSEndpoint endpoint;
071:
072: private static final Logger logger = Logger
073: .getLogger(WSDLRetriever.class.getName());
074:
075: /*
076: * This class is used by the SDDocument object in the
077: * jax-ws runtime. The agreement we have is to return
078: * null to the jax-ws runtime for mex responses. This
079: * tells the jax-ws runtime not to add schemaLocation
080: * attributes for schema import statements.
081: */
082: private static final DocumentAddressResolver dar = new DocumentAddressResolver() {
083: public String getRelativeAddressFor(final SDDocument doc1,
084: final SDDocument doc2) {
085:
086: return null;
087: }
088: };
089:
090: public WSDLRetriever(WSEndpoint endpoint) {
091: this .endpoint = endpoint;
092: }
093:
094: /*
095: * This method is called by the server pipe to write out
096: * the wsdl and schema documents to the mex response.
097: */
098: void addDocuments(final XMLStreamWriter writer,
099: final Packet request, final String address)
100: throws XMLStreamException {
101:
102: final ServiceDefinition sDef = endpoint.getServiceDefinition();
103: if (sDef == null) {
104: return;
105: }
106: final Iterator<SDDocument> docs = sDef.iterator();
107: while (docs.hasNext()) {
108: writeDoc(writer, docs.next(), address);
109: }
110: }
111:
112: /*
113: * This method writes out each individual document, which
114: * must be wrapped in a MetadataSection element within
115: * the mex response. It also sets the Dialect attribure
116: * of the section so that sections can be classified as
117: * wsdl, schema, etc. when parsed.
118: */
119: private void writeDoc(final XMLStreamWriter writer,
120: final SDDocument doc, final String add)
121: throws XMLStreamException {
122:
123: try {
124: writer.writeStartElement(MEX_PREFIX, "MetadataSection",
125: MEX_NAMESPACE);
126: if (doc.isWSDL()) {
127: writer.writeAttribute("Dialect", WSDL_DIALECT);
128: writer.writeAttribute("Identifier",
129: ((SDDocument.WSDL) doc).getTargetNamespace());
130: } else if (doc.isSchema()) {
131: writer.writeAttribute("Dialect", SCHEMA_DIALECT);
132: writer.writeAttribute("Identifier",
133: ((SDDocument.Schema) doc).getTargetNamespace());
134: }
135: doc.writeTo(new PortAddressResolverImpl(add), dar, writer);
136: writer.writeEndElement();
137: } catch (IOException ioe) {
138: // this should be very rare
139: String exceptionMessage = MessagesMessages
140: .MEX_0015_IOEXCEPTION_WHILE_WRITING_RESPONSE(add);
141: logger.log(Level.SEVERE, exceptionMessage, ioe);
142: throw new WebServiceException(exceptionMessage, ioe);
143: }
144: }
145:
146: /*
147: * This object is passed to the jax-ws runtime to give
148: * the address to be included in the wsdl.
149: */
150: static class PortAddressResolverImpl extends PortAddressResolver {
151:
152: private final String address;
153:
154: PortAddressResolverImpl(String address) {
155: this .address = address;
156: }
157:
158: public String getAddressFor(QName serviceName,
159: final String portName) {
160: return address;
161: }
162:
163: }
164: }
|