source: java/net/deterlab/abac/CredentialFactory.java @ 4560b65

abac0-leakabac0-meimei-idmei-rt0-nmei_rt0tvf-new-xml
Last change on this file since 4560b65 was 4560b65, checked in by Ted Faber <faber@…>, 11 years ago

Bump version number

  • Property mode set to 100644
File size: 6.6 KB
Line 
1package net.deterlab.abac;
2
3import java.io.*;
4import java.util.*;
5import java.lang.reflect.*;
6
7
8/**
9 * The system demultiplex point for parsing Credentials.  Static class that
10 * keeps an ordered list of CredentialParsers to try and a couple helper
11 * functions to open files, etc in calling the CredentialParsers.  All
12 * credential parsing should use a credential factory.
13 * @author <a href="http://abac.deterlab.net">ISI ABAC team</a>
14 * @version 1.4
15 */
16public class CredentialFactory implements Cloneable {
17    protected List<CredentialParser> parsers;
18    static public String defName = "net.deterlab.abac.GENICredential";
19    public static final int maxSize = 50 * 1024;
20
21    /**
22     * Create a Credential Factory that parses the default type(s)
23     * @throws ABACException if the object cannot be created
24     */
25    public CredentialFactory() throws ABACException {
26        parsers = new ArrayList<CredentialParser>();
27        registerClass(defName);
28    }
29
30    /**
31     * Create a Credential Factory that parses the given type(s).  Each String
32     * should be the binary name for a class that exports a static
33     * getCredentialParser method that returns a CredentialParser for the
34     * class.
35     * @param names a Collection of Strings naming the classes to parse
36     * @throws ABACException if the object cannot be created
37     */
38    public CredentialFactory(Collection<String> names) throws ABACException {
39        parsers = new ArrayList<CredentialParser>();
40        for (String n : names ) 
41            registerClass(n);
42    }
43
44    /**
45     * Create a Credential Factory that parses the given type(s).  Each String
46     * should be the binary name for a class that exports a static
47     * getCredentialParser method that returns a CredentialParser for the
48     * class.
49     * @param names an Array of Strings naming the classes to parse
50     * @throws ABACException if the object cannot be created
51     */
52    public CredentialFactory(String[] names) throws ABACException {
53        parsers = new ArrayList<CredentialParser>();
54        for (String n : names ) 
55            registerClass(n);
56    }
57
58    /**
59     * Create a Credential Factory that is a clone of the given
60     * CredentialFactory.
61     * @param cf the CredentialFactory to copy
62     * @throws ABACException if the object cannot be created
63     */
64    public CredentialFactory(CredentialFactory cf) throws ABACException {
65        this();
66
67        parsers = new ArrayList<CredentialParser>();
68
69        for ( int i = 0; i < parsers.size(); i++) 
70            parsers.add(cf.parsers.get(i));
71    }
72
73    /**
74     * Make a copy of this CredentialFactory
75     * @returns a CredentialFactory, a copy of this one
76     */
77    public Object clone() throws CloneNotSupportedException {
78        CredentialFactory cf = null;
79        try {
80            cf = new CredentialFactory(this);
81        }
82        catch (ABACException ae) {
83            return null;
84        }
85        return cf;
86    }
87
88
89    /**
90     * Parse an input stream using each possible credential format and the
91     * available identities for validation.  Return the credentials found or
92     * throw an ABACException with the problem.  It wraps the input stream in a
93     * BufferedInputStream in order to retry is a parser fails.  Credentials
94     * larger than maxSize will nor be able to be reparsed.
95     * @param is an InputStream to parse
96     * @param ids a Collection of Identities for validation
97     * @return an Array of Credentials parsed
98     * @throws CertInvalidException if the stream is unparsable
99     * @throws MissingIssuerException if none of the Identities can validate the
100     * @throws BadSignatureException if the signature check fails
101     */
102    public Credential[] parseCredential(InputStream is, 
103            Collection<Identity> ids) throws ABACException {
104        Credential[] rv = null;
105        ABACException err = null;
106
107        if (!is.markSupported() && parsers.size() > 1) 
108            is = new BufferedInputStream(is, maxSize);
109        if (parsers.size() > 1)
110            is.mark(maxSize);
111
112        for (CredentialParser c : parsers ) {
113            try {
114                rv = c.parseCredential(is, ids);
115                break;
116            }
117            catch (ABACException e ) {
118                err = e;
119                rv = null;
120            }
121            try {
122                if (parsers.size() > 1) is.reset(); 
123            }
124            catch (IOException ie) { 
125                break;
126            }
127        }
128
129        if ( rv != null ) 
130            return rv;
131        else 
132            throw (err != null) ? err :
133                new ABACException("null exception and failed construction??");
134    }
135
136
137    /**
138     * Parse a File using each possible credential format and the
139     * available identities for validation.  Return the credentials found or
140     * throw an ABACException with the problem.  Calls the InputStream version
141     * internally.
142     * @param f a File to parse
143     * @param ids a Collection of Identities for validation
144     * @return an Array of Credentials parsed
145     * @throws CertInvalidException if the stream is unparsable
146     * @throws MissingIssuerException if none of the Identities can validate the
147     * @throws BadSignatureException if the signature check fails
148     */
149    public Credential[] parseCredential(File f, 
150            Collection<Identity> ids) throws ABACException {
151        try {
152            return parseCredential(new FileInputStream(f), ids);
153        }
154        catch (FileNotFoundException e) {
155            throw new CertInvalidException(e.getMessage(), e);
156        }
157    }
158    /**
159     * Parse a file named fn using each possible credential format and the
160     * available identities for validation.  Return the credentials found or
161     * throw an ABACException with the problem.  Calls the InputStream version
162     * internally.
163     * @param fn a String holding the filename to parse
164     * @param ids a Collection of Identities for validation
165     * @return an Array of Credentials parsed
166     * @throws CertInvalidException if the stream is unparsable
167     * @throws MissingIssuerException if none of the Identities can validate the
168     * @throws BadSignatureException if the signature check fails
169     */
170    public Credential[] parseCredential(String fn, 
171            Collection<Identity> ids) throws ABACException {
172        return parseCredential(new File(fn), ids);
173    }
174
175    /**
176     * Add the named class to the list of usable parsers.  The class passed in
177     * must have a static getCredentialParser() method that returns a
178     * CredentialParser to use.
179     * @param name a String containing the binary name of the class to register
180     * @throws ABACException if there is a problem.  The cause field of this
181     * exception is set to the classloading exception, if any.
182     */
183    public void registerClass(String name)
184        throws ABACException {
185        CredentialParser cp = null;
186
187        try {
188            Class c = Class.forName(name);
189            Method m = c.getMethod("getCredentialParser");
190
191            cp = (CredentialParser) m.invoke(null);
192        }
193        catch (Exception e) {
194            throw new ABACException("Could not register credential type" + 
195                    e.getMessage(), e);
196        }
197
198        parsers.add(cp);
199    }
200}
Note: See TracBrowser for help on using the repository browser.