Search in sources :

Example 1 with JAXBBeanInfo

use of org.apache.cxf.common.jaxb.JAXBBeanInfo in project tomee by apache.

the class JAXBDataBinding method checkForJAXBAnnotations.

private void checkForJAXBAnnotations(MessagePartInfo mpi, SchemaCollection schemaCollection, String ns) {
    Annotation[] anns = (Annotation[]) mpi.getProperty("parameter.annotations");
    JAXBContextProxy ctx = JAXBUtils.createJAXBContextProxy(context, schemaCollection, ns);
    XmlJavaTypeAdapter jta = JAXBSchemaInitializer.findFromTypeAdapter(ctx, mpi.getTypeClass(), anns);
    if (jta != null) {
        JAXBBeanInfo jtaBeanInfo = JAXBSchemaInitializer.findFromTypeAdapter(ctx, jta.value());
        JAXBBeanInfo beanInfo = JAXBSchemaInitializer.getBeanInfo(ctx, mpi.getTypeClass());
        if (jtaBeanInfo != beanInfo) {
            mpi.setProperty("parameter.annotations", anns);
            mpi.setProperty("honor.jaxb.annotations", Boolean.TRUE);
        }
    }
}
Also used : XmlJavaTypeAdapter(javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter) JAXBBeanInfo(org.apache.cxf.common.jaxb.JAXBBeanInfo) Annotation(java.lang.annotation.Annotation) JAXBContextProxy(org.apache.cxf.common.jaxb.JAXBContextProxy)

Example 2 with JAXBBeanInfo

use of org.apache.cxf.common.jaxb.JAXBBeanInfo in project tomee by apache.

the class JAXBSchemaInitializer method buildExceptionType.

