Package com.almilli.htmlparser

Source Code of com.almilli.htmlparser.CookieFixConnectionManager

package com.almilli.htmlparser;

import java.util.Hashtable;
import java.util.Vector;
import org.htmlparser.http.ConnectionManager;
import org.htmlparser.http.Cookie;

public class CookieFixConnectionManager extends ConnectionManager {


    /**
     * Adds a cookie to the cookie jar.
     * @param cookie The cookie to add.
     * @param domain The domain to use in case the cookie has no domain attribute.
     */
    public void setCookie (Cookie cookie, String domain)
    {
        String path;
        Vector cookies;
        Cookie probe;

        if (null != cookie.getDomain ())
            domain = cookie.getDomain ();
        path = cookie.getPath ();
        if (null == mCookieJar)
            mCookieJar = new Hashtable (); // turn on cookie processing
        cookies = (Vector)mCookieJar.get (domain);
        if (null != cookies)
        {
          boolean found = false;
            for (int j = 0; j < cookies.size (); j++)
            {
                probe = (Cookie)cookies.elementAt (j);
                if (probe.getName ().equalsIgnoreCase (cookie.getName ()))
                {
                    // we keep paths sorted most specific to least
                    if (probe.getPath ().equals (path))
                    {
                        cookies.setElementAt (cookie, j); // replace
                        found = true;
                        break;
                    }
                    else if (path.startsWith (probe.getPath ()))
                    {
                        cookies.insertElementAt (cookie, j);
                        found = true;
                        break;
                    }
                }
            }
            if (!found) {
                cookies.addElement(cookie);
            }
        }
        else
        {   // new cookie list needed
            cookies = new Vector ();
            cookies.addElement (cookie);
            mCookieJar.put (domain, cookies);
        }

    }
}
TOP

Related Classes of com.almilli.htmlparser.CookieFixConnectionManager

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.