Example usage for org.apache.http.impl.cookie BasicClientCookie setValue

List of usage examples for org.apache.http.impl.cookie BasicClientCookie setValue

Introduction

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

Prototype

public void setValue(final String value) 

Source Link

Document

Sets the value

Usage

From source file:com.volley.demo.ExampleCookies.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cookie);

    // we hold a reference to the HttpClient in order to be able to get/set cookies
    mHttpClient = new DefaultHttpClient();

    mQueue = Volley.newRequestQueue(ExampleCookies.this, new HttpClientStack(mHttpClient));

    mTvCookie = (TextView) findViewById(R.id.tv_cookie);
    setTvCookieText("n/a");

    Button btnRequest = (Button) findViewById(R.id.btn_execute_request);
    btnRequest.setOnClickListener(new OnClickListener() {
        @Override/*www .j a v a2  s.c  o m*/
        public void onClick(View v) {
            mQueue.add(createRequest());
        }
    });

    mBtnSetCookie = (Button) findViewById(R.id.btn_set_cookie);
    mBtnSetCookie.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            CookieStore cs = mHttpClient.getCookieStore();
            BasicClientCookie c = (BasicClientCookie) getCookie(cs, "my_cookie");
            c.setValue("41");
            cs.addCookie(c);

            mQueue.add(createRequest());
        }
    });
}

From source file:com.github.irib_examples.Act_Cookies.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.act__cookie);

    // we hold a reference to the HttpClient in order to be able to get/set cookies
    mHttpClient = new DefaultHttpClient();

    mQueue = Volley.newRequestQueue(Act_Cookies.this, new HttpClientStack(mHttpClient));

    mTvCookie = (TextView) findViewById(R.id.tv_cookie);
    setTvCookieText("n/a");

    Button btnRequest = (Button) findViewById(R.id.btn_execute_request);
    btnRequest.setOnClickListener(new OnClickListener() {
        @Override/*from   w ww.ja  v a 2s .c  om*/
        public void onClick(View v) {
            mQueue.add(createRequest());
        }
    });

    mBtnSetCookie = (Button) findViewById(R.id.btn_set_cookie);
    mBtnSetCookie.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            CookieStore cs = mHttpClient.getCookieStore();
            BasicClientCookie c = (BasicClientCookie) getCookie(cs, "my_cookie");
            c.setValue("41");
            cs.addCookie(c);

            mQueue.add(createRequest());
        }
    });
}

From source file:com.lillicoder.newsblurry.net.SerializableCookie.java

private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
    // Read all values from write operation in order.
    String comment = (String) in.readObject();
    String domain = (String) in.readObject();
    Date expiryDate = (Date) in.readObject();
    String name = (String) in.readObject();
    String path = (String) in.readObject();
    String value = (String) in.readObject();
    int version = in.readInt();
    boolean isSecure = in.readBoolean();

    // Create a new basic cookie and set this wrapper's cookie instance.
    BasicClientCookie cookie = new BasicClientCookie(name, value);
    cookie.setComment(comment);// w w  w .j  a  v  a2 s .  c om
    cookie.setDomain(domain);
    cookie.setExpiryDate(expiryDate);
    cookie.setPath(path);
    cookie.setValue(value);
    cookie.setVersion(version);
    cookie.setSecure(isSecure);

    this.setCookie(cookie);
}

From source file:com.partnet.automation.http.ApacheHttpAdapter.java

private List<Cookie> convertCookieAdapterToApacheCookie(List<CookieAdapter> cookies) {
    List<Cookie> apacheCookie = new ArrayList<>();

    for (CookieAdapter adaptCookie : cookies) {
        BasicClientCookie basicCookie = new BasicClientCookie(adaptCookie.getName(), adaptCookie.getValue());
        basicCookie.setDomain(adaptCookie.getDomain());
        basicCookie.setExpiryDate(adaptCookie.getExpiryDate());
        basicCookie.setPath(adaptCookie.getPath());
        basicCookie.setValue(adaptCookie.getValue());
        basicCookie.setVersion(adaptCookie.getVersion());
        apacheCookie.add(basicCookie);//from www  . j  av  a 2  s  .c  o  m
    }
    return apacheCookie;
}

From source file:com.machinepublishers.jbrowserdriver.OptionsServer.java

private org.apache.http.cookie.Cookie convert(Cookie in) {
    BasicClientCookie out = new BasicClientCookie(in.getName(), in.getValue());
    String domainStr = null;//from   ww w.j ava 2s . c om
    if (StringUtils.isEmpty(in.getDomain())) {
        String urlStr = context.item().engine.get().getLocation();
        try {
            URL url = new URL(urlStr);
            domainStr = url.getHost();
        } catch (MalformedURLException e) {
            Matcher matcher = domain.matcher(urlStr);
            if (matcher.matches()) {
                domainStr = matcher.group(1);
            }
        }
    }
    out.setDomain(domainStr == null ? in.getDomain() : domainStr);
    if (in.getExpiry() != null) {
        out.setExpiryDate(in.getExpiry());
    }
    out.setPath(in.getPath());
    out.setSecure(in.isSecure());
    out.setValue(in.getValue());
    out.setVersion(1);
    return out;
}

