Search in sources :

Example 16 with BasicClientCookie

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());
}
Also used : BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie)

Example 17 with BasicClientCookie

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;
}
Also used : CookieStore(org.apache.http.client.CookieStore) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) Map(java.util.Map)

Example 18 with BasicClientCookie

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);
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie)

Example 19 with BasicClientCookie

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);
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder)

Example 20 with BasicClientCookie

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;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) RequestConfig(org.apache.http.client.config.RequestConfig) MultipartEntityBuilder(org.apache.http.entity.mime.MultipartEntityBuilder) SocketConfig(org.apache.http.config.SocketConfig) HttpEntity(org.apache.http.HttpEntity) HttpResponse(org.apache.http.HttpResponse) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) IOException(java.io.IOException) ZAuthToken(com.zimbra.common.auth.ZAuthToken) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) HttpClient(org.apache.http.client.HttpClient) HttpException(org.apache.http.HttpException) Map(java.util.Map)

Aggregations

BasicClientCookie (org.apache.http.impl.cookie.BasicClientCookie)88 BasicCookieStore (org.apache.http.impl.client.BasicCookieStore)36 Test (org.junit.Test)24 HttpResponse (org.apache.http.HttpResponse)19 Cookie (org.apache.http.cookie.Cookie)17 HttpClient (org.apache.http.client.HttpClient)15 HttpGet (org.apache.http.client.methods.HttpGet)15 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)15 Map (java.util.Map)14 RequestConfig (org.apache.http.client.config.RequestConfig)13 HttpPost (org.apache.http.client.methods.HttpPost)9 CookieAttributeHandler (org.apache.http.cookie.CookieAttributeHandler)9 CookieOrigin (org.apache.http.cookie.CookieOrigin)9 URL (java.net.URL)8 HttpEntity (org.apache.http.HttpEntity)8 CookieStore (org.apache.http.client.CookieStore)8 IOException (java.io.IOException)7 Date (java.util.Date)7 ClientCookie (org.apache.http.cookie.ClientCookie)7 HashMap (java.util.HashMap)6