1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.opensaml.xml;
18
19 import java.security.NoSuchAlgorithmException;
20 import java.util.Map;
21 import java.util.Set;
22 import java.util.concurrent.ConcurrentHashMap;
23 import java.util.concurrent.CopyOnWriteArraySet;
24
25 import javax.crypto.Cipher;
26 import javax.crypto.NoSuchPaddingException;
27 import javax.xml.namespace.QName;
28 import javax.xml.parsers.DocumentBuilderFactory;
29
30 import org.opensaml.xml.io.Marshaller;
31 import org.opensaml.xml.io.MarshallerFactory;
32 import org.opensaml.xml.io.Unmarshaller;
33 import org.opensaml.xml.io.UnmarshallerFactory;
34 import org.opensaml.xml.security.SecurityConfiguration;
35 import org.opensaml.xml.util.XMLConstants;
36 import org.opensaml.xml.validation.ValidatorSuite;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.w3c.dom.Element;
40
41
42
43
44 public class Configuration {
45
46
47 private static Logger log = LoggerFactory.getLogger(Configuration.class);
48
49
50 private static QName defaultProvider = new QName(XMLConstants.XMLTOOLING_CONFIG_NS,
51 XMLConstants.XMLTOOLING_DEFAULT_OBJECT_PROVIDER);
52
53
54 private static Map<QName, Element> configuredObjectProviders = new ConcurrentHashMap<QName, Element>();
55
56
57 private static Map<String, Element> validatorSuiteConfigurations = new ConcurrentHashMap<String, Element>();
58
59
60 private static XMLObjectBuilderFactory builderFactory = new XMLObjectBuilderFactory();
61
62
63 private static MarshallerFactory marshallerFactory = new MarshallerFactory();
64
65
66 private static UnmarshallerFactory unmarshallerFactory = new UnmarshallerFactory();
67
68
69 private static Map<String, ValidatorSuite> validatorSuites = new ConcurrentHashMap<String, ValidatorSuite>();
70
71
72 private static Set<QName> idAttributeNames = new CopyOnWriteArraySet<QName>();
73
74
75 private static SecurityConfiguration globalSecurityConfig;
76
77
78 protected Configuration() {
79
80 }
81
82
83
84
85
86
87
88 public static QName getDefaultProviderQName() {
89 return defaultProvider;
90 }
91
92
93
94
95
96
97
98
99
100
101
102 public static void registerObjectProvider(QName providerName, XMLObjectBuilder builder, Marshaller marshaller,
103 Unmarshaller unmarshaller, Element configuration) {
104 log.debug("Registering new builder, marshaller, and unmarshaller for {}", providerName);
105 if(configuration != null){
106 configuredObjectProviders.put(providerName, configuration);
107 }
108 builderFactory.registerBuilder(providerName, builder);
109 marshallerFactory.registerMarshaller(providerName, marshaller);
110 unmarshallerFactory.registerUnmarshaller(providerName, unmarshaller);
111 }
112
113
114
115
116
117
118 public static void deregisterObjectProvider(QName key) {
119 log.debug("Unregistering builder, marshaller, and unmarshaller for {}", key);
120 configuredObjectProviders.remove(key);
121 builderFactory.deregisterBuilder(key);
122 marshallerFactory.deregisterMarshaller(key);
123 unmarshallerFactory.deregisterUnmarshaller(key);
124 }
125
126
127
128
129
130
131
132
133
134
135 public static Element getObjectProviderConfiguration(QName qualifedName) {
136 return (Element) configuredObjectProviders.get(qualifedName).cloneNode(true);
137 }
138
139
140
141
142
143
144 public static XMLObjectBuilderFactory getBuilderFactory() {
145 return builderFactory;
146 }
147
148
149
150
151
152
153 public static MarshallerFactory getMarshallerFactory() {
154 return marshallerFactory;
155 }
156
157
158
159
160
161
162
163 public static UnmarshallerFactory getUnmarshallerFactory() {
164 return unmarshallerFactory;
165 }
166
167
168
169
170
171
172
173
174 public static void registerValidatorSuite(String suiteId, ValidatorSuite suite, Element configuration) {
175 validatorSuiteConfigurations.put(suiteId, configuration);
176 validatorSuites.put(suiteId, suite);
177 }
178
179
180
181
182
183
184 public static void deregisterValidatorSuite(String suiteId) {
185 validatorSuiteConfigurations.remove(suiteId);
186 validatorSuites.remove(suiteId);
187 }
188
189
190
191
192
193
194
195
196
197
198 public static Element getValidatorSuiteConfiguration(String suiteId) {
199 return (Element) validatorSuiteConfigurations.get(suiteId).cloneNode(true);
200 }
201
202
203
204
205
206
207
208
209 public static ValidatorSuite getValidatorSuite(String suiteId) {
210 return validatorSuites.get(suiteId);
211 }
212
213
214
215
216
217
218 public static void registerIDAttribute(QName attributeName) {
219 if (!idAttributeNames.contains(attributeName)) {
220 idAttributeNames.add(attributeName);
221 }
222 }
223
224
225
226
227
228
229 public static void deregisterIDAttribute(QName attributeName) {
230 if (idAttributeNames.contains(attributeName)) {
231 idAttributeNames.remove(attributeName);
232 }
233 }
234
235
236
237
238
239
240
241 public static boolean isIDAttribute(QName attributeName) {
242 return idAttributeNames.contains(attributeName);
243 }
244
245
246
247
248
249
250 public static SecurityConfiguration getGlobalSecurityConfiguration() {
251 return globalSecurityConfig;
252 }
253
254
255
256
257
258
259 public static void setGlobalSecurityConfiguration(SecurityConfiguration config) {
260 globalSecurityConfig = config;
261 }
262
263
264
265
266 public static void validateNonSunJAXP() {
267 String builderFactoryClass = DocumentBuilderFactory.newInstance().getClass().getName();
268 log.debug("VM using JAXP parser {}", builderFactoryClass);
269
270 if (builderFactoryClass.startsWith("com.sun")) {
271 String errorMsg = "\n\n\nOpenSAML requires an xml parser that supports JAXP 1.3 and DOM3.\n"
272 + "The JVM is currently configured to use the Sun XML parser, which is known\n"
273 + "to be buggy and can not be used with OpenSAML. Please endorse a functional\n"
274 + "JAXP library(ies) such as Xerces and Xalan. For instructions on how to endorse\n"
275 + "a new parser see http://java.sun.com/j2se/1.5.0/docs/guide/standards/index.html\n\n\n";
276
277 log.error(errorMsg);
278 throw new Error(errorMsg);
279 }
280 }
281
282
283
284
285
286
287
288
289
290
291 public static boolean validateJCEProviders() {
292 boolean ret = true;
293
294
295
296
297
298 String errorMsgAESPadding = "The JCE providers currently configured in the JVM do not support\n"
299 + "required capabilities for XML Encryption, either the 'AES' cipher algorithm\n"
300 + "or the 'ISO10126Padding' padding scheme\n";
301
302 try {
303 Cipher.getInstance("AES/CBC/ISO10126Padding");
304 } catch (NoSuchAlgorithmException e) {
305
306
307 log.warn(errorMsgAESPadding);
308 ret = false;
309 } catch (NoSuchPaddingException e) {
310 log.warn(errorMsgAESPadding);
311 ret = false;
312 }
313
314
315
316 return ret;
317 }
318
319 static {
320 validateNonSunJAXP();
321
322 validateJCEProviders();
323
324
325 registerIDAttribute(new QName(javax.xml.XMLConstants.XML_NS_URI, "id"));
326 }
327 }