-
Notifications
You must be signed in to change notification settings - Fork 8
/
AllOfExpression.java
175 lines (148 loc) · 6.83 KB
/
AllOfExpression.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* Copyright (C) 2013-2016 Canh Ngo <canhnt@gmail.com>
* All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package nl.uva.sne.xacml.policy.parsers;
import nl.uva.sne.midd.MIDDException;
import nl.uva.sne.midd.interval.EndPoint;
import nl.uva.sne.midd.interval.Interval;
import nl.uva.sne.xacml.policy.parsers.util.DataTypeConverterUtil;
import nl.uva.sne.xacml.policy.parsers.util.MatchIdConverterUtil;
import nl.uva.sne.xacml.policy.parsers.util.MatchIdConverterUtil.OperatorType;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* AllOfExpression class is to parse the AllOf XACML 3.0 element, which returns the map of parsed intervals for each
* variable identifiers.
* <p/>
* Note: AllOf XACML3 element contains conjunctive sequence of Match expressions.
* <p/>
* AllOfType := SEQUENCE_OF<MatchExpression> MatchExpression := (operator, value, varId) <br/>
* operator := [gt, ge, lt, le, eq]
*
* @author Canh Ngo
* @date: Sep 27, 2012
*/
public class AllOfExpression {
private static final Logger log = LoggerFactory.getLogger(AllOfExpression.class);
@SuppressWarnings("rawtypes")
private Map<String, AttributeInfo> variables;
public AllOfExpression() {
variables = new HashMap<String, AttributeInfo>();
}
/**
* @return List of variables with their equivalent intervals.
*/
public Map<String, AttributeInfo> getAttributeInfos() {
return this.variables;
}
private EndPoint getValue(AttributeValueType attributeValue)
throws XACMLParsingException, MIDDException {
List<Object> objects = attributeValue.getContent();
if (objects == null || objects.size() == 0) {
throw new XACMLParsingException("Cannot extract attribute value");
}
// only support 1-value at the moment.
String strValue = (String) objects.get(0);
Comparable value = DataTypeConverterUtil.convert(strValue, attributeValue.getDataType());
return new EndPoint(value);
}
public void parse(AllOfType allOf) throws XACMLParsingException, MIDDException {
List<MatchType> lstMatches = allOf.getMatch();
// Collect all intervals in the allOf expression and put to each attribute
Map<String, AttributeInfo> intervals = new HashMap<String, AttributeInfo>();
for (MatchType match : lstMatches) {
// validating the match expression
validate(match);
String varId = match.getAttributeDesignator().getAttributeId();
boolean isMustBePresent = match.getAttributeDesignator().isMustBePresent();
if (!intervals.containsKey(varId)) {
// initialize interval as (-inf, +inf)
Interval newInterval = new Interval(new EndPoint(EndPoint.Infinity.NEGATIVE),
new EndPoint(EndPoint.Infinity.POSITIVE));
intervals.put(varId, new AttributeInfo(isMustBePresent, newInterval));
}
// update mustBePresent state of the variable: if it is set before in the expression.
intervals.get(varId).isMustBePresent = intervals.get(varId).isMustBePresent ^ isMustBePresent;
Interval interval = intervals.get(varId).getInterval();
OperatorType operator = MatchIdConverterUtil.getOperator(match
.getMatchId());
EndPoint value = getValue(match.getAttributeValue());
switch (operator) {
case LESS_THAN:
case LESS_THAN_OR_EQUAL: {
// find the largest lower-bound
if (value.compareTo(interval.getLowerBound()) >= 0) {
interval.setLowerBound(value);
interval.closeLeft(operator == OperatorType.LESS_THAN_OR_EQUAL);
}
break;
}
case GREATER_THAN:
case GREATER_THAN_OR_EQUAL: {
int c = value.compareTo(interval.getUpperBound());
if (c <= 0) {
interval.setUpperBound(value);
interval.closeRight(operator == OperatorType.GREATER_THAN_OR_EQUAL);
}
break;
}
case EQUAL:
interval.setSingleValue(value);
break;
}
}
// validating extracted intervals
Map<String, AttributeInfo> validIntervals = new HashMap<String, AttributeInfo>();
for (String varId : intervals.keySet()) {
Interval interval = intervals.get(varId).getInterval();
if (interval.validate()) {
// System.err.println("Invalid AllOf expression: unable to extract satisfied interval");
validIntervals.put(varId, intervals.get(varId));
} else {
// throw new XACMLParsingException("Invalid AllOf expression: unable to extract satisfied interval");
log.error("Invalid AllOf expression: unable to extract satisfied interval");
}
}
// variables = intervals;
variables = validIntervals;
}
private void validate(MatchType match) throws XACMLParsingException {
AttributeValueType attrValue = match.getAttributeValue();
AttributeDesignatorType attrDesignator = match.getAttributeDesignator();
if (null == attrValue) {
throw new XACMLParsingException("No attribute value found");
}
if (null == attrDesignator) {
throw new XACMLParsingException("No attribute designator found");
}
String attrValueDataType = attrValue.getDataType();
if (attrValueDataType == null
|| !attrValueDataType.equals(attrDesignator.getDataType())) {
throw new XACMLParsingException(
"Data types in match expression do not valid");
}
}
}