Search in sources :

Example 1 with XmlAdapter

use of jakarta.xml.bind.annotation.adapters.XmlAdapter in project eclipselink by eclipse-ee4j.

the class PersistenceContext method unmarshal.

/**
 * Unmarshal.
 *
 * @param type the type of the entity to unmarshal
 * @param acceptedMediaType the accepted media type
 * @param in the input stream to unmarshal
 * @return the object
 * @throws JAXBException the JAXB exception
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public Object unmarshal(Class type, MediaType acceptedMediaType, InputStream in) throws JAXBException {
    Unmarshaller unmarshaller = getJAXBContext().createUnmarshaller();
    unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, Boolean.FALSE);
    unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, acceptedMediaType.toString());
    unmarshaller.setAdapter(new LinkAdapter(getBaseURI().toString(), this));
    unmarshaller.setEventHandler(new ValidationEventHandler() {

        @Override
        public /*
             * ReferenceAdaptor unmarshal throws exception if the object referred by a link
             * doesn't exist, and this handler is required to interrupt the unmarshal
             * operation under this condition.
             * (non-Javadoc) @see jakarta.xml.bind.ValidationEventHandler#handleEvent(jakarta.xml.bind.ValidationEvent)
             *
             */
        boolean handleEvent(ValidationEvent event) {
            if (event.getSeverity() != ValidationEvent.WARNING) {
                // and linked exception, just return false;
                return false;
            }
            return true;
        }
    });
    for (XmlAdapter adapter : getAdapters()) {
        unmarshaller.setAdapter(adapter);
    }
    if (acceptedMediaType == MediaType.APPLICATION_JSON_TYPE) {
        // Part of the fix for https://bugs.eclipse.org/bugs/show_bug.cgi?id=394059
        // This issue happens when request has objects derived from an abstract class.
        // JSON_INCLUDE_ROOT is set to false for  JPA-RS. This means JSON requests won't have root tag.
        // The unmarshal method needs to be called with type, so that moxy can unmarshal the message based on type.
        // For xml, root tag is always set, unmarshaller must use root of the message for unmarshalling and type should
        // not be passed to unmarshal for xml type requests.
        JAXBElement<?> element = unmarshaller.unmarshal(new StreamSource(in), type);
        if (element.getValue() instanceof List<?>) {
            for (Object object : (List<?>) element.getValue()) {
                wrap(object);
            }
            return element.getValue();
        } else {
            wrap(element.getValue());
        }
        return element.getValue();
    }
    Object domainObject = unmarshaller.unmarshal(new StreamSource(in));
    if (domainObject instanceof List<?>) {
        for (Object object : (List<?>) domainObject) {
            wrap(object);
        }
        return domainObject;
    } else {
        wrap(domainObject);
    }
    return domainObject;
}
Also used : ValidationEventHandler(jakarta.xml.bind.ValidationEventHandler) ValidationEvent(jakarta.xml.bind.ValidationEvent) StreamSource(javax.xml.transform.stream.StreamSource) LinkAdapter(org.eclipse.persistence.jpa.rs.util.xmladapters.LinkAdapter) RelationshipLinkAdapter(org.eclipse.persistence.jpa.rs.util.xmladapters.RelationshipLinkAdapter) ArrayList(java.util.ArrayList) List(java.util.List) SingleResultQueryList(org.eclipse.persistence.jpa.rs.util.list.SingleResultQueryList) ReportQueryResultList(org.eclipse.persistence.jpa.rs.util.list.ReportQueryResultList) Unmarshaller(jakarta.xml.bind.Unmarshaller) XmlAdapter(jakarta.xml.bind.annotation.adapters.XmlAdapter)

Example 2 with XmlAdapter

use of jakarta.xml.bind.annotation.adapters.XmlAdapter in project eclipselink by eclipse-ee4j.

the class PreLoginMappingAdapter method convertMappingToXMLChoiceMapping.

