View Javadoc

1   /*
2    * Copyright 2002-2012 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 org.springframework.beans.factory.InitializingBean;
24  import org.springframework.util.Assert;
25  import org.springframework.ws.soap.security.callback.AbstractCallbackHandler;
26  
27  import com.sun.xml.wss.impl.callback.PasswordCallback;
28  import com.sun.xml.wss.impl.callback.UsernameCallback;
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  
48      /**
49       * Constructs an empty instance of the {@code SimpleUsernamePasswordCallbackHandler}.
50       */
51      public SimpleUsernamePasswordCallbackHandler() {
52      }
53  
54      /**
55       * Constructs an instance of the {@code SimpleUsernamePasswordCallbackHandler} with the given name and password.
56       */
57      public SimpleUsernamePasswordCallbackHandler(String username, String password) {
58          this.username = username;
59          this.password = password;
60      }
61  
62      public void setPassword(String password) {
63          this.password = password;
64      }
65  
66      public void setUsername(String username) {
67          this.username = username;
68      }
69  
70      public void afterPropertiesSet() throws Exception {
71          Assert.hasLength(username, "username must be set");
72          Assert.hasLength(password, "password must be set");
73      }
74  
75      @Override
76      protected void handleInternal(Callback callback) throws IOException, UnsupportedCallbackException {
77          if (callback instanceof UsernameCallback) {
78              UsernameCallback usernameCallback = (UsernameCallback) callback;
79              usernameCallback.setUsername(username);
80          }
81          else if (callback instanceof PasswordCallback) {
82              PasswordCallback passwordCallback = (PasswordCallback) callback;
83              passwordCallback.setPassword(password);
84          }
85          else {
86              throw new UnsupportedCallbackException(callback);
87          }
88      }
89  }