API Overview API Index Package Overview Direct link to this page
JDK 1.6
  org.jcp.xml.dsig.internal.dom. DOMRetrievalMethod View Javadoc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234

/*
 * Portions copyright 2006 Sun Microsystems, Inc. All rights reserved.
 */
/*
 * =========================================================================== 
 *
 * (C) Copyright IBM Corp. 2003 All Rights Reserved.
 *
 * ===========================================================================
 */
/*
 * $Id: DOMRetrievalMethod.java,v 1.24 2005/05/12 19:28:32 mullan Exp $
 */
package org.jcp.xml.dsig.internal.dom;

import java.io.ByteArrayInputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.*;
import javax.xml.crypto.*;
import javax.xml.crypto.dsig.*;
import javax.xml.crypto.dom.DOMCryptoContext;
import javax.xml.crypto.dom.DOMURIReference;
import javax.xml.crypto.dsig.keyinfo.RetrievalMethod;
import javax.xml.parsers.*;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput;

/**
 * DOM-based implementation of RetrievalMethod.
 *
 * @author Sean Mullan
 * @author Joyce Leung
 */
public final class DOMRetrievalMethod extends DOMStructure
    implements RetrievalMethod, DOMURIReference {

    private final List transforms;
    private String uri;
    private String type;
    private Attr here;

    /**
     * Creates a <code>DOMRetrievalMethod</code> containing the specified 
     * URIReference and List of Transforms.
     *
     * @param uri the URI
     * @param type the type
     * @param transforms a list of {@link Transform}s. The list is defensively
     *    copied to prevent subsequent modification. May be <code>null</code>
     *    or empty.
     * @throws IllegalArgumentException if the format of <code>uri</code> is 
     *    invalid, as specified by Reference's URI attribute in the W3C
     *    specification for XML-Signature Syntax and Processing
     * @throws NullPointerException if <code>uriReference</code>
     *    is <code>null</code> 
     * @throws ClassCastException if <code>transforms</code> contains any
     *    entries that are not of type {@link Transform}
     */
    public DOMRetrievalMethod(String uri, String type, List transforms) {
	if (uri == null) {
	    throw new NullPointerException("uri cannot be null");
	}
        if (transforms == null || transforms.isEmpty()) {
            this.transforms = Collections.EMPTY_LIST;
        } else {
            List transformsCopy = new ArrayList(transforms);
            for (int i = 0, size = transformsCopy.size(); i < size; i++) {
                if (!(transformsCopy.get(i) instanceof Transform)) {
                    throw new ClassCastException
                        ("transforms["+i+"] is not a valid type");
                }
            }
            this.transforms = Collections.unmodifiableList(transformsCopy);
        }
	this.uri = uri;
        if ((uri != null) && (!uri.equals(""))) {
            try {
                new URI(uri);
            } catch (URISyntaxException e) {
                throw new IllegalArgumentException(e.getMessage());
            }
        }

	this.type = type;
    }
	
    /**
     * Creates a <code>DOMRetrievalMethod</code> from an element.
     *
     * @param rmElem a RetrievalMethod element
     */
    public DOMRetrievalMethod(Element rmElem, XMLCryptoContext context) 
	throws MarshalException {
        // get URI and Type attributes
        uri = DOMUtils.getAttributeValue(rmElem, "URI");
        type = DOMUtils.getAttributeValue(rmElem, "Type");

	// get here node
	here = rmElem.getAttributeNodeNS(null, "URI");

        // get Transforms, if specified
        List transforms = new ArrayList();
        Element transformsElem = DOMUtils.getFirstChildElement(rmElem);
        if (transformsElem != null) {
            Element transformElem = 
		DOMUtils.getFirstChildElement(transformsElem);
            while (transformElem != null) {
                transforms.add(new DOMTransform(transformElem, context));
                transformElem = DOMUtils.getNextSiblingElement(transformElem);
	    }
        }
	if (transforms.isEmpty()) {
            this.transforms = Collections.EMPTY_LIST;
	} else {
            this.transforms = Collections.unmodifiableList(transforms);
	}
    }

    public String getURI() {
	return uri;
    }

    public String getType() {
	return type;
    }

    public List getTransforms() {
	return transforms;
    }

    public void marshal(Node parent, String dsPrefix, DOMCryptoContext context)
	throws MarshalException {
        Document ownerDoc = DOMUtils.getOwnerDocument(parent);

        Element rmElem = DOMUtils.createElement
            (ownerDoc, "RetrievalMethod", XMLSignature.XMLNS, dsPrefix);

        // add URI and Type attributes
        DOMUtils.setAttribute(rmElem, "URI", uri);
        DOMUtils.setAttribute(rmElem, "Type", type);

        // add Transforms elements
	if (!transforms.isEmpty()) {
	    Element transformsElem = DOMUtils.createElement
                (ownerDoc, "Transforms", XMLSignature.XMLNS, dsPrefix);
	    rmElem.appendChild(transformsElem);
	    for (int i = 0, size = transforms.size(); i < size; i++) {
	        ((DOMTransform) transforms.get(i)).marshal
		    (transformsElem, dsPrefix, context);
            }
	}

        parent.appendChild(rmElem);

	// save here node
	here = rmElem.getAttributeNodeNS(null, "URI");
    }

    public Node getHere() {
	return here;
    }

    public Data dereference(XMLCryptoContext context)
        throws URIReferenceException {

	if (context == null) {
	    throw new NullPointerException("context cannot be null");
	}

	/*
         * If URIDereferencer is specified in context; use it, otherwise use 
	 * built-in.
	 */
        URIDereferencer deref = context.getURIDereferencer();
        if (deref == null) {
	    deref = DOMURIDereferencer.INSTANCE;
	}

	Data data = deref.dereference(this, context);

        // pass dereferenced data through Transforms
	try {
	    for (int i = 0, size = transforms.size(); i < size; i++) {
                Transform transform = (Transform) transforms.get(i);
                data = ((DOMTransform) transform).transform(data, context);
            }
	} catch (Exception e) {
	    throw new URIReferenceException(e);
	}
	return data;
    }

    public XMLStructure dereferenceAsXMLStructure(XMLCryptoContext context)
	throws URIReferenceException {

	try {
	    ApacheData data = (ApacheData) dereference(context);
	    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
	    dbf.setNamespaceAware(true);
	    DocumentBuilder db = dbf.newDocumentBuilder();
	    Document doc = db.parse(new ByteArrayInputStream
		(data.getXMLSignatureInput().getBytes()));
	    Element kiElem = doc.getDocumentElement();
            if (kiElem.getLocalName().equals("X509Data")) {
		return new DOMX509Data(kiElem);
	    } else {
		return null; // unsupported
	    }
	} catch (Exception e) {
	    throw new URIReferenceException(e);
	}
    }

    public boolean equals(Object obj) {
	if (this == obj) {
            return true;
	}
        if (!(obj instanceof RetrievalMethod)) {
            return false;
	}
        RetrievalMethod orm = (RetrievalMethod) obj;

	boolean typesEqual = (type == null ? orm.getType() == null :
            type.equals(orm.getType()));

	return (uri.equals(orm.getURI()) && 
	    transforms.equals(orm.getTransforms()) && typesEqual);
    }
}

Generated By: JavaOnTracks Doclet 0.1.4     ©Thibaut Colar