Search in sources :

Example 26 with XMLElement

use of com.zimbra.common.soap.Element.XMLElement in project zm-mailbox by Zimbra.

the class JaxbToJsonTest method transientHandling.

/**
 * XmlTransient handling - Need to ignore fields with {@link XmlElementRef} annotation.
 */
@Test
public void transientHandling() throws Exception {
    String str = "my string - should NOT be serialized";
    int num = 321;
    TransientTester jaxb = new TransientTester(str, num);
    Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb);
    Element xmlElem = JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory, true, false);
    logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
    logDebug("XmlElement (for comparison) ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
    TransientTester roundtrippedX = JaxbUtil.elementToJaxb(xmlElem, TransientTester.class);
    TransientTester roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, TransientTester.class);
    Assert.assertEquals("roundtrippedX num", new Integer(num), roundtrippedX.getNummer());
    Assert.assertNull("roundtrippedX str", roundtrippedX.getToBeIgnored());
    Assert.assertEquals("roundtripped num", new Integer(num), roundtripped.getNummer());
    Assert.assertNull("roundtripped str", roundtripped.getToBeIgnored());
}
Also used : TransientTester(com.zimbra.soap.jaxb.TransientTester) XmlAnyElement(javax.xml.bind.annotation.XmlAnyElement) Element(com.zimbra.common.soap.Element) XMLElement(com.zimbra.common.soap.Element.XMLElement) JSONElement(com.zimbra.common.soap.Element.JSONElement) XmlElement(javax.xml.bind.annotation.XmlElement) FilterTest(com.zimbra.soap.mail.type.FilterTest) Test(org.junit.Test)

Example 27 with XMLElement

use of com.zimbra.common.soap.Element.XMLElement in project zm-mailbox by Zimbra.

the class JaxbToJsonTest method stringElemAndZimbraJsonlongAttr.

/**
 * Desired JSON :
 *      {
 *        "authToken": [{
 *            "_content": "authenticationtoken"
 *          }],
 *        "lifetime": 3000,
 *        "_jsns": "urn:zimbraAdmin"
 *      }
 */
@Test
public void stringElemAndZimbraJsonlongAttr() throws Exception {
    final long lifetime = 3000;
    final String lifetimeStr = new Long(lifetime).toString();
    final String authToken = "authenticationtoken";
    AuthResponse tstr = new AuthResponse();
    tstr.setAuthToken(authToken);
    tstr.setLifetime(lifetime);
    Element xmlElem = JaxbUtil.jaxbToElement(tstr, Element.XMLElement.mFactory, true, false);
    AuthResponse roundtrippedX = JaxbUtil.elementToJaxb(xmlElem, AuthResponse.class);
    Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(tstr);
    AuthResponse roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, AuthResponse.class);
    logDebug("XmlElement (for comparison) ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
    logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
    Assert.assertEquals("JSONElement lifetime", lifetimeStr, jsonJaxbElem.getAttribute("lifetime"));
    Assert.assertEquals("JSONElement authToken", authToken, jsonJaxbElem.getElement("authToken").getText());
    Assert.assertEquals("roundtripped lifetime", lifetime, roundtripped.getLifetime());
    Assert.assertEquals("roundtripped authToken", authToken, roundtripped.getAuthToken());
    Assert.assertEquals("roundtrippedX lifetime", lifetime, roundtrippedX.getLifetime());
    Assert.assertEquals("roundtrippedX authToken", authToken, roundtrippedX.getAuthToken());
}
Also used : XmlAnyElement(javax.xml.bind.annotation.XmlAnyElement) Element(com.zimbra.common.soap.Element) XMLElement(com.zimbra.common.soap.Element.XMLElement) JSONElement(com.zimbra.common.soap.Element.JSONElement) XmlElement(javax.xml.bind.annotation.XmlElement) AuthResponse(com.zimbra.soap.admin.message.AuthResponse) FilterTest(com.zimbra.soap.mail.type.FilterTest) Test(org.junit.Test)

Example 28 with XMLElement

use of com.zimbra.common.soap.Element.XMLElement in project zm-mailbox by Zimbra.

the class JaxbToJsonTest method stringAttrAndElem.

/**
 * Testing String attribute and String element.
 * Also testing differing namespaces on package, root element and field element
 * Desired JSON :
 *      {
 *        "attribute-1": "My attribute ONE",
 *        "element1": [{
 *            "_content": "My element ONE",
 *            "_jsns": "urn:ZimbraTest3"
 *          }],
 *        "_jsns": "urn:ZimbraTest2"
 *      }
 */