private void buildExceptionType(MessagePartInfo part, Class<?> cls) {
    SchemaInfo schemaInfo = null;
    for (SchemaInfo s : serviceInfo.getSchemas()) {
        if (s.getNamespaceURI().equals(part.getElementQName().getNamespaceURI())) {
            schemaInfo = s;
            break;
        }
    }
    XmlAccessorOrder xmlAccessorOrder = cls.getAnnotation(XmlAccessorOrder.class);
    XmlType xmlTypeAnno = cls.getAnnotation(XmlType.class);
    String[] propertyOrder = null;
    boolean respectXmlTypeNS = false;
    XmlSchema faultBeanSchema = null;
    if (xmlTypeAnno != null && !StringUtils.isEmpty(xmlTypeAnno.namespace()) && !xmlTypeAnno.namespace().equals(part.getElementQName().getNamespaceURI())) {
        respectXmlTypeNS = true;
        NamespaceMap nsMap = new NamespaceMap();
        nsMap.add(WSDLConstants.CONVENTIONAL_TNS_PREFIX, xmlTypeAnno.namespace());
        nsMap.add(WSDLConstants.NP_SCHEMA_XSD, WSDLConstants.NS_SCHEMA_XSD);
        SchemaInfo faultBeanSchemaInfo = createSchemaIfNeeded(xmlTypeAnno.namespace(), nsMap);
        faultBeanSchema = faultBeanSchemaInfo.getSchema();
    }
    if (xmlTypeAnno != null && xmlTypeAnno.propOrder().length > 0) {
        propertyOrder = xmlTypeAnno.propOrder();
    // TODO: handle @XmlAccessOrder
    }
    if (schemaInfo == null) {
        NamespaceMap nsMap = new NamespaceMap();
        nsMap.add(WSDLConstants.CONVENTIONAL_TNS_PREFIX, part.getElementQName().getNamespaceURI());
        nsMap.add(WSDLConstants.NP_SCHEMA_XSD, WSDLConstants.NS_SCHEMA_XSD);
        schemaInfo = createSchemaIfNeeded(part.getElementQName().getNamespaceURI(), nsMap);
    }
    XmlSchema schema = schemaInfo.getSchema();
    // Before updating everything, make sure we haven't added this
    // type yet.  Multiple methods that throw the same exception
    // types will cause duplicates.
    String faultTypeName = xmlTypeAnno != null && !StringUtils.isEmpty(xmlTypeAnno.name()) ? xmlTypeAnno.name() : part.getElementQName().getLocalPart();
    XmlSchemaType existingType = schema.getTypeByName(faultTypeName);
    if (existingType != null) {
        return;
    }
    XmlSchemaElement el = new XmlSchemaElement(schema, true);
    el.setName(part.getElementQName().getLocalPart());
    part.setXmlSchema(el);
    schemaInfo.setElement(null);
    if (respectXmlTypeNS) {
        // create complexType in the new created schema for xmlType
        schema = faultBeanSchema;
    }
    XmlSchemaComplexType ct = new XmlSchemaComplexType(schema, true);
    ct.setName(faultTypeName);
    el.setSchemaTypeName(ct.getQName());
    XmlSchemaSequence seq = new XmlSchemaSequence();
    ct.setParticle(seq);
    String namespace = part.getElementQName().getNamespaceURI();
    XmlAccessType accessType = Utils.getXmlAccessType(cls);
    // 
    for (Field f : Utils.getFields(cls, accessType)) {
        // map field
        Type type = Utils.getFieldType(f);
        // generic return type.
        if ((type == null) && (f.getGenericType() instanceof ParameterizedType)) {
            type = f.getGenericType();
        }
        if (generateGenericType(type)) {
            buildGenericElements(schema, seq, f);
        } else {
            JAXBBeanInfo beanInfo = getBeanInfo(type);
            if (beanInfo != null) {
                XmlElement xmlElementAnno = f.getAnnotation(XmlElement.class);
                addElement(schema, seq, beanInfo, new QName(namespace, f.getName()), isArray(type), xmlElementAnno);
            }
        }
    }
    for (Method m : Utils.getGetters(cls, accessType)) {
        // map method
        Type type = Utils.getMethodReturnType(m);
        // generic return type.
        if ((type == null) && (m.getGenericReturnType() instanceof ParameterizedType)) {
            type = m.getGenericReturnType();
        }
        if (generateGenericType(type)) {
            buildGenericElements(schema, seq, m, type);
        } else {
            JAXBBeanInfo beanInfo = getBeanInfo(type);
            if (beanInfo != null) {
                int idx = m.getName().startsWith("get") ? 3 : 2;
                String name = m.getName().substring(idx);
                name = Character.toLowerCase(name.charAt(0)) + name.substring(1);
                XmlElement xmlElementAnno = m.getAnnotation(XmlElement.class);
                addElement(schema, seq, beanInfo, new QName(namespace, name), isArray(type), xmlElementAnno);
            }
        }
    }
    // Create element in xsd:sequence for Exception.class
    if (Exception.class.isAssignableFrom(cls)) {
        addExceptionMessage(cls, schema, seq);
    }
    if (propertyOrder != null) {
        if (propertyOrder.length == seq.getItems().size()) {
            sortItems(seq, propertyOrder);
        } else if (propertyOrder.length > 1 || (propertyOrder.length == 1 && !propertyOrder[0].isEmpty())) {
            LOG.log(Level.WARNING, "propOrder in @XmlType doesn't define all schema elements :" + Arrays.toString(propertyOrder));
        }
    }
    if (xmlAccessorOrder != null && xmlAccessorOrder.value().equals(XmlAccessOrder.ALPHABETICAL) && propertyOrder == null) {
        sort(seq);
    }
    schemas.addCrossImports();
    part.setProperty(JAXBDataBinding.class.getName() + ".CUSTOM_EXCEPTION", Boolean.TRUE);
}
Also used : XmlAccessorOrder(javax.xml.bind.annotation.XmlAccessorOrder) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) QName(javax.xml.namespace.QName) Method(java.lang.reflect.Method) XmlSchemaType(org.apache.ws.commons.schema.XmlSchemaType) XmlType(javax.xml.bind.annotation.XmlType) ParameterizedType(java.lang.reflect.ParameterizedType) XmlSchemaSequence(org.apache.ws.commons.schema.XmlSchemaSequence) Field(java.lang.reflect.Field) GenericArrayType(java.lang.reflect.GenericArrayType) XmlSchemaComplexType(org.apache.ws.commons.schema.XmlSchemaComplexType) XmlSchemaType(org.apache.ws.commons.schema.XmlSchemaType) XmlAccessType(javax.xml.bind.annotation.XmlAccessType) Type(java.lang.reflect.Type) XmlSchemaSimpleType(org.apache.ws.commons.schema.XmlSchemaSimpleType) XmlType(javax.xml.bind.annotation.XmlType) ParameterizedType(java.lang.reflect.ParameterizedType) XmlSchema(org.apache.ws.commons.schema.XmlSchema) NamespaceMap(org.apache.ws.commons.schema.utils.NamespaceMap) JAXBBeanInfo(org.apache.cxf.common.jaxb.JAXBBeanInfo) XmlElement(javax.xml.bind.annotation.XmlElement) XmlSchemaComplexType(org.apache.ws.commons.schema.XmlSchemaComplexType) XmlAccessType(javax.xml.bind.annotation.XmlAccessType) SchemaInfo(org.apache.cxf.service.model.SchemaInfo)

