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.callback;
18  
19  import java.io.IOException;
20  import javax.security.auth.callback.Callback;
21  import javax.security.auth.callback.CallbackHandler;
22  import javax.security.auth.callback.UnsupportedCallbackException;
23  
24  /**
25   * Represents a chain of <code>CallbackHandler</code>s. For each callback, each of the handlers is called in term. If a
26   * handler throws a <code>UnsupportedCallbackException</code>, the next handler is tried.
27   *
28   * @author Arjen Poutsma
29   * @since 1.5.0
30   */
31  public class CallbackHandlerChain extends AbstractCallbackHandler {
32  
33      private final CallbackHandler[] callbackHandlers;
34  
35      public CallbackHandlerChain(CallbackHandler[] callbackHandlers) {
36          this.callbackHandlers = callbackHandlers;
37      }
38  
39      public CallbackHandler[] getCallbackHandlers() {
40          return callbackHandlers;
41      }
42  
43      @Override
44      protected void handleInternal(Callback callback) throws IOException, UnsupportedCallbackException {
45          boolean allUnsupported = true;
46          for (CallbackHandler callbackHandler : callbackHandlers) {
47              try {
48                  callbackHandler.handle(new Callback[]{callback});
49                  allUnsupported = false;
50              }
51              catch (UnsupportedCallbackException ex) {
52                  // if an UnsupportedCallbackException occurs, go to the next handler
53              }
54          }
55          if (allUnsupported) {
56              throw new UnsupportedCallbackException(callback);
57          }
58      }
59  }