Search in sources :

Example 81 with ToolException

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

the class ServiceProcessor method processPort.

private JavaPort processPort(JavaModel model, ServiceInfo si, EndpointInfo port) throws ToolException {
    BindingInfo binding = port.getBinding();
    String portType = binding.getInterface().getName().getLocalPart();
    JavaInterface intf = PortTypeProcessor.getInterface(context, si, binding.getInterface());
    JavaPort jport = new JavaPort(NameUtil.mangleNameToClassName(port.getName().getLocalPart()));
    jport.setPackageName(intf.getPackageName());
    jport.setPortName(port.getName().getLocalPart());
    jport.setBindingAdress(port.getAddress());
    jport.setBindingName(binding.getName().getLocalPart());
    jport.setPortType(portType);
    jport.setInterfaceClass(intf.getName());
    bindingType = getBindingType(binding);
    if (bindingType == null) {
        org.apache.cxf.common.i18n.Message msg = new org.apache.cxf.common.i18n.Message("BINDING_SPECIFY_ONE_PROTOCOL", LOG, binding.getName());
        throw new ToolException(msg);
    }
    if (isSoapBinding()) {
        SoapBinding spbd = SOAPBindingUtil.getProxy(SoapBinding.class, this.bindingObj);
        jport.setStyle(SOAPBindingUtil.getSoapStyle(spbd.getStyle()));
        jport.setTransURI(spbd.getTransportURI());
    }
    Collection<BindingOperationInfo> operations = binding.getOperations();
    for (BindingOperationInfo bop : operations) {
        processOperation(model, bop, binding);
    }
    jport.setJavaDoc(port.getDocumentation());
    JAXWSBinding bind = port.getExtensor(JAXWSBinding.class);
    if (bind != null) {
        jport.setMethodName(bind.getMethodName());
        jport.setJavaDoc(bind.getMethodJavaDoc());
    }
    return jport;
}
Also used : JavaInterface(org.apache.cxf.tools.common.model.JavaInterface) BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) Message(org.apache.cxf.common.i18n.Message) Message(org.apache.cxf.common.i18n.Message) SoapBinding(org.apache.cxf.binding.soap.wsdl.extensions.SoapBinding) JavaPort(org.apache.cxf.tools.common.model.JavaPort) BindingInfo(org.apache.cxf.service.model.BindingInfo) JAXWSBinding(org.apache.cxf.tools.wsdlto.frontend.jaxws.customization.JAXWSBinding) ToolException(org.apache.cxf.tools.common.ToolException)

Example 82 with ToolException

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

the class ServiceProcessor method processMultipart.

public void processMultipart(JavaMethod jm, BindingOperationInfo operation, MIMEMultipartRelated ext, JavaType.Style style) throws ToolException {
    List<MIMEPart> mimeParts = CastUtils.cast(ext.getMIMEParts());
    for (MIMEPart mPart : mimeParts) {
        List<ExtensibilityElement> extns = CastUtils.cast(mPart.getExtensibilityElements());
        for (ExtensibilityElement extElement : extns) {
            if (extElement instanceof MIMEContent) {
                MIMEContent mimeContent = (MIMEContent) extElement;
                String mimeJavaType = getJavaTypeForMimeType(mPart);
                if (JavaType.Style.IN.equals(style)) {
                    String paramName = ProcessorUtil.mangleNameToVariableName(mimeContent.getPart());
                    JavaParameter jp = jm.getParameter(paramName);
                    if (jp == null) {
                        Message message = new Message("MIMEPART_CANNOT_MAP", LOG, mimeContent.getPart());
                        throw new ToolException(message);
                    }
                    if (!jp.getClassName().equals(mimeJavaType)) {
                        // jp.setType(mimeJavaType);
                        jp.setClassName(mimeJavaType);
                    }
                } else if (JavaType.Style.OUT.equals(style)) {
                    JavaType jp = null;
                    if (!"void".equals(jm.getReturn().getType()) && mimeContent.getPart().equals(jm.getReturn().getName())) {
                        jp = jm.getReturn();
                        jp.setClassName(mimeJavaType);
                    }
                    if (jp == null) {
                        for (JavaParameter para : jm.getParameters()) {
                            if (mimeContent.getPart().equals(para.getPartName())) {
                                jp = para;
                            }
                        }
                        if (jp != null) {
                            ((JavaParameter) jp).setClassName(mimeJavaType);
                        }
                    }
                    if (jp == null) {
                        Message message = new Message("MIMEPART_CANNOT_MAP", LOG, mimeContent.getPart());
                        throw new ToolException(message);
                    }
                }
            } else if (extElement instanceof SOAPHeader) {
                processSoapHeader(jm, operation, extElement);
            }
        }
    }
}
Also used : JavaType(org.apache.cxf.tools.common.model.JavaType) MIMEContent(javax.wsdl.extensions.mime.MIMEContent) Message(org.apache.cxf.common.i18n.Message) JavaParameter(org.apache.cxf.tools.common.model.JavaParameter) ToolException(org.apache.cxf.tools.common.ToolException) ExtensibilityElement(javax.wsdl.extensions.ExtensibilityElement) SOAPHeader(javax.wsdl.extensions.soap.SOAPHeader) MIMEPart(javax.wsdl.extensions.mime.MIMEPart)

