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