View | Details | Raw Unified | Return to bug 51861
Collapse All | Expand All

(-)src/protocol/http/org/apache/jmeter/protocol/http/config/gui/UrlConfigGui.java (-7 / +170 lines)
Lines 27-38 Link Here
27
import javax.swing.BoxLayout;
27
import javax.swing.BoxLayout;
28
import javax.swing.JCheckBox;
28
import javax.swing.JCheckBox;
29
import javax.swing.JLabel;
29
import javax.swing.JLabel;
30
import javax.swing.JOptionPane;
30
import javax.swing.JPanel;
31
import javax.swing.JPanel;
31
import javax.swing.JPasswordField;
32
import javax.swing.JPasswordField;
33
import javax.swing.JTabbedPane;
32
import javax.swing.JTextField;
34
import javax.swing.JTextField;
33
import javax.swing.event.ChangeEvent;
35
import javax.swing.event.ChangeEvent;
34
import javax.swing.event.ChangeListener;
36
import javax.swing.event.ChangeListener;
35
37
38
import org.apache.commons.lang.StringUtils;
36
import org.apache.jmeter.config.Arguments;
39
import org.apache.jmeter.config.Arguments;
37
import org.apache.jmeter.config.ConfigTestElement;
40
import org.apache.jmeter.config.ConfigTestElement;
38
import org.apache.jmeter.gui.util.HorizontalPanel;
41
import org.apache.jmeter.gui.util.HorizontalPanel;
Lines 43-51 Link Here
43
import org.apache.jmeter.protocol.http.util.HTTPArgument;
46
import org.apache.jmeter.protocol.http.util.HTTPArgument;
44
import org.apache.jmeter.testelement.TestElement;
47
import org.apache.jmeter.testelement.TestElement;
45
import org.apache.jmeter.testelement.property.BooleanProperty;
48
import org.apache.jmeter.testelement.property.BooleanProperty;
49
import org.apache.jmeter.testelement.property.PropertyIterator;
46
import org.apache.jmeter.testelement.property.TestElementProperty;
50
import org.apache.jmeter.testelement.property.TestElementProperty;
47
import org.apache.jmeter.util.JMeterUtils;
51
import org.apache.jmeter.util.JMeterUtils;
48
import org.apache.jorphan.gui.JLabeledChoice;
52
import org.apache.jorphan.gui.JLabeledChoice;
53
import org.apache.jorphan.gui.JLabeledTextArea;
49
54
50
/**
55
/**
51
 * Basic URL / HTTP Request configuration:
56
 * Basic URL / HTTP Request configuration:
Lines 58-63 Link Here
58
63
59
    private static final long serialVersionUID = 240L;
64
    private static final long serialVersionUID = 240L;
60
65
66
    private static final int TAB_PARAMETERS = 0;
67
    private static final int TAB_RAW_BODY = 1;
68
    public static final String POST_BODY_RAW = "HTTPSampler.postBodyRaw"; // TODO - belongs elsewhere 
69
70
    private static final boolean POST_BODY_RAW_DEFAULT = false;
71
61
    private HTTPArgumentsPanel argsPanel;
72
    private HTTPArgumentsPanel argsPanel;
62
73
63
    private JTextField domain;
74
    private JTextField domain;
Lines 101-106 Link Here
101
    
112
    
102
    private final boolean showImplementation; // Set false for AJP
113
    private final boolean showImplementation; // Set false for AJP
103
114
115
    // Raw POST Body 
116
    private JLabeledTextArea postBodyContent;
117
118
    // Tabbed pane that contains parameters and raw body
119
    private JTabbedPane postContentTabbedPane;
120
121
    private int selectedTPIndex;
122
104
    public UrlConfigGui() {
123
    public UrlConfigGui() {
105
        this(true);
124
        this(true);
106
    }
125
    }
Lines 139-144 Link Here
139
        protocol.setText(""); // $NON-NLS-1$
158
        protocol.setText(""); // $NON-NLS-1$
140
        contentEncoding.setText(""); // $NON-NLS-1$
159
        contentEncoding.setText(""); // $NON-NLS-1$
141
        argsPanel.clear();
160
        argsPanel.clear();
161
        postBodyContent.setText("");// $NON-NLS-1$
162
        this.selectedTPIndex=TAB_PARAMETERS;
163
        postContentTabbedPane.setSelectedIndex(selectedTPIndex);
142
    }
164
    }
143
165
144
    public TestElement createTestElement() {
166
    public TestElement createTestElement() {
Lines 157-165 Link Here
157
     * @param element
179
     * @param element
158
     */
180
     */
