001: /*
002: * Copyright 2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.ws.soap.security.xwss.callback;
018:
019: import java.io.IOException;
020: import java.security.cert.X509Certificate;
021:
022: import javax.security.auth.callback.Callback;
023: import javax.security.auth.callback.CallbackHandler;
024: import javax.security.auth.callback.UnsupportedCallbackException;
025:
026: import com.sun.xml.wss.impl.callback.CertificateValidationCallback;
027: import com.sun.xml.wss.impl.callback.PasswordValidationCallback;
028: import com.sun.xml.wss.impl.callback.TimestampValidationCallback;
029:
030: /**
031: * Represents a chain of <code>CallbackHandler</code>s. For each callback, each of the handlers is called in term. If a
032: * handler throws a <code>UnsupportedCallbackException</code>, the next handler is tried.
033: *
034: * @author Arjen Poutsma
035: */
036: public class CallbackHandlerChain extends AbstractCallbackHandler {
037:
038: private CallbackHandler[] callbackHandlers;
039:
040: public CallbackHandlerChain(CallbackHandler[] callbackHandlers) {
041: this .callbackHandlers = callbackHandlers;
042: }
043:
044: public void setCallbackHandlers(CallbackHandler[] callbackHandlers) {
045: this .callbackHandlers = callbackHandlers;
046: }
047:
048: protected void handleInternal(Callback callback)
049: throws IOException, UnsupportedCallbackException {
050: if (callback instanceof CertificateValidationCallback) {
051: handleCertificateValidationCallback((CertificateValidationCallback) callback);
052: } else if (callback instanceof PasswordValidationCallback) {
053: handlePasswordValidationCallback((PasswordValidationCallback) callback);
054: } else if (callback instanceof TimestampValidationCallback) {
055: handleTimestampValidationCallback((TimestampValidationCallback) callback);
056: } else {
057: boolean allUnsupported = true;
058: for (int i = 0; i < callbackHandlers.length; i++) {
059: CallbackHandler callbackHandler = callbackHandlers[i];
060: try {
061: callbackHandler.handle(new Callback[] { callback });
062: allUnsupported = false;
063: } catch (UnsupportedCallbackException ex) {
064: // if an UnsupportedCallbackException occurs, go to the next handler
065: }
066: }
067: if (allUnsupported) {
068: throw new UnsupportedCallbackException(callback);
069: }
070: }
071: }
072:
073: private void handleCertificateValidationCallback(
074: CertificateValidationCallback callback) {
075: callback.setValidator(new CertificateValidatorChain(callback));
076: }
077:
078: private void handlePasswordValidationCallback(
079: PasswordValidationCallback callback) {
080: callback.setValidator(new PasswordValidatorChain(callback));
081: }
082:
083: private void handleTimestampValidationCallback(
084: TimestampValidationCallback callback) {
085: callback.setValidator(new TimestampValidatorChain(callback));
086: }
087:
088: private class TimestampValidatorChain implements
089: TimestampValidationCallback.TimestampValidator {
090:
091: private TimestampValidationCallback callback;
092:
093: private TimestampValidatorChain(
094: TimestampValidationCallback callback) {
095: this .callback = callback;
096: }
097:
098: public void validate(TimestampValidationCallback.Request request)
099: throws TimestampValidationCallback.TimestampValidationException {
100: for (int i = 0; i < callbackHandlers.length; i++) {
101: CallbackHandler callbackHandler = callbackHandlers[i];
102: try {
103: callbackHandler.handle(new Callback[] { callback });
104: callback.getResult();
105: } catch (IOException e) {
106: throw new TimestampValidationCallback.TimestampValidationException(
107: e);
108: } catch (UnsupportedCallbackException e) {
109: // ignore
110: }
111: }
112: }
113: }
114:
115: private class PasswordValidatorChain implements
116: PasswordValidationCallback.PasswordValidator {
117:
118: private PasswordValidationCallback callback;
119:
120: private PasswordValidatorChain(
121: PasswordValidationCallback callback) {
122: this .callback = callback;
123: }
124:
125: public boolean validate(
126: PasswordValidationCallback.Request request)
127: throws PasswordValidationCallback.PasswordValidationException {
128: boolean allUnsupported = true;
129: for (int i = 0; i < callbackHandlers.length; i++) {
130: CallbackHandler callbackHandler = callbackHandlers[i];
131: try {
132: callbackHandler.handle(new Callback[] { callback });
133: allUnsupported = false;
134: if (!callback.getResult()) {
135: return false;
136: }
137: } catch (IOException e) {
138: throw new PasswordValidationCallback.PasswordValidationException(
139: e);
140: } catch (UnsupportedCallbackException e) {
141: // ignore
142: }
143: }
144: return !allUnsupported;
145: }
146: }
147:
148: private class CertificateValidatorChain implements
149: CertificateValidationCallback.CertificateValidator {
150:
151: private CertificateValidationCallback callback;
152:
153: private CertificateValidatorChain(
154: CertificateValidationCallback callback) {
155: this .callback = callback;
156: }
157:
158: public boolean validate(X509Certificate certificate)
159: throws CertificateValidationCallback.CertificateValidationException {
160: boolean allUnsupported = true;
161: for (int i = 0; i < callbackHandlers.length; i++) {
162: CallbackHandler callbackHandler = callbackHandlers[i];
163: try {
164: callbackHandler.handle(new Callback[] { callback });
165: allUnsupported = false;
166: if (!callback.getResult()) {
167: return false;
168: }
169: } catch (IOException e) {
170: throw new CertificateValidationCallback.CertificateValidationException(
171: e);
172: } catch (UnsupportedCallbackException e) {
173: // ignore
174: }
175: }
176: return !allUnsupported;
177: }
178: }
179: }
|