-
Notifications
You must be signed in to change notification settings - Fork 8
/
XACMLUtil.java
183 lines (144 loc) · 6.03 KB
/
XACMLUtil.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
176
177
178
179
180
181
182
183
/*
* SNE-XACML: A high performance XACML evaluation engine.
*
* Copyright (C) 2013-2014 Canh Ngo <canhnt@gmail.com>
* System and Network Engineering Group, University of Amsterdam.
* 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.util;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObjectFactory;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicySetType;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.RequestType;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;
import javax.xml.bind.*;
import javax.xml.parsers.ParserConfigurationException;
import java.io.*;
/**
* @author Canh Ngo
*/
public class XACMLUtil {
public static PolicySetType unmarshalPolicySetType(InputStream istream)
throws ParserConfigurationException, SAXException, IOException {
Document doc = readXML(istream);
Element xmlDom = doc.getDocumentElement();
PolicySetType policyset = unmarshalPolicySetType(xmlDom);
if (policyset != null && policyset.getPolicySetId() != null) {
return policyset;
}
return null;
}
public static PolicySetType unmarshalPolicySetType(String policysetFile)
throws ParserConfigurationException, SAXException, IOException {
Document doc = readXML(policysetFile);
Element xmlDom = doc.getDocumentElement();
return unmarshalPolicySetType(xmlDom);
}
/**
* @param domRequest
* @return
*/
private static PolicySetType unmarshalPolicySetType(Element dom) {
return unmarshall(PolicySetType.class, dom);
}
private static <T> T unmarshall(Class<T> cls, Element dom) {
try {
JAXBContext jc = JAXBContext.newInstance(cls);
Unmarshaller unmarshaller = jc.createUnmarshaller();
JAXBElement<T> jaxbObject = unmarshaller.unmarshal(dom, cls);
return jaxbObject.getValue();
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static Document readXML(InputStream istream)
throws ParserConfigurationException, SAXException, IOException {
javax.xml.parsers.DocumentBuilderFactory dbf = javax.xml.parsers.DocumentBuilderFactory
.newInstance();
dbf.setNamespaceAware(true);
javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();
org.w3c.dom.Document doc = db.parse(istream);
return doc;
}
public static Document readXML(String xmlFile)
throws ParserConfigurationException, SAXException, IOException {
javax.xml.parsers.DocumentBuilderFactory dbf = javax.xml.parsers.DocumentBuilderFactory
.newInstance();
dbf.setNamespaceAware(true);
javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();
org.w3c.dom.Document doc = db.parse(new FileInputStream(xmlFile));
return doc;
}
public static RequestType unmarshalRequestType(InputStream istream)
throws ParserConfigurationException, SAXException, IOException {
Document doc = readXML(istream);
Element xmlDom = doc.getDocumentElement();
return unmarshalRequestType(xmlDom);
}
public static RequestType unmarshalRequestType(Element xmlDom) {
return unmarshall(RequestType.class, xmlDom);
}
public static PolicyType unmarshalPolicyType(InputStream istream)
throws ParserConfigurationException, SAXException, IOException {
Document doc = readXML(istream);
Element xmlDom = doc.getDocumentElement();
return unmarshalPolicyType(xmlDom);
}
public static PolicyType unmarshalPolicyType(String policyFile)
throws ParserConfigurationException, SAXException, IOException {
Document doc = readXML(policyFile);
Element xmlDom = doc.getDocumentElement();
PolicyType policy = unmarshalPolicyType(xmlDom);
if (policy != null && policy.getPolicyId() != null) {
return policy;
}
return null;
}
private static PolicyType unmarshalPolicyType(Element xmlDom) {
return unmarshall(PolicyType.class, xmlDom);
}
public static RequestType unmarshalRequestType(String xmlFile)
throws ParserConfigurationException, SAXException, IOException {
Document doc = readXML(xmlFile);
Element xmlDom = doc.getDocumentElement();
return unmarshalRequestType(xmlDom);
}
public static <T> void print(JAXBElement<T> jaxbElement, Class<T> cls,
OutputStream os) {
JAXBContext jc;
try {
jc = JAXBContext.newInstance(cls);
Marshaller m = jc.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.marshal(jaxbElement, os);
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static String print(RequestType request) {
OutputStream os = new ByteArrayOutputStream();
print((new ObjectFactory()).createRequest(request),
oasis.names.tc.xacml._3_0.core.schema.wd_17.RequestType.class, os);
return os.toString();
}
}