01: /*
02: * Copyright 2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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:
22: import javax.security.auth.callback.Callback;
23: import javax.security.auth.callback.UnsupportedCallbackException;
24:
25: import com.sun.xml.wss.impl.callback.CertificateValidationCallback;
26: import com.sun.xml.wss.impl.callback.PasswordValidationCallback;
27:
28: /**
29: * Mock implementation of of callback handler that accepts all password and certificate validation callbacks.
30: * <p/>
31: * If the <code>valid</code> property is set to <code>true</code> (the default), this handler simply accepts and
32: * validates every password or certificate validation callback that is passed to it.
33: * <p/>
34: * This class handles <code>CertificateValidationCallback</code>s and <code>PasswordValidationCallback</code>s, and
35: * throws an <code>UnsupportedCallbackException</code> for others
36: *
37: * @author Arjen Poutsma
38: */
39: public class MockValidationCallbackHandler extends
40: AbstractCallbackHandler {
41:
42: private boolean isValid = true;
43:
44: public MockValidationCallbackHandler() {
45: }
46:
47: public MockValidationCallbackHandler(boolean valid) {
48: isValid = valid;
49: }
50:
51: protected void handleInternal(Callback callback)
52: throws IOException, UnsupportedCallbackException {
53: if (callback instanceof CertificateValidationCallback) {
54: CertificateValidationCallback validationCallback = (CertificateValidationCallback) callback;
55: validationCallback
56: .setValidator(new MockCertificateValidator());
57: } else if (callback instanceof PasswordValidationCallback) {
58: PasswordValidationCallback validationCallback = (PasswordValidationCallback) callback;
59: validationCallback
60: .setValidator(new MockPasswordValidator());
61: } else {
62: throw new UnsupportedCallbackException(callback);
63: }
64: }
65:
66: public void setValid(boolean valid) {
67: isValid = valid;
68: }
69:
70: private class MockCertificateValidator implements
71: CertificateValidationCallback.CertificateValidator {
72:
73: public boolean validate(X509Certificate certificate)
74: throws CertificateValidationCallback.CertificateValidationException {
75: return isValid;
76: }
77: }
78:
79: private class MockPasswordValidator implements
80: PasswordValidationCallback.PasswordValidator {
81:
82: public boolean validate(
83: PasswordValidationCallback.Request request)
84: throws PasswordValidationCallback.PasswordValidationException {
85: return isValid;
86: }
87: }
88: }
|