/*
 * [TestCookie.java]
 *
 * Summary: Example use of ability of Applet to look at cookies with java.net.CookieHandler.
 *
 * Copyright: (c) 2009-2017 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.8+
 *
 * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/
 *
 * 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.*;

/**
 * Example use of ability of Applet to look at cookies with java.net.CookieHandler.
 * <p/>
 * Requires JDK 1.8+
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.0 2009-01-01 initial version
 * @since 2009-01-01
 */
public final class TestCookie extends Applet
    {
    /**
     * Called by the browser or Applet viewer to inform
     * this Applet that it has been loaded into the system.
     */
    @Override
    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() );
            }
        this.validate();
        this.setVisible( true );
        }
    }