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:
019: package org.apache.jmeter.gui.action;
020:
021: import java.awt.event.ActionEvent;
022: import java.io.File;
023: import java.io.IOException;
024: import java.util.Collections;
025: import java.util.HashSet;
026: import java.util.Set;
027:
028: import javax.swing.JFileChooser;
029: import javax.swing.filechooser.FileFilter;
030:
031: import org.apache.jmeter.gui.GuiPackage;
032: import org.apache.jmeter.util.JMeterUtils;
033: import org.apache.jmeter.util.SSLManager;
034:
035: //
036: /**
037: * SSL Manager Command. The SSL Manager provides a mechanism to change your
038: * client authentication if required by the server. If you have JSSE 1.0.2
039: * installed, you can select your client identity from a list of installed keys.
040: * You can also change your keystore. JSSE 1.0.2 allows you to export a PKCS#12
041: * key from Netscape 4.04 or higher and use it in a read only format. You must
042: * supply a password that is greater than six characters due to limitations in
043: * the keytool program--and possibly the rest of the system.
044: * <p>
045: * By selecting a *.p12 file as your keystore (your PKCS#12) format file, you
046: * can have a whopping one key keystore. The advantage is that you can test a
047: * connection using the assigned Certificate from a Certificate Authority.
048: * </p>
049: * TODO ?
050: * N.B. The present implementation does not seem to allow selection of keys,
051: * it only allows a change of keystore at run-time, or to provide one if not
052: * already defined via the property.
053: *
054: * @author <a href="bloritsch@apache.org">Berin Loritsch</a>
055: */
056: public class SSLManagerCommand implements Command {
057: private static Set commandSet;
058: static {
059: HashSet commands = new HashSet();
060: commands.add(ActionNames.SSL_MANAGER);
061: SSLManagerCommand.commandSet = Collections
062: .unmodifiableSet(commands);
063: }
064:
065: private JFileChooser keyStoreChooser;
066:
067: /**
068: * Handle the "sslmanager" action by displaying the "SSL CLient Manager"
069: * dialog box. The Dialog Box is NOT modal, because those should be avoided
070: * if at all possible.
071: */
072: public void doAction(ActionEvent e) {
073: if (e.getActionCommand().equals(ActionNames.SSL_MANAGER)) {
074: this .sslManager();
075: }
076: }
077:
078: /**
079: * Provide the list of Action names that are available in this command.
080: */
081: public Set getActionNames() {
082: return SSLManagerCommand.commandSet;
083: }
084:
085: /**
086: * Called by sslManager button. Raises sslManager dialog.
087: * I.e. a FileChooser for PCSI12 (.p12|.P12) files.
088: */
089: private void sslManager() {
090: SSLManager.reset();
091:
092: keyStoreChooser = new JFileChooser(JMeterUtils
093: .getJMeterProperties().getProperty("user.dir")); //$NON-NLS-1$
094: keyStoreChooser
095: .addChoosableFileFilter(new AcceptPKCS12FileFilter());
096: keyStoreChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
097: int retVal = keyStoreChooser.showOpenDialog(GuiPackage
098: .getInstance().getMainFrame());
099:
100: if (JFileChooser.APPROVE_OPTION == retVal) {
101: File selectedFile = keyStoreChooser.getSelectedFile();
102: try {
103: System.setProperty(SSLManager.JAVAX_NET_SSL_KEY_STORE,
104: selectedFile.getCanonicalPath());
105: } catch (IOException e) {
106: //Ignored
107: }
108: }
109:
110: keyStoreChooser = null;
111: SSLManager.getInstance();
112: }
113:
114: /**
115: * Internal class to add a PKCS12 file format filter for JFileChooser.
116: */
117: static private class AcceptPKCS12FileFilter extends FileFilter {
118: /**
119: * Get the description that shows up in JFileChooser filter menu.
120: *
121: * @return description
122: */
123: public String getDescription() {
124: return JMeterUtils.getResString("pkcs12_desc"); //$NON-NLS-1$
125: }
126:
127: /**
128: * Tests to see if the file ends with "*.p12" or "*.P12".
129: *
130: * @param testFile
131: * file to test
132: * @return true if file is accepted, false otherwise
133: */
134: public boolean accept(File testFile) {
135: return testFile.isDirectory()
136: || testFile.getName().endsWith(".p12") //$NON-NLS-1$
137: || testFile.getName().endsWith(".P12"); //$NON-NLS-1$
138: }
139: }
140: }
|