Example 3 with JAXBBeanInfo

use of org.apache.cxf.common.jaxb.JAXBBeanInfo in project tomee by apache.

the class JAXBSchemaInitializer method begin.

@Override
public void begin(MessagePartInfo part) {
    // Check to see if the WSDL information has been filled in for us.
    if (part.getTypeQName() != null || part.getElementQName() != null) {
        checkForExistence(part);
        return;
    }
    Class<?> clazz = part.getTypeClass();
    if (clazz == null) {
        return;
    }
    boolean isFromWrapper = part.getMessageInfo().getOperation().isUnwrapped();
    boolean isList = false;
    if (clazz.isArray()) {
        if (isFromWrapper && !Byte.TYPE.equals(clazz.getComponentType())) {
            clazz = clazz.getComponentType();
        } else if (!isFromWrapper) {
            Annotation[] anns = (Annotation[]) part.getProperty("parameter.annotations");
            for (Annotation a : anns) {
                if (a instanceof XmlList) {
                    part.setProperty("honor.jaxb.annotations", Boolean.TRUE);
                    clazz = clazz.getComponentType();
                    isList = true;
                }
            }
        }
    }
    Annotation[] anns = (Annotation[]) part.getProperty("parameter.annotations");
    XmlJavaTypeAdapter jta = findFromTypeAdapter(context, clazz, anns);
    JAXBBeanInfo jtaBeanInfo = null;
    if (jta != null) {
        jtaBeanInfo = findFromTypeAdapter(context, jta.value());
    }
    JAXBBeanInfo beanInfo = getBeanInfo(clazz);
    if (jtaBeanInfo != beanInfo && jta != null) {
        beanInfo = jtaBeanInfo;
        if (anns == null) {
            anns = new Annotation[] { jta };
        } else {
            boolean found = false;
            for (Annotation t : anns) {
                if (t == jta) {
                    found = true;
                }
            }
            if (!found) {
                Annotation[] tmp = new Annotation[anns.length + 1];
                System.arraycopy(anns, 0, tmp, 0, anns.length);
                tmp[anns.length] = jta;
                anns = tmp;
            }
        }
        part.setProperty("parameter.annotations", anns);
        part.setProperty("honor.jaxb.annotations", Boolean.TRUE);
    }
    if (beanInfo == null) {
        if (Exception.class.isAssignableFrom(clazz)) {
            QName name = (QName) part.getMessageInfo().getProperty("elementName");
            part.setElementQName(name);
            buildExceptionType(part, clazz);
        }
        return;
    }
    boolean isElement = beanInfo.isElement() && !Boolean.TRUE.equals(part.getMessageInfo().getOperation().getProperty("operation.force.types"));
    boolean hasType = !beanInfo.getTypeNames().isEmpty();
    if (isElement && isFromWrapper && hasType) {
        // if there is both a Global element and a global type, AND we are in a wrapper,
        // make sure we use the type instead of a ref to the element to
        // match the rules for wrapped/unwrapped
        isElement = false;
    }
    part.setElement(isElement);
    if (isElement) {
        QName name = new QName(beanInfo.getElementNamespaceURI(null), beanInfo.getElementLocalName(null));
        XmlSchemaElement el = schemas.getElementByQName(name);
        if (el != null && el.getRef().getTarget() != null) {
            part.setTypeQName(el.getRef().getTargetQName());
        } else {
            part.setElementQName(name);
        }
        part.setXmlSchema(el);
    } else {
        QName typeName = getTypeName(beanInfo);
        if (typeName != null) {
            XmlSchemaType type = schemas.getTypeByQName(typeName);
            if (isList && type instanceof XmlSchemaSimpleType) {
                XmlSchemaSimpleType simpleType = new XmlSchemaSimpleType(type.getParent(), false);
                XmlSchemaSimpleTypeList list = new XmlSchemaSimpleTypeList();
                XmlSchemaSimpleType stype = (XmlSchemaSimpleType) type;
                list.setItemTypeName(stype.getQName());
                simpleType.setContent(list);
                part.setXmlSchema(simpleType);
                if (part.getConcreteName() == null) {
                    part.setConcreteName(new QName(null, part.getName().getLocalPart()));
                }
            } else {
                part.setTypeQName(typeName);
                part.setXmlSchema(type);
            }
        }
    }
}
Also used : XmlSchemaSimpleTypeList(org.apache.ws.commons.schema.XmlSchemaSimpleTypeList) QName(javax.xml.namespace.QName) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) XmlSchemaSimpleType(org.apache.ws.commons.schema.XmlSchemaSimpleType) XmlJavaTypeAdapter(javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter) JAXBBeanInfo(org.apache.cxf.common.jaxb.JAXBBeanInfo) XmlSchemaType(org.apache.ws.commons.schema.XmlSchemaType) Annotation(java.lang.annotation.Annotation) XmlList(javax.xml.bind.annotation.XmlList)

