Search in sources :

Example 31 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)

Example 32 with SoapHttpTransport

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

the class ProxyTarget method execute.

public Pair<Element, Element> execute(Element request, ZimbraSoapContext zsc) throws ServiceException {
    if (zsc == null)
        return new Pair<Element, Element>(null, dispatch(request));
    SoapProtocol proto = request instanceof Element.JSONElement ? SoapProtocol.SoapJS : SoapProtocol.Soap12;
    if (proto == SoapProtocol.Soap12 && zsc.getRequestProtocol() == SoapProtocol.Soap11) {
        proto = SoapProtocol.Soap11;
    }
    /* Bug 77604 When a user has been configured to change their password on next login, the resulting proxied
         * ChangePasswordRequest was failing because account was specified in context but no authentication token
         * was supplied.  The server handler rejects a context which has account information but no authentication
         * info - see ZimbraSoapContext constructor - solution is to exclude the account info from the context.
         */
    boolean excludeAccountDetails = AccountConstants.CHANGE_PASSWORD_REQUEST.equals(request.getQName());
    Element envelope = proto.soapEnvelope(request, zsc.toProxyContext(proto, excludeAccountDetails));
    SoapHttpTransport transport = null;
    try {
        transport = new SoapHttpTransport(mURL);
        transport.setTargetAcctId(zsc.getRequestedAccountId());
        if (mMaxAttempts > 0)
            transport.setRetryCount(mMaxAttempts);
        if (mTimeout >= 0)
            transport.setTimeout((int) Math.min(mTimeout, Integer.MAX_VALUE));
        transport.setResponseProtocol(zsc.getResponseProtocol());
        AuthToken authToken = AuthToken.getCsrfUnsecuredAuthToken(zsc.getAuthToken());
        if (authToken != null && !StringUtil.isNullOrEmpty(authToken.getProxyAuthToken())) {
            transport.setAuthToken(authToken.getProxyAuthToken());
        }
        if (ZimbraLog.soap.isDebugEnabled()) {
            ZimbraLog.soap.debug("Proxying request: proxy=%s targetAcctId=%s", toString(), zsc.getRequestedAccountId());
        }
        disableCsrfFlagInAuthToken(envelope, authToken, request.getQName());
        Element response = transport.invokeRaw(envelope);
        Element body = transport.extractBodyElement(response);
        return new Pair<Element, Element>(transport.getZimbraContext(), body);
    } catch (IOException e) {
        throw ServiceException.PROXY_ERROR(e, mURL);
    } finally {
        if (transport != null)
            transport.shutdown();
    }
}
Also used : Element(com.zimbra.common.soap.Element) SoapProtocol(com.zimbra.common.soap.SoapProtocol) AuthToken(com.zimbra.cs.account.AuthToken) SoapHttpTransport(com.zimbra.common.soap.SoapHttpTransport) IOException(java.io.IOException) Pair(com.zimbra.common.util.Pair)

Example 33 with SoapHttpTransport

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

the class TestFileUpload method testAdminUploadWithCsrfInFormField.

@Test
public void testAdminUploadWithCsrfInFormField() throws Exception {
    SoapHttpTransport transport = new SoapHttpTransport(TestUtil.getAdminSoapUrl());
    com.zimbra.soap.admin.message.AuthRequest req = new com.zimbra.soap.admin.message.AuthRequest(LC.zimbra_ldap_user.value(), LC.zimbra_ldap_password.value());
    req.setCsrfSupported(true);
    Element response = transport.invoke(JaxbUtil.jaxbToElement(req, SoapProtocol.SoapJS.getFactory()));
    com.zimbra.soap.admin.message.AuthResponse authResp = JaxbUtil.elementToJaxb(response);
    String authToken = authResp.getAuthToken();
    String csrfToken = authResp.getCsrfToken();
    int port = 7071;
    try {
        port = Provisioning.getInstance().getLocalServer().getIntAttr(Provisioning.A_zimbraAdminPort, 0);
    } catch (ServiceException e) {
        ZimbraLog.test.error("Unable to get admin SOAP port", e);
    }
    String Url = "https://localhost:" + port + ADMIN_UPLOAD_URL;
    PostMethod post = new PostMethod(Url);
    FilePart part = new FilePart(FILE_NAME, new ByteArrayPartSource(FILE_NAME, "some file content".getBytes()));
    Part csrfPart = new StringPart("csrfToken", csrfToken);
    String contentType = "application/x-msdownload";
    part.setContentType(contentType);
    HttpClient client = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
    HttpState state = new HttpState();
    state.addCookie(new org.apache.commons.httpclient.Cookie("localhost", ZimbraCookie.authTokenCookieName(true), authToken, "/", null, false));
    client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
    client.setState(state);
    post.setRequestEntity(new MultipartRequestEntity(new Part[] { part, csrfPart }, post.getParams()));
    int statusCode = HttpClientUtil.executeMethod(client, post);
    Assert.assertEquals("This request should succeed. Getting status code " + statusCode, HttpStatus.SC_OK, statusCode);
    String resp = post.getResponseBodyAsString();
    Assert.assertNotNull("Response should not be empty", resp);
    Assert.assertTrue("Incorrect HTML response", resp.contains(RESP_STR));
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) HeaderElement(org.apache.commons.httpclient.HeaderElement) Element(com.zimbra.common.soap.Element) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) HttpState(org.apache.commons.httpclient.HttpState) MultipartRequestEntity(org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) ByteArrayPartSource(org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource) ServiceException(com.zimbra.common.service.ServiceException) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) Part(org.apache.commons.httpclient.methods.multipart.Part) HttpClient(org.apache.commons.httpclient.HttpClient) SoapHttpTransport(com.zimbra.common.soap.SoapHttpTransport) Test(org.junit.Test)

