use of javax.xml.bind.annotation.XmlElement in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method bug65572_BooleanAndXmlElements.
/**
*
{
"filterRules": [{
"filterRule": [{
"name": "filter.bug65572",
"active": false,
"filterTests": [{
"condition": "anyof",
"headerTest": [{
"index": 0,
"header": "X-Spam-Score",
"caseSensitive": false,
"stringComparison": "contains",
"value": "0"
}]
}],
"filterActions": [{
"actionFlag": [{
"flagName": "flagged",
"index": 0
}],
"actionStop": [{
"index": 1
}]
}]
}]
}],
"_jsns": "urn:zimbraMail"
}
*/
/**
* This also tests {@link XmlElements} - It is used in {@link FilterTests}
* @throws Exception
*/
@Test
public void bug65572_BooleanAndXmlElements() throws Exception {
Element legacyXmlElem = mkFilterRulesResponse(XMLElement.mFactory);
Element legacyJsonElem = mkFilterRulesResponse(JSONElement.mFactory);
GetFilterRulesResponse jaxb = new GetFilterRulesResponse();
FilterTests tests = FilterTests.createForCondition("anyof");
FilterTest.HeaderTest hdrTest = FilterTest.HeaderTest.createForIndexNegative(0, null);
hdrTest.setHeaders("X-Spam-Score");
hdrTest.setCaseSensitive(false);
hdrTest.setStringComparison("contains");
hdrTest.setValue("0");
tests.addTest(hdrTest);
FilterAction.FlagAction flagAction = new FilterAction.FlagAction("flagged");
flagAction.setIndex(0);
FilterAction.StopAction stopAction = new FilterAction.StopAction();
stopAction.setIndex(1);
FilterRule rule1 = FilterRule.createForNameFilterTestsAndActiveSetting("filter.bug65572", tests, false);
rule1.addFilterAction(flagAction);
rule1.addFilterAction(stopAction);
jaxb.addFilterRule(rule1);
Element xmlElem = JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory);
logDebug("legacyXMLElement ---> prettyPrint\n%1$s", legacyXmlElem.prettyPrint());
logDebug("XMLElement from JAXB ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
// Attribute Ordering not reliable: Assert.assertEquals("XML", legacyXmlElem.prettyPrint(), xmlElem.prettyPrint());
Element xmlFr = xmlElem.getElement(MailConstants.E_FILTER_RULES).getElement(MailConstants.E_FILTER_RULE);
Assert.assertEquals("XMLElement from JAXB filter rule name", "filter.bug65572", xmlFr.getAttribute(MailConstants.A_NAME));
Assert.assertEquals("XMLElement from JAXB filter rule active", false, xmlFr.getAttributeBool(MailConstants.A_ACTIVE));
Element xmlFT = xmlFr.getElement(MailConstants.E_FILTER_TESTS);
Assert.assertEquals("XMLElement from JAXB filter tests condition", "anyof", xmlFT.getAttribute(MailConstants.A_CONDITION));
Element xmlHdrT = xmlFT.getElement(MailConstants.E_HEADER_TEST);
Assert.assertEquals("XMLElement from JAXB filter hdr test index", 0, xmlHdrT.getAttributeInt(MailConstants.A_INDEX));
Assert.assertEquals("XMLElement from JAXB filter hdr test hdr", "X-Spam-Score", xmlHdrT.getAttribute(MailConstants.A_HEADER));
Assert.assertEquals("XMLElement from JAXB filter hdr test caseSense", false, xmlHdrT.getAttributeBool(MailConstants.A_CASE_SENSITIVE));
Assert.assertEquals("XMLElement from JAXB filter hdr test comparison", "contains", xmlHdrT.getAttribute(MailConstants.A_STRING_COMPARISON));
Assert.assertEquals("XMLElement from JAXB filter hdr test value", 0, xmlHdrT.getAttributeInt(MailConstants.A_VALUE));
Element xmlFA = xmlFr.getElement(MailConstants.E_FILTER_ACTIONS);
Element xmlFlag = xmlFA.getElement(MailConstants.E_ACTION_FLAG);
Assert.assertEquals("XMLElement from JAXB action flag name", "flagged", xmlFlag.getAttribute(MailConstants.A_FLAG_NAME));
Assert.assertEquals("XMLElement from JAXB action flag index", 0, xmlFlag.getAttributeInt(MailConstants.A_INDEX));
Element xmlStop = xmlFA.getElement(MailConstants.E_ACTION_STOP);
Assert.assertEquals("XMLElement from JAXB action stop index", 1, xmlStop.getAttributeInt(MailConstants.A_INDEX));
Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb, MailConstants.GET_FILTER_RULES_RESPONSE);
logDebug("GetFilterRulesResponse legacyJSONElement ---> prettyPrint\n%1$s", legacyJsonElem.prettyPrint());
logDebug("GetFilterRulesResponse JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
Assert.assertEquals("JSON", legacyJsonElem.prettyPrint(), jsonJaxbElem.prettyPrint());
GetFilterRulesResponse roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, GetFilterRulesResponse.class);
List<FilterRule> rules = roundtripped.getFilterRules();
Assert.assertEquals("num roundtripped rules", 1, rules.size());
FilterRule rtRule = rules.get(0);
Assert.assertEquals("roundtripped rule name", "filter.bug65572", rtRule.getName());
Assert.assertEquals("roundtripped rule active setting", false, rtRule.isActive());
Assert.assertEquals("roundtripped rule action count", 2, rtRule.getActionCount());
FilterTests rtTests = rtRule.getFilterTests();
Assert.assertEquals("roundtripped filterTests condition", "anyof", rtTests.getCondition());
List<FilterTest> rtFilterTests = rtTests.getTests();
Assert.assertEquals("num roundtripped filter tests", 1, rtFilterTests.size());
FilterTest.HeaderTest rtHdrTest = (FilterTest.HeaderTest) rtFilterTests.get(0);
Assert.assertEquals("roundtripped header test index", 0, rtHdrTest.getIndex());
Assert.assertEquals("roundtripped header test header", "X-Spam-Score", rtHdrTest.getHeaders());
Assert.assertEquals("roundtripped header test caseSens", false, rtHdrTest.isCaseSensitive());
Assert.assertEquals("roundtripped header test stringComparison", "contains", rtHdrTest.getStringComparison());
Assert.assertEquals("roundtripped header test value", "0", rtHdrTest.getValue());
List<FilterAction> rtActions = rtRule.getFilterActions();
Assert.assertEquals("num roundtripped actions", 2, rtActions.size());
FilterAction.FlagAction rtFlagAction = (FilterAction.FlagAction) rtActions.get(0);
Assert.assertEquals("roundtripped FlagAction name", "flagged", rtFlagAction.getFlag());
Assert.assertEquals("roundtripped FlagAction index", 0, rtFlagAction.getIndex());
FilterAction.StopAction rtStopAction = (FilterAction.StopAction) rtActions.get(1);
Assert.assertEquals("roundtripped StopAction index", 1, rtStopAction.getIndex());
}
use of javax.xml.bind.annotation.XmlElement in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method anyElementHandling.
/**
* {@link XmlAnyElement} handling
* <pre>
* @XmlAnyElement
* private List<org.w3c.dom.Element> elems = Lists.newArrayList();
* </pre>
*/
@Test
public void anyElementHandling() throws Exception {
String given = "Given information";
List<Object> elems = Lists.newArrayList();
AnyTester jaxb = new AnyTester();
DocumentBuilder builder = javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder();
org.w3c.dom.Document doc = builder.newDocument();
org.w3c.dom.Element elem = doc.createElementNS("urn:foreign", "alien");
elem.setAttribute("myAttr", "myValue");
org.w3c.dom.Element child = doc.createElementNS("urn:foreign", "child");
child.setTextContent("Purple beans");
elem.appendChild(child);
org.w3c.dom.Element child2 = doc.createElementNS("urn:foreign", "daughter");
child2.setAttribute("name", "Kate");
child2.setAttribute("age", "23");
elem.appendChild(child2);
elems.add(elem);
org.w3c.dom.Element elem2 = doc.createElementNS("urn:wooky", "may");
elem2.setAttribute("fourth", "be with you");
jaxb.setGiven(given);
jaxb.setElems(Lists.newArrayList(elem, elem2));
Element xmlElem = JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory, true, false);
xmlElem = JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory, true, false);
logDebug("XmlElement (for comparison) ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
AnyTester roundtrippedX = JaxbUtil.elementToJaxb(xmlElem, AnyTester.class);
Assert.assertEquals("roundtrippedX given", given, roundtrippedX.getGiven());
Assert.assertEquals("roundtrippedX num elems", 2, roundtrippedX.getElems().size());
org.w3c.dom.Element w3ce = roundtrippedX.getElems().get(0);
Assert.assertEquals("roundtrippedX elem name", "alien", w3ce.getLocalName());
logDebug("STRING from JAXB ---> prettyPrint\n%1$s", getZimbraJsonJaxbString(jaxb));
Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb);
logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
AnyTester roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, AnyTester.class);
Assert.assertEquals("roundtripped given", given, roundtripped.getGiven());
Assert.assertEquals("roundtripped num elems", 2, roundtripped.getElems().size());
org.w3c.dom.Element rtElem = roundtripped.getElems().get(0);
Assert.assertEquals("roundtripped elem name", "alien", rtElem.getTagName());
Assert.assertEquals("roundtripped elem namespace", "urn:foreign", rtElem.getNamespaceURI());
}
use of javax.xml.bind.annotation.XmlElement in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method anyAttributeHandling.
/**
* {@link XmlAnyAttribute} handling - the field with this annotation needs to be a {@link Map}
* <pre>
* @XmlAnyAttribute
* private Map<javax.xml.namespace.QName,Object> extraAttributes = Maps.newHashMap();
* </pre>
* Desired JSON:
* {
* "given": "Given information",
* "attr2": "222",
* "attr1": "First attr",
* "_jsns": "urn:zimbraTest"
* }
*/
@Test
public void anyAttributeHandling() throws Exception {
String given = "Given information";
String first = "First attr";
int second = 222;
Map<javax.xml.namespace.QName, Object> extras = Maps.newHashMap();
extras.put(new javax.xml.namespace.QName("attr1"), first);
// Would expect this to work with integer but the XML JAXB fails saying can't cast Integer to String
extras.put(new javax.xml.namespace.QName("attr2"), new Integer(second).toString());
AnyAttrTester jaxb = new AnyAttrTester();
jaxb.setGiven(given);
jaxb.setExtraAttributes(extras);
Element xmlElem = JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory, true, false);
xmlElem = JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory, true, false);
logDebug("XmlElement (for comparison) ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
AnyAttrTester roundtrippedX = JaxbUtil.elementToJaxb(xmlElem, AnyAttrTester.class);
Assert.assertEquals("roundtrippedX given", given, roundtrippedX.getGiven());
Map<javax.xml.namespace.QName, Object> rtXextras = roundtrippedX.getExtraAttributes();
Assert.assertEquals("roundtrippedX num extras", 2, rtXextras.size());
Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb);
logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
AnyAttrTester roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, AnyAttrTester.class);
Assert.assertEquals("roundtripped given", given, roundtripped.getGiven());
Map<javax.xml.namespace.QName, Object> rtextras = roundtripped.getExtraAttributes();
Assert.assertEquals("roundtripped num extras", 2, rtextras.size());
for (Entry<javax.xml.namespace.QName, Object> attrib : rtextras.entrySet()) {
if ("attr1".equals(attrib.getKey().getLocalPart())) {
Assert.assertTrue("attr1 attribute has correct value", first.equals(attrib.getValue()));
} else if ("attr2".equals(attrib.getKey().getLocalPart())) {
Assert.assertTrue("attr2 attribute has correct value", "222".equals(attrib.getValue()));
} else {
Assert.fail("Unexpected attribute name for attrib " + attrib.toString());
}
}
}
use of javax.xml.bind.annotation.XmlElement in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method bug61264_AttributeDispositionCONTENThandling.
/**
* the element referenced MailConstants.E_FRAG is treated in XML as an element with content but no attributes.
* However in JSON, it is just treated like an ordinary attribute.
* So, in JSON we DO want:
* "fr": "Here is some wonderful text and some more",
* We do NOT want it to be treated as if it was an element which would look like:
* "fr": [{
* "_content": "Here is some wonderful text and some more"
* }],
*/
@Test
public void bug61264_AttributeDispositionCONTENThandling() throws Exception {
StringBuilder sb;
final String uid = "uidString";
final String frag = "Fragment text";
final String name = "name Attribute";
// From stacktrace of where the SOAP response gets assembled:
// at com.zimbra.common.soap.Element.output(Element.java:432)
// at com.zimbra.soap.SoapServlet.sendResponse(SoapServlet.java:349)
// at com.zimbra.soap.SoapServlet.doWork(SoapServlet.java:307)
// at com.zimbra.soap.SoapServlet.doPost(SoapServlet.java:206)
// Bug 61264 is about issues with code that used JAXB which replaced code in GetCalendarItemSummaries
// which started with an element created using:
// calItemElem = lc.createElement(isAppointment ? MailConstants.E_APPOINTMENT : MailConstants.E_TASK);
// At present, that code has been reverted to be element based.
// For comparison purposes, create an JSONElement tree and a XMLElement tree
Element jsoncalItemElem = JSONElement.mFactory.createElement(MailConstants.E_APPOINTMENT);
jsoncalItemElem.addAttribute(MailConstants.A_UID, uid);
jsoncalItemElem.addAttribute("x_uid", uid);
Element instElt = jsoncalItemElem.addElement(MailConstants.E_INSTANCE);
instElt.addAttribute(MailConstants.E_FRAG, frag, Element.Disposition.CONTENT);
instElt.addAttribute(MailConstants.A_CAL_IS_EXCEPTION, true);
instElt.addAttribute(MailConstants.A_NAME, name);
Element xmlcalItemElem = XMLElement.mFactory.createElement(MailConstants.E_APPOINTMENT);
xmlcalItemElem.addAttribute(MailConstants.A_UID, uid);
xmlcalItemElem.addAttribute("x_uid", uid);
Element xmlinstElt = xmlcalItemElem.addElement(MailConstants.E_INSTANCE);
xmlinstElt.addAttribute(MailConstants.E_FRAG, frag, Element.Disposition.CONTENT);
xmlinstElt.addAttribute(MailConstants.A_CAL_IS_EXCEPTION, true);
xmlinstElt.addAttribute(MailConstants.A_NAME, name);
CalendaringDataInterface calData = null;
calData = new AppointmentData(uid, uid);
InstanceDataInfo instance = new InstanceDataInfo();
calData.addCalendaringInstance(instance);
instance.setIsException(true);
instance.setName(name);
instance.setFragment(frag);
Element jsonJaxbElem = JaxbUtil.jaxbToNamedElement(MailConstants.E_APPOINTMENT, MailConstants.NAMESPACE_STR, calData, JSONElement.mFactory);
Element xmlJaxbElem = JaxbUtil.jaxbToNamedElement(MailConstants.E_APPOINTMENT, MailConstants.NAMESPACE_STR, calData, XMLElement.mFactory);
// As AppointmentData doesn't have an XmlRootElement, this gives a poor choice for the root name.
// Element jacksonJaxbElem = JacksonUtil.jaxbToJSONElement(calData);
// This is probably a closer analog to JSONElement.mFactory.createElement(MailConstants.E_APPOINTMENT);
// Element jacksonJaxbElem = JacksonUtil.jaxbToJSONElement(calData, new QName("appt", null));
Element jacksonJaxbElem = JacksonUtil.jaxbToJSONElement(calData, new QName(MailConstants.E_APPOINTMENT, MailConstants.NAMESPACE));
Element parent4legacyJson = JSONElement.mFactory.createElement(new QName("legacy-json", MailConstants.NAMESPACE));
Element parent4jackson = JSONElement.mFactory.createElement(new QName("jacksonjson", MailConstants.NAMESPACE));
parent4legacyJson.addElement(jsoncalItemElem);
parent4jackson.addElement(jacksonJaxbElem);
sb = new StringBuilder();
xmlcalItemElem.output(sb);
logDebug("bug61264 - XML from XMLElement\n%1$s", sb.toString());
sb = new StringBuilder();
xmlJaxbElem.output(sb);
logDebug("bug61264 - XML from JAXB\n%1$s", sb.toString());
// something that is appendable for Element.out(Appendable) to play with
sb = new StringBuilder();
jsoncalItemElem.output(sb);
String jsonFromElement = sb.toString();
logDebug("bug61264 - JSON from JSONElement\n%1$s", jsonFromElement);
sb = new StringBuilder();
jsonJaxbElem.output(sb);
logDebug("bug61264 - JSON from JAXB\n%1$s", sb.toString());
sb = new StringBuilder();
jacksonJaxbElem.output(sb);
logDebug("bug61264 - JSON from JAXB using Jackson\n%1$s", sb.toString());
sb = new StringBuilder();
parent4legacyJson.output(sb);
logDebug("bug61264 - JSON from JAXB child using Jackson\n%1$s", sb.toString());
sb = new StringBuilder();
parent4jackson.output(sb);
logDebug("bug61264 - JSON from JSONElement child\n%1$s", sb.toString());
Assert.assertEquals("UID", uid, jacksonJaxbElem.getAttribute(MailConstants.A_UID));
Assert.assertEquals("x_uid", uid, jacksonJaxbElem.getAttribute("x_uid"));
Element instE = jacksonJaxbElem.getElement(MailConstants.E_INSTANCE);
Assert.assertNotNull("instance elem", instE);
Assert.assertEquals("fragment", frag, instE.getAttribute(MailConstants.E_FRAG));
Assert.assertTrue("is exception", instE.getAttributeBool(MailConstants.A_CAL_IS_EXCEPTION));
Assert.assertEquals("name", name, instE.getAttribute(MailConstants.A_NAME));
}
use of javax.xml.bind.annotation.XmlElement in project zm-mailbox by Zimbra.
the class JaxbInfo method processFieldRelatedAnnotations.
private void processFieldRelatedAnnotations(Annotation[] annots, String fieldName, Type defaultGenericType) {
WrappedElementInfo wrappedInfo = null;
for (Annotation annot : annots) {
if (annot instanceof XmlElementWrapper) {
XmlElementWrapper wrapper = (XmlElementWrapper) annot;
wrappedInfo = new WrappedElementInfo(wrapper, fieldName);
jaxbElemNodeInfo.add(wrappedInfo);
break;
}
}
for (Annotation annot : annots) {
if (annot instanceof XmlValue) {
elementValue = new JaxbValueInfo((XmlValue) annot, fieldName, defaultGenericType);
} else if (annot instanceof XmlAttribute) {
XmlAttribute attr = (XmlAttribute) annot;
String attrName = attr.name();
if ((attrName == null) || DEFAULT_MARKER.equals(attrName)) {
attrName = fieldName;
}
this.setXmlAttributeInfo(attr, fieldName, defaultGenericType);
this.attributeNames.add(attrName);
} else if (annot instanceof XmlElement) {
XmlElement xmlElem = (XmlElement) annot;
if (wrappedInfo == null) {
setXmlElementInfo(xmlElem, fieldName, defaultGenericType);
} else {
wrappedInfo.add(xmlElem, fieldName, defaultGenericType);
}
} else if (annot instanceof XmlElementRef) {
XmlElementRef xmlElemR = (XmlElementRef) annot;
if (wrappedInfo == null) {
setXmlElementInfo(xmlElemR, null, null);
} else {
wrappedInfo.add(xmlElemR, null, null);
}
} else if (annot instanceof XmlElements) {
JaxbPseudoNodeChoiceInfo choiceNode = new JaxbPseudoNodeChoiceInfo(fieldName, defaultGenericType);
if (wrappedInfo == null) {
jaxbElemNodeInfo.add(choiceNode);
} else {
wrappedInfo.add(choiceNode);
}
XmlElements xmlElemsAnnot = (XmlElements) annot;
for (XmlElement xmlE : xmlElemsAnnot.value()) {
choiceNode.add(xmlE);
}
} else if (annot instanceof XmlElementRefs) {
JaxbPseudoNodeChoiceInfo choiceNode = new JaxbPseudoNodeChoiceInfo(fieldName, defaultGenericType);
if (wrappedInfo == null) {
jaxbElemNodeInfo.add(choiceNode);
} else {
wrappedInfo.add(choiceNode);
}
XmlElementRefs elemRefs = (XmlElementRefs) annot;
for (XmlElementRef xmlE : elemRefs.value()) {
choiceNode.add(xmlE);
}
}
}
}
Aggregations