use of org.apache.commons.httpclient.Cookie 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);
}
use of org.apache.commons.httpclient.Cookie in project zm-mailbox by Zimbra.
the class SoapDebugListener method sendSoapMessage.
@Override
public void sendSoapMessage(PostMethod postMethod, Element envelope, HttpState httpState) {
if (level == Level.OFF) {
return;
}
System.out.println();
System.out.println("=== Request ===");
if (Level.needsHeader(level)) {
try {
URI uri = postMethod.getURI();
System.out.println(uri.toString());
} catch (URIException e) {
e.printStackTrace();
}
// headers
Header[] headers = postMethod.getRequestHeaders();
for (Header header : headers) {
// trim the ending crlf
System.out.println(header.toString().trim());
}
System.out.println();
//cookies
if (httpState != null) {
Cookie[] cookies = httpState.getCookies();
for (Cookie cookie : cookies) {
System.out.println("Cookie: " + cookie.toString());
}
}
System.out.println();
}
if (Level.needsBody(level)) {
System.out.println(envelope.prettyPrint());
}
}
use of org.apache.commons.httpclient.Cookie in project zm-mailbox by Zimbra.
the class SpamExtract method extract.
private static void extract(String authToken, Account account, Server server, String query, File outdir, boolean delete, boolean raw) throws ServiceException, HttpException, SoapFaultException, IOException {
String soapURL = getSoapURL(server, false);
URL restURL = getServerURL(server, false);
// CLI only, don't need conn mgr
HttpClient hc = new HttpClient();
HttpState state = new HttpState();
GetMethod gm = new GetMethod();
gm.setFollowRedirects(true);
Cookie authCookie = new Cookie(restURL.getHost(), ZimbraCookie.COOKIE_ZM_AUTH_TOKEN, authToken, "/", -1, false);
state.addCookie(authCookie);
hc.setState(state);
hc.getHostConfiguration().setHost(restURL.getHost(), restURL.getPort(), Protocol.getProtocol(restURL.getProtocol()));
gm.getParams().setSoTimeout(60000);
if (verbose) {
LOG.info("Mailbox requests to: " + restURL);
}
SoapHttpTransport transport = new SoapHttpTransport(soapURL);
transport.setRetryCount(1);
transport.setTimeout(0);
transport.setAuthToken(authToken);
int totalProcessed = 0;
boolean haveMore = true;
int offset = 0;
while (haveMore) {
Element searchReq = new Element.XMLElement(MailConstants.SEARCH_REQUEST);
searchReq.addElement(MailConstants.A_QUERY).setText(query);
searchReq.addAttribute(MailConstants.A_SEARCH_TYPES, MailItem.Type.MESSAGE.toString());
searchReq.addAttribute(MailConstants.A_QUERY_OFFSET, offset);
searchReq.addAttribute(MailConstants.A_LIMIT, BATCH_SIZE);
try {
if (LOG.isDebugEnabled()) {
LOG.debug(searchReq.prettyPrint());
}
Element searchResp = transport.invoke(searchReq, false, true, account.getId());
if (LOG.isDebugEnabled()) {
LOG.debug(searchResp.prettyPrint());
}
StringBuilder deleteList = new StringBuilder();
List<String> ids = new ArrayList<String>();
for (Iterator<Element> iter = searchResp.elementIterator(MailConstants.E_MSG); iter.hasNext(); ) {
offset++;
Element e = iter.next();
String mid = e.getAttribute(MailConstants.A_ID);
if (mid == null) {
LOG.warn("null message id SOAP response");
continue;
}
LOG.debug("adding id %s", mid);
ids.add(mid);
if (ids.size() >= BATCH_SIZE || !iter.hasNext()) {
StringBuilder path = new StringBuilder("/service/user/" + account.getName() + "/?fmt=tgz&list=" + StringUtils.join(ids, ","));
LOG.debug("sending request for path %s", path.toString());
List<String> extractedIds = extractMessages(hc, gm, path.toString(), outdir, raw);
if (ids.size() > extractedIds.size()) {
ids.removeAll(extractedIds);
LOG.warn("failed to extract %s", ids);
}
for (String id : extractedIds) {
deleteList.append(id).append(',');
}
ids.clear();
}
totalProcessed++;
}
haveMore = false;
String more = searchResp.getAttribute(MailConstants.A_QUERY_MORE);
if (more != null && more.length() > 0) {
try {
int m = Integer.parseInt(more);
if (m > 0) {
haveMore = true;
try {
Thread.sleep(SLEEP_TIME);
} catch (InterruptedException e) {
}
}
} catch (NumberFormatException nfe) {
LOG.warn("more flag from server not a number: " + more, nfe);
}
}
if (delete && deleteList.length() > 0) {
// -1 removes trailing comma
deleteList.deleteCharAt(deleteList.length() - 1);
Element msgActionReq = new Element.XMLElement(MailConstants.MSG_ACTION_REQUEST);
Element action = msgActionReq.addElement(MailConstants.E_ACTION);
action.addAttribute(MailConstants.A_ID, deleteList.toString());
action.addAttribute(MailConstants.A_OPERATION, ItemAction.OP_HARD_DELETE);
if (LOG.isDebugEnabled()) {
LOG.debug(msgActionReq.prettyPrint());
}
Element msgActionResp = transport.invoke(msgActionReq, false, true, account.getId());
if (LOG.isDebugEnabled()) {
LOG.debug(msgActionResp.prettyPrint());
}
//put offset back to 0 so we always get top N messages even after delete
offset = 0;
}
} finally {
gm.releaseConnection();
}
}
LOG.info("Total messages processed: " + totalProcessed);
}
use of org.apache.commons.httpclient.Cookie in project ecf by eclipse.
the class TestCookieCompatibilitySpec method testMatchedCookiesOrder.
public void testMatchedCookiesOrder() throws Exception {
CookieSpec cookiespec = new CookieSpecBase();
Cookie[] cookies = { new Cookie("host", "nomatch", "value", "/noway", null, false), new Cookie("host", "name2", "value", "/foobar/yada", null, false), new Cookie("host", "name3", "value", "/foobar", null, false), new Cookie("host", "name1", "value", "/foobar/yada/yada", null, false) };
Cookie[] matched = cookiespec.match("host", 80, "/foobar/yada/yada", false, cookies);
assertNotNull(matched);
assertEquals(3, matched.length);
assertEquals("name1", matched[0].getName());
assertEquals("name2", matched[1].getName());
assertEquals("name3", matched[2].getName());
}
use of org.apache.commons.httpclient.Cookie in project ecf by eclipse.
the class TestCookieCompatibilitySpec method testCookieMatch2.
public void testCookieMatch2() throws Exception {
CookieSpec cookiespec = new CookieSpecBase();
Cookie cookie = new Cookie(".whatever.com", "name", "value", "/", null, false);
assertTrue(cookiespec.match(".whatever.com", 80, "/", false, cookie));
}
Aggregations