KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > mailer > MailAuthenticator


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

16
17 package org.apache.taglibs.mailer;
18
19 import java.awt.*;
20 import java.awt.event.ActionEvent JavaDoc;
21 import java.awt.event.ActionListener JavaDoc;
22 import javax.mail.Authenticator JavaDoc;
23 import javax.mail.PasswordAuthentication JavaDoc;
24
25 /****************************************************************************
26  *
27  * MailAuthenticator - a password authentication class used to access a mail
28  * server that needs authentication
29  *
30  * @author Rich Catlett
31  *
32  * @version 1.0
33  *
34  ***************************************************************************/

35 public class MailAuthenticator extends Authenticator JavaDoc {
36     /**
37      * The users username used for basic authentication
38      */

39     protected String JavaDoc m_user = null;
40     /**
41      * The users password used for basic authentication
42      */

43     protected String JavaDoc m_password = null;
44
45     /**
46      * constructor calls the super class constructor
47      *
48      */

49     public MailAuthenticator(String JavaDoc user, String JavaDoc password) {
50         super();
51         m_user = user;
52         m_password = password;
53     }
54
55     /**
56      * constructor calls the super class constructor
57      *
58      */

59     public MailAuthenticator() {
60     super();
61     }
62
63     /**
64      * overrides the getPasswordAuthentication method in the super class
65      * it creates a dialog box to collect the users name and password for a
66      * connection to the mail server
67      *
68      * @return - PasswordAuthentication object containing the users name and
69      * password
70      *
71      */

72     protected PasswordAuthentication JavaDoc getPasswordAuthentication() {
73
74     if (m_user != null && m_password != null) {
75         return new PasswordAuthentication JavaDoc(m_user, m_password);
76     }
77
78     final Dialog dialog = new Dialog(new Frame("Login"),
79                      "Please Login to the mail server", true);
80     dialog.setBounds(200, 200, 200, 100);
81     dialog.setLayout(new GridLayout (0, 1));
82     Label label = new Label(getRequestingPrompt());
83     dialog.add (label);
84     TextField username = new TextField();
85     username.setBackground (Color.white);
86     dialog.add (username);
87     TextField password = new TextField();
88     password.setEchoChar ('*');
89     password.setBackground (Color.white);
90     dialog.add (password);
91     Button button = new Button ("OK");
92     dialog.add (button);
93     button.addActionListener (new ActionListener JavaDoc() {
94         public void actionPerformed (ActionEvent JavaDoc e) {
95             dialog.dispose();
96         }
97         });
98     dialog.pack();
99     dialog.setVisible(true);
100     return new PasswordAuthentication JavaDoc(username.getText(),
101                               password.getText());
102     }
103 }
104
Popular Tags