Search in sources :

Example 1 with XmlAccessType

use of javax.xml.bind.annotation.XmlAccessType 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 2 with XmlAccessType

use of javax.xml.bind.annotation.XmlAccessType in project cxf by apache.

the class JAXBContextInitializer method walkReferences.

private void walkReferences(Class<?> cls) {
    if (cls == null) {
        return;
    }
    if (cls.getName().startsWith("java.") || cls.getName().startsWith("javax.")) {
        return;
    }
    // walk the public fields/methods to try and find all the classes. JAXB will only load the
    // EXACT classes in the fields/methods if they are in a different package. Thus,
    // subclasses won't be found and the xsi:type stuff won't work at all.
    // We'll grab the public field/method types and then add the ObjectFactory stuff
    // as well as look for jaxb.index files in those packages.
    XmlAccessType accessType = Utils.getXmlAccessType(cls);
    if (accessType != XmlAccessType.PROPERTY) {
        // only look for fields if we are instructed to
        // fields are accessible even if not public, must look at the declared fields
        // then walk to parents declared fields, etc...
        Field[] fields = ReflectionUtil.getDeclaredFields(cls);
        for (Field f : fields) {
            if (isFieldAccepted(f, accessType)) {
                XmlJavaTypeAdapter xjta = Utils.getFieldXJTA(f);
                if (xjta != null) {
                    Type t = Utils.getTypeFromXmlAdapter(xjta);
                    if (t != null) {
                        addType(t);
                        continue;
                    }
                }
                addType(f.getGenericType());
            }
        }
        walkReferences(cls.getSuperclass());
    }
    if (accessType != XmlAccessType.FIELD) {
        // only look for methods if we are instructed to
        Method[] methods = ReflectionUtil.getDeclaredMethods(cls);
        for (Method m : methods) {
            if (isMethodAccepted(m, accessType)) {
                XmlJavaTypeAdapter xjta = Utils.getMethodXJTA(m);
                if (xjta != null) {
                    Type t = Utils.getTypeFromXmlAdapter(xjta);
                    if (t != null) {
                        addType(t);
                        continue;
                    }
                }
                addType(m.getGenericReturnType());
                for (Type t : m.getGenericParameterTypes()) {
                    addType(t);
                }
            }
        }
    }
}
Also used : Field(java.lang.reflect.Field) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) XmlType(javax.xml.bind.annotation.XmlType) XmlAccessType(javax.xml.bind.annotation.XmlAccessType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) XmlJavaTypeAdapter(javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter) Method(java.lang.reflect.Method) XmlAccessType(javax.xml.bind.annotation.XmlAccessType)

Example 3 with XmlAccessType

use of javax.xml.bind.annotation.XmlAccessType in project cxf by apache.

the class JAXBEncoderDecoder method unmarshallException.

