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
RevLine 
[3797bbe]1package net.deterlab.abac;
2
3import java.io.*;
4import java.util.*;
[3612811]5import java.lang.reflect.*;
[3797bbe]6
7
8/**
[f84d71e]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.
[3797bbe]13 * @author <a href="http://abac.deterlab.net">ISI ABAC team</a>
14 * @version 1.3
15 */
16public class CredentialFactory {
[f84d71e]17    static List<CredentialParser> defParsers =new ArrayList<CredentialParser>();
18    static List<CredentialParser> parsers = new ArrayList<CredentialParser>();
[3612811]19    static String defName = "net.deterlab.abac.GENICredential";
20    public static final int maxSize = 50 * 1024;
21
[f84d71e]22    /**
23     * Initialize the Default parser list.
24     * @throws ABACException in extreme cases.  Should never happen.
25     */
26    static void initDefaultParser() throws ABACException {
[3797bbe]27        try {
[3612811]28            Class c = Class.forName(defName);
[f84d71e]29            Method m = c.getMethod("getCredentialParser");
30            CredentialParser cp = (CredentialParser) m.invoke(null);
31
32            defParsers.add(cp);
[3797bbe]33        }
[3612811]34        catch (Exception e) {
35            throw new ABACException("Could not initialize default " +
36                    "credential parser.  Something is very wrong", e);
[3797bbe]37        }
[3612811]38    }
39
[f84d71e]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;
[3612811]56        ABACException err = null;
[f84d71e]57        List<CredentialParser> useParsers = 
58            (parsers.size()> 0 ) ? parsers : defParsers;
[3612811]59
[f84d71e]60        if ( defParsers.size() < 1 )  initDefaultParser();
[3612811]61
[f84d71e]62        if (!is.markSupported() && useParsers.size() > 1) 
[3612811]63            is = new BufferedInputStream(is, maxSize);
64        is.mark(maxSize);
65
[f84d71e]66        for (CredentialParser c : useParsers ) {
[3612811]67            try {
[f84d71e]68                rv = c.parseCredential(is, ids);
69                break;
[3612811]70            }
[f84d71e]71            catch (ABACException e ) {
72                err = e;
[3612811]73                rv = null;
74            }
75            try {
[f84d71e]76                if (useParsers.size() > 1) is.reset(); 
[3612811]77            }
78            catch (IOException ie) { 
79                break;
80            }
[3797bbe]81        }
[3612811]82
[3797bbe]83        if ( rv != null ) 
[f84d71e]84            return rv;
[3797bbe]85        else 
[3612811]86            throw (err != null) ? err :
87                new ABACException("null exception and failed construction??");
88    }
89
90
[f84d71e]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 {
[3612811]105        try {
[f84d71e]106            return parseCredential(new FileInputStream(f), ids);
[3612811]107        }
108        catch (FileNotFoundException e) {
109            throw new CertInvalidException(e.getMessage(), e);
110        }
[3797bbe]111    }
[f84d71e]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);
[3797bbe]127    }
[3612811]128
[f84d71e]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     */
[3612811]137    static public void registerClass(String name)
[f84d71e]138        throws ABACException {
139        CredentialParser cp = null;
140
141        try {
[65dbf06]142            Class c = Class.forName(name);
[f84d71e]143            Method m = c.getMethod("getCredentialParser");
[3612811]144
[f84d71e]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        }
[3612811]151
[f84d71e]152        parsers.add(cp);
[3612811]153    }
[3797bbe]154}
Note: See TracBrowser for help on using the repository browser.