View Javadoc

1   /*
2    * Copyright 2005-2012 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.ws.soap.security.wss4j;
18  
19  import java.util.List;
20  import java.util.Properties;
21  
22  import org.springframework.ws.context.MessageContext;
23  
24  import org.apache.ws.security.WSSecurityEngineResult;
25  import org.apache.ws.security.WSSecurityException;
26  import org.apache.ws.security.components.crypto.Crypto;
27  import org.apache.ws.security.handler.RequestData;
28  import org.apache.ws.security.handler.WSHandler;
29  import org.apache.ws.security.handler.WSHandlerConstants;
30  import org.w3c.dom.Document;
31  
32  /**
33   * @author Tareq Abed Rabbo
34   * @author Arjen Poutsma
35   * @since 1.5.0
36   */
37  class Wss4jHandler extends WSHandler {
38  
39      /** Keys are constants from [email protected] WSHandlerConstants}; values are strings. */
40      private Properties options = new Properties();
41  
42      private String securementPassword;
43  
44      private Crypto securementEncryptionCrypto;
45  
46      private Crypto securementSignatureCrypto;
47  
48      Wss4jHandler() {
49          // set up default handler properties
50          options.setProperty(WSHandlerConstants.MUST_UNDERSTAND, Boolean.toString(true));
51          options.setProperty(WSHandlerConstants.ENABLE_SIGNATURE_CONFIRMATION, Boolean.toString(true));
52      }
53  
54      @Override
55      protected boolean checkReceiverResultsAnyOrder(List<WSSecurityEngineResult> wsResult, List<Integer> actions) {
56          return super.checkReceiverResultsAnyOrder(wsResult, actions);
57      }
58  
59      void setOption(String key, String value) {
60          options.setProperty(key, value);
61      }
62  
63      void setOption(String key, boolean value) {
64          options.setProperty(key, Boolean.toString(value));
65      }
66  
67      @Override
68      public Object getOption(String key) {
69          return options.getProperty(key);
70      }
71  
72      void setSecurementPassword(String securementPassword) {
73          this.securementPassword = securementPassword;
74      }
75  
76      void setSecurementEncryptionCrypto(Crypto securementEncryptionCrypto) {
77          this.securementEncryptionCrypto = securementEncryptionCrypto;
78      }
79  
80      void setSecurementSignatureCrypto(Crypto securementSignatureCrypto) {
81          this.securementSignatureCrypto = securementSignatureCrypto;
82      }
83  
84      @Override
85      public String getPassword(Object msgContext) {
86          return securementPassword;
87      }
88  
89      @Override
90      public Object getProperty(Object msgContext, String key) {
91          return ((MessageContext) msgContext).getProperty(key);
92      }
93  
94      @Override
95      protected Crypto loadEncryptionCrypto(RequestData reqData) throws WSSecurityException {
96          return securementEncryptionCrypto;
97      }
98  
99      @Override
100     public Crypto loadSignatureCrypto(RequestData reqData) throws WSSecurityException {
101         return securementSignatureCrypto;
102     }
103 
104     @Override
105     public void setPassword(Object msgContext, String password) {
106         securementPassword = password;
107     }
108 
109     @Override
110     public void setProperty(Object msgContext, String key, Object value) {
111         ((MessageContext) msgContext).setProperty(key, value);
112     }
113 
114     @Override
115     protected void doSenderAction(int doAction,
116                                   Document doc,
117                                   RequestData reqData,
118                                   List<Integer> actions,
119                                   boolean isRequest) throws WSSecurityException {
120         super.doSenderAction(doAction, doc, reqData, actions, isRequest);
121     }
122 }