Search in sources :

Example 1 with XmlSchemaException

use of org.apache.ws.commons.schema.XmlSchemaException in project cxf by apache.

the class CatalogXmlSchemaURIResolver method resolveEntity.

public InputSource resolveEntity(String targetNamespace, String schemaLocation, String baseUri) {
    String resolvedSchemaLocation = null;
    OASISCatalogManager catalogResolver = OASISCatalogManager.getCatalogManager(bus);
    try {
        resolvedSchemaLocation = new OASISCatalogManagerHelper().resolve(catalogResolver, schemaLocation, baseUri);
    } catch (Exception e) {
        throw new RuntimeException("Catalog resolution failed", e);
    }
    InputSource in = null;
    if (resolvedSchemaLocation == null) {
        in = this.resolver.resolve(schemaLocation, baseUri);
    } else {
        resolved.put(schemaLocation, resolvedSchemaLocation);
        in = this.resolver.resolve(resolvedSchemaLocation, baseUri);
    }
    // but without any nice error message. So let's just throw a nice error here.
    if (in == null) {
        throw new XmlSchemaException("Unable to locate imported document " + "at '" + schemaLocation + "'" + (baseUri == null ? "." : ", relative to '" + baseUri + "'."));
    } else if (in.getByteStream() != null && !(in.getByteStream() instanceof ByteArrayInputStream)) {
        // file handles.  We'll just load the file into a byte[] and return that.
        try {
            InputStream ins = IOUtils.loadIntoBAIS(in.getByteStream());
            in.setByteStream(ins);
        } catch (IOException e) {
            throw new XmlSchemaException("Unable to load imported document " + "at '" + schemaLocation + "'" + (baseUri == null ? "." : ", relative to '" + baseUri + "'."), e);
        }
    }
    return in;
}
Also used : InputSource(org.xml.sax.InputSource) XmlSchemaException(org.apache.ws.commons.schema.XmlSchemaException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) IOException(java.io.IOException) XmlSchemaException(org.apache.ws.commons.schema.XmlSchemaException)

Example 2 with XmlSchemaException

use of org.apache.ws.commons.schema.XmlSchemaException in project cxf by apache.

the class WSDLServiceFactory method create.

public Service create() {
    List<ServiceInfo> services;
    if (serviceName == null) {
        try {
            WSDLServiceBuilder builder = new WSDLServiceBuilder(getBus());
            builder.setAllowElementRefs(allowRefs);
            services = builder.buildServices(definition);
        } catch (XmlSchemaException ex) {
            throw new ServiceConstructionException(new Message("SERVICE_CREATION_MSG", LOG), ex);
        }
        if (services.isEmpty()) {
            throw new ServiceConstructionException(new Message("NO_SERVICE_EXC", LOG));
        }
        // @@TODO  - this isn't good, need to return all the services
        serviceName = services.get(0).getName();
        // get all the service info's that match that first one.
        Iterator<ServiceInfo> it = services.iterator();
        while (it.hasNext()) {
            if (!it.next().getName().equals(serviceName)) {
                it.remove();
            }
        }
    } else {
        javax.wsdl.Service wsdlService = definition.getService(serviceName);
        if (wsdlService == null) {
            if ((!PartialWSDLProcessor.isServiceExisted(definition, serviceName)) && (!PartialWSDLProcessor.isBindingExisted(definition, serviceName)) && (PartialWSDLProcessor.isPortTypeExisted(definition, serviceName))) {
                try {
                    Map<QName, PortType> portTypes = CastUtils.cast(definition.getAllPortTypes());
                    String existPortName = null;
                    PortType portType = null;
                    for (Map.Entry<QName, PortType> entry : portTypes.entrySet()) {
                        existPortName = entry.getKey().getLocalPart();
                        if (serviceName.getLocalPart().contains(existPortName)) {
                            portType = entry.getValue();
                            break;
                        }
                    }
                    WSDLFactory factory = WSDLFactory.newInstance();
                    ExtensionRegistry extReg = factory.newPopulatedExtensionRegistry();
                    Binding binding = PartialWSDLProcessor.doAppendBinding(definition, existPortName, portType, extReg);
                    definition.addBinding(binding);
                    wsdlService = PartialWSDLProcessor.doAppendService(definition, existPortName, extReg, binding);
                    definition.addService(wsdlService);
                } catch (Exception e) {
                    throw new ServiceConstructionException(new Message("NO_SUCH_SERVICE_EXC", LOG, serviceName));
                }
            } else {
                throw new ServiceConstructionException(new Message("NO_SUCH_SERVICE_EXC", LOG, serviceName));
            }
        }
        try {
            services = new WSDLServiceBuilder(getBus()).buildServices(definition, wsdlService, endpointName);
            if (services.isEmpty()) {
                throw new ServiceConstructionException(new Message("NO_SUCH_ENDPOINT_EXC", LOG, endpointName));
            }
        } catch (XmlSchemaException ex) {
            throw new ServiceConstructionException(new Message("SERVICE_CREATION_MSG", LOG), ex);
        }
    }
    ServiceImpl service = new ServiceImpl(services);
    setService(service);
    return service;
}
Also used : Binding(javax.wsdl.Binding) Message(org.apache.cxf.common.i18n.Message) XmlSchemaException(org.apache.ws.commons.schema.XmlSchemaException) QName(javax.xml.namespace.QName) ServiceImpl(org.apache.cxf.service.ServiceImpl) ServiceConstructionException(org.apache.cxf.service.factory.ServiceConstructionException) XmlSchemaException(org.apache.ws.commons.schema.XmlSchemaException) WSDLException(javax.wsdl.WSDLException) ServiceConstructionException(org.apache.cxf.service.factory.ServiceConstructionException) ExtensionRegistry(javax.wsdl.extensions.ExtensionRegistry) ServiceInfo(org.apache.cxf.service.model.ServiceInfo) WSDLFactory(javax.wsdl.factory.WSDLFactory) Map(java.util.Map) PortType(javax.wsdl.PortType)

Example 3 with XmlSchemaException

use of org.apache.ws.commons.schema.XmlSchemaException 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)

Aggregations

XmlSchemaException (org.apache.ws.commons.schema.XmlSchemaException)3 Map (java.util.Map)2 QName (javax.xml.namespace.QName)2 ServiceInfo (org.apache.cxf.service.model.ServiceInfo)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 Binding (javax.wsdl.Binding)1 PortType (javax.wsdl.PortType)1 WSDLException (javax.wsdl.WSDLException)1 ExtensionRegistry (javax.wsdl.extensions.ExtensionRegistry)1 WSDLFactory (javax.wsdl.factory.WSDLFactory)1 AegisType (org.apache.cxf.aegis.type.AegisType)1 Message (org.apache.cxf.common.i18n.Message)1 SchemaCollection (org.apache.cxf.common.xmlschema.SchemaCollection)1 ServiceImpl (org.apache.cxf.service.ServiceImpl)1 ServiceConstructionException (org.apache.cxf.service.factory.ServiceConstructionException)1