001 /* 002 * Copyright 2010 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package com.hs.mail.security.login; 017 018 import java.io.IOException; 019 020 import javax.security.auth.callback.Callback; 021 import javax.security.auth.callback.CallbackHandler; 022 import javax.security.auth.callback.NameCallback; 023 import javax.security.auth.callback.PasswordCallback; 024 import javax.security.auth.callback.UnsupportedCallbackException; 025 026 /** 027 * Basic JAAS username password CallbackHandler 028 * 029 * @author Won Chul Doh 030 * @since Jul 18, 2007 031 * 032 */ 033 public class BasicCallbackHandler implements CallbackHandler { 034 035 private String username; 036 private char[] password; 037 038 public BasicCallbackHandler(String username, char[] password) { 039 this.username = username; 040 this.password = password; 041 } 042 043 public void handle(Callback[] callbacks) throws IOException, 044 UnsupportedCallbackException { 045 for (int i = 0; i < callbacks.length; i++) { 046 Callback callback = callbacks[i]; 047 if (callback instanceof NameCallback) 048 ((NameCallback) callback).setName(username); 049 else if (callback instanceof PasswordCallback) 050 ((PasswordCallback) callback).setPassword(password); 051 else 052 throw new UnsupportedCallbackException(callback, 053 "Callback class not supported"); 054 } 055 } 056 057 }