Search in sources :

Example 11 with WSDLWriter

use of javax.wsdl.xml.WSDLWriter in project carbon-apimgt by wso2.

the class WSDL11ProcessorImpl method getWSDLByteArrayOutputStream.

/**
 * Retrieves a {@link ByteArrayOutputStream} for provided {@link Definition}.
 *
 * @param definition WSDL Definition
 * @return A {@link ByteArrayOutputStream} for provided {@link Definition}
 * @throws APIMgtWSDLException If an error occurs while creating {@link ByteArrayOutputStream}
 */
private ByteArrayOutputStream getWSDLByteArrayOutputStream(Definition definition) throws APIMgtWSDLException {
    WSDLWriter writer = getWsdlFactoryInstance().newWSDLWriter();
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    try {
        writer.writeWSDL(definition, byteArrayOutputStream);
    } catch (WSDLException e) {
        throw new APIMgtWSDLException("Error while stringifying WSDL definition", e, ExceptionCodes.INTERNAL_WSDL_EXCEPTION);
    }
    return byteArrayOutputStream;
}
Also used : APIMgtWSDLException(org.wso2.carbon.apimgt.core.exception.APIMgtWSDLException) APIMgtWSDLException(org.wso2.carbon.apimgt.core.exception.APIMgtWSDLException) WSDLException(javax.wsdl.WSDLException) WSDLWriter(javax.wsdl.xml.WSDLWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 12 with WSDLWriter

use of javax.wsdl.xml.WSDLWriter in project carbon-apimgt by wso2.

the class WSDL11ProcessorImpl method getUpdatedWSDLPath.

/**
 * Updates the endpoints of all the WSDL files in the path based on the provided API (context) and Label (host).
 *
 * @param api   Provided API object
 * @param label Provided label object
 * @return Updated WSDL file path
 * @throws APIMgtWSDLException Error while updating WSDL files
 */
@Override
public String getUpdatedWSDLPath(API api, Label label) throws APIMgtWSDLException {
    if (label != null) {
        for (Map.Entry<String, Definition> entry : pathToDefinitionMap.entrySet()) {
            Definition definition = entry.getValue();
            if (log.isDebugEnabled()) {
                log.debug("Updating endpoints of WSDL: " + entry.getKey());
            }
            updateEndpoints(label.getAccessUrls(), api, definition);
            if (log.isDebugEnabled()) {
                log.debug("Successfully updated endpoints of WSDL: " + entry.getKey());
            }
            try (FileOutputStream wsdlFileOutputStream = new FileOutputStream(new File(entry.getKey()))) {
                WSDLWriter writer = getWsdlFactoryInstance().newWSDLWriter();
                writer.writeWSDL(definition, wsdlFileOutputStream);
            } catch (IOException | WSDLException e) {
                throw new APIMgtWSDLException("Failed to create WSDL archive for API:" + api.getName() + ":" + api.getVersion() + " for label " + label.getName(), ExceptionCodes.ERROR_WHILE_CREATING_WSDL_ARCHIVE);
            }
        }
    }
    return wsdlArchiveExtractedPath;
}
Also used : APIMgtWSDLException(org.wso2.carbon.apimgt.core.exception.APIMgtWSDLException) APIMgtWSDLException(org.wso2.carbon.apimgt.core.exception.APIMgtWSDLException) WSDLException(javax.wsdl.WSDLException) FileOutputStream(java.io.FileOutputStream) Definition(javax.wsdl.Definition) WSDLWriter(javax.wsdl.xml.WSDLWriter) IOException(java.io.IOException) Map(java.util.Map) HashMap(java.util.HashMap) File(java.io.File)

Example 13 with WSDLWriter

use of javax.wsdl.xml.WSDLWriter in project cxf by apache.

the class WSDLToJavaContainer method generateLocalWSDL.

@SuppressWarnings("unchecked")
private void generateLocalWSDL(ToolContext context) {
    String outputdir = (String) context.get(ToolConstants.CFG_CLASSDIR);
    File wsdlFile = new File(outputdir, (String) context.get(ToolConstants.CFG_WSDLLOCATION));
    Definition def = context.get(Definition.class);
    try {
        // get imported schemas
        int xsdCount = 0;
        SchemaCollection schemas = (SchemaCollection) context.get(ToolConstants.XML_SCHEMA_COLLECTION);
        Map<String, String> sourceMap = new HashMap<>();
        for (XmlSchema imp : schemas.getXmlSchemas()) {
            if (imp.getSourceURI() != null && !imp.getSourceURI().contains(".wsdl#")) {
                String schemaFileName = "schema" + (++xsdCount) + ".xsd";
                sourceMap.put(imp.getTargetNamespace(), schemaFileName);
            }
        }
        // get imported wsdls
        int wsdlImportCount = 0;
        List<Definition> defs = (List<Definition>) context.get(ToolConstants.IMPORTED_DEFINITION);
        Map<String, String> importWSDLMap = new HashMap<>();
        for (Definition importDef : defs) {
            File importedWsdlFile = null;
            if (!StringUtils.isEmpty(importDef.getDocumentBaseURI())) {
                importedWsdlFile = new File(importDef.getDocumentBaseURI());
            } else {
                importedWsdlFile = new File(importDef.getQName().getLocalPart() + ".wsdl");
            }
            if (!FileUtils.isValidFileName(importedWsdlFile.getName())) {
                importedWsdlFile = new File("import" + (++wsdlImportCount) + ".wsdl");
            }
            importWSDLMap.put(importDef.getTargetNamespace(), importedWsdlFile.getName());
        }
        OutputStreamCreator outputStreamCreator = null;
        if (context.get(OutputStreamCreator.class) != null) {
            outputStreamCreator = context.get(OutputStreamCreator.class);
        } else {
            outputStreamCreator = new OutputStreamCreator();
            context.put(OutputStreamCreator.class, outputStreamCreator);
        }
        Writer os = null;
        for (XmlSchema imp : schemas.getXmlSchemas()) {
            if (imp.getSourceURI() != null && !imp.getSourceURI().contains(".wsdl#")) {
                String schemaFileName = sourceMap.get(imp.getTargetNamespace());
                File impfile = new File(wsdlFile.getParentFile(), schemaFileName);
                Element el = imp.getSchemaDocument().getDocumentElement();
                updateImports(el, sourceMap);
                os = new FileWriterUtil(impfile.getParent(), context.get(OutputStreamCreator.class)).getWriter(impfile, StandardCharsets.UTF_8.name());
                StaxUtils.writeTo(el, os, 2);
                os.close();
            }
        }
        // change the import location in wsdl file
        OutputStream wsdloutput = new BufferedOutputStream(Files.newOutputStream(wsdlFile.toPath()));
        WSDLWriter wsdlWriter = WSDLFactory.newInstance().newWSDLWriter();
        LoadingByteArrayOutputStream bout = new LoadingByteArrayOutputStream();
        wsdlWriter.writeWSDL(def, bout);
        Element defEle = StaxUtils.read(bout.createInputStream()).getDocumentElement();
        List<Element> xsdElements = DOMUtils.findAllElementsByTagNameNS(defEle, WSDLConstants.NS_SCHEMA_XSD, "schema");
        for (Element xsdEle : xsdElements) {
            updateImports(xsdEle, sourceMap);
        }
        updateWSDLImports(defEle, importWSDLMap);
        StaxUtils.writeTo(defEle, wsdloutput);
        wsdloutput.close();
        for (Definition importDef : defs) {
            File importWsdlFile = new File(outputdir, importWSDLMap.get(importDef.getTargetNamespace()));
            OutputStream wsdlOs = new BufferedOutputStream(Files.newOutputStream(importWsdlFile.toPath()));
            bout = new LoadingByteArrayOutputStream();
            wsdlWriter.writeWSDL(importDef, bout);
            Element importEle = StaxUtils.read(bout.createInputStream()).getDocumentElement();
            xsdElements = DOMUtils.findAllElementsByTagNameNS(importEle, WSDLConstants.NS_SCHEMA_XSD, "schema");
            for (Element xsdEle : xsdElements) {
                updateImports(xsdEle, sourceMap);
            }
            updateWSDLImports(importEle, importWSDLMap);
            StaxUtils.writeTo(importEle, wsdlOs);
            wsdlOs.close();
        }
    } catch (Exception ex) {
        LOG.log(Level.SEVERE, "FAILED_TO_GEN_LOCAL_WSDL", ex);
        Message msg = new Message("FAILED_TO_GEN_LOCAL_WSDL", LOG);
        throw new ToolException(msg, ex);
    }
}
Also used : Message(org.apache.cxf.common.i18n.Message) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) FileWriterUtil(org.apache.cxf.tools.util.FileWriterUtil) Element(org.w3c.dom.Element) LoadingByteArrayOutputStream(org.apache.cxf.helpers.LoadingByteArrayOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) JarOutputStream(java.util.jar.JarOutputStream) OutputStream(java.io.OutputStream) Definition(javax.wsdl.Definition) WSDLWriter(javax.wsdl.xml.WSDLWriter) BadUsageException(org.apache.cxf.tools.common.toolspec.parser.BadUsageException) IOException(java.io.IOException) ToolException(org.apache.cxf.tools.common.ToolException) LoadingByteArrayOutputStream(org.apache.cxf.helpers.LoadingByteArrayOutputStream) XmlSchema(org.apache.ws.commons.schema.XmlSchema) List(java.util.List) ArrayList(java.util.ArrayList) OutputStreamCreator(org.apache.cxf.tools.util.OutputStreamCreator) ToolException(org.apache.cxf.tools.common.ToolException) SchemaCollection(org.apache.cxf.common.xmlschema.SchemaCollection) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) Writer(java.io.Writer) WSDLWriter(javax.wsdl.xml.WSDLWriter)

