use of javax.xml.bind.annotation.XmlElement in project midpoint by Evolveum.
the class PrismBeanInspector method findFieldElementQNameUncached.
private QName findFieldElementQNameUncached(String fieldName, Class beanClass, String defaultNamespace) {
Field field;
try {
field = beanClass.getDeclaredField(fieldName);
} catch (NoSuchFieldException e) {
// TODO implement this if needed (lookup the getter method instead of the field)
return new QName(defaultNamespace, fieldName);
}
String realLocalName = fieldName;
String realNamespace = defaultNamespace;
XmlElement xmlElement = field.getAnnotation(XmlElement.class);
if (xmlElement != null) {
String name = xmlElement.name();
if (name != null && !BeanMarshaller.DEFAULT_PLACEHOLDER.equals(name)) {
realLocalName = name;
}
String namespace = xmlElement.namespace();
if (namespace != null && !BeanMarshaller.DEFAULT_PLACEHOLDER.equals(namespace)) {
realNamespace = namespace;
}
}
return new QName(realNamespace, realLocalName);
}
use of javax.xml.bind.annotation.XmlElement in project midpoint by Evolveum.
the class PrismBeanInspector method findPropertyFieldExactUncached.
private <T> Field findPropertyFieldExactUncached(Class<T> classType, String propName) {
for (Field field : classType.getDeclaredFields()) {
XmlElement xmlElement = field.getAnnotation(XmlElement.class);
if (xmlElement != null && xmlElement.name() != null && xmlElement.name().equals(propName)) {
return field;
}
XmlAttribute xmlAttribute = field.getAnnotation(XmlAttribute.class);
if (xmlAttribute != null && xmlAttribute.name() != null && xmlAttribute.name().equals(propName)) {
return field;
}
}
try {
return classType.getDeclaredField(propName);
} catch (NoSuchFieldException e) {
// nothing found
}
Class<? super T> superclass = classType.getSuperclass();
if (superclass == null || Object.class.equals(superclass)) {
return null;
}
return findPropertyField(superclass, propName);
}
use of javax.xml.bind.annotation.XmlElement in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method kvpForCreateDLResp_bug74371.
/*
# zmsoap -z -t account -m user1 GetDistributionListRequest/dl=grendl@coco.local @by=name
<GetDistributionListResponse xmlns="urn:zimbraAccount">
<dl id="7a3e8ec5-4892-4b17-9225-cf17e8b3acc9" dynamic="1" name="grendl@coco.local" isOwner="1" isMember="1">
<a n="mail">grendl@coco.local</a>
<a n="zimbraMailStatus">enabled</a>
<a n="zimbraMailAlias">grendl@coco.local</a>
<a n="description">Wonder at that</a>
<a n="displayName">Gren DLfun</a>
<a n="zimbraDistributionListSubscriptionPolicy">ACCEPT</a>
<a n="zimbraDistributionListUnsubscriptionPolicy">ACCEPT</a>
</dl>
</GetDistributionListResponse>
# zmsoap --json -z -t account -m user1 GetDistributionListRequest/dl=grendl@coco.local @by=name
{
"dl": [{
"name": "grendl@coco.local",
"id": "7a3e8ec5-4892-4b17-9225-cf17e8b3acc9",
"dynamic": true,
"isMember": true,
"isOwner": true,
"_attrs": {
"mail": "grendl@coco.local",
"zimbraMailStatus": "enabled",
"zimbraMailAlias": "grendl@coco.local",
"description": "Wonder at that",
"displayName": "Gren DLfun",
"zimbraDistributionListSubscriptionPolicy": "ACCEPT",
"zimbraDistributionListUnsubscriptionPolicy": "ACCEPT"
}
}],
"_jsns": "urn:zimbraAccount"
}
Extract from mailbox.log for creation of this DL by ZWC - demonstrating the different handling of attrs - See Bug 74371
"CreateDistributionListResponse": [{
"dl": [{
"name": "grendl@coco.local",
"id": "7a3e8ec5-4892-4b17-9225-cf17e8b3acc9",
"dynamic": true,
"a": [
{
"n": "memberURL",
"_content": "ldap:///??sub?(|(zimbraMemberOf=7a3e8ec5-4892-4b17-9225-cf17e8b3acc9)(zimbraId=de47828e-94dd-45c3-9770-4dbd255564ca))"
},
{
"n": "mail",
"_content": "grendl@coco.local"
},
...
*/
/**
* Desired JSON
*/
// Re-enable when Bug 74371 is fixed? @Test
public void kvpForCreateDLResp_bug74371() throws Exception {
Element jsonElem = JSONElement.mFactory.createElement(QName.get(AccountConstants.E_CREATE_DISTRIBUTION_LIST_RESPONSE, AccountConstants.NAMESPACE_STR));
populateCreateDlResp(jsonElem);
Element xmlElem = XMLElement.mFactory.createElement(QName.get(AccountConstants.E_CREATE_DISTRIBUTION_LIST_RESPONSE, AccountConstants.NAMESPACE_STR));
populateCreateDlResp(xmlElem);
logDebug("XmlElement (for comparison) ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
logDebug("JSONElement (for comparison) ---> prettyPrint\n%1$s", jsonElem.prettyPrint());
List<KeyValuePair> attrs = Lists.newArrayList();
attrs.add(new KeyValuePair("key1", "value1"));
attrs.add(new KeyValuePair("key2", "value2"));
DLInfo dl = new DLInfo("myId", "myRef", "my name", null, null, null, null, null);
CreateDistributionListResponse jaxb = new CreateDistributionListResponse(dl);
Element xmlJaxbElem = JaxbUtil.jaxbToElement(jaxb, XMLElement.mFactory);
Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb);
DLInfo roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, DLInfo.class);
logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
logDebug("XMLElement from JAXB ---> prettyPrint\n%1$s", xmlJaxbElem.prettyPrint());
Element eDL = jsonJaxbElem.getElement(AdminConstants.E_DL);
List<? extends KeyValuePair> kvps = roundtripped.getAttrList();
Assert.assertEquals("roundtripped kvps num", 2, kvps.size());
List<com.zimbra.common.soap.Element.KeyValuePair> elemKVPs = eDL.getElement("a").listKeyValuePairs();
Assert.assertEquals("elemKVP num", 2, elemKVPs.size());
Assert.assertEquals("prettyPrint", jsonElem.prettyPrint(), jsonJaxbElem.prettyPrint());
}
use of javax.xml.bind.annotation.XmlElement in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method kvpForGetDLResp.
/**
* Desired JSON
* {
* "dl": [{
* "name": "my name",
* "id": "myId",
* "dynamic": true,
* "_attrs": {
* "mail": "fun@example.test",
* "zimbraMailStatus": "enabled"
* }
* }],
* "_jsns": "urn:zimbraAccount"
* }
*/
@Test
public void kvpForGetDLResp() throws Exception {
Element jsonElem = JSONElement.mFactory.createElement(QName.get(AccountConstants.E_GET_DISTRIBUTION_LIST_RESPONSE, AccountConstants.NAMESPACE_STR));
populateGetDlResp(jsonElem);
Element xmlElem = XMLElement.mFactory.createElement(QName.get(AccountConstants.E_GET_DISTRIBUTION_LIST_RESPONSE, AccountConstants.NAMESPACE_STR));
populateGetDlResp(xmlElem);
logDebug("XmlElement (for comparison) ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
logDebug("JSONElement (for comparison) ---> prettyPrint\n%1$s", jsonElem.prettyPrint());
// ObjectInfo declares this field:
// @ZimbraKeyValuePairs
// @XmlElement(name=AccountConstants.E_A /* a */, required=false)
// private final List<KeyValuePair> attrList;
List<KeyValuePair> attrs = Lists.newArrayList();
attrs.add(new KeyValuePair("mail", "fun@example.test"));
attrs.add(new KeyValuePair("zimbraMailStatus", "enabled"));
DistributionListInfo dl = new DistributionListInfo("myId", "my name", null, attrs);
dl.setDynamic(true);
GetDistributionListResponse jaxb = new GetDistributionListResponse(dl);
Element xmlJaxbElem = JaxbUtil.jaxbToElement(jaxb, XMLElement.mFactory);
Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb);
GetDistributionListResponse roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, GetDistributionListResponse.class);
GetDistributionListResponse roundtrippedX = JaxbUtil.elementToJaxb(xmlJaxbElem, GetDistributionListResponse.class);
logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
logDebug("XMLElement from JAXB ---> prettyPrint\n%1$s", xmlJaxbElem.prettyPrint());
List<? extends KeyValuePair> kvps = roundtripped.getDl().getAttrList();
Assert.assertEquals("roundtripped kvps num", 2, kvps.size());
Assert.assertEquals("prettyPrint", jsonElem.prettyPrint(), jsonJaxbElem.prettyPrint());
// ensure that the JAXB handles empty owners OK (not using empty list in JAXB field initializer)
Assert.assertNull("roundtripped owner", roundtripped.getDl().getOwners());
Assert.assertNull("roundtrippedX owner", roundtrippedX.getDl().getOwners());
}
use of javax.xml.bind.annotation.XmlElement in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method zimbraKeyValuePairsAnnotation.
/**
* Check that JSON can be deserialised into a JAXB object when some of the JSON represents Zimbra KeyValuePairs
* (i.e. An "_attrs" array).
* In this case, the target objects for each keyvalue pair use the defaults of "a" for the element name and
* "n" for the attribute name in the XML form.
* Desired XML :<br />
* <key-value-pairs-tester xmlns="urn:zimbraTest">
* <a n="key1">value1</a>
* <a n="key2">value2-a</a>
* <a n="key2">value2-b</a>
* </key-value-pairs-tester>
*<br />
* Desired JSON :<br />
* {
* "_attrs": {
* "key1": "value1",
* "key2": [
* "value2-a",
* "value2-b"]
* },
* "_jsns": "urn:zimbraTest"
* }
*/
@Test
public void zimbraKeyValuePairsAnnotation() throws Exception {
Element jsonElem = JSONElement.mFactory.createElement(QName.get("key-value-pairs", "urn:zimbraTest"));
jsonElem.addKeyValuePair("key1", "value1");
jsonElem.addKeyValuePair("key2", "value2-a");
jsonElem.addKeyValuePair("key2", "value2-b");
List<KeyValuePair> attrs = Lists.newArrayList();
attrs.add(new KeyValuePair("key1", "value1"));
attrs.add(new KeyValuePair("key2", "value2-a"));
attrs.add(new KeyValuePair("key2", "value2-b"));
KeyValuePairsTester jaxb = new KeyValuePairsTester(attrs);
logDebug("XMLElement (from JAXB) ---> prettyPrint\n%1$s", JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory, true, false).prettyPrint());
Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb);
logDebug("JSONElement (for comparison) ---> prettyPrint\n%1$s", jsonElem.prettyPrint());
logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
KeyValuePairsTester roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, KeyValuePairsTester.class);
List<com.zimbra.common.soap.Element.KeyValuePair> elemKVPs = jsonJaxbElem.listKeyValuePairs();
Assert.assertEquals("elemKVP num", 3, elemKVPs.size());
Assert.assertEquals("prettyPrint", jsonElem.prettyPrint(), jsonJaxbElem.prettyPrint());
List<KeyValuePair> kvps = roundtripped.getAttrList();
Assert.assertEquals("roundtripped kvps num", 3, kvps.size());
}
Aggregations