J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | /* |
| 26 | * $Id: DOMURIDereferencer.java,v 1.19 2005/09/23 20:09:34 mullan Exp $ |
| 27 | */ |
| 28 | package org.jcp.xml.dsig.internal.dom; |
| 29 | |
| 30 | import org.w3c.dom.Attr; |
| 31 | import org.w3c.dom.Element; |
| 32 | import org.w3c.dom.Node; |
| 33 | |
| 34 | import com.sun.org.apache.xml.internal.security.Init; |
| 35 | import com.sun.org.apache.xml.internal.security.utils.IdResolver; |
| 36 | import com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolver; |
| 37 | import com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput; |
| 38 | |
| 39 | import javax.xml.crypto.*; |
| 40 | import javax.xml.crypto.dom.*; |
| 41 | import javax.xml.crypto.dsig.*; |
| 42 | |
| 43 | /** |
| 44 | * DOM-based implementation of URIDereferencer. |
| 45 | * |
| 46 | * @author Sean Mullan |
| 47 | */ |
| 48 | public class DOMURIDereferencer implements URIDereferencer { |
| 49 | |
| 50 | static final URIDereferencer INSTANCE = new DOMURIDereferencer(); |
| 51 | |
| 52 | private DOMURIDereferencer() { |
| 53 | // need to call com.sun.org.apache.xml.internal.security.Init.init() |
| 54 | // before calling any apache security code |
| 55 | Init.init(); |
| 56 | } |
| 57 | |
| 58 | public Data dereference(URIReference uriRef, XMLCryptoContext context) |
| 59 | throws URIReferenceException { |
| 60 | |
| 61 | if (uriRef == null) { |
| 62 | throw new NullPointerException("uriRef cannot be null"); |
| 63 | } |
| 64 | if (context == null) { |
| 65 | throw new NullPointerException("context cannot be null"); |
| 66 | } |
| 67 | |
| 68 | DOMURIReference domRef = (DOMURIReference) uriRef; |
| 69 | Attr uriAttr = (Attr) domRef.getHere(); |
| 70 | String uri = uriRef.getURI(); |
| 71 | DOMCryptoContext dcc = (DOMCryptoContext) context; |
| 72 | |
| 73 | // Check if same-document URI and register ID |
| 74 | if (uri != null && uri.length() != 0 && uri.charAt(0) == '#') { |
| 75 | String id = uri.substring(1); |
| 76 | |
| 77 | if (id.startsWith("xpointer(id(")) { |
| 78 | int i1 = id.indexOf('\''); |
| 79 | int i2 = id.indexOf('\'', i1+1); |
| 80 | id = id.substring(i1+1, i2); |
| 81 | } |
| 82 | |
| 83 | // this is a bit of a hack to check for registered |
| 84 | // IDRefs and manually register them with Apache's IdResolver |
| 85 | // map which includes builtin schema knowledge of DSig/Enc IDs |
| 86 | if (context instanceof XMLSignContext) { |
| 87 | Node referencedElem = dcc.getElementById(id); |
| 88 | if (referencedElem != null) { |
| 89 | IdResolver.registerElementById((Element) referencedElem, id); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | try { |
| 95 | String baseURI = context.getBaseURI(); |
| 96 | ResourceResolver apacheResolver = |
| 97 | ResourceResolver.getInstance(uriAttr, baseURI); |
| 98 | XMLSignatureInput in = apacheResolver.resolve(uriAttr, baseURI); |
| 99 | if (in.isOctetStream()) { |
| 100 | return new ApacheOctetStreamData(in); |
| 101 | } else { |
| 102 | return new ApacheNodeSetData(in); |
| 103 | } |
| 104 | } catch (Exception e) { |
| 105 | throw new URIReferenceException(e); |
| 106 | } |
| 107 | } |
| 108 | } |