Example 83 with ToolException

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

the class WSDLToJavaContainer method processWsdl.

private void processWsdl() {
    validate(context);
    FrontEndProfile frontend = context.get(FrontEndProfile.class);
    if (frontend == null) {
        throw new ToolException(new Message("FOUND_NO_FRONTEND", LOG));
    }
    WSDLConstants.WSDLVersion version = getWSDLVersion();
    String wsdlURL = (String) context.get(ToolConstants.CFG_WSDLURL);
    @SuppressWarnings("unchecked") List<ServiceInfo> serviceList = (List<ServiceInfo>) context.get(ToolConstants.SERVICE_LIST);
    if (serviceList == null) {
        serviceList = new ArrayList<>();
        // 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());
            wsdlURL = URIParserUtil.getAbsoluteURI(wsdlURL);
            builder.build(wsdlURL);
            builder.customize();
            Definition definition = builder.getWSDLModel();
            context.put(Definition.class, definition);
            builder.validate(definition);
            WSDLServiceBuilder serviceBuilder = new WSDLServiceBuilder(getBus());
            if (context.isVerbose()) {
                serviceBuilder.setUnwrapLogLevel(Level.INFO);
            }
            serviceBuilder.setIgnoreUnknownBindings(true);
            String allowRefs = (String) context.get(ToolConstants.CFG_ALLOW_ELEMENT_REFS);
            if (!StringUtils.isEmpty(allowRefs) || context.optionSet(ToolConstants.CFG_ALLOW_ELEMENT_REFS)) {
                if (allowRefs.length() > 0 && allowRefs.charAt(0) == '=') {
                    allowRefs = allowRefs.substring(1);
                }
                if (StringUtils.isEmpty(allowRefs)) {
                    allowRefs = "true";
                }
                serviceBuilder.setAllowElementRefs(Boolean.valueOf(allowRefs));
            }
            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);
            }
            // remove definition from cache so that won't fail when encounter same wsdl file
            // name but different wsdl content(CXF-3340)
            getBus().getExtension(WSDLManager.class).removeDefinition(definition);
        } else {
        // TODO: wsdl2.0 support
        }
    }
    context.put(ToolConstants.SERVICE_LIST, serviceList);
    Map<String, InterfaceInfo> interfaces = new LinkedHashMap<String, InterfaceInfo>();
    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, createClassCollector());
    Processor processor = frontend.getProcessor();
    if (processor instanceof ClassNameProcessor) {
        processor.setEnvironment(context);
        for (ServiceInfo service : serviceList) {
            context.put(ServiceInfo.class, service);
            ((ClassNameProcessor) processor).processClassNames();
            context.put(ServiceInfo.class, null);
        }
    }
    if (context.optionSet(ToolConstants.CFG_NO_TYPES)) {
        context.remove(ToolConstants.CFG_TYPES);
        context.remove(ToolConstants.CFG_ALL);
        context.remove(ToolConstants.CFG_COMPILE);
    }
    generateTypes();
    if (context.getErrorListener().getErrorCount() > 0) {
        return;
    }
    for (ServiceInfo service : serviceList) {
        context.put(ServiceInfo.class, service);
        if (context.basicValidateWSDL()) {
            validate(service);
        }
        if (context.getErrorListener().getErrorCount() == 0) {
            // Build the JavaModel from the ServiceModel
            processor.setEnvironment(context);
            processor.process();
        }
    }
    if (context.getErrorListener().getErrorCount() > 0) {
        return;
    }
    if (context.optionSet(ToolConstants.CFG_CLIENT_JAR)) {
        enforceWSDLLocation(context);
    }
    if (!isSuppressCodeGen()) {
        // Generate artifacts
        for (FrontEndGenerator generator : frontend.getGenerators()) {
            generator.generate(context);
        }
    }
    context.remove(ToolConstants.SERVICE_LIST);
    // Build projects: compile classes and copy resources etc.
    if (context.optionSet(ToolConstants.CFG_COMPILE)) {
        new ClassUtils().compile(context);
    }
    if (context.isExcludeNamespaceEnabled()) {
        try {
            removeExcludeFiles();
        } catch (IOException e) {
            throw new ToolException(e);
        }
    }
    if (context.optionSet(ToolConstants.CFG_CLIENT_JAR)) {
        processClientJar(context);
    }
}
Also used : AbstractWSDLBuilder(org.apache.cxf.tools.wsdlto.core.AbstractWSDLBuilder) ClassNameProcessor(org.apache.cxf.tools.common.ClassNameProcessor) Processor(org.apache.cxf.tools.common.Processor) Message(org.apache.cxf.common.i18n.Message) Definition(javax.wsdl.Definition) IOException(java.io.IOException) ClassUtils(org.apache.cxf.tools.common.ClassUtils) 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) FrontEndGenerator(org.apache.cxf.tools.common.FrontEndGenerator) WSDLManager(org.apache.cxf.wsdl.WSDLManager) ClassNameProcessor(org.apache.cxf.tools.common.ClassNameProcessor) List(java.util.List) ArrayList(java.util.ArrayList) ToolException(org.apache.cxf.tools.common.ToolException) InterfaceInfo(org.apache.cxf.service.model.InterfaceInfo) SchemaCollection(org.apache.cxf.common.xmlschema.SchemaCollection)