/**
 * Build an XMLChoiceObjectMapping based on a particular mapping and replace that mapping with
 * the newly created XMLChoiceObjectMapping in jaxbDescriptor.
 * @param jaxbDescriptor the jaxb descriptor
 * @param jpaMapping the jpa mapping
 * @param cl the classloader
 */
@SuppressWarnings("rawtypes")
private static void convertMappingToXMLChoiceMapping(ClassDescriptor jaxbDescriptor, DatabaseMapping jpaMapping, ClassLoader cl, Session jpaSession) {
    if ((jpaMapping != null) && (jaxbDescriptor != null)) {
        if ((jpaMapping instanceof ForeignReferenceMapping) && ((jpaMapping.isAggregateCollectionMapping()) || (jpaMapping.isAggregateMapping()))) {
            // Aggregates don't have identity to create links, thus no weaved REST adapters to insert choice mappings
            return;
        }
        String attributeName = jpaMapping.getAttributeName();
        DatabaseMapping jaxbMapping = jaxbDescriptor.getMappingForAttributeName(jpaMapping.getAttributeName());
        if (!(jaxbMapping.isXMLMapping() && (jaxbMapping.isAbstractCompositeCollectionMapping() || jaxbMapping.isAbstractCompositeObjectMapping()))) {
            return;
        }
        ClassDescriptor refDesc = null;
        if (jpaMapping instanceof ForeignReferenceMapping) {
            Class clazz = ((ForeignReferenceMapping) jpaMapping).getReferenceClass();
            refDesc = jpaSession.getDescriptor(clazz);
        } else if (jpaMapping instanceof XMLCompositeObjectMapping) {
            Class clazz = ((XMLCompositeObjectMapping) jpaMapping).getReferenceClass();
            refDesc = jpaSession.getDescriptor(clazz);
        }
        if (refDesc == null) {
            return;
        }
        String adapterClassName = RestAdapterClassWriter.constructClassNameForReferenceAdapter(refDesc.getJavaClassName());
        if (adapterClassName != null) {
            try {
                if (jaxbMapping.isAbstractCompositeObjectMapping()) {
                    XMLChoiceObjectMapping xmlChoiceMapping = new XMLChoiceObjectMapping();
                    xmlChoiceMapping.setAttributeName(attributeName);
                    copyAccessorToMapping(jaxbMapping, xmlChoiceMapping);
                    xmlChoiceMapping.setProperties(jaxbMapping.getProperties());
                    XMLCompositeObjectMapping compositeMapping = (XMLCompositeObjectMapping) jaxbMapping;
                    xmlChoiceMapping.addChoiceElement(compositeMapping.getXPath(), Link.class);
                    xmlChoiceMapping.addChoiceElement(compositeMapping.getXPath(), refDesc.getJavaClass());
                    xmlChoiceMapping.setConverter(new XMLJavaTypeConverter((Class<? extends XmlAdapter<?, ?>>) Class.forName(adapterClassName, true, cl)));
                    jaxbDescriptor.removeMappingForAttributeName(jaxbMapping.getAttributeName());
                    jaxbDescriptor.addMapping(xmlChoiceMapping);
                } else if (jaxbMapping.isAbstractCompositeCollectionMapping()) {
                    XMLChoiceCollectionMapping xmlChoiceMapping = new XMLChoiceCollectionMapping();
                    xmlChoiceMapping.setAttributeName(attributeName);
                    copyAccessorToMapping(jaxbMapping, xmlChoiceMapping);
                    xmlChoiceMapping.setProperties(jaxbMapping.getProperties());
                    XMLCompositeCollectionMapping compositeMapping = (XMLCompositeCollectionMapping) jaxbMapping;
                    xmlChoiceMapping.addChoiceElement(compositeMapping.getXPath(), Link.class);
                    xmlChoiceMapping.addChoiceElement(compositeMapping.getXPath(), refDesc.getJavaClass());
                    xmlChoiceMapping.setContainerPolicy(jaxbMapping.getContainerPolicy());
                    xmlChoiceMapping.setConverter(new XMLJavaTypeConverter((Class<? extends XmlAdapter<?, ?>>) Class.forName(adapterClassName, true, cl)));
                    jaxbDescriptor.removeMappingForAttributeName(jaxbMapping.getAttributeName());
                    jaxbDescriptor.addMapping(xmlChoiceMapping);
                }
            } catch (Exception ex) {
                throw JPARSException.exceptionOccurred(ex);
            }
        }
    }
}
Also used : XMLChoiceObjectMapping(org.eclipse.persistence.oxm.mappings.XMLChoiceObjectMapping) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) XMLChoiceCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLChoiceCollectionMapping) JPARSException(org.eclipse.persistence.jpa.rs.exceptions.JPARSException) ForeignReferenceMapping(org.eclipse.persistence.mappings.ForeignReferenceMapping) XMLCompositeCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping) XMLCompositeObjectMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping) XmlAdapter(jakarta.xml.bind.annotation.adapters.XmlAdapter) Link(org.eclipse.persistence.internal.jpa.rs.metadata.model.Link) XMLJavaTypeConverter(org.eclipse.persistence.internal.jaxb.XMLJavaTypeConverter)

