Search in sources :

Example 1 with JSONElement

use of com.zimbra.common.soap.Element.JSONElement 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());
}
Also used : KeyValuePair(com.zimbra.soap.type.KeyValuePair) 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) CreateDistributionListResponse(com.zimbra.soap.account.message.CreateDistributionListResponse) DLInfo(com.zimbra.soap.account.type.DLInfo)

Example 2 with JSONElement

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

the class JaxbToJsonTest method XmlValueAnnotation.

/**
     * XmlValue should map to an attribute with name "_content"
     * At present, classes that want this feature need the annotation @JsonProperty("_content"),
     * otherwise, the name "value" is used.
     *    "chunk": [ { "disp": "disposition 1", "_content": "text 1\nIn the sun" },
     *               { "disp": "disPosition 2", "_content": "text 2" }],
     */
@Test
public void XmlValueAnnotation() throws Exception {
    String dispos1 = "disposition 1";
    String text1 = "text 1\nIn the sun";
    String dispos2 = "disPosition 2";
    String text2 = "text 2";
    // ---------------------------------  For Comparison - Element handling where the JAXB has an @XmlValue
    Element legacyElem = JSONElement.mFactory.createElement(MailConstants.DIFF_DOCUMENT_RESPONSE);
    legacyElem.addElement(MailConstants.E_CHUNK).addAttribute(MailConstants.A_DISP, dispos1).setText(text1);
    legacyElem.addElement(MailConstants.E_CHUNK).addAttribute(MailConstants.A_DISP, dispos2).setText(text2);
    logDebug("DiffDocumentResponse JSONElement ---> prettyPrint\n%1$s", legacyElem.prettyPrint());
    // --------------------------------- @XmlValue handling test - need @JsonProperty("_content") annotation
    DiffDocumentResponse ddResp = new DiffDocumentResponse();
    ddResp.addChunk(DispositionAndText.create(dispos1, text1));
    ddResp.addChunk(DispositionAndText.create(dispos2, text2));
    Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(ddResp);
    logDebug("DiffDocumentResponse JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
    List<Element> chunks = jsonJaxbElem.listElements();
    Assert.assertEquals("Number of child elements", 2, chunks.size());
    Element chunk1 = chunks.get(0);
    Element chunk2 = chunks.get(1);
    Assert.assertEquals("1st chunk disposition", dispos1, chunk1.getAttribute(MailConstants.A_DISP));
    Assert.assertEquals("1st chunk value", text1, chunk1.getText());
    Assert.assertEquals("2nd chunk disposition", dispos2, chunk2.getAttribute(MailConstants.A_DISP));
    Assert.assertEquals("2nd chunk value", text2, chunk2.getText());
    DiffDocumentResponse roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, DiffDocumentResponse.class);
    List<DispositionAndText> rtChunks = roundtripped.getChunks();
    Assert.assertEquals("Number of roundtripped chunks", 2, rtChunks.size());
    DispositionAndText rtChunk1 = rtChunks.get(0);
    DispositionAndText rtChunk2 = rtChunks.get(1);
    Assert.assertEquals("1st roundtripped chunk disposition", dispos1, rtChunk1.getDisposition());
    Assert.assertEquals("1st roundtripped chunk value", text1, rtChunk1.getText());
    Assert.assertEquals("2nd roundtripped chunk disposition", dispos2, rtChunk2.getDisposition());
    Assert.assertEquals("2nd roundtripped chunk value", text2, rtChunk2.getText());
}
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) DiffDocumentResponse(com.zimbra.soap.mail.message.DiffDocumentResponse) DispositionAndText(com.zimbra.soap.mail.type.DispositionAndText) FilterTest(com.zimbra.soap.mail.type.FilterTest) Test(org.junit.Test)

Example 3 with JSONElement

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

the class JaxbToJsonTest method ZmBooleanAntStringXmlElements.

/**
     *  {
     *    "status": [{
     *        "_content": "true"   # Actually get true not "true" but should be ok
     *      }],
     *    "message": [{
     *        "_content": "ver ndx message"
     *      }],
     *    "_jsns": "urn:zimbraAdmin"
     *  }
     */
@Test
public void ZmBooleanAntStringXmlElements() throws Exception {
    final String msg = "ver ndx message";
    // ---------------------------------  For Comparison - Element handling
    Element legacyElem = JSONElement.mFactory.createElement(AdminConstants.VERIFY_INDEX_RESPONSE);
    legacyElem.addElement(AdminConstants.E_STATUS).addText(String.valueOf(true));
    legacyElem.addElement(AdminConstants.E_MESSAGE).addText(msg);
    logDebug("VerifyIndexResponse JSONElement ---> prettyPrint\n%1$s", legacyElem.prettyPrint());
    VerifyIndexResponse viResp = new VerifyIndexResponse(true, msg);
    Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(viResp);
    logDebug("VerifyIndexResponse JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
    Assert.assertEquals("status", true, jsonJaxbElem.getAttributeBool(AdminConstants.E_STATUS));
    Assert.assertEquals("message", msg, jsonJaxbElem.getAttribute(AdminConstants.E_MESSAGE));
    VerifyIndexResponse roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, VerifyIndexResponse.class);
    Assert.assertEquals("roundtripped status", true, roundtripped.isStatus());
    Assert.assertEquals("roundtripped message", msg, roundtripped.getMessage());
}
Also used : VerifyIndexResponse(com.zimbra.soap.admin.message.VerifyIndexResponse) 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 4 with JSONElement

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

the class SoapTransport method generateSoapMessage.

