source: java/net/deterlab/abac/CredentialFactory.java @ 8ee55e7

abac0-leakabac0-meimei-idmei-rt0-nmei_rt0tvf-new-xml
Last change on this file since 8ee55e7 was 65dbf06, checked in by Ted Faber <faber@…>, 12 years ago

Parses either

  • Property mode set to 100644
File size: 5.3 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.3
15 */
16public class CredentialFactory {
17    static List<CredentialParser> defParsers =new ArrayList<CredentialParser>();
18    static List<CredentialParser> parsers = new ArrayList<CredentialParser>();
19    static String defName = "net.deterlab.abac.GENICredential";
20    public static final int maxSize = 50 * 1024;
21
22    /**
23     * Initialize the Default parser list.
24     * @throws ABACException in extreme cases.  Should never happen.
25     */
26    static void initDefaultParser() throws ABACException {
27        try {
28            Class c = Class.forName(defName);
29            Method m = c.getMethod("getCredentialParser");
30            CredentialParser cp = (CredentialParser) m.invoke(null);
31
32            defParsers.add(cp);
33        }
34        catch (Exception e) {
35            throw new ABACException("Could not initialize default " +
36                    "credential parser.  Something is very wrong", e);
37        }
38    }
39
40    /**
41     * Parse an input stream using each possible credential format and the
42     * available identities for validation.  Return the credentials found or
43     * throw an ABACException with the problem.  It wraps the input stream in a
44     * BufferedInputStream in order to retry is a parser fails.  Credentials
45     * larger than maxSize will nor be able to be reparsed.
46     * @param is an InputStream to parse
47     * @param ids a Collection of Identities for validation
48     * @return an Array of Credentials parsed
49     * @throws CertInvalidException if the stream is unparsable
50     * @throws MissingIssuerException if none of the Identities can validate the
51     * @throws BadSignatureException if the signature check fails
52     */
53    static public Credential[] parseCredential(InputStream is, 
54            Collection<Identity> ids) throws ABACException {
55        Credential[] rv = null;
56        ABACException err = null;
57        List<CredentialParser> useParsers = 
58            (parsers.size()> 0 ) ? parsers : defParsers;
59
60        if ( defParsers.size() < 1 )  initDefaultParser();
61
62        if (!is.markSupported() && useParsers.size() > 1) 
63            is = new BufferedInputStream(is, maxSize);
64        is.mark(maxSize);
65
66        for (CredentialParser c : useParsers ) {
67            try {
68                rv = c.parseCredential(is, ids);
69                break;
70            }
71            catch (ABACException e ) {
72                err = e;
73                rv = null;
74            }
75            try {
76                if (useParsers.size() > 1) is.reset(); 
77            }
78            catch (IOException ie) { 
79                break;
80            }
81        }
82
83        if ( rv != null ) 
84            return rv;
85        else 
86            throw (err != null) ? err :
87                new ABACException("null exception and failed construction??");
88    }
89
90
91    /**
92     * Parse a File using each possible credential format and the
93     * available identities for validation.  Return the credentials found or
94     * throw an ABACException with the problem.  Calls the InputStream version
95     * internally.
96     * @param f a File to parse
97     * @param ids a Collection of Identities for validation
98     * @return an Array of Credentials parsed
99     * @throws CertInvalidException if the stream is unparsable
100     * @throws MissingIssuerException if none of the Identities can validate the
101     * @throws BadSignatureException if the signature check fails
102     */
103    static public Credential[] parseCredential(File f, 
104            Collection<Identity> ids) throws ABACException {
105        try {
106            return parseCredential(new FileInputStream(f), ids);
107        }
108        catch (FileNotFoundException e) {
109            throw new CertInvalidException(e.getMessage(), e);
110        }
111    }
112    /**
113     * Parse a file named fn using each possible credential format and the
114     * available identities for validation.  Return the credentials found or
115     * throw an ABACException with the problem.  Calls the InputStream version
116     * internally.
117     * @param fn a String holding the filename to parse
118     * @param ids a Collection of Identities for validation
119     * @return an Array of Credentials parsed
120     * @throws CertInvalidException if the stream is unparsable
121     * @throws MissingIssuerException if none of the Identities can validate the
122     * @throws BadSignatureException if the signature check fails
123     */
124    static public Credential[] parseCredential(String fn, 
125            Collection<Identity> ids) throws ABACException {
126        return parseCredential(new File(fn), ids);
127    }
128
129    /**
130     * Add the named class to the list of usable parsers.  The class passed in
131     * must have a static getCredentialParser() method that returns a
132     * CredentialParser to use.
133     * @param name a String containing the binary name of the class to register
134     * @throws ABACException if there is a problem.  The cause field of this
135     * exception is set to the classloading exception, if any.
136     */
137    static public void registerClass(String name)
138        throws ABACException {
139        CredentialParser cp = null;
140
141        try {
142            Class c = Class.forName(name);
143            Method m = c.getMethod("getCredentialParser");
144
145            cp = (CredentialParser) m.invoke(null);
146        }
147        catch (Exception e) {
148            throw new ABACException("Could not register credential type" + 
149                    e.getMessage(), e);
150        }
151
152        parsers.add(cp);
153    }
154}
Note: See TracBrowser for help on using the repository browser.