KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > soap > handlers > security > BaseSecurityCallbackHandler


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.soap.handlers.security;
18
19 import java.io.IOException JavaDoc;
20
21 import javax.security.auth.callback.Callback JavaDoc;
22 import javax.security.auth.callback.CallbackHandler JavaDoc;
23 import javax.security.auth.callback.UnsupportedCallbackException JavaDoc;
24
25 import org.apache.ws.security.WSPasswordCallback;
26
27 /**
28  * Base implementation for security callback handler.
29  *
30  * @author gnodet
31  */

32 public class BaseSecurityCallbackHandler implements CallbackHandler JavaDoc {
33     
34     public void handle(Callback JavaDoc[] callbacks) throws IOException JavaDoc, UnsupportedCallbackException JavaDoc {
35         if (callbacks == null || callbacks.length == 0) {
36             throw new IllegalStateException JavaDoc("callbacks is null or empty");
37         }
38         for (int i = 0; i < callbacks.length; i++) {
39             if (callbacks[i] instanceof WSPasswordCallback == false) {
40                 throw new UnsupportedCallbackException JavaDoc(callbacks[i]);
41             }
42             processCallback((WSPasswordCallback) callbacks[i]);
43         }
44     }
45     
46     protected void processCallback(WSPasswordCallback callback) throws IOException JavaDoc, UnsupportedCallbackException JavaDoc {
47         switch (callback.getUsage()) {
48         case WSPasswordCallback.DECRYPT:
49             processDecrypt(callback);
50             break;
51         case WSPasswordCallback.USERNAME_TOKEN:
52             processUsernameToken(callback);
53             break;
54         case WSPasswordCallback.SIGNATURE:
55             processSignature(callback);
56             break;
57         case WSPasswordCallback.KEY_NAME:
58             processKeyName(callback);
59             break;
60         case WSPasswordCallback.USERNAME_TOKEN_UNKNOWN:
61             processUsernameTokenUnkown(callback);
62             break;
63         default:
64             throw new UnsupportedCallbackException JavaDoc(callback);
65         }
66     }
67
68     /**
69      * Need a password to get the private key of
70      * this identifier (username) from the keystore. WSS4J uses this private
71      * key to decrypt the session (symmetric) key. Because the encryption
72      * method uses the public key to encrypt the session key it needs no
73      * password (a public key is usually not protected by a password)
74      */

75     protected void processDecrypt(WSPasswordCallback callback) throws IOException JavaDoc, UnsupportedCallbackException JavaDoc {
76         throw new UnsupportedCallbackException JavaDoc(callback);
77     }
78     
79     /**
80      * Need the password to fill in or to
81      * verify a <code>UsernameToken</code>
82      */

83     protected void processUsernameToken(WSPasswordCallback callback) throws IOException JavaDoc, UnsupportedCallbackException JavaDoc {
84         throw new UnsupportedCallbackException JavaDoc(callback);
85     }
86     
87     /**
88      * Need the password to get the private key of
89      * this identifier (username) from the keystore. WSS4J uses this private
90      * key to produce a signature. The signature verfication uses the public
91      * key to verfiy the signature
92      */

93     protected void processSignature(WSPasswordCallback callback) throws IOException JavaDoc, UnsupportedCallbackException JavaDoc {
94         throw new UnsupportedCallbackException JavaDoc(callback);
95     }
96     
97     /**
98      * Need the <i>key</i>, not the password,
99      * associated with the identifier. WSS4J uses this key to encrypt or
100      * decrypt parts of the SOAP request. Note, the key must match the
101      * symmetric encryption/decryption algorithm specified (refer to
102      * {@link org.apache.ws.security.handler.WSHandlerConstants#ENC_SYM_ALGO})
103      */

104     protected void processKeyName(WSPasswordCallback callback) throws IOException JavaDoc, UnsupportedCallbackException JavaDoc {
105         throw new UnsupportedCallbackException JavaDoc(callback);
106     }
107     
108     /**
109      * Either a not specified
110      * password type or a password type passwordText. In these both cases <b>only</b>
111      * the password variable is <b>set</>. The callback class now may check if
112      * the username and password match. If they don't match the callback class must
113      * throw an exception. The exception can be a UnsupportedCallbackException or
114      * an IOException.</li>
115      */

116     protected void processUsernameTokenUnkown(WSPasswordCallback callback) throws IOException JavaDoc, UnsupportedCallbackException JavaDoc {
117         throw new UnsupportedCallbackException JavaDoc(callback);
118     }
119     
120 }
121
Popular Tags