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.CallbackHandler;
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  import com.sun.xml.wss.impl.callback.TimestampValidationCallback;
28  
29  import org.springframework.ws.soap.security.callback.CallbackHandlerChain;
30  
31  /**
32   * Represents a chain of <code>CallbackHandler</code>s. For each callback, each of the handlers is called in term. If a
33   * handler throws a <code>UnsupportedCallbackException</code>, the next handler is tried.
34   *
35   * @author Arjen Poutsma
36   * @since 1.0.0
37   */
38  public class XwssCallbackHandlerChain extends CallbackHandlerChain {
39  
40      public XwssCallbackHandlerChain(CallbackHandler[] callbackHandlers) {
41          super(callbackHandlers);
42      }
43  
44      @Override
45      protected void handleInternal(Callback callback) throws IOException, UnsupportedCallbackException {
46          if (callback instanceof CertificateValidationCallback) {
47              handleCertificateValidationCallback((CertificateValidationCallback) callback);
48          }
49          else if (callback instanceof PasswordValidationCallback) {
50              handlePasswordValidationCallback((PasswordValidationCallback) callback);
51          }
52          else if (callback instanceof TimestampValidationCallback) {
53              handleTimestampValidationCallback((TimestampValidationCallback) callback);
54          }
55          else {
56              super.handleInternal(callback);
57          }
58      }
59  
60      private void handleCertificateValidationCallback(CertificateValidationCallback callback) {
61          callback.setValidator(new CertificateValidatorChain(callback));
62      }
63  
64      private void handlePasswordValidationCallback(PasswordValidationCallback callback) {
65          callback.setValidator(new PasswordValidatorChain(callback));
66      }
67  
68      private void handleTimestampValidationCallback(TimestampValidationCallback callback) {
69          callback.setValidator(new TimestampValidatorChain(callback));
70      }
71  
72      private class TimestampValidatorChain implements TimestampValidationCallback.TimestampValidator {
73  
74          private TimestampValidationCallback callback;
75  
76          private TimestampValidatorChain(TimestampValidationCallback callback) {
77              this.callback = callback;
78          }
79  
80          public void validate(TimestampValidationCallback.Request request)
81                  throws TimestampValidationCallback.TimestampValidationException {
82              for (int i = 0; i < getCallbackHandlers().length; i++) {
83                  CallbackHandler callbackHandler = getCallbackHandlers()[i];
84                  try {
85                      callbackHandler.handle(new Callback[]{callback});
86                      callback.getResult();
87                  }
88                  catch (IOException e) {
89                      throw new TimestampValidationCallback.TimestampValidationException(e);
90                  }
91                  catch (UnsupportedCallbackException e) {
92                      // ignore
93                  }
94              }
95          }
96      }
97  
98      private class PasswordValidatorChain implements PasswordValidationCallback.PasswordValidator {
99  
100         private PasswordValidationCallback callback;
101 
102         private PasswordValidatorChain(PasswordValidationCallback callback) {
103             this.callback = callback;
104         }
105 
106         public boolean validate(PasswordValidationCallback.Request request)
107                 throws PasswordValidationCallback.PasswordValidationException {
108             boolean allUnsupported = true;
109             for (int i = 0; i < getCallbackHandlers().length; i++) {
110                 CallbackHandler callbackHandler = getCallbackHandlers()[i];
111                 try {
112                     callbackHandler.handle(new Callback[]{callback});
113                     allUnsupported = false;
114                     if (!callback.getResult()) {
115                         return false;
116                     }
117                 }
118                 catch (IOException e) {
119                     throw new PasswordValidationCallback.PasswordValidationException(e);
120                 }
121                 catch (UnsupportedCallbackException e) {
122                     // ignore
123                 }
124             }
125             return !allUnsupported;
126         }
127     }
128 
129     private class CertificateValidatorChain implements CertificateValidationCallback.CertificateValidator {
130 
131         private CertificateValidationCallback callback;
132 
133         private CertificateValidatorChain(CertificateValidationCallback callback) {
134             this.callback = callback;
135         }
136 
137         public boolean validate(X509Certificate certificate)
138                 throws CertificateValidationCallback.CertificateValidationException {
139             boolean allUnsupported = true;
140             for (int i = 0; i < getCallbackHandlers().length; i++) {
141                 CallbackHandler callbackHandler = getCallbackHandlers()[i];
142                 try {
143                     callbackHandler.handle(new Callback[]{callback});
144                     allUnsupported = false;
145                     if (!callback.getResult()) {
146                         return false;
147                     }
148                 }
149                 catch (IOException e) {
150                     throw new CertificateValidationCallback.CertificateValidationException(e);
151                 }
152                 catch (UnsupportedCallbackException e) {
153                     // ignore
154                 }
155             }
156             return !allUnsupported;
157         }
158     }
159 }