ohair@286: /* alanb@368: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. ohair@286: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ohair@286: * ohair@286: * This code is free software; you can redistribute it and/or modify it ohair@286: * under the terms of the GNU General Public License version 2 only, as ohair@286: * published by the Free Software Foundation. Oracle designates this ohair@286: * particular file as subject to the "Classpath" exception as provided ohair@286: * by Oracle in the LICENSE file that accompanied this code. ohair@286: * ohair@286: * This code is distributed in the hope that it will be useful, but WITHOUT ohair@286: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ohair@286: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ohair@286: * version 2 for more details (a copy is included in the LICENSE file that ohair@286: * accompanied this code). ohair@286: * ohair@286: * You should have received a copy of the GNU General Public License version ohair@286: * 2 along with this work; if not, write to the Free Software Foundation, ohair@286: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ohair@286: * ohair@286: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@286: * or visit www.oracle.com if you need additional information or have any ohair@286: * questions. ohair@286: */ ohair@286: ohair@286: package com.sun.tools.internal.ws.wsdl.parser; ohair@286: ohair@286: import com.sun.istack.internal.NotNull; ohair@286: import com.sun.istack.internal.Nullable; ohair@286: import com.sun.tools.internal.ws.resources.WscompileMessages; ohair@286: import com.sun.tools.internal.ws.resources.WsdlMessages; ohair@286: import com.sun.tools.internal.ws.wscompile.AbortException; ohair@286: import com.sun.tools.internal.ws.wscompile.ErrorReceiver; ohair@286: import com.sun.tools.internal.ws.wscompile.WsimportOptions; ohair@286: import com.sun.tools.internal.ws.wsdl.document.WSDLConstants; ohair@286: import com.sun.tools.internal.ws.wsdl.document.schema.SchemaConstants; ohair@286: import com.sun.tools.internal.ws.wsdl.framework.ParseException; ohair@286: import com.sun.xml.internal.ws.api.wsdl.parser.MetaDataResolver; ohair@286: import com.sun.xml.internal.ws.api.wsdl.parser.MetadataResolverFactory; ohair@286: import com.sun.xml.internal.ws.api.wsdl.parser.ServiceDescriptor; ohair@286: import com.sun.xml.internal.ws.util.DOMUtil; ohair@286: import com.sun.xml.internal.ws.util.JAXWSUtils; ohair@286: import com.sun.xml.internal.ws.util.ServiceFinder; ohair@286: import org.w3c.dom.Document; ohair@286: import org.w3c.dom.Element; ohair@286: import org.w3c.dom.Node; ohair@286: import org.w3c.dom.NodeList; ohair@286: import org.xml.sax.EntityResolver; ohair@286: import org.xml.sax.InputSource; ohair@286: import org.xml.sax.SAXException; ohair@286: import org.xml.sax.SAXParseException; ohair@286: ohair@286: import javax.net.ssl.HostnameVerifier; ohair@286: import javax.net.ssl.HttpsURLConnection; ohair@286: import javax.net.ssl.SSLSession; ohair@286: import javax.xml.transform.Source; ohair@286: import javax.xml.transform.dom.DOMSource; ohair@286: import java.io.FileNotFoundException; ohair@286: import java.io.IOException; ohair@286: import java.io.InputStream; ohair@286: import java.net.*; ohair@286: import java.util.*; ohair@286: ohair@286: /** ohair@286: * @author Vivek Pandey ohair@286: */ ohair@286: public final class MetadataFinder extends DOMForest{ ohair@286: ohair@286: public boolean isMexMetadata; ohair@286: private String rootWSDL; alanb@368: private final Set rootWsdls = new HashSet(); ohair@286: ohair@286: public MetadataFinder(InternalizationLogic logic, WsimportOptions options, ErrorReceiver errReceiver) { ohair@286: super(logic, new WSEntityResolver(options,errReceiver), options, errReceiver); ohair@286: ohair@286: } ohair@286: alanb@368: @SuppressWarnings("element-type-mismatch") ohair@286: public void parseWSDL(){ ohair@286: // parse source grammars ohair@286: for (InputSource value : options.getWSDLs()) { ohair@286: String systemID = value.getSystemId(); ohair@286: errorReceiver.pollAbort(); ohair@286: alanb@368: Document dom; alanb@368: Element doc; ohair@286: ohair@286: try { alanb@368: //if there is entity resolver use it alanb@368: if (options.entityResolver != null) { alanb@368: value = options.entityResolver.resolveEntity(null, systemID); alanb@368: } alanb@368: if (value == null) { alanb@368: value = new InputSource(systemID); alanb@368: } ohair@286: dom = parse(value, true); ohair@286: ohair@286: doc = dom.getDocumentElement(); ohair@286: if (doc == null) { ohair@286: continue; ohair@286: } ohair@286: //if its not a WSDL document, retry with MEX ohair@286: if (doc.getNamespaceURI() == null || !doc.getNamespaceURI().equals(WSDLConstants.NS_WSDL) || !doc.getLocalName().equals("definitions")) { ohair@286: throw new SAXParseException(WsdlMessages.INVALID_WSDL(systemID, alanb@368: com.sun.xml.internal.ws.wsdl.parser.WSDLConstants.QNAME_DEFINITIONS, doc.getNodeName(), locatorTable.getStartLocation(doc).getLineNumber()), locatorTable.getStartLocation(doc)); ohair@286: } alanb@368: } catch (FileNotFoundException e) { ohair@286: errorReceiver.error(WsdlMessages.FILE_NOT_FOUND(systemID), e); ohair@286: return; ohair@286: } catch (IOException e) { ohair@286: doc = getFromMetadataResolver(systemID, e); ohair@286: } catch (SAXParseException e) { ohair@286: doc = getFromMetadataResolver(systemID, e); ohair@286: } catch (SAXException e) { ohair@286: doc = getFromMetadataResolver(systemID, e); ohair@286: } ohair@286: ohair@286: if (doc == null) { ohair@286: continue; ohair@286: } ohair@286: ohair@286: NodeList schemas = doc.getElementsByTagNameNS(SchemaConstants.NS_XSD, "schema"); ohair@286: for (int i = 0; i < schemas.getLength(); i++) { alanb@368: if (!inlinedSchemaElements.contains(schemas.item(i))) { ohair@286: inlinedSchemaElements.add((Element) schemas.item(i)); alanb@368: } ohair@286: } ohair@286: } ohair@286: identifyRootWsdls(); ohair@286: } ohair@286: ohair@286: public static class WSEntityResolver implements EntityResolver { ohair@286: WsimportOptions options; ohair@286: ErrorReceiver errorReceiver; ohair@286: alanb@368: private URLConnection c = null; alanb@368: private boolean doReset = false; alanb@368: ohair@286: public WSEntityResolver(WsimportOptions options, ErrorReceiver errReceiver) { ohair@286: this.options = options; ohair@286: this.errorReceiver = errReceiver; ohair@286: } ohair@286: alanb@368: @Override ohair@286: public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { ohair@286: InputSource inputSource = null; ohair@286: ohair@286: if(options.entityResolver != null ) { ohair@286: inputSource = options.entityResolver.resolveEntity(null, systemId); ohair@286: } ohair@286: if (inputSource == null) { ohair@286: inputSource = new InputSource(systemId); ohair@286: InputStream is = null; ohair@286: int redirects = 0; ohair@286: boolean redirect; ohair@286: URL url = JAXWSUtils.getFileOrURL(inputSource.getSystemId()); ohair@286: URLConnection conn = url.openConnection(); ohair@286: do { ohair@286: if (conn instanceof HttpsURLConnection) { ohair@286: if (options.disableSSLHostnameVerification) { ohair@286: ((HttpsURLConnection) conn).setHostnameVerifier(new HttpClientVerifier()); ohair@286: } ohair@286: } ohair@286: redirect = false; ohair@286: if (conn instanceof HttpURLConnection) { ohair@286: ((HttpURLConnection) conn).setInstanceFollowRedirects(false); ohair@286: } ohair@286: alanb@368: if (conn instanceof JarURLConnection) { alanb@368: if (conn.getUseCaches()) { alanb@368: doReset = true; alanb@368: conn.setDefaultUseCaches(false); alanb@368: c = conn; alanb@368: } alanb@368: } alanb@368: ohair@286: try { ohair@286: is = conn.getInputStream(); ohair@286: //is = sun.net.www.protocol.http.HttpURLConnection.openConnectionCheckRedirects(conn); ohair@286: } catch (IOException e) { ohair@286: if (conn instanceof HttpURLConnection) { ohair@286: HttpURLConnection httpConn = ((HttpURLConnection) conn); ohair@286: int code = httpConn.getResponseCode(); ohair@286: if (code == 401) { ohair@286: errorReceiver.error(new SAXParseException(WscompileMessages.WSIMPORT_AUTH_INFO_NEEDED(e.getMessage(), alanb@368: systemId, WsimportOptions.defaultAuthfile), null, e)); ohair@286: throw new AbortException(); ohair@286: } ohair@286: //FOR other code we will retry with MEX ohair@286: } ohair@286: throw e; ohair@286: } ohair@286: ohair@286: //handle 302 or 303, JDK does not seem to handle 302 very well. ohair@286: //Need to redesign this a bit as we need to throw better error message for IOException in this case ohair@286: if (conn instanceof HttpURLConnection) { ohair@286: HttpURLConnection httpConn = ((HttpURLConnection) conn); ohair@286: int code = httpConn.getResponseCode(); ohair@286: if (code == 302 || code == 303) { ohair@286: //retry with the value in Location header ohair@286: List seeOther = httpConn.getHeaderFields().get("Location"); ohair@286: if (seeOther != null && seeOther.size() > 0) { ohair@286: URL newurl = new URL(url, seeOther.get(0)); ohair@286: if (!newurl.equals(url)) { ohair@286: errorReceiver.info(new SAXParseException(WscompileMessages.WSIMPORT_HTTP_REDIRECT(code, seeOther.get(0)), null)); ohair@286: url = newurl; ohair@286: httpConn.disconnect(); ohair@286: if (redirects >= 5) { ohair@286: errorReceiver.error(new SAXParseException(WscompileMessages.WSIMPORT_MAX_REDIRECT_ATTEMPT(), null)); ohair@286: throw new AbortException(); ohair@286: } ohair@286: conn = url.openConnection(); ohair@286: inputSource.setSystemId(url.toExternalForm()); ohair@286: redirects++; ohair@286: redirect = true; ohair@286: } ohair@286: } ohair@286: } ohair@286: } ohair@286: } while (redirect); ohair@286: inputSource.setByteStream(is); ohair@286: } ohair@286: ohair@286: return inputSource; ohair@286: } ohair@286: alanb@368: @Override alanb@368: protected void finalize() throws Throwable { alanb@368: //see http://java.net/jira/browse/JAX_WS-1087 alanb@368: if (doReset) { alanb@368: c.setDefaultUseCaches(true); alanb@368: } alanb@368: } ohair@286: } ohair@286: ohair@286: // overide default SSL HttpClientVerifier to always return true ohair@286: // effectively overiding Hostname client verification when using SSL ohair@286: private static class HttpClientVerifier implements HostnameVerifier { alanb@368: @Override ohair@286: public boolean verify(String s, SSLSession sslSession) { ohair@286: return true; ohair@286: } ohair@286: } ohair@286: ohair@286: /** ohair@286: * Gives the root wsdl document systemId. A root wsdl document is the one which has wsdl:service. ohair@286: * @return null if there is no root wsdl ohair@286: */ ohair@286: public @Nullable ohair@286: String getRootWSDL(){ ohair@286: return rootWSDL; ohair@286: } ohair@286: ohair@286: /** ohair@286: * Gives all the WSDL documents. ohair@286: */ ohair@286: public @NotNull ohair@286: Set getRootWSDLs(){ ohair@286: return rootWsdls; ohair@286: } ohair@286: ohair@286: ohair@286: /** ohair@286: * Identifies WSDL documents from the {@link DOMForest}. Also identifies the root wsdl document. ohair@286: */ ohair@286: private void identifyRootWsdls(){ ohair@286: for(String location: rootDocuments){ ohair@286: Document doc = get(location); ohair@286: if(doc!=null){ ohair@286: Element definition = doc.getDocumentElement(); ohair@286: if(definition == null || definition.getLocalName() == null || definition.getNamespaceURI() == null) ohair@286: continue; ohair@286: if(definition.getNamespaceURI().equals(WSDLConstants.NS_WSDL) && definition.getLocalName().equals("definitions")){ ohair@286: rootWsdls.add(location); ohair@286: //set the root wsdl at this point. Root wsdl is one which has wsdl:service in it ohair@286: NodeList nl = definition.getElementsByTagNameNS(WSDLConstants.NS_WSDL, "service"); ohair@286: ohair@286: //TODO:what if there are more than one wsdl with wsdl:service element. Probably such cases ohair@286: //are rare and we will take any one of them, this logic should still work ohair@286: if(nl.getLength() > 0) ohair@286: rootWSDL = location; ohair@286: } ohair@286: } ohair@286: } ohair@286: //no wsdl with wsdl:service found, throw error ohair@286: if(rootWSDL == null){ alanb@368: StringBuilder strbuf = new StringBuilder(); ohair@286: for(String str : rootWsdls){ ohair@286: strbuf.append(str); ohair@286: strbuf.append('\n'); ohair@286: } ohair@286: errorReceiver.error(null, WsdlMessages.FAILED_NOSERVICE(strbuf.toString())); ohair@286: } ohair@286: } ohair@286: ohair@286: /* ohair@286: * If source and target namespace are also passed in, ohair@286: * then if the mex resolver is found and it cannot get ohair@286: * the data, wsimport attempts to add ?wsdl to the ohair@286: * address and retrieve the data with a normal http get. ohair@286: * This behavior should only happen when trying a ohair@286: * mex request first. ohair@286: */ ohair@286: private @Nullable Element getFromMetadataResolver(String systemId, Exception ex) { ohair@286: //try MEX ohair@286: MetaDataResolver resolver; ohair@286: ServiceDescriptor serviceDescriptor = null; ohair@286: for (MetadataResolverFactory resolverFactory : ServiceFinder.find(MetadataResolverFactory.class)) { ohair@286: resolver = resolverFactory.metadataResolver(options.entityResolver); ohair@286: try { ohair@286: serviceDescriptor = resolver.resolve(new URI(systemId)); ohair@286: //we got the ServiceDescriptor, now break ohair@286: if (serviceDescriptor != null) ohair@286: break; ohair@286: } catch (URISyntaxException e) { ohair@286: throw new ParseException(e); ohair@286: } ohair@286: } ohair@286: ohair@286: if (serviceDescriptor != null) { ohair@286: errorReceiver.warning(new SAXParseException(WsdlMessages.TRY_WITH_MEX(ex.getMessage()), null, ex)); ohair@286: return parseMetadata(systemId, serviceDescriptor); ohair@286: } else { ohair@286: errorReceiver.error(null, WsdlMessages.PARSING_UNABLE_TO_GET_METADATA(ex.getMessage(), WscompileMessages.WSIMPORT_NO_WSDL(systemId)), ex); ohair@286: } ohair@286: return null; ohair@286: } ohair@286: ohair@286: private Element parseMetadata(@NotNull String systemId, @NotNull ServiceDescriptor serviceDescriptor) { ohair@286: List mexWsdls = serviceDescriptor.getWSDLs(); ohair@286: List mexSchemas = serviceDescriptor.getSchemas(); ohair@286: Document root = null; ohair@286: for (Source src : mexWsdls) { ohair@286: if (src instanceof DOMSource) { ohair@286: Node n = ((DOMSource) src).getNode(); ohair@286: Document doc; ohair@286: if (n.getNodeType() == Node.ELEMENT_NODE && n.getOwnerDocument() == null) { ohair@286: doc = DOMUtil.createDom(); ohair@286: doc.importNode(n, true); ohair@286: } else { ohair@286: doc = n.getOwnerDocument(); ohair@286: } ohair@286: ohair@286: // Element e = (n.getNodeType() == Node.ELEMENT_NODE)?(Element)n: DOMUtil.getFirstElementChild(n); ohair@286: if (root == null) { ohair@286: //check if its main wsdl, then set it to root ohair@286: NodeList nl = doc.getDocumentElement().getElementsByTagNameNS(WSDLConstants.NS_WSDL, "service"); ohair@286: if (nl.getLength() > 0) { ohair@286: root = doc; ohair@286: rootWSDL = src.getSystemId(); ohair@286: } ohair@286: } ohair@286: NodeList nl = doc.getDocumentElement().getElementsByTagNameNS(WSDLConstants.NS_WSDL, "import"); ohair@286: for(int i = 0; i < nl.getLength(); i++){ ohair@286: Element imp = (Element) nl.item(i); ohair@286: String loc = imp.getAttribute("location"); ohair@286: if (loc != null) { ohair@286: if (!externalReferences.contains(loc)) ohair@286: externalReferences.add(loc); ohair@286: } ohair@286: } ohair@286: if (core.keySet().contains(systemId)) ohair@286: core.remove(systemId); ohair@286: core.put(src.getSystemId(), doc); ohair@286: resolvedCache.put(systemId, doc.getDocumentURI()); ohair@286: isMexMetadata = true; ohair@286: } ohair@286: ohair@286: //TODO:handle SAXSource ohair@286: //TODO:handler StreamSource ohair@286: } ohair@286: ohair@286: for (Source src : mexSchemas) { ohair@286: if (src instanceof DOMSource) { ohair@286: Node n = ((DOMSource) src).getNode(); ohair@286: Element e = (n.getNodeType() == Node.ELEMENT_NODE) ? (Element) n : DOMUtil.getFirstElementChild(n); ohair@286: inlinedSchemaElements.add(e); ohair@286: } ohair@286: //TODO:handle SAXSource ohair@286: //TODO:handler StreamSource ohair@286: } ohair@286: return root.getDocumentElement(); ohair@286: } ohair@286: }