Search in sources :

Example 1 with ToolException

use of org.apache.cxf.tools.common.ToolException in project cxf by apache.

the class JAXRSContainer method readWadl.

protected String readWadl(String wadlURI, String authentication) {
    try {
        URL url = new URL(wadlURI);
        InputStream is = null;
        if (wadlURI.startsWith("https") && authentication != null) {
            is = SecureConnectionHelper.getStreamFromSecureConnection(url, authentication);
        } else {
            is = url.openStream();
        }
        Reader reader = new InputStreamReader(is, StandardCharsets.UTF_8);
        return IOUtils.toString(reader);
    } catch (IOException e) {
        throw new ToolException(e);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) ToolException(org.apache.cxf.tools.common.ToolException) URL(java.net.URL)

Example 2 with ToolException

use of org.apache.cxf.tools.common.ToolException in project cxf by apache.

the class JavaScriptContainer method execute.

@SuppressWarnings("unchecked")
public void execute() throws ToolException {
    if (hasInfoOption()) {
        return;
    }
    buildToolContext();
    validate(context);
    WSDLConstants.WSDLVersion version = getWSDLVersion();
    String wsdlURL = (String) context.get(ToolConstants.CFG_WSDLURL);
    List<ServiceInfo> serviceList = (List<ServiceInfo>) context.get(ToolConstants.SERVICE_LIST);
    if (serviceList == null) {
        serviceList = new ArrayList<>();
        PluginLoader pluginLoader = PluginLoader.newInstance();
        // for JavaScript generation, we always use JAX-WS.
        FrontEndProfile frontend = pluginLoader.getFrontEndProfile("jaxws");
        // Build the ServiceModel from the WSDLModel
        if (version == WSDLConstants.WSDLVersion.WSDL11) {
            AbstractWSDLBuilder builder = frontend.getWSDLBuilder();
            builder.setContext(context);
            builder.setBus(getBus());
            context.put(Bus.class, getBus());
            builder.build(URIParserUtil.getAbsoluteURI(wsdlURL));
            builder.customize();
            Definition definition = builder.getWSDLModel();
            context.put(Definition.class, definition);
            builder.validate(definition);
            WSDLServiceBuilder serviceBuilder = new WSDLServiceBuilder(getBus());
            String serviceName = (String) context.get(ToolConstants.CFG_SERVICENAME);
            if (serviceName != null) {
                List<ServiceInfo> services = serviceBuilder.buildServices(definition, getServiceQName(definition));
                serviceList.addAll(services);
            } else if (definition.getServices().size() > 0) {
                serviceList = serviceBuilder.buildServices(definition);
            } else {
                serviceList = serviceBuilder.buildMockServices(definition);
            }
        } else {
            // TODO: wsdl2.0 support
            throw new ToolException("Only WSDL 1.1 supported");
        }
    }
    if (serviceList.isEmpty()) {
        throw new ToolException("Did not find any services in WSDL");
    }
    Map<String, InterfaceInfo> interfaces = new LinkedHashMap<>();
    ServiceInfo service0 = serviceList.get(0);
    SchemaCollection schemaCollection = service0.getXmlSchemaCollection();
    context.put(ToolConstants.XML_SCHEMA_COLLECTION, schemaCollection);
    context.put(ToolConstants.PORTTYPE_MAP, interfaces);
    context.put(ClassCollector.class, new ClassCollector());
    WSDLToJavaScriptProcessor processor = new WSDLToJavaScriptProcessor();
    for (ServiceInfo service : serviceList) {
        context.put(ServiceInfo.class, service);
        validate(service);
        processor.setEnvironment(context);
        processor.process();
    }
}
Also used : AbstractWSDLBuilder(org.apache.cxf.tools.wsdlto.core.AbstractWSDLBuilder) ClassCollector(org.apache.cxf.tools.util.ClassCollector) Definition(javax.wsdl.Definition) FrontEndProfile(org.apache.cxf.tools.wsdlto.core.FrontEndProfile) LinkedHashMap(java.util.LinkedHashMap) ServiceInfo(org.apache.cxf.service.model.ServiceInfo) WSDLConstants(org.apache.cxf.wsdl.WSDLConstants) WSDLServiceBuilder(org.apache.cxf.wsdl11.WSDLServiceBuilder) ArrayList(java.util.ArrayList) List(java.util.List) ToolException(org.apache.cxf.tools.common.ToolException) InterfaceInfo(org.apache.cxf.service.model.InterfaceInfo) SchemaCollection(org.apache.cxf.common.xmlschema.SchemaCollection) PluginLoader(org.apache.cxf.tools.wsdlto.core.PluginLoader)

Example 3 with ToolException

use of org.apache.cxf.tools.common.ToolException in project cxf by apache.

the class JavaScriptContainer method validate.

public void validate(final ServiceInfo service) throws ToolException {
    for (ServiceValidator validator : getServiceValidators()) {
        service.setProperty(ToolContext.class.getName(), context);
        validator.setService(service);
        if (!validator.isValid()) {
            throw new ToolException(validator.getErrorMessage());
        }
    }
}
Also used : ServiceValidator(org.apache.cxf.tools.validator.ServiceValidator) ToolContext(org.apache.cxf.tools.common.ToolContext) ToolException(org.apache.cxf.tools.common.ToolException)

Example 4 with ToolException

use of org.apache.cxf.tools.common.ToolException in project cxf by apache.

the class JavaScriptContainer method setNamespaceJavascriptPrefixes.

public void setNamespaceJavascriptPrefixes(ToolContext env) {
    Map<String, String> nsPrefixMap = new HashMap<>();
    if (env.get(ToolConstants.CFG_JSPACKAGEPREFIX) != null) {
        final String[] pns;
        try {
            pns = (String[]) env.get(ToolConstants.CFG_JSPACKAGEPREFIX);
        } catch (ClassCastException e) {
            Message msg = new Message("INVALID_PREFIX_MAPPING", LOG, env.get(ToolConstants.CFG_JSPACKAGEPREFIX));
            throw new ToolException(msg);
        }
        for (String jsprefix : pns) {
            int pos = jsprefix.indexOf('=');
            if (pos != -1) {
                String ns = jsprefix.substring(0, pos);
                jsprefix = jsprefix.substring(pos + 1);
                nsPrefixMap.put(ns, jsprefix);
            }
        }
        env.put(ToolConstants.CFG_JSPREFIXMAP, nsPrefixMap);
    }
}
Also used : Message(org.apache.cxf.common.i18n.Message) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ToolException(org.apache.cxf.tools.common.ToolException)

Example 5 with ToolException

use of org.apache.cxf.tools.common.ToolException in project cxf by apache.

the class JavaScriptContainer method validate.

public void validate(ToolContext env) throws ToolException {
    String outdir = (String) env.get(ToolConstants.CFG_OUTPUTDIR);
    if (outdir != null) {
        File dir = new File(outdir);
        if (!dir.exists() && !dir.mkdirs()) {
            Message msg = new Message("DIRECTORY_COULD_NOT_BE_CREATED", LOG, outdir);
            throw new ToolException(msg);
        }
        if (!dir.isDirectory()) {
            Message msg = new Message("NOT_A_DIRECTORY", LOG, outdir);
            throw new ToolException(msg);
        }
    }
    if (env.optionSet(ToolConstants.CFG_COMPILE)) {
        String clsdir = (String) env.get(ToolConstants.CFG_CLASSDIR);
        if (clsdir != null) {
            File dir = new File(clsdir);
            if (!dir.exists() && !dir.mkdirs()) {
                Message msg = new Message("DIRECTORY_COULD_NOT_BE_CREATED", LOG, clsdir);
                throw new ToolException(msg);
            }
        }
    }
    String wsdl = (String) env.get(ToolConstants.CFG_WSDLURL);
    if (StringUtils.isEmpty(wsdl)) {
        Message msg = new Message("NO_WSDL_URL", LOG);
        throw new ToolException(msg);
    }
    env.put(ToolConstants.CFG_WSDLURL, URIParserUtil.normalize(wsdl));
    String[] bindingFiles;
    try {
        bindingFiles = (String[]) env.get(ToolConstants.CFG_BINDING);
        if (bindingFiles == null) {
            return;
        }
    } catch (ClassCastException e) {
        bindingFiles = new String[1];
        bindingFiles[0] = (String) env.get(ToolConstants.CFG_BINDING);
    }
    for (int i = 0; i < bindingFiles.length; i++) {
        bindingFiles[i] = URIParserUtil.getAbsoluteURI(bindingFiles[i]);
    }
    env.put(ToolConstants.CFG_BINDING, bindingFiles);
}
Also used : Message(org.apache.cxf.common.i18n.Message) ToolException(org.apache.cxf.tools.common.ToolException) File(java.io.File)

Aggregations

ToolException (org.apache.cxf.tools.common.ToolException)132 Message (org.apache.cxf.common.i18n.Message)69 IOException (java.io.IOException)39 File (java.io.File)29 QName (javax.xml.namespace.QName)22 WSDLException (javax.wsdl.WSDLException)19 ToolContext (org.apache.cxf.tools.common.ToolContext)16 BadUsageException (org.apache.cxf.tools.common.toolspec.parser.BadUsageException)16 XMLStreamException (javax.xml.stream.XMLStreamException)14 Test (org.junit.Test)12 FileNotFoundException (java.io.FileNotFoundException)10 Binding (javax.wsdl.Binding)10 Element (org.w3c.dom.Element)10 URISyntaxException (java.net.URISyntaxException)9 URL (java.net.URL)9 ArrayList (java.util.ArrayList)9 InputStream (java.io.InputStream)8 SoapBinding (org.apache.cxf.binding.soap.wsdl.extensions.SoapBinding)8 HashMap (java.util.HashMap)7 SoapOperation (org.apache.cxf.binding.soap.wsdl.extensions.SoapOperation)7