159
    public void modifyTestElement(TestElement element) {
181
    public void modifyTestElement(TestElement element) {
160
        Arguments args = (Arguments) argsPanel.createTestElement();
182
        boolean useRaw = postContentTabbedPane.getSelectedIndex()==TAB_RAW_BODY;
161
183
        Arguments args;
162
        HTTPArgument.convertArgumentsToHTTP(args);
184
        if(useRaw) {
185
            args = new Arguments();
186
            HTTPArgument arg = new HTTPArgument("", postBodyContent.getText(), true);
187
            arg.setAlwaysEncoded(false);
188
            args.addArgument(arg);
189
        } else {
190
            args = (Arguments) argsPanel.createTestElement();
191
            HTTPArgument.convertArgumentsToHTTP(args);
192
        }
193
        element.setProperty(POST_BODY_RAW, useRaw, POST_BODY_RAW_DEFAULT);
163
        element.setProperty(new TestElementProperty(HTTPSamplerBase.ARGUMENTS, args));
194
        element.setProperty(new TestElementProperty(HTTPSamplerBase.ARGUMENTS, args));
164
        element.setProperty(HTTPSamplerBase.DOMAIN, domain.getText());
195
        element.setProperty(HTTPSamplerBase.DOMAIN, domain.getText());
165
        element.setProperty(HTTPSamplerBase.PORT, port.getText());
196
        element.setProperty(HTTPSamplerBase.PORT, port.getText());
Lines 185-190 Link Here
185
        }
216
        }
186
    }
217
    }
187
218
219
    // FIXME FACTOR WITH HTTPHC4Impl, HTTPHC3Impl
220
    // Just append all the parameter values, and use that as the post body
221
    /**
222
     * Compute Post body from arguments
223
     * @param arguments {@link Arguments}
224
     * @return {@link String}
225
     */
226
    private static final String computePostBody(Arguments arguments) {
227
        StringBuilder postBody = new StringBuilder();
228
        PropertyIterator args = arguments.iterator();
229
        while (args.hasNext()) {
230
            HTTPArgument arg = (HTTPArgument) args.next().getObjectValue();
231
            String value = arg.getValue();
232
            postBody.append(value);
233
        }
234
        return postBody.toString();
235
    }
236
188
    /**
237
    /**
189
     * Set the text, etc. in the UI.
238
     * Set the text, etc. in the UI.
190
     *
239
     *
Lines 193-199 Link Here
193
     */
242
     */
194
    public void configure(TestElement el) {
243
    public void configure(TestElement el) {
195
        setName(el.getName());
244
        setName(el.getName());
196
        argsPanel.configure((TestElement) el.getProperty(HTTPSamplerBase.ARGUMENTS).getObjectValue());
245
        Arguments arguments = (Arguments) el.getProperty(HTTPSamplerBase.ARGUMENTS).getObjectValue();
246
247
        boolean useRaw = el.getPropertyAsBoolean(POST_BODY_RAW, POST_BODY_RAW_DEFAULT);
248
        if(useRaw) {
249
            String postBody = computePostBody(arguments);
250
            postBodyContent.setText(postBody);   
251
            this.selectedTPIndex=TAB_RAW_BODY;
252
            postContentTabbedPane.setSelectedIndex(selectedTPIndex);
253
        } else {
254
            argsPanel.configure(arguments);
255
            this.selectedTPIndex=TAB_PARAMETERS;
256
            postContentTabbedPane.setSelectedIndex(this.selectedTPIndex);
257
        }
258
197
        domain.setText(el.getPropertyAsString(HTTPSamplerBase.DOMAIN));
259
        domain.setText(el.getPropertyAsString(HTTPSamplerBase.DOMAIN));
198
260
199
        String portString = el.getPropertyAsString(HTTPSamplerBase.PORT);
261
        String portString = el.getPropertyAsString(HTTPSamplerBase.PORT);
Lines 504-513 Link Here
504
        return panel;
566
        return panel;
505
    }
567
    }
506
568
507
    protected JPanel getParameterPanel() {
569
    protected JTabbedPane getParameterPanel() {
570
        postContentTabbedPane = new JTabbedPane();
508
        argsPanel = new HTTPArgumentsPanel();
571
        argsPanel = new HTTPArgumentsPanel();
509
572
        postBodyContent = new JLabeledTextArea(JMeterUtils.getResString("post_body_raw"));// $NON-NLS-1$
510
        return argsPanel;
573
        postContentTabbedPane.add(JMeterUtils.getResString("post_as_parameters"), argsPanel);// $NON-NLS-1$
574
        postContentTabbedPane.add(JMeterUtils.getResString("post_body"), postBodyContent);// $NON-NLS-1$
575
        postContentTabbedPane.addChangeListener(this);
576
        return postContentTabbedPane;
511
    }
577
    }
512
578
513
    // autoRedirects and followRedirects cannot both be selected
579
    // autoRedirects and followRedirects cannot both be selected
Lines 522-526 Link Here
522
                autoRedirects.setSelected(false);
588
                autoRedirects.setSelected(false);
523
            }
589
            }
524
        }
590
        }
591
        if(e.getSource() == postContentTabbedPane)
