summaryrefslogtreecommitdiff
path: root/bcpkix/src/main/java/org/bouncycastle/cert/dane/DANEEntryStore.java
blob: 0d29c80a6e724e49b6cee77f5ff4c23d14496644 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package org.bouncycastle.cert.dane;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.bouncycastle.util.CollectionStore;
import org.bouncycastle.util.Selector;
import org.bouncycastle.util.Store;
import org.bouncycastle.util.StoreException;

/**
 * Class storing DANEEntry objects.
 */
public class DANEEntryStore
    implements Store
{
    private final Map entries;

    DANEEntryStore(List entries)
    {
        Map entryMap = new HashMap();

         for (Iterator it = entries.iterator(); it.hasNext();)
         {
             DANEEntry entry = (DANEEntry)it.next();

             entryMap.put(entry.getDomainName(), entry);
         }

        this.entries = Collections.unmodifiableMap(entryMap);
    }

    /**
     * Return a collection of entries matching the passed in selector.
     *
     * @param selector the selector to validate entries against.
     * @return a possibly empty collection of matched entries.
     * @throws StoreException in case of an underlying issue.
     */
    public Collection getMatches(Selector selector)
        throws StoreException
    {
        if (selector == null)
        {
            return entries.values();
        }

        List results = new ArrayList();

        for (Iterator it = entries.values().iterator(); it.hasNext();)
        {
            Object next = it.next();
            if (selector.match(next))
            {
                results.add(next);
            }
        }

        return Collections.unmodifiableList(results);
    }

    /**
     * Return a Store of X509CertificateHolder objects representing all the certificates associated with
     * entries in the store.
     *
     * @return a Store of X509CertificateHolder.
     */
    public Store toCertificateStore()
    {
        Collection col = this.getMatches(null);
        List certColl = new ArrayList(col.size());

        for (Iterator it = col.iterator(); it.hasNext();)
        {
            DANEEntry entry = (DANEEntry)it.next();

            certColl.add(entry.getCertificate());
        }

        return new CollectionStore(certColl);
    }
}