Search in sources :

Example 1 with SoapHttpTransport

use of com.zimbra.common.soap.SoapHttpTransport in project zm-mailbox by Zimbra.

the class FlushCache method flushCacheOnAllServers.

private static void flushCacheOnAllServers(ZimbraSoapContext zsc, FlushCacheRequest req) throws ServiceException {
    // make sure we don't go round in loops
    req.getCache().setAllServers(false);
    Provisioning prov = Provisioning.getInstance();
    String localServerId = prov.getLocalServer().getId();
    for (Server server : prov.getAllMailClientServers()) {
        if (localServerId.equals(server.getId())) {
            continue;
        }
        Element request = JaxbUtil.jaxbToElement(req);
        ZimbraLog.misc.debug("Flushing cache on server: %s", server.getName());
        String adminUrl = URLUtil.getAdminURL(server, AdminConstants.ADMIN_SERVICE_URI);
        SoapHttpTransport mTransport = new SoapHttpTransport(adminUrl);
        mTransport.setAuthToken(zsc.getRawAuthToken());
        try {
            mTransport.invoke(request);
        } catch (ServiceException | IOException e) {
            // log and continue
            ZimbraLog.misc.warn("Encountered exception during FlushCache on server '%s', skip & continue with the next server", server.getName(), e);
        }
    }
}
Also used : Server(com.zimbra.cs.account.Server) ServiceException(com.zimbra.common.service.ServiceException) Element(com.zimbra.common.soap.Element) SoapHttpTransport(com.zimbra.common.soap.SoapHttpTransport) IOException(java.io.IOException) Provisioning(com.zimbra.cs.account.Provisioning)

Example 2 with SoapHttpTransport

use of com.zimbra.common.soap.SoapHttpTransport in project zm-mailbox by Zimbra.

the class SpamExtract method extract.