From source file:com.feigdev.webcom.PersistentCookieStore.java

/**
 * parseCookie() parses the cookieString which is a comma-separated list of
 * one or more cookies in the format of "NAME=VALUE; expires=DATE;
 * path=PATH; domain=DOMAIN_NAME; secure httponly" to a list of Cookies.
 * Here is a sample: IGDND=1, IGPC=ET=UB8TSNwtDmQ:AF=0; expires=Sun,
 * 17-Jan-2038 19:14:07 GMT; path=/ig; domain=.google.com, =,
 * PREF=ID=408909b1b304593d:TM=1156459854:LM=1156459854:S=V-vCAU6Sh-gobCfO;
 * expires=Sun, 17-Jan-2038 19:14:07 GMT; path=/; domain=.google.com which
 * contains 3 cookies IGDND, IGPC, PREF and an empty cookie
 * @param host The default host//from  w w  w .j  a va  2 s.  c om
 * @param path The default path
 * @param cookieString The string coming from "Set-Cookie:"
 * @return A list of Cookies
 */
private ArrayList<BasicClientCookie> parseCookie(String host, String path, String cookieString) {
    ArrayList<BasicClientCookie> ret = new ArrayList<BasicClientCookie>();

    int index = 0;
    int length = cookieString.length();
    while (true) {
        BasicClientCookie cookie = null;

        // done
        if (index < 0 || index >= length) {
            break;
        }

        // skip white space
        if (cookieString.charAt(index) == WHITE_SPACE) {
            index++;
            continue;
        }

        /*
         * get NAME=VALUE; pair. detecting the end of a pair is tricky, it
         * can be the end of a string, like "foo=bluh", it can be semicolon
         * like "foo=bluh;path=/"; or it can be enclosed by \", like
         * "foo=\"bluh bluh\";path=/"
         *
         * Note: in the case of "foo=bluh, bar=bluh;path=/", we interpret
         * it as one cookie instead of two cookies.
         */
        int semicolonIndex = cookieString.indexOf(SEMICOLON, index);
        int equalIndex = cookieString.indexOf(EQUAL, index);

        // Cookies like "testcookie; path=/;" are valid and used
        // (lovefilm.se).
        // Look for 2 cases:
        // 1. "foo" or "foo;" where equalIndex is -1
        // 2. "foo; path=..." where the first semicolon is before an equal
        //    and a semicolon exists.
        if ((semicolonIndex != -1 && (semicolonIndex < equalIndex)) || equalIndex == -1) {
            // Fix up the index in case we have a string like "testcookie"
            if (semicolonIndex == -1) {
                semicolonIndex = length;
            }
            cookie = new BasicClientCookie(cookieString.substring(index, semicolonIndex), null);
            cookie.setDomain(host);
            cookie.setPath(path);
        } else {
            cookie = new BasicClientCookie(cookieString.substring(index, equalIndex), null);
            cookie.setDomain(host);
            cookie.setPath(path);
            // Make sure we do not throw an exception if the cookie is like
            // "foo="
            if ((equalIndex < length - 1) && (cookieString.charAt(equalIndex + 1) == QUOTATION)) {
                index = cookieString.indexOf(QUOTATION, equalIndex + 2);
                if (index == -1) {
                    // bad format, force return
                    break;
                }
            }
            // Get the semicolon index again in case it was contained within
            // the quotations.
            semicolonIndex = cookieString.indexOf(SEMICOLON, index);
            if (semicolonIndex == -1) {
                semicolonIndex = length;
            }
            if (semicolonIndex - equalIndex > MAX_COOKIE_LENGTH) {
                // cookie is too big, trim it
                cookie.setValue(cookieString.substring(equalIndex + 1, equalIndex + 1 + MAX_COOKIE_LENGTH));
            } else if (equalIndex + 1 == semicolonIndex || semicolonIndex < equalIndex) {
                // this is an unusual case like "foo=;" or "foo="
                cookie.setValue("");
            } else {
                cookie.setValue(cookieString.substring(equalIndex + 1, semicolonIndex));
            }
        }
        // get attributes
        index = semicolonIndex;
        while (true) {
            // done
            if (index < 0 || index >= length) {
                break;
            }

            // skip white space and semicolon
            if (cookieString.charAt(index) == WHITE_SPACE || cookieString.charAt(index) == SEMICOLON) {
                index++;
                continue;
            }

            // comma means next cookie
            if (cookieString.charAt(index) == COMMA) {
                index++;
                break;
            }

            // "secure" is a known attribute doesn't use "=";
            // while sites like live.com uses "secure="
            if (length - index >= SECURE_LENGTH
                    && cookieString.substring(index, index + SECURE_LENGTH).equalsIgnoreCase(SECURE)) {
                index += SECURE_LENGTH;
                cookie.setSecure(true);
                if (index == length)
                    break;
                if (cookieString.charAt(index) == EQUAL)
                    index++;
                continue;
            }

            // "httponly" is a known attribute doesn't use "=";
            // while sites like live.com uses "httponly="
            if (length - index >= HTTP_ONLY_LENGTH
                    && cookieString.substring(index, index + HTTP_ONLY_LENGTH).equalsIgnoreCase(HTTP_ONLY)) {
                index += HTTP_ONLY_LENGTH;
                if (index == length)
                    break;
                if (cookieString.charAt(index) == EQUAL)
                    index++;
                // FIXME: currently only parse the attribute
                continue;
            }
            equalIndex = cookieString.indexOf(EQUAL, index);
            if (equalIndex > 0) {
                String name = cookieString.substring(index, equalIndex).toLowerCase();
                if (name.equals(EXPIRES)) {
                    int comaIndex = cookieString.indexOf(COMMA, equalIndex);

                    // skip ',' in (Wdy, DD-Mon-YYYY HH:MM:SS GMT) or
                    // (Weekday, DD-Mon-YY HH:MM:SS GMT) if it applies.
                    // "Wednesday" is the longest Weekday which has length 9
                    if ((comaIndex != -1) && (comaIndex - equalIndex <= 10)) {
                        index = comaIndex + 1;
                    }
                }
                semicolonIndex = cookieString.indexOf(SEMICOLON, index);
                int commaIndex = cookieString.indexOf(COMMA, index);
                if (semicolonIndex == -1 && commaIndex == -1) {
                    index = length;
                } else if (semicolonIndex == -1) {
                    index = commaIndex;
                } else if (commaIndex == -1) {
                    index = semicolonIndex;
                } else {
                    index = Math.min(semicolonIndex, commaIndex);
                }
                String value = cookieString.substring(equalIndex + 1, index);

                // Strip quotes if they exist
                if (value.length() > 2 && value.charAt(0) == QUOTATION) {
                    int endQuote = value.indexOf(QUOTATION, 1);
                    if (endQuote > 0) {
                        value = value.substring(1, endQuote);
                    }
                }
                if (name.equals(EXPIRES)) {
                    try {
                        cookie.setExpiryDate(new Date(AndroidHttpClient.parseDate(value)));
                    } catch (IllegalArgumentException ex) {
                        Log.e(LOGTAG, "illegal format for expires: " + value);
                    }
                } else if (name.equals(MAX_AGE)) {
                    try {
                        cookie.setExpiryDate(
                                new Date(System.currentTimeMillis() + 1000 * Long.parseLong(value)));
                    } catch (NumberFormatException ex) {
                        Log.e(LOGTAG, "illegal format for max-age: " + value);
                    }
                } else if (name.equals(PATH)) {
                    // only allow non-empty path value
                    if (value.length() > 0) {
                        cookie.setPath(value);
                    }
                } else if (name.equals(DOMAIN)) {
                    int lastPeriod = value.lastIndexOf(PERIOD);
                    if (lastPeriod == 0) {
                        // disallow cookies set for TLDs like [.com]
                        cookie.setDomain(null);
                        continue;
                    }
                    try {
                        Integer.parseInt(value.substring(lastPeriod + 1));
                        // no wildcard for ip address match
                        if (!value.equals(host)) {
                            // no cross-site cookie
                            cookie.setDomain(null);
                        }
                        continue;
                    } catch (NumberFormatException ex) {
                        // ignore the exception, value is a host name
                    }
                    value = value.toLowerCase();
                    if (value.charAt(0) != PERIOD) {
                        // pre-pended dot to make it as a domain cookie
                        value = PERIOD + value;
                        lastPeriod++;
                    }
                    if (host.endsWith(value.substring(1))) {
                        int len = value.length();
                        int hostLen = host.length();
                        if (hostLen > (len - 1) && host.charAt(hostLen - len) != PERIOD) {
                            // make sure the bar.com doesn't match .ar.com
                            cookie.setDomain(null);
                            continue;
                        }
                        // disallow cookies set on ccTLDs like [.co.uk]
                        if ((len == lastPeriod + 3) && (len >= 6 && len <= 8)) {
                            String s = value.substring(1, lastPeriod);
                            if (Arrays.binarySearch(BAD_COUNTRY_2LDS, s) >= 0) {
                                cookie.setDomain(null);
                                continue;
                            }
                        }
                        cookie.setDomain(value);
                    } else {
                        // no cross-site or more specific sub-domain cookie
                        cookie.setDomain(null);
                    }
                }
            } else {
                // bad format, force return
                index = length;
            }
        }
        if (cookie != null && cookie.getDomain() != null) {
            ret.add(cookie);
        }
    }
    return ret;
}