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.config.gui;
020:
021: import java.awt.BorderLayout;
022: import java.awt.Component;
023: import java.awt.FlowLayout;
024:
025: import javax.swing.BorderFactory;
026: import javax.swing.Box;
027: import javax.swing.BoxLayout;
028: import javax.swing.JCheckBox;
029: import javax.swing.JLabel;
030: import javax.swing.JPanel;
031: import javax.swing.JTextField;
032: import javax.swing.event.ChangeEvent;
033: import javax.swing.event.ChangeListener;
034:
035: import org.apache.jmeter.config.Arguments;
036: import org.apache.jmeter.config.ConfigTestElement;
037: import org.apache.jmeter.gui.util.VerticalPanel;
038: import org.apache.jmeter.protocol.http.gui.HTTPArgumentsPanel;
039: import org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase;
040: import org.apache.jmeter.protocol.http.util.HTTPArgument;
041: import org.apache.jmeter.testelement.AbstractTestElement;
042: import org.apache.jmeter.testelement.TestElement;
043: import org.apache.jmeter.testelement.property.BooleanProperty;
044: import org.apache.jmeter.testelement.property.TestElementProperty;
045: import org.apache.jmeter.util.JMeterUtils;
046: import org.apache.jorphan.gui.JLabeledChoice;
047:
048: /**
049: * Basic URL / HTTP Request configuration:
050: * - host and port
051: * - path, method, encoding, parameters
052: * - redirects & keepalive
053: */
054: public class UrlConfigGui extends JPanel implements ChangeListener {
055: protected HTTPArgumentsPanel argsPanel;
056:
057: private static String DOMAIN = "domain"; // $NON-NLS-1$
058:
059: private static String PORT = "port"; // $NON-NLS-1$
060:
061: private static String PROTOCOL = "protocol"; // $NON-NLS-1$
062:
063: private static String CONTENT_ENCODING = "content_encoding"; // $NON-NLS-1$
064:
065: private static String PATH = "path"; // $NON-NLS-1$
066:
067: private static String FOLLOW_REDIRECTS = "follow_redirects"; // $NON-NLS-1$
068:
069: private static String AUTO_REDIRECTS = "auto_redirects"; // $NON-NLS-1$
070:
071: private static String USE_KEEPALIVE = "use_keepalive"; // $NON-NLS-1$
072:
073: private static String USE_MULTIPART_FOR_POST = "use_multipart_for_post"; // $NON-NLS-1$
074:
075: private JTextField domain;
076:
077: private JTextField port;
078:
079: private JTextField protocol;
080:
081: private JTextField contentEncoding;
082:
083: private JTextField path;
084:
085: private JCheckBox followRedirects;
086:
087: private JCheckBox autoRedirects;
088:
089: private JCheckBox useKeepAlive;
090:
091: private JCheckBox useMultipartForPost;
092:
093: private JLabeledChoice method;
094:
095: private final boolean notConfigOnly;
096:
097: // set this true to suppress some items for use in HTTP Request defaults
098:
099: public UrlConfigGui() {
100: notConfigOnly = true;
101: init();
102: }
103:
104: public UrlConfigGui(boolean value) {
105: notConfigOnly = value;
106: init();
107: }
108:
109: protected void configureTestElement(TestElement mc) {
110: mc.setProperty(TestElement.NAME, getName());
111: mc
112: .setProperty(TestElement.GUI_CLASS, this .getClass()
113: .getName());
114: mc.setProperty(TestElement.TEST_CLASS, mc.getClass().getName());
115: }
116:
117: public void clear() {
118: domain.setText(""); // $NON-NLS-1$
119: if (notConfigOnly) {
120: followRedirects.setSelected(false);
121: autoRedirects.setSelected(true);
122: method.setText(HTTPSamplerBase.DEFAULT_METHOD);
123: useKeepAlive.setSelected(true);
124: useMultipartForPost.setSelected(false);
125: }
126: path.setText(""); // $NON-NLS-1$
127: port.setText(""); // $NON-NLS-1$
128: protocol.setText(""); // $NON-NLS-1$
129: contentEncoding.setText(""); // $NON-NLS-1$
130: argsPanel.clear();
131: }
132:
133: public TestElement createTestElement() {
134: ConfigTestElement element = new ConfigTestElement();
135:
136: this .configureTestElement(element);
137: Arguments args = (Arguments) argsPanel.createTestElement();
138:
139: HTTPArgument.convertArgumentsToHTTP(args);
140: element.setProperty(new TestElementProperty(
141: HTTPSamplerBase.ARGUMENTS, args));
142: element.setProperty(HTTPSamplerBase.DOMAIN, domain.getText());
143: element.setProperty(HTTPSamplerBase.PORT, port.getText());
144: element.setProperty(HTTPSamplerBase.PROTOCOL, protocol
145: .getText());
146: element.setProperty(HTTPSamplerBase.CONTENT_ENCODING,
147: contentEncoding.getText());
148: element.setProperty(HTTPSamplerBase.PATH, path.getText());
149: if (notConfigOnly) {
150: element.setProperty(HTTPSamplerBase.METHOD, method
151: .getText());
152: element.setProperty(new BooleanProperty(
153: HTTPSamplerBase.FOLLOW_REDIRECTS, followRedirects
154: .isSelected()));
155: element.setProperty(new BooleanProperty(
156: HTTPSamplerBase.AUTO_REDIRECTS, autoRedirects
157: .isSelected()));
158: element.setProperty(new BooleanProperty(
159: HTTPSamplerBase.USE_KEEPALIVE, useKeepAlive
160: .isSelected()));
161: element.setProperty(new BooleanProperty(
162: HTTPSamplerBase.DO_MULTIPART_POST,
163: useMultipartForPost.isSelected()));
164: }
165: return element;
166: }
167:
168: /**
169: * Set the text, etc. in the UI.
170: *
171: * @param el
172: * contains the data to be displayed
173: */
174: public void configure(TestElement el) {
175: setName(el.getPropertyAsString(TestElement.NAME));
176: argsPanel.configure((TestElement) el.getProperty(
177: HTTPSamplerBase.ARGUMENTS).getObjectValue());
178: domain.setText(el.getPropertyAsString(HTTPSamplerBase.DOMAIN));
179:
180: String portString = el
181: .getPropertyAsString(HTTPSamplerBase.PORT);
182:
183: // Only display the port number if it is meaningfully specified
184: if (portString
185: .equals(HTTPSamplerBase.UNSPECIFIED_PORT_AS_STRING)) {
186: port.setText(""); // $NON-NLS-1$
187: } else {
188: port.setText(portString);
189: }
190: protocol.setText(el
191: .getPropertyAsString(HTTPSamplerBase.PROTOCOL));
192: contentEncoding.setText(el
193: .getPropertyAsString(HTTPSamplerBase.CONTENT_ENCODING));
194: path.setText(el.getPropertyAsString(HTTPSamplerBase.PATH));
195: if (notConfigOnly) {
196: method.setText(el
197: .getPropertyAsString(HTTPSamplerBase.METHOD));
198: followRedirects
199: .setSelected(((AbstractTestElement) el)
200: .getPropertyAsBoolean(HTTPSamplerBase.FOLLOW_REDIRECTS));
201: autoRedirects
202: .setSelected(((AbstractTestElement) el)
203: .getPropertyAsBoolean(HTTPSamplerBase.AUTO_REDIRECTS));
204: useKeepAlive
205: .setSelected(((AbstractTestElement) el)
206: .getPropertyAsBoolean(HTTPSamplerBase.USE_KEEPALIVE));
207: useMultipartForPost
208: .setSelected(((AbstractTestElement) el)
209: .getPropertyAsBoolean(HTTPSamplerBase.DO_MULTIPART_POST));
210: }
211: }
212:
213: protected void init() {
214: this .setLayout(new BorderLayout());
215:
216: // WEB SERVER PANEL
217: VerticalPanel webServerPanel = new VerticalPanel();
218: webServerPanel.setBorder(BorderFactory.createTitledBorder(
219: BorderFactory.createEtchedBorder(), JMeterUtils
220: .getResString("web_server"))); // $NON-NLS-1$
221: final JPanel domainPanel = getDomainPanel();
222: final JPanel portPanel = getPortPanel();
223: domainPanel.add(portPanel, BorderLayout.EAST);
224: webServerPanel.add(domainPanel);
225:
226: JPanel northPanel = new JPanel();
227: northPanel
228: .setLayout(new BoxLayout(northPanel, BoxLayout.Y_AXIS));
229: northPanel.add(getProtocolAndMethodPanel());
230: northPanel.add(getPathPanel());
231:
232: // WEB REQUEST PANEL
233: JPanel webRequestPanel = new JPanel();
234: webRequestPanel.setLayout(new BorderLayout());
235: webRequestPanel.setBorder(BorderFactory.createTitledBorder(
236: BorderFactory.createEtchedBorder(), JMeterUtils
237: .getResString("web_request"))); // $NON-NLS-1$
238:
239: webRequestPanel.add(northPanel, BorderLayout.NORTH);
240: webRequestPanel.add(getParameterPanel(), BorderLayout.CENTER);
241:
242: this .add(webServerPanel, BorderLayout.NORTH);
243: this .add(webRequestPanel, BorderLayout.CENTER);
244: }
245:
246: protected JPanel getPortPanel() {
247: port = new JTextField(6);
248: port.setName(PORT);
249:
250: JLabel label = new JLabel(JMeterUtils
251: .getResString("web_server_port")); // $NON-NLS-1$
252: label.setLabelFor(port);
253:
254: JPanel panel = new JPanel(new BorderLayout(5, 0));
255: panel.add(label, BorderLayout.WEST);
256: panel.add(port, BorderLayout.CENTER);
257:
258: return panel;
259: }
260:
261: protected JPanel getDomainPanel() {
262: domain = new JTextField(20);
263: domain.setName(DOMAIN);
264:
265: JLabel label = new JLabel(JMeterUtils
266: .getResString("web_server_domain")); // $NON-NLS-1$
267: label.setLabelFor(domain);
268:
269: JPanel panel = new JPanel(new BorderLayout(5, 0));
270: panel.add(label, BorderLayout.WEST);
271: panel.add(domain, BorderLayout.CENTER);
272: return panel;
273: }
274:
275: /**
276: * This method defines the Panel for the HTTP path, 'Follow Redirects'
277: * 'Use KeepAlive', and 'Use multipart for HTTP POST' elements.
278: *
279: * @return JPanel The Panel for the path, 'Follow Redirects' and 'Use
280: * KeepAlive' elements.
281: */
282: protected Component getPathPanel() {
283: path = new JTextField(15);
284: path.setName(PATH);
285:
286: JLabel label = new JLabel(JMeterUtils.getResString("path")); //$NON-NLS-1$
287: label.setLabelFor(path);
288:
289: if (notConfigOnly) {
290: followRedirects = new JCheckBox(JMeterUtils
291: .getResString("follow_redirects")); // $NON-NLS-1$
292: followRedirects.setName(FOLLOW_REDIRECTS);
293: followRedirects.setSelected(false);
294:
295: autoRedirects = new JCheckBox(JMeterUtils
296: .getResString("follow_redirects_auto")); //$NON-NLS-1$
297: autoRedirects.setName(AUTO_REDIRECTS);
298: autoRedirects.addChangeListener(this );
299: autoRedirects.setSelected(true);// Default changed in 2.3
300:
301: useKeepAlive = new JCheckBox(JMeterUtils
302: .getResString("use_keepalive")); // $NON-NLS-1$
303: useKeepAlive.setName(USE_KEEPALIVE);
304: useKeepAlive.setSelected(true);
305:
306: useMultipartForPost = new JCheckBox(JMeterUtils
307: .getResString("use_multipart_for_http_post")); // $NON-NLS-1$
308: useMultipartForPost.setName(USE_MULTIPART_FOR_POST);
309: useMultipartForPost.setSelected(false);
310: }
311:
312: JPanel pathPanel = new JPanel(new BorderLayout(5, 0));
313: pathPanel.add(label, BorderLayout.WEST);
314: pathPanel.add(path, BorderLayout.CENTER);
315: pathPanel.setMinimumSize(pathPanel.getPreferredSize());
316:
317: JPanel panel = new JPanel();
318: panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
319: panel.add(pathPanel);
320: if (notConfigOnly) {
321: JPanel optionPanel = new JPanel(new FlowLayout(
322: FlowLayout.LEFT));
323: optionPanel.add(autoRedirects);
324: optionPanel.add(followRedirects);
325: optionPanel.add(useKeepAlive);
326: optionPanel.add(useMultipartForPost);
327: optionPanel.setMinimumSize(optionPanel.getPreferredSize());
328: panel.add(optionPanel);
329: }
330:
331: return panel;
332: }
333:
334: protected JPanel getProtocolAndMethodPanel() {
335: // PROTOCOL
336: protocol = new JTextField(10);
337: protocol.setName(PROTOCOL);
338: // CONTENT_ENCODING
339: contentEncoding = new JTextField(10);
340: contentEncoding.setName(CONTENT_ENCODING);
341:
342: JLabel protocolLabel = new JLabel(JMeterUtils
343: .getResString("protocol")); // $NON-NLS-1$
344: protocolLabel.setLabelFor(protocol);
345: JLabel contentEncodingLabel = new JLabel(JMeterUtils
346: .getResString("content_encoding")); // $NON-NLS-1$
347: protocolLabel.setLabelFor(contentEncoding);
348: if (notConfigOnly) {
349: method = new JLabeledChoice(JMeterUtils
350: .getResString("method"), // $NON-NLS-1$
351: HTTPSamplerBase.getValidMethodsAsArray());
352: }
353:
354: JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
355:
356: panel.add(protocolLabel);
357: panel.add(protocol);
358: panel.add(Box.createHorizontalStrut(5));
359:
360: if (notConfigOnly) {
361: panel.add(method);
362: }
363: panel.setMinimumSize(panel.getPreferredSize());
364: panel.add(Box.createHorizontalStrut(5));
365:
366: panel.add(contentEncodingLabel);
367: panel.add(contentEncoding);
368: return panel;
369: }
370:
371: protected JPanel getParameterPanel() {
372: argsPanel = new HTTPArgumentsPanel();
373:
374: return argsPanel;
375: }
376:
377: // Disable follow redirects if Autoredirect is selected
378: public void stateChanged(ChangeEvent e) {
379: if (e.getSource() == autoRedirects) {
380: if (autoRedirects.isSelected()) {
381: followRedirects.setEnabled(false);
382: } else {
383: followRedirects.setEnabled(true);
384: }
385: }
386: }
387: }
|