Example 3 with XmlAdapter

use of jakarta.xml.bind.annotation.adapters.XmlAdapter in project jaxb-ri by eclipse-ee4j.

the class TypeUseImpl method createConstant.

@Override
public JExpression createConstant(Outline outline, XmlString lexical) {
    if (isCollection())
        return null;
    if (adapter == null)
        return coreType.createConstant(outline, lexical);
    // [RESULT] new Adapter().unmarshal(CONSTANT);
    JExpression cons = coreType.createConstant(outline, lexical);
    Class<? extends XmlAdapter> atype = adapter.getAdapterIfKnown();
    // try to run the adapter now rather than later.
    if (cons instanceof JStringLiteral && atype != null) {
        JStringLiteral scons = (JStringLiteral) cons;
        XmlAdapter a = ClassFactory.create(atype);
        try {
            Object value = a.unmarshal(scons.str);
            if (value instanceof String) {
                return JExpr.lit((String) value);
            }
        } catch (Exception e) {
        // assume that we can't eagerly bind this
        }
    }
    return JExpr._new(adapter.getAdapterClass(outline)).invoke("unmarshal").arg(cons);
}
Also used : JStringLiteral(com.sun.codemodel.JStringLiteral) JExpression(com.sun.codemodel.JExpression) XmlString(com.sun.xml.xsom.XmlString) XmlAdapter(jakarta.xml.bind.annotation.adapters.XmlAdapter)

Aggregations

XmlAdapter (jakarta.xml.bind.annotation.adapters.XmlAdapter)3 JExpression (com.sun.codemodel.JExpression)1 JStringLiteral (com.sun.codemodel.JStringLiteral)1 XmlString (com.sun.xml.xsom.XmlString)1 Unmarshaller (jakarta.xml.bind.Unmarshaller)1 ValidationEvent (jakarta.xml.bind.ValidationEvent)1 ValidationEventHandler (jakarta.xml.bind.ValidationEventHandler)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 StreamSource (javax.xml.transform.stream.StreamSource)1 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)1 XMLJavaTypeConverter (org.eclipse.persistence.internal.jaxb.XMLJavaTypeConverter)1 Link (org.eclipse.persistence.internal.jpa.rs.metadata.model.Link)1 JPARSException (org.eclipse.persistence.jpa.rs.exceptions.JPARSException)1 ReportQueryResultList (org.eclipse.persistence.jpa.rs.util.list.ReportQueryResultList)1 SingleResultQueryList (org.eclipse.persistence.jpa.rs.util.list.SingleResultQueryList)1 LinkAdapter (org.eclipse.persistence.jpa.rs.util.xmladapters.LinkAdapter)1 RelationshipLinkAdapter (org.eclipse.persistence.jpa.rs.util.xmladapters.RelationshipLinkAdapter)1 DatabaseMapping (org.eclipse.persistence.mappings.DatabaseMapping)1 ForeignReferenceMapping (org.eclipse.persistence.mappings.ForeignReferenceMapping)1