use of com.zimbra.common.soap.Element.XMLElement in project zm-mailbox by Zimbra.
the class ZAuthToken method main.
public static void main(String[] args) throws Exception {
SoapHttpTransport trans = new SoapHttpTransport("http://localhost:7070/service/soap/");
trans.setUserAgent("ZAuthTokenTest", null);
SoapHttpTransport.DebugListener dl = new SoapHttpTransport.DebugListener() {
public void receiveSoapMessage(Element envelope) {
System.out.printf("======== SOAP RECEIVE =========\n");
System.out.println(envelope.prettyPrint());
System.out.printf("===============================\n");
}
public void sendSoapMessage(Element envelope) {
System.out.println("========== SOAP SEND ==========");
System.out.println(envelope.prettyPrint());
System.out.println("===============================");
}
};
trans.setDebugListener(dl);
Element req = new XMLElement(AccountConstants.AUTH_REQUEST);
req.addElement(AccountConstants.E_ACCOUNT).addAttribute(AccountConstants.A_BY, "name").setText("user1");
req.addElement(AccountConstants.E_PASSWORD).setText("test123");
Element resp = trans.invoke(req);
Element authToken = resp.getElement(AccountConstants.E_AUTH_TOKEN);
ZAuthToken zAuthToken = new ZAuthToken(authToken, false);
String type = zAuthToken.getType();
String value = zAuthToken.getValue();
Map<String, String> attrs = zAuthToken.getAttrs();
System.out.println("type: " + type);
System.out.println("value: " + value);
System.out.println("attrs:");
if (attrs != null) {
for (Map.Entry<String, String> a : attrs.entrySet()) System.out.println(a.getKey() + ": " + a.getValue());
}
Element req1 = new XMLElement(AccountConstants.GET_INFO_REQUEST);
trans.setAuthToken(new ZAuthToken(null, value, attrs));
resp = trans.invoke(req1);
Element req2 = new XMLElement(AccountConstants.GET_INFO_REQUEST);
attrs = new HashMap<String, String>();
attrs.put("X", "x ...");
attrs.put("Y", "y ...");
trans.setAuthToken(new ZAuthToken("foobar", null, attrs));
resp = trans.invoke(req2);
}
use of com.zimbra.common.soap.Element.XMLElement in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method mixedHandlingJustText.
/**
* {@link XmlMixed} handling
* In the places we use XmlMixed, we typically have either just text or just elements
* This tests where we have just text.
*/
@Test
public void mixedHandlingJustText() throws Exception {
String textStr = "text string";
List<Object> elems = Lists.newArrayList();
MixedTester jaxb = new MixedTester();
elems.add(textStr);
jaxb.setElems(elems);
Element xmlElem = JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory, true, false);
xmlElem = JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory, true, false);
logDebug("XmlElement (for comparison) [Mixed has just text] ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
MixedTester roundtrippedX = JaxbUtil.elementToJaxb(xmlElem, MixedTester.class);
Assert.assertEquals("roundtrippedX [Mixed has just text] num elems", 1, roundtrippedX.getElems().size());
Assert.assertEquals("roundtrippedX [Mixed has just text] str", textStr, (String) roundtrippedX.getElems().get(0));
Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb);
logDebug("JSONElement from JAXB [Mixed has just text] ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
MixedTester roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, MixedTester.class);
Assert.assertEquals("roundtripped [Mixed has just text] num elems", 1, roundtripped.getElems().size());
Assert.assertEquals("roundtripped [Mixed has just text] str", textStr, (String) roundtripped.getElems().get(0));
}
use of com.zimbra.common.soap.Element.XMLElement in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method multipleDispositionCONTENTAttributes.
/**
* <p>Demonstrates that CANNOT have more than one attribute with same name, even if using
* {@link Element.Disposition.CONTENT} to force treating the attribute as an element in XML.</p>
* Desired serialization to XML:
* <pre>
* <multi-content-attrs xmlns="urn:zimbraTest">
* <soapURL>https://soap.example.test</soapURL>
* </multi-content-attrs>
* </pre>
* Desired serialization to JSON:
* <pre>
* {
* "soapURL": "https://soap.example.test",
* "_jsns": "urn:zimbraTest"
* }
* </pre>
*/
@Test
public void multipleDispositionCONTENTAttributes() throws Exception {
final String httpSoap = "http://soap.example.test";
final String httpsSoap = "https://soap.example.test";
Element jsonElem = JSONElement.mFactory.createElement(QName.get("multi-content-attrs", "urn:zimbraTest"));
Element xmlElem = XMLElement.mFactory.createElement(QName.get("multi-content-attrs", "urn:zimbraTest"));
jsonElem.addAttribute(AccountConstants.E_SOAP_URL, httpSoap, Element.Disposition.CONTENT);
jsonElem.addAttribute(AccountConstants.E_SOAP_URL, httpsSoap, Element.Disposition.CONTENT);
xmlElem.addAttribute(AccountConstants.E_SOAP_URL, httpSoap, Element.Disposition.CONTENT);
xmlElem.addAttribute(AccountConstants.E_SOAP_URL, httpsSoap, Element.Disposition.CONTENT);
logDebug("MultiContentAttrs XMLElement ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
logDebug("MultiContentAttrs JSONElement ---> prettyPrint\n%1$s", jsonElem.prettyPrint());
Assert.assertEquals("XMLElement soapURL as attribute", httpsSoap, xmlElem.getAttribute(AccountConstants.E_SOAP_URL));
Assert.assertEquals("XMLElement num soapURL elements", 1, xmlElem.listElements(AccountConstants.E_SOAP_URL).size());
Assert.assertEquals("XMLElement soapURL as element", httpsSoap, xmlElem.getElement(AccountConstants.E_SOAP_URL).getText());
Assert.assertEquals("JSONElement soapURL as attribute", httpsSoap, jsonElem.getAttribute(AccountConstants.E_SOAP_URL));
// Note difference from XMLElement - for JSON this is Always an attribute but for XML it can be treated as an element
Assert.assertEquals("JSONElement num soapURL elements", 0, jsonElem.listElements(AccountConstants.E_SOAP_URL).size());
}
use of com.zimbra.common.soap.Element.XMLElement 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<com.zimbra.common.soap.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);
}
}
use of com.zimbra.common.soap.Element.XMLElement in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method contentList.
/**
* Test for "List of strings" field annotated with {@link XmlElement}.
* Desired JSON:
* {
* "more": false,
* "total": 23,
* "dlm": [
* {
* "_content": "dlmember1@no.where"
* },
* {
* "_content": "dlmember2@no.where"
* },
* {
* "_content": "dlmember3@no.where"
* }],
* "_jsns": "urn:zimbraAccount"
* }
*/
@Test
public void contentList() throws Exception {
Element legacyElem = JSONElement.mFactory.createElement(AccountConstants.GET_DISTRIBUTION_LIST_MEMBERS_RESPONSE);
legacyElem.addAttribute(AccountConstants.A_MORE, false);
legacyElem.addAttribute(AccountConstants.A_TOTAL, 23);
legacyElem.addElement(AccountConstants.E_DLM).setText("dlmember1@no.where");
legacyElem.addElement(AccountConstants.E_DLM).setText("dlmember2@no.where");
legacyElem.addElement(AccountConstants.E_DLM).setText("dlmember3@no.where");
logDebug("GetDistributionListMembersResponse JSONElement ---> prettyPrint\n%1$s", legacyElem.prettyPrint());
// GetDistributionListMembersResponse has:
// @XmlElement(name=AccountConstants.E_DLM, required=false)
// private List<String> dlMembers = Lists.newArrayList();
GetDistributionListMembersResponse jaxb = new GetDistributionListMembersResponse();
jaxb.setMore(false);
jaxb.setTotal(23);
jaxb.addDlMember("dlmember1@no.where");
jaxb.addDlMember("dlmember2@no.where");
jaxb.addDlMember("dlmember3@no.where");
Element elem = JacksonUtil.jaxbToJSONElement(jaxb, AccountConstants.GET_DISTRIBUTION_LIST_MEMBERS_RESPONSE);
logDebug("GetDistributionListMembersResponse JSONElement from JAXB ---> prettyPrint\n%1$s", elem.prettyPrint());
List<Element> dlMembers = elem.listElements(AccountConstants.E_DLM);
Assert.assertEquals("Number of dlMembers", 3, dlMembers.size());
Element dlMem3 = dlMembers.get(2);
Assert.assertEquals("dlMember 3", "dlmember3@no.where", dlMem3.getText());
Assert.assertEquals("total", 23, elem.getAttributeInt(AccountConstants.A_TOTAL));
Assert.assertEquals("more", false, elem.getAttributeBool(AccountConstants.A_MORE));
Assert.assertEquals("prettyPrint", legacyElem.prettyPrint(), elem.prettyPrint());
}
Aggregations