001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.ivy.util;
019:
020: import java.awt.GridBagConstraints;
021: import java.awt.GridBagLayout;
022: import java.awt.Insets;
023: import java.io.File;
024: import java.io.FileInputStream;
025: import java.io.FileOutputStream;
026: import java.io.IOException;
027: import java.util.Properties;
028:
029: import javax.swing.ImageIcon;
030: import javax.swing.JCheckBox;
031: import javax.swing.JLabel;
032: import javax.swing.JOptionPane;
033: import javax.swing.JPanel;
034: import javax.swing.JPasswordField;
035: import javax.swing.JTextField;
036:
037: import org.apache.ivy.Ivy;
038:
039: public final class CredentialsUtil {
040:
041: private static final class CredentialPanel extends JPanel {
042: private static final int FIELD_LENGTH = 20;
043:
044: private JTextField userNameField = new JTextField(FIELD_LENGTH);
045:
046: private JTextField passwordField = new JPasswordField(
047: FIELD_LENGTH);
048:
049: private JCheckBox rememberDataCB = new JCheckBox(
050: "remember my information");
051:
052: CredentialPanel(Credentials credentials, File passfile) {
053: GridBagLayout layout = new GridBagLayout();
054: setLayout(layout);
055: GridBagConstraints c = new GridBagConstraints();
056: c.insets = new Insets(2, 2, 2, 2);
057:
058: c.gridx = 1;
059: c.gridheight = 1;
060: c.gridwidth = 2;
061: String prompt = credentials.getRealm() != null ? "Enter username and password for \""
062: + credentials.getRealm()
063: + "\" at "
064: + credentials.getHost()
065: : "Enter username and password for "
066: + credentials.getHost();
067: add(new JLabel(prompt), c);
068:
069: c.gridy = 1;
070: c.gridwidth = 1;
071:
072: add(new JLabel("username: "), c);
073: c.gridx = 2;
074: add(userNameField, c);
075: c.gridx = 1;
076: c.gridy++;
077:
078: if (credentials.getUserName() != null) {
079: userNameField.setText(credentials.getUserName());
080: }
081:
082: if (credentials.getPasswd() == null) {
083: add(new JLabel("passwd: "), c);
084: c.gridx = 2;
085: add(passwordField, c);
086: c.gridx = 1;
087: c.gridy++;
088: } else {
089: passwordField.setText(credentials.getPasswd());
090: }
091:
092: if (passfile != null) {
093: c.gridwidth = 2;
094: add(rememberDataCB, c);
095: c.gridy++;
096: }
097: c.gridwidth = 2;
098: add(new JLabel(), c); // spacer
099:
100: }
101: }
102:
103: public static Credentials promptCredentials(Credentials c,
104: File passfile) {
105: c = loadPassfile(c, passfile);
106: if (c.getUserName() != null && c.getPasswd() != null) {
107: return c;
108: }
109: CredentialPanel credentialPanel = new CredentialPanel(c,
110: passfile);
111: if (JOptionPane.showOptionDialog(null, credentialPanel, c
112: .getHost()
113: + " credentials", JOptionPane.OK_CANCEL_OPTION, 0,
114: new ImageIcon(Ivy.class.getResource("logo.png")), null,
115: new Integer(JOptionPane.OK_OPTION)) == JOptionPane.OK_OPTION) {
116: String username = credentialPanel.userNameField.getText();
117: String passwd = credentialPanel.passwordField.getText();
118: if (credentialPanel.rememberDataCB.isSelected()) {
119: Properties props = new EncrytedProperties();
120: props.setProperty("username", username);
121: props.setProperty("passwd", passwd);
122: FileOutputStream fos = null;
123: try {
124: fos = new FileOutputStream(passfile);
125: props.store(fos, "");
126: } catch (Exception e) {
127: Message
128: .warn("error occured while saving password file "
129: + passfile + ": " + e);
130: } finally {
131: if (fos != null) {
132: try {
133: fos.close();
134: } catch (Exception e) {
135: // ignored
136: }
137: }
138: }
139: }
140: c = new Credentials(c.getRealm(), c.getHost(), username,
141: passwd);
142: }
143: return c;
144: }
145:
146: public static Credentials loadPassfile(Credentials c, File passfile) {
147: if (passfile != null && passfile.exists()) {
148: Properties props = new EncrytedProperties();
149: FileInputStream fis = null;
150: try {
151: fis = new FileInputStream(passfile);
152: props.load(fis);
153: String username = c.getUserName();
154: String passwd = c.getPasswd();
155: if (username == null) {
156: username = props.getProperty("username");
157: }
158: if (passwd == null) {
159: passwd = props.getProperty("passwd");
160: }
161: return new Credentials(c.getRealm(), c.getHost(),
162: username, passwd);
163: } catch (IOException e) {
164: Message
165: .warn("error occured while loading password file "
166: + passfile + ": " + e);
167: } finally {
168: if (fis != null) {
169: try {
170: fis.close();
171: } catch (IOException e) {
172: // ignored
173: }
174: }
175: }
176: }
177: return c;
178: }
179:
180: private CredentialsUtil() {
181: }
182: }
|