简体   繁体   中英

Zimbra SOAP API - java?

I need to use the Zimbra Soap API for a new feature we're working on. However, I have not been able to find a lot of examples of Java clients using this API and I am overall a bit lost as to what I need to look into. (I am pretty new to using SoAP in general)

Basically, I will need to send a username and get some sort of zimbra ID for the user, modify user info with my java code, and then push that data back to the server.

I have found the wsdl files for this on the server, but I'm not sure where to go from here. Any help would be appreciated - anything from high level explanations to examples to detailed steps.

Thanks in advance!

Sadly, the Zimbra SOAP API isn't really SOAP. It's basically XML-over-HTTP. So you'll have to manually create the xml-documents you send to zimbra.

I don't know about a Java library for this, I did a Python one .

You can use Zimbra libraries for calling SOAP API:

package com.example.test
import com.zimbra.common.account.ZAttrProvisioning;
import com.zimbra.common.service.ServiceException;
import com.zimbra.common.soap.AdminConstants;
import com.zimbra.common.soap.Element;
import com.zimbra.common.soap.SoapHttpTransport;
import com.zimbra.cs.account.AccountServiceException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;

public class ZimbraApi {
  private static SoapHttpTransport soapHttpTransport = null;
  private String baseUrl = "https://mail.example.com:7071/service/admin/soap/";

  public String getAdminToken(String username, String password) {
    URL url = new URL(baseUrl);
    soapHttpTransport = new SoapHttpTransport(url.toURI().toString());
    soapHttpTransport.setVoidOnExpired(true);
    Element request = Element.XMLElement.mFactory.createElement(AdminConstants.AUTH_REQUEST);
    request.addAttribute(AdminConstants.E_NAME, username);
    request.addAttribute(AdminConstants.E_PASSWORD, password);
    Element response = soapHttpTransport.invoke(request);
    String token = response.getAttribute(AdminConstants.E_AUTH_TOKEN);
    soapHttpTransport.setAuthToken(token);
    return token;
  }

  public void createAccount(String username, String password) {
    Element request = Element.XMLElement.mFactory.createElement(AdminConstants.CREATE_ACCOUNT_REQUEST);
    request.addAttribute(AdminConstants.E_NAME, username);
    request.addAttribute(AdminConstants.E_PASSWORD, password);
    soapHttpTransport.invoke(request);

  }

}

ENDPOINT: https://your.domain.for.zimbra/service/soap/GetIdentitiesRequest

ENVELOPE SOAP

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
   <soap:Header>
    <context xmlns="urn:zimbra">
        <format type="xml"/>
        <csrfToken>????????</csrfToken>
    </context>
   </soap:Header>
   <soap:Body>
       <GetIdentitiesRequest xmlns="urn:zimbraAccount">
          <authToken>??????</authToken>
       </GetIdentitiesRequest>
   </soap:Body>
</soap:Envelope>

csrfToken your give in js window.csrfToken
authToken you give in cooke ZM_AUTH_TOKEN

The requests for "soap" must pass cookies ZM_AUTH_TOKEN , JSESSIONID and ZM_TEST

Code with Jetty Client API

ContentResponse contentResponse = client.POST(endpoint)
                .content(new StringContentProvider("XML envelope"))
                .cookie(new HttpCookie("JSESSIONID", jSessionIdCookieValue))
                .cookie(new HttpCookie("ZM_AUTH_TOKEN", authToken))
                .cookie(new HttpCookie("ZM_TEST", "true"))
                .send();

Moreover, there are many endpoints

https://your.domain.for.zimbra/service/soap/GetInfoRequest 
https://your.domain.for.zimbra/service/soap/GetRightsRequest
https://your.domain.for.zimbra/service/soap/CheckRightsRequest

The Envelope change just in child element from for each endpoint

<GetIdentitiesRequest>
<GetInfoRequest>
<GetRightsRequest>
<CheckRightsRequest>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM