jdk/src/share/classes/org/jcp/xml/dsig/internal/dom/DOMRetrievalMethod.java
author mullan
Mon, 22 Apr 2013 11:23:33 +0100
changeset 18240 cda839ac048f
parent 5506 202f599c92aa
child 18266 26e69da689b9
permissions -rw-r--r--
6741606: Integrate Apache Santuario Reviewed-by: vinnie, hawtin
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!
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
     4
 */
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
     5
/*
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
     6
 * Copyright 2005 The Apache Software Foundation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 *
1337
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
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 *
1337
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
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 *
1337
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.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 */
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    21
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1337
diff changeset
    22
 * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    23
 */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * ===========================================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * (C) Copyright IBM Corp. 2003 All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * ===========================================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/*
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    32
 * $Id: DOMRetrievalMethod.java,v 1.2 2008/07/24 15:20:32 mullan Exp $
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
package org.jcp.xml.dsig.internal.dom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.ByteArrayInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.net.URI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.net.URISyntaxException;
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    39
import java.security.Provider;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.*;
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
    41
import javax.xml.XMLConstants;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.xml.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.xml.crypto.dsig.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.xml.crypto.dom.DOMCryptoContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.xml.crypto.dom.DOMURIReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.xml.crypto.dsig.keyinfo.RetrievalMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.xml.parsers.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import org.w3c.dom.Attr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import org.w3c.dom.Document;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import org.w3c.dom.Element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import org.w3c.dom.Node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * DOM-based implementation of RetrievalMethod.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @author Sean Mullan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @author Joyce Leung
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
public final class DOMRetrievalMethod extends DOMStructure
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    implements RetrievalMethod, DOMURIReference {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private final List transforms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private String uri;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private String type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private Attr here;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Creates a <code>DOMRetrievalMethod</code> containing the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * URIReference and List of Transforms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * @param uri the URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * @param type the type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @param transforms a list of {@link Transform}s. The list is defensively
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *    copied to prevent subsequent modification. May be <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     *    or empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * @throws IllegalArgumentException if the format of <code>uri</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     *    invalid, as specified by Reference's URI attribute in the W3C
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *    specification for XML-Signature Syntax and Processing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @throws NullPointerException if <code>uriReference</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *    is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @throws ClassCastException if <code>transforms</code> contains any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *    entries that are not of type {@link Transform}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public DOMRetrievalMethod(String uri, String type, List transforms) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        if (uri == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            throw new NullPointerException("uri cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if (transforms == null || transforms.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            this.transforms = Collections.EMPTY_LIST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            List transformsCopy = new ArrayList(transforms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            for (int i = 0, size = transformsCopy.size(); i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                if (!(transformsCopy.get(i) instanceof Transform)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                    throw new ClassCastException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                        ("transforms["+i+"] is not a valid type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            this.transforms = Collections.unmodifiableList(transformsCopy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        this.uri = uri;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        if ((uri != null) && (!uri.equals(""))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                new URI(uri);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            } catch (URISyntaxException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                throw new IllegalArgumentException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * Creates a <code>DOMRetrievalMethod</code> from an element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @param rmElem a RetrievalMethod element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   119
    public DOMRetrievalMethod(Element rmElem, XMLCryptoContext context,
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   120
        Provider provider) throws MarshalException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        // get URI and Type attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        uri = DOMUtils.getAttributeValue(rmElem, "URI");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        type = DOMUtils.getAttributeValue(rmElem, "Type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        // get here node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        here = rmElem.getAttributeNodeNS(null, "URI");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   128
        boolean secVal = Utils.secureValidation(context);
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   129
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        // get Transforms, if specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        List transforms = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        Element transformsElem = DOMUtils.getFirstChildElement(rmElem);
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   133
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   134
        int transformCount = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if (transformsElem != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            Element transformElem =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                DOMUtils.getFirstChildElement(transformsElem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            while (transformElem != null) {
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   139
                transforms.add
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   140
                    (new DOMTransform(transformElem, context, provider));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                transformElem = DOMUtils.getNextSiblingElement(transformElem);
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   142
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   143
                transformCount++;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   144
                if (secVal &&
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   145
                    (transformCount > DOMReference.MAXIMUM_TRANSFORM_COUNT))
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   146
                {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   147
                    String error = "A maxiumum of " +
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   148
                                   DOMReference.MAXIMUM_TRANSFORM_COUNT +
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   149
                                   " transforms per Reference are allowed" +
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   150
                                   " with secure validation";
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   151
                    throw new MarshalException(error);
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   152
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if (transforms.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            this.transforms = Collections.EMPTY_LIST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            this.transforms = Collections.unmodifiableList(transforms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public String getURI() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        return uri;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    public String getType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        return type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public List getTransforms() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return transforms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public void marshal(Node parent, String dsPrefix, DOMCryptoContext context)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        throws MarshalException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        Document ownerDoc = DOMUtils.getOwnerDocument(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        Element rmElem = DOMUtils.createElement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            (ownerDoc, "RetrievalMethod", XMLSignature.XMLNS, dsPrefix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        // add URI and Type attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        DOMUtils.setAttribute(rmElem, "URI", uri);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        DOMUtils.setAttribute(rmElem, "Type", type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        // add Transforms elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        if (!transforms.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            Element transformsElem = DOMUtils.createElement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                (ownerDoc, "Transforms", XMLSignature.XMLNS, dsPrefix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            rmElem.appendChild(transformsElem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            for (int i = 0, size = transforms.size(); i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                ((DOMTransform) transforms.get(i)).marshal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                    (transformsElem, dsPrefix, context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        parent.appendChild(rmElem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        // save here node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        here = rmElem.getAttributeNodeNS(null, "URI");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    public Node getHere() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        return here;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    public Data dereference(XMLCryptoContext context)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        throws URIReferenceException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        if (context == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            throw new NullPointerException("context cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
         * If URIDereferencer is specified in context; use it, otherwise use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
         * built-in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        URIDereferencer deref = context.getURIDereferencer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        if (deref == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            deref = DOMURIDereferencer.INSTANCE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        Data data = deref.dereference(this, context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        // pass dereferenced data through Transforms
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            for (int i = 0, size = transforms.size(); i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                Transform transform = (Transform) transforms.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                data = ((DOMTransform) transform).transform(data, context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            throw new URIReferenceException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        return data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    public XMLStructure dereferenceAsXMLStructure(XMLCryptoContext context)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        throws URIReferenceException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            ApacheData data = (ApacheData) dereference(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            dbf.setNamespaceAware(true);
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   243
            dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING,
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   244
                           Boolean.TRUE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            DocumentBuilder db = dbf.newDocumentBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            Document doc = db.parse(new ByteArrayInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                (data.getXMLSignatureInput().getBytes()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            Element kiElem = doc.getDocumentElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            if (kiElem.getLocalName().equals("X509Data")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                return new DOMX509Data(kiElem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                return null; // unsupported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            throw new URIReferenceException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        if (this == obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        if (!(obj instanceof RetrievalMethod)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        RetrievalMethod orm = (RetrievalMethod) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        boolean typesEqual = (type == null ? orm.getType() == null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            type.equals(orm.getType()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        return (uri.equals(orm.getURI()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            transforms.equals(orm.getTransforms()) && typesEqual);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
}