KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > webservice > AppclientWSSCallbackHandler


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * AppclientWSSCallbackHandler.java
26  *
27  * Created on September 14, 2004, 12:20 PM
28  */

29
30 package com.sun.enterprise.webservice;
31
32 import javax.security.auth.callback.Callback JavaDoc;
33 import javax.security.auth.callback.CallbackHandler JavaDoc;
34 import javax.security.auth.callback.ChoiceCallback JavaDoc;
35 import javax.security.auth.callback.NameCallback JavaDoc;
36 import javax.security.auth.callback.PasswordCallback JavaDoc;
37 import javax.security.auth.callback.UnsupportedCallbackException JavaDoc;
38 import java.io.IOException JavaDoc;
39 import java.util.logging.*;
40 import com.sun.enterprise.appclient.AppContainer;
41 import com.sun.enterprise.security.wss.*;
42 import com.sun.enterprise.security.jauth.callback.*;
43
44
45
46 /**
47  * Appclient side Callback Handler for WSS.
48  * @author Harpreet Singh
49  */

50 public final class AppclientWSSCallbackHandler extends WSSCallbackHandler {
51     private static final String JavaDoc LOGIN_NAME = "j2eelogin.name";
52     private static final String JavaDoc LOGIN_PASSWORD = "j2eelogin.password";
53     private static WSSCallbackHandler INSTANCE = null;
54     /** Creates a new instance of AppclientWSSCallbackHandler */
55     private AppclientWSSCallbackHandler() {
56     }
57     // cannot use getInstance as super class has a method with that name
58
/**
59      * returns a singleton instance of this class.
60      */

61     static WSSCallbackHandler newInstance() {
62         if(INSTANCE == null){
63             INSTANCE = new AppclientWSSCallbackHandler();
64         }
65         return INSTANCE;
66     }
67
68     public void handle(Callback JavaDoc[] callbacks)
69         throws IOException JavaDoc, UnsupportedCallbackException JavaDoc
70     {
71         if(callbacks == null){
72             return;
73         }
74         // this variable is set to true if we have used the older jaas
75
// mechanisms to process the callbacks - and we will not need
76
// to process further as the inside loop, just takes care
77
// of processing all callbacks
78
boolean processedSomeAppclientCallbacks = false;
79         for (int i=0; i<callbacks.length; i++){
80             if(!isSupportedCallback(callbacks[i])){
81                 _logger.log(Level.FINE, "wss-container-auth: UnsupportedCallback : "+
82                 callbacks[i].getClass().getName());
83                 throw new UnsupportedCallbackException JavaDoc(callbacks[i]);
84             }
85             if(!processedSomeAppclientCallbacks){
86                 if((callbacks[i] instanceof NameCallback JavaDoc) ||
87                 (callbacks[i] instanceof PasswordCallback JavaDoc) ||
88                 (callbacks[i] instanceof ChoiceCallback JavaDoc)) {
89
90                     String JavaDoc loginName = System.getProperty(LOGIN_NAME);
91                     CallbackHandler JavaDoc callbackHandler = AppContainer.getCallbackHandler();
92                     if(loginName != null){// username/password set already
93
String JavaDoc password = System.getProperty(LOGIN_PASSWORD);
94                         for(int j = 0; j < callbacks.length; j++){
95                             if(callbacks[j] instanceof NameCallback JavaDoc){
96                                 NameCallback JavaDoc nc = (NameCallback JavaDoc)callbacks[j];
97                                 nc.setName(loginName);
98                             } else if (callbacks[j] instanceof PasswordCallback JavaDoc){
99                                 PasswordCallback JavaDoc pc = (PasswordCallback JavaDoc)callbacks[j];
100                                 if(password != null)
101                                     pc.setPassword(password.toCharArray());
102                                 else{
103                                     callbackHandler.handle(callbacks);
104                                 }
105                             }
106                         }
107                     } else{
108                         // once this is called all callbacks will be handled by
109
// callbackHandler and then we dont have to check for
110
// NameCallback PasswordCallback and ChoiceCallback
111
// again.
112
// Let control flow to the callback processors
113
callbackHandler.handle(callbacks);
114                     }
115                     processedSomeAppclientCallbacks = true;
116                     break;
117                 }
118             }
119             processCallback(callbacks[i]);
120         }
121     }
122
123     boolean isSupportedCallback(Callback JavaDoc callback)
124     {
125        boolean supported = false;
126         if (callback instanceof NameCallback JavaDoc) {
127             supported = true;
128         } else if (callback instanceof PasswordCallback JavaDoc) {
129             supported = true;
130         } else if (callback instanceof ChoiceCallback JavaDoc) {
131             supported = true;
132         } else if (callback instanceof CertStoreCallback) {
133             supported = true;
134         } else if (callback instanceof PasswordValidationCallback) {
135             supported = true;
136         } else if (callback instanceof SecretKeyCallback) {
137             supported = true;
138         } else if (callback instanceof PrivateKeyCallback) {
139             supported = true;
140         } else if (callback instanceof TrustStoreCallback) {
141             supported = true;
142         } else {
143             supported = false;
144         }
145         return supported;
146     }
147     
148 }
149
Popular Tags