2
|
1 |
/*
|
1337
|
2 |
* reserved comment block
|
|
3 |
* DO NOT REMOVE OR ALTER!
|
2
|
4 |
*/
|
|
5 |
/*
|
1337
|
6 |
* Copyright 2005 The Apache Software Foundation.
|
|
7 |
*
|
|
8 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
9 |
* you may not use this file except in compliance with the License.
|
|
10 |
* You may obtain a copy of the License at
|
|
11 |
*
|
|
12 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
13 |
*
|
|
14 |
* Unless required by applicable law or agreed to in writing, software
|
|
15 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
16 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
17 |
* See the License for the specific language governing permissions and
|
|
18 |
* limitations under the License.
|
|
19 |
*
|
|
20 |
*/
|
|
21 |
/*
|
1639
|
22 |
* Copyright 2005-2008 Sun Microsystems, Inc. All rights reserved.
|
1337
|
23 |
*/
|
|
24 |
/*
|
|
25 |
* $Id: DOMURIDereferencer.java,v 1.2 2008/07/24 15:20:32 mullan Exp $
|
2
|
26 |
*/
|
|
27 |
package org.jcp.xml.dsig.internal.dom;
|
|
28 |
|
|
29 |
import org.w3c.dom.Attr;
|
|
30 |
import org.w3c.dom.Element;
|
|
31 |
import org.w3c.dom.Node;
|
|
32 |
|
|
33 |
import com.sun.org.apache.xml.internal.security.Init;
|
|
34 |
import com.sun.org.apache.xml.internal.security.utils.IdResolver;
|
|
35 |
import com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolver;
|
|
36 |
import com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput;
|
|
37 |
|
|
38 |
import javax.xml.crypto.*;
|
|
39 |
import javax.xml.crypto.dom.*;
|
|
40 |
import javax.xml.crypto.dsig.*;
|
|
41 |
|
|
42 |
/**
|
|
43 |
* DOM-based implementation of URIDereferencer.
|
|
44 |
*
|
|
45 |
* @author Sean Mullan
|
|
46 |
*/
|
|
47 |
public class DOMURIDereferencer implements URIDereferencer {
|
|
48 |
|
|
49 |
static final URIDereferencer INSTANCE = new DOMURIDereferencer();
|
|
50 |
|
|
51 |
private DOMURIDereferencer() {
|
|
52 |
// need to call com.sun.org.apache.xml.internal.security.Init.init()
|
|
53 |
// before calling any apache security code
|
|
54 |
Init.init();
|
|
55 |
}
|
|
56 |
|
|
57 |
public Data dereference(URIReference uriRef, XMLCryptoContext context)
|
|
58 |
throws URIReferenceException {
|
|
59 |
|
|
60 |
if (uriRef == null) {
|
|
61 |
throw new NullPointerException("uriRef cannot be null");
|
|
62 |
}
|
|
63 |
if (context == null) {
|
|
64 |
throw new NullPointerException("context cannot be null");
|
|
65 |
}
|
|
66 |
|
|
67 |
DOMURIReference domRef = (DOMURIReference) uriRef;
|
|
68 |
Attr uriAttr = (Attr) domRef.getHere();
|
|
69 |
String uri = uriRef.getURI();
|
|
70 |
DOMCryptoContext dcc = (DOMCryptoContext) context;
|
|
71 |
|
|
72 |
// Check if same-document URI and register ID
|
|
73 |
if (uri != null && uri.length() != 0 && uri.charAt(0) == '#') {
|
|
74 |
String id = uri.substring(1);
|
|
75 |
|
|
76 |
if (id.startsWith("xpointer(id(")) {
|
|
77 |
int i1 = id.indexOf('\'');
|
|
78 |
int i2 = id.indexOf('\'', i1+1);
|
|
79 |
id = id.substring(i1+1, i2);
|
|
80 |
}
|
|
81 |
|
|
82 |
// this is a bit of a hack to check for registered
|
|
83 |
// IDRefs and manually register them with Apache's IdResolver
|
|
84 |
// map which includes builtin schema knowledge of DSig/Enc IDs
|
1337
|
85 |
Node referencedElem = dcc.getElementById(id);
|
|
86 |
if (referencedElem != null) {
|
|
87 |
IdResolver.registerElementById((Element) referencedElem, id);
|
2
|
88 |
}
|
|
89 |
}
|
|
90 |
|
|
91 |
try {
|
|
92 |
String baseURI = context.getBaseURI();
|
|
93 |
ResourceResolver apacheResolver =
|
|
94 |
ResourceResolver.getInstance(uriAttr, baseURI);
|
|
95 |
XMLSignatureInput in = apacheResolver.resolve(uriAttr, baseURI);
|
|
96 |
if (in.isOctetStream()) {
|
|
97 |
return new ApacheOctetStreamData(in);
|
|
98 |
} else {
|
|
99 |
return new ApacheNodeSetData(in);
|
|
100 |
}
|
|
101 |
} catch (Exception e) {
|
|
102 |
throw new URIReferenceException(e);
|
|
103 |
}
|
|
104 |
}
|
|
105 |
}
|