Search in sources :

Example 6 with HttpState

use of org.apache.commons.httpclient.HttpState 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
     */
@Test
public void testForceEndSession() throws ServiceException, IOException {
    //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);
    //create evesdropper's connection
    HttpClient eve = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
    Cookie[] cookies = alice.getState().getCookies();
    HttpState state = new HttpState();
    for (int i = 0; i < cookies.length; i++) {
        Cookie cookie = cookies[i];
        state.addCookie(new Cookie(uri.getHost(), cookie.getName(), cookie.getValue(), "/", null, false));
    }
    eve.setState(state);
    Account a = TestUtil.getAccount(USER_NAME);
    a.setForceClearCookies(false);
    EndSessionRequest esr = new EndSessionRequest();
    esr.setLogOff(true);
    mbox.invokeJaxb(esr);
    GetMethod get = new GetMethod(uri.toString());
    int statusCode = HttpClientUtil.executeMethod(eve, get);
    Assert.assertEquals("This request should not succeed. Getting status code " + statusCode, HttpStatus.SC_UNAUTHORIZED, statusCode);
}
Also used : Cookie(org.apache.commons.httpclient.Cookie) Account(com.zimbra.cs.account.Account) ZMailbox(com.zimbra.client.ZMailbox) HttpClient(org.apache.commons.httpclient.HttpClient) EndSessionRequest(com.zimbra.soap.account.message.EndSessionRequest) HttpState(org.apache.commons.httpclient.HttpState) GetMethod(org.apache.commons.httpclient.methods.GetMethod) URI(java.net.URI) Test(org.junit.Test)

Example 7 with HttpState

use of org.apache.commons.httpclient.HttpState 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
     */
@Test
public void testAutoEndSession() throws ServiceException, IOException {
    //establish legitimate connection
    TestUtil.setAccountAttr(USER_NAME, Provisioning.A_zimbraForceClearCookies, "TRUE");
    ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
    URI uri = mbox.getRestURI("Inbox?fmt=rss");
    HttpClient alice = mbox.getHttpClient(uri);
    //create evesdropper's connection
    HttpClient eve = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
    Cookie[] cookies = alice.getState().getCookies();
    HttpState state = new HttpState();
    for (int i = 0; i < cookies.length; i++) {
        Cookie cookie = cookies[i];
        state.addCookie(new Cookie(uri.getHost(), cookie.getName(), cookie.getValue(), "/", null, false));
    }
    eve.setState(state);
    Account a = TestUtil.getAccount(USER_NAME);
    a.setForceClearCookies(true);
    EndSessionRequest esr = new EndSessionRequest();
    mbox.invokeJaxb(esr);
    GetMethod get = new GetMethod(uri.toString());
    int statusCode = HttpClientUtil.executeMethod(eve, get);
    Assert.assertEquals("This request should not succeed. Getting status code " + statusCode, HttpStatus.SC_UNAUTHORIZED, statusCode);
}
Also used : Cookie(org.apache.commons.httpclient.Cookie) Account(com.zimbra.cs.account.Account) ZMailbox(com.zimbra.client.ZMailbox) HttpClient(org.apache.commons.httpclient.HttpClient) EndSessionRequest(com.zimbra.soap.account.message.EndSessionRequest) HttpState(org.apache.commons.httpclient.HttpState) GetMethod(org.apache.commons.httpclient.methods.GetMethod) URI(java.net.URI) Test(org.junit.Test)

Example 8 with HttpState

use of org.apache.commons.httpclient.HttpState in project zm-mailbox by Zimbra.

the class TestDeployZimlet method adminUpload.

public String adminUpload(String authToken, String fileName, String filePath) throws Exception {
    PostMethod post = new PostMethod(ADMIN_UPLOAD_URL);
    FilePart part = new FilePart(fileName, new FilePartSource(new File(filePath)));
    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(localServer.getServiceHostname(), ZimbraCookie.authTokenCookieName(true), authToken, "/", null, false));
    client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
    client.setState(state);
    post.setRequestEntity(new MultipartRequestEntity(new Part[] { part }, post.getParams()));
    int statusCode = HttpClientUtil.executeMethod(client, post);
    assertEquals("This request should succeed. Getting status code " + statusCode, HttpStatus.SC_OK, statusCode);
    String resp = post.getResponseBodyAsString();
    assertNotNull("Response should not be empty", resp);
    ZimbraLog.test.debug("Upload response " + resp);
    String[] responseParts = resp.split(",", 3);
    String aid = null;
    if (responseParts.length == 3) {
        aid = responseParts[2].trim();
        if (aid.startsWith("'") || aid.startsWith("\"")) {
            aid = aid.substring(1);
        }
        if (aid.endsWith("'") || aid.endsWith("\"")) {
            aid = aid.substring(0, aid.length() - 1);
        }
    }
    return aid;
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) HttpState(org.apache.commons.httpclient.HttpState) MultipartRequestEntity(org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) FilePartSource(org.apache.commons.httpclient.methods.multipart.FilePartSource) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) Part(org.apache.commons.httpclient.methods.multipart.Part) HttpClient(org.apache.commons.httpclient.HttpClient) File(java.io.File)

Example 9 with HttpState

use of org.apache.commons.httpclient.HttpState in project zm-mailbox by Zimbra.

