View Javadoc
1   /*
2    * Copyright 2005-2010 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.xwss.callback;
18  
19  import java.io.IOException;
20  import java.security.cert.X509Certificate;
21  import javax.security.auth.callback.Callback;
22  import javax.security.auth.callback.UnsupportedCallbackException;
23  
24  import com.sun.xml.wss.impl.callback.CertificateValidationCallback;
25  import com.sun.xml.wss.impl.callback.PasswordValidationCallback;
26  
27  import org.springframework.ws.soap.security.callback.AbstractCallbackHandler;
28  
29  /**
30   * Mock implementation of of callback handler that accepts all password and certificate validation callbacks.
31   * <p/>
32   * If the <code>valid</code> property is set to <code>true</code> (the default), this handler simply accepts and
33   * validates every password or certificate validation callback that is passed to it.
34   * <p/>
35   * This class handles <code>CertificateValidationCallback</code>s and <code>PasswordValidationCallback</code>s, and
36   * throws an <code>UnsupportedCallbackException</code> for others
37   *
38   * @author Arjen Poutsma
39   * @since 1.0.0
40   */
41  public class MockValidationCallbackHandler extends AbstractCallbackHandler {
42  
43      private boolean isValid = true;
44  
45      public MockValidationCallbackHandler() {
46      }
47  
48      public MockValidationCallbackHandler(boolean valid) {
49          isValid = valid;
50      }
51  
52      @Override
53      protected void handleInternal(Callback callback) throws IOException, UnsupportedCallbackException {
54          if (callback instanceof CertificateValidationCallback) {
55              CertificateValidationCallback validationCallback = (CertificateValidationCallback) callback;
56              validationCallback.setValidator(new MockCertificateValidator());
57          }
58          else if (callback instanceof PasswordValidationCallback) {
59              PasswordValidationCallback validationCallback = (PasswordValidationCallback) callback;
60              validationCallback.setValidator(new MockPasswordValidator());
61          }
62          else {
63              throw new UnsupportedCallbackException(callback);
64          }
65      }
66  
67      public void setValid(boolean valid) {
68          isValid = valid;
69      }
70  
71      private class MockCertificateValidator implements CertificateValidationCallback.CertificateValidator {
72  
73          public boolean validate(X509Certificate certificate)
74                  throws CertificateValidationCallback.CertificateValidationException {
75              return isValid;
76          }
77      }
78  
79      private class MockPasswordValidator implements PasswordValidationCallback.PasswordValidator {
80  
81          public boolean validate(PasswordValidationCallback.Request request)
82                  throws PasswordValidationCallback.PasswordValidationException {
83              return isValid;
84          }
85      }
86  }