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.action;
21  
22  import org.apache.ws.security.WSConstants;
23  import org.apache.ws.security.WSEncryptionPart;
24  import org.apache.ws.security.WSSecurityEngineResult;
25  import org.apache.ws.security.WSSecurityException;
26  import org.apache.ws.security.handler.RequestData;
27  import org.apache.ws.security.handler.WSHandler;
28  import org.apache.ws.security.handler.WSHandlerConstants;
29  import org.apache.ws.security.handler.WSHandlerResult;
30  import org.apache.ws.security.message.WSSecSignatureConfirmation;
31  import org.apache.ws.security.util.WSSecurityUtil;
32  import org.w3c.dom.Document;
33  
34  import java.util.ArrayList;
35  import java.util.List;
36  
37  public class SignatureConfirmationAction implements Action {
38      protected static final org.apache.commons.logging.Log log = 
39          org.apache.commons.logging.LogFactory.getLog(SignatureConfirmationAction.class);
40  
41      @SuppressWarnings("unchecked")
42      public void execute(WSHandler handler, int actionToDo, Document doc, RequestData reqData)
43              throws WSSecurityException {
44          if (log.isDebugEnabled()) {
45              log.debug("Perform Signature confirmation");
46          }
47  
48          List<WSHandlerResult> results = 
49              (List<WSHandlerResult>) handler.getProperty(
50                  reqData.getMsgContext(), WSHandlerConstants.RECV_RESULTS
51              );
52          if (results == null) {
53              return;
54          }
55          //
56          // Loop over all the (signature) results gathered by all the processors, and store
57          // them in a list.
58          //
59          List<WSSecurityEngineResult> signatureActions = new ArrayList<WSSecurityEngineResult>();
60          for (WSHandlerResult wshResult : results) {
61              List<WSSecurityEngineResult> resultList = wshResult.getResults();
62  
63              WSSecurityUtil.fetchAllActionResults(
64                  resultList, WSConstants.SIGN, signatureActions
65              );
66              WSSecurityUtil.fetchAllActionResults(
67                  resultList, WSConstants.ST_SIGNED, signatureActions
68              );
69              WSSecurityUtil.fetchAllActionResults(
70                  resultList, WSConstants.UT_SIGN, signatureActions
71              );
72          }
73          //
74          // prepare a SignatureConfirmation token
75          //
76          WSSecSignatureConfirmation wsc = new WSSecSignatureConfirmation(reqData.getWssConfig());
77          List<WSEncryptionPart> signatureParts = reqData.getSignatureParts();
78          if (signatureActions.size() > 0) {
79              if (log.isDebugEnabled()) {
80                  log.debug("Signature Confirmation: number of Signature results: "
81                          + signatureActions.size());
82              }
83              for (int i = 0; i < signatureActions.size(); i++) {
84                  WSSecurityEngineResult wsr = (WSSecurityEngineResult) signatureActions.get(i);
85                  byte[] sigVal = (byte[]) wsr.get(WSSecurityEngineResult.TAG_SIGNATURE_VALUE);
86                  wsc.build(doc, sigVal, reqData.getSecHeader());
87                  signatureParts.add(new WSEncryptionPart(wsc.getId()));
88              }
89          } else {
90              wsc.build(doc, null, reqData.getSecHeader());
91              signatureParts.add(new WSEncryptionPart(wsc.getId()));
92          }
93          handler.setProperty(
94              reqData.getMsgContext(), WSHandlerConstants.SIG_CONF_DONE, ""
95          );
96      }
97      
98  }