Search in sources :

Example 1 with LmcSoapClientException

use of com.zimbra.cs.client.soap.LmcSoapClientException in project zm-mailbox by Zimbra.

the class LmcMessage method downloadAttachment.

public byte[] downloadAttachment(String partNo, String baseURL, LmcSession session, String cookieDomain, int msTimeout) throws LmcSoapClientException, IOException {
    // set the cookie.
    if (session == null)
        System.err.println(System.currentTimeMillis() + " " + Thread.currentThread() + " LmcMessage.downloadAttachment session=null");
    HttpClient client = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
    String url = baseURL + "?id=" + getID() + "&part=" + partNo;
    GetMethod get = new GetMethod(url);
    ZAuthToken zat = session.getAuthToken();
    Map<String, String> cookieMap = zat.cookieMap(false);
    if (cookieMap != null) {
        HttpState initialState = new HttpState();
        for (Map.Entry<String, String> ck : cookieMap.entrySet()) {
            Cookie cookie = new Cookie(cookieDomain, ck.getKey(), ck.getValue(), "/", -1, false);
            initialState.addCookie(cookie);
        }
        client.setState(initialState);
        client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
    }
    get.getParams().setSoTimeout(msTimeout);
    int statusCode = -1;
    try {
        statusCode = HttpClientUtil.executeMethod(client, get);
        // parse the response
        if (statusCode == 200) {
            return get.getResponseBody();
        } else {
            throw new LmcSoapClientException("Attachment download failed, status=" + statusCode);
        }
    } catch (IOException e) {
        System.err.println("Attachment download failed");
        e.printStackTrace();
        throw e;
    } finally {
        get.releaseConnection();
    }
}
Also used : Cookie(org.apache.commons.httpclient.Cookie) LmcSoapClientException(com.zimbra.cs.client.soap.LmcSoapClientException) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) HttpState(org.apache.commons.httpclient.HttpState) IOException(java.io.IOException) ZAuthToken(com.zimbra.common.auth.ZAuthToken) Map(java.util.Map)

Aggregations

ZAuthToken (com.zimbra.common.auth.ZAuthToken)1 LmcSoapClientException (com.zimbra.cs.client.soap.LmcSoapClientException)1 IOException (java.io.IOException)1 Map (java.util.Map)1 Cookie (org.apache.commons.httpclient.Cookie)1 HttpClient (org.apache.commons.httpclient.HttpClient)1 HttpState (org.apache.commons.httpclient.HttpState)1 GetMethod (org.apache.commons.httpclient.methods.GetMethod)1