the class TestCookieReuse method testValidSessionCookieReuse.

/**
     * Verify that we can RE-use the cookie for REST session if the session is valid
     */
@Test
public void testValidSessionCookieReuse() throws ServiceException, IOException {
    //establish legitimate connection
    ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
    URI uri = mbox.getRestURI("Inbox?fmt=rss");
    HttpClient alice = mbox.getHttpClient(uri);
    //create evesdropper's connection
    HttpClient eve = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
    Cookie[] cookies = alice.getState().getCookies();
    HttpState state = new HttpState();
    for (int i = 0; i < cookies.length; i++) {
        Cookie cookie = cookies[i];
        state.addCookie(new Cookie(uri.getHost(), cookie.getName(), cookie.getValue(), "/", null, false));
    }
    eve.setState(state);
    GetMethod get = new GetMethod(uri.toString());
    int statusCode = HttpClientUtil.executeMethod(eve, get);
    Assert.assertEquals("This request should succeed. Getting status code " + statusCode, HttpStatus.SC_OK, statusCode);
}
Also used : Cookie(org.apache.commons.httpclient.Cookie) ZMailbox(com.zimbra.client.ZMailbox) HttpClient(org.apache.commons.httpclient.HttpClient) HttpState(org.apache.commons.httpclient.HttpState) GetMethod(org.apache.commons.httpclient.methods.GetMethod) URI(java.net.URI) Test(org.junit.Test)

Example 10 with HttpState

use of org.apache.commons.httpclient.HttpState in project zm-mailbox by Zimbra.

the class TestProvIDN method testBasicAuth.

@Test
public void testBasicAuth() throws Exception {
    Names.IDNName domainName = new Names.IDNName(makeTestDomainName("basicAuthTest."));
    Domain domain = createDomain(domainName.uName(), domainName.uName());
    Names.IDNName acctName = new Names.IDNName("acct", domainName.uName());
    Account acct = (Account) createTest(EntryType.ACCOUNT, NameType.UNAME, acctName);
    HttpState initialState = new HttpState();
    /*
        Cookie authCookie = new Cookie(restURL.getURL().getHost(), "ZM_AUTH_TOKEN", mAuthToken, "/", null, false);
        Cookie sessionCookie = new Cookie(restURL.getURL().getHost(), "JSESSIONID", mSessionId, "/zimbra", null, false);
        initialState.addCookie(authCookie);
        initialState.addCookie(sessionCookie);
        */
    String guestName = acct.getUnicodeName();
    String guestPassword = "test123";
    Credentials loginCredentials = new UsernamePasswordCredentials(guestName, guestPassword);
    initialState.setCredentials(AuthScope.ANY, loginCredentials);
    HttpClient client = new HttpClient();
    client.setState(initialState);
    String url = UserServlet.getRestUrl(acct) + "/Calendar";
    System.out.println("REST URL: " + url);
    HttpMethod method = new GetMethod(url);
    HttpMethodParams methodParams = method.getParams();
    methodParams.setCredentialCharset("UTF-8");
    try {
        int respCode = HttpClientUtil.executeMethod(client, method);
        if (respCode != HttpStatus.SC_OK) {
            System.out.println("failed, respCode=" + respCode);
        } else {
            boolean chunked = false;
            boolean textContent = false;
        /*
                 System.out.println("Headers:");
                 System.out.println("--------");
                 for (Header header : method.getRequestHeaders()) {
                     System.out.print("    " + header.toString());
                 }
                 System.out.println();
                 
                 System.out.println("Body:");
                 System.out.println("-----");
                 String respBody = method.getResponseBodyAsString();
                 System.out.println(respBody);
                 */
        }
    } finally {
        // Release the connection.
        method.releaseConnection();
    }
}
Also used : Account(com.zimbra.cs.account.Account) HttpState(org.apache.commons.httpclient.HttpState) HttpMethodParams(org.apache.commons.httpclient.params.HttpMethodParams) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) Names(com.zimbra.qa.unittest.prov.Names) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) Domain(com.zimbra.cs.account.Domain) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) Credentials(org.apache.commons.httpclient.Credentials) HttpMethod(org.apache.commons.httpclient.HttpMethod)

Aggregations

HttpState (org.apache.commons.httpclient.HttpState)33 HttpClient (org.apache.commons.httpclient.HttpClient)25 GetMethod (org.apache.commons.httpclient.methods.GetMethod)18 Test (org.junit.Test)13 ServiceException (com.zimbra.common.service.ServiceException)9 ZAuthToken (com.zimbra.common.auth.ZAuthToken)8 URI (java.net.URI)8 Cookie (org.apache.commons.httpclient.Cookie)8 PostMethod (org.apache.commons.httpclient.methods.PostMethod)8 ZMailbox (com.zimbra.client.ZMailbox)7 AuthToken (com.zimbra.cs.account.AuthToken)7 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)6 Account (com.zimbra.cs.account.Account)5 IOException (java.io.IOException)5 Map (java.util.Map)5 HttpMethod (org.apache.commons.httpclient.HttpMethod)5 Element (com.zimbra.common.soap.Element)4 SoapHttpTransport (com.zimbra.common.soap.SoapHttpTransport)4 ZimbraAuthToken (com.zimbra.cs.account.ZimbraAuthToken)4 ArrayList (java.util.ArrayList)4