Example 84 with ToolException

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

the class WSDLToJavaContainer method getServiceQName.

@SuppressWarnings("unchecked")
public QName getServiceQName(Definition def) {
    List<Definition> defs = new ArrayList<>();
    defs.add(def);
    Iterator<?> ite1 = def.getImports().values().iterator();
    while (ite1.hasNext()) {
        List<javax.wsdl.Import> defList = CastUtils.cast((List<?>) ite1.next());
        for (javax.wsdl.Import importDef : defList) {
            defs.add(importDef.getDefinition());
        }
    }
    String serviceName = (String) context.get(ToolConstants.CFG_SERVICENAME);
    for (Definition definition : defs) {
        if (serviceName != null) {
            for (Iterator<QName> ite = definition.getServices().keySet().iterator(); ite.hasNext(); ) {
                QName qn = ite.next();
                if (qn.getLocalPart().equalsIgnoreCase(serviceName.toLowerCase())) {
                    return qn;
                }
            }
        }
    }
    Message msg = new Message("SERVICE_NOT_FOUND", LOG, new Object[] { serviceName });
    throw new ToolException(msg);
}
Also used : Message(org.apache.cxf.common.i18n.Message) QName(javax.xml.namespace.QName) Definition(javax.wsdl.Definition) ArrayList(java.util.ArrayList) ToolException(org.apache.cxf.tools.common.ToolException)

Example 85 with ToolException

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

the class WSDLToJavaContainer method validate.

public void validate(ToolContext env) throws ToolException {
    String outdir = (String) env.get(ToolConstants.CFG_OUTPUTDIR);
    if (!isSuppressCodeGen()) {
        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.getAbsoluteURI(wsdl));
    if (!env.containsKey(ToolConstants.CFG_WSDLLOCATION)) {
        // make sure the "raw" form is used for the wsdlLocation
        // instead of the absolute URI that normalize may return
        boolean assumeFileURI = false;
        try {
            URI uri = new URI(wsdl);
            String uriScheme = uri.getScheme();
            if (uriScheme == null) {
                assumeFileURI = true;
            }
            wsdl = uri.toString();
        } catch (Exception e) {
            // not a URL, assume file
            assumeFileURI = true;
        }
        if (assumeFileURI) {
            if (wsdl.indexOf(":") != -1 && !wsdl.startsWith("/")) {
                wsdl = "file:/" + wsdl;
            } else {
                wsdl = "file:" + wsdl;
            }
            try {
                URI uri = new URI(wsdl);
                wsdl = uri.toString();
            } catch (Exception e1) {
            // ignore...
            }
        }
        wsdl = wsdl.replace("\\", "/");
        env.put(ToolConstants.CFG_WSDLLOCATION, 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) URI(java.net.URI) BadUsageException(org.apache.cxf.tools.common.toolspec.parser.BadUsageException) IOException(java.io.IOException) ToolException(org.apache.cxf.tools.common.ToolException)

Aggregations

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