jdk/src/java.xml.crypto/share/classes/javax/xml/crypto/dsig/TransformService.java
author wetmore
Tue, 20 Jun 2017 12:57:42 -0700
changeset 45665 6f21cd7ec80e
parent 42991 174ff3aebaf7
permissions -rw-r--r--
8178114: Fix guide links in security APIs Reviewed-by: xuelei, mullan, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
42991
174ff3aebaf7 8172003: getInstance() with unknown provider throws NPE
apetcher
parents: 39827
diff changeset
     2
 * Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
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
 * $Id: TransformService.java,v 1.6.4.1 2005/09/15 12:42:11 mullan Exp $
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
package javax.xml.crypto.dsig;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.InvalidAlgorithmParameterException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.NoSuchAlgorithmException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.NoSuchProviderException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.Provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.Provider.Service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.Security;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.xml.crypto.MarshalException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.xml.crypto.XMLStructure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.xml.crypto.XMLCryptoContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.xml.crypto.dsig.spec.TransformParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * A Service Provider Interface for transform and canonicalization algorithms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p>Each instance of <code>TransformService</code> supports a specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * transform or canonicalization algorithm and XML mechanism type. To create a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <code>TransformService</code>, call one of the static
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * {@link #getInstance getInstance} methods, passing in the algorithm URI and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * XML mechanism type desired, for example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <blockquote><code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * TransformService ts = TransformService.getInstance(Transform.XPATH2, "DOM");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * </code></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <p><code>TransformService</code> implementations are registered and loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * using the {@link java.security.Provider} mechanism.  Each
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <code>TransformService</code> service provider implementation should include
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * a <code>MechanismType</code> service attribute that identifies the XML
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * mechanism type that it supports. If the attribute is not specified,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * "DOM" is assumed. For example, a service provider that supports the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * XPath Filter 2 Transform and DOM mechanism would be specified in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <code>Provider</code> subclass as:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *     put("TransformService." + Transform.XPATH2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *         "org.example.XPath2TransformService");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *     put("TransformService." + Transform.XPATH2 + " MechanismType", "DOM");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * <code>TransformService</code> implementations that support the DOM
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * mechanism type must abide by the DOM interoperability requirements defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * in the
45665
6f21cd7ec80e 8178114: Fix guide links in security APIs
wetmore
parents: 42991
diff changeset
    72
 * {@extLink security_guide_xmldsig_rqmts DOM Mechanism Requirements} section
6f21cd7ec80e 8178114: Fix guide links in security APIs
wetmore
parents: 42991
diff changeset
    73
 * of the API overview. See the
6f21cd7ec80e 8178114: Fix guide links in security APIs
wetmore
parents: 42991
diff changeset
    74
 * {@extLink security_guide_xmldsig_provider Service Providers} section of
6f21cd7ec80e 8178114: Fix guide links in security APIs
wetmore
parents: 42991
diff changeset
    75
 * the API overview for a list of standard mechanism types.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * Once a <code>TransformService</code> has been created, it can be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * to process <code>Transform</code> or <code>CanonicalizationMethod</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * objects. If the <code>Transform</code> or <code>CanonicalizationMethod</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * exists in XML form (for example, when validating an existing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * <code>XMLSignature</code>), the {@link #init(XMLStructure, XMLCryptoContext)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * method must be first called to initialize the transform and provide document
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * context (even if there are no parameters). Alternatively, if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * <code>Transform</code> or <code>CanonicalizationMethod</code> is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * created from scratch, the {@link #init(TransformParameterSpec)} method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * is called to initialize the transform with parameters and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * {@link #marshalParams marshalParams} method is called to marshal the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * parameters to XML and provide the transform with document context. Finally,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * the {@link #transform transform} method is called to perform the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * transformation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * <b>Concurrent Access</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * <p>The static methods of this class are guaranteed to be thread-safe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * Multiple threads may concurrently invoke the static methods defined in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * class with no ill effects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * <p>However, this is not true for the non-static methods defined by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * class. Unless otherwise documented by a specific provider, threads that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * need to access a single <code>TransformService</code> instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * concurrently should synchronize amongst themselves and provide the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * necessary locking. Multiple threads each manipulating a different
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * <code>TransformService</code> instance need not synchronize.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * @author Sean Mullan
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * @author JSR 105 Expert Group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
public abstract class TransformService implements Transform {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private String algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    private String mechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private Provider provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * Default constructor, for invocation by subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    protected TransformService() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Returns a <code>TransformService</code> that supports the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * algorithm URI (ex: {@link Transform#XPATH2}) and mechanism type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * (ex: DOM).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * <p>This method uses the standard JCA provider lookup mechanism to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * locate and instantiate a <code>TransformService</code> implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * of the desired algorithm and <code>MechanismType</code> service
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * attribute. It traverses the list of registered security
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * <code>Provider</code>s, starting with the most preferred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * <code>Provider</code>. A new <code>TransformService</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * from the first <code>Provider</code> that supports the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * algorithm and mechanism type is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
33241
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 25859
diff changeset
   136
     * @implNote
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 25859
diff changeset
   137
     * The JDK Reference Implementation additionally uses the
37348
9ccec3170d5e 8152205: jdk.security.provider.preferred is ambiguously documented
ascarpino
parents: 33241
diff changeset
   138
     * {@code jdk.security.provider.preferred}
9ccec3170d5e 8152205: jdk.security.provider.preferred is ambiguously documented
ascarpino
parents: 33241
diff changeset
   139
     * {@link Security#getProperty(String) Security} property to determine
33241
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 25859
diff changeset
   140
     * the preferred provider order for the specified algorithm. This
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 25859
diff changeset
   141
     * may be different than the order of providers returned by
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 25859
diff changeset
   142
     * {@link Security#getProviders() Security.getProviders()}.
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 25859
diff changeset
   143
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @param algorithm the URI of the algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @param mechanismType the type of the XML processing mechanism and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *   representation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @return a new <code>TransformService</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @throws NullPointerException if <code>algorithm</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *   <code>mechanismType</code> is  <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @throws NoSuchAlgorithmException if no <code>Provider</code> supports a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *   <code>TransformService</code> implementation for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *   algorithm and mechanism type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @see Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    public static TransformService getInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        (String algorithm, String mechanismType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (mechanismType == null || algorithm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        boolean dom = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if (mechanismType.equals("DOM")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            dom = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
39827
21a9b8df49f5 8159488: Deprivilege java.xml.crypto
valeriep
parents: 37348
diff changeset
   165
21a9b8df49f5 8159488: Deprivilege java.xml.crypto
valeriep
parents: 37348
diff changeset
   166
        Provider[] provs = Security.getProviders();
21a9b8df49f5 8159488: Deprivilege java.xml.crypto
valeriep
parents: 37348
diff changeset
   167
        for (Provider p : provs) {
21a9b8df49f5 8159488: Deprivilege java.xml.crypto
valeriep
parents: 37348
diff changeset
   168
            Service s = p.getService("TransformService", algorithm);
21a9b8df49f5 8159488: Deprivilege java.xml.crypto
valeriep
parents: 37348
diff changeset
   169
            if (s != null) {
21a9b8df49f5 8159488: Deprivilege java.xml.crypto
valeriep
parents: 37348
diff changeset
   170
                String value = s.getAttribute("MechanismType");
21a9b8df49f5 8159488: Deprivilege java.xml.crypto
valeriep
parents: 37348
diff changeset
   171
                if ((value == null && dom) ||
21a9b8df49f5 8159488: Deprivilege java.xml.crypto
valeriep
parents: 37348
diff changeset
   172
                    (value != null && value.equals(mechanismType))) {
21a9b8df49f5 8159488: Deprivilege java.xml.crypto
valeriep
parents: 37348
diff changeset
   173
                    Object obj = s.newInstance(null);
21a9b8df49f5 8159488: Deprivilege java.xml.crypto
valeriep
parents: 37348
diff changeset
   174
                    if (obj instanceof TransformService) {
21a9b8df49f5 8159488: Deprivilege java.xml.crypto
valeriep
parents: 37348
diff changeset
   175
                        TransformService ts = (TransformService) obj;
21a9b8df49f5 8159488: Deprivilege java.xml.crypto
valeriep
parents: 37348
diff changeset
   176
                        ts.algorithm = algorithm;
21a9b8df49f5 8159488: Deprivilege java.xml.crypto
valeriep
parents: 37348
diff changeset
   177
                        ts.mechanism = mechanismType;
21a9b8df49f5 8159488: Deprivilege java.xml.crypto
valeriep
parents: 37348
diff changeset
   178
                        ts.provider = p;
21a9b8df49f5 8159488: Deprivilege java.xml.crypto
valeriep
parents: 37348
diff changeset
   179
                        return ts;
21a9b8df49f5 8159488: Deprivilege java.xml.crypto
valeriep
parents: 37348
diff changeset
   180
                    }
21a9b8df49f5 8159488: Deprivilege java.xml.crypto
valeriep
parents: 37348
diff changeset
   181
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        throw new NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            (algorithm + " algorithm and " + mechanismType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                 + " mechanism not available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * Returns a <code>TransformService</code> that supports the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * algorithm URI (ex: {@link Transform#XPATH2}) and mechanism type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * (ex: DOM) as supplied by the specified provider. Note that the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * <code>Provider</code> object does not have to be registered in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @param algorithm the URI of the algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @param mechanismType the type of the XML processing mechanism and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *   representation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @param provider the <code>Provider</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @return a new <code>TransformService</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @throws NullPointerException if <code>provider</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *   <code>algorithm</code>, or <code>mechanismType</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *   <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @throws NoSuchAlgorithmException if a <code>TransformService</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     *   implementation for the specified algorithm and mechanism type is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *   available from the specified <code>Provider</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @see Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    public static TransformService getInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        (String algorithm, String mechanismType, Provider provider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        if (mechanismType == null || algorithm == null || provider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        boolean dom = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        if (mechanismType.equals("DOM")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            dom = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
39827
21a9b8df49f5 8159488: Deprivilege java.xml.crypto
valeriep
parents: 37348
diff changeset
   220
        Service s = provider.getService("TransformService", algorithm);
21a9b8df49f5 8159488: Deprivilege java.xml.crypto
valeriep
parents: 37348
diff changeset
   221
        if (s != null) {
21a9b8df49f5 8159488: Deprivilege java.xml.crypto
valeriep
parents: 37348
diff changeset
   222
            String value = s.getAttribute("MechanismType");
21a9b8df49f5 8159488: Deprivilege java.xml.crypto
valeriep
parents: 37348
diff changeset
   223
            if ((value == null && dom) ||
21a9b8df49f5 8159488: Deprivilege java.xml.crypto
valeriep
parents: 37348
diff changeset
   224
                (value != null && value.equals(mechanismType))) {
21a9b8df49f5 8159488: Deprivilege java.xml.crypto
valeriep
parents: 37348
diff changeset
   225
                Object obj = s.newInstance(null);
21a9b8df49f5 8159488: Deprivilege java.xml.crypto
valeriep
parents: 37348
diff changeset
   226
                if (obj instanceof TransformService) {
21a9b8df49f5 8159488: Deprivilege java.xml.crypto
valeriep
parents: 37348
diff changeset
   227
                    TransformService ts = (TransformService) obj;
21a9b8df49f5 8159488: Deprivilege java.xml.crypto
valeriep
parents: 37348
diff changeset
   228
                    ts.algorithm = algorithm;
21a9b8df49f5 8159488: Deprivilege java.xml.crypto
valeriep
parents: 37348
diff changeset
   229
                    ts.mechanism = mechanismType;
21a9b8df49f5 8159488: Deprivilege java.xml.crypto
valeriep
parents: 37348
diff changeset
   230
                    ts.provider = provider;
21a9b8df49f5 8159488: Deprivilege java.xml.crypto
valeriep
parents: 37348
diff changeset
   231
                    return ts;
21a9b8df49f5 8159488: Deprivilege java.xml.crypto
valeriep
parents: 37348
diff changeset
   232
                }
21a9b8df49f5 8159488: Deprivilege java.xml.crypto
valeriep
parents: 37348
diff changeset
   233
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        throw new NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            (algorithm + " algorithm and " + mechanismType
39827
21a9b8df49f5 8159488: Deprivilege java.xml.crypto
valeriep
parents: 37348
diff changeset
   237
                 + " mechanism not available from " + provider.getName());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * Returns a <code>TransformService</code> that supports the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * algorithm URI (ex: {@link Transform#XPATH2}) and mechanism type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * (ex: DOM) as supplied by the specified provider. The specified provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * must be registered in the security provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * <p>Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @param algorithm the URI of the algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @param mechanismType the type of the XML processing mechanism and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *   representation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * @param provider the string name of the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @return a new <code>TransformService</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @throws NoSuchProviderException if the specified provider is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *   registered in the security provider list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @throws NullPointerException if <code>provider</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *   <code>mechanismType</code>, or <code>algorithm</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *   <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @throws NoSuchAlgorithmException if a <code>TransformService</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     *   implementation for the specified algorithm and mechanism type is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *   available from the specified provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @see Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    public static TransformService getInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        (String algorithm, String mechanismType, String provider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        throws NoSuchAlgorithmException, NoSuchProviderException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        if (mechanismType == null || algorithm == null || provider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        } else if (provider.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            throw new NoSuchProviderException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        boolean dom = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        if (mechanismType.equals("DOM")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            dom = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
39827
21a9b8df49f5 8159488: Deprivilege java.xml.crypto
valeriep
parents: 37348
diff changeset
   276
        Provider p = Security.getProvider(provider);
42991
174ff3aebaf7 8172003: getInstance() with unknown provider throws NPE
apetcher
parents: 39827
diff changeset
   277
        if (p == null) {
174ff3aebaf7 8172003: getInstance() with unknown provider throws NPE
apetcher
parents: 39827
diff changeset
   278
            throw new NoSuchProviderException("No such provider: " +
174ff3aebaf7 8172003: getInstance() with unknown provider throws NPE
apetcher
parents: 39827
diff changeset
   279
                                              provider);
174ff3aebaf7 8172003: getInstance() with unknown provider throws NPE
apetcher
parents: 39827
diff changeset
   280
        }
39827
21a9b8df49f5 8159488: Deprivilege java.xml.crypto
valeriep
parents: 37348
diff changeset
   281
        Service s = p.getService("TransformService", algorithm);
21a9b8df49f5 8159488: Deprivilege java.xml.crypto
valeriep
parents: 37348
diff changeset
   282
        if (s != null) {
21a9b8df49f5 8159488: Deprivilege java.xml.crypto
valeriep
parents: 37348
diff changeset
   283
            String value = s.getAttribute("MechanismType");
21a9b8df49f5 8159488: Deprivilege java.xml.crypto
valeriep
parents: 37348
diff changeset
   284
            if ((value == null && dom) ||
21a9b8df49f5 8159488: Deprivilege java.xml.crypto
valeriep
parents: 37348
diff changeset
   285
                (value != null && value.equals(mechanismType))) {
21a9b8df49f5 8159488: Deprivilege java.xml.crypto
valeriep
parents: 37348
diff changeset
   286
                Object obj = s.newInstance(null);
21a9b8df49f5 8159488: Deprivilege java.xml.crypto
valeriep
parents: 37348
diff changeset
   287
                if (obj instanceof TransformService) {
21a9b8df49f5 8159488: Deprivilege java.xml.crypto
valeriep
parents: 37348
diff changeset
   288
                    TransformService ts = (TransformService) obj;
21a9b8df49f5 8159488: Deprivilege java.xml.crypto
valeriep
parents: 37348
diff changeset
   289
                    ts.algorithm = algorithm;
21a9b8df49f5 8159488: Deprivilege java.xml.crypto
valeriep
parents: 37348
diff changeset
   290
                    ts.mechanism = mechanismType;
21a9b8df49f5 8159488: Deprivilege java.xml.crypto
valeriep
parents: 37348
diff changeset
   291
                    ts.provider = p;
21a9b8df49f5 8159488: Deprivilege java.xml.crypto
valeriep
parents: 37348
diff changeset
   292
                    return ts;
21a9b8df49f5 8159488: Deprivilege java.xml.crypto
valeriep
parents: 37348
diff changeset
   293
                }
21a9b8df49f5 8159488: Deprivilege java.xml.crypto
valeriep
parents: 37348
diff changeset
   294
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        throw new NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            (algorithm + " algorithm and " + mechanismType
39827
21a9b8df49f5 8159488: Deprivilege java.xml.crypto
valeriep
parents: 37348
diff changeset
   298
                 + " mechanism not available from " + provider);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
11276
6a7de6dddc18 7118546: fix warnings in javax.xml.crypto, javax.script
smarks
parents: 5506
diff changeset
   301
    private static class MechanismMapEntry implements Map.Entry<String,String> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        private final String mechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        private final String algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        private final String key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        MechanismMapEntry(String algorithm, String mechanism) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            this.algorithm = algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            this.mechanism = mechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            this.key = "TransformService." + algorithm + " MechanismType";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            if (!(o instanceof Map.Entry)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            }
11276
6a7de6dddc18 7118546: fix warnings in javax.xml.crypto, javax.script
smarks
parents: 5506
diff changeset
   314
            Map.Entry<?,?> e = (Map.Entry<?,?>) o;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            return (getKey()==null ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                    e.getKey()==null : getKey().equals(e.getKey())) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                   (getValue()==null ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                    e.getValue()==null : getValue().equals(e.getValue()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        }
11276
6a7de6dddc18 7118546: fix warnings in javax.xml.crypto, javax.script
smarks
parents: 5506
diff changeset
   320
        public String getKey() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        }
11276
6a7de6dddc18 7118546: fix warnings in javax.xml.crypto, javax.script
smarks
parents: 5506
diff changeset
   323
        public String getValue() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            return mechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
11276
6a7de6dddc18 7118546: fix warnings in javax.xml.crypto, javax.script
smarks
parents: 5506
diff changeset
   326
        public String setValue(String value) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            return (getKey()==null ? 0 : getKey().hashCode()) ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                   (getValue()==null ? 0 : getValue().hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * Returns the mechanism type supported by this <code>TransformService</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * @return the mechanism type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    public final String getMechanismType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        return mechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * Returns the URI of the algorithm supported by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * <code>TransformService</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @return the algorithm URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    public final String getAlgorithm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        return algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * Returns the provider of this <code>TransformService</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * @return the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    public final Provider getProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        return provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * Initializes this <code>TransformService</code> with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * <p>If the parameters exist in XML form, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * {@link #init(XMLStructure, XMLCryptoContext)} method should be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * initialize the <code>TransformService</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * @param params the algorithm parameters (may be <code>null</code> if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *   not required or optional)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * @throws InvalidAlgorithmParameterException if the specified parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     *   are invalid for this algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    public abstract void init(TransformParameterSpec params)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        throws InvalidAlgorithmParameterException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * Marshals the algorithm-specific parameters. If there are no parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * to be marshalled, this method returns without throwing an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * @param parent a mechanism-specific structure containing the parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     *    node that the marshalled parameters should be appended to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * @param context the <code>XMLCryptoContext</code> containing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     *    additional context (may be <code>null</code> if not applicable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * @throws ClassCastException if the type of <code>parent</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     *    <code>context</code> is not compatible with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     *    <code>TransformService</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * @throws NullPointerException if <code>parent</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * @throws MarshalException if the parameters cannot be marshalled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    public abstract void marshalParams
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        (XMLStructure parent, XMLCryptoContext context)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        throws MarshalException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * Initializes this <code>TransformService</code> with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * parameters and document context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * @param parent a mechanism-specific structure containing the parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     *    structure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * @param context the <code>XMLCryptoContext</code> containing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     *    additional context (may be <code>null</code> if not applicable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * @throws ClassCastException if the type of <code>parent</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     *    <code>context</code> is not compatible with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     *    <code>TransformService</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * @throws NullPointerException if <code>parent</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * @throws InvalidAlgorithmParameterException if the specified parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     *   are invalid for this algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    public abstract void init(XMLStructure parent, XMLCryptoContext context)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        throws InvalidAlgorithmParameterException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
}