Search in sources :

Example 66 with BasicCookieStore

use of org.apache.http.impl.client.BasicCookieStore in project zm-mailbox by Zimbra.

the class TestDeployZimlet method adminUpload.

public String adminUpload(String authToken, String fileName, String filePath) throws Exception {
    HttpPost post = new HttpPost(ADMIN_UPLOAD_URL);
    String contentType = "application/x-msdownload";
    HttpClientBuilder clientBuilder = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
    BasicCookieStore state = new BasicCookieStore();
    BasicClientCookie cookie = new BasicClientCookie(ZimbraCookie.authTokenCookieName(true), authToken);
    cookie.setDomain(localServer.getServiceHostname());
    cookie.setPath("/");
    cookie.setSecure(false);
    state.addCookie(cookie);
    clientBuilder.setDefaultCookieStore(state);
    RequestConfig reqConfig = RequestConfig.copy(ZimbraHttpConnectionManager.getInternalHttpConnMgr().getZimbraConnMgrParams().getReqConfig()).setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY).build();
    clientBuilder.setDefaultRequestConfig(reqConfig);
    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    builder.addBinaryBody(fileName, new File(filePath), ContentType.create(contentType), fileName);
    HttpEntity httpEntity = builder.build();
    post.setEntity(httpEntity);
    HttpClient client = clientBuilder.build();
    HttpResponse response = HttpClientUtil.executeMethod(client, post);
    int statusCode = response.getStatusLine().getStatusCode();
    assertEquals("This request should succeed. Getting status code " + statusCode, HttpStatus.SC_OK, statusCode);
    String resp = EntityUtils.toString(response.getEntity());
    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 : HttpPost(org.apache.http.client.methods.HttpPost) RequestConfig(org.apache.http.client.config.RequestConfig) MultipartEntityBuilder(org.apache.http.entity.mime.MultipartEntityBuilder) HttpEntity(org.apache.http.HttpEntity) HttpResponse(org.apache.http.HttpResponse) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) HttpClient(org.apache.http.client.HttpClient) File(java.io.File)

Example 67 with BasicCookieStore

use of org.apache.http.impl.client.BasicCookieStore 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 68 with BasicCookieStore

use of org.apache.http.impl.client.BasicCookieStore 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 69 with BasicCookieStore

use of org.apache.http.impl.client.BasicCookieStore in project zm-mailbox by Zimbra.

the class TestCollectConfigServletsAccess method testConfigDelegatedAdmin.

/**
 * Verify that global admin can access servlet at /service/collectconfig/
 * @throws Exception
 */
/*  @Test
    public void testConfigGlobalAdmin() throws Exception {
        ZAuthToken at = TestUtil.getAdminSoapTransport().getAuthToken();
        URI servletURI = new URI(getConfigServletUrl());
        BasicCookieStore initialState = HttpClientUtil.newHttpState(at, servletURI.getHost(), true);
        HttpClient restClient = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
        restClient.setState(initialState);
        restClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
        HttpGet get = new HttpGet(servletURI.toString());
        int statusCode = HttpClientUtil.executeMethod(restClient, get);
        if(statusCode == HttpStatus.SC_INTERNAL_SERVER_ERROR) {
            fail("collectconfig servlet is failing. Likely Zimbra SSH access is not properly configured. " + get.getResponseHeader("X-Zimbra-Fault-Message").getValue());
        } else {
            assertEquals("This request should succeed. Getting status code " + statusCode, HttpStatus.SC_OK,statusCode);
        }
    }*/
/**
 * Verify that delegated admin canNOT access servlet at /service/collectconfig/
 * @throws Exception
 */
@Test
public void testConfigDelegatedAdmin() throws Exception {
    ZAuthToken at = TestUtil.getAdminSoapTransport(TEST_ADMIN_NAME, PASSWORD).getAuthToken();
    URI servletURI = new URI(getConfigServletUrl());
    BasicCookieStore initialState = HttpClientUtil.newHttpState(at, servletURI.getHost(), true);
    HttpClientBuilder restClientBuilder = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
    restClientBuilder.setDefaultCookieStore(initialState);
    RequestConfig reqConfig = RequestConfig.copy(ZimbraHttpConnectionManager.getInternalHttpConnMgr().getZimbraConnMgrParams().getReqConfig()).setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY).build();
    restClientBuilder.setDefaultRequestConfig(reqConfig);
    HttpGet get = new HttpGet(servletURI.toString());
    HttpClient restClient = restClientBuilder.build();
    HttpResponse response = HttpClientUtil.executeMethod(restClient, get);
    int statusCode = response.getStatusLine().getStatusCode();
    assertEquals("This request should NOT succeed. Getting status code " + statusCode, HttpStatus.SC_UNAUTHORIZED, statusCode);
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) HttpGet(org.apache.http.client.methods.HttpGet) HttpClient(org.apache.http.client.HttpClient) HttpResponse(org.apache.http.HttpResponse) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) ZAuthToken(com.zimbra.common.auth.ZAuthToken) URI(java.net.URI) Test(org.junit.Test)

