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;
}
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);
}
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);
}
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);
}
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);
}
Aggregations