Search in sources :

Example 41 with ZMailbox

use of com.zimbra.client.ZMailbox in project zm-mailbox by Zimbra.

the class TestCookieReuse method setUp.

@Before
public void setUp() throws Exception {
    currentSupportedAuthVersion = Provisioning.getInstance().getLocalServer().getLowestSupportedAuthVersion();
    String prefix = NAME_PREFIX + "-" + testInfo.getMethodName().toLowerCase() + "-";
    USER_NAME = prefix + "user1";
    UNAUTHORIZED_USER = AccountTestUtil.getAddress(prefix + "unauthorized");
    cleanUp();
    TestUtil.createAccount(USER_NAME);
    ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
    // Add a test message, to make sure the account isn't empty
    TestUtil.addMessage(mbox, NAME_PREFIX);
}
Also used : ZMailbox(com.zimbra.client.ZMailbox) Before(org.junit.Before)

Example 42 with ZMailbox

use of com.zimbra.client.ZMailbox in project zm-mailbox by Zimbra.

the class TestCookieReuse method testInvalidSearchRequest.

/**
 * Verify that we canNOT RE-use the cookie taken from a legitimate HTTP session for a SOAP request after
 * ending the original session
 */
@Test
public void testInvalidSearchRequest() throws ServiceException, IOException, HttpException {
    // establish legitimate connection
    TestUtil.setAccountAttr(USER_NAME, Provisioning.A_zimbraForceClearCookies, "FALSE");
    ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
    URI uri = mbox.getRestURI("Inbox?fmt=rss");
    mbox.getHttpClient(uri);
    ZAuthToken authT = mbox.getAuthToken();
    // create evesdropper's SOAP client
    SoapHttpTransport transport = new HttpCookieSoapTransport(TestUtil.getSoapUrl());
    transport.setAuthToken(authT);
    // check that search returns something
    SearchRequest searchReq = new SearchRequest();
    searchReq.setSearchTypes(MailItem.Type.MESSAGE.toString());
    searchReq.setQuery("in:inbox");
    Element req = JaxbUtil.jaxbToElement(searchReq, SoapProtocol.SoapJS.getFactory());
    Element res = transport.invoke(req);
    SearchResponse searchResp = JaxbUtil.elementToJaxb(res);
    List<SearchHit> searchHits = searchResp.getSearchHits();
    Assert.assertFalse("this search request should return some conversations", searchHits.isEmpty());
    // explicitely end cookie session
    Account a = TestUtil.getAccount(USER_NAME);
    a.setForceClearCookies(false);
    EndSessionRequest esr = new EndSessionRequest();
    esr.setLogOff(true);
    mbox.invokeJaxb(esr);
    // check that search returns nothing
    transport = new HttpCookieSoapTransport(TestUtil.getSoapUrl());
    transport.setAuthToken(authT);
    searchReq = new SearchRequest();
    searchReq.setSearchTypes(MailItem.Type.MESSAGE.toString());
    searchReq.setQuery("in:inbox");
    try {
        req = JaxbUtil.jaxbToElement(searchReq, SoapProtocol.SoapJS.getFactory());
        res = transport.invoke(req);
        searchResp = JaxbUtil.elementToJaxb(res);
        searchHits = searchResp.getSearchHits();
        Assert.assertTrue("this search request should fail", searchHits.isEmpty());
    } catch (SoapFaultException ex) {
        Assert.assertEquals("Should be getting 'auth required' exception", ServiceException.AUTH_EXPIRED, ex.getCode());
    }
}
Also used : SearchRequest(com.zimbra.soap.mail.message.SearchRequest) Account(com.zimbra.cs.account.Account) SearchHit(com.zimbra.soap.type.SearchHit) Element(com.zimbra.common.soap.Element) XMLElement(com.zimbra.common.soap.Element.XMLElement) JSONElement(com.zimbra.common.soap.Element.JSONElement) URI(java.net.URI) ZAuthToken(com.zimbra.common.auth.ZAuthToken) SoapFaultException(com.zimbra.common.soap.SoapFaultException) SearchResponse(com.zimbra.soap.mail.message.SearchResponse) ZMailbox(com.zimbra.client.ZMailbox) EndSessionRequest(com.zimbra.soap.account.message.EndSessionRequest) SoapHttpTransport(com.zimbra.common.soap.SoapHttpTransport) Test(org.junit.Test)

