use of com.zimbra.common.soap.Element.JSONElement in project zm-mailbox by Zimbra.
the class TestGetContactsRequest method testComaSeparatedIds.
@Test
public void testComaSeparatedIds() throws Exception {
Element req = new JSONElement(MailConstants.GET_CONTACTS_REQUEST);
req.addAttribute(MailConstants.A_SYNC, true);
req.addNonUniqueElement(MailConstants.E_CONTACT).addAttribute(MailConstants.A_ID, String.format("%s,%s", ids.get(0), ids.get(1)));
Element resp = transport.invoke(req);
GetContactsResponse getContactsResp = JaxbUtil.elementToJaxb(resp);
List<ContactInfo> contacts = getContactsResp.getContacts();
assertEquals(2, contacts.size());
}
use of com.zimbra.common.soap.Element.JSONElement 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 com.zimbra.common.soap.Element.JSONElement 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 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) {
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);
}
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;
}
use of com.zimbra.common.soap.Element.JSONElement in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method zimbraOddKeyValuePairsAnnotation.
/**
* 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 do NOT 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">
* <oddElemName name="key1">value1</oddElemName>
* <oddElemName name="key2">value2-a</oddElemName>
* <oddElemName name="key2">value2-b</oddElemName>
* </key-value-pairs-tester>
*<br />
* Desired JSON :<br />
* {
* "_attrs": {
* "key1": "value1",
* "key2": [
* "value2-a",
* "value2-b"]
* },
* "_jsns": "urn:zimbraTest"
* }
*/
@Test
public void zimbraOddKeyValuePairsAnnotation() throws Exception {
Element jsonElem = JSONElement.mFactory.createElement(QName.get("key-value-pairs", "urn:zimbraTest"));
jsonElem.addKeyValuePair("key1", "value1", "oddElemName", "name");
jsonElem.addKeyValuePair("key2", "value2-a", "oddElemName", "name");
jsonElem.addKeyValuePair("key2", "value2-b", "oddElemName", "name");
List<Attr> attrs = Lists.newArrayList();
attrs.add(new Attr("key1", "value1"));
attrs.add(new Attr("key2", "value2-a"));
attrs.add(new Attr("key2", "value2-b"));
OddKeyValuePairsTester jaxb = new OddKeyValuePairsTester(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());
String origJson = jsonJaxbElem.prettyPrint();
logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", origJson);
OddKeyValuePairsTester roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, OddKeyValuePairsTester.class);
List<Element.KeyValuePair> elemKVPs = jsonJaxbElem.listKeyValuePairs();
Assert.assertEquals("elemKVP num", 3, elemKVPs.size());
Assert.assertEquals("prettyPrint", jsonElem.prettyPrint(), jsonJaxbElem.prettyPrint());
List<Attr> kvps = roundtripped.getAttrList();
Assert.assertEquals("roundtripped kvps num", 3, kvps.size());
jsonJaxbElem = JacksonUtil.jaxbToJSONElement(roundtripped);
String finalJson = jsonJaxbElem.prettyPrint();
if (!origJson.equals(finalJson)) {
logDebug("JSONElement from roundtripped JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
Assert.assertEquals("roundtripped JSON pretty print", origJson, finalJson);
}
}
Aggregations