Example 4 with JAXBBeanInfo

use of org.apache.cxf.common.jaxb.JAXBBeanInfo in project tomee by apache.

the class JAXBSchemaInitializer method checkForExistence.

public void checkForExistence(MessagePartInfo part) {
    QName qn = part.getElementQName();
    if (qn != null) {
        XmlSchemaElement el = schemas.getElementByQName(qn);
        if (el == null) {
            Class<?> clazz = part.getTypeClass();
            if (clazz == null) {
                return;
            }
            boolean isFromWrapper = part.getMessageInfo().getOperation().isUnwrapped();
            if (isFromWrapper && clazz.isArray() && !Byte.TYPE.equals(clazz.getComponentType())) {
                clazz = clazz.getComponentType();
            }
            JAXBBeanInfo beanInfo = getBeanInfo(clazz);
            if (beanInfo == null) {
                if (Exception.class.isAssignableFrom(clazz)) {
                    QName name = (QName) part.getMessageInfo().getProperty("elementName");
                    part.setElementQName(name);
                    buildExceptionType(part, clazz);
                }
                return;
            }
            QName typeName = getTypeName(beanInfo);
            createBridgeXsElement(part, qn, typeName);
        } else if (part.getXmlSchema() == null) {
            part.setXmlSchema(el);
        }
    }
}
Also used : QName(javax.xml.namespace.QName) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) JAXBBeanInfo(org.apache.cxf.common.jaxb.JAXBBeanInfo)

Example 5 with JAXBBeanInfo

use of org.apache.cxf.common.jaxb.JAXBBeanInfo in project cxf by apache.

the class JAXBSchemaInitializer method buildExceptionType.

