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;
}
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);
}
}
}
}
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);
}
Aggregations