/*
 * @(#)TestCookie.java
 *
 * Summary: Example use of ability of Applet to look at cookies with java.net.CookieHandler.
 *
 * Copyright: (c) 2009 Roedy Green, Canadian Mind Products, http://mindprod.com
 *
 * Licence: This software may be copied and used freely for any purpose but military.
 *          http://mindprod.com/contact/nonmil.html
 *
 * Requires: JDK 1.6+
 *
 * Created with: IntelliJ IDEA IDE.
 *
 * Version History:
 *  1.0 2009-01-01 - initial version
 */
package com.mindprod.example;

import java.applet.Applet;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.net.CookieStore;
import java.net.HttpCookie;

import static java.lang.System.out;

/**
 * Example use of ability of Applet to look at cookies with java.net.CookieHandler.
 * <p/>
 * Requires JDK 1.6+
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.0 2009-01-01 - initial version
 * @since 2009-01-01
 */
public final class TestCookie extends Applet
    {
    // -------------------------- PUBLIC INSTANCE  METHODS --------------------------
    /**
     * standard applet Init
     */
    public void init()
        {
        out.println( "init called" );

        // fetch cookies from the Browser's cache of cookies
        // Requires signed Applet permission

        final CookieManager cm = new CookieManager( null /* in ram store */, CookiePolicy.ACCEPT_ALL );
        CookieHandler.setDefault( cm );

        // from now all all our HTTP traffic to and from the Applet will collect/apply cookies as needed
        // automatically.

        final CookieStore cs = cm.getCookieStore();
        // show all cookies collected so far
        for ( HttpCookie hc : cs.getCookies() )
            {
            out.println( hc.toString() );
            }
        }
    }