Example 70 with BasicCookieStore

use of org.apache.http.impl.client.BasicCookieStore in project zm-mailbox by Zimbra.

the class TestCollectConfigServletsAccess method testLDAPConfigDelegatedAdmin.

/**
 * Verify that global admin can access servlet at /service/collectldapconfig/
 * @throws Exception
 */
/*  @Test
    public void testLDAPConfigGlobalAdmin() throws Exception {
        ZAuthToken at = TestUtil.getAdminSoapTransport().getAuthToken();
        URI servletURI = new URI(getLDAPConfigServletUrl());
        BasicCookieStore initialState = HttpClientUtil.newHttpState(at, servletURI.getHost(), true);
        HttpClient restClient = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
        restClient.setState(initialState);
        restClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
        HttpGet get = new HttpGet(servletURI.toString());
        int statusCode = HttpClientUtil.executeMethod(restClient, get);
        if(statusCode == HttpStatus.SC_INTERNAL_SERVER_ERROR) {
            fail("collectldapconfig servlet is failing. Likely Zimbra SSH access is not properly configured. " + get.getResponseHeader("X-Zimbra-Fault-Message").getValue());
        } else {
            assertEquals("This request should succeed. Getting status code " + statusCode, HttpStatus.SC_OK,statusCode);
        }
    }*/
/**
 * Verify that delegated admin canNOT access servlet at /service/collectldapconfig/
 * @throws Exception
 */
@Test
public void testLDAPConfigDelegatedAdmin() throws Exception {
    ZAuthToken at = TestUtil.getAdminSoapTransport(TEST_ADMIN_NAME, PASSWORD).getAuthToken();
    URI servletURI = new URI(getLDAPConfigServletUrl());
    BasicCookieStore initialState = HttpClientUtil.newHttpState(at, servletURI.getHost(), true);
    HttpClientBuilder restClientBuilder = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
    restClientBuilder.setDefaultCookieStore(initialState);
    RequestConfig reqConfig = RequestConfig.copy(ZimbraHttpConnectionManager.getInternalHttpConnMgr().getZimbraConnMgrParams().getReqConfig()).setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY).build();
    restClientBuilder.setDefaultRequestConfig(reqConfig);
    HttpGet get = new HttpGet(servletURI.toString());
    HttpClient restClient = restClientBuilder.build();
    HttpResponse response = HttpClientUtil.executeMethod(restClient, get);
    int statusCode = response.getStatusLine().getStatusCode();
    assertEquals("This request should NOT succeed. Getting status code " + statusCode, HttpStatus.SC_UNAUTHORIZED, statusCode);
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) HttpGet(org.apache.http.client.methods.HttpGet) HttpClient(org.apache.http.client.HttpClient) HttpResponse(org.apache.http.HttpResponse) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) ZAuthToken(com.zimbra.common.auth.ZAuthToken) URI(java.net.URI) Test(org.junit.Test)

Aggregations

BasicCookieStore (org.apache.http.impl.client.BasicCookieStore)138 HttpResponse (org.apache.http.HttpResponse)54 HttpGet (org.apache.http.client.methods.HttpGet)51 Test (org.junit.Test)44 BasicClientCookie (org.apache.http.impl.cookie.BasicClientCookie)40 RequestConfig (org.apache.http.client.config.RequestConfig)36 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)36 CookieStore (org.apache.http.client.CookieStore)26 Header (org.apache.http.Header)25 HttpClient (org.apache.http.client.HttpClient)25 IOException (java.io.IOException)23 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)22 Cookie (org.apache.http.cookie.Cookie)19 HttpPost (org.apache.http.client.methods.HttpPost)15 CredentialsProvider (org.apache.http.client.CredentialsProvider)14 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)14 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)14 BasicHttpContext (org.apache.http.protocol.BasicHttpContext)13 URI (java.net.URI)12 HttpEntity (org.apache.http.HttpEntity)12