Search in sources :

Example 21 with SchemaCollection

use of org.apache.cxf.common.xmlschema.SchemaCollection in project cxf by apache.

the class ReflectionServiceFactoryBean method buildServiceFromClass.

protected void buildServiceFromClass() {
    Object o = getBus().getProperty("requireExplicitContractLocation");
    if (o != null && ("true".equals(o) || Boolean.TRUE.equals(o))) {
        throw new ServiceConstructionException(new Message("NO_WSDL_PROVIDED", LOG, getServiceClass().getName()));
    }
    if (LOG.isLoggable(Level.INFO)) {
        LOG.info("Creating Service " + getServiceQName() + " from class " + getServiceClass().getName());
    }
    populateFromClass = true;
    if (Proxy.isProxyClass(this.getServiceClass())) {
        LOG.log(Level.WARNING, "USING_PROXY_FOR_SERVICE", getServiceClass());
    }
    sendEvent(Event.CREATE_FROM_CLASS, getServiceClass());
    ServiceInfo serviceInfo = new ServiceInfo();
    SchemaCollection col = serviceInfo.getXmlSchemaCollection();
    col.getXmlSchemaCollection().setSchemaResolver(new CatalogXmlSchemaURIResolver(this.getBus()));
    col.getExtReg().registerSerializer(MimeAttribute.class, new MimeSerializer());
    ServiceImpl service = new ServiceImpl(serviceInfo);
    setService(service);
    setServiceProperties();
    serviceInfo.setName(getServiceQName());
    serviceInfo.setTargetNamespace(serviceInfo.getName().getNamespaceURI());
    sendEvent(Event.SERVICE_SET, getService());
    createInterface(serviceInfo);
    Set<?> wrapperClasses = this.getExtraClass();
    for (ServiceInfo si : getService().getServiceInfos()) {
        if (wrapperClasses != null && !wrapperClasses.isEmpty()) {
            si.setProperty(EXTRA_CLASS, wrapperClasses);
        }
    }
    initializeDataBindings();
    boolean isWrapped = isWrapped() || hasWrappedMethods(serviceInfo.getInterface());
    if (isWrapped) {
        initializeWrappedSchema(serviceInfo);
    }
    for (OperationInfo opInfo : serviceInfo.getInterface().getOperations()) {
        Method m = (Method) opInfo.getProperty(METHOD);
        if (!isWrapped(m) && !isRPC(m) && opInfo.getInput() != null) {
            createBareMessage(serviceInfo, opInfo, false);
        }
        if (!isWrapped(m) && !isRPC(m) && opInfo.getOutput() != null) {
            createBareMessage(serviceInfo, opInfo, true);
        }
        if (opInfo.hasFaults()) {
            // check to make sure the faults are elements
            for (FaultInfo fault : opInfo.getFaults()) {
                QName qn = (QName) fault.getProperty("elementName");
                MessagePartInfo part = fault.getFirstMessagePart();
                if (!part.isElement()) {
                    part.setElement(true);
                    part.setElementQName(qn);
                    checkForElement(serviceInfo, part);
                }
            }
        }
    }
    if (LOG.isLoggable(Level.FINE) || isValidate()) {
        ServiceModelSchemaValidator validator = new ServiceModelSchemaValidator(serviceInfo);
        validator.walk();
        String validationComplaints = validator.getComplaints();
        if (!"".equals(validationComplaints)) {
            if (isValidate()) {
                LOG.warning(validationComplaints);
            } else {
                LOG.fine(validationComplaints);
            }
        }
    }
}
Also used : OperationInfo(org.apache.cxf.service.model.OperationInfo) BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) UnwrappedOperationInfo(org.apache.cxf.service.model.UnwrappedOperationInfo) FaultInfo(org.apache.cxf.service.model.FaultInfo) Message(org.apache.cxf.common.i18n.Message) ServiceImpl(org.apache.cxf.service.ServiceImpl) QName(javax.xml.namespace.QName) ServiceModelSchemaValidator(org.apache.cxf.service.ServiceModelSchemaValidator) ServiceConstructionException(org.apache.cxf.service.factory.ServiceConstructionException) Method(java.lang.reflect.Method) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) ServiceInfo(org.apache.cxf.service.model.ServiceInfo) MimeSerializer(org.apache.cxf.databinding.source.mime.MimeSerializer) CatalogXmlSchemaURIResolver(org.apache.cxf.catalog.CatalogXmlSchemaURIResolver) SchemaCollection(org.apache.cxf.common.xmlschema.SchemaCollection)

