package edu.cmu.cs.diamond.opendiamond;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.Map.Entry;

/**
 * Representation of a set of Diamond "cookies" for authenticating to Diamond
 * servers.
 * 
 */
public class CookieMap {

    private final Map<String, Cookie> cookieMap;

    /**
     * Creates a CookieMap with settings taken from the current environment.
     * 
     * @return a new CookieMap
     * @throws IOException
     *             if the source of the default map cannot be found or used
     */
    public static CookieMap createDefaultCookieMap() throws IOException {
        // get newscope file
        File home = new File(System.getProperty("user.home"));
        File diamondDir = new File(home, ".diamond");
        File newscope = new File(diamondDir, "NEWSCOPE");

        InputStream in = new FileInputStream(newscope);
        String megacookie = new String(Util.readFully(in));
        in.close();

        return new CookieMap(megacookie);
    }

    /**
     * Constructs a new CookieMap from a string representation.
     * 
     * @param megacookie
     *            the string to construct the CookieMap from
     * @throws IOException
     *             if the string representation is malformed
     */
    public CookieMap(String megacookie) throws IOException {
        Map<String, Cookie> cookieMap = new HashMap<String, Cookie>();

        // fill map from hostnames to cookies
        List<String> cookies = splitCookies(megacookie);
        for (String s : cookies) {
            Cookie c = new Cookie(s);
            // System.out.println(c);

            List<String> servers = c.getServers();
            for (String server : servers) {
                cookieMap.put(server, c);
            }
        }

        this.cookieMap = cookieMap;
    }

    private static List<String> splitCookies(String megacookie) {
        List<String> result = new ArrayList<String>();

        String lines[] = megacookie.split("\n");
        boolean inCookie = false;
        StringBuilder sb = null;
        for (String l : lines) {
            if (l.equals(Cookie.BEGIN_COOKIE)) {
                inCookie = true;
                sb = new StringBuilder();
            }

            if (!inCookie) {
                continue;
            }

            sb.append(l);
            sb.append('\n');

            if (!l.equals(Cookie.END_COOKIE)) {
                continue;
            }

            inCookie = false;
            result.add(sb.toString());
        }

        return result;
    }

    Set<Entry<String, Cookie>> entrySet() {
        return cookieMap.entrySet();
    }

    Cookie get(String host) {
        return cookieMap.get(host);
    }
}

@@ -2779,11 +2779,100 @@
 );%0A    %7D
+%0A%0A    @Override%0A    public String toString() %7B%0A        return cookieMap.toString();%0A    %7D
 %0A%7D%0A

Add toString for cookiemap