Example 43 with ZMailbox

use of com.zimbra.client.ZMailbox in project zm-mailbox by Zimbra.

the class TestCookieReuse method testAutoEndSession.

/**
 * Verify that we canNOT RE-use the cookie for REST session if the session is valid
 * @throws HttpException
 */
@Test
public void testAutoEndSession() throws ServiceException, IOException, HttpException {
    // establish legitimate connection
    TestUtil.setAccountAttr(USER_NAME, Provisioning.A_zimbraForceClearCookies, "TRUE");
    ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
    URI uri = mbox.getRestURI("Inbox?fmt=rss");
    HttpClientContext context = HttpClientContext.create();
    HttpClient alice = mbox.getHttpClient(uri);
    // create evesdropper's connection
    HttpClientBuilder eve = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
    List<Cookie> cookies = context.getCookieStore().getCookies();
    BasicCookieStore cookieStore = new BasicCookieStore();
    for (Cookie cookie : cookies) {
        BasicClientCookie basicCookie = new BasicClientCookie(cookie.getName(), cookie.getValue());
        basicCookie.setDomain(uri.getHost());
        basicCookie.setPath("/");
        basicCookie.setSecure(false);
        cookieStore.addCookie(cookie);
    }
    eve.setDefaultCookieStore(cookieStore);
    Account a = TestUtil.getAccount(USER_NAME);
    a.setForceClearCookies(true);
    EndSessionRequest esr = new EndSessionRequest();
    mbox.invokeJaxb(esr);
    HttpGet get = new HttpGet(uri.toString());
    HttpResponse response = HttpClientUtil.executeMethod(eve.build(), get, context);
    int statusCode = response.getStatusLine().getStatusCode();
    Assert.assertEquals("This request should not succeed. Getting status code " + statusCode, HttpStatus.SC_UNAUTHORIZED, statusCode);
}
Also used : Cookie(org.apache.http.cookie.Cookie) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) Account(com.zimbra.cs.account.Account) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) URI(java.net.URI) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) ZMailbox(com.zimbra.client.ZMailbox) HttpClient(org.apache.http.client.HttpClient) EndSessionRequest(com.zimbra.soap.account.message.EndSessionRequest) Test(org.junit.Test)

Example 44 with ZMailbox

use of com.zimbra.client.ZMailbox in project zm-mailbox by Zimbra.

the class TestCookieReuse method testForceEndSession.

/**
 * Verify that we canNOT RE-use the cookie taken from a legitimate HTTP session for a REST request
 * after ending the original session
 * @throws HttpException
 */
