001: /*
002: * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025: /*
026: * $Id: DOMURIDereferencer.java,v 1.19 2005/09/23 20:09:34 mullan Exp $
027: */
028: package org.jcp.xml.dsig.internal.dom;
029:
030: import org.w3c.dom.Attr;
031: import org.w3c.dom.Element;
032: import org.w3c.dom.Node;
033:
034: import com.sun.org.apache.xml.internal.security.Init;
035: import com.sun.org.apache.xml.internal.security.utils.IdResolver;
036: import com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolver;
037: import com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput;
038:
039: import javax.xml.crypto.*;
040: import javax.xml.crypto.dom.*;
041: import javax.xml.crypto.dsig.*;
042:
043: /**
044: * DOM-based implementation of URIDereferencer.
045: *
046: * @author Sean Mullan
047: */
048: public class DOMURIDereferencer implements URIDereferencer {
049:
050: static final URIDereferencer INSTANCE = new DOMURIDereferencer();
051:
052: private DOMURIDereferencer() {
053: // need to call com.sun.org.apache.xml.internal.security.Init.init()
054: // before calling any apache security code
055: Init.init();
056: }
057:
058: public Data dereference(URIReference uriRef,
059: XMLCryptoContext context) throws URIReferenceException {
060:
061: if (uriRef == null) {
062: throw new NullPointerException("uriRef cannot be null");
063: }
064: if (context == null) {
065: throw new NullPointerException("context cannot be null");
066: }
067:
068: DOMURIReference domRef = (DOMURIReference) uriRef;
069: Attr uriAttr = (Attr) domRef.getHere();
070: String uri = uriRef.getURI();
071: DOMCryptoContext dcc = (DOMCryptoContext) context;
072:
073: // Check if same-document URI and register ID
074: if (uri != null && uri.length() != 0 && uri.charAt(0) == '#') {
075: String id = uri.substring(1);
076:
077: if (id.startsWith("xpointer(id(")) {
078: int i1 = id.indexOf('\'');
079: int i2 = id.indexOf('\'', i1 + 1);
080: id = id.substring(i1 + 1, i2);
081: }
082:
083: // this is a bit of a hack to check for registered
084: // IDRefs and manually register them with Apache's IdResolver
085: // map which includes builtin schema knowledge of DSig/Enc IDs
086: if (context instanceof XMLSignContext) {
087: Node referencedElem = dcc.getElementById(id);
088: if (referencedElem != null) {
089: IdResolver.registerElementById(
090: (Element) referencedElem, id);
091: }
092: }
093: }
094:
095: try {
096: String baseURI = context.getBaseURI();
097: ResourceResolver apacheResolver = ResourceResolver
098: .getInstance(uriAttr, baseURI);
099: XMLSignatureInput in = apacheResolver.resolve(uriAttr,
100: baseURI);
101: if (in.isOctetStream()) {
102: return new ApacheOctetStreamData(in);
103: } else {
104: return new ApacheNodeSetData(in);
105: }
106: } catch (Exception e) {
107: throw new URIReferenceException(e);
108: }
109: }
110: }
|