KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > security > test > xacml > modules > TestRoleAttributeFinderModule


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.test.security.test.xacml.modules;
23
24 import java.net.URI JavaDoc;
25 import java.net.URISyntaxException JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Set JavaDoc;
30
31 import com.sun.xacml.EvaluationCtx;
32 import com.sun.xacml.attr.AttributeDesignator;
33 import com.sun.xacml.attr.BagAttribute;
34 import com.sun.xacml.attr.StringAttribute;
35 import com.sun.xacml.cond.EvaluationResult;
36 import com.sun.xacml.ctx.Status;
37 import com.sun.xacml.finder.AttributeFinderModule;
38
39 //$Id: TestRoleAttributeFinderModule.java 58115 2006-11-04 08:42:14Z scott.stark@jboss.org $
40

41 /**
42  * An attribute finder module for testing that only deals with the
43  * role identifier called as
44  * "urn:oasis:names:tc:xacml:1.0:example:attribute:role"
45  * @author <a HREF="mailto:Anil.Saldhana@jboss.org">Anil Saldhana</a>
46  * @since May 26, 2006
47  * @version $Revision: 58115 $
48  */

49
50 public class TestRoleAttributeFinderModule extends AttributeFinderModule
51 {
52    /**
53     * XACML Identifier supported by this module
54     */

55    public static final String JavaDoc ROLE_IDENTIFIER =
56       "urn:oasis:names:tc:xacml:1.0:example:attribute:role";
57    
58    // subject-id standard identifier
59
private static URI JavaDoc SUBJECT_IDENTIFIER = null;
60    
61    private static URI JavaDoc SUBJECT_SOMEATTRIBUTE_IDENTIFIER = null;
62    
63    // initialize the standard subject identifier
64
static
65    {
66       try
67       {
68          SUBJECT_IDENTIFIER =
69             new URI JavaDoc("urn:oasis:names:tc:xacml:1.0:subject:subject-id");
70          SUBJECT_SOMEATTRIBUTE_IDENTIFIER =
71             new URI JavaDoc("urn:oasis:names:tc:xacml:2.0:jboss-test:some-attribute");
72       }
73       catch (URISyntaxException JavaDoc ex)
74       {
75       }
76    };
77    
78    /**
79     * Default constructor.
80     */

81    public TestRoleAttributeFinderModule()
82    {
83       
84    }
85    
86    /**
87     * @see AttributeFinderModule#isDesignatorSupported()
88     *
89     * @return true
90     */

91    public boolean isDesignatorSupported()
92    {
93       return true;
94    }
95    
96    /**
97     * @see AttributeFinderModule#getSupportedDesignatorTypes()
98     * Returns only <code>SUBJECT_TARGET</code> since this module only
99     * supports Subject attributes.
100     *
101     * @return a <code>Set</code> with an <code>Integer</code> of value
102     * <code>AttributeDesignator.SUBJECT_TARGET</code>
103     */

104    public Set JavaDoc getSupportedDesignatorTypes()
105    {
106       Set JavaDoc set = new HashSet JavaDoc();
107       set.add(new Integer JavaDoc(AttributeDesignator.SUBJECT_TARGET));
108       return set;
109    }
110    
111    /**
112     * @see AttributeFinderModule#getSupportedIds()
113     *
114     * @return a <code>Set</code> containing <code>ROLE_IDENTIFIER</code>
115     */

116    public Set JavaDoc getSupportedIds()
117    {
118       Set JavaDoc set = new HashSet JavaDoc();
119       set.add(ROLE_IDENTIFIER);
120       return set;
121    }
122    
123    /**
124     * Supports the retrieval of exactly one kind of attribute.
125     */

126    public EvaluationResult findAttribute(URI JavaDoc attributeType, URI JavaDoc attributeId,
127          URI JavaDoc issuer, URI JavaDoc subjectLogger,
128          EvaluationCtx context,
129          int designatorType)
130    {
131       // Check the identifier
132
if (! attributeId.toString().equals(ROLE_IDENTIFIER))
133          return new EvaluationResult(BagAttribute.
134                createEmptyBag(attributeType));
135       
136       // Did they ask for a String??
137
if (! attributeType.toString().equals(StringAttribute.identifier))
138          return new EvaluationResult(BagAttribute.
139                createEmptyBag(attributeType));
140       
141       // Retrieve the subject identifer from the context
142
EvaluationResult result =
143          context.getSubjectAttribute(attributeType, SUBJECT_IDENTIFIER,
144                issuer, subjectLogger);
145       if (result.indeterminate())
146          return result;
147       
148       // Check that we succeeded in getting the subject identifier
149
BagAttribute bag = (BagAttribute)(result.getAttributeValue());
150       if (bag.isEmpty())
151       {
152          ArrayList JavaDoc code = new ArrayList JavaDoc();
153          code.add(Status.STATUS_MISSING_ATTRIBUTE);
154          Status status = new Status(code, "missing subject-id");
155          return new EvaluationResult(status);
156       }
157       
158       // Finally search for the subject with the role-mapping defined,
159
// and if there is a match, add their role
160
BagAttribute returnBag = null;
161       Iterator JavaDoc it = bag.iterator();
162       while (it.hasNext())
163       {
164          StringAttribute attr = (StringAttribute)(it.next());
165          if (attr.getValue().equals("Anil Saldhana"))
166          {
167             Set JavaDoc set = new HashSet JavaDoc();
168             set.add(new StringAttribute("Developer"));
169             returnBag = new BagAttribute(attributeType, set);
170             break;
171          }
172       }
173       
174       return new EvaluationResult(returnBag);
175    }
176 }
177
Popular Tags