Example usage for org.apache.http.impl.cookie BasicClientCookie2 BasicClientCookie2

List of usage examples for org.apache.http.impl.cookie BasicClientCookie2 BasicClientCookie2

Introduction

In this page you can find the example usage for org.apache.http.impl.cookie BasicClientCookie2 BasicClientCookie2.

Prototype

public BasicClientCookie2(final String name, final String value) 

Source Link

Document

Default Constructor taking a name and a value.

Usage

From source file:com.gistlabs.mechanize.cookie.Cookie.java

public Cookie(String name, String value) {
    this.httpCookie = new BasicClientCookie2(name, value);
}

From source file:org.tellervo.desktop.wsi.util.WSCookieWrapper.java

public Cookie toApacheCookie() {
    BasicClientCookie2 cookie = new BasicClientCookie2(name, value);

    cookie.setComment(cookieComment);/*from   w  ww  .ja va2  s .  c om*/
    cookie.setDomain(cookieDomain);
    cookie.setExpiryDate(cookieExpiryDate);
    cookie.setPath(cookiePath);
    cookie.setSecure(isSecure);
    cookie.setVersion(cookieVersion);
    cookie.setPorts(ports);

    // copy over attributes
    /*
    for(Entry<String, String> entry : attribs.entrySet()) {
       cookie.setAttribute(entry.getKey(), entry.getValue());
    }*/

    return cookie;
}

From source file:fi.iki.murgo.irssinotifier.Server.java

private boolean authenticate(int retryCount) throws IOException {
    if (usingDevServer) {
        BasicClientCookie2 cookie = new BasicClientCookie2("dev_appserver_login",
                "irssinotifier@gmail.com:False:118887942201532232498");
        cookie.setDomain("10.0.2.2");
        cookie.setPath("/");
        http_client.getCookieStore().addCookie(cookie);

        return true;
    }//from   w  w w.  j a  v a 2 s.c o  m

    String token = preferences.getAuthToken();
    try {
        if (token == null) {
            String accountName = preferences.getAccountName();
            if (accountName == null) {
                return false;
            }

            token = generateToken(accountName);
            preferences.setAuthToken(token);
        }

        boolean success = doAuthenticate(token);
        if (success) {
            Log.v(TAG, "Succesfully logged in.");
            return true;
        }
    } catch (IOException e) {
        throw e;
    } catch (Exception e) {
        Log.e(TAG, "Unable to send settings: " + e.toString());
        e.printStackTrace();
        preferences.setAccountName(null); // reset because authentication or unforeseen error
        return false;
    }

    Log.w(TAG, "Login failed, retrying... Retry count " + (retryCount + 1));
    http_client = new DefaultHttpClient();
    preferences.setAuthToken(null);

    if (retryCount >= maxRetryCount) {
        preferences.setAccountName(null); // reset because it's not accepted by the server
        return false;
    }

    return authenticate(retryCount + 1);
}

From source file:org.geometerplus.android.fbreader.network.BearerAuthenticator.java

static boolean onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
    final BearerAuthenticator ba = ourAuthenticators.get(activity);
    boolean processed = true;
    try {/*from ww  w  .j a  v  a  2  s  . c om*/
        switch (requestCode) {
        default:
            processed = false;
            break;
        case NetworkLibraryActivity.REQUEST_ACCOUNT_PICKER:
            if (resultCode == Activity.RESULT_OK && data != null) {
                ba.myAccount = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
            }
            break;
        case NetworkLibraryActivity.REQUEST_AUTHORISATION:
            if (resultCode == Activity.RESULT_OK) {
                ba.myAuthorizationConfirmed = true;
            }
            break;
        case NetworkLibraryActivity.REQUEST_WEB_AUTHORISATION_SCREEN:
            if (resultCode == Activity.RESULT_OK && data != null) {
                final CookieStore store = ZLNetworkManager.Instance().cookieStore();
                final Map<String, String> cookies = (Map<String, String>) data
                        .getSerializableExtra(NetworkLibraryActivity.COOKIES_KEY);
                if (cookies != null) {
                    for (Map.Entry<String, String> entry : cookies.entrySet()) {
                        final BasicClientCookie2 c = new BasicClientCookie2(entry.getKey(), entry.getValue());
                        c.setDomain(data.getData().getHost());
                        c.setPath("/");
                        final Calendar expire = Calendar.getInstance();
                        expire.add(Calendar.YEAR, 1);
                        c.setExpiryDate(expire.getTime());
                        c.setSecure(true);
                        c.setDiscard(false);
                        store.addCookie(c);
                    }
                }
            }
            break;
        }
    } finally {
        if (processed) {
            synchronized (ba) {
                ba.notifyAll();
            }
        }
        return processed;
    }
}

From source file:com.android.fastlibrary.volley.VolleyHelper.java

/**
 * cookie/*from w  w w. j  av  a 2 s.  c  o  m*/
 * <p/>
 * cookie?
 * setCookie();
 */
public void setCookie() {
    CookieStore cs = httpClient.getCookieStore();
    cs.addCookie(new BasicClientCookie2("cookie", "spooky"));
}

From source file:org.geometerplus.android.fbreader.network.ActivityNetworkContext.java