592
        {
593
            if(this.selectedTPIndex != postContentTabbedPane.getSelectedIndex())
594
            {
595
                if(noData()) {
596
                    // If there is no data, then switching between Parameters and Raw should be
597
                    // allowed with no further user interaction.
598
                    this.argsPanel.clear();
599
                    this.postBodyContent.setText("");
600
                    this.selectedTPIndex=postContentTabbedPane.getSelectedIndex();
601
                }
602
                else { 
603
                    if(this.selectedTPIndex == TAB_RAW_BODY) {
604
                        // If RAW data and Parameters match we allow switching
605
                        if(postBodyContent.getText().equals(computePostBody((Arguments)argsPanel.createTestElement()).trim())) {
606
                            this.selectedTPIndex=postContentTabbedPane.getSelectedIndex();
607
                        }
608
                        else {
609
                            // If there is data in the Raw panel, then the user should be 
610
                            // prevented from switching (that would be easy to track).
611
                            postContentTabbedPane.setSelectedIndex(selectedTPIndex);
612
                        }
613
                    }
614
                    else {
615
                        // If the Parameter data can be converted (i.e. no names), we 
616
                        // warn the user that the Parameter data will be lost.
617
                        if(canConvertParameters()) {
618
                            Object[] options = {
619
                                    JMeterUtils.getResString("confirm"),
620
                                    JMeterUtils.getResString("cancel")};
621
                            int n = JOptionPane.showOptionDialog(this,
622
                                JMeterUtils.getResString("web_parameters_lost_message"),
623
                                JMeterUtils.getResString("warning"),
624
                                JOptionPane.YES_NO_CANCEL_OPTION,
625
                                JOptionPane.QUESTION_MESSAGE,
626
                                null,
627
                                options,
628
                                options[1]);
629
                            if(n == JOptionPane.YES_OPTION) {
630
                                convertParametersToRaw();
631
                                this.selectedTPIndex=postContentTabbedPane.getSelectedIndex();
632
                            }
633
                            else{
634
                                postContentTabbedPane.setSelectedIndex(selectedTPIndex);                    
635
                            }
636
                        }
637
                        else {
638
                            // If the Parameter data cannot be converted to Raw, then the user should be
639
                            // prevented from doing so raise an error dialog
640
                            JOptionPane.showConfirmDialog(this,
641
                                    JMeterUtils.getResString("web_cannot_convert_parameters_to_raw"), // $NON-NLS-1$
642
                                    JMeterUtils.getResString("warning"), // $NON-NLS-1$
643
                                    JOptionPane.DEFAULT_OPTION, 
644
                                    JOptionPane.ERROR_MESSAGE);
645
                            postContentTabbedPane.setSelectedIndex(selectedTPIndex);
646
                        }
647
                    }
648
                }
649
            }
650
        }
651
    }
652
653
    /**
654
     * Convert Parameters to Raw Body
655
     */
656
    private void convertParametersToRaw() {
657
        postBodyContent.setText(computePostBody((Arguments)argsPanel.createTestElement()));
658
    }
659
660
    /**
661
     * 
662
     * @return true if no argument has a name
663
     */
664
    private boolean canConvertParameters() {
665
        Arguments arguments = (Arguments)argsPanel.createTestElement();
666
        for (int i = 0; i < arguments.getArgumentCount(); i++) {
667
            if(!StringUtils.isEmpty(arguments.getArgument(i).getName()))
668
            {
669
                return false;
670
            }
671
        }
672
        return true;
673
    }
674
675
    /**
676
     * @return true if neither Parameters tab nor Raw Body tab contain data
677
     */
678
    private boolean noData() {
679
        if(selectedTPIndex == TAB_RAW_BODY)
680
        {
681
            return StringUtils.isEmpty(postBodyContent.getText().trim());
682
        }
683
        else
684
        {
685
            Arguments element = (Arguments)argsPanel.createTestElement();
686
            return element.getArgumentCount()==0;
687
        }
525
    }
688
    }
526
}
689
}
(-)src/core/org/apache/jmeter/resources/messages.properties (+7 lines)
Lines 1030-1035 Link Here
1030
web_server_client=Client implementation:
1030
web_server_client=Client implementation:
1031
web_server_domain=Server Name or IP\:
1031
web_server_domain=Server Name or IP\:
1032
web_server_port=Port Number\:
1032
web_server_port=Port Number\:
1033
web_parameters_lost_message=Switching to RAW Post body will convert parameters to raw body and loose\n parameters table when you select another node or save test plan, do you confirm ?
1034
web_cannot_convert_parameters_to_raw=Cannot convert parameters to RAW Post body because one of the parameters has a name
1035
confirm=Confirm
1036
post_as_parameters=Parameters
1037
post_as_rawbody=RAW Body
1038
post_body_raw=Raw Post Body
1039
post_body=Post Body
1033
web_testing2_source_ip=Source IP address:
1040
web_testing2_source_ip=Source IP address:
1034
web_testing2_title=HTTP Request HTTPClient
1041
web_testing2_title=HTTP Request HTTPClient
1035
web_testing_concurrent_download=Use concurrent pool. Size:
1042
web_testing_concurrent_download=Use concurrent pool. Size:

Return to bug 51861