001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)ProxyCallbackHandler.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: /**
030: * ProxyCallbackHandler.java
031: *
032: * SUN PROPRIETARY/CONFIDENTIAL.
033: * This software is the proprietary information of Sun Microsystems, Inc.
034: * Use is subject to license terms.
035: *
036: * Created on February 23, 2005, 9:54 AM
037: */package com.sun.jbi.internal.security.callback;
038:
039: import com.sun.enterprise.security.jauth.callback.*;
040: import com.sun.jbi.binding.security.PasswordCredential;
041: import com.sun.jbi.internal.security.ThreadLocalContext;
042:
043: import javax.security.auth.callback.Callback;
044: import javax.security.auth.callback.CallbackHandler;
045: import javax.security.auth.callback.NameCallback;
046: import javax.security.auth.callback.PasswordCallback;
047: import javax.security.auth.callback.UnsupportedCallbackException;
048:
049: /**
050: * This is a Delegating CallbackHandler, it delegates the task of handling the
051: * callbacks to secondary callback handlers.
052: *
053: * @author Sun Microsystems, Inc.
054: */
055: public class ProxyCallbackHandler implements CallbackHandler {
056: /** The Key Information Callback Handler. */
057: private CallbackHandler mKeyInfoHandler;
058:
059: /** The User Information Callback Handler. */
060: private CallbackHandler mAuthHandler;
061:
062: /**
063: * Creates a new instance of ProxyCallbackHandler.
064: *
065: * @param keyInfoHandler is the callback handler to which this handler will delegate
066: * the handling of the Callbacks for Key/Certificate information.
067: *
068: * @param authHandler is the callback handler to which this handler will delegate
069: * the handling of the Callbacks for validating user information.
070: */
071: public ProxyCallbackHandler(CallbackHandler keyInfoHandler,
072: CallbackHandler authHandler) {
073: mKeyInfoHandler = keyInfoHandler;
074: mAuthHandler = authHandler;
075: }
076:
077: /**
078: * The implementation on the CallbackInterface.
079: *
080: * @param callbacks - array of Callbacks to be handled.
081: * @throws java.io.IOException - if an input or output error occurs.
082: * @throws UnsupportedCallbackException - if the implementation of this method
083: * does not support one or more of the Callbacks specified in the callbacks
084: * parameter.
085: */
086: public void handle(Callback[] callbacks)
087: throws java.io.IOException, UnsupportedCallbackException {
088: for (int i = 0; i < callbacks.length; i++) {
089: CallbackHandler handler = null;
090:
091: if ((callbacks[i] instanceof CertStoreCallback)
092: || (callbacks[i] instanceof PrivateKeyCallback)
093: || (callbacks[i] instanceof SecretKeyCallback)
094: || (callbacks[i] instanceof TrustStoreCallback)) {
095: mKeyInfoHandler.handle(new Callback[] { callbacks[i] });
096: } else if (callbacks[i] instanceof PasswordValidationCallback) {
097: mAuthHandler.handle(new Callback[] { callbacks[i] });
098: }
099: // -- Don't need separate classes for this
100: else if (callbacks[i] instanceof NameCallback) {
101: NameCallback nameCB = (NameCallback) callbacks[i];
102: nameCB.setName("no-name");
103: if (ThreadLocalContext.getLocalSubject() != null) {
104: java.util.Iterator itr = ThreadLocalContext
105: .getLocalSubject().getPrivateCredentials(
106: PasswordCredential.class)
107: .iterator();
108:
109: if (itr.hasNext()) {
110: PasswordCredential pc = (PasswordCredential) itr
111: .next();
112: nameCB.setName(pc.getUsername());
113: }
114: }
115: } else if (callbacks[i] instanceof PasswordCallback) {
116: PasswordCallback pwdCB = (PasswordCallback) callbacks[i];
117: pwdCB.setPassword(new String("no-pwd").toCharArray());
118: if (ThreadLocalContext.getLocalSubject() != null) {
119: java.util.Iterator itr = ThreadLocalContext
120: .getLocalSubject().getPrivateCredentials(
121: PasswordCredential.class)
122: .iterator();
123:
124: if (itr.hasNext()) {
125: PasswordCredential pc = (PasswordCredential) itr
126: .next();
127: pwdCB.setPassword(pc.getPassword()
128: .toCharArray());
129: }
130: }
131: } else {
132: throw new UnsupportedCallbackException(callbacks[i]);
133: }
134: }
135: }
136:
137: }
|