Subversion Repositories javautils

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  *
  8.  *   - Redistributions of source code must retain the above copyright
  9.  *     notice, this list of conditions and the following disclaimer.
  10.  *
  11.  *   - Redistributions in binary form must reproduce the above copyright
  12.  *     notice, this list of conditions and the following disclaimer in the
  13.  *     documentation and/or other materials provided with the distribution.
  14.  *
  15.  *   - Neither the name of Sun Microsystems nor the names of its
  16.  *     contributors may be used to endorse or promote products derived
  17.  *     from this software without specific prior written permission.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  20.  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  21.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27.  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30.  */
  31. /*
  32.  * Copyright (c) 1996-2007 by Sun Microsystems, Inc.
  33.  * All Rights Reserved.
  34.  */
  35.  
  36. import javax.mail.*;
  37. import java.net.InetAddress;
  38. import java.awt.*;
  39. import javax.swing.*;
  40.  
  41. /**
  42.  * Simple Authenticator for requesting password information.
  43.  *
  44.  * @author      Christopher Cotton
  45.  * @author      Bill Shannon
  46.  */
  47.  
  48. public class SimpleAuthenticator extends Authenticator {
  49.  
  50.     Frame frame;
  51.     String username;
  52.     String password;
  53.  
  54.     public SimpleAuthenticator(Frame f) {
  55.         this.frame = f;
  56.     }
  57.  
  58.     protected PasswordAuthentication getPasswordAuthentication() {
  59.  
  60.         // given a prompt?
  61.         String prompt = getRequestingPrompt();
  62.         if (prompt == null)
  63.             prompt = "Please login...";
  64.  
  65.         // protocol
  66.         String protocol = getRequestingProtocol();
  67.         if (protocol == null)
  68.             protocol = "Unknown protocol";
  69.  
  70.         // get the host
  71.         String host = null;
  72.         InetAddress inet = getRequestingSite();
  73.         if (inet != null)
  74.             host = inet.getHostName();
  75.         if (host == null)
  76.             host = "Unknown host";
  77.  
  78.         // port
  79.         String port = "";
  80.         int portnum = getRequestingPort();
  81.         if (portnum != -1)
  82.             port = ", port " + portnum + " ";
  83.  
  84.         // Build the info string
  85.         String info = "Connecting to " + protocol + " mail service on host " +
  86.                                                                 host + port;
  87.  
  88.         //JPanel d = new JPanel();
  89.         // XXX - for some reason using a JPanel here causes JOptionPane
  90.         // to display incorrectly, so we workaround the problem using
  91.         // an anonymous JComponent.
  92.         JComponent d = new JComponent() { };
  93.  
  94.         GridBagLayout gb = new GridBagLayout();
  95.         GridBagConstraints c = new GridBagConstraints();
  96.         d.setLayout(gb);
  97.         c.insets = new Insets(2, 2, 2, 2);
  98.  
  99.         c.anchor = GridBagConstraints.WEST;
  100.         c.gridwidth = GridBagConstraints.REMAINDER;
  101.         c.weightx = 0.0;
  102.         d.add(constrain(new JLabel(info), gb, c));
  103.         d.add(constrain(new JLabel(prompt), gb, c));
  104.  
  105.         c.gridwidth = 1;
  106.         c.anchor = GridBagConstraints.EAST;
  107.         c.fill = GridBagConstraints.NONE;
  108.         c.weightx = 0.0;
  109.         d.add(constrain(new JLabel("Username:"), gb, c));
  110.  
  111.         c.anchor = GridBagConstraints.EAST;
  112.         c.fill = GridBagConstraints.HORIZONTAL;
  113.         c.gridwidth = GridBagConstraints.REMAINDER;
  114.         c.weightx = 1.0;
  115.         String user = getDefaultUserName();
  116.         JTextField username = new JTextField(user, 20);
  117.         d.add(constrain(username, gb, c));
  118.  
  119.         c.gridwidth = 1;
  120.         c.fill = GridBagConstraints.NONE;
  121.         c.anchor = GridBagConstraints.EAST;
  122.         c.weightx = 0.0;
  123.         d.add(constrain(new JLabel("Password:"), gb, c));
  124.  
  125.         c.anchor = GridBagConstraints.EAST;
  126.         c.fill = GridBagConstraints.HORIZONTAL;
  127.         c.gridwidth = GridBagConstraints.REMAINDER;
  128.         c.weightx = 1.0;
  129.         JPasswordField password = new JPasswordField("", 20);
  130.         d.add(constrain(password, gb, c));
  131.         // XXX - following doesn't work
  132.         if (user != null && user.length() > 0)
  133.             password.requestFocus();
  134.         else
  135.             username.requestFocus();
  136.        
  137.         int result = JOptionPane.showConfirmDialog(frame, d, "Login",
  138.             JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
  139.        
  140.         if (result == JOptionPane.OK_OPTION)
  141.             return new PasswordAuthentication(username.getText(),
  142.                                                 password.getText());
  143.         else
  144.             return null;
  145.     }
  146.  
  147.     private Component constrain(Component cmp,
  148.                                 GridBagLayout gb, GridBagConstraints c) {
  149.         gb.setConstraints(cmp, c);
  150.         return (cmp);
  151.     }
  152. }
  153.