Exemple #1
0
  /**
   * Determines the target node of the "bindings" element by using the inherited target node, then
   * put the result into the "result" map.
   */
  private void buildTargetNodeMap(
      Element bindings, Node inheritedTarget, Map<Element, Node> result) {
    // start by the inherited target
    Node target = inheritedTarget;

    validate(bindings); // validate this node

    // look for @wsdlLocation
    if (isTopLevelBinding(bindings)) {
      String wsdlLocation;
      if (bindings.getAttributeNode("wsdlLocation") != null) {
        wsdlLocation = bindings.getAttribute("wsdlLocation");

        try {
          // absolutize this URI.
          // TODO: use the URI class
          // TODO: honor xml:base
          wsdlLocation =
              new URL(new URL(forest.getSystemId(bindings.getOwnerDocument())), wsdlLocation)
                  .toExternalForm();
        } catch (MalformedURLException e) {
          wsdlLocation = JAXWSUtils.absolutize(JAXWSUtils.getFileOrURLName(wsdlLocation));
        }
      } else {
        // the node does not have
        wsdlLocation = forest.getFirstRootDocument();
      }
      target = forest.get(wsdlLocation);

      if (target == null) {
        reportError(
            bindings,
            WsdlMessages.INTERNALIZER_INCORRECT_SCHEMA_REFERENCE(
                wsdlLocation, EditDistance.findNearest(wsdlLocation, forest.listSystemIDs())));
        return; // abort processing this <JAXWS:bindings>
      }
    }

    // if the target node is xs:schema, declare the jaxb version on it as latter on it will be
    // required by the inlined schema bindings

    Element element = DOMUtil.getFirstElementChild(target);
    if (element != null
        && element.getNamespaceURI().equals(Constants.NS_WSDL)
        && element.getLocalName().equals("definitions")) {
      // get all schema elements
      Element type = DOMUtils.getFirstChildElement(element, Constants.NS_WSDL, "types");
      if (type != null) {
        for (Element schemaElement : DOMUtils.getChildElements(type, Constants.NS_XSD, "schema")) {
          if (!schemaElement.hasAttributeNS(Constants.NS_XMLNS, "jaxb")) {
            schemaElement.setAttributeNS(
                Constants.NS_XMLNS, "xmlns:jaxb", JAXWSBindingsConstants.NS_JAXB_BINDINGS);
          }

          // add jaxb:bindings version info. Lets put it to 1.0, may need to change latter
          if (!schemaElement.hasAttributeNS(JAXWSBindingsConstants.NS_JAXB_BINDINGS, "version")) {
            schemaElement.setAttributeNS(
                JAXWSBindingsConstants.NS_JAXB_BINDINGS,
                "jaxb:version",
                JAXWSBindingsConstants.JAXB_BINDING_VERSION);
          }
        }
      }
    }

    boolean hasNode = true;
    if ((isJAXWSBindings(bindings) || isJAXBBindings(bindings))
        && bindings.getAttributeNode("node") != null) {
      target =
          evaluateXPathNode(
              bindings, target, bindings.getAttribute("node"), new NamespaceContextImpl(bindings));
    } else if (isJAXWSBindings(bindings)
        && (bindings.getAttributeNode("node") == null)
        && !isTopLevelBinding(bindings)) {
      hasNode = false;
    } else if (isGlobalBinding(bindings)
        && !isWSDLDefinition(target)
        && isTopLevelBinding(bindings.getParentNode())) {
      target = getWSDLDefintionNode(bindings, target);
    }

    // if target is null it means the xpath evaluation has some problem,
    // just return
    if (target == null) return;

    // update the result map
    if (hasNode) result.put(bindings, target);

    // look for child <JAXWS:bindings> and process them recursively
    Element[] children = getChildElements(bindings);
    for (Element child : children) buildTargetNodeMap(child, target, result);
  }
    @Override
    public InputSource resolveEntity(String publicId, String systemId)
        throws SAXException, IOException {
      InputSource inputSource = null;

      if (options.entityResolver != null) {
        inputSource = options.entityResolver.resolveEntity(null, systemId);
      }
      if (inputSource == null) {
        inputSource = new InputSource(systemId);
        InputStream is = null;
        int redirects = 0;
        boolean redirect;
        URL url = JAXWSUtils.getFileOrURL(inputSource.getSystemId());
        URLConnection conn = url.openConnection();
        do {
          if (conn instanceof HttpsURLConnection) {
            if (options.disableSSLHostnameVerification) {
              ((HttpsURLConnection) conn).setHostnameVerifier(new HttpClientVerifier());
            }
          }
          redirect = false;
          if (conn instanceof HttpURLConnection) {
            ((HttpURLConnection) conn).setInstanceFollowRedirects(false);
          }

          if (conn instanceof JarURLConnection) {
            if (conn.getUseCaches()) {
              doReset = true;
              conn.setDefaultUseCaches(false);
              c = conn;
            }
          }

          try {
            is = conn.getInputStream();
            // is = sun.net.www.protocol.http.HttpURLConnection.openConnectionCheckRedirects(conn);
          } catch (IOException e) {
            if (conn instanceof HttpURLConnection) {
              HttpURLConnection httpConn = ((HttpURLConnection) conn);
              int code = httpConn.getResponseCode();
              if (code == 401) {
                errorReceiver.error(
                    new SAXParseException(
                        WscompileMessages.WSIMPORT_AUTH_INFO_NEEDED(
                            e.getMessage(), systemId, WsimportOptions.defaultAuthfile),
                        null,
                        e));
                throw new AbortException();
              }
              // FOR other code we will retry with MEX
            }
            throw e;
          }

          // handle 302 or 303, JDK does not seem to handle 302 very well.
          // Need to redesign this a bit as we need to throw better error message for IOException in
          // this case
          if (conn instanceof HttpURLConnection) {
            HttpURLConnection httpConn = ((HttpURLConnection) conn);
            int code = httpConn.getResponseCode();
            if (code == 302 || code == 303) {
              // retry with the value in Location header
              List<String> seeOther = httpConn.getHeaderFields().get("Location");
              if (seeOther != null && seeOther.size() > 0) {
                URL newurl = new URL(url, seeOther.get(0));
                if (!newurl.equals(url)) {
                  errorReceiver.info(
                      new SAXParseException(
                          WscompileMessages.WSIMPORT_HTTP_REDIRECT(code, seeOther.get(0)), null));
                  url = newurl;
                  httpConn.disconnect();
                  if (redirects >= 5) {
                    errorReceiver.error(
                        new SAXParseException(
                            WscompileMessages.WSIMPORT_MAX_REDIRECT_ATTEMPT(), null));
                    throw new AbortException();
                  }
                  conn = url.openConnection();
                  inputSource.setSystemId(url.toExternalForm());
                  redirects++;
                  redirect = true;
                }
              }
            }
          }
        } while (redirect);
        inputSource.setByteStream(is);
      }

      return inputSource;
    }