use of org.apache.http.impl.client.BasicCookieStore in project zm-mailbox by Zimbra.
the class HttpClientUtil method newCookieStore.
/**
* Returns cookie store for Apache HTTP Components HttpClient 4.x
* @param authToken
* @param host
* @param isAdmin
* @return {@link CookieStore}
*/
public static CookieStore newCookieStore(ZAuthToken authToken, String host, boolean isAdmin) {
CookieStore cookieStore = new BasicCookieStore();
if (authToken != null) {
Map<String, String> cookieMap = authToken.cookieMap(isAdmin);
if (cookieMap != null) {
for (Map.Entry<String, String> ck : cookieMap.entrySet()) {
BasicClientCookie cookie = new BasicClientCookie(ck.getKey(), ck.getValue());
cookie.setDomain(host);
cookie.setPath("/");
cookie.setSecure(false);
cookieStore.addCookie(cookie);
}
}
}
return cookieStore;
}
use of org.apache.http.impl.client.BasicCookieStore in project zm-mailbox by Zimbra.
the class ExchangeFreeBusyProvider method formAuth.
private boolean formAuth(HttpClientBuilder clientBuilder, ServerInfo info) throws IOException, HttpException {
StringBuilder buf = new StringBuilder();
buf.append("destination=");
buf.append(URLEncoder.encode(info.url, "UTF-8"));
buf.append("&username=");
buf.append(info.authUsername);
buf.append("&password=");
buf.append(URLEncoder.encode(info.authPassword, "UTF-8"));
buf.append("&flags=0");
buf.append("&SubmitCreds=Log On");
buf.append("&trusted=0");
String url = info.url + LC.calendar_exchange_form_auth_url.value();
HttpPost method = new HttpPost(url);
ByteArrayEntity re = new ByteArrayEntity(buf.toString().getBytes(), ContentType.create("x-www-form-urlencoded"));
method.setEntity(re);
BasicCookieStore state = new BasicCookieStore();
clientBuilder.setDefaultCookieStore(state);
HttpResponse response = null;
try {
response = HttpClientUtil.executeMethod(clientBuilder.build(), method);
int status = response.getStatusLine().getStatusCode();
if (status >= 400) {
ZimbraLog.fb.error("form auth to Exchange returned an error: " + status);
return false;
}
} finally {
EntityUtils.consumeQuietly(response.getEntity());
}
return true;
}
use of org.apache.http.impl.client.BasicCookieStore in project zm-mailbox by Zimbra.
the class ZimbraAuthToken method encode.
@Override
public void encode(HttpClientBuilder clientBuilder, HttpRequestBase method, boolean isAdminReq, String cookieDomain) throws ServiceException {
String origAuthData = AuthTokenUtil.getOrigAuthData(this);
BasicCookieStore state = new BasicCookieStore();
BasicClientCookie cookie = new BasicClientCookie(ZimbraCookie.authTokenCookieName(isAdminReq), origAuthData);
cookie.setDomain(cookieDomain);
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);
}
use of org.apache.http.impl.client.BasicCookieStore in project zm-mailbox by Zimbra.
the class ZimbraAuthToken method encode.
@Override
public void encode(HttpClient client, HttpRequestBase method, boolean isAdminReq, String cookieDomain) throws ServiceException {
String origAuthData = AuthTokenUtil.getOrigAuthData(this);
BasicCookieStore state = new BasicCookieStore();
BasicClientCookie cookie = new BasicClientCookie(ZimbraCookie.authTokenCookieName(isAdminReq), origAuthData);
cookie.setDomain(cookieDomain);
cookie.setPath("/");
cookie.setSecure(false);
state.addCookie(cookie);
HttpClientBuilder clientBuilder = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
clientBuilder.setDefaultCookieStore(state);
RequestConfig reqConfig = RequestConfig.copy(ZimbraHttpConnectionManager.getInternalHttpConnMgr().getZimbraConnMgrParams().getReqConfig()).setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY).build();
clientBuilder.setDefaultRequestConfig(reqConfig);
}
use of org.apache.http.impl.client.BasicCookieStore in project zm-mailbox by Zimbra.
the class LmcSendMsgRequest method postAttachment.
/*
* Post the attachment represented by File f and return the attachment ID
*/
public String postAttachment(String uploadURL, LmcSession session, File f, // cookie domain e.g. ".example.zimbra.com"
String domain, int msTimeout) throws LmcSoapClientException, IOException, HttpException {
String aid = null;
// set the cookie.
if (session == null)
System.err.println(System.currentTimeMillis() + " " + Thread.currentThread() + " LmcSendMsgRequest.postAttachment session=null");
HttpClientBuilder clientBuilder = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
HttpPost post = new HttpPost(uploadURL);
ZAuthToken zat = session.getAuthToken();
Map<String, String> cookieMap = zat.cookieMap(false);
if (cookieMap != null) {
BasicCookieStore initialState = new BasicCookieStore();
for (Map.Entry<String, String> ck : cookieMap.entrySet()) {
BasicClientCookie cookie = new BasicClientCookie(ck.getKey(), ck.getValue());
cookie.setDomain(domain);
cookie.setPath("/");
cookie.setSecure(false);
cookie.setExpiryDate(null);
initialState.addCookie(cookie);
}
clientBuilder.setDefaultCookieStore(initialState);
RequestConfig reqConfig = RequestConfig.copy(ZimbraHttpConnectionManager.getInternalHttpConnMgr().getZimbraConnMgrParams().getReqConfig()).setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY).build();
clientBuilder.setDefaultRequestConfig(reqConfig);
}
SocketConfig config = SocketConfig.custom().setSoTimeout(msTimeout).build();
clientBuilder.setDefaultSocketConfig(config);
int statusCode = -1;
try {
String contentType = URLConnection.getFileNameMap().getContentTypeFor(f.getName());
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addBinaryBody("upfile", f, ContentType.create(contentType, "UTF-8"), f.getName());
HttpEntity entity = builder.build();
post.setEntity(entity);
HttpClient client = clientBuilder.build();
HttpResponse httpResponse = HttpClientUtil.executeMethod(client, post);
statusCode = httpResponse.getStatusLine().getStatusCode();
// parse the response
if (statusCode == 200) {
// paw through the returned HTML and get the attachment id
String response = EntityUtils.toString(httpResponse.getEntity());
// System.out.println("response is\n" + response);
int lastQuote = response.lastIndexOf("'");
int firstQuote = response.indexOf("','") + 3;
if (lastQuote == -1 || firstQuote == -1)
throw new LmcSoapClientException("Attachment post failed, unexpected response: " + response);
aid = response.substring(firstQuote, lastQuote);
} else {
throw new LmcSoapClientException("Attachment post failed, status=" + statusCode);
}
} catch (IOException | HttpException e) {
System.err.println("Attachment post failed");
e.printStackTrace();
throw e;
} finally {
post.releaseConnection();
}
return aid;
}
Aggregations