use of com.zimbra.common.soap.Element.XMLElement 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());
}
use of com.zimbra.common.soap.Element.XMLElement in project zm-mailbox by Zimbra.
the class TestUtil method getAdminSoapTransport.
/**
* Returns an authenticated transport for the <tt>zimbra</tt> account.
*/
public static SoapTransport getAdminSoapTransport() throws SoapFaultException, IOException, ServiceException {
SoapHttpTransport transport = new SoapHttpTransport(getAdminSoapUrl());
// Create auth element
Element auth = new XMLElement(AdminConstants.AUTH_REQUEST);
auth.addNonUniqueElement(AdminConstants.E_NAME).setText(LC.zimbra_ldap_user.value());
auth.addNonUniqueElement(AdminConstants.E_PASSWORD).setText(LC.zimbra_ldap_password.value());
// Authenticate and get auth token
Element response = transport.invoke(auth);
String authToken = response.getElement(AccountConstants.E_AUTH_TOKEN).getText();
transport.setAuthToken(authToken);
transport.setAdmin(true);
return transport;
}
use of com.zimbra.common.soap.Element.XMLElement in project zm-mailbox by Zimbra.
the class FileUploadServletTest method testFileUploadAuthTokenNotCsrfEnabled.
@Test
public void testFileUploadAuthTokenNotCsrfEnabled() throws Exception {
URL url = new URL("http://localhost:7070/service/upload?lbfums=");
ByteBuilder bb = new ByteBuilder(CharsetUtil.UTF_8);
addFormField(bb, "_charset_", "");
addFormField(bb, "filename1", filename1);
addFormFile(bb, filename1, "text/plain", content1);
endForm(bb);
byte[] form = bb.toByteArray();
HashMap<String, String> headers = new HashMap<String, String>();
Account acct = Provisioning.getInstance().get(Key.AccountBy.name, "test@zimbra.com");
XMLElement req = new XMLElement(AccountConstants.AUTH_REQUEST);
com.zimbra.common.soap.Element a = req.addUniqueElement(AccountConstants.E_ACCOUNT);
a.addAttribute(AccountConstants.A_BY, "name");
a.setText(acct.getName());
req.addUniqueElement(AccountConstants.E_PASSWORD).setText("secret");
Element response = new Auth().handle(req, ServiceTestUtil.getRequestContext(acct));
String authToken = response.getElement(AccountConstants.E_AUTH_TOKEN).getText();
MockHttpServletRequest mockreq = new MockHttpServletRequest(form, url, "multipart/form-data; boundary=" + boundary, 7070, "test", headers);
mockreq.setAttribute(CsrfFilter.CSRF_TOKEN_CHECK, Boolean.FALSE);
Cookie cookie = new Cookie("ZM_AUTH_TOKEN", authToken);
mockreq.setCookies(cookie);
MockHttpServletResponse resp = new MockHttpServletResponse();
servlet.doPost(mockreq, resp);
String respStrg = resp.output.toString();
System.out.println(respStrg);
assertTrue(respStrg.contains("200"));
}
use of com.zimbra.common.soap.Element.XMLElement in project zm-mailbox by Zimbra.
the class FileUploadServletTest method testFileUploadAuthTokenCsrfEnabledButNoCsrfToken.
@Test
public void testFileUploadAuthTokenCsrfEnabledButNoCsrfToken() throws Exception {
URL url = new URL("http://localhost:7070/service/upload?lbfums=");
ByteBuilder bb = new ByteBuilder(CharsetUtil.UTF_8);
addFormField(bb, "_charset_", "");
addFormField(bb, "filename1", filename1);
addFormFile(bb, filename1, "text/plain", content1);
endForm(bb);
byte[] form = bb.toByteArray();
HashMap<String, String> headers = new HashMap<String, String>();
Account acct = Provisioning.getInstance().get(Key.AccountBy.name, "test@zimbra.com");
XMLElement req = new XMLElement(AccountConstants.AUTH_REQUEST);
req.addAttribute(AccountConstants.A_CSRF_SUPPORT, "1");
com.zimbra.common.soap.Element a = req.addUniqueElement(AccountConstants.E_ACCOUNT);
a.addAttribute(AccountConstants.A_BY, "name");
a.setText(acct.getName());
req.addUniqueElement(AccountConstants.E_PASSWORD).setText("secret");
Map<String, Object> context = ServiceTestUtil.getRequestContext(acct);
MockHttpServletRequest authReq = (MockHttpServletRequest) context.get(SoapServlet.SERVLET_REQUEST);
authReq.setAttribute(Provisioning.A_zimbraCsrfTokenCheckEnabled, Boolean.TRUE);
Random nonceGen = new Random();
authReq.setAttribute(CsrfFilter.CSRF_SALT, nonceGen.nextInt() + 1);
Element response = new Auth().handle(req, context);
String authToken = response.getElement(AccountConstants.E_AUTH_TOKEN).getText();
MockHttpServletRequest mockreq = new MockHttpServletRequest(form, url, "multipart/form-data; boundary=" + boundary, 7070, "test", headers);
mockreq.setAttribute(CsrfFilter.CSRF_TOKEN_CHECK, Boolean.TRUE);
Cookie cookie = new Cookie("ZM_AUTH_TOKEN", authToken);
mockreq.setCookies(cookie);
MockHttpServletResponse resp = new MockHttpServletResponse();
servlet.doPost(mockreq, resp);
// <html><head><script language='javascript'>function doit() { window.parent._uploadManager.loaded(401,'null'); }
// </script></head><body onload='doit()'></body></html>
String respStrg = resp.output.toString();
assertTrue(respStrg.contains("401"));
}
use of com.zimbra.common.soap.Element.XMLElement in project zm-mailbox by Zimbra.
the class FileUploadServletTest method testFileUploadAuthTokenCsrfEnabled2.
@Test
public void testFileUploadAuthTokenCsrfEnabled2() throws Exception {
HashMap<String, String> headers = new HashMap<String, String>();
Account acct = Provisioning.getInstance().get(Key.AccountBy.name, "test2@zimbra.com");
XMLElement req = new XMLElement(AccountConstants.AUTH_REQUEST);
req.addAttribute(AccountConstants.A_CSRF_SUPPORT, "1");
com.zimbra.common.soap.Element a = req.addUniqueElement(AccountConstants.E_ACCOUNT);
a.addAttribute(AccountConstants.A_BY, "name");
a.setText(acct.getName());
req.addUniqueElement(AccountConstants.E_PASSWORD).setText("secret");
Map<String, Object> context = ServiceTestUtil.getRequestContext(acct);
MockHttpServletRequest authReq = (MockHttpServletRequest) context.get(SoapServlet.SERVLET_REQUEST);
authReq.setAttribute(Provisioning.A_zimbraCsrfTokenCheckEnabled, Boolean.TRUE);
Random nonceGen = new Random();
authReq.setAttribute(CsrfFilter.CSRF_SALT, nonceGen.nextInt() + 1);
Element response = new Auth().handle(req, context);
String authToken = response.getElement(AccountConstants.E_AUTH_TOKEN).getText();
String csrfToken = response.getElement("csrfToken").getText();
URL url = new URL("http://localhost:7070/service/upload?lbfums=");
ByteBuilder bb = new ByteBuilder(CharsetUtil.UTF_8);
addFormField(bb, "_charset_", "");
addFormField(bb, "filename1", filename1);
addFormField(bb, "csrfToken", csrfToken);
addFormFile(bb, filename1, "text/plain", content1);
endForm(bb);
byte[] form = bb.toByteArray();
MockHttpServletRequest mockreq = new MockHttpServletRequest(form, url, "multipart/form-data; boundary=" + boundary, 7070, "test", headers);
mockreq.setAttribute(CsrfFilter.CSRF_TOKEN_CHECK, Boolean.TRUE);
Cookie cookie = new Cookie("ZM_AUTH_TOKEN", authToken);
mockreq.setCookies(cookie);
MockHttpServletResponse resp = new MockHttpServletResponse();
servlet.doPost(mockreq, resp);
String respStrg = resp.output.toString();
assertTrue(respStrg.contains("200"));
}
Aggregations