1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 19 package org.apache.jmeter.protocol.http.config.gui; 20 21 import java.awt.BorderLayout; 22 import java.awt.Component; 23 import java.awt.FlowLayout; 24 25 import javax.swing.BorderFactory; 26 import javax.swing.Box; 27 import javax.swing.BoxLayout; 28 import javax.swing.JCheckBox; 29 import javax.swing.JLabel; 30 import javax.swing.JPanel; 31 import javax.swing.JTextField; 32 import javax.swing.event.ChangeEvent; 33 import javax.swing.event.ChangeListener; 34 35 import org.apache.jmeter.config.Arguments; 36 import org.apache.jmeter.config.ConfigTestElement; 37 import org.apache.jmeter.gui.util.HorizontalPanel; 38 import org.apache.jmeter.gui.util.VerticalPanel; 39 import org.apache.jmeter.protocol.http.gui.HTTPArgumentsPanel; 40 import org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase; 41 import org.apache.jmeter.protocol.http.util.HTTPArgument; 42 import org.apache.jmeter.testelement.AbstractTestElement; 43 import org.apache.jmeter.testelement.TestElement; 44 import org.apache.jmeter.testelement.property.BooleanProperty; 45 import org.apache.jmeter.testelement.property.TestElementProperty; 46 import org.apache.jmeter.util.JMeterUtils; 47 import org.apache.jorphan.gui.JLabeledChoice; 48 49 /** 50 * Basic URL / HTTP Request configuration: 51 * - host and port 52 * - connect and response timeouts 53 * - path, method, encoding, parameters 54 * - redirects & keepalive 55 */ 56 public class UrlConfigGui extends JPanel implements ChangeListener { 57 private HTTPArgumentsPanel argsPanel; 58 59 private JTextField domain; 60 61 private JTextField port; 62 63 private JTextField connectTimeOut; 64 65 private JTextField responseTimeOut; 66 67 private JTextField protocol; 68 69 private JTextField contentEncoding; 70 71 private JTextField path; 72 73 private JCheckBox followRedirects; 74 75 private JCheckBox autoRedirects; 76 77 private JCheckBox useKeepAlive; 78 79 private JCheckBox useMultipartForPost; 80 81 private JLabeledChoice method; 82 83 private final boolean notConfigOnly; 84 // set this true to suppress some items for use in HTTP Request defaults 85 86 public UrlConfigGui() { 87 notConfigOnly=true; 88 init(); 89 } 90 91 public UrlConfigGui(boolean value) { 92 notConfigOnly=value; 93 init(); 94 } 95 96 public void clear() { 97 domain.setText(""); // $NON-NLS-1$ 98 if (notConfigOnly){ 99 followRedirects.setSelected(false); 100 autoRedirects.setSelected(true); 101 method.setText(HTTPSamplerBase.DEFAULT_METHOD); 102 useKeepAlive.setSelected(true); 103 useMultipartForPost.setSelected(false); 104 } 105 path.setText(""); // $NON-NLS-1$ 106 port.setText(""); // $NON-NLS-1$ 107 connectTimeOut.setText(""); // $NON-NLS-1$ 108 responseTimeOut.setText(""); // $NON-NLS-1$ 109 protocol.setText(""); // $NON-NLS-1$ 110 contentEncoding.setText(""); // $NON-NLS-1$ 111 argsPanel.clear(); 112 } 113 114 public TestElement createTestElement() { 115 ConfigTestElement element = new ConfigTestElement(); 116 117 element.setName(this.getName()); 118 element.setProperty(TestElement.GUI_CLASS, this.getClass().getName()); 119 element.setProperty(TestElement.TEST_CLASS, element.getClass().getName()); 120 modifyTestElement(element); 121 return element; 122 } 123 124 /** 125 * Save the GUI values in the sampler. 126 * 127 * @param element 128 */ 129 public void modifyTestElement(TestElement element) { 130 Arguments args = (Arguments) argsPanel.createTestElement(); 131 132 HTTPArgument.convertArgumentsToHTTP(args); 133 element.setProperty(new TestElementProperty(HTTPSamplerBase.ARGUMENTS, args)); 134 element.setProperty(HTTPSamplerBase.DOMAIN, domain.getText()); 135 element.setProperty(HTTPSamplerBase.PORT, port.getText()); 136 element.setProperty(HTTPSamplerBase.CONNECT_TIMEOUT, connectTimeOut.getText()); 137 element.setProperty(HTTPSamplerBase.RESPONSE_TIMEOUT, responseTimeOut.getText()); 138 element.setProperty(HTTPSamplerBase.PROTOCOL, protocol.getText()); 139 element.setProperty(HTTPSamplerBase.CONTENT_ENCODING, contentEncoding.getText()); 140 element.setProperty(HTTPSamplerBase.PATH, path.getText()); 141 if (notConfigOnly){ 142 element.setProperty(HTTPSamplerBase.METHOD, method.getText()); 143 element.setProperty(new BooleanProperty(HTTPSamplerBase.FOLLOW_REDIRECTS, followRedirects.isSelected())); 144 element.setProperty(new BooleanProperty(HTTPSamplerBase.AUTO_REDIRECTS, autoRedirects.isSelected())); 145 element.setProperty(new BooleanProperty(HTTPSamplerBase.USE_KEEPALIVE, useKeepAlive.isSelected())); 146 element.setProperty(new BooleanProperty(HTTPSamplerBase.DO_MULTIPART_POST, useMultipartForPost.isSelected())); 147 } 148 } 149 150 /** 151 * Set the text, etc. in the UI. 152 * 153 * @param el 154 * contains the data to be displayed 155 */ 156 public void configure(TestElement el) { 157 setName(el.getName()); 158 argsPanel.configure((TestElement) el.getProperty(HTTPSamplerBase.ARGUMENTS).getObjectValue()); 159 domain.setText(el.getPropertyAsString(HTTPSamplerBase.DOMAIN)); 160 161 String portString = el.getPropertyAsString(HTTPSamplerBase.PORT); 162 163 // Only display the port number if it is meaningfully specified 164 if (portString.equals(HTTPSamplerBase.UNSPECIFIED_PORT_AS_STRING)) { 165 port.setText(""); // $NON-NLS-1$ 166 } else { 167 port.setText(portString); 168 } 169 connectTimeOut.setText(el.getPropertyAsString(HTTPSamplerBase.CONNECT_TIMEOUT)); 170 responseTimeOut.setText(el.getPropertyAsString(HTTPSamplerBase.RESPONSE_TIMEOUT)); 171 protocol.setText(el.getPropertyAsString(HTTPSamplerBase.PROTOCOL)); 172 contentEncoding.setText(el.getPropertyAsString(HTTPSamplerBase.CONTENT_ENCODING)); 173 path.setText(el.getPropertyAsString(HTTPSamplerBase.PATH)); 174 if (notConfigOnly){ 175 method.setText(el.getPropertyAsString(HTTPSamplerBase.METHOD)); 176 followRedirects.setSelected(((AbstractTestElement) el).getPropertyAsBoolean(HTTPSamplerBase.FOLLOW_REDIRECTS)); 177 autoRedirects.setSelected(((AbstractTestElement) el).getPropertyAsBoolean(HTTPSamplerBase.AUTO_REDIRECTS)); 178 useKeepAlive.setSelected(((AbstractTestElement) el).getPropertyAsBoolean(HTTPSamplerBase.USE_KEEPALIVE)); 179 useMultipartForPost.setSelected(((AbstractTestElement) el).getPropertyAsBoolean(HTTPSamplerBase.DO_MULTIPART_POST)); 180 } 181 } 182 183 private void init() {// called from ctor, so must not be overridable 184 this.setLayout(new BorderLayout()); 185 186 // WEB REQUEST PANEL 187 JPanel webRequestPanel = new JPanel(); 188 webRequestPanel.setLayout(new BorderLayout()); 189 webRequestPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), 190 JMeterUtils.getResString("web_request"))); // $NON-NLS-1$ 191 192 JPanel northPanel = new JPanel(); 193 northPanel.setLayout(new BoxLayout(northPanel, BoxLayout.Y_AXIS)); 194 northPanel.add(getProtocolAndMethodPanel()); 195 northPanel.add(getPathPanel()); 196 197 webRequestPanel.add(northPanel, BorderLayout.NORTH); 198 webRequestPanel.add(getParameterPanel(), BorderLayout.CENTER); 199 200 this.add(getWebServerTimeoutPanel(), BorderLayout.NORTH); 201 this.add(webRequestPanel, BorderLayout.CENTER); 202 } 203 204 /** 205 * Create a panel containing the webserver (domain+port) and timeouts (connect+request). 206 * 207 * @return the panel 208 */ 209 protected final JPanel getWebServerTimeoutPanel() { 210 // WEB SERVER PANEL 211 JPanel webServerPanel = new HorizontalPanel(); 212 webServerPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), 213 JMeterUtils.getResString("web_server"))); // $NON-NLS-1$ 214 final JPanel domainPanel = getDomainPanel(); 215 final JPanel portPanel = getPortPanel(); 216 webServerPanel.add(domainPanel, BorderLayout.CENTER); 217 webServerPanel.add(portPanel, BorderLayout.EAST); 218 219 JPanel timeOut = new HorizontalPanel(); 220 timeOut.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), 221 JMeterUtils.getResString("web_server_timeout_title"))); // $NON-NLS-1$ 222 final JPanel connPanel = getConnectTimeOutPanel(); 223 final JPanel reqPanel = getResponseTimeOutPanel(); 224 timeOut.add(connPanel); 225 timeOut.add(reqPanel); 226 227 228 JPanel webServerTimeoutPanel = new VerticalPanel(); 229 webServerTimeoutPanel.add(webServerPanel, BorderLayout.CENTER); 230 webServerTimeoutPanel.add(timeOut, BorderLayout.EAST); 231 return webServerTimeoutPanel; 232 } 233 234 private JPanel getPortPanel() { 235 port = new JTextField(4); 236 237 JLabel label = new JLabel(JMeterUtils.getResString("web_server_port")); // $NON-NLS-1$ 238 label.setLabelFor(port); 239 240 JPanel panel = new JPanel(new BorderLayout(5, 0)); 241 panel.add(label, BorderLayout.WEST); 242 panel.add(port, BorderLayout.CENTER); 243 244 return panel; 245 } 246 247 private JPanel getConnectTimeOutPanel() { 248 connectTimeOut = new JTextField(4); 249 250 JLabel label = new JLabel(JMeterUtils.getResString("web_server_timeout_connect")); // $NON-NLS-1$ 251 label.setLabelFor(connectTimeOut); 252 253 JPanel panel = new JPanel(new BorderLayout(5, 0)); 254 panel.add(label, BorderLayout.WEST); 255 panel.add(connectTimeOut, BorderLayout.CENTER); 256 257 return panel; 258 } 259 260 private JPanel getResponseTimeOutPanel() { 261 responseTimeOut = new JTextField(4); 262 263 JLabel label = new JLabel(JMeterUtils.getResString("web_server_timeout_response")); // $NON-NLS-1$ 264 label.setLabelFor(responseTimeOut); 265 266 JPanel panel = new JPanel(new BorderLayout(5, 0)); 267 panel.add(label, BorderLayout.WEST); 268 panel.add(responseTimeOut, BorderLayout.CENTER); 269 270 return panel; 271 } 272 273 private JPanel getDomainPanel() { 274 domain = new JTextField(20); 275 276 JLabel label = new JLabel(JMeterUtils.getResString("web_server_domain")); // $NON-NLS-1$ 277 label.setLabelFor(domain); 278 279 JPanel panel = new JPanel(new BorderLayout(5, 0)); 280 panel.add(label, BorderLayout.WEST); 281 panel.add(domain, BorderLayout.CENTER); 282 return panel; 283 } 284 285 /** 286 * This method defines the Panel for the HTTP path, 'Follow Redirects' 287 * 'Use KeepAlive', and 'Use multipart for HTTP POST' elements. 288 * 289 * @return JPanel The Panel for the path, 'Follow Redirects' and 'Use 290 * KeepAlive' elements. 291 */ 292 protected Component getPathPanel() { 293 path = new JTextField(15); 294 295 JLabel label = new JLabel(JMeterUtils.getResString("path")); //$NON-NLS-1$ 296 label.setLabelFor(path); 297 298 if (notConfigOnly){ 299 followRedirects = new JCheckBox(JMeterUtils.getResString("follow_redirects")); // $NON-NLS-1$ 300 followRedirects.setSelected(false); 301 302 autoRedirects = new JCheckBox(JMeterUtils.getResString("follow_redirects_auto")); //$NON-NLS-1$ 303 autoRedirects.addChangeListener(this); 304 autoRedirects.setSelected(true);// Default changed in 2.3 305 306 useKeepAlive = new JCheckBox(JMeterUtils.getResString("use_keepalive")); // $NON-NLS-1$ 307 useKeepAlive.setSelected(true); 308 309 useMultipartForPost = new JCheckBox(JMeterUtils.getResString("use_multipart_for_http_post")); // $NON-NLS-1$ 310 useMultipartForPost.setSelected(false); 311 } 312 313 JPanel pathPanel = new JPanel(new BorderLayout(5, 0)); 314 pathPanel.add(label, BorderLayout.WEST); 315 pathPanel.add(path, BorderLayout.CENTER); 316 pathPanel.setMinimumSize(pathPanel.getPreferredSize()); 317 318 JPanel panel = new JPanel(); 319 panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); 320 panel.add(pathPanel); 321 if (notConfigOnly){ 322 JPanel optionPanel = new JPanel(new FlowLayout(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 // CONTENT_ENCODING 338 contentEncoding = new JTextField(10); 339 340 JLabel protocolLabel = new JLabel(JMeterUtils.getResString("protocol")); // $NON-NLS-1$ 341 protocolLabel.setLabelFor(protocol); 342 JLabel contentEncodingLabel = new JLabel(JMeterUtils.getResString("content_encoding")); // $NON-NLS-1$ 343 protocolLabel.setLabelFor(contentEncoding); 344 if (notConfigOnly){ 345 method = new JLabeledChoice(JMeterUtils.getResString("method"), // $NON-NLS-1$ 346 HTTPSamplerBase.getValidMethodsAsArray()); 347 } 348 349 JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT)); 350 351 panel.add(protocolLabel); 352 panel.add(protocol); 353 panel.add(Box.createHorizontalStrut(5)); 354 355 if (notConfigOnly){ 356 panel.add(method); 357 } 358 panel.setMinimumSize(panel.getPreferredSize()); 359 panel.add(Box.createHorizontalStrut(5)); 360 361 panel.add(contentEncodingLabel); 362 panel.add(contentEncoding); 363 return panel; 364 } 365 366 protected JPanel getParameterPanel() { 367 argsPanel = new HTTPArgumentsPanel(); 368 369 return argsPanel; 370 } 371 372 // Disable follow redirects if Autoredirect is selected 373 public void stateChanged(ChangeEvent e) { 374 if (e.getSource() == autoRedirects){ 375 if (autoRedirects.isSelected()) { 376 followRedirects.setEnabled(false); 377 } else { 378 followRedirects.setEnabled(true); 379 } 380 } 381 } 382 }