KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > security > auth > CredentialsCallbackHandler


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.security.auth;
14
15 import java.io.IOException JavaDoc;
16
17 import javax.security.auth.callback.Callback JavaDoc;
18 import javax.security.auth.callback.CallbackHandler JavaDoc;
19 import javax.security.auth.callback.NameCallback JavaDoc;
20 import javax.security.auth.callback.PasswordCallback JavaDoc;
21 import javax.security.auth.callback.TextOutputCallback JavaDoc;
22 import javax.security.auth.callback.UnsupportedCallbackException JavaDoc;
23
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27
28 /**
29  * Plain text callback handler
30  * @author Sameer Charles
31  * @version $Id: CredentialsCallbackHandler.java 6341 2006-09-12 09:18:27Z philipp $
32  */

33 public class CredentialsCallbackHandler implements CallbackHandler JavaDoc {
34
35     /**
36      * Logger
37      */

38     protected static Logger log = LoggerFactory.getLogger(CredentialsCallbackHandler.class);
39
40     /**
41      * user id
42      */

43     protected String JavaDoc name;
44
45     /**
46      * password
47      */

48     protected char[] pswd;
49
50     /**
51      * default constructor required by java security framework
52      */

53     public CredentialsCallbackHandler() {
54         // do not instanciate with this constructor
55
}
56
57     /**
58      * @param name
59      * @param pswd
60      */

61     public CredentialsCallbackHandler(String JavaDoc name, char[] pswd) {
62         this.name = name;
63         this.pswd = pswd;
64     }
65
66     /**
67      * handle name and password callback which must be set while constructing this handler
68      */

69     public void handle(Callback JavaDoc[] callbacks) throws IOException JavaDoc, UnsupportedCallbackException JavaDoc {
70         for (int i = 0; i < callbacks.length; i++) {
71             if (callbacks[i] instanceof NameCallback JavaDoc) {
72                 ((NameCallback JavaDoc) callbacks[i]).setName(this.name);
73             }
74             else if (callbacks[i] instanceof PasswordCallback JavaDoc) {
75                 ((PasswordCallback JavaDoc) callbacks[i]).setPassword(this.pswd);
76             }
77             else if (callbacks[i] instanceof TextOutputCallback JavaDoc) {
78                 TextOutputCallback JavaDoc outputCallback = (TextOutputCallback JavaDoc) callbacks[i];
79                 switch (outputCallback.getMessageType()) {
80                     case TextOutputCallback.INFORMATION:
81                         log.info(outputCallback.getMessage());
82                         break;
83                     case TextOutputCallback.ERROR:
84                         log.error(outputCallback.getMessage());
85                         break;
86                     case TextOutputCallback.WARNING:
87                         log.warn(outputCallback.getMessage());
88                         break;
89                     default:
90                         if (log.isDebugEnabled()) {
91                             log.debug("Unsupported message type : {}", Integer
92                                 .toString(outputCallback.getMessageType()));
93                             log.debug("Message : {}", outputCallback.getMessage());
94                         }
95                 }
96             }
97         }
98     }
99 }
100
Popular Tags