@Test
public void stringAttrAndElem() throws Exception {
    final String attr1Val = "My attribute ONE";
    final String elem1Val = "My element ONE";
    Element jsonElem = JSONElement.mFactory.createElement(QName.get("string-tester", "urn:zimbraTest2"));
    jsonElem.addAttribute("attribute-1", attr1Val);
    jsonElem.addNonUniqueElement(QName.get("element1", "urn:zimbraTest3")).addText(elem1Val);
    StringAttrStringElem tstr = new StringAttrStringElem();
    tstr.setAttr1(attr1Val);
    tstr.setElem1(elem1Val);
    Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(tstr);
    Element xmlElem = JaxbUtil.jaxbToElement(tstr, Element.XMLElement.mFactory, true, false);
    logDebug("JSONElement (for comparison) ---> prettyPrint\n%1$s", jsonElem.prettyPrint());
    logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
    logDebug("XmlElement (for comparison) ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
    StringAttrStringElem roundtrippedX = JaxbUtil.elementToJaxb(xmlElem, StringAttrStringElem.class);
    StringAttrStringElem roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, StringAttrStringElem.class);
    Assert.assertEquals("JSONElement attr1", attr1Val, jsonJaxbElem.getAttribute("attribute-1"));
    Assert.assertEquals("JSONElement elem1", elem1Val, jsonJaxbElem.getElement("element1").getText());
    Assert.assertEquals("roundtrippedX attr1", attr1Val, roundtrippedX.getAttr1());
    Assert.assertEquals("roundtrippedX elem1", elem1Val, roundtrippedX.getElem1());
    Assert.assertEquals("roundtripped attr1", attr1Val, roundtripped.getAttr1());
    Assert.assertEquals("roundtripped elem1", elem1Val, roundtripped.getElem1());
}
Also used : StringAttrStringElem(com.zimbra.soap.jaxb.StringAttrStringElem) XmlAnyElement(javax.xml.bind.annotation.XmlAnyElement) Element(com.zimbra.common.soap.Element) XMLElement(com.zimbra.common.soap.Element.XMLElement) JSONElement(com.zimbra.common.soap.Element.JSONElement) XmlElement(javax.xml.bind.annotation.XmlElement) FilterTest(com.zimbra.soap.mail.type.FilterTest) Test(org.junit.Test)

Example 29 with XMLElement

use of com.zimbra.common.soap.Element.XMLElement in project zm-mailbox by Zimbra.

the class JaxbToJsonTest method bug61264AttributeDispositionCONTENThandling.

/**
 * 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 bug61264AttributeDispositionCONTENThandling() 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.addNonUniqueElement(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.addNonUniqueElement(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.addNonUniqueElement(jsoncalItemElem);
    parent4jackson.addNonUniqueElement(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));
}
Also used : QName(org.dom4j.QName) XmlAnyElement(javax.xml.bind.annotation.XmlAnyElement) Element(com.zimbra.common.soap.Element) XMLElement(com.zimbra.common.soap.Element.XMLElement) JSONElement(com.zimbra.common.soap.Element.JSONElement) XmlElement(javax.xml.bind.annotation.XmlElement) CalendaringDataInterface(com.zimbra.soap.mail.type.CalendaringDataInterface) AppointmentData(com.zimbra.soap.mail.type.AppointmentData) InstanceDataInfo(com.zimbra.soap.mail.type.InstanceDataInfo) FilterTest(com.zimbra.soap.mail.type.FilterTest) Test(org.junit.Test)

Example 30 with XMLElement

use of com.zimbra.common.soap.Element.XMLElement in project zm-mailbox by Zimbra.

the class JaxbToJsonTest method booleanMarshal.

/**
 * By default JAXB renders true and false in XML as "true" and "false"
 * For Zimbra SOAP the XML flavor uses "1" and "0"
 * The JSON flavor uses "true" and "false"
 * @throws Exception
 */
