-
Notifications
You must be signed in to change notification settings - Fork 8
/
SunXACMLUtil.java
executable file
·199 lines (166 loc) · 5.87 KB
/
SunXACMLUtil.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
* SNE-XACML demo project: illustrate how to use SNE-XACML engine.
*
* Copyright (C) 2013 Canh T. 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.evaltest.sunxacml;
import oasis.names.tc.xacml._2_0.context.schema.os.ObjectFactory;
import oasis.names.tc.xacml._2_0.context.schema.os.RequestType;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;
import javax.xml.bind.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.IOException;
import java.io.StringWriter;
/**
* Convert from OpenSAML datatypes to SunXACML datatypes
*
* @author CanhNT <canhnt@gmail.com>
*
*/
public class SunXACMLUtil {
/**
* Unmarshall from DOM Element to a generic JAXBElement
*
* @param <T>
* @param cls
* @param domRequest
* @return
*/
private static <T> T unmarshall(Class<T> cls,
Element domRequest) {
try {
JAXBContext jc = JAXBContext.newInstance(cls);
Unmarshaller unmarshaller = jc.createUnmarshaller();
JAXBElement<T> jaxbObject = unmarshaller.unmarshal(domRequest, cls);
return jaxbObject.getValue();
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* Marshall from JAXB RequestType in SunXACML to DOM Element
*
* @param sunxacmlRequest
* @return
*/
public static Element marshall(
oasis.names.tc.xacml._2_0.context.schema.os.RequestType sunxacmlRequest) {
JAXBElement<oasis.names.tc.xacml._2_0.context.schema.os.RequestType> jaxbRequest = (new ObjectFactory()).createRequest(sunxacmlRequest);
Element element = null;
try {
element = marshall(oasis.names.tc.xacml._2_0.context.schema.os.RequestType.class, jaxbRequest);
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return element;
}
public static Element marshal(
oasis.names.tc.xacml._2_0.context.schema.os.ResponseType response) {
JAXBElement<oasis.names.tc.xacml._2_0.context.schema.os.ResponseType> jaxbRequest = (new ObjectFactory()).createResponse(response);
Element element = null;
try {
element = marshall(oasis.names.tc.xacml._2_0.context.schema.os.ResponseType.class, jaxbRequest);
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return element;
}
/**
* Marshall a generic JAXBElement to DOM Element
*
* @param <T>
* @param cls
* @param jaxbObject
* @return
* @throws JAXBException
* @throws ParserConfigurationException
*/
private static <T> Element marshall(Class<T> cls, JAXBElement<T> jaxbObject) throws JAXBException, ParserConfigurationException {
JAXBContext jc = JAXBContext.newInstance(cls);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT,
new Boolean(true));
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.newDocument();
marshaller.marshal(jaxbObject, doc);
return doc.getDocumentElement();
}
/**
* Unmarshal from DOM Element to oasis.names.tc.xacml._2_0.context.schema.os.RequestType
*
* @param domRequest
* @return
*/
public static RequestType unmarshalRequestType(Element domRequest) {
return unmarshall(RequestType.class, domRequest);
}
public static RequestType unmarshalRequestType(String xacmlFileName) throws ParserConfigurationException, SAXException, IOException {
Document doc = readXML(xacmlFileName);
Element xmlDom = doc.getDocumentElement();
return unmarshalRequestType(xmlDom);
}
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(xmlFile);
return doc;
}
public static String toString(Element element) {
try {
Transformer transformer = TransformerFactory.newInstance()
.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
StringWriter buffer = new StringWriter();
transformer.transform(new DOMSource(element), new StreamResult(
buffer));
return buffer.toString();
} catch (TransformerConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerFactoryConfigurationError e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
return null;
}
}