View Javadoc

1   /*
2    * Copyright 2006 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 javax.security.auth.callback.Callback;
21  import javax.security.auth.callback.UnsupportedCallbackException;
22  
23  import com.sun.xml.wss.impl.callback.PasswordCallback;
24  import com.sun.xml.wss.impl.callback.UsernameCallback;
25  
26  import org.springframework.beans.factory.InitializingBean;
27  import org.springframework.util.Assert;
28  import org.springframework.ws.soap.security.callback.AbstractCallbackHandler;
29  
30  /**
31   * Simple callback handler that supplies a username and password to a username token at runtime.
32   * <p/>
33   * This class handles <code>UsernameCallback</code>s and <code>PasswordCallback</code>s, and throws an
34   * <code>UnsupportedCallbackException</code> for others
35   *
36   * @author Arjen Poutsma
37   * @see #setUsername(String)
38   * @see #setPassword(String)
39   * @since 1.0.0
40   */
41  public class SimpleUsernamePasswordCallbackHandler extends AbstractCallbackHandler implements InitializingBean {
42  
43      private String username;
44  
45      private String password;
46  
47      public void setPassword(String password) {
48          this.password = password;
49      }
50  
51      public void setUsername(String username) {
52          this.username = username;
53      }
54  
55      public void afterPropertiesSet() throws Exception {
56          Assert.hasLength(username, "username must be set");
57          Assert.hasLength(password, "password must be set");
58      }
59  
60      protected void handleInternal(Callback callback) throws IOException, UnsupportedCallbackException {
61          if (callback instanceof UsernameCallback) {
62              UsernameCallback usernameCallback = (UsernameCallback) callback;
63              usernameCallback.setUsername(username);
64          }
65          else if (callback instanceof PasswordCallback) {
66              PasswordCallback passwordCallback = (PasswordCallback) callback;
67              passwordCallback.setPassword(password);
68          }
69          else {
70              throw new UnsupportedCallbackException(callback);
71          }
72      }
73  }