Example 14 with WSDLWriter

use of javax.wsdl.xml.WSDLWriter in project cxf by apache.

the class WSDLToXMLProcessor method writeToWSDL.

private void writeToWSDL() throws ToolException {
    WSDLWriter wsdlWriter = wsdlFactory.newWSDLWriter();
    Writer outputWriter = getOutputWriter(NEW_FILE_NAME_MODIFIER);
    try {
        wsdlWriter.writeWSDL(wsdlDefinition, outputWriter);
    } catch (WSDLException wse) {
        Message msg = new Message("FAIL_TO_WRITE_WSDL", LOG);
        throw new ToolException(msg);
    }
    try {
        outputWriter.close();
    } catch (IOException ioe) {
        Message msg = new Message("FAIL_TO_CLOSE_WSDL_FILE", LOG);
        throw new ToolException(msg);
    }
}
Also used : Message(org.apache.cxf.common.i18n.Message) WSDLException(javax.wsdl.WSDLException) WSDLWriter(javax.wsdl.xml.WSDLWriter) ToolException(org.apache.cxf.tools.common.ToolException) IOException(java.io.IOException) WSDLWriter(javax.wsdl.xml.WSDLWriter) Writer(java.io.Writer)

Example 15 with WSDLWriter

use of javax.wsdl.xml.WSDLWriter in project cxf by apache.

