Search in sources :

Example 16 with XMLElement

use of com.zimbra.common.soap.Element.XMLElement 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 17 with XMLElement

use of com.zimbra.common.soap.Element.XMLElement 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)

Example 18 with XMLElement

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

the class JaxbToJsonTest method keyValuePairMbxSearch.

/**
 * CreateXMbxSearchRequest currently extends AdminKeyValuePairs which uses the {@link ZimbraKeyValuePairs}
 * annotation.
 *
 * Want JSON for KeyValuePairs to look something like :
 *   "_attrs": {
 *     "query": "Kitchen",
 *     "accounts": "*"
 *   },
 */
@Test
public void keyValuePairMbxSearch() throws Exception {
    final String notifMsg = "Search task %taskId% completed with status %status%. \nImported: %numMsgs% messages. \nSearch query used: %query%.";
    Element legacyElem = JSONElement.mFactory.createElement(XMbxSearchConstants.CREATE_XMBX_SEARCH_REQUEST);
    legacyElem.addKeyValuePair("query", "Kitchen");
    legacyElem.addKeyValuePair("accounts", "*");
    legacyElem.addKeyValuePair("limit", "0");
    legacyElem.addKeyValuePair("notificationMessage", notifMsg);
    logDebug("CreateXmbxSearchRequest JSONElement ---> prettyPrint\n%1$s", legacyElem.prettyPrint());
    // CreateXMbxSearchRequest extends AdminKeyValuePairs which uses ZimbraKeyValuePairs annotation to flag need
    // for special handling required  when serializing to JSON:
    // @ZimbraKeyValuePairs
    // @XmlElement(name=AdminConstants.E_A /* a */, required=false)
    // private List<KeyValuePair> keyValuePairs;
    CreateXMbxSearchRequest jaxb = new CreateXMbxSearchRequest();
    jaxb.addKeyValuePair(new KeyValuePair("query", "Kitchen"));
    jaxb.addKeyValuePair(new KeyValuePair("accounts", "*"));
    jaxb.addKeyValuePair(new KeyValuePair("limit", "0"));
    jaxb.addKeyValuePair(new KeyValuePair("notificationMessage", notifMsg));
    Element elem = JacksonUtil.jaxbToJSONElement(jaxb, XMbxSearchConstants.CREATE_XMBX_SEARCH_REQUEST);
    logDebug("CreateXmbxSearchRequest JSONElement from JAXB ---> prettyPrint\n%1$s", elem.prettyPrint());
    List<Element.KeyValuePair> kvps = elem.listKeyValuePairs();
    Assert.assertEquals("Number of keyValuePairs ", 4, kvps.size());
    Element.KeyValuePair kvp4 = kvps.get(3);
    Assert.assertEquals("KeyValuePair notificationMessage key", "notificationMessage", kvp4.getKey());
    Assert.assertEquals("KeyValuePair notificationMessage value", notifMsg, kvp4.getValue());
}
Also used : KeyValuePair(com.zimbra.soap.type.KeyValuePair) CreateXMbxSearchRequest(com.zimbra.soap.admin.message.CreateXMbxSearchRequest) 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 19 with XMLElement

use of com.zimbra.common.soap.Element.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());
}
Also used : GetDistributionListResponse(com.zimbra.soap.account.message.GetDistributionListResponse) DistributionListInfo(com.zimbra.soap.account.type.DistributionListInfo) 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) FilterTest(com.zimbra.soap.mail.type.FilterTest) Test(org.junit.Test)

Example 20 with XMLElement

use of com.zimbra.common.soap.Element.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 />
 * &lt;key-value-pairs-tester xmlns="urn:zimbraTest">
 *   &lt;a n="key1">value1&lt;/a>
 *   &lt;a n="key2">value2-a&lt;/a>
 *   &lt;a n="key2">value2-b&lt;/a>
 * &lt;/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<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());
}
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) WrappedKeyValuePairsTester(com.zimbra.soap.jaxb.WrappedKeyValuePairsTester) KeyValuePairsTester(com.zimbra.soap.jaxb.KeyValuePairsTester) OddKeyValuePairsTester(com.zimbra.soap.jaxb.OddKeyValuePairsTester) 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