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.protocol.http.gui;
020:
021: import java.awt.BorderLayout;
022: import java.awt.Dimension;
023: import java.awt.event.ActionEvent;
024: import java.awt.event.ActionListener;
025: import java.io.File;
026: import java.io.IOException;
027:
028: import javax.swing.BorderFactory;
029: import javax.swing.JButton;
030: import javax.swing.JCheckBox;
031: import javax.swing.JPanel;
032: import javax.swing.JScrollPane;
033: import javax.swing.JTable;
034: import javax.swing.ListSelectionModel;
035: import javax.swing.table.TableCellEditor;
036:
037: import org.apache.commons.httpclient.cookie.CookiePolicy;
038: import org.apache.jmeter.config.gui.AbstractConfigGui;
039: import org.apache.jmeter.gui.util.FileDialoger;
040: import org.apache.jmeter.gui.util.PowerTableModel;
041: import org.apache.jmeter.protocol.http.control.Cookie;
042: import org.apache.jmeter.protocol.http.control.CookieManager;
043: import org.apache.jmeter.testelement.TestElement;
044: import org.apache.jmeter.testelement.property.PropertyIterator;
045: import org.apache.jmeter.util.JMeterUtils;
046: import org.apache.jorphan.gui.JLabeledChoice;
047: import org.apache.jorphan.gui.layout.VerticalLayout;
048: import org.apache.jorphan.logging.LoggingManager;
049: import org.apache.log.Logger;
050:
051: /**
052: * This is the GUI for Cookie Manager
053: *
054: * Allows the user to specify if she needs cookie services, and give parameters
055: * for this service.
056: *
057: */
058: public class CookiePanel extends AbstractConfigGui implements
059: ActionListener {
060:
061: private static final Logger log = LoggingManager
062: .getLoggerForClass();
063:
064: private static final String ADD_COMMAND = "Add"; //$NON-NLS-1$
065:
066: private static final String DELETE_COMMAND = "Delete"; //$NON-NLS-1$
067:
068: private static final String LOAD_COMMAND = "Load"; //$NON-NLS-1$
069:
070: private static final String SAVE_COMMAND = "Save"; //$NON-NLS-1$
071:
072: private JTable cookieTable;
073:
074: private PowerTableModel tableModel;
075:
076: private JCheckBox clearEachIteration;
077:
078: private static final String clearEachIterationLabel = "clear_cookies_per_iter"; //$NON-NLS-1$
079:
080: private static final String[] columnNames = {
081: JMeterUtils.getResString("name"), //$NON-NLS-1$
082: JMeterUtils.getResString("value"), //$NON-NLS-1$
083: JMeterUtils.getResString("domain"), //$NON-NLS-1$
084: JMeterUtils.getResString("path"), //$NON-NLS-1$
085: JMeterUtils.getResString("secure"), //$NON-NLS-1$
086: // removed expiration because it's just an annoyance for static cookies
087: };
088:
089: private static final Class[] columnClasses = { String.class,
090: String.class, String.class, String.class, Boolean.class, };
091:
092: private JButton addButton;
093:
094: private JButton deleteButton;
095:
096: private JButton loadButton;
097:
098: private JButton saveButton;
099:
100: /*
101: * List of cookie policies.
102: *
103: * These are used both for the display, and for setting the policy
104: */
105: private final String[] policies = CookiePolicy
106: .getRegisteredCookieSpecs();
107:
108: private JLabeledChoice policy;
109:
110: /**
111: * Default constructor.
112: */
113: public CookiePanel() {
114: init();
115: }
116:
117: public String getLabelResource() {
118: return "cookie_manager_title"; //$NON-NLS-1$
119: }
120:
121: public void actionPerformed(ActionEvent e) {
122: String action = e.getActionCommand();
123:
124: if (action.equals(DELETE_COMMAND)) {
125: if (tableModel.getRowCount() > 0) {
126: // If a table cell is being edited, we must cancel the editing
127: // before deleting the row.
128: if (cookieTable.isEditing()) {
129: TableCellEditor cellEditor = cookieTable
130: .getCellEditor(cookieTable.getEditingRow(),
131: cookieTable.getEditingColumn());
132: cellEditor.cancelCellEditing();
133: }
134:
135: int rowSelected = cookieTable.getSelectedRow();
136:
137: if (rowSelected != -1) {
138: tableModel.removeRow(rowSelected);
139: tableModel.fireTableDataChanged();
140:
141: // Disable the DELETE and SAVE buttons if no rows remaining
142: // after delete.
143: if (tableModel.getRowCount() == 0) {
144: deleteButton.setEnabled(false);
145: saveButton.setEnabled(false);
146: }
147:
148: // Table still contains one or more rows, so highlight
149: // (select) the appropriate one.
150: else {
151: int rowToSelect = rowSelected;
152:
153: if (rowSelected >= tableModel.getRowCount()) {
154: rowToSelect = rowSelected - 1;
155: }
156:
157: cookieTable.setRowSelectionInterval(
158: rowToSelect, rowToSelect);
159: }
160: }
161: }
162: } else if (action.equals(ADD_COMMAND)) {
163: // If a table cell is being edited, we should accept the current
164: // value and stop the editing before adding a new row.
165: if (cookieTable.isEditing()) {
166: TableCellEditor cellEditor = cookieTable.getCellEditor(
167: cookieTable.getEditingRow(), cookieTable
168: .getEditingColumn());
169: cellEditor.stopCellEditing();
170: }
171:
172: tableModel.addNewRow();
173: tableModel.fireTableDataChanged();
174:
175: // Enable the DELETE and SAVE buttons if they are currently
176: // disabled.
177: if (!deleteButton.isEnabled()) {
178: deleteButton.setEnabled(true);
179: }
180: if (!saveButton.isEnabled()) {
181: saveButton.setEnabled(true);
182: }
183:
184: // Highlight (select) the appropriate row.
185: int rowToSelect = tableModel.getRowCount() - 1;
186: cookieTable.setRowSelectionInterval(rowToSelect,
187: rowToSelect);
188: } else if (action.equals(LOAD_COMMAND)) {
189: try {
190: final String[] _txt = { ".txt" }; //$NON-NLS-1$
191: File tmp = FileDialoger.promptToOpenFile(_txt)
192: .getSelectedFile();
193: if (tmp != null) {
194: CookieManager manager = new CookieManager();
195: manager.addFile(tmp.getAbsolutePath());
196: for (int i = 0; i < manager.getCookieCount(); i++) {
197: addCookieToTable(manager.get(i));
198: }
199: tableModel.fireTableDataChanged();
200:
201: if (tableModel.getRowCount() > 0) {
202: deleteButton.setEnabled(true);
203: saveButton.setEnabled(true);
204: }
205: }
206: } catch (IOException ex) {
207: log.error("", ex);
208: } catch (NullPointerException err) {
209: }
210: } else if (action.equals(SAVE_COMMAND)) {
211: try {
212: File tmp = FileDialoger
213: .promptToSaveFile("cookies.txt").getSelectedFile(); //$NON-NLS-1$
214: if (tmp != null) {
215: ((CookieManager) createTestElement()).save(tmp
216: .getAbsolutePath());
217: }
218: } catch (IOException ex) {
219: log.error("", ex);
220: } catch (NullPointerException err) {
221: }
222: }
223: }
224:
225: private void addCookieToTable(Cookie cookie) {
226: tableModel
227: .addRow(new Object[] { cookie.getName(),
228: cookie.getValue(), cookie.getDomain(),
229: cookie.getPath(),
230: Boolean.valueOf(cookie.getSecure()) });
231: }
232:
233: /**
234: * Modifies a given TestElement to mirror the data in the gui components.
235: *
236: * @see org.apache.jmeter.gui.JMeterGUIComponent#modifyTestElement(TestElement)
237: */
238: public void modifyTestElement(TestElement cm) {
239: if (cookieTable.isEditing()) {
240: cookieTable.getCellEditor().stopCellEditing();
241: }
242: cm.clear();
243: configureTestElement(cm);
244: if (cm instanceof CookieManager) {
245: CookieManager cookieManager = (CookieManager) cm;
246: for (int i = 0; i < tableModel.getRowCount(); i++) {
247: Cookie cookie = createCookie(tableModel.getRowData(i));
248: cookieManager.add(cookie);
249: }
250: cookieManager.setClearEachIteration(clearEachIteration
251: .isSelected());
252: cookieManager.setCookiePolicy(policy.getText());
253: }
254: }
255:
256: /**
257: * Implements JMeterGUIComponent.clearGui
258: */
259: public void clearGui() {
260: super .clearGui();
261:
262: tableModel.clearData();
263: clearEachIteration.setSelected(false);
264: policy.setSelectedIndex(policies.length - 1);
265: deleteButton.setEnabled(false);
266: saveButton.setEnabled(false);
267: }
268:
269: private Cookie createCookie(Object[] rowData) {
270: Cookie cookie = new Cookie((String) rowData[0],
271: (String) rowData[1], (String) rowData[2],
272: (String) rowData[3], ((Boolean) rowData[4])
273: .booleanValue(), 0); // Non-expiring
274: return cookie;
275: }
276:
277: private void populateTable(CookieManager manager) {
278: tableModel.clearData();
279: PropertyIterator iter = manager.getCookies().iterator();
280: while (iter.hasNext()) {
281: addCookieToTable((Cookie) iter.next().getObjectValue());
282: }
283: }
284:
285: public TestElement createTestElement() {
286: CookieManager cookieManager = new CookieManager();
287: modifyTestElement(cookieManager);
288: return cookieManager;
289: }
290:
291: public void configure(TestElement el) {
292: super .configure(el);
293:
294: CookieManager cookieManager = (CookieManager) el;
295: populateTable(cookieManager);
296: clearEachIteration.setSelected((cookieManager)
297: .getClearEachIteration());
298: policy.setText(cookieManager.getPolicy());
299: }
300:
301: /**
302: * Shows the main cookie configuration panel.
303: */
304: private void init() {
305: tableModel = new PowerTableModel(columnNames, columnClasses);
306: clearEachIteration = new JCheckBox(JMeterUtils
307: .getResString(clearEachIterationLabel), false);
308: policy = new JLabeledChoice(JMeterUtils
309: .getResString("cookie_manager_policy"), //$NON-NLS-1$
310: policies);
311: policy.setText(CookieManager.DEFAULT_POLICY);
312: setLayout(new BorderLayout());
313: setBorder(makeBorder());
314: JPanel northPanel = new JPanel();
315: northPanel
316: .setLayout(new VerticalLayout(5, VerticalLayout.BOTH));
317: northPanel.add(makeTitlePanel());
318: northPanel.add(clearEachIteration);
319: northPanel.add(policy);
320: add(northPanel, BorderLayout.NORTH);
321: add(createCookieTablePanel(), BorderLayout.CENTER);
322: }
323:
324: public JPanel createCookieTablePanel() {
325: // create the JTable that holds one cookie per row
326: cookieTable = new JTable(tableModel);
327: cookieTable
328: .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
329: cookieTable.setPreferredScrollableViewportSize(new Dimension(
330: 100, 70));
331:
332: JPanel buttonPanel = createButtonPanel();
333:
334: JPanel panel = new JPanel(new BorderLayout(0, 5));
335: panel.setBorder(BorderFactory.createTitledBorder(BorderFactory
336: .createEtchedBorder(), JMeterUtils
337: .getResString("cookies_stored"))); //$NON-NLS-1$
338:
339: panel.add(new JScrollPane(cookieTable), BorderLayout.CENTER);
340: panel.add(buttonPanel, BorderLayout.SOUTH);
341: return panel;
342: }
343:
344: private JButton createButton(String resName, char mnemonic,
345: String command, boolean enabled) {
346: JButton button = new JButton(JMeterUtils.getResString(resName));
347: button.setMnemonic(mnemonic);
348: button.setActionCommand(command);
349: button.setEnabled(enabled);
350: button.addActionListener(this );
351: return button;
352: }
353:
354: private JPanel createButtonPanel() {
355: boolean tableEmpty = (tableModel.getRowCount() == 0);
356:
357: addButton = createButton("add", 'A', ADD_COMMAND, true); //$NON-NLS-1$
358: deleteButton = createButton(
359: "delete", 'D', DELETE_COMMAND, !tableEmpty); //$NON-NLS-1$
360: loadButton = createButton("load", 'L', LOAD_COMMAND, true); //$NON-NLS-1$
361: saveButton = createButton(
362: "save", 'S', SAVE_COMMAND, !tableEmpty); //$NON-NLS-1$
363:
364: JPanel buttonPanel = new JPanel();
365: buttonPanel.add(addButton);
366: buttonPanel.add(deleteButton);
367: buttonPanel.add(loadButton);
368: buttonPanel.add(saveButton);
369: return buttonPanel;
370: }
371: }
|