use of org.apache.http.impl.cookie.BasicClientCookie in project ThinkAndroid by white-cat.
the class SerializableCookie method readObject.
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
String name = (String) in.readObject();
String value = (String) in.readObject();
clientCookie = new BasicClientCookie(name, value);
clientCookie.setComment((String) in.readObject());
clientCookie.setDomain((String) in.readObject());
clientCookie.setExpiryDate((Date) in.readObject());
clientCookie.setPath((String) in.readObject());
clientCookie.setVersion(in.readInt());
clientCookie.setSecure(in.readBoolean());
}
use of org.apache.http.impl.cookie.BasicClientCookie 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.cookie.BasicClientCookie 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.cookie.BasicClientCookie 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.cookie.BasicClientCookie 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