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