use of org.apache.http.impl.cookie.BasicClientCookie in project zm-mailbox by Zimbra.
the class ZimbraServlet method proxyServletRequest.
public static void proxyServletRequest(HttpServletRequest req, HttpServletResponse resp, HttpRequestBase method, BasicCookieStore state) throws IOException, ServiceException, HttpException {
// create an HTTP client with the same cookies
javax.servlet.http.Cookie[] cookies = req.getCookies();
String hostname = method.getURI().getHost();
boolean hasZMAuth = hasZimbraAuthCookie(state);
boolean hasJwtSalt = hasJWTSaltCookie(state);
if (cookies != null) {
for (int i = 0; i < cookies.length; i++) {
if ((cookies[i].getName().equals(ZimbraCookie.COOKIE_ZM_AUTH_TOKEN) && hasZMAuth) || (hasJwtSalt && cookies[i].getName().equals(ZimbraCookie.COOKIE_ZM_JWT)))
continue;
BasicClientCookie cookie = new BasicClientCookie(cookies[i].getName(), cookies[i].getValue());
cookie.setDomain(hostname);
cookie.setPath("/");
cookie.setSecure(false);
state.addCookie(cookie);
}
}
HttpClientBuilder clientBuilder = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
if (state != null)
clientBuilder.setDefaultCookieStore(state);
int hopcount = 0;
for (Enumeration<?> enm = req.getHeaderNames(); enm.hasMoreElements(); ) {
String hname = (String) enm.nextElement(), hlc = hname.toLowerCase();
if (hlc.equals("x-zimbra-hopcount"))
try {
hopcount = Math.max(Integer.parseInt(req.getHeader(hname)), 0);
} catch (NumberFormatException e) {
}
else if (hlc.startsWith("x-") || hlc.startsWith("content-") || hlc.equals("authorization"))
method.addHeader(hname, req.getHeader(hname));
}
if (hopcount >= MAX_PROXY_HOPCOUNT)
throw ServiceException.TOO_MANY_HOPS(HttpUtil.getFullRequestURL(req));
method.addHeader("X-Zimbra-Hopcount", Integer.toString(hopcount + 1));
if (method.getFirstHeader("X-Zimbra-Orig-Url") == null)
method.addHeader("X-Zimbra-Orig-Url", req.getRequestURL().toString());
String ua = req.getHeader("User-Agent");
if (ua != null)
method.addHeader("User-Agent", ua);
// dispatch the request and copy over the results
int statusCode = -1;
HttpClient client = clientBuilder.build();
HttpResponse httpResp = null;
for (int retryCount = 3; statusCode == -1 && retryCount > 0; retryCount--) {
httpResp = HttpClientUtil.executeMethod(client, method);
statusCode = httpResp.getStatusLine().getStatusCode();
}
if (statusCode == -1) {
resp.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, "retry limit reached");
return;
} else if (statusCode >= 300) {
resp.sendError(statusCode, httpResp.getStatusLine().getReasonPhrase());
return;
}
Header[] headers = httpResp.getAllHeaders();
for (int i = 0; i < headers.length; i++) {
String hname = headers[i].getName(), hlc = hname.toLowerCase();
if (hlc.startsWith("x-") || hlc.startsWith("content-") || hlc.startsWith("www-"))
resp.addHeader(hname, headers[i].getValue());
}
InputStream responseStream = httpResp.getEntity().getContent();
if (responseStream == null || resp.getOutputStream() == null)
return;
ByteUtil.copy(httpResp.getEntity().getContent(), false, resp.getOutputStream(), false);
}
use of org.apache.http.impl.cookie.BasicClientCookie 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
HttpClientBuilder hc = HttpClientBuilder.create();
BasicCookieStore cookieStore = new BasicCookieStore();
HttpGet gm = new HttpGet();
hc.setRedirectStrategy(new DefaultRedirectStrategy());
BasicClientCookie cookie = new BasicClientCookie(ZimbraCookie.COOKIE_ZM_AUTH_TOKEN, authToken);
cookie.setDomain(restURL.getHost());
cookie.setPath("/");
cookie.setSecure(false);
cookie.setExpiryDate(null);
cookieStore.addCookie(cookie);
hc.setDefaultCookieStore(cookieStore);
HttpHost target = new HttpHost(restURL.getHost(), restURL.getPort(), null);
SocketConfig config = SocketConfig.custom().setSoTimeout(60000).build();
hc.setDefaultSocketConfig(config);
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(restURL.toString() + "/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.http.impl.cookie.BasicClientCookie in project zm-mailbox by Zimbra.
the class TestFileUpload method testAdminUploadWithCsrfInHeader.
@Test
public void testAdminUploadWithCsrfInHeader() throws Exception {
SoapHttpTransport transport = new SoapHttpTransport(TestUtil.getAdminSoapUrl());
com.zimbra.soap.admin.message.AuthRequest req = new com.zimbra.soap.admin.message.AuthRequest(LC.zimbra_ldap_user.value(), LC.zimbra_ldap_password.value());
req.setCsrfSupported(true);
Element response = transport.invoke(JaxbUtil.jaxbToElement(req, SoapProtocol.SoapJS.getFactory()));
com.zimbra.soap.admin.message.AuthResponse authResp = JaxbUtil.elementToJaxb(response);
String authToken = authResp.getAuthToken();
String csrfToken = authResp.getCsrfToken();
int port = 7071;
try {
port = Provisioning.getInstance().getLocalServer().getIntAttr(Provisioning.A_zimbraAdminPort, 0);
} catch (ServiceException e) {
ZimbraLog.test.error("Unable to get admin SOAP port", e);
}
String Url = "https://localhost:" + port + ADMIN_UPLOAD_URL;
HttpPost post = new HttpPost(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("localhost");
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(FILE_NAME, "some file content".getBytes(), ContentType.create(contentType), FILE_NAME);
HttpEntity httpEntity = builder.build();
post.setEntity(httpEntity);
HttpClient client = clientBuilder.build();
post.addHeader(Constants.CSRF_TOKEN, csrfToken);
HttpResponse httpResponse = HttpClientUtil.executeMethod(client, post);
int statusCode = httpResponse.getStatusLine().getStatusCode();
Assert.assertEquals("This request should succeed. Getting status code " + statusCode, HttpStatus.SC_OK, statusCode);
String resp = EntityUtils.toString(httpResponse.getEntity());
Assert.assertNotNull("Response should not be empty", resp);
Assert.assertTrue("Incorrect HTML response", resp.contains(RESP_STR));
}
use of org.apache.http.impl.cookie.BasicClientCookie in project zm-mailbox by Zimbra.
the class TestFileUpload method testMissingCsrfAdminUpload.
@Test
public void testMissingCsrfAdminUpload() throws Exception {
SoapHttpTransport transport = new SoapHttpTransport(TestUtil.getAdminSoapUrl());
com.zimbra.soap.admin.message.AuthRequest req = new com.zimbra.soap.admin.message.AuthRequest(LC.zimbra_ldap_user.value(), LC.zimbra_ldap_password.value());
req.setCsrfSupported(true);
Element response = transport.invoke(JaxbUtil.jaxbToElement(req, SoapProtocol.SoapJS.getFactory()));
com.zimbra.soap.admin.message.AuthResponse authResp = JaxbUtil.elementToJaxb(response);
String authToken = authResp.getAuthToken();
int port = 7071;
try {
port = Provisioning.getInstance().getLocalServer().getIntAttr(Provisioning.A_zimbraAdminPort, 0);
} catch (ServiceException e) {
ZimbraLog.test.error("Unable to get admin SOAP port", e);
}
String Url = "https://localhost:" + port + ADMIN_UPLOAD_URL;
HttpPost post = new HttpPost(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("localhost");
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(FILE_NAME, "some file content".getBytes(), ContentType.create(contentType), FILE_NAME);
HttpEntity httpEntity = builder.build();
post.setEntity(httpEntity);
HttpClient client = clientBuilder.build();
HttpResponse httpResponse = HttpClientUtil.executeMethod(client, post);
int statusCode = httpResponse.getStatusLine().getStatusCode();
Assert.assertEquals("This request should succeed. Getting status code " + statusCode, HttpStatus.SC_OK, statusCode);
String resp = EntityUtils.toString(httpResponse.getEntity());
Assert.assertNotNull("Response should not be empty", resp);
Assert.assertTrue("Incorrect HTML response", resp.contains(RESP_STR));
}
use of org.apache.http.impl.cookie.BasicClientCookie 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;
}
Aggregations