/**
   * Convert from EndpointReference to CXF internal 2005/08 EndpointReferenceType
   *
   * @param external the javax.xml.ws.EndpointReference
   * @return CXF internal 2005/08 EndpointReferenceType
   */
  public static EndpointReferenceType convertToInternal(EndpointReference external) {
    if (external instanceof W3CEndpointReference) {

      try {
        Document doc = XMLUtils.newDocument();
        DOMResult result = new DOMResult(doc);
        external.writeTo(result);
        W3CDOMStreamReader reader = new W3CDOMStreamReader(doc.getDocumentElement());

        // CXF internal 2005/08 EndpointReferenceType should be
        // compatible with W3CEndpointReference
        // jaxContext = ContextUtils.getJAXBContext();
        JAXBContext context =
            JAXBContext.newInstance(new Class[] {org.apache.cxf.ws.addressing.ObjectFactory.class});
        EndpointReferenceType internal =
            context.createUnmarshaller().unmarshal(reader, EndpointReferenceType.class).getValue();
        return internal;
      } catch (JAXBException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      return null;
    } else {
      // TODO: 200408
    }
    return null;
  }
 @Override
 public Document convert(byte[] source) {
   try {
     return XMLUtils.parse(source);
   } catch (Exception e) {
     throw new IllegalArgumentException(e);
   }
 }
  @Override
  public void writeResponse(
      String baseUri, String ctxUri, EndpointInfo endpointInfo, OutputStream os) {
    try {
      int idx = baseUri.toLowerCase().indexOf("?");
      Map<String, String> params = UrlUtils.parseQueryString(baseUri.substring(idx + 1));

      String base;

      if (endpointInfo.getProperty("publishedEndpointUrl") != null) {
        base = String.valueOf(endpointInfo.getProperty("publishedEndpointUrl"));
      } else {
        base = baseUri.substring(0, baseUri.toLowerCase().indexOf("?"));
      }

      String wsdl = params.get("wsdl");
      if (wsdl != null) {
        // Always use the URL decoded version to ensure that we have a
        // canonical representation of the import URL for lookup.
        wsdl = URLDecoder.decode(wsdl, "utf-8");
      }

      String xsd = params.get("xsd");
      if (xsd != null) {
        // Always use the URL decoded version to ensure that we have a
        // canonical representation of the import URL for lookup.
        xsd = URLDecoder.decode(xsd, "utf-8");
      }

      Map<String, Definition> mp =
          CastUtils.cast(
              (Map) endpointInfo.getService().getProperty(WSDLQueryHandler.class.getName()));
      Map<String, SchemaReference> smp =
          CastUtils.cast(
              (Map)
                  endpointInfo
                      .getService()
                      .getProperty(WSDLQueryHandler.class.getName() + ".Schemas"));

      if (mp == null) {
        endpointInfo
            .getService()
            .setProperty(WSDLQueryHandler.class.getName(), new ConcurrentHashMap());
        mp =
            CastUtils.cast(
                (Map) endpointInfo.getService().getProperty(WSDLQueryHandler.class.getName()));
      }
      if (smp == null) {
        endpointInfo
            .getService()
            .setProperty(WSDLQueryHandler.class.getName() + ".Schemas", new ConcurrentHashMap());
        smp =
            CastUtils.cast(
                (Map)
                    endpointInfo
                        .getService()
                        .getProperty(WSDLQueryHandler.class.getName() + ".Schemas"));
      }

      if (!mp.containsKey("")) {
        Definition def = getDefinition(endpointInfo);

        mp.put("", def);
        updateDefinition(def, mp, smp, base, endpointInfo);
      }

      Document doc;
      if (xsd == null) {
        Definition def = mp.get(wsdl);
        if (def == null) {
          String wsdl2 =
              resolveWithCatalogs(OASISCatalogManager.getCatalogManager(bus), wsdl, base);
          if (wsdl2 != null) {
            def = mp.get(wsdl2);
          }
        }
        if (def == null) {
          throw new WSDLQueryException(
              new org.apache.cxf.common.i18n.Message("WSDL_NOT_FOUND", LOG, wsdl), null);
        }

        synchronized (def) {
          // writing a def is not threadsafe.  Sync on it to make sure
          // we don't get any ConcurrentModificationExceptions
          if (endpointInfo.getProperty("publishedEndpointUrl") != null) {
            String publishingUrl = String.valueOf(endpointInfo.getProperty("publishedEndpointUrl"));
            updatePublishedEndpointUrl(publishingUrl, def, endpointInfo.getName());
          }

          WSDLWriter wsdlWriter =
              bus.getExtension(WSDLManager.class).getWSDLFactory().newWSDLWriter();
          def.setExtensionRegistry(bus.getExtension(WSDLManager.class).getExtensionRegistry());
          doc = wsdlWriter.getDocument(def);
        }
      } else {
        SchemaReference si = smp.get(xsd);
        if (si == null) {
          String xsd2 = resolveWithCatalogs(OASISCatalogManager.getCatalogManager(bus), xsd, base);
          if (xsd2 != null) {
            si = smp.get(xsd2);
          }
        }
        if (si == null) {
          throw new WSDLQueryException(
              new org.apache.cxf.common.i18n.Message("SCHEMA_NOT_FOUND", LOG, wsdl), null);
        }

        String uri = si.getReferencedSchema().getDocumentBaseURI();
        uri =
            resolveWithCatalogs(
                OASISCatalogManager.getCatalogManager(bus),
                uri,
                si.getReferencedSchema().getDocumentBaseURI());
        if (uri == null) {
          uri = si.getReferencedSchema().getDocumentBaseURI();
        }
        ResourceManagerWSDLLocator rml = new ResourceManagerWSDLLocator(uri, bus);

        InputSource src = rml.getBaseInputSource();
        doc = XMLUtils.getParser().parse(src);
      }

      updateDoc(doc, base, mp, smp, endpointInfo);
      String enc = null;
      try {
        enc = doc.getXmlEncoding();
      } catch (Exception ex) {
        // ignore - not dom level 3
      }
      if (enc == null) {
        enc = "utf-8";
      }

      XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(os, enc);
      StaxUtils.writeNode(doc, writer, true);
      writer.flush();
    } catch (WSDLQueryException wex) {
      throw wex;
    } catch (Exception wex) {
      throw new WSDLQueryException(
          new org.apache.cxf.common.i18n.Message("COULD_NOT_PROVIDE_WSDL", LOG, baseUri), wex);
    }
  }