1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/tools/internal/ws/util/WSDLFetcher.java Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,227 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.tools.internal.ws.util; 1.30 + 1.31 +import com.sun.istack.internal.NotNull; 1.32 +import com.sun.tools.internal.ws.resources.WscompileMessages; 1.33 +import com.sun.tools.internal.ws.wscompile.WsimportListener; 1.34 +import com.sun.tools.internal.ws.wscompile.WsimportOptions; 1.35 +import com.sun.tools.internal.ws.wsdl.parser.DOMForest; 1.36 +import com.sun.tools.internal.ws.wsdl.parser.MetadataFinder; 1.37 +import com.sun.xml.internal.txw2.output.IndentingXMLStreamWriter; 1.38 +import com.sun.xml.internal.ws.api.server.PortAddressResolver; 1.39 +import com.sun.xml.internal.ws.streaming.SourceReaderFactory; 1.40 +import com.sun.xml.internal.ws.wsdl.parser.WSDLConstants; 1.41 +import com.sun.xml.internal.ws.wsdl.writer.DocumentLocationResolver; 1.42 +import com.sun.xml.internal.ws.wsdl.writer.WSDLPatcher; 1.43 +import org.w3c.dom.Document; 1.44 +import org.w3c.dom.Element; 1.45 +import org.w3c.dom.Node; 1.46 +import org.w3c.dom.NodeList; 1.47 + 1.48 +import javax.xml.namespace.QName; 1.49 +import javax.xml.stream.XMLOutputFactory; 1.50 +import javax.xml.stream.XMLStreamException; 1.51 +import javax.xml.stream.XMLStreamReader; 1.52 +import javax.xml.stream.XMLStreamWriter; 1.53 +import javax.xml.transform.dom.DOMSource; 1.54 +import java.io.*; 1.55 +import java.net.MalformedURLException; 1.56 +import java.net.URL; 1.57 +import java.util.HashMap; 1.58 +import java.util.Map; 1.59 +import java.util.Set; 1.60 + 1.61 +/** 1.62 + * @author Rama Pulavarthi 1.63 + */ 1.64 +public class WSDLFetcher { 1.65 + private WsimportOptions options; 1.66 + private WsimportListener listener; 1.67 + public WSDLFetcher(WsimportOptions options, WsimportListener listener) { 1.68 + this.options = options; 1.69 + this.listener = listener; 1.70 + } 1.71 + 1.72 + 1.73 + /** 1.74 + * Fetches the wsdls in the DOMForest to the options.destDir 1.75 + * @param forest 1.76 + * @return 1.77 + * @throws IOException 1.78 + * @throws XMLStreamException 1.79 + * @throws FileNotFoundException 1.80 + */ 1.81 + public String fetchWsdls(MetadataFinder forest) throws IOException, XMLStreamException { 1.82 + String rootWsdl = null; 1.83 + for(String root: forest.getRootDocuments()) { 1.84 + rootWsdl = root; 1.85 + } 1.86 + 1.87 + Set<String> externalRefs = forest.getExternalReferences(); 1.88 + Map<String,String> documentMap = createDocumentMap(forest, getWSDLDownloadDir(), rootWsdl, externalRefs); 1.89 + String rootWsdlName = fetchFile(rootWsdl,forest, documentMap,getWSDLDownloadDir()); 1.90 + for(String reference: forest.getExternalReferences()) { 1.91 + fetchFile(reference,forest,documentMap,getWSDLDownloadDir()); 1.92 + } 1.93 + return WSDL_PATH +"/" + rootWsdlName; 1.94 + } 1.95 + 1.96 + private String fetchFile(final String doc, DOMForest forest, final Map<String, String> documentMap, File destDir) throws IOException, XMLStreamException { 1.97 + 1.98 + DocumentLocationResolver docLocator = createDocResolver(doc, forest, documentMap); 1.99 + WSDLPatcher wsdlPatcher = new WSDLPatcher(new PortAddressResolver() { 1.100 + @Override 1.101 + public String getAddressFor(@NotNull QName serviceName, @NotNull String portName) { 1.102 + return null; 1.103 + } 1.104 + }, docLocator); 1.105 + 1.106 + //XMLInputFactory readerFactory = XMLInputFactory.newInstance(); 1.107 + //XMLStreamReader xsr = readerFactory.createXMLStreamReader(new DOMSource(forest.get(rootWsdl))); 1.108 + 1.109 + XMLStreamReader xsr = SourceReaderFactory.createSourceReader(new DOMSource(forest.get(doc)), false); 1.110 + XMLOutputFactory writerfactory = XMLOutputFactory.newInstance(); 1.111 + String resolvedRootWsdl = docLocator.getLocationFor(null, doc); 1.112 + File outFile = new File(destDir, resolvedRootWsdl); 1.113 + OutputStream os = new FileOutputStream(outFile); 1.114 + if(options.verbose) { 1.115 + listener.message(WscompileMessages.WSIMPORT_DOCUMENT_DOWNLOAD(doc,outFile)); 1.116 + } 1.117 + XMLStreamWriter xsw = writerfactory.createXMLStreamWriter(os); 1.118 + //DOMForest eats away the whitespace loosing all the indentation, so write it through 1.119 + // indenting writer for better readability of fetched documents 1.120 + IndentingXMLStreamWriter indentingWriter = new IndentingXMLStreamWriter(xsw); 1.121 + wsdlPatcher.bridge(xsr, indentingWriter); 1.122 + xsr.close(); 1.123 + xsw.close(); 1.124 + os.close(); 1.125 + options.addGeneratedFile(outFile); 1.126 + return resolvedRootWsdl; 1.127 + 1.128 + 1.129 + } 1.130 + private Map<String,String> createDocumentMap(MetadataFinder forest, File baseDir, final String rootWsdl, Set<String> externalReferences) { 1.131 + Map<String,String> map = new HashMap<String,String>(); 1.132 + String rootWsdlFileName = rootWsdl; 1.133 + String rootWsdlName; 1.134 + 1.135 + int slashIndex = rootWsdl.lastIndexOf("/"); 1.136 + if( slashIndex >= 0) { 1.137 + rootWsdlFileName = rootWsdl.substring(slashIndex+1); 1.138 + } 1.139 + if(!rootWsdlFileName.endsWith(WSDL_FILE_EXTENSION)) { 1.140 + Document rootWsdlDoc = forest.get(rootWsdl); 1.141 + NodeList serviceNodes = rootWsdlDoc.getElementsByTagNameNS(WSDLConstants.QNAME_SERVICE.getNamespaceURI(),WSDLConstants.QNAME_SERVICE.getLocalPart()); 1.142 + if(serviceNodes.getLength() == 0) 1.143 + rootWsdlName = "Service"; 1.144 + else { 1.145 + Node serviceNode = serviceNodes.item(0); 1.146 + String serviceName = ((Element)serviceNode).getAttribute( WSDLConstants.ATTR_NAME); 1.147 + rootWsdlName = serviceName; 1.148 + } 1.149 + rootWsdlFileName = rootWsdlName+ WSDL_FILE_EXTENSION; 1.150 + } else { 1.151 + rootWsdlName = rootWsdlFileName.substring(0,rootWsdlFileName.length()-5); 1.152 + } 1.153 + 1.154 + map.put(rootWsdl,sanitize(rootWsdlFileName)); 1.155 + 1.156 + int i =1; 1.157 + for(String ref: externalReferences) { 1.158 + Document refDoc = forest.get(ref); 1.159 + Element rootEl = refDoc.getDocumentElement(); 1.160 + String fileExtn; 1.161 + String fileName = null; 1.162 + int index = ref.lastIndexOf("/"); 1.163 + if (index >= 0) { 1.164 + fileName = ref.substring(index + 1); 1.165 + } 1.166 + if(rootEl.getLocalName().equals(WSDLConstants.QNAME_DEFINITIONS.getLocalPart()) && rootEl.getNamespaceURI().equals(WSDLConstants.NS_WSDL)) { 1.167 + fileExtn = WSDL_FILE_EXTENSION; 1.168 + } else if(rootEl.getLocalName().equals(WSDLConstants.QNAME_SCHEMA.getLocalPart()) && rootEl.getNamespaceURI().equals(WSDLConstants.NS_XMLNS)) { 1.169 + fileExtn = SCHEMA_FILE_EXTENSION; 1.170 + } else { 1.171 + fileExtn = ".xml"; 1.172 + } 1.173 + if(fileName != null && (fileName.endsWith(WSDL_FILE_EXTENSION) || fileName.endsWith(SCHEMA_FILE_EXTENSION))) { 1.174 + map.put(ref, rootWsdlName+"_"+fileName); 1.175 + } else { 1.176 + map.put(ref, rootWsdlName+"_metadata"+ (i++) + fileExtn); 1.177 + } 1.178 + } 1.179 + return map; 1.180 + } 1.181 + 1.182 + private DocumentLocationResolver createDocResolver(final String baseWsdl, final DOMForest forest, final Map<String,String> documentMap) { 1.183 + 1.184 + return new DocumentLocationResolver() { 1.185 + public String getLocationFor(String namespaceURI, String systemId) { 1.186 + try { 1.187 + URL reference = new URL(new URL(baseWsdl),systemId); 1.188 + systemId = reference.toExternalForm(); 1.189 + } catch (MalformedURLException e) { 1.190 + throw new RuntimeException(e); 1.191 + } 1.192 + if(documentMap.get(systemId) != null) { 1.193 + return documentMap.get(systemId); 1.194 + } else { 1.195 + String parsedEntity = forest.getReferencedEntityMap().get(systemId); 1.196 + return documentMap.get(parsedEntity); 1.197 + } 1.198 + } 1.199 + }; 1.200 + } 1.201 + 1.202 + private String sanitize(String fileName) { 1.203 + fileName = fileName.replace('?', '.'); 1.204 + StringBuffer sb = new StringBuffer(fileName); 1.205 + for (int i = 0; i < sb.length(); i++) { 1.206 + char c = sb.charAt(i); 1.207 + if (Character.isLetterOrDigit(c) || 1.208 + (c == '/') || 1.209 + (c == '.') || 1.210 + (c == '_') || 1.211 + (c == ' ') || 1.212 + (c == '-')) { 1.213 + continue; 1.214 + } else { 1.215 + sb.setCharAt(i, '_'); 1.216 + } 1.217 + } 1.218 + return sb.toString(); 1.219 + } 1.220 + 1.221 + private File getWSDLDownloadDir() { 1.222 + File wsdlDir = new File(options.destDir,WSDL_PATH); 1.223 + wsdlDir.mkdirs(); 1.224 + return wsdlDir; 1.225 + } 1.226 + 1.227 + private static String WSDL_PATH="META-INF/wsdl"; 1.228 + private static String WSDL_FILE_EXTENSION=".wsdl"; 1.229 + private static String SCHEMA_FILE_EXTENSION=".xsd"; 1.230 +}