@Test
public void testForceEndSession() throws ServiceException, IOException, HttpException {
    // establish legitimate connection
    TestUtil.setAccountAttr(USER_NAME, Provisioning.A_zimbraForceClearCookies, "FALSE");
    ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
    URI uri = mbox.getRestURI("Inbox?fmt=rss");
    HttpClient alice = mbox.getHttpClient(uri);
    HttpClientContext context = HttpClientContext.create();
    // create evesdropper's connection
    HttpClientBuilder eve = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
    List<Cookie> cookies = context.getCookieStore().getCookies();
    BasicCookieStore cookieStore = new BasicCookieStore();
    for (Cookie cookie : cookies) {
        BasicClientCookie basicCookie = new BasicClientCookie(cookie.getName(), cookie.getValue());
        basicCookie.setDomain(uri.getHost());
        basicCookie.setPath("/");
        basicCookie.setSecure(false);
        cookieStore.addCookie(cookie);
    }
    eve.setDefaultCookieStore(cookieStore);
    Account a = TestUtil.getAccount(USER_NAME);
    a.setForceClearCookies(false);
    EndSessionRequest esr = new EndSessionRequest();
    esr.setLogOff(true);
    mbox.invokeJaxb(esr);
    HttpGet get = new HttpGet(uri.toString());
    HttpResponse response = HttpClientUtil.executeMethod(eve.build(), get);
    int statusCode = response.getStatusLine().getStatusCode();
    Assert.assertEquals("This request should not succeed. Getting status code " + statusCode, HttpStatus.SC_UNAUTHORIZED, statusCode);
}
Also used : Cookie(org.apache.http.cookie.Cookie) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) Account(com.zimbra.cs.account.Account) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) URI(java.net.URI) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) ZMailbox(com.zimbra.client.ZMailbox) HttpClient(org.apache.http.client.HttpClient) EndSessionRequest(com.zimbra.soap.account.message.EndSessionRequest) Test(org.junit.Test)

Example 45 with ZMailbox

use of com.zimbra.client.ZMailbox in project zm-mailbox by Zimbra.

the class TestContactGroup method csvOldZCOClient.

@Test
public void csvOldZCOClient() throws Exception {
    Account acct = Provisioning.getInstance().get(AccountBy.name, TestUtil.getAddress("user1"));
    // String relativePath = "/?fmt=zip&meta=0&zlv=4&list=259";
    String relativePath = "/?fmt=zip&meta=1&zlv=4&list=257";
    final int BUFFER = 2048;
    ZMailbox mbox = TestUtil.getZMailbox(acct.getName());
    ZipInputStream zin = new ZipInputStream(mbox.getRESTResource(relativePath));
    ZipEntry ze = null;
    while ((ze = zin.getNextEntry()) != null) {
        System.out.println("Unzipping " + ze.getName());
        int count;
        byte[] data = new byte[BUFFER];
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        while ((count = zin.read(data, 0, BUFFER)) != -1) {
            os.write(data, 0, count);
        }
        os.flush();
        ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
        byte[] bytes = ByteUtil.getContent(is, 1024);
        String dd = new String(bytes, "UTF-8");
        System.out.println(dd);
        is.close();
        os.close();
    }
    zin.close();
}
Also used : LdapAccount(com.zimbra.cs.account.ldap.entry.LdapAccount) Account(com.zimbra.cs.account.Account) ZipInputStream(java.util.zip.ZipInputStream) ZMailbox(com.zimbra.client.ZMailbox) ByteArrayInputStream(java.io.ByteArrayInputStream) ZipEntry(java.util.zip.ZipEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Aggregations

ZMailbox (com.zimbra.client.ZMailbox)383 Test (org.junit.Test)288 ZFolder (com.zimbra.client.ZFolder)90 ZMessage (com.zimbra.client.ZMessage)82 Mailbox (com.zimbra.cs.mailbox.Mailbox)61 Account (com.zimbra.cs.account.Account)60 ServiceException (com.zimbra.common.service.ServiceException)55 ArrayList (java.util.ArrayList)38 IOException (java.io.IOException)35 MessageData (com.zimbra.cs.mailclient.imap.MessageData)28 ZMountpoint (com.zimbra.client.ZMountpoint)26 Message (com.zimbra.cs.mailbox.Message)24 HashMap (java.util.HashMap)23 ZOutgoingMessage (com.zimbra.client.ZMailbox.ZOutgoingMessage)21 ZTag (com.zimbra.client.ZTag)21 SoapFaultException (com.zimbra.common.soap.SoapFaultException)21 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)21 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)20 HttpClient (org.apache.http.client.HttpClient)19 ZSearchParams (com.zimbra.client.ZSearchParams)18