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