Search in sources :

Example 1 with XMLName

use of org.apache.cxf.jaxrs.ext.xml.XMLName in project tomee by apache.

the class ResourceUtils method checkJaxbType.

private static void checkJaxbType(Class<?> serviceClass, Class<?> type, Type genericType, ResourceTypes types, Annotation[] anns, MessageBodyWriter<?> jaxbWriter, Class<?> jaxbElement) {
    boolean isCollection = false;
    if (InjectionUtils.isSupportedCollectionOrArray(type)) {
        type = InjectionUtils.getActualType(genericType);
        isCollection = true;
    }
    if (type == Object.class && !(genericType instanceof Class) || genericType instanceof TypeVariable) {
        Type theType = InjectionUtils.processGenericTypeIfNeeded(serviceClass, Object.class, genericType);
        type = InjectionUtils.getActualType(theType);
    }
    if (type == null || InjectionUtils.isPrimitive(type) || (jaxbElement != null && jaxbElement.isAssignableFrom(type)) || Response.class.isAssignableFrom(type) || type.isInterface()) {
        return;
    }
    MessageBodyWriter<?> writer = jaxbWriter;
    if (writer == null) {
        JAXBElementProvider<Object> defaultWriter = new JAXBElementProvider<>();
        defaultWriter.setMarshallAsJaxbElement(true);
        defaultWriter.setXmlTypeAsJaxbElementOnly(true);
        writer = defaultWriter;
    }
    if (writer.isWriteable(type, type, anns, MediaType.APPLICATION_XML_TYPE)) {
        types.getAllTypes().put(type, type);
        Class<?> genCls = InjectionUtils.getActualType(genericType);
        if (genCls != type && genCls != null && genCls != Object.class && !InjectionUtils.isSupportedCollectionOrArray(genCls)) {
            types.getAllTypes().put(genCls, genCls);
        }
        XMLName name = AnnotationUtils.getAnnotation(anns, XMLName.class);
        QName qname = name != null ? JAXRSUtils.convertStringToQName(name.value()) : null;
        if (isCollection) {
            types.getCollectionMap().put(type, qname);
        } else {
            types.getXmlNameMap().put(type, qname);
        }
    }
}
Also used : JAXBElementProvider(org.apache.cxf.jaxrs.provider.JAXBElementProvider) ParameterType(org.apache.cxf.jaxrs.model.ParameterType) MediaType(javax.ws.rs.core.MediaType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable) QName(javax.xml.namespace.QName) ElementClass(org.apache.cxf.jaxrs.ext.xml.ElementClass) XMLName(org.apache.cxf.jaxrs.ext.xml.XMLName)

Example 2 with XMLName

use of org.apache.cxf.jaxrs.ext.xml.XMLName in project cxf by apache.

the class WadlGenerator method generateQName.

private void generateQName(StringBuilder sb, ElementQNameResolver qnameResolver, Map<Class<?>, QName> clsMap, Class<?> type, boolean isCollection, Annotation[] annotations) {
    if (!isCollection) {
        QName typeQName = clsMap.get(type);
        if (typeQName != null) {
            writeQName(sb, typeQName);
            return;
        }
    }
    QName qname = qnameResolver.resolve(type, annotations, Collections.unmodifiableMap(clsMap));
    if (qname != null) {
        if (!isCollection) {
            writeQName(sb, qname);
            clsMap.put(type, qname);
        } else {
            XMLName name = AnnotationUtils.getAnnotation(annotations, XMLName.class);
            String localPart;
            if (name != null) {
                localPart = JAXRSUtils.convertStringToQName(name.value()).getLocalPart();
            } else {
                localPart = qname.getLocalPart() + "s";
            }
            QName collectionName = new QName(qname.getNamespaceURI(), localPart, qname.getPrefix());
            writeQName(sb, collectionName);
        }
    }
}
Also used : QName(javax.xml.namespace.QName) XMLName(org.apache.cxf.jaxrs.ext.xml.XMLName)

Example 3 with XMLName

use of org.apache.cxf.jaxrs.ext.xml.XMLName in project cxf by apache.

the class ResourceUtils method checkJaxbType.

private static void checkJaxbType(Class<?> serviceClass, Class<?> type, Type genericType, ResourceTypes types, Annotation[] anns, MessageBodyWriter<?> jaxbWriter, Class<?> jaxbElement) {
    boolean isCollection = false;
    if (InjectionUtils.isSupportedCollectionOrArray(type)) {
        type = InjectionUtils.getActualType(genericType);
        isCollection = true;
    }
    if (type == Object.class && !(genericType instanceof Class) || genericType instanceof TypeVariable) {
        Type theType = InjectionUtils.processGenericTypeIfNeeded(serviceClass, Object.class, genericType);
        type = InjectionUtils.getActualType(theType);
    }
    if (type == null || InjectionUtils.isPrimitive(type) || (jaxbElement != null && jaxbElement.isAssignableFrom(type)) || Response.class.isAssignableFrom(type) || type.isInterface()) {
        return;
    }
    MessageBodyWriter<?> writer = jaxbWriter;
    if (writer == null) {
        JAXBElementProvider<Object> defaultWriter = new JAXBElementProvider<>();
        defaultWriter.setMarshallAsJaxbElement(true);
        defaultWriter.setXmlTypeAsJaxbElementOnly(true);
        writer = defaultWriter;
    }
    if (writer.isWriteable(type, type, anns, MediaType.APPLICATION_XML_TYPE)) {
        types.getAllTypes().put(type, type);
        Class<?> genCls = InjectionUtils.getActualType(genericType);
        if (genCls != type && genCls != null && genCls != Object.class && !InjectionUtils.isSupportedCollectionOrArray(genCls)) {
            types.getAllTypes().put(genCls, genCls);
        }
        XMLName name = AnnotationUtils.getAnnotation(anns, XMLName.class);
        QName qname = name != null ? JAXRSUtils.convertStringToQName(name.value()) : null;
        if (isCollection) {
            types.getCollectionMap().put(type, qname);
        } else {
            types.getXmlNameMap().put(type, qname);
        }
    }
}
Also used : JAXBElementProvider(org.apache.cxf.jaxrs.provider.JAXBElementProvider) ParameterType(org.apache.cxf.jaxrs.model.ParameterType) MediaType(javax.ws.rs.core.MediaType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable) QName(javax.xml.namespace.QName) ElementClass(org.apache.cxf.jaxrs.ext.xml.ElementClass) XMLName(org.apache.cxf.jaxrs.ext.xml.XMLName)

Aggregations

QName (javax.xml.namespace.QName)3 XMLName (org.apache.cxf.jaxrs.ext.xml.XMLName)3 Type (java.lang.reflect.Type)2 TypeVariable (java.lang.reflect.TypeVariable)2 MediaType (javax.ws.rs.core.MediaType)2 ElementClass (org.apache.cxf.jaxrs.ext.xml.ElementClass)2 ParameterType (org.apache.cxf.jaxrs.model.ParameterType)2 JAXBElementProvider (org.apache.cxf.jaxrs.provider.JAXBElementProvider)2