src/share/jaxws_classes/com/sun/tools/internal/ws/util/WSDLFetcher.java

Fri, 04 Oct 2013 16:21:34 +0100

author
mkos
date
Fri, 04 Oct 2013 16:21:34 +0100
changeset 408
b0610cd08440
parent 368
0989ad8c0860
child 637
9c07ef4934dd
permissions
-rw-r--r--

8025054: Update JAX-WS RI integration to 2.2.9-b130926.1035
Reviewed-by: chegar

ohair@286 1 /*
alanb@368 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
ohair@286 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286 4 *
ohair@286 5 * This code is free software; you can redistribute it and/or modify it
ohair@286 6 * under the terms of the GNU General Public License version 2 only, as
ohair@286 7 * published by the Free Software Foundation. Oracle designates this
ohair@286 8 * particular file as subject to the "Classpath" exception as provided
ohair@286 9 * by Oracle in the LICENSE file that accompanied this code.
ohair@286 10 *
ohair@286 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286 14 * version 2 for more details (a copy is included in the LICENSE file that
ohair@286 15 * accompanied this code).
ohair@286 16 *
ohair@286 17 * You should have received a copy of the GNU General Public License version
ohair@286 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286 20 *
ohair@286 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286 22 * or visit www.oracle.com if you need additional information or have any
ohair@286 23 * questions.
ohair@286 24 */
ohair@286 25
ohair@286 26 package com.sun.tools.internal.ws.util;
ohair@286 27
ohair@286 28 import com.sun.istack.internal.NotNull;
ohair@286 29 import com.sun.tools.internal.ws.resources.WscompileMessages;
ohair@286 30 import com.sun.tools.internal.ws.wscompile.WsimportListener;
ohair@286 31 import com.sun.tools.internal.ws.wscompile.WsimportOptions;
ohair@286 32 import com.sun.tools.internal.ws.wsdl.parser.DOMForest;
ohair@286 33 import com.sun.tools.internal.ws.wsdl.parser.MetadataFinder;
ohair@286 34 import com.sun.xml.internal.txw2.output.IndentingXMLStreamWriter;
ohair@286 35 import com.sun.xml.internal.ws.api.server.PortAddressResolver;
ohair@286 36 import com.sun.xml.internal.ws.streaming.SourceReaderFactory;
ohair@286 37 import com.sun.xml.internal.ws.wsdl.parser.WSDLConstants;
ohair@286 38 import com.sun.xml.internal.ws.wsdl.writer.DocumentLocationResolver;
ohair@286 39 import com.sun.xml.internal.ws.wsdl.writer.WSDLPatcher;
ohair@286 40 import org.w3c.dom.Document;
ohair@286 41 import org.w3c.dom.Element;
ohair@286 42 import org.w3c.dom.Node;
ohair@286 43 import org.w3c.dom.NodeList;
ohair@286 44
ohair@286 45 import javax.xml.namespace.QName;
ohair@286 46 import javax.xml.stream.XMLOutputFactory;
ohair@286 47 import javax.xml.stream.XMLStreamException;
ohair@286 48 import javax.xml.stream.XMLStreamReader;
ohair@286 49 import javax.xml.stream.XMLStreamWriter;
ohair@286 50 import javax.xml.transform.dom.DOMSource;
ohair@286 51 import java.io.*;
ohair@286 52 import java.net.MalformedURLException;
ohair@286 53 import java.net.URL;
ohair@286 54 import java.util.HashMap;
ohair@286 55 import java.util.Map;
ohair@286 56 import java.util.Set;
ohair@286 57
ohair@286 58 /**
ohair@286 59 * @author Rama Pulavarthi
ohair@286 60 */
ohair@286 61 public class WSDLFetcher {
ohair@286 62 private WsimportOptions options;
ohair@286 63 private WsimportListener listener;
ohair@286 64 public WSDLFetcher(WsimportOptions options, WsimportListener listener) {
ohair@286 65 this.options = options;
ohair@286 66 this.listener = listener;
ohair@286 67 }
ohair@286 68
ohair@286 69
ohair@286 70 /**
ohair@286 71 * Fetches the wsdls in the DOMForest to the options.destDir
ohair@286 72 * @param forest
alanb@368 73 * @return location of fetched root WSDL document
ohair@286 74 * @throws IOException
ohair@286 75 * @throws XMLStreamException
ohair@286 76 * @throws FileNotFoundException
ohair@286 77 */
ohair@286 78 public String fetchWsdls(MetadataFinder forest) throws IOException, XMLStreamException {
ohair@286 79 String rootWsdl = null;
ohair@286 80 for(String root: forest.getRootDocuments()) {
ohair@286 81 rootWsdl = root;
ohair@286 82 }
ohair@286 83
ohair@286 84 Set<String> externalRefs = forest.getExternalReferences();
ohair@286 85 Map<String,String> documentMap = createDocumentMap(forest, getWSDLDownloadDir(), rootWsdl, externalRefs);
ohair@286 86 String rootWsdlName = fetchFile(rootWsdl,forest, documentMap,getWSDLDownloadDir());
ohair@286 87 for(String reference: forest.getExternalReferences()) {
ohair@286 88 fetchFile(reference,forest,documentMap,getWSDLDownloadDir());
ohair@286 89 }
ohair@286 90 return WSDL_PATH +"/" + rootWsdlName;
ohair@286 91 }
ohair@286 92
ohair@286 93 private String fetchFile(final String doc, DOMForest forest, final Map<String, String> documentMap, File destDir) throws IOException, XMLStreamException {
ohair@286 94
ohair@286 95 DocumentLocationResolver docLocator = createDocResolver(doc, forest, documentMap);
ohair@286 96 WSDLPatcher wsdlPatcher = new WSDLPatcher(new PortAddressResolver() {
ohair@286 97 @Override
ohair@286 98 public String getAddressFor(@NotNull QName serviceName, @NotNull String portName) {
ohair@286 99 return null;
ohair@286 100 }
ohair@286 101 }, docLocator);
ohair@286 102
alanb@368 103 XMLStreamReader xsr = null;
alanb@368 104 XMLStreamWriter xsw = null;
alanb@368 105 OutputStream os = null;
alanb@368 106 String resolvedRootWsdl = null;
alanb@368 107 try {
alanb@368 108 XMLOutputFactory writerfactory;
alanb@368 109 xsr = SourceReaderFactory.createSourceReader(new DOMSource(forest.get(doc)), false);
alanb@368 110 writerfactory = XMLOutputFactory.newInstance();
alanb@368 111 resolvedRootWsdl = docLocator.getLocationFor(null, doc);
alanb@368 112 File outFile = new File(destDir, resolvedRootWsdl);
alanb@368 113 os = new FileOutputStream(outFile);
alanb@368 114 if(options.verbose) {
alanb@368 115 listener.message(WscompileMessages.WSIMPORT_DOCUMENT_DOWNLOAD(doc,outFile));
alanb@368 116 }
alanb@368 117 xsw = writerfactory.createXMLStreamWriter(os);
alanb@368 118 //DOMForest eats away the whitespace loosing all the indentation, so write it through
alanb@368 119 // indenting writer for better readability of fetched documents
alanb@368 120 IndentingXMLStreamWriter indentingWriter = new IndentingXMLStreamWriter(xsw);
alanb@368 121 wsdlPatcher.bridge(xsr, indentingWriter);
alanb@368 122 options.addGeneratedFile(outFile);
alanb@368 123 } finally {
alanb@368 124 try {
alanb@368 125 if (xsr != null) {xsr.close();}
alanb@368 126 if (xsw != null) {xsw.close();}
alanb@368 127 } finally {
alanb@368 128 if (os != null) {os.close();}
alanb@368 129 }
ohair@286 130 }
ohair@286 131 return resolvedRootWsdl;
ohair@286 132
ohair@286 133
ohair@286 134 }
ohair@286 135 private Map<String,String> createDocumentMap(MetadataFinder forest, File baseDir, final String rootWsdl, Set<String> externalReferences) {
ohair@286 136 Map<String,String> map = new HashMap<String,String>();
ohair@286 137 String rootWsdlFileName = rootWsdl;
ohair@286 138 String rootWsdlName;
ohair@286 139
ohair@286 140 int slashIndex = rootWsdl.lastIndexOf("/");
ohair@286 141 if( slashIndex >= 0) {
ohair@286 142 rootWsdlFileName = rootWsdl.substring(slashIndex+1);
ohair@286 143 }
ohair@286 144 if(!rootWsdlFileName.endsWith(WSDL_FILE_EXTENSION)) {
ohair@286 145 Document rootWsdlDoc = forest.get(rootWsdl);
ohair@286 146 NodeList serviceNodes = rootWsdlDoc.getElementsByTagNameNS(WSDLConstants.QNAME_SERVICE.getNamespaceURI(),WSDLConstants.QNAME_SERVICE.getLocalPart());
alanb@368 147 if (serviceNodes.getLength() == 0) {
ohair@286 148 rootWsdlName = "Service";
alanb@368 149 } else {
ohair@286 150 Node serviceNode = serviceNodes.item(0);
ohair@286 151 String serviceName = ((Element)serviceNode).getAttribute( WSDLConstants.ATTR_NAME);
ohair@286 152 rootWsdlName = serviceName;
ohair@286 153 }
ohair@286 154 rootWsdlFileName = rootWsdlName+ WSDL_FILE_EXTENSION;
ohair@286 155 } else {
ohair@286 156 rootWsdlName = rootWsdlFileName.substring(0,rootWsdlFileName.length()-5);
ohair@286 157 }
ohair@286 158
ohair@286 159 map.put(rootWsdl,sanitize(rootWsdlFileName));
ohair@286 160
ohair@286 161 int i =1;
ohair@286 162 for(String ref: externalReferences) {
ohair@286 163 Document refDoc = forest.get(ref);
ohair@286 164 Element rootEl = refDoc.getDocumentElement();
ohair@286 165 String fileExtn;
ohair@286 166 String fileName = null;
ohair@286 167 int index = ref.lastIndexOf("/");
ohair@286 168 if (index >= 0) {
ohair@286 169 fileName = ref.substring(index + 1);
ohair@286 170 }
ohair@286 171 if(rootEl.getLocalName().equals(WSDLConstants.QNAME_DEFINITIONS.getLocalPart()) && rootEl.getNamespaceURI().equals(WSDLConstants.NS_WSDL)) {
ohair@286 172 fileExtn = WSDL_FILE_EXTENSION;
ohair@286 173 } else if(rootEl.getLocalName().equals(WSDLConstants.QNAME_SCHEMA.getLocalPart()) && rootEl.getNamespaceURI().equals(WSDLConstants.NS_XMLNS)) {
ohair@286 174 fileExtn = SCHEMA_FILE_EXTENSION;
ohair@286 175 } else {
ohair@286 176 fileExtn = ".xml";
ohair@286 177 }
ohair@286 178 if(fileName != null && (fileName.endsWith(WSDL_FILE_EXTENSION) || fileName.endsWith(SCHEMA_FILE_EXTENSION))) {
ohair@286 179 map.put(ref, rootWsdlName+"_"+fileName);
ohair@286 180 } else {
ohair@286 181 map.put(ref, rootWsdlName+"_metadata"+ (i++) + fileExtn);
ohair@286 182 }
ohair@286 183 }
ohair@286 184 return map;
ohair@286 185 }
ohair@286 186
ohair@286 187 private DocumentLocationResolver createDocResolver(final String baseWsdl, final DOMForest forest, final Map<String,String> documentMap) {
ohair@286 188 return new DocumentLocationResolver() {
alanb@368 189 @Override
ohair@286 190 public String getLocationFor(String namespaceURI, String systemId) {
ohair@286 191 try {
ohair@286 192 URL reference = new URL(new URL(baseWsdl),systemId);
ohair@286 193 systemId = reference.toExternalForm();
ohair@286 194 } catch (MalformedURLException e) {
ohair@286 195 throw new RuntimeException(e);
ohair@286 196 }
ohair@286 197 if(documentMap.get(systemId) != null) {
ohair@286 198 return documentMap.get(systemId);
ohair@286 199 } else {
ohair@286 200 String parsedEntity = forest.getReferencedEntityMap().get(systemId);
ohair@286 201 return documentMap.get(parsedEntity);
ohair@286 202 }
ohair@286 203 }
ohair@286 204 };
ohair@286 205 }
ohair@286 206
ohair@286 207 private String sanitize(String fileName) {
ohair@286 208 fileName = fileName.replace('?', '.');
alanb@368 209 StringBuilder sb = new StringBuilder(fileName);
ohair@286 210 for (int i = 0; i < sb.length(); i++) {
ohair@286 211 char c = sb.charAt(i);
ohair@286 212 if (Character.isLetterOrDigit(c) ||
ohair@286 213 (c == '/') ||
ohair@286 214 (c == '.') ||
ohair@286 215 (c == '_') ||
ohair@286 216 (c == ' ') ||
ohair@286 217 (c == '-')) {
ohair@286 218 continue;
ohair@286 219 } else {
ohair@286 220 sb.setCharAt(i, '_');
ohair@286 221 }
ohair@286 222 }
ohair@286 223 return sb.toString();
ohair@286 224 }
ohair@286 225
ohair@286 226 private File getWSDLDownloadDir() {
alanb@368 227 File wsdlDir = new File(options.destDir, WSDL_PATH);
alanb@368 228 boolean created = wsdlDir.mkdirs();
alanb@368 229 if (options.verbose && !created) {
alanb@368 230 listener.message(WscompileMessages.WSCOMPILE_NO_SUCH_DIRECTORY(wsdlDir));
alanb@368 231 }
ohair@286 232 return wsdlDir;
ohair@286 233 }
ohair@286 234
ohair@286 235 private static String WSDL_PATH="META-INF/wsdl";
ohair@286 236 private static String WSDL_FILE_EXTENSION=".wsdl";
ohair@286 237 private static String SCHEMA_FILE_EXTENSION=".xsd";
ohair@286 238 }

mercurial