protected Element generateSoapMessage(Element document, boolean raw, boolean noSession, String requestedAccountId, String changeToken, String tokenType, NotificationFormat nFormat, String curWaitSetID) {
    if (raw) {
        if (mDebugListener != null) {
            mDebugListener.sendSoapMessage(document);
        }
        return document;
    }
    // don't use the default protocol version if it's incompatible with the passed-in request
    SoapProtocol proto = mRequestProto;
    if (proto == SoapProtocol.SoapJS) {
        if (document instanceof XMLElement)
            proto = SoapProtocol.Soap12;
    } else {
        if (document instanceof JSONElement)
            proto = SoapProtocol.SoapJS;
    }
    SoapProtocol responseProto = mResponseProto == null ? proto : mResponseProto;
    String targetId = requestedAccountId != null ? requestedAccountId : mTargetAcctId;
    String targetName = targetId == null ? mTargetAcctName : null;
    Element context = null;
    if (generateContextHeader()) {
        context = SoapUtil.toCtxt(proto, mAuthToken, this.csrfToken);
        if (noSession) {
            SoapUtil.disableNotificationOnCtxt(context);
        } else {
            SoapUtil.addSessionToCtxt(context, mAuthToken == null ? null : mSessionId, mMaxNotifySeq, nFormat, curWaitSetID);
        }
        SoapUtil.addTargetAccountToCtxt(context, targetId, targetName);
        SoapUtil.addChangeTokenToCtxt(context, changeToken, tokenType);
        SoapUtil.addUserAgentToCtxt(context, getUserAgentName(), getUserAgentVersion());
        SoapUtil.addAuthTokenControl(context, voidOnExpired);
        if (responseProto != proto) {
            SoapUtil.addResponseProtocolToCtxt(context, responseProto);
        }
        String via = viaHolder.get().peek();
        if (via != null) {
            context.addUniqueElement(HeaderConstants.E_VIA).setText(via);
        }
    }
    Element envelope = proto.soapEnvelope(document, context);
    if (mDebugListener != null) {
        mDebugListener.sendSoapMessage(envelope);
    }
    return envelope;
}
Also used : JSONElement(com.zimbra.common.soap.Element.JSONElement) XMLElement(com.zimbra.common.soap.Element.XMLElement) JSONElement(com.zimbra.common.soap.Element.JSONElement) XMLElement(com.zimbra.common.soap.Element.XMLElement)

Example 5 with JSONElement

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

the class JaxbToJsonTest method elementRefHandling.

/**
 * XmlElementRef handling.  Note that slightly counter-intuitively, any name specified is ignored (unless
 * type=JAXBElement.class)
 * <pre>
 *   @XmlElementRef(name="ignored-name-root-elem-name-used-instead", type=StringAttribIntValue.class)
 *   private StringAttribIntValue byRef;
 *  </pre>
 * Desired JSON :
 * {
 *   "string-attr-int-value": [{
 *       "attr1": "my string",
 *       "_content": 321
 *     }],
 *   "_jsns": "urn:zimbraTest"
 * }
 */
@Test
public void elementRefHandling() throws Exception {
    String str = "my string";
    int num = 321;
    ElementRefTester jaxb = new ElementRefTester();
    StringAttribIntValue inner = new StringAttribIntValue(str, num);
    jaxb.setByRef(inner);
    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());
    ElementRefTester roundtrippedX = JaxbUtil.elementToJaxb(xmlElem, ElementRefTester.class);
    ElementRefTester roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, ElementRefTester.class);
    StringAttribIntValue rtByRef = roundtripped.getByRef();
    StringAttribIntValue rtXmlByRef = roundtrippedX.getByRef();
    Assert.assertEquals("roundtrippedX str", str, rtXmlByRef.getAttrib1());
    Assert.assertEquals("roundtrippedX num", num, rtXmlByRef.getMyValue());
    Assert.assertEquals("roundtripped str", str, rtByRef.getAttrib1());
    Assert.assertEquals("roundtripped num", num, rtByRef.getMyValue());
}
Also used : StringAttribIntValue(com.zimbra.soap.jaxb.StringAttribIntValue) 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) ElementRefTester(com.zimbra.soap.jaxb.ElementRefTester) FilterTest(com.zimbra.soap.mail.type.FilterTest) Test(org.junit.Test)

Aggregations

JSONElement (com.zimbra.common.soap.Element.JSONElement)49 XMLElement (com.zimbra.common.soap.Element.XMLElement)48 Element (com.zimbra.common.soap.Element)47 XmlAnyElement (javax.xml.bind.annotation.XmlAnyElement)45 XmlElement (javax.xml.bind.annotation.XmlElement)45 Test (org.junit.Test)44 FilterTest (com.zimbra.soap.mail.type.FilterTest)43 KeyValuePair (com.zimbra.soap.type.KeyValuePair)9 StringAttribIntValue (com.zimbra.soap.jaxb.StringAttribIntValue)5 StringAttrStringElem (com.zimbra.soap.jaxb.StringAttrStringElem)3 ViewEnum (com.zimbra.soap.jaxb.ViewEnum)3 WrappedEnumElemList (com.zimbra.soap.jaxb.WrappedEnumElemList)3 FilterTests (com.zimbra.soap.mail.type.FilterTests)3 ServiceException (com.zimbra.common.service.ServiceException)2 CreateDistributionListResponse (com.zimbra.soap.account.message.CreateDistributionListResponse)2 GetDistributionListResponse (com.zimbra.soap.account.message.GetDistributionListResponse)2 DLInfo (com.zimbra.soap.account.type.DLInfo)2 DistributionListInfo (com.zimbra.soap.account.type.DistributionListInfo)2 VerifyIndexResponse (com.zimbra.soap.admin.message.VerifyIndexResponse)2 EnumAttribEnumElem (com.zimbra.soap.jaxb.EnumAttribEnumElem)2