private static void extract(String authToken, Account account, Server server, String query, File outdir, boolean delete, boolean raw) throws ServiceException, HttpException, SoapFaultException, IOException {
    String soapURL = getSoapURL(server, false);
    URL restURL = getServerURL(server, false);
    // CLI only, don't need conn mgr
    HttpClient hc = new HttpClient();
    HttpState state = new HttpState();
    GetMethod gm = new GetMethod();
    gm.setFollowRedirects(true);
    Cookie authCookie = new Cookie(restURL.getHost(), ZimbraCookie.COOKIE_ZM_AUTH_TOKEN, authToken, "/", -1, false);
    state.addCookie(authCookie);
    hc.setState(state);
    hc.getHostConfiguration().setHost(restURL.getHost(), restURL.getPort(), Protocol.getProtocol(restURL.getProtocol()));
    gm.getParams().setSoTimeout(60000);
    if (verbose) {
        LOG.info("Mailbox requests to: " + restURL);
    }
    SoapHttpTransport transport = new SoapHttpTransport(soapURL);
    transport.setRetryCount(1);
    transport.setTimeout(0);
    transport.setAuthToken(authToken);
    int totalProcessed = 0;
    boolean haveMore = true;
    int offset = 0;
    while (haveMore) {
        Element searchReq = new Element.XMLElement(MailConstants.SEARCH_REQUEST);
        searchReq.addElement(MailConstants.A_QUERY).setText(query);
        searchReq.addAttribute(MailConstants.A_SEARCH_TYPES, MailItem.Type.MESSAGE.toString());
        searchReq.addAttribute(MailConstants.A_QUERY_OFFSET, offset);
        searchReq.addAttribute(MailConstants.A_LIMIT, BATCH_SIZE);
        try {
            if (LOG.isDebugEnabled()) {
                LOG.debug(searchReq.prettyPrint());
            }
            Element searchResp = transport.invoke(searchReq, false, true, account.getId());
            if (LOG.isDebugEnabled()) {
                LOG.debug(searchResp.prettyPrint());
            }
            StringBuilder deleteList = new StringBuilder();
            List<String> ids = new ArrayList<String>();
            for (Iterator<Element> iter = searchResp.elementIterator(MailConstants.E_MSG); iter.hasNext(); ) {
                offset++;
                Element e = iter.next();
                String mid = e.getAttribute(MailConstants.A_ID);
                if (mid == null) {
                    LOG.warn("null message id SOAP response");
                    continue;
                }
                LOG.debug("adding id %s", mid);
                ids.add(mid);
                if (ids.size() >= BATCH_SIZE || !iter.hasNext()) {
                    StringBuilder path = new StringBuilder("/service/user/" + account.getName() + "/?fmt=tgz&list=" + StringUtils.join(ids, ","));
                    LOG.debug("sending request for path %s", path.toString());
                    List<String> extractedIds = extractMessages(hc, gm, path.toString(), outdir, raw);
                    if (ids.size() > extractedIds.size()) {
                        ids.removeAll(extractedIds);
                        LOG.warn("failed to extract %s", ids);
                    }
                    for (String id : extractedIds) {
                        deleteList.append(id).append(',');
                    }
                    ids.clear();
                }
                totalProcessed++;
            }
            haveMore = false;
            String more = searchResp.getAttribute(MailConstants.A_QUERY_MORE);
            if (more != null && more.length() > 0) {
                try {
                    int m = Integer.parseInt(more);
                    if (m > 0) {
                        haveMore = true;
                        try {
                            Thread.sleep(SLEEP_TIME);
                        } catch (InterruptedException e) {
                        }
                    }
                } catch (NumberFormatException nfe) {
                    LOG.warn("more flag from server not a number: " + more, nfe);
                }
            }
            if (delete && deleteList.length() > 0) {
                // -1 removes trailing comma
                deleteList.deleteCharAt(deleteList.length() - 1);
                Element msgActionReq = new Element.XMLElement(MailConstants.MSG_ACTION_REQUEST);
                Element action = msgActionReq.addElement(MailConstants.E_ACTION);
                action.addAttribute(MailConstants.A_ID, deleteList.toString());
                action.addAttribute(MailConstants.A_OPERATION, ItemAction.OP_HARD_DELETE);
                if (LOG.isDebugEnabled()) {
                    LOG.debug(msgActionReq.prettyPrint());
                }
                Element msgActionResp = transport.invoke(msgActionReq, false, true, account.getId());
                if (LOG.isDebugEnabled()) {
                    LOG.debug(msgActionResp.prettyPrint());
                }
                //put offset back to 0 so we always get top N messages even after delete
                offset = 0;
            }
        } finally {
            gm.releaseConnection();
        }
    }
    LOG.info("Total messages processed: " + totalProcessed);
}
Also used : ZimbraCookie(com.zimbra.common.util.ZimbraCookie) Cookie(org.apache.commons.httpclient.Cookie) Element(com.zimbra.common.soap.Element) HttpState(org.apache.commons.httpclient.HttpState) ArrayList(java.util.ArrayList) URL(java.net.URL) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) SoapHttpTransport(com.zimbra.common.soap.SoapHttpTransport)

Example 3 with SoapHttpTransport

use of com.zimbra.common.soap.SoapHttpTransport in project zm-mailbox by Zimbra.

the class GalSyncAccountUtil method syncGalAccount.

private void syncGalAccount() throws ServiceException, IOException {
    checkArgs();
    mTransport = null;
    try {
        mTransport = new SoapHttpTransport(mAdminURL);
        auth();
        mTransport.setAuthToken(mAuth);
        XMLElement req = new XMLElement(AdminConstants.SYNC_GAL_ACCOUNT_REQUEST);
        Element acct = req.addElement(AdminConstants.E_ACCOUNT);
        acct.addAttribute(AdminConstants.A_ID, mAccountId);
        Element ds = acct.addElement(AdminConstants.E_DATASOURCE);
        if (mDataSourceId != null)
            ds.addAttribute(AdminConstants.A_BY, "id").setText(mDataSourceId);
        else
            ds.addAttribute(AdminConstants.A_BY, "name").setText(mDataSourceName);
        if (mFullSync)
            ds.addAttribute(AdminConstants.A_FULLSYNC, "TRUE");
        if (mForceSync)
            ds.addAttribute(AdminConstants.A_RESET, "TRUE");
        mTransport.invoke(req);
    } finally {
        if (mTransport != null)
            mTransport.shutdown();
    }
}
Also used : XMLElement(com.zimbra.common.soap.Element.XMLElement) Element(com.zimbra.common.soap.Element) SoapHttpTransport(com.zimbra.common.soap.SoapHttpTransport) XMLElement(com.zimbra.common.soap.Element.XMLElement)

