01: /*
02: * Copyright 2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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:
21: import javax.security.auth.callback.Callback;
22: import javax.security.auth.callback.UnsupportedCallbackException;
23:
24: import com.sun.xml.wss.impl.callback.PasswordCallback;
25: import com.sun.xml.wss.impl.callback.UsernameCallback;
26:
27: import org.springframework.beans.factory.InitializingBean;
28: import org.springframework.util.Assert;
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: */
40: public class SimpleUsernamePasswordCallbackHandler extends
41: 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)
61: throws IOException, UnsupportedCallbackException {
62: if (callback instanceof UsernameCallback) {
63: UsernameCallback usernameCallback = (UsernameCallback) callback;
64: usernameCallback.setUsername(username);
65: } else if (callback instanceof PasswordCallback) {
66: PasswordCallback passwordCallback = (PasswordCallback) callback;
67: passwordCallback.setPassword(password);
68: } else {
69: throw new UnsupportedCallbackException(callback);
70: }
71: }
72: }
|