public static Exception unmarshallException(Unmarshaller u, Object source, MessagePartInfo part) {
    XMLStreamReader reader;
    if (source instanceof XMLStreamReader) {
        reader = (XMLStreamReader) source;
    } else if (source instanceof Element) {
        reader = StaxUtils.createXMLStreamReader((Element) source);
        try {
            // advance into the node
            reader.nextTag();
        } catch (XMLStreamException e) {
        // ignore
        }
    } else {
        throw new Fault(new Message("UNKNOWN_SOURCE", LOG, source.getClass().getName()));
    }
    try {
        QName qn = part.getElementQName();
        if (!qn.equals(reader.getName())) {
            throw new Fault(new Message("ELEMENT_NAME_MISMATCH", LOG, qn, reader.getName()));
        }
        Class<?> cls = part.getTypeClass();
        Object obj;
        try {
            Constructor<?> cons = cls.getConstructor();
            obj = cons.newInstance();
        } catch (NoSuchMethodException nse) {
            Constructor<?> cons = cls.getConstructor(new Class[] { String.class });
            obj = cons.newInstance(new Object[1]);
        }
        XmlAccessType accessType = Utils.getXmlAccessType(cls);
        reader.nextTag();
        while (reader.getEventType() == XMLStreamConstants.START_ELEMENT) {
            QName q = reader.getName();
            String fieldName = q.getLocalPart();
            Field f = Utils.getField(cls, accessType, fieldName);
            if (f != null) {
                Type type = f.getGenericType();
                ReflectionUtil.setAccessible(f);
                if (JAXBSchemaInitializer.isArray(type)) {
                    Class<?> compType = JAXBSchemaInitializer.getArrayComponentType(type);
                    List<Object> ret = unmarshallArray(u, reader, q, compType, createList(type));
                    Object o = ret;
                    if (!isList(type)) {
                        if (compType.isPrimitive()) {
                            o = java.lang.reflect.Array.newInstance(compType, ret.size());
                            for (int x = 0; x < ret.size(); x++) {
                                Array.set(o, x, ret.get(x));
                            }
                        } else {
                            o = ret.toArray((Object[]) Array.newInstance(compType, ret.size()));
                        }
                    }
                    f.set(obj, o);
                } else {
                    Object o = getElementValue(u.unmarshal(reader, Utils.getFieldType(f)));
                    Utils.setFieldValue(f, obj, o);
                }
            } else {
                String s = StringUtils.capitalize(q.getLocalPart());
                Method m = Utils.getMethod(cls, accessType, "get" + s);
                if (m == null) {
                    m = Utils.getMethod(cls, accessType, "is" + s);
                }
                Type type = m.getGenericReturnType();
                Object o;
                if (JAXBSchemaInitializer.isArray(type)) {
                    Class<?> compType = JAXBSchemaInitializer.getArrayComponentType(type);
                    List<Object> ret = unmarshallArray(u, reader, q, compType, createList(type));
                    o = ret;
                    if (!isList(type)) {
                        if (compType.isPrimitive()) {
                            o = java.lang.reflect.Array.newInstance(compType, ret.size());
                            for (int x = 0; x < ret.size(); x++) {
                                Array.set(o, x, ret.get(x));
                            }
                        } else {
                            o = ret.toArray((Object[]) Array.newInstance(compType, ret.size()));
                        }
                    }
                } else {
                    o = getElementValue(u.unmarshal(reader, Utils.getMethodReturnType(m)));
                }
                Method m2 = Utils.getMethod(cls, accessType, "set" + s, m.getReturnType());
                if (m2 != null) {
                    if (JAXBSchemaInitializer.isArray(type)) {
                        m2.invoke(obj, o);
                    } else {
                        Utils.setMethodValue(m, m2, obj, o);
                    }
                } else {
                    Field fn = ReflectionUtil.getDeclaredField(cls, q.getLocalPart());
                    if (fn != null) {
                        ReflectionUtil.setAccessible(fn);
                        fn.set(obj, o);
                    }
                }
            }
            if (reader.getEventType() == XMLStreamConstants.END_ELEMENT && q.equals(reader.getName())) {
                reader.next();
            }
        }
        return (Exception) obj;
    } catch (Exception e) {
        throw new Fault(new Message("MARSHAL_ERROR", LOG, e.getMessage()), e);
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) DepthXMLStreamReader(org.apache.cxf.staxutils.DepthXMLStreamReader) Message(org.apache.cxf.common.i18n.Message) QName(javax.xml.namespace.QName) Constructor(java.lang.reflect.Constructor) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) JAXBElement(javax.xml.bind.JAXBElement) Element(org.w3c.dom.Element) Fault(org.apache.cxf.interceptor.Fault) Method(java.lang.reflect.Method) XMLStreamException(javax.xml.stream.XMLStreamException) JAXBException(javax.xml.bind.JAXBException) PrivilegedActionException(java.security.PrivilegedActionException) Field(java.lang.reflect.Field) GenericArrayType(java.lang.reflect.GenericArrayType) 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) XMLStreamException(javax.xml.stream.XMLStreamException) XmlAccessType(javax.xml.bind.annotation.XmlAccessType)

Example 4 with XmlAccessType

use of javax.xml.bind.annotation.XmlAccessType in project cxf by apache.

