View Javadoc

1   /*
2    * Copyright (C) 2007 ETH Zurich
3    *
4    * This file is part of Fosstrak (www.fosstrak.org).
5    *
6    * Fosstrak is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License version 2.1, as published by the Free Software Foundation.
9    *
10   * Fosstrak is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13   * Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with Fosstrak; if not, write to the Free
17   * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18   * Boston, MA  02110-1301  USA
19   */
20  
21  package org.fosstrak.epcis.gui;
22  
23  import java.awt.FlowLayout;
24  import java.awt.event.ActionEvent;
25  import java.awt.event.ActionListener;
26  import java.io.File;
27  import java.util.HashMap;
28  import java.util.LinkedHashMap;
29  import java.util.Map;
30  
31  import javax.swing.JButton;
32  import javax.swing.JComboBox;
33  import javax.swing.JFileChooser;
34  import javax.swing.JLabel;
35  import javax.swing.JPanel;
36  import javax.swing.JPasswordField;
37  import javax.swing.JTextField;
38  import javax.swing.event.DocumentEvent;
39  import javax.swing.event.DocumentListener;
40  import javax.swing.filechooser.FileFilter;
41  
42  import org.fosstrak.epcis.utils.AuthenticationType;
43  
44  
45  /**
46   * The AuthenticationConfigurationPanel is a pull down list of authentication modes and
47   * widgets to accept parameters relevant to those choices. It is displayed
48   * inline beneath the "URL" text field in the capture and query clients. It has two subcomponents
49   * (BasicOptionsPanel and CertificateOptionsPanel) that contain inputs relevant
50   * to the parameters for those authentication methods.
51   * 
52   * @author Sean Wellington
53   */
54  public class AuthenticationOptionsPanel extends JPanel implements ActionListener {
55  
56      private static final long serialVersionUID = -6085494400041808090L;
57  
58      private static final Map<String, AuthenticationType> authTypes = new LinkedHashMap<String, AuthenticationType>();
59      static {
60      	authTypes.put("None", AuthenticationType.NONE);
61      	authTypes.put("Basic", AuthenticationType.BASIC);
62      	authTypes.put("X.509 Certificate", AuthenticationType.HTTPS_WITH_CLIENT_CERT);
63      }
64      
65      private AuthenticationOptionsChangeListener helper;
66  
67      private JLabel authTypeLabel;
68      private JComboBox authTypeSelector;
69      private OptionsPanel selectedOptionsPanel;
70  
71      private Map<AuthenticationType, OptionsPanel> allOptionsPanels = new HashMap<AuthenticationType, OptionsPanel>();
72  
73      public AuthenticationOptionsPanel(AuthenticationOptionsChangeListener helper) {
74          super(new FlowLayout(FlowLayout.LEFT, 5, 0));
75          this.helper = helper;
76  
77          authTypeLabel = new JLabel("Authentication Mode:");
78          add(authTypeLabel);
79  
80          authTypeSelector = new JComboBox(authTypes.keySet().toArray());
81          authTypeSelector.addActionListener(this);
82          add(authTypeSelector);
83  
84          OptionsPanel noneOptions = new NoneOptionsPanel();
85          allOptionsPanels.put(AuthenticationType.NONE, noneOptions);
86          add((JPanel) noneOptions);
87  
88          OptionsPanel basicOptions = new BasicOptionsPanel();
89          allOptionsPanels.put(AuthenticationType.BASIC, basicOptions);
90          add((JPanel) basicOptions);
91  
92          OptionsPanel certOptions = new CertificateOptionsPanel();
93          allOptionsPanels.put(AuthenticationType.HTTPS_WITH_CLIENT_CERT, certOptions);
94          add((JPanel) certOptions);
95  
96          selectedOptionsPanel = noneOptions;
97      }
98  
99      public void actionPerformed(ActionEvent e) {
100         if (authTypeSelector == e.getSource()) {
101             selectedOptionsPanel.setVisible(false);
102             AuthenticationType at = authTypes.get(authTypeSelector.getSelectedItem());
103             selectedOptionsPanel = allOptionsPanels.get(at);
104             selectedOptionsPanel.setVisible(true);
105             helper.configurationChanged(new AuthenticationOptionsChangeEvent(this, selectedOptionsPanel.isComplete()));
106         }
107     }
108 
109     public Object[] getAuthenticationOptions() {
110         return selectedOptionsPanel.getAuthenticationOptions();
111     }
112 
113     private interface OptionsPanel {
114 
115     	public Object[] getAuthenticationOptions();
116 
117         public void setVisible(boolean visible);
118 
119         public boolean isComplete();
120     }
121 
122     private class NoneOptionsPanel extends JPanel implements OptionsPanel {
123 
124         private static final long serialVersionUID = -3875349682626806242L;
125 
126         public Object[] getAuthenticationOptions() {
127             return new Object[] { AuthenticationType.NONE };
128         }
129 
130         public boolean isComplete() {
131             return true;
132         }
133     }
134 
135     private class BasicOptionsPanel extends JPanel implements DocumentListener, OptionsPanel {
136 
137         private static final long serialVersionUID = -8162893711119565008L;
138 
139         private JLabel userNameLabel;
140         private JTextField userNameInput;
141         private JLabel passwordLabel;
142         private JPasswordField passwordInput;
143 
144         private BasicOptionsPanel() {
145             super(new FlowLayout(FlowLayout.LEFT, 10, 0));
146 
147             userNameLabel = new JLabel("Username:");
148             add(userNameLabel);
149 
150             userNameInput = new JTextField(10);
151             userNameInput.getDocument().addDocumentListener(this);
152             add(userNameInput);
153 
154             passwordLabel = new JLabel("Password:");
155             add(passwordLabel);
156 
157             passwordInput = new JPasswordField(10);
158             passwordInput.getDocument().addDocumentListener(this);
159             add(passwordInput);
160 
161             setVisible(false);
162         }
163 
164         public Object[] getAuthenticationOptions() {
165             return new Object[] {
166                     AuthenticationType.BASIC, userNameInput.getText(), new String(passwordInput.getPassword()) };
167         }
168 
169         public void changedUpdate(DocumentEvent e) {
170             helper.configurationChanged(new AuthenticationOptionsChangeEvent(this, isComplete()));
171         }
172 
173         public void insertUpdate(DocumentEvent e) {
174             helper.configurationChanged(new AuthenticationOptionsChangeEvent(this, isComplete()));
175         }
176 
177         public void removeUpdate(DocumentEvent e) {
178             helper.configurationChanged(new AuthenticationOptionsChangeEvent(this, isComplete()));
179         }
180 
181         public boolean isComplete() {
182             String userName = userNameInput.getText();
183             char[] password = passwordInput.getPassword();
184             return (userName != null && userName.length() > 0) && (password != null && password.length > 0);
185         }
186     }
187 
188     private class CertificateOptionsPanel extends JPanel implements OptionsPanel, ActionListener, DocumentListener {
189 
190         private static final long serialVersionUID = 761325536394318272L;
191 
192         private JLabel keyStoreLabel;
193         private JTextField keyStoreInput;
194         private JButton loadKeyStoreButton;
195         private JFileChooser fileChooser;
196         private JLabel passwordLabel;
197         private JPasswordField passwordInput;
198 
199         CertificateOptionsPanel() {
200             super(new FlowLayout(FlowLayout.LEFT, 10, 0));
201 
202             keyStoreLabel = new JLabel("Key Store:");
203             add(keyStoreLabel);
204 
205             keyStoreInput = new JTextField(23);
206             keyStoreInput.setEnabled(false);
207             add(keyStoreInput);
208 
209             loadKeyStoreButton = new JButton("Open");
210             loadKeyStoreButton.addActionListener(this);
211             add(loadKeyStoreButton);
212 
213             passwordLabel = new JLabel("Password:");
214             add(passwordLabel);
215 
216             passwordInput = new JPasswordField(10);
217             passwordInput.getDocument().addDocumentListener(this);
218             add(passwordInput);
219 
220             fileChooser = new JFileChooser();
221             fileChooser.setFileFilter(new FileFilter() {
222                 public boolean accept(File pathname) {
223                     String s = pathname.getName();
224                     return pathname.isDirectory() || s.endsWith(".p12") || s.endsWith(".jks");
225                 }
226 
227                 public String getDescription() {
228                     return "Key Store Files (*.jks; *.p12)";
229                 }
230             });
231 
232             setVisible(false);
233         }
234 
235         public Object[] getAuthenticationOptions() {
236             return new Object[] {
237                     AuthenticationType.HTTPS_WITH_CLIENT_CERT, keyStoreInput.getText(),
238                     new String(passwordInput.getPassword()) };
239         }
240 
241         public void actionPerformed(ActionEvent e) {
242             if (e.getSource() == loadKeyStoreButton) {
243                 int returnCode = fileChooser.showOpenDialog(getRootPane());
244                 if (returnCode == JFileChooser.APPROVE_OPTION) {
245                     File f = fileChooser.getSelectedFile();
246                     keyStoreInput.setText(f.toString());
247                     helper.configurationChanged(new AuthenticationOptionsChangeEvent(this, isComplete()));
248                 }
249             }
250         }
251 
252         public boolean isComplete() {
253             String filename = keyStoreInput.getText();
254             char[] password = passwordInput.getPassword();
255             return (filename != null && filename.length() > 0) && (password != null && password.length > 0);
256         }
257 
258         public void changedUpdate(DocumentEvent e) {
259             helper.configurationChanged(new AuthenticationOptionsChangeEvent(this, isComplete()));
260         }
261 
262         public void insertUpdate(DocumentEvent e) {
263             helper.configurationChanged(new AuthenticationOptionsChangeEvent(this, isComplete()));
264         }
265 
266         public void removeUpdate(DocumentEvent e) {
267             helper.configurationChanged(new AuthenticationOptionsChangeEvent(this, isComplete()));
268         }
269     }
270 }