private void buildExceptionType(MessagePartInfo part, Class<?> cls) {
    SchemaInfo schemaInfo = null;
    for (SchemaInfo s : serviceInfo.getSchemas()) {
        if (s.getNamespaceURI().equals(part.getElementQName().getNamespaceURI())) {
            schemaInfo = s;
            break;
        }
    }
    XmlAccessorOrder xmlAccessorOrder = cls.getAnnotation(XmlAccessorOrder.class);
    XmlType xmlTypeAnno = cls.getAnnotation(XmlType.class);
    String[] propertyOrder = null;
    boolean respectXmlTypeNS = false;
    XmlSchema faultBeanSchema = null;
    if (xmlTypeAnno != null && !StringUtils.isEmpty(xmlTypeAnno.namespace()) && !xmlTypeAnno.namespace().equals(part.getElementQName().getNamespaceURI())) {
        respectXmlTypeNS = true;
        NamespaceMap nsMap = new NamespaceMap();
        nsMap.add(WSDLConstants.CONVENTIONAL_TNS_PREFIX, xmlTypeAnno.namespace());
        nsMap.add(WSDLConstants.NP_SCHEMA_XSD, WSDLConstants.NS_SCHEMA_XSD);
        SchemaInfo faultBeanSchemaInfo = createSchemaIfNeeded(xmlTypeAnno.namespace(), nsMap);
        faultBeanSchema = faultBeanSchemaInfo.getSchema();
    }
    if (xmlTypeAnno != null && xmlTypeAnno.propOrder().length > 0) {
        propertyOrder = xmlTypeAnno.propOrder();
    // TODO: handle @XmlAccessOrder
    }
    if (schemaInfo == null) {
        NamespaceMap nsMap = new NamespaceMap();
        nsMap.add(WSDLConstants.CONVENTIONAL_TNS_PREFIX, part.getElementQName().getNamespaceURI());
        nsMap.add(WSDLConstants.NP_SCHEMA_XSD, WSDLConstants.NS_SCHEMA_XSD);
        schemaInfo = createSchemaIfNeeded(part.getElementQName().getNamespaceURI(), nsMap);
    }
    XmlSchema schema = schemaInfo.getSchema();
    // Before updating everything, make sure we haven't added this
    // type yet.  Multiple methods that throw the same exception
    // types will cause duplicates.
    String faultTypeName = xmlTypeAnno != null && !StringUtils.isEmpty(xmlTypeAnno.name()) ? xmlTypeAnno.name() : part.getElementQName().getLocalPart();
    XmlSchemaType existingType = schema.getTypeByName(faultTypeName);
    if (existingType != null) {
        return;
    }
    XmlSchemaElement el = new XmlSchemaElement(schema, true);
    el.setName(part.getElementQName().getLocalPart());
    part.setXmlSchema(el);
    schemaInfo.setElement(null);
    if (respectXmlTypeNS) {
        // create complexType in the new created schema for xmlType
        schema = faultBeanSchema;
    }
    XmlSchemaComplexType ct = new XmlSchemaComplexType(schema, true);
    ct.setName(faultTypeName);
    el.setSchemaTypeName(ct.getQName());
    XmlSchemaSequence seq = new XmlSchemaSequence();
    ct.setParticle(seq);
    String namespace = part.getElementQName().getNamespaceURI();
    XmlAccessType accessType = Utils.getXmlAccessType(cls);
    // 
    for (Field f : Utils.getFields(cls, accessType)) {
        // map field
        Type type = Utils.getFieldType(f);
        // generic return type.
        if ((type == null) && (f.getGenericType() instanceof ParameterizedType)) {
            type = f.getGenericType();
        }
        if (generateGenericType(type)) {
            buildGenericElements(schema, seq, f);
        } else {
            JAXBBeanInfo beanInfo = getBeanInfo(type);
            if (beanInfo != null) {
                XmlElement xmlElementAnno = f.getAnnotation(XmlElement.class);
                addElement(schema, seq, beanInfo, new QName(namespace, f.getName()), isArray(type), xmlElementAnno);
            }
        }
    }
    for (Method m : Utils.getGetters(cls, accessType)) {
        // map method
        Type type = Utils.getMethodReturnType(m);
        // generic return type.
        if ((type == null) && (m.getGenericReturnType() instanceof ParameterizedType)) {
            type = m.getGenericReturnType();
        }
        if (generateGenericType(type)) {
            buildGenericElements(schema, seq, m, type);
        } else {
            JAXBBeanInfo beanInfo = getBeanInfo(type);
            if (beanInfo != null) {
                int idx = m.getName().startsWith("get") ? 3 : 2;
                String name = m.getName().substring(idx);
                name = Character.toLowerCase(name.charAt(0)) + name.substring(1);
                XmlElement xmlElementAnno = m.getAnnotation(XmlElement.class);
                addElement(schema, seq, beanInfo, new QName(namespace, name), isArray(type), xmlElementAnno);
            }
        }
    }
    // Create element in xsd:sequence for Exception.class
    if (Exception.class.isAssignableFrom(cls)) {
        addExceptionMessage(cls, schema, seq);
    }
    if (propertyOrder != null) {
        if (propertyOrder.length == seq.getItems().size()) {
            sortItems(seq, propertyOrder);
        } else if (propertyOrder.length > 1 || (propertyOrder.length == 1 && !propertyOrder[0].isEmpty())) {
            LOG.log(Level.WARNING, "propOrder in @XmlType doesn't define all schema elements :" + Arrays.toString(propertyOrder));
        }
    }
    if (xmlAccessorOrder != null && xmlAccessorOrder.value().equals(XmlAccessOrder.ALPHABETICAL) && propertyOrder == null) {
        sort(seq);
    }
    schemas.addCrossImports();
    part.setProperty(JAXBDataBinding.class.getName() + ".CUSTOM_EXCEPTION", Boolean.TRUE);
}
Also used : XmlAccessorOrder(javax.xml.bind.annotation.XmlAccessorOrder) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) QName(javax.xml.namespace.QName) Method(java.lang.reflect.Method) XmlSchemaType(org.apache.ws.commons.schema.XmlSchemaType) XmlType(javax.xml.bind.annotation.XmlType) ParameterizedType(java.lang.reflect.ParameterizedType) XmlSchemaSequence(org.apache.ws.commons.schema.XmlSchemaSequence) Field(java.lang.reflect.Field) GenericArrayType(java.lang.reflect.GenericArrayType) XmlSchemaComplexType(org.apache.ws.commons.schema.XmlSchemaComplexType) XmlSchemaType(org.apache.ws.commons.schema.XmlSchemaType) XmlAccessType(javax.xml.bind.annotation.XmlAccessType) Type(java.lang.reflect.Type) XmlSchemaSimpleType(org.apache.ws.commons.schema.XmlSchemaSimpleType) XmlType(javax.xml.bind.annotation.XmlType) ParameterizedType(java.lang.reflect.ParameterizedType) XmlSchema(org.apache.ws.commons.schema.XmlSchema) NamespaceMap(org.apache.ws.commons.schema.utils.NamespaceMap) JAXBBeanInfo(org.apache.cxf.common.jaxb.JAXBBeanInfo) XmlElement(javax.xml.bind.annotation.XmlElement) XmlSchemaComplexType(org.apache.ws.commons.schema.XmlSchemaComplexType) XmlAccessType(javax.xml.bind.annotation.XmlAccessType) SchemaInfo(org.apache.cxf.service.model.SchemaInfo)