Example 22 with SchemaCollection

use of org.apache.cxf.common.xmlschema.SchemaCollection in project cxf by apache.

the class AegisDatabinding method createSchemas.

private void createSchemas(Service s, Set<AegisType> deps) {
    Map<String, Set<AegisType>> tns2Type = new HashMap<>();
    for (AegisType t : deps) {
        String ns = t.getSchemaType().getNamespaceURI();
        Set<AegisType> types = tns2Type.get(ns);
        if (types == null) {
            types = new HashSet<>();
            tns2Type.put(ns, types);
        }
        types.add(t);
    }
    for (ServiceInfo si : s.getServiceInfos()) {
        SchemaCollection col = si.getXmlSchemaCollection();
        if (col.getXmlSchemas().length > 1) {
            // someone has already filled in the types
            continue;
        }
    }
    Map<String, String> namespaceMap = getDeclaredNamespaceMappings();
    for (ServiceInfo si : s.getServiceInfos()) {
        // these two must be recalculated per-service-info!
        boolean needXmimeSchema = false;
        boolean needTypesSchema = false;
        for (Map.Entry<String, Set<AegisType>> entry : tns2Type.entrySet()) {
            String schemaNamespaceUri = entry.getKey();
            if (Constants.URI_2001_SCHEMA_XSD.equals(schemaNamespaceUri)) {
                continue;
            }
            if (AegisContext.UTILITY_TYPES_SCHEMA_NS.equals(schemaNamespaceUri)) {
                // we handle this separately.
                continue;
            }
            if (AbstractXOPType.XML_MIME_NS.equals(schemaNamespaceUri)) {
                // similiarly.
                continue;
            }
            SchemaInfo schemaInfo = si.addNewSchema(entry.getKey());
            XmlSchema schema = schemaInfo.getSchema();
            NamespaceMap xmlsNamespaceMap = new NamespaceMap();
            // user-requested prefix mappings.
            if (namespaceMap != null) {
                for (Map.Entry<String, String> e : namespaceMap.entrySet()) {
                    xmlsNamespaceMap.add(e.getValue(), e.getKey());
                }
            }
            // tns: is conventional, and besides we have unit tests that are hardcoded to it.
            if (!xmlsNamespaceMap.containsKey(WSDLConstants.CONVENTIONAL_TNS_PREFIX) && // if some wants something other than TNS, they get it.
            !xmlsNamespaceMap.containsValue(entry.getKey())) {
                xmlsNamespaceMap.add(WSDLConstants.CONVENTIONAL_TNS_PREFIX, entry.getKey());
            }
            // ditto for xsd: instead of just namespace= for the schema schema.
            if (!xmlsNamespaceMap.containsKey("xsd") && !xmlsNamespaceMap.containsValue(Constants.URI_2001_SCHEMA_XSD)) {
                xmlsNamespaceMap.add("xsd", Constants.URI_2001_SCHEMA_XSD);
            }
            schema.setNamespaceContext(xmlsNamespaceMap);
            schema.setTargetNamespace(entry.getKey());
            schema.setElementFormDefault(XmlSchemaForm.QUALIFIED);
            schema.setAttributeFormDefault(XmlSchemaForm.QUALIFIED);
            for (AegisType t : entry.getValue()) {
                try {
                    t.writeSchema(schema);
                } catch (XmlSchemaException ex) {
                    QName name = t.getSchemaType();
                    String expected = " Schema for namespace '" + name.getNamespaceURI() + "' already contains type '" + name.getLocalPart() + "'";
                    String message = ex.getMessage();
                    if (expected.equals(message)) {
                        continue;
                    }
                    throw ex;
                }
            }
            if (schemaImportsXmime(schema)) {
                needXmimeSchema = true;
            }
            if (AegisContext.schemaImportsUtilityTypes(schema)) {
                needTypesSchema = true;
            }
        }
        if (needXmimeSchema) {
            XmlSchema schema = aegisContext.addXmimeSchemaDocument(si.getXmlSchemaCollection().getXmlSchemaCollection());
            SchemaInfo schemaInfo = new SchemaInfo(schema.getTargetNamespace());
            schemaInfo.setSchema(schema);
            si.addSchema(schemaInfo);
        }
        if (needTypesSchema) {
            XmlSchema schema = aegisContext.addTypesSchemaDocument(si.getXmlSchemaCollection().getXmlSchemaCollection());
            SchemaInfo schemaInfo = new SchemaInfo(schema.getTargetNamespace());
            schemaInfo.setSchema(schema);
            si.addSchema(schemaInfo);
        }
        // it's quite likely that the code in Aegis missed at least one ...
        si.getXmlSchemaCollection().addCrossImports();
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) AegisType(org.apache.cxf.aegis.type.AegisType) XmlSchemaException(org.apache.ws.commons.schema.XmlSchemaException) QName(javax.xml.namespace.QName) ServiceInfo(org.apache.cxf.service.model.ServiceInfo) XmlSchema(org.apache.ws.commons.schema.XmlSchema) NamespaceMap(org.apache.ws.commons.schema.utils.NamespaceMap) SchemaCollection(org.apache.cxf.common.xmlschema.SchemaCollection) HashMap(java.util.HashMap) Map(java.util.Map) NamespaceMap(org.apache.ws.commons.schema.utils.NamespaceMap) SchemaInfo(org.apache.cxf.service.model.SchemaInfo)

Example 23 with SchemaCollection

use of org.apache.cxf.common.xmlschema.SchemaCollection in project cxf by apache.

the class AegisDatabinding method initializeMessageTypes.

protected void initializeMessageTypes(ServiceInfo s, AbstractMessageContainer container, int partType) {
    if (container == null) {
        return;
    }
    SchemaCollection col = s.getXmlSchemaCollection();
    for (MessagePartInfo part : container.getMessageParts()) {
        if (part.getXmlSchema() == null) {
            if (part.isElement()) {
                XmlSchemaAnnotated tp = col.getElementByQName(part.getElementQName());
                part.setXmlSchema(tp);
            } else {
                XmlSchemaAnnotated tp = col.getTypeByQName(part.getTypeQName());
                part.setXmlSchema(tp);
            }
        }
    }
}
Also used : XmlSchemaAnnotated(org.apache.ws.commons.schema.XmlSchemaAnnotated) SchemaCollection(org.apache.cxf.common.xmlschema.SchemaCollection) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo)

Aggregations

SchemaCollection (org.apache.cxf.common.xmlschema.SchemaCollection)23 ServiceInfo (org.apache.cxf.service.model.ServiceInfo)12 QName (javax.xml.namespace.QName)8 ArrayList (java.util.ArrayList)7 IOException (java.io.IOException)6 MessagePartInfo (org.apache.cxf.service.model.MessagePartInfo)6 XmlSchemaElement (org.apache.ws.commons.schema.XmlSchemaElement)5 HashMap (java.util.HashMap)4 LinkedHashMap (java.util.LinkedHashMap)4 Definition (javax.wsdl.Definition)4 Message (org.apache.cxf.common.i18n.Message)4 XmlSchema (org.apache.ws.commons.schema.XmlSchema)4 HashSet (java.util.HashSet)3 List (java.util.List)3 Map (java.util.Map)3 SchemaInfo (org.apache.cxf.service.model.SchemaInfo)3 ToolException (org.apache.cxf.tools.common.ToolException)3 XmlSchemaType (org.apache.ws.commons.schema.XmlSchemaType)3 File (java.io.File)2 URL (java.net.URL)2