public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
    boolean processed = true;
    try {/*  w  w w .j a  v a2  s  . c om*/
        switch (requestCode) {
        default:
            processed = false;
            break;
        case NetworkLibraryActivity.REQUEST_ACCOUNT_PICKER:
            if (resultCode == Activity.RESULT_OK && data != null) {
                myAccount = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
            }
            break;
        case NetworkLibraryActivity.REQUEST_AUTHORISATION:
            if (resultCode == Activity.RESULT_OK) {
                myAuthorizationConfirmed = true;
            }
            break;
        case NetworkLibraryActivity.REQUEST_WEB_AUTHORISATION_SCREEN:
            if (resultCode == Activity.RESULT_OK && data != null) {
                final CookieStore store = cookieStore();
                final Map<String, String> cookies = (Map<String, String>) data
                        .getSerializableExtra(NetworkLibraryActivity.COOKIES_KEY);
                if (cookies != null) {
                    for (Map.Entry<String, String> entry : cookies.entrySet()) {
                        final BasicClientCookie2 c = new BasicClientCookie2(entry.getKey(), entry.getValue());
                        c.setDomain(data.getData().getHost());
                        c.setPath("/");
                        final Calendar expire = Calendar.getInstance();
                        expire.add(Calendar.YEAR, 1);
                        c.setExpiryDate(expire.getTime());
                        c.setSecure(true);
                        c.setDiscard(false);
                        store.addCookie(c);
                    }
                }
            }
            break;
        }
    } finally {
        if (processed) {
            synchronized (this) {
                notifyAll();
            }
        }
        return processed;
    }
}

From source file:com.google.corp.productivity.specialprojects.android.comm.SerializableCookieStore.java

public SerializableCookieStore(SharedPreferences preferences) {
    String cookiesString = preferences.getString(UserPreferences.COOKIE_PREFERENCE_KEY, null);
    if (cookiesString != null) {
        try {/*from w w  w . j  a  v  a  2  s. c  o  m*/
            JSONArray cookies = new JSONArray(cookiesString);
            int n = cookies.length();
            for (int i = 0; i < n; i++) {
                JSONObject json = cookies.optJSONObject(i);
                if (!JSONObject.NULL.equals(json)) {
                    BasicClientCookie2 cookie = new BasicClientCookie2(json.optString(ATTR_NAME),
                            json.optString(ATTR_VALUE));
                    if (json.has(ATTR_COMMENT)) {
                        cookie.setComment(json.optString(ATTR_COMMENT));
                    }
                    if (json.has(ATTR_COMMENT_URL)) {
                        cookie.setCommentURL(json.optString(ATTR_COMMENT_URL));
                    }
                    if (json.has(ATTR_DOMAIN)) {
                        cookie.setDomain(json.optString(ATTR_DOMAIN));
                    }
                    if (json.has(ATTR_EXPIRY_DATE)) {
                        cookie.setExpiryDate(new Date(json.optLong(ATTR_EXPIRY_DATE)));
                    }
                    if (json.has(ATTR_PATH)) {
                        cookie.setPath(json.optString(ATTR_PATH));
                    }
                    if (json.has(ATTR_SECURE)) {
                        cookie.setSecure(json.optBoolean(ATTR_SECURE));
                    }
                    if (json.has(ATTR_VERSION)) {
                        cookie.setVersion(json.optInt(ATTR_VERSION));
                    }
                    if (json.has(ATTR_PORTS)) {
                        JSONArray arr = json.optJSONArray(ATTR_PORTS);
                        if (arr != null) {
                            int m = arr.length();
                            int[] ports = new int[m];
                            for (int j = 0; j < m; j++) {
                                ports[j] = arr.optInt(j);
                            }
                            cookie.setPorts(ports);
                        }
                    }
                    super.addCookie(cookie);
                }
            }
        } catch (JSONException x) {
            // Invalid string format, no cookies can be read.
        }
    }
    dirty = false;
}

From source file:org.geometerplus.android.fbreader.network.auth.WebAuthorisationScreen.java

private void storeCookies(String host, Map<String, String> cookies) {
    final ZLNetworkManager.CookieStore store = myNetworkContext.cookieStore();

    for (Map.Entry<String, String> entry : cookies.entrySet()) {
        final BasicClientCookie2 c = new BasicClientCookie2(entry.getKey(), entry.getValue());
        c.setDomain(host);/*from   w ww .  j  a va  2  s  .c  o  m*/
        c.setPath("/");
        final Calendar expire = Calendar.getInstance();
        expire.add(Calendar.YEAR, 1);
        c.setExpiryDate(expire.getTime());
        c.setSecure(true);
        c.setDiscard(false);
        store.addCookie(c);
    }
}

From source file:com.cloudant.client.org.lightcouch.CouchDbClientBase.java

private void setCookie(CouchDbProperties props) {
    BasicClientCookie2 cookie = new BasicClientCookie2("AuthSession", props.getAuthCookie());
    cookie.setDomain(props.getHost());//w w w  .ja va  2s  . c  o m
    cookies.addCookie(cookie);

}

From source file:com.cloudant.client.org.lightcouch.CouchDbClientBase.java

private void getCookie(final CouchDbProperties props) {
    if (props.getUsername() == null || props.getPassword() == null) {
        return;//from ww  w . j  ava  2 s  .c  o  m
    }
    URI uri = buildUri(baseURI).path("_session").build();
    String body = "name=" + props.getUsername() + "&password=" + props.getPassword();

    HttpResponse response = null;
    try {
        response = executeRequest(CouchDbUtil.createPost(uri, body, "application/x-www-form-urlencoded"));
        for (Header h : response.getHeaders("set-cookie")) {
            if (h.getName().equalsIgnoreCase("AuthSession")) {
                cookies.addCookie(new BasicClientCookie2("AuthSession", h.getValue()));
                break;
            }
        }
    } finally {
        close(response);
    }

}