Aggregations

JAXBBeanInfo (org.apache.cxf.common.jaxb.JAXBBeanInfo)15 XmlSchemaElement (org.apache.ws.commons.schema.XmlSchemaElement)12 QName (javax.xml.namespace.QName)10 Method (java.lang.reflect.Method)8 XmlSchemaSequence (org.apache.ws.commons.schema.XmlSchemaSequence)6 Annotation (java.lang.annotation.Annotation)4 Field (java.lang.reflect.Field)4 XmlAccessType (javax.xml.bind.annotation.XmlAccessType)4 XmlJavaTypeAdapter (javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter)4 SchemaInfo (org.apache.cxf.service.model.SchemaInfo)4 XmlSchemaComplexType (org.apache.ws.commons.schema.XmlSchemaComplexType)4 XmlSchemaSimpleType (org.apache.ws.commons.schema.XmlSchemaSimpleType)4 XmlSchemaType (org.apache.ws.commons.schema.XmlSchemaType)4 GenericArrayType (java.lang.reflect.GenericArrayType)2 ParameterizedType (java.lang.reflect.ParameterizedType)2 Type (java.lang.reflect.Type)2 TypeVariable (java.lang.reflect.TypeVariable)2 Iterator (java.util.Iterator)2 XmlAccessorOrder (javax.xml.bind.annotation.XmlAccessorOrder)2 XmlElement (javax.xml.bind.annotation.XmlElement)2