@Test
public void booleanMarshal() throws Exception {
    Element legacy0Elem = JSONElement.mFactory.createElement(MailConstants.NO_OP_RESPONSE);
    legacy0Elem.addAttribute(MailConstants.A_WAIT_DISALLOWED, false);
    logDebug("NoOpResponse JSONElement ---> prettyPrint\n%1$s", legacy0Elem.prettyPrint());
    Element legacy1Elem = JSONElement.mFactory.createElement(MailConstants.NO_OP_RESPONSE);
    legacy1Elem.addAttribute(MailConstants.A_WAIT_DISALLOWED, true);
    Element legacyFalseElem = JSONElement.mFactory.createElement(MailConstants.NO_OP_RESPONSE);
    legacyFalseElem.addAttribute(MailConstants.A_WAIT_DISALLOWED, "false");
    Element legacyTrueElem = JSONElement.mFactory.createElement(MailConstants.NO_OP_RESPONSE);
    legacyTrueElem.addAttribute(MailConstants.A_WAIT_DISALLOWED, "true");
    // NoOpResponse has:
    // @XmlAttribute(name=MailConstants.A_WAIT_DISALLOWED, required=false)
    // private ZmBoolean waitDisallowed;
    NoOpResponse jaxb = NoOpResponse.create(false);
    Element xmlElem = JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory);
    logDebug("XMLElement from JAXB (false)---> prettyPrint\n%1$s", xmlElem.prettyPrint());
    Assert.assertEquals("false Value of 'waitDisallowed'", "0", xmlElem.getAttribute(MailConstants.A_WAIT_DISALLOWED));
    Element jsonElem = JacksonUtil.jaxbToJSONElement(jaxb, MailConstants.NO_OP_RESPONSE);
    logDebug("NoOpResponse JSONElement from JAXB (false)---> prettyPrint\n%1$s", jsonElem.prettyPrint());
    Assert.assertEquals("false Value of 'waitDisallowed'", "false", jsonElem.getAttribute(MailConstants.A_WAIT_DISALLOWED));
    // ensure that marshaling where Boolean has value true works
    jaxb.setWaitDisallowed(true);
    xmlElem = JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory);
    logDebug("XMLElement from JAXB (true) ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
    Assert.assertEquals("true Value of 'waitDisallowed'", "1", xmlElem.getAttribute(MailConstants.A_WAIT_DISALLOWED));
    jsonElem = JacksonUtil.jaxbToJSONElement(jaxb, MailConstants.NO_OP_RESPONSE);
    logDebug("NoOpResponse JSONElement from JAXB (true) ---> prettyPrint\n%1$s", jsonElem.prettyPrint());
    Assert.assertEquals("true Value of 'waitDisallowed'", "true", jsonElem.getAttribute(MailConstants.A_WAIT_DISALLOWED));
    // ensure that unmarshaling where XML Boolean representation is "1" for true works
    jaxb = JaxbUtil.elementToJaxb(legacy1Elem);
    Assert.assertEquals("legacy1Elem waitDisallowed value", Boolean.TRUE, jaxb.getWaitDisallowed());
    // ensure that unmarshaling where XML Boolean representation is "0" for false works
    jaxb = JaxbUtil.elementToJaxb(legacy0Elem);
    Assert.assertEquals("legacy0Elem waitDisallowed value", Boolean.FALSE, jaxb.getWaitDisallowed());
    // ensure that unmarshaling where XML Boolean representation is "false" works
    jaxb = JaxbUtil.elementToJaxb(legacyFalseElem);
    Assert.assertEquals("legacyFalseElem waitDisallowed value", Boolean.FALSE, jaxb.getWaitDisallowed());
    // ensure that unmarshaling where XML Boolean representation is "true" works
    jaxb = JaxbUtil.elementToJaxb(legacyTrueElem);
    Assert.assertEquals("legacyTrueElem waitDisallowed value", Boolean.TRUE, jaxb.getWaitDisallowed());
}
Also used : NoOpResponse(com.zimbra.soap.mail.message.NoOpResponse) XmlAnyElement(javax.xml.bind.annotation.XmlAnyElement) Element(com.zimbra.common.soap.Element) XMLElement(com.zimbra.common.soap.Element.XMLElement) JSONElement(com.zimbra.common.soap.Element.JSONElement) XmlElement(javax.xml.bind.annotation.XmlElement) FilterTest(com.zimbra.soap.mail.type.FilterTest) Test(org.junit.Test)

Aggregations

XMLElement (com.zimbra.common.soap.Element.XMLElement)146 Element (com.zimbra.common.soap.Element)116 JSONElement (com.zimbra.common.soap.Element.JSONElement)33 Test (org.junit.Test)32 XmlAnyElement (javax.xml.bind.annotation.XmlAnyElement)28 XmlElement (javax.xml.bind.annotation.XmlElement)28 FilterTest (com.zimbra.soap.mail.type.FilterTest)26 SoapHttpTransport (com.zimbra.common.soap.SoapHttpTransport)9 HashMap (java.util.HashMap)8 KeyValuePair (com.zimbra.soap.type.KeyValuePair)7 ArrayList (java.util.ArrayList)7 Account (com.zimbra.cs.account.Account)6 ZAuthToken (com.zimbra.common.auth.ZAuthToken)5 ByteBuilder (com.zimbra.common.mime.HeaderUtils.ByteBuilder)4 ServiceException (com.zimbra.common.service.ServiceException)4 DataSource (com.zimbra.cs.account.DataSource)4 Auth (com.zimbra.cs.service.account.Auth)4 URL (java.net.URL)4 Cookie (javax.servlet.http.Cookie)4 HttpException (org.apache.http.HttpException)4