Example 34 with SoapHttpTransport

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

the class TestDeployZimlet method testZipWithInvalidCharacter.

@Test
public void testZipWithInvalidCharacter() throws Exception {
    SoapHttpTransport transport = new SoapHttpTransport(TestUtil.getAdminSoapUrl());
    com.zimbra.soap.admin.message.AuthRequest authReq = new com.zimbra.soap.admin.message.AuthRequest(LC.zimbra_ldap_user.value(), LC.zimbra_ldap_password.value());
    authReq.setCsrfSupported(false);
    Element response = transport.invoke(JaxbUtil.jaxbToElement(authReq, SoapProtocol.SoapJS.getFactory()));
    com.zimbra.soap.admin.message.AuthResponse authResp = JaxbUtil.elementToJaxb(response);
    String authToken = authResp.getAuthToken();
    String aid = adminUpload(authToken, "jelmer.zip", "/opt/zimbra/unittest/zimlets/jelmer.zip");
    assertNotNull("Attachment ID should not be null", aid);
    AttachmentIdAttrib att = new AttachmentIdAttrib(aid);
    transport.setAdmin(true);
    transport.setAuthToken(authToken);
    DeployZimletRequest deployReq = new DeployZimletRequest(AdminConstants.A_DEPLOYLOCAL, false, true, att);
    Element req = JaxbUtil.jaxbToElement(deployReq);
    try {
        Element res = transport.invoke(req);
        JaxbUtil.elementToJaxb(res);
        fail("Should throw SoapFaultException");
    } catch (SoapFaultException e) {
    //expected
    }
}
Also used : Element(com.zimbra.common.soap.Element) SoapFaultException(com.zimbra.common.soap.SoapFaultException) DeployZimletRequest(com.zimbra.soap.admin.message.DeployZimletRequest) SoapHttpTransport(com.zimbra.common.soap.SoapHttpTransport) AttachmentIdAttrib(com.zimbra.soap.admin.type.AttachmentIdAttrib) Test(org.junit.Test)

Example 35 with SoapHttpTransport

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

the class TestAuth method authTokenCookieMaxAge.

@Test
public void authTokenCookieMaxAge() throws Exception {
    // 1 hour, has to match code in VerifyCookieExpireListener
    String authTokenLifetime = "1h";
    /*
         * test admin Auth
         */
    Account admin = provUtil.createGlobalAdmin(genAcctNameLocalPart("admin"), domain);
    // set the account's auth token lifetime to a short period
    admin.setAdminAuthTokenLifetime(authTokenLifetime);
    SoapHttpTransport transportAdmin = new SoapHttpTransport(TestUtil.getAdminSoapUrl());
    transportAdmin.setHttpDebugListener(new VerifyCookieExpireListener(ZimbraCookie.COOKIE_ZM_ADMIN_AUTH_TOKEN));
    com.zimbra.soap.admin.message.AuthRequest reqAdmin = new com.zimbra.soap.admin.message.AuthRequest(admin.getName(), "test123");
    reqAdmin.setPersistAuthTokenCookie(Boolean.TRUE);
    com.zimbra.soap.admin.message.AuthResponse respAdmin = invokeJaxb(transportAdmin, reqAdmin);
    /*
         * test account auth
         */
    Account acct = provUtil.createAccount(genAcctNameLocalPart("user"), domain);
    // set the account's auth token lifetime to a short period
    acct.setAuthTokenLifetime(authTokenLifetime);
    SoapHttpTransport transportAcct = new SoapHttpTransport(TestUtil.getSoapUrl());
    transportAcct.setHttpDebugListener(new VerifyCookieExpireListener(ZimbraCookie.COOKIE_ZM_AUTH_TOKEN));
    com.zimbra.soap.account.message.AuthRequest reqAcct = new com.zimbra.soap.account.message.AuthRequest(AccountSelector.fromName(acct.getName()), "test123");
    reqAcct.setPersistAuthTokenCookie(Boolean.TRUE);
    com.zimbra.soap.account.message.AuthResponse respAcct = invokeJaxb(transportAcct, reqAcct);
    provUtil.deleteAccount(admin);
    provUtil.deleteAccount(acct);
}
Also used : Account(com.zimbra.cs.account.Account) AuthRequest(com.zimbra.soap.account.message.AuthRequest) AuthResponse(com.zimbra.soap.account.message.AuthResponse) SoapHttpTransport(com.zimbra.common.soap.SoapHttpTransport) AuthRequest(com.zimbra.soap.account.message.AuthRequest) Test(org.junit.Test)

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