Skip to content

Package: SimpleAuthenticator$1

SimpleAuthenticator$1

nameinstructionbranchcomplexitylinemethod
{...}
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Distribution License v. 1.0, which is available at
6: * http://www.eclipse.org/org/documents/edl-v10.php.
7: *
8: * SPDX-License-Identifier: BSD-3-Clause
9: */
10:
11: package example.client;
12:
13: import jakarta.mail.Authenticator;
14: import jakarta.mail.PasswordAuthentication;
15:
16: import javax.swing.*;
17: import java.awt.*;
18: import java.net.InetAddress;
19:
20: /**
21: * Simple Authenticator for requesting password information.
22: *
23: * @author Christopher Cotton
24: * @author Bill Shannon
25: */
26: public class SimpleAuthenticator extends Authenticator {
27:
28: Frame frame;
29: String username;
30: String password;
31:
32: public SimpleAuthenticator(Frame f) {
33: this.frame = f;
34: }
35:
36: protected PasswordAuthentication getPasswordAuthentication() {
37:
38: // given a prompt?
39: String prompt = getRequestingPrompt();
40: if (prompt == null)
41: prompt = "Please login...";
42:
43: // protocol
44: String protocol = getRequestingProtocol();
45: if (protocol == null)
46: protocol = "Unknown protocol";
47:
48: // get the host
49: String host = null;
50: InetAddress inet = getRequestingSite();
51: if (inet != null)
52: host = inet.getHostName();
53: if (host == null)
54: host = "Unknown host";
55:
56: // port
57: String port = "";
58: int portnum = getRequestingPort();
59: if (portnum != -1)
60: port = ", port " + portnum + " ";
61:
62: // Build the info string
63: String info = "Connecting to " + protocol + " mail service on host " +
64: host + port;
65:
66: //JPanel d = new JPanel();
67: // XXX - for some reason using a JPanel here causes JOptionPane
68: // to display incorrectly, so we workaround the problem using
69: // an anonymous JComponent.
70: JComponent d = new JComponent() {
71: };
72:
73: GridBagLayout gb = new GridBagLayout();
74: GridBagConstraints c = new GridBagConstraints();
75: d.setLayout(gb);
76: c.insets = new Insets(2, 2, 2, 2);
77:
78: c.anchor = GridBagConstraints.WEST;
79: c.gridwidth = GridBagConstraints.REMAINDER;
80: c.weightx = 0.0;
81: d.add(constrain(new JLabel(info), gb, c));
82: d.add(constrain(new JLabel(prompt), gb, c));
83:
84: c.gridwidth = 1;
85: c.anchor = GridBagConstraints.EAST;
86: c.fill = GridBagConstraints.NONE;
87: c.weightx = 0.0;
88: d.add(constrain(new JLabel("Username:"), gb, c));
89:
90: c.anchor = GridBagConstraints.EAST;
91: c.fill = GridBagConstraints.HORIZONTAL;
92: c.gridwidth = GridBagConstraints.REMAINDER;
93: c.weightx = 1.0;
94: String user = getDefaultUserName();
95: JTextField username = new JTextField(user, 20);
96: d.add(constrain(username, gb, c));
97:
98: c.gridwidth = 1;
99: c.fill = GridBagConstraints.NONE;
100: c.anchor = GridBagConstraints.EAST;
101: c.weightx = 0.0;
102: d.add(constrain(new JLabel("Password:"), gb, c));
103:
104: c.anchor = GridBagConstraints.EAST;
105: c.fill = GridBagConstraints.HORIZONTAL;
106: c.gridwidth = GridBagConstraints.REMAINDER;
107: c.weightx = 1.0;
108: JPasswordField password = new JPasswordField("", 20);
109: d.add(constrain(password, gb, c));
110: // XXX - following doesn't work
111: if (user != null && user.length() > 0)
112: password.requestFocus();
113: else
114: username.requestFocus();
115:
116: int result = JOptionPane.showConfirmDialog(frame, d, "Login",
117: JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
118:
119: if (result == JOptionPane.OK_OPTION)
120: return new PasswordAuthentication(username.getText(),
121: password.getText());
122: else
123: return null;
124: }
125:
126: private Component constrain(Component cmp,
127: GridBagLayout gb, GridBagConstraints c) {
128: gb.setConstraints(cmp, c);
129: return (cmp);
130: }
131: }