source: fedd/abac-src/ttg/credential/DiscoveryService.java @ 7321a43

version-1.30
Last change on this file since 7321a43 was 8780cbec, checked in by Jay Jacobs <Jay.Jacobs@…>, 15 years ago

ABAC sources from Cobham

  • Property mode set to 100644
File size: 5.3 KB
Line 
1package com.nailabs.abac.credential;
2
3import java.io.*;
4import java.util.*;
5import edu.stanford.peer.rbtm.credential.*;
6import edu.stanford.peer.rbtm.engine.*;
7import edu.stanford.peer.rbtm.util.*;
8import edu.stanford.rt.parser.*;
9import edu.stanford.rt.credential.*;
10 
11/**
12 * A service object which encapsulates the functions of a CredentialManager
13 * that are necessary for remote and local discovery.
14 */
15public class DiscoveryService {
16    /** The graphine which performs the actual searches */
17    protected RtmlEngine engine;
18    /** An RTML parser which can be shared for all discovery services */
19    protected static RTParser parser = null;
20    /** AN RTML context which can be shared across all discovery services */
21    protected static RTContext context = null;
22
23    static {
24        try {
25            parser = new RTParser();
26            context = new RTContext(parser);
27        } catch(Exception ex) {
28            ex.printStackTrace();
29        }
30    }
31
32    /** Default constructor */
33    public DiscoveryService(String storeFile) {
34        try {
35            parser = new RTParser();
36            context = new RTContext(parser);
37            CredentialStore store = new CredentialStore(parser);
38            parser.parseCredentialStore
39                (new FileInputStream(new File(storeFile)), context, store);
40            this.engine = new RtmlEngine(store);
41        } catch (Exception ex) {
42            //ex.printStackTrace();
43        }
44    }
45
46    /**
47     * For each result in the map, match the RTML for the domain of the
48     * rtml credential.
49     */
50    protected String[] soapify(ResultEvidenceMap map) {
51        Object results[] = map.resultSet().toArray();
52        int max = results.length;
53        String[] evidence = new String[max];
54        for(int i = 0; i < max; i++) {
55            Object obj = map.getResultEvidence(results[i]);
56            if(obj instanceof RtmlCredential) {
57                RtmlCredential cred = (RtmlCredential)obj;
58                evidence[i] = cred.toXML();
59            } else {
60                evidence[i] = results[i] + " -" + obj.toString();
61            }
62        }
63        return evidence;
64    }
65
66    /**
67     * Convert a collection of <CODE>RTMLCredential</CODE> instances into a
68     * set of their <CODE>CredentialDomain</CODE> instances.
69     */
70    protected String[] soapify(Collection c) {
71        //String[] credentials = new String[c.size()];
72        HashSet credentials = new HashSet(c.size());
73        Iterator results = c.iterator();
74        String output[];
75        int i = 0;
76       
77        while(results.hasNext()) {
78            Object obj = results.next();
79            if(obj instanceof RtmlCredential) {
80                RtmlCredential cred = (RtmlCredential)obj;
81                //credentials[i++] = cred.toXML();
82                credentials.add(cred.toXML());
83            } else {
84                //credentials[i++] = obj.toString();
85                credentials.add(obj.toString());
86            }
87        }
88        output = new String[credentials.size()];
89        results = credentials.iterator(); 
90        for(i = 0; results.hasNext(); i++) {
91            output[i] = results.next().toString();
92        }
93        return output;
94    }
95
96    /**
97     * For a specified entity name, return all <code>CredentialDomain</code>
98     * instances which were issued by the entity.
99     */
100    public String[] getCredentialsIssuedBy(String entityName) {
101        Entity e = new RtmlEntity(entityName);
102        return soapify(engine.getCredentialsIssuedBy(e));
103        //return soapify(engine.backwardSearch(e));
104    }
105
106    /**
107     * For the specified role, return all <code>CredentialDomain</code>
108     * instances which define the specified role (entity.role).
109     */
110    public String[] findCredentialsDefiningRole(String entity, String role) {
111        Entity e = new RtmlEntity(entity);
112        edu.stanford.peer.rbtm.credential.Role r = 
113            new edu.stanford.peer.rbtm.credential.Role(e, role);
114        Iterator results = engine.findCredentialsDefiningRole(r);
115        HashSet evidence = new HashSet();
116        StringBuffer buff = new StringBuffer("DefiningRole(");
117        buff.append(entity).append(".").append(role).append(") = ");
118        while(results.hasNext()) {
119            Object result = results.next();
120            //System.out.println("def result = " + result);
121            buff.append(result.toString()).append(" ");
122            evidence.add(result);
123        }
124        System.out.println(buff.toString());
125        return soapify(evidence);
126    }
127
128    /**
129     * For a given entity expression, find the <code>CredentialDomain</code>
130     * instances which define credentials that have the entity expression as
131     * a subject.
132     */
133    public String[] findCredentialsBySubject(String expr) {
134        EntityExpression ee = null;
135        try {
136            ee = StaticCredential.getEntityExpression(expr);
137            ee = RtmlExpression.convert(ee);
138        } catch (Exception cpe) {
139            cpe.printStackTrace();
140        }
141        Iterator results = engine.findCredentialsBySubject(ee);
142        HashSet evidence = new HashSet();
143        StringBuffer buff = new StringBuffer("BySubject(");
144        buff.append(expr).append(") = ");
145        while(results.hasNext()) {
146            Object result = results.next();
147            //System.out.println("subj result = " + result);
148            buff.append(result).append(" ");
149            if(!evidence.contains(result)) {
150                evidence.add(result);
151            }
152        }
153        System.out.println(buff.toString());
154        return soapify(evidence);
155    }
156
157    /**
158     * Add a new <code>CredentialDomain</code> to the underlying
159     * <code>CredentialStore</code>.
160     * @param xml an xml block which defined a <code>CredentialDomain</code>
161     */
162    public void addCredentialDomain(String xml) {
163        try {
164            InputStream in = new ByteArrayInputStream(xml.getBytes());
165            CredentialDomain domain = 
166                parser.parseCredentialDomain(in, context);
167            synchronized(engine) {
168                engine.addDomain(domain);
169            }
170        } catch(Exception ex) {
171            ex.printStackTrace();
172        }
173    }
174
175}
Note: See TracBrowser for help on using the repository browser.