the class JaxWsServerFactoryBeanTest method testSimpleServiceClass.

@Test
public void testSimpleServiceClass() throws Exception {
    ServerFactoryBean factory = new ServerFactoryBean();
    factory.setServiceClass(Hello.class);
    String address = "http://localhost:9001/jaxwstest";
    factory.setAddress(address);
    Server server = factory.create();
    Endpoint endpoint = server.getEndpoint();
    ServiceInfo service = endpoint.getEndpointInfo().getService();
    assertNotNull(service);
    Bus bus = factory.getBus();
    Definition def = new ServiceWSDLBuilder(bus, service).build();
    WSDLWriter wsdlWriter = bus.getExtension(WSDLManager.class).getWSDLFactory().newWSDLWriter();
    def.setExtensionRegistry(bus.getExtension(WSDLManager.class).getExtensionRegistry());
    Document doc = wsdlWriter.getDocument(def);
    Map<String, String> ns = new HashMap<>();
    ns.put("wsdl", "http://schemas.xmlsoap.org/wsdl/");
    ns.put("soap", "http://schemas.xmlsoap.org/wsdl/soap/");
    XPathUtils xpather = new XPathUtils(ns);
    xpather.isExist("/wsdl:definitions/wsdl:binding/soap:binding", doc, XPathConstants.NODE);
    xpather.isExist("/wsdl:definitions/wsdl:binding/wsdl:operation[@name='add']/soap:operation", doc, XPathConstants.NODE);
    xpather.isExist("/wsdl:definitions/wsdl:service/wsdl:port[@name='add']/soap:address[@location='" + address + "']", doc, XPathConstants.NODE);
}
Also used : Bus(org.apache.cxf.Bus) Server(org.apache.cxf.endpoint.Server) HashMap(java.util.HashMap) Definition(javax.wsdl.Definition) ServerFactoryBean(org.apache.cxf.frontend.ServerFactoryBean) WSDLWriter(javax.wsdl.xml.WSDLWriter) Document(org.w3c.dom.Document) ServiceWSDLBuilder(org.apache.cxf.wsdl11.ServiceWSDLBuilder) ServiceInfo(org.apache.cxf.service.model.ServiceInfo) Endpoint(org.apache.cxf.endpoint.Endpoint) XPathUtils(org.apache.cxf.helpers.XPathUtils) Test(org.junit.Test)

Aggregations

WSDLWriter (javax.wsdl.xml.WSDLWriter)22 Definition (javax.wsdl.Definition)14 HashMap (java.util.HashMap)9 IOException (java.io.IOException)7 WSDLException (javax.wsdl.WSDLException)7 Bus (org.apache.cxf.Bus)7 ServiceWSDLBuilder (org.apache.cxf.wsdl11.ServiceWSDLBuilder)7 Element (org.w3c.dom.Element)7 Writer (java.io.Writer)6 Server (org.apache.cxf.endpoint.Server)6 XPathUtils (org.apache.cxf.helpers.XPathUtils)6 ToolException (org.apache.cxf.tools.common.ToolException)6 Message (org.apache.cxf.common.i18n.Message)5 File (java.io.File)4 JaxWsServerFactoryBean (org.apache.cxf.jaxws.JaxWsServerFactoryBean)4 LocalTransportFactory (org.apache.cxf.transport.local.LocalTransportFactory)4 Document (org.w3c.dom.Document)4 QName (javax.xml.namespace.QName)3 EndpointPolicy (org.apache.cxf.ws.policy.EndpointPolicy)3 PolicyEngine (org.apache.cxf.ws.policy.PolicyEngine)3