the class JAXBEncoderDecoder method marshallException.

public static void marshallException(Marshaller marshaller, Exception elValue, MessagePartInfo part, Object source) {
    XMLStreamWriter writer = getStreamWriter(source);
    QName qn = part.getElementQName();
    try {
        writer.writeStartElement("ns1", qn.getLocalPart(), qn.getNamespaceURI());
        Class<?> cls = part.getTypeClass();
        XmlAccessType accessType = Utils.getXmlAccessType(cls);
        String namespace = part.getElementQName().getNamespaceURI();
        String attNs = namespace;
        SchemaInfo sch = part.getMessageInfo().getOperation().getInterface().getService().getSchema(namespace);
        if (sch == null) {
            LOG.warning("Schema associated with " + namespace + " is null");
            namespace = null;
            attNs = null;
        } else {
            if (!sch.isElementFormQualified()) {
                namespace = null;
            }
            if (!sch.isAttributeFormQualified()) {
                attNs = null;
            }
        }
        List<Member> combinedMembers = new ArrayList<>();
        for (Field f : Utils.getFields(cls, accessType)) {
            XmlAttribute at = f.getAnnotation(XmlAttribute.class);
            if (at == null) {
                combinedMembers.add(f);
            } else {
                QName fname = new QName(attNs, StringUtils.isEmpty(at.name()) ? f.getName() : at.name());
                ReflectionUtil.setAccessible(f);
                Object o = Utils.getFieldValue(f, elValue);
                DocumentFragment frag = DOMUtils.getEmptyDocument().createDocumentFragment();
                writeObject(marshaller, frag, newJAXBElement(fname, String.class, o));
                if (attNs != null) {
                    writer.writeAttribute(attNs, fname.getLocalPart(), DOMUtils.getAllContent(frag));
                } else {
                    writer.writeAttribute(fname.getLocalPart(), DOMUtils.getAllContent(frag));
                }
            }
        }
        for (Method m : Utils.getGetters(cls, accessType)) {
            if (!m.isAnnotationPresent(XmlAttribute.class)) {
                combinedMembers.add(m);
            } else {
                int idx = m.getName().startsWith("get") ? 3 : 2;
                String name = m.getName().substring(idx);
                name = Character.toLowerCase(name.charAt(0)) + name.substring(1);
                XmlAttribute at = m.getAnnotation(XmlAttribute.class);
                QName mname = new QName(namespace, StringUtils.isEmpty(at.name()) ? name : at.name());
                DocumentFragment frag = DOMUtils.getEmptyDocument().createDocumentFragment();
                Object o = Utils.getMethodValue(m, elValue);
                writeObject(marshaller, frag, newJAXBElement(mname, String.class, o));
                if (attNs != null) {
                    writer.writeAttribute(attNs, mname.getLocalPart(), DOMUtils.getAllContent(frag));
                } else {
                    writer.writeAttribute(mname.getLocalPart(), DOMUtils.getAllContent(frag));
                }
            }
        }
        XmlAccessorOrder xmlAccessorOrder = cls.getAnnotation(XmlAccessorOrder.class);
        if (xmlAccessorOrder != null && xmlAccessorOrder.value().equals(XmlAccessOrder.ALPHABETICAL)) {
            Collections.sort(combinedMembers, new Comparator<Member>() {

                public int compare(Member m1, Member m2) {
                    return m1.getName().compareTo(m2.getName());
                }
            });
        }
        XmlType xmlType = cls.getAnnotation(XmlType.class);
        if (xmlType != null && xmlType.propOrder().length > 1 && !xmlType.propOrder()[0].isEmpty()) {
            final List<String> orderList = Arrays.asList(xmlType.propOrder());
            Collections.sort(combinedMembers, new Comparator<Member>() {

                public int compare(Member m1, Member m2) {
                    String m1Name = getName(m1);
                    String m2Name = getName(m2);
                    int m1Index = orderList.indexOf(m1Name);
                    int m2Index = orderList.indexOf(m2Name);
                    if (m1Index != -1 && m2Index != -1) {
                        return m1Index - m2Index;
                    }
                    if (m1Index == -1 && m2Index != -1) {
                        return 1;
                    }
                    if (m1Index != -1 && m2Index == -1) {
                        return -1;
                    }
                    return 0;
                }
            });
        }
        for (Member member : combinedMembers) {
            if (member instanceof Field) {
                Field f = (Field) member;
                QName fname = new QName(namespace, f.getName());
                ReflectionUtil.setAccessible(f);
                if (JAXBSchemaInitializer.isArray(f.getGenericType())) {
                    writeArrayObject(marshaller, writer, fname, f.get(elValue));
                } else {
                    Object o = Utils.getFieldValue(f, elValue);
                    writeObject(marshaller, writer, newJAXBElement(fname, String.class, o));
                }
            } else {
                // it's a Method
                Method m = (Method) member;
                int idx = m.getName().startsWith("get") ? 3 : 2;
                String name = m.getName().substring(idx);
                name = Character.toLowerCase(name.charAt(0)) + name.substring(1);
                QName mname = new QName(namespace, name);
                if (JAXBSchemaInitializer.isArray(m.getGenericReturnType())) {
                    writeArrayObject(marshaller, writer, mname, m.invoke(elValue));
                } else {
                    Object o = Utils.getMethodValue(m, elValue);
                    writeObject(marshaller, writer, newJAXBElement(mname, String.class, o));
                }
            }
        }
        writer.writeEndElement();
        writer.flush();
    } catch (Exception e) {
        throw new Fault(new Message("MARSHAL_ERROR", LOG, e.getMessage()), e);
    } finally {
        StaxUtils.close(writer);
    }
}
Also used : XmlAttribute(javax.xml.bind.annotation.XmlAttribute) XmlAccessorOrder(javax.xml.bind.annotation.XmlAccessorOrder) Message(org.apache.cxf.common.i18n.Message) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) Fault(org.apache.cxf.interceptor.Fault) Method(java.lang.reflect.Method) XMLStreamException(javax.xml.stream.XMLStreamException) JAXBException(javax.xml.bind.JAXBException) PrivilegedActionException(java.security.PrivilegedActionException) XmlType(javax.xml.bind.annotation.XmlType) Field(java.lang.reflect.Field) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) XmlAccessType(javax.xml.bind.annotation.XmlAccessType) Member(java.lang.reflect.Member) DocumentFragment(org.w3c.dom.DocumentFragment) SchemaInfo(org.apache.cxf.service.model.SchemaInfo)

