View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied. See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.ws.security.message;
21  
22  import org.apache.ws.security.WSSConfig;
23  import org.apache.ws.security.message.token.SignatureConfirmation;
24  import org.apache.ws.security.util.WSSecurityUtil;
25  import org.w3c.dom.Document;
26  import org.w3c.dom.Element;
27  
28  /**
29   * Builds a WS SignatureConfirmation and inserts it into the SOAP Envelope.
30   * 
31   * @author Werner Dittmann (Werner.Dittmann@t-online.de).
32   */
33  
34  public class WSSecSignatureConfirmation extends WSSecBase {
35      private static org.apache.commons.logging.Log log = 
36          org.apache.commons.logging.LogFactory.getLog(WSSecSignatureConfirmation.class);
37  
38      private SignatureConfirmation sc = null;
39  
40      private byte[] signatureValue = null;
41  
42      public WSSecSignatureConfirmation() {
43          super();
44      }
45      public WSSecSignatureConfirmation(WSSConfig config) {
46          super(config);
47      }
48      
49      /**
50       * Set the Signature value to store in this SignatureConfirmation.
51       * 
52       * @param signatureValue The Signature value to store in the SignatureConfirmation element
53       */
54      public void setSignatureValue(byte[] signatureValue) {
55          this.signatureValue = signatureValue;
56      }
57  
58  
59      /**
60       * Creates a SignatureConfimation element.
61       * 
62       * The method prepares and initializes a WSSec SignatureConfirmation structure after
63       * the relevant information was set. Before calling <code>prepare()</code> the
64       * filed <code>signatureValue</code> must be set
65       * 
66       * @param doc The SOAP envelope as W3C document
67       */
68      public void prepare(Document doc) {
69          sc = new SignatureConfirmation(doc, signatureValue);
70          sc.setID(getWsConfig().getIdAllocator().createId("SC-", sc));
71      }
72      
73      /**
74       * Prepends the SignatureConfirmation element to the elements already in the
75       * Security header.
76       * 
77       * The method can be called any time after <code>prepare()</code>.
78       * This allows to insert the SignatureConfirmation element at any position in the
79       * Security header.
80       * 
81       * @param secHeader The security header that holds the Signature element.
82       */
83      public void prependToHeader(WSSecHeader secHeader) {
84          WSSecurityUtil.prependChildElement(secHeader.getSecurityHeader(), sc.getElement());
85      }
86      
87      /**
88       * Adds a new <code>SignatureConfirmation</code> to a soap envelope.
89       * 
90       * A complete <code>SignatureConfirmation</code> is constructed and added
91       * to the <code>wsse:Security</code> header.
92       * 
93       * @param doc The SOAP envelope as W3C document
94       * @param sigVal the Signature value. This will be the content of the "Value" attribute.
95       * @param secHeader The security header that holds the Signature element.
96       * @return Document with SignatureConfirmation added
97       */
98      public Document build(Document doc, byte[] sigVal, WSSecHeader secHeader) {
99          log.debug("Begin add signature confirmation...");
100         
101         signatureValue = sigVal;
102         prepare(doc);
103         prependToHeader(secHeader);
104         
105         return doc;
106     }
107 
108     /**
109      * Get the id generated during <code>prepare()</code>.
110      * 
111      * Returns the the value of wsu:Id attribute of this SignatureConfirmation. 
112      * 
113      * @return Return the wsu:Id of this token or null if <code>prepareToken()</code>
114      * was not called before.
115      */
116     public String getId() {
117         if (sc == null) {
118             return null;
119         }
120         return sc.getID();
121     }
122     
123     /**
124      * Get the SignatureConfirmation element generated during 
125      * <code>prepare()</code>.
126      * 
127      * @return Return the SignatureConfirmation element or null if <code>prepare()</code>
128      * was not called before.
129      */
130     public Element getSignatureConfirmationElement() {
131         return (sc != null) ? sc.getElement() : null;
132     }
133     
134 }