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.wss4j.dom.message;
21  
22  import org.apache.wss4j.dom.message.token.SignatureConfirmation;
23  import org.apache.wss4j.dom.util.WSSecurityUtil;
24  import org.w3c.dom.Document;
25  import org.w3c.dom.Element;
26  
27  /**
28   * Builds a WS SignatureConfirmation and inserts it into the SOAP Envelope.
29   */
30  public class WSSecSignatureConfirmation extends WSSecBase {
31      private static final org.slf4j.Logger LOG =
32          org.slf4j.LoggerFactory.getLogger(WSSecSignatureConfirmation.class);
33  
34      private SignatureConfirmation sc;
35  
36      private byte[] signatureValue;
37  
38      public WSSecSignatureConfirmation(WSSecHeader securityHeader) {
39          super(securityHeader);
40      }
41  
42      public WSSecSignatureConfirmation(Document doc) {
43          super(doc);
44      }
45  
46      /**
47       * Set the Signature value to store in this SignatureConfirmation.
48       *
49       * @param signatureValue The Signature value to store in the SignatureConfirmation element
50       */
51      public void setSignatureValue(byte[] signatureValue) {
52          this.signatureValue = signatureValue;
53      }
54  
55  
56      /**
57       * Creates a SignatureConfimation element.
58       *
59       * The method prepares and initializes a WSSec SignatureConfirmation structure after
60       * the relevant information was set. Before calling <code>prepare()</code> the
61       * filed <code>signatureValue</code> must be set
62       */
63      public void prepare() {
64          sc = new SignatureConfirmation(getDocument(), signatureValue);
65          sc.setID(getIdAllocator().createId("SC-", sc));
66          if (addWSUNamespace) {
67              sc.addWSUNamespace();
68          }
69      }
70  
71      /**
72       * Prepends the SignatureConfirmation element to the elements already in the
73       * Security header.
74       *
75       * The method can be called any time after <code>prepare()</code>.
76       * This allows to insert the SignatureConfirmation element at any position in the
77       * Security header.
78       */
79      public void prependToHeader() {
80          Element securityHeaderElement = getSecurityHeader().getSecurityHeaderElement();
81          WSSecurityUtil.prependChildElement(securityHeaderElement, sc.getElement());
82      }
83  
84      /**
85       * Adds a new <code>SignatureConfirmation</code> to a soap envelope.
86       *
87       * A complete <code>SignatureConfirmation</code> is constructed and added
88       * to the <code>wsse:Security</code> header.
89       *
90       * @param sigVal the Signature value. This will be the content of the "Value" attribute.
91       * @return Document with SignatureConfirmation added
92       */
93      public Document build(byte[] sigVal) {
94          LOG.debug("Begin add signature confirmation...");
95  
96          signatureValue = sigVal;
97          prepare();
98          prependToHeader();
99  
100         return getDocument();
101     }
102 
103     /**
104      * Get the id generated during <code>prepare()</code>.
105      *
106      * Returns the the value of wsu:Id attribute of this SignatureConfirmation.
107      *
108      * @return Return the wsu:Id of this token or null if <code>prepareToken()</code>
109      * was not called before.
110      */
111     public String getId() {
112         if (sc == null) {
113             return null;
114         }
115         return sc.getID();
116     }
117 
118     /**
119      * Get the SignatureConfirmation element generated during
120      * <code>prepare()</code>.
121      *
122      * @return Return the SignatureConfirmation element or null if <code>prepare()</code>
123      * was not called before.
124      */
125     public Element getSignatureConfirmationElement() {
126         return (sc != null) ? sc.getElement() : null;
127     }
128 
129 }