src/share/jaxws_classes/com/sun/tools/internal/ws/wsdl/parser/MetadataFinder.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.wsdl.parser;
ohair@286 27
ohair@286 28 import com.sun.istack.internal.NotNull;
ohair@286 29 import com.sun.istack.internal.Nullable;
ohair@286 30 import com.sun.tools.internal.ws.resources.WscompileMessages;
ohair@286 31 import com.sun.tools.internal.ws.resources.WsdlMessages;
ohair@286 32 import com.sun.tools.internal.ws.wscompile.AbortException;
ohair@286 33 import com.sun.tools.internal.ws.wscompile.ErrorReceiver;
ohair@286 34 import com.sun.tools.internal.ws.wscompile.WsimportOptions;
ohair@286 35 import com.sun.tools.internal.ws.wsdl.document.WSDLConstants;
ohair@286 36 import com.sun.tools.internal.ws.wsdl.document.schema.SchemaConstants;
ohair@286 37 import com.sun.tools.internal.ws.wsdl.framework.ParseException;
ohair@286 38 import com.sun.xml.internal.ws.api.wsdl.parser.MetaDataResolver;
ohair@286 39 import com.sun.xml.internal.ws.api.wsdl.parser.MetadataResolverFactory;
ohair@286 40 import com.sun.xml.internal.ws.api.wsdl.parser.ServiceDescriptor;
ohair@286 41 import com.sun.xml.internal.ws.util.DOMUtil;
ohair@286 42 import com.sun.xml.internal.ws.util.JAXWSUtils;
ohair@286 43 import com.sun.xml.internal.ws.util.ServiceFinder;
ohair@286 44 import org.w3c.dom.Document;
ohair@286 45 import org.w3c.dom.Element;
ohair@286 46 import org.w3c.dom.Node;
ohair@286 47 import org.w3c.dom.NodeList;
ohair@286 48 import org.xml.sax.EntityResolver;
ohair@286 49 import org.xml.sax.InputSource;
ohair@286 50 import org.xml.sax.SAXException;
ohair@286 51 import org.xml.sax.SAXParseException;
ohair@286 52
ohair@286 53 import javax.net.ssl.HostnameVerifier;
ohair@286 54 import javax.net.ssl.HttpsURLConnection;
ohair@286 55 import javax.net.ssl.SSLSession;
ohair@286 56 import javax.xml.transform.Source;
ohair@286 57 import javax.xml.transform.dom.DOMSource;
ohair@286 58 import java.io.FileNotFoundException;
ohair@286 59 import java.io.IOException;
ohair@286 60 import java.io.InputStream;
ohair@286 61 import java.net.*;
ohair@286 62 import java.util.*;
ohair@286 63
ohair@286 64 /**
ohair@286 65 * @author Vivek Pandey
ohair@286 66 */
ohair@286 67 public final class MetadataFinder extends DOMForest{
ohair@286 68
ohair@286 69 public boolean isMexMetadata;
ohair@286 70 private String rootWSDL;
alanb@368 71 private final Set<String> rootWsdls = new HashSet<String>();
ohair@286 72
ohair@286 73 public MetadataFinder(InternalizationLogic logic, WsimportOptions options, ErrorReceiver errReceiver) {
ohair@286 74 super(logic, new WSEntityResolver(options,errReceiver), options, errReceiver);
ohair@286 75
ohair@286 76 }
ohair@286 77
alanb@368 78 @SuppressWarnings("element-type-mismatch")
ohair@286 79 public void parseWSDL(){
ohair@286 80 // parse source grammars
ohair@286 81 for (InputSource value : options.getWSDLs()) {
ohair@286 82 String systemID = value.getSystemId();
ohair@286 83 errorReceiver.pollAbort();
ohair@286 84
alanb@368 85 Document dom;
alanb@368 86 Element doc;
ohair@286 87
ohair@286 88 try {
alanb@368 89 //if there is entity resolver use it
alanb@368 90 if (options.entityResolver != null) {
alanb@368 91 value = options.entityResolver.resolveEntity(null, systemID);
alanb@368 92 }
alanb@368 93 if (value == null) {
alanb@368 94 value = new InputSource(systemID);
alanb@368 95 }
ohair@286 96 dom = parse(value, true);
ohair@286 97
ohair@286 98 doc = dom.getDocumentElement();
ohair@286 99 if (doc == null) {
ohair@286 100 continue;
ohair@286 101 }
ohair@286 102 //if its not a WSDL document, retry with MEX
ohair@286 103 if (doc.getNamespaceURI() == null || !doc.getNamespaceURI().equals(WSDLConstants.NS_WSDL) || !doc.getLocalName().equals("definitions")) {
ohair@286 104 throw new SAXParseException(WsdlMessages.INVALID_WSDL(systemID,
alanb@368 105 com.sun.xml.internal.ws.wsdl.parser.WSDLConstants.QNAME_DEFINITIONS, doc.getNodeName(), locatorTable.getStartLocation(doc).getLineNumber()), locatorTable.getStartLocation(doc));
ohair@286 106 }
alanb@368 107 } catch (FileNotFoundException e) {
ohair@286 108 errorReceiver.error(WsdlMessages.FILE_NOT_FOUND(systemID), e);
ohair@286 109 return;
ohair@286 110 } catch (IOException e) {
ohair@286 111 doc = getFromMetadataResolver(systemID, e);
ohair@286 112 } catch (SAXParseException e) {
ohair@286 113 doc = getFromMetadataResolver(systemID, e);
ohair@286 114 } catch (SAXException e) {
ohair@286 115 doc = getFromMetadataResolver(systemID, e);
ohair@286 116 }
ohair@286 117
ohair@286 118 if (doc == null) {
ohair@286 119 continue;
ohair@286 120 }
ohair@286 121
ohair@286 122 NodeList schemas = doc.getElementsByTagNameNS(SchemaConstants.NS_XSD, "schema");
ohair@286 123 for (int i = 0; i < schemas.getLength(); i++) {
alanb@368 124 if (!inlinedSchemaElements.contains(schemas.item(i))) {
ohair@286 125 inlinedSchemaElements.add((Element) schemas.item(i));
alanb@368 126 }
ohair@286 127 }
ohair@286 128 }
ohair@286 129 identifyRootWsdls();
ohair@286 130 }
ohair@286 131
ohair@286 132 public static class WSEntityResolver implements EntityResolver {
ohair@286 133 WsimportOptions options;
ohair@286 134 ErrorReceiver errorReceiver;
ohair@286 135
alanb@368 136 private URLConnection c = null;
alanb@368 137 private boolean doReset = false;
alanb@368 138
ohair@286 139 public WSEntityResolver(WsimportOptions options, ErrorReceiver errReceiver) {
ohair@286 140 this.options = options;
ohair@286 141 this.errorReceiver = errReceiver;
ohair@286 142 }
ohair@286 143
alanb@368 144 @Override
ohair@286 145 public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
ohair@286 146 InputSource inputSource = null;
ohair@286 147
ohair@286 148 if(options.entityResolver != null ) {
ohair@286 149 inputSource = options.entityResolver.resolveEntity(null, systemId);
ohair@286 150 }
ohair@286 151 if (inputSource == null) {
ohair@286 152 inputSource = new InputSource(systemId);
ohair@286 153 InputStream is = null;
ohair@286 154 int redirects = 0;
ohair@286 155 boolean redirect;
ohair@286 156 URL url = JAXWSUtils.getFileOrURL(inputSource.getSystemId());
ohair@286 157 URLConnection conn = url.openConnection();
ohair@286 158 do {
ohair@286 159 if (conn instanceof HttpsURLConnection) {
ohair@286 160 if (options.disableSSLHostnameVerification) {
ohair@286 161 ((HttpsURLConnection) conn).setHostnameVerifier(new HttpClientVerifier());
ohair@286 162 }
ohair@286 163 }
ohair@286 164 redirect = false;
ohair@286 165 if (conn instanceof HttpURLConnection) {
ohair@286 166 ((HttpURLConnection) conn).setInstanceFollowRedirects(false);
ohair@286 167 }
ohair@286 168
alanb@368 169 if (conn instanceof JarURLConnection) {
alanb@368 170 if (conn.getUseCaches()) {
alanb@368 171 doReset = true;
alanb@368 172 conn.setDefaultUseCaches(false);
alanb@368 173 c = conn;
alanb@368 174 }
alanb@368 175 }
alanb@368 176
ohair@286 177 try {
ohair@286 178 is = conn.getInputStream();
ohair@286 179 //is = sun.net.www.protocol.http.HttpURLConnection.openConnectionCheckRedirects(conn);
ohair@286 180 } catch (IOException e) {
ohair@286 181 if (conn instanceof HttpURLConnection) {
ohair@286 182 HttpURLConnection httpConn = ((HttpURLConnection) conn);
ohair@286 183 int code = httpConn.getResponseCode();
ohair@286 184 if (code == 401) {
ohair@286 185 errorReceiver.error(new SAXParseException(WscompileMessages.WSIMPORT_AUTH_INFO_NEEDED(e.getMessage(),
alanb@368 186 systemId, WsimportOptions.defaultAuthfile), null, e));
ohair@286 187 throw new AbortException();
ohair@286 188 }
ohair@286 189 //FOR other code we will retry with MEX
ohair@286 190 }
ohair@286 191 throw e;
ohair@286 192 }
ohair@286 193
ohair@286 194 //handle 302 or 303, JDK does not seem to handle 302 very well.
ohair@286 195 //Need to redesign this a bit as we need to throw better error message for IOException in this case
ohair@286 196 if (conn instanceof HttpURLConnection) {
ohair@286 197 HttpURLConnection httpConn = ((HttpURLConnection) conn);
ohair@286 198 int code = httpConn.getResponseCode();
ohair@286 199 if (code == 302 || code == 303) {
ohair@286 200 //retry with the value in Location header
ohair@286 201 List<String> seeOther = httpConn.getHeaderFields().get("Location");
ohair@286 202 if (seeOther != null && seeOther.size() > 0) {
ohair@286 203 URL newurl = new URL(url, seeOther.get(0));
ohair@286 204 if (!newurl.equals(url)) {
ohair@286 205 errorReceiver.info(new SAXParseException(WscompileMessages.WSIMPORT_HTTP_REDIRECT(code, seeOther.get(0)), null));
ohair@286 206 url = newurl;
ohair@286 207 httpConn.disconnect();
ohair@286 208 if (redirects >= 5) {
ohair@286 209 errorReceiver.error(new SAXParseException(WscompileMessages.WSIMPORT_MAX_REDIRECT_ATTEMPT(), null));
ohair@286 210 throw new AbortException();
ohair@286 211 }
ohair@286 212 conn = url.openConnection();
ohair@286 213 inputSource.setSystemId(url.toExternalForm());
ohair@286 214 redirects++;
ohair@286 215 redirect = true;
ohair@286 216 }
ohair@286 217 }
ohair@286 218 }
ohair@286 219 }
ohair@286 220 } while (redirect);
ohair@286 221 inputSource.setByteStream(is);
ohair@286 222 }
ohair@286 223
ohair@286 224 return inputSource;
ohair@286 225 }
ohair@286 226
alanb@368 227 @Override
alanb@368 228 protected void finalize() throws Throwable {
alanb@368 229 //see http://java.net/jira/browse/JAX_WS-1087
alanb@368 230 if (doReset) {
alanb@368 231 c.setDefaultUseCaches(true);
alanb@368 232 }
alanb@368 233 }
ohair@286 234 }
ohair@286 235
ohair@286 236 // overide default SSL HttpClientVerifier to always return true
ohair@286 237 // effectively overiding Hostname client verification when using SSL
ohair@286 238 private static class HttpClientVerifier implements HostnameVerifier {
alanb@368 239 @Override
ohair@286 240 public boolean verify(String s, SSLSession sslSession) {
ohair@286 241 return true;
ohair@286 242 }
ohair@286 243 }
ohair@286 244
ohair@286 245 /**
ohair@286 246 * Gives the root wsdl document systemId. A root wsdl document is the one which has wsdl:service.
ohair@286 247 * @return null if there is no root wsdl
ohair@286 248 */
ohair@286 249 public @Nullable
ohair@286 250 String getRootWSDL(){
ohair@286 251 return rootWSDL;
ohair@286 252 }
ohair@286 253
ohair@286 254 /**
ohair@286 255 * Gives all the WSDL documents.
ohair@286 256 */
ohair@286 257 public @NotNull
ohair@286 258 Set<String> getRootWSDLs(){
ohair@286 259 return rootWsdls;
ohair@286 260 }
ohair@286 261
ohair@286 262
ohair@286 263 /**
ohair@286 264 * Identifies WSDL documents from the {@link DOMForest}. Also identifies the root wsdl document.
ohair@286 265 */
ohair@286 266 private void identifyRootWsdls(){
ohair@286 267 for(String location: rootDocuments){
ohair@286 268 Document doc = get(location);
ohair@286 269 if(doc!=null){
ohair@286 270 Element definition = doc.getDocumentElement();
ohair@286 271 if(definition == null || definition.getLocalName() == null || definition.getNamespaceURI() == null)
ohair@286 272 continue;
ohair@286 273 if(definition.getNamespaceURI().equals(WSDLConstants.NS_WSDL) && definition.getLocalName().equals("definitions")){
ohair@286 274 rootWsdls.add(location);
ohair@286 275 //set the root wsdl at this point. Root wsdl is one which has wsdl:service in it
ohair@286 276 NodeList nl = definition.getElementsByTagNameNS(WSDLConstants.NS_WSDL, "service");
ohair@286 277
ohair@286 278 //TODO:what if there are more than one wsdl with wsdl:service element. Probably such cases
ohair@286 279 //are rare and we will take any one of them, this logic should still work
ohair@286 280 if(nl.getLength() > 0)
ohair@286 281 rootWSDL = location;
ohair@286 282 }
ohair@286 283 }
ohair@286 284 }
ohair@286 285 //no wsdl with wsdl:service found, throw error
ohair@286 286 if(rootWSDL == null){
alanb@368 287 StringBuilder strbuf = new StringBuilder();
ohair@286 288 for(String str : rootWsdls){
ohair@286 289 strbuf.append(str);
ohair@286 290 strbuf.append('\n');
ohair@286 291 }
ohair@286 292 errorReceiver.error(null, WsdlMessages.FAILED_NOSERVICE(strbuf.toString()));
ohair@286 293 }
ohair@286 294 }
ohair@286 295
ohair@286 296 /*
ohair@286 297 * If source and target namespace are also passed in,
ohair@286 298 * then if the mex resolver is found and it cannot get
ohair@286 299 * the data, wsimport attempts to add ?wsdl to the
ohair@286 300 * address and retrieve the data with a normal http get.
ohair@286 301 * This behavior should only happen when trying a
ohair@286 302 * mex request first.
ohair@286 303 */
ohair@286 304 private @Nullable Element getFromMetadataResolver(String systemId, Exception ex) {
ohair@286 305 //try MEX
ohair@286 306 MetaDataResolver resolver;
ohair@286 307 ServiceDescriptor serviceDescriptor = null;
ohair@286 308 for (MetadataResolverFactory resolverFactory : ServiceFinder.find(MetadataResolverFactory.class)) {
ohair@286 309 resolver = resolverFactory.metadataResolver(options.entityResolver);
ohair@286 310 try {
ohair@286 311 serviceDescriptor = resolver.resolve(new URI(systemId));
ohair@286 312 //we got the ServiceDescriptor, now break
ohair@286 313 if (serviceDescriptor != null)
ohair@286 314 break;
ohair@286 315 } catch (URISyntaxException e) {
ohair@286 316 throw new ParseException(e);
ohair@286 317 }
ohair@286 318 }
ohair@286 319
ohair@286 320 if (serviceDescriptor != null) {
ohair@286 321 errorReceiver.warning(new SAXParseException(WsdlMessages.TRY_WITH_MEX(ex.getMessage()), null, ex));
ohair@286 322 return parseMetadata(systemId, serviceDescriptor);
ohair@286 323 } else {
ohair@286 324 errorReceiver.error(null, WsdlMessages.PARSING_UNABLE_TO_GET_METADATA(ex.getMessage(), WscompileMessages.WSIMPORT_NO_WSDL(systemId)), ex);
ohair@286 325 }
ohair@286 326 return null;
ohair@286 327 }
ohair@286 328
ohair@286 329 private Element parseMetadata(@NotNull String systemId, @NotNull ServiceDescriptor serviceDescriptor) {
ohair@286 330 List<? extends Source> mexWsdls = serviceDescriptor.getWSDLs();
ohair@286 331 List<? extends Source> mexSchemas = serviceDescriptor.getSchemas();
ohair@286 332 Document root = null;
ohair@286 333 for (Source src : mexWsdls) {
ohair@286 334 if (src instanceof DOMSource) {
ohair@286 335 Node n = ((DOMSource) src).getNode();
ohair@286 336 Document doc;
ohair@286 337 if (n.getNodeType() == Node.ELEMENT_NODE && n.getOwnerDocument() == null) {
ohair@286 338 doc = DOMUtil.createDom();
ohair@286 339 doc.importNode(n, true);
ohair@286 340 } else {
ohair@286 341 doc = n.getOwnerDocument();
ohair@286 342 }
ohair@286 343
ohair@286 344 // Element e = (n.getNodeType() == Node.ELEMENT_NODE)?(Element)n: DOMUtil.getFirstElementChild(n);
ohair@286 345 if (root == null) {
ohair@286 346 //check if its main wsdl, then set it to root
ohair@286 347 NodeList nl = doc.getDocumentElement().getElementsByTagNameNS(WSDLConstants.NS_WSDL, "service");
ohair@286 348 if (nl.getLength() > 0) {
ohair@286 349 root = doc;
ohair@286 350 rootWSDL = src.getSystemId();
ohair@286 351 }
ohair@286 352 }
ohair@286 353 NodeList nl = doc.getDocumentElement().getElementsByTagNameNS(WSDLConstants.NS_WSDL, "import");
ohair@286 354 for(int i = 0; i < nl.getLength(); i++){
ohair@286 355 Element imp = (Element) nl.item(i);
ohair@286 356 String loc = imp.getAttribute("location");
ohair@286 357 if (loc != null) {
ohair@286 358 if (!externalReferences.contains(loc))
ohair@286 359 externalReferences.add(loc);
ohair@286 360 }
ohair@286 361 }
ohair@286 362 if (core.keySet().contains(systemId))
ohair@286 363 core.remove(systemId);
ohair@286 364 core.put(src.getSystemId(), doc);
ohair@286 365 resolvedCache.put(systemId, doc.getDocumentURI());
ohair@286 366 isMexMetadata = true;
ohair@286 367 }
ohair@286 368
ohair@286 369 //TODO:handle SAXSource
ohair@286 370 //TODO:handler StreamSource
ohair@286 371 }
ohair@286 372
ohair@286 373 for (Source src : mexSchemas) {
ohair@286 374 if (src instanceof DOMSource) {
ohair@286 375 Node n = ((DOMSource) src).getNode();
ohair@286 376 Element e = (n.getNodeType() == Node.ELEMENT_NODE) ? (Element) n : DOMUtil.getFirstElementChild(n);
ohair@286 377 inlinedSchemaElements.add(e);
ohair@286 378 }
ohair@286 379 //TODO:handle SAXSource
ohair@286 380 //TODO:handler StreamSource
ohair@286 381 }
ohair@286 382 return root.getDocumentElement();
ohair@286 383 }
ohair@286 384 }

mercurial