/
TestAttributeProviderModule.java
158 lines (139 loc) · 5.51 KB
/
TestAttributeProviderModule.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/**
* Copyright (C) 2012-2016 Thales Services SAS.
*
* This file is part of AuthZForce CE.
*
* AuthZForce CE is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* AuthZForce CE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with AuthZForce CE. If not, see <http://www.gnu.org/licenses/>.
*/
/**
*
*/
package org.ow2.authzforce.core.test.custom;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.ow2.authzforce.core.pdp.api.AttributeGUID;
import org.ow2.authzforce.core.pdp.api.AttributeProvider;
import org.ow2.authzforce.core.pdp.api.BaseAttributeProviderModule;
import org.ow2.authzforce.core.pdp.api.CloseableAttributeProviderModule;
import org.ow2.authzforce.core.pdp.api.EnvironmentProperties;
import org.ow2.authzforce.core.pdp.api.EvaluationContext;
import org.ow2.authzforce.core.pdp.api.IndeterminateEvaluationException;
import org.ow2.authzforce.core.pdp.api.JaxbXACMLUtils.JaxbXACMLAttributeParser;
import org.ow2.authzforce.core.pdp.api.JaxbXACMLUtils.NonIssuedLikeIssuedStrictJaxbXACMLAttributeParser;
import org.ow2.authzforce.core.pdp.api.StatusHelper;
import org.ow2.authzforce.core.pdp.api.value.AttributeValue;
import org.ow2.authzforce.core.pdp.api.value.Bag;
import org.ow2.authzforce.core.pdp.api.value.Datatype;
import org.ow2.authzforce.core.pdp.api.value.DatatypeFactoryRegistry;
import org.ow2.authzforce.core.xmlns.test.TestAttributeProvider;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.Attribute;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.Attributes;
/**
*
* Fake AttributeProviderModule for test purposes only that can be configured to support a specific set of attribute Providers, but always return an empty bag
* as attribute value.
*
*/
public class TestAttributeProviderModule extends BaseAttributeProviderModule
{
/**
* module factory
*
*/
public static class Factory extends CloseableAttributeProviderModule.FactoryBuilder<TestAttributeProvider>
{
@Override
public Class<TestAttributeProvider> getJaxbClass()
{
return TestAttributeProvider.class;
}
@Override
public DependencyAwareFactory getInstance(final TestAttributeProvider conf, EnvironmentProperties environmentProperties)
{
return new DependencyAwareFactory()
{
@Override
public Set<AttributeDesignatorType> getDependencies()
{
// no dependency
return null;
}
@Override
public CloseableAttributeProviderModule getInstance(DatatypeFactoryRegistry attrDatatypeFactory, AttributeProvider depAttrProvider)
{
return new TestAttributeProviderModule(conf, attrDatatypeFactory);
}
};
}
}
private final Set<AttributeDesignatorType> supportedDesignatorTypes = new HashSet<>();
private final Map<AttributeGUID, Bag<?>> attrMap = new HashMap<>();
private TestAttributeProviderModule(TestAttributeProvider conf, DatatypeFactoryRegistry attrDatatypeFactory) throws IllegalArgumentException
{
super(conf.getId());
final JaxbXACMLAttributeParser<Bag<?>> xacmlAttributeParser = new NonIssuedLikeIssuedStrictJaxbXACMLAttributeParser(attrDatatypeFactory);
final Set<String> attrCategoryNames = new HashSet<>();
for (final Attributes jaxbAttributes : conf.getAttributes())
{
final String categoryName = jaxbAttributes.getCategory();
if (!attrCategoryNames.add(categoryName))
{
throw new IllegalArgumentException("Unsupported repetition of Attributes[@Category='" + categoryName + "']");
}
for (final Attribute jaxbAttr : jaxbAttributes.getAttributes())
{
xacmlAttributeParser.parseAttribute(attrMap, new AttributeGUID(categoryName, jaxbAttr.getIssuer(), jaxbAttr.getAttributeId()),
jaxbAttr.getAttributeValues(), null);
}
}
for (final Entry<AttributeGUID, Bag<?>> attrEntry : attrMap.entrySet())
{
final AttributeGUID attrKey = attrEntry.getKey();
final Bag<?> attrVals = attrEntry.getValue();
supportedDesignatorTypes.add(new AttributeDesignatorType(attrKey.getCategory(), attrKey.getId(), attrVals.getElementDatatype().getId(), attrKey
.getIssuer(), false));
}
}
@Override
public void close() throws IOException
{
// nothing to close
}
@Override
public Set<AttributeDesignatorType> getProvidedAttributes()
{
return supportedDesignatorTypes;
}
@Override
public <AV extends AttributeValue> Bag<AV> get(AttributeGUID attributeGUID, Datatype<AV> attributeDatatype, EvaluationContext context)
throws IndeterminateEvaluationException
{
final Bag<?> attrVals = attrMap.get(attributeGUID);
if (attrVals == null)
{
return null;
}
if (attrVals.getElementDatatype().equals(attributeDatatype))
{
return (Bag<AV>) attrVals;
}
throw new IndeterminateEvaluationException("Requested datatype (" + attributeDatatype + ") != provided by " + this + " ("
+ attrVals.getElementDatatype() + ")", StatusHelper.STATUS_MISSING_ATTRIBUTE);
}
}