Example 4 with SoapHttpTransport

use of com.zimbra.common.soap.SoapHttpTransport in project zm-mailbox by Zimbra.

the class SoapCLI method initTransport.

private void initTransport() {
    SoapHttpTransport trans = new SoapHttpTransport(mServerUrl);
    trans.setRetryCount(1);
    mTrans = trans;
}
Also used : SoapHttpTransport(com.zimbra.common.soap.SoapHttpTransport)

Example 5 with SoapHttpTransport

use of com.zimbra.common.soap.SoapHttpTransport in project zm-mailbox by Zimbra.

the class GalSyncAccountUtil method addGalSyncDataSource.

private Element addGalSyncDataSource(String accountName, String dsName, String domain, String type, String folder, String pollingInterval) throws ServiceException, IOException {
    mTransport = null;
    try {
        mTransport = new SoapHttpTransport(mAdminURL);
        auth();
        mTransport.setAuthToken(mAuth);
        XMLElement req = new XMLElement(AdminConstants.ADD_GAL_SYNC_DATASOURCE_REQUEST);
        req.addAttribute(AdminConstants.A_NAME, dsName);
        req.addAttribute(AdminConstants.A_DOMAIN, domain);
        req.addAttribute(AdminConstants.A_TYPE, type);
        if (folder != null)
            req.addAttribute(AdminConstants.E_FOLDER, folder);
        Element acct = req.addElement(AdminConstants.E_ACCOUNT);
        acct.addAttribute(AdminConstants.A_BY, AccountBy.name.name());
        acct.setText(accountName);
        if (pollingInterval != null)
            req.addElement(AdminConstants.E_A).addAttribute(AdminConstants.A_N, Provisioning.A_zimbraDataSourcePollingInterval).setText(pollingInterval);
        return mTransport.invokeWithoutSession(req);
    } finally {
        if (mTransport != null)
            mTransport.shutdown();
    }
}
Also used : XMLElement(com.zimbra.common.soap.Element.XMLElement) Element(com.zimbra.common.soap.Element) SoapHttpTransport(com.zimbra.common.soap.SoapHttpTransport) XMLElement(com.zimbra.common.soap.Element.XMLElement)

Aggregations

SoapHttpTransport (com.zimbra.common.soap.SoapHttpTransport)69 Element (com.zimbra.common.soap.Element)54 Test (org.junit.Test)32 SoapFaultException (com.zimbra.common.soap.SoapFaultException)16 ServiceException (com.zimbra.common.service.ServiceException)13 Account (com.zimbra.cs.account.Account)13 DeployZimletRequest (com.zimbra.soap.admin.message.DeployZimletRequest)13 AuthRequest (com.zimbra.soap.account.message.AuthRequest)12 AttachmentIdAttrib (com.zimbra.soap.admin.type.AttachmentIdAttrib)12 XMLElement (com.zimbra.common.soap.Element.XMLElement)11 AuthToken (com.zimbra.cs.account.AuthToken)10 ZAuthToken (com.zimbra.common.auth.ZAuthToken)8 AuthResponse (com.zimbra.soap.account.message.AuthResponse)8 IOException (java.io.IOException)8 ZMailbox (com.zimbra.client.ZMailbox)7 ZimbraAuthToken (com.zimbra.cs.account.ZimbraAuthToken)6 AccountSelector (com.zimbra.soap.type.AccountSelector)6 HashMap (java.util.HashMap)5 Domain (com.zimbra.cs.account.Domain)4 Server (com.zimbra.cs.account.Server)4