Example 5 with XmlAccessType

use of javax.xml.bind.annotation.XmlAccessType 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

Field (java.lang.reflect.Field)11 Method (java.lang.reflect.Method)11 XmlAccessType (javax.xml.bind.annotation.XmlAccessType)11 XmlType (javax.xml.bind.annotation.XmlType)9 QName (javax.xml.namespace.QName)8 GenericArrayType (java.lang.reflect.GenericArrayType)6 ParameterizedType (java.lang.reflect.ParameterizedType)6 Type (java.lang.reflect.Type)6 XmlSchemaElement (org.apache.ws.commons.schema.XmlSchemaElement)6 PrivilegedActionException (java.security.PrivilegedActionException)4 JAXBException (javax.xml.bind.JAXBException)4 XmlAccessorOrder (javax.xml.bind.annotation.XmlAccessorOrder)4 XMLStreamException (javax.xml.stream.XMLStreamException)4 Message (org.apache.cxf.common.i18n.Message)4 JAXBBeanInfo (org.apache.cxf.common.jaxb.JAXBBeanInfo)4 Fault (org.apache.cxf.interceptor.Fault)4 SchemaInfo (org.apache.cxf.service.model.SchemaInfo)4 XmlSchemaSequence (org.apache.ws.commons.schema.XmlSchemaSequence)4 XmlSchemaSimpleType (org.apache.ws.commons.schema.XmlSchemaSimpleType)4 Constructor (java.lang.reflect.Constructor)2