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 / +191 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 ValidationTabbedPane postContentTabbedPane;
120
104
    public UrlConfigGui() {
121
    public UrlConfigGui() {
105
        this(true);
122
        this(true);
106
    }
123
    }
Lines 139-144 Link Here
139
        protocol.setText(""); // $NON-NLS-1$
156
        protocol.setText(""); // $NON-NLS-1$
140
        contentEncoding.setText(""); // $NON-NLS-1$
157
        contentEncoding.setText(""); // $NON-NLS-1$
141
        argsPanel.clear();
158
        argsPanel.clear();
159
        postBodyContent.setText("");// $NON-NLS-1$
160
        postContentTabbedPane.setSelectedIndex(TAB_PARAMETERS, false);
142
    }
161
    }
143
162
144
    public TestElement createTestElement() {
163
    public TestElement createTestElement() {
Lines 157-165 Link Here
157
     * @param element
176
     * @param element
158
     */
177
     */
159
    public void modifyTestElement(TestElement element) {
178
    public void modifyTestElement(TestElement element) {
160
        Arguments args = (Arguments) argsPanel.createTestElement();
179
        boolean useRaw = postContentTabbedPane.getSelectedIndex()==TAB_RAW_BODY;
161
180
        Arguments args;
162
        HTTPArgument.convertArgumentsToHTTP(args);
181
        if(useRaw) {
182
            args = new Arguments();
183
            HTTPArgument arg = new HTTPArgument("", postBodyContent.getText(), true);
184
            arg.setAlwaysEncoded(false);
185
            args.addArgument(arg);
186
        } else {
187
            args = (Arguments) argsPanel.createTestElement();
188
            HTTPArgument.convertArgumentsToHTTP(args);
189
        }
190
        element.setProperty(POST_BODY_RAW, useRaw, POST_BODY_RAW_DEFAULT);
163
        element.setProperty(new TestElementProperty(HTTPSamplerBase.ARGUMENTS, args));
191
        element.setProperty(new TestElementProperty(HTTPSamplerBase.ARGUMENTS, args));
164
        element.setProperty(HTTPSamplerBase.DOMAIN, domain.getText());
192
        element.setProperty(HTTPSamplerBase.DOMAIN, domain.getText());
165
        element.setProperty(HTTPSamplerBase.PORT, port.getText());
193
        element.setProperty(HTTPSamplerBase.PORT, port.getText());
Lines 185-190 Link Here
185
        }
213
        }
186
    }
214
    }
187
215
216
    // FIXME FACTOR WITH HTTPHC4Impl, HTTPHC3Impl
217
    // Just append all the parameter values, and use that as the post body
218
    /**
219
     * Compute Post body from arguments
220
     * @param arguments {@link Arguments}
221
     * @return {@link String}
222
     */
223
    private static final String computePostBody(Arguments arguments) {
224
        StringBuilder postBody = new StringBuilder();
225
        PropertyIterator args = arguments.iterator();
226
        while (args.hasNext()) {
227
            HTTPArgument arg = (HTTPArgument) args.next().getObjectValue();
228
            String value = arg.getValue();
229
            postBody.append(value);
230
        }
231
        return postBody.toString();
232
    }
233
188
    /**
234
    /**
189
     * Set the text, etc. in the UI.
235
     * Set the text, etc. in the UI.
190
     *
236
     *
Lines 193-199 Link Here
193
     */
239
     */
194
    public void configure(TestElement el) {
240
    public void configure(TestElement el) {
195
        setName(el.getName());
241
        setName(el.getName());
196
        argsPanel.configure((TestElement) el.getProperty(HTTPSamplerBase.ARGUMENTS).getObjectValue());
242
        Arguments arguments = (Arguments) el.getProperty(HTTPSamplerBase.ARGUMENTS).getObjectValue();
243
244
        boolean useRaw = el.getPropertyAsBoolean(POST_BODY_RAW, POST_BODY_RAW_DEFAULT);
245
        if(useRaw) {
246
            String postBody = computePostBody(arguments);
247
            postBodyContent.setText(postBody);   
248
            postContentTabbedPane.setSelectedIndex(TAB_RAW_BODY, false);
249
        } else {
250
            argsPanel.configure(arguments);
251
            postContentTabbedPane.setSelectedIndex(TAB_PARAMETERS, false);
252
        }
253
197
        domain.setText(el.getPropertyAsString(HTTPSamplerBase.DOMAIN));
254
        domain.setText(el.getPropertyAsString(HTTPSamplerBase.DOMAIN));
198
255
199
        String portString = el.getPropertyAsString(HTTPSamplerBase.PORT);
256
        String portString = el.getPropertyAsString(HTTPSamplerBase.PORT);
Lines 504-515 Link Here
504
        return panel;
561
        return panel;
505
    }
562
    }
506
563
507
    protected JPanel getParameterPanel() {
564
    protected JTabbedPane getParameterPanel() {
565
        postContentTabbedPane = new ValidationTabbedPane();
508
        argsPanel = new HTTPArgumentsPanel();
566
        argsPanel = new HTTPArgumentsPanel();
509
567
        postBodyContent = new JLabeledTextArea(JMeterUtils.getResString("post_body_raw"));// $NON-NLS-1$
510
        return argsPanel;
568
        postContentTabbedPane.add(JMeterUtils.getResString("post_as_parameters"), argsPanel);// $NON-NLS-1$
569
        postContentTabbedPane.add(JMeterUtils.getResString("post_body"), postBodyContent);// $NON-NLS-1$
570
        return postContentTabbedPane;
511
    }
571
    }
512
572
573
    /**
574
     * 
575
     */
576
    class ValidationTabbedPane extends JTabbedPane{
577
578
        /**
579
         * 
580
         */
581
        private static final long serialVersionUID = 7014311238367882880L;
582
583
        /* (non-Javadoc)
584
         * @see javax.swing.JTabbedPane#setSelectedIndex(int)
585
         */
586
        @Override
587
        public void setSelectedIndex(int index) {
588
            setSelectedIndex(index, true);
589
        }
590
        /**
591
         * Apply some check rules if check is true
592
         */
593
        public void setSelectedIndex(int index, boolean check) {
594
            int oldSelectedIndex = getSelectedIndex();
595
            if(!check || oldSelectedIndex==-1) {
596
                super.setSelectedIndex(index);
597
            }
598
            else if(index != this.getSelectedIndex())
599
            {
600
                if(noData(getSelectedIndex())) {
601
                    // If there is no data, then switching between Parameters and Raw should be
602
                    // allowed with no further user interaction.
603
                    argsPanel.clear();
604
                    postBodyContent.setText("");
605
                    super.setSelectedIndex(index);
606
                }
607
                else { 
608
                    if(oldSelectedIndex == TAB_RAW_BODY) {
609
                        // If RAW data and Parameters match we allow switching
610
                        if(postBodyContent.getText().equals(computePostBody((Arguments)argsPanel.createTestElement()).trim())) {
611
                            super.setSelectedIndex(index);
612
                        }
613
                        else {
614
                            // If there is data in the Raw panel, then the user should be 
615
                            // prevented from switching (that would be easy to track).
616
                            JOptionPane.showConfirmDialog(this,
617
                                    JMeterUtils.getResString("web_cannot_switch_tab"), // $NON-NLS-1$
618
                                    JMeterUtils.getResString("warning"), // $NON-NLS-1$
619
                                    JOptionPane.DEFAULT_OPTION, 
620
                                    JOptionPane.ERROR_MESSAGE);
621
                            return;
622
                        }
623
                    }
624
                    else {
625
                        // If the Parameter data can be converted (i.e. no names), we 
626
                        // warn the user that the Parameter data will be lost.
627
                        if(canConvertParameters()) {
628
                            Object[] options = {
629
                                    JMeterUtils.getResString("confirm"),
630
                                    JMeterUtils.getResString("cancel")};
631
                            int n = JOptionPane.showOptionDialog(this,
632
                                JMeterUtils.getResString("web_parameters_lost_message"),
633
                                JMeterUtils.getResString("warning"),
634
                                JOptionPane.YES_NO_CANCEL_OPTION,
635
                                JOptionPane.QUESTION_MESSAGE,
636
                                null,
637
                                options,
638
                                options[1]);
639
                            if(n == JOptionPane.YES_OPTION) {
640
                                convertParametersToRaw();
641
                                super.setSelectedIndex(index);
642
                            }
643
                            else{
644
                                return;
645
                            }
646
                        }
647
                        else {
648
                            // If the Parameter data cannot be converted to Raw, then the user should be
649
                            // prevented from doing so raise an error dialog
650
                            JOptionPane.showConfirmDialog(this,
651
                                    JMeterUtils.getResString("web_cannot_convert_parameters_to_raw"), // $NON-NLS-1$
652
                                    JMeterUtils.getResString("warning"), // $NON-NLS-1$
653
                                    JOptionPane.DEFAULT_OPTION, 
654
                                    JOptionPane.ERROR_MESSAGE);
655
                            return;
656
                        }
657
                    }
658
                }
659
            }
660
        }   
661
    }
513
    // autoRedirects and followRedirects cannot both be selected
662
    // autoRedirects and followRedirects cannot both be selected
514
    public void stateChanged(ChangeEvent e) {
663
    public void stateChanged(ChangeEvent e) {
515
        if (e.getSource() == autoRedirects){
664
        if (e.getSource() == autoRedirects){
Lines 523-526 Link Here
523
            }
672
            }
524
        }
673
        }
525
    }
674
    }
675
676
677
    /**
678
     * Convert Parameters to Raw Body
679
     */
680
    void convertParametersToRaw() {
681
        postBodyContent.setText(computePostBody((Arguments)argsPanel.createTestElement()));
682
    }
683
684
    /**
685
     * 
686
     * @return true if no argument has a name
687
     */
688
    boolean canConvertParameters() {
689
        Arguments arguments = (Arguments)argsPanel.createTestElement();
690
        for (int i = 0; i < arguments.getArgumentCount(); i++) {
691
            if(!StringUtils.isEmpty(arguments.getArgument(i).getName())) {
692
                return false;
693
            }
694
        }
695
        return true;
696
    }
697
698
    /**
699
     * @return true if neither Parameters tab nor Raw Body tab contain data
700
     */
701
    boolean noData(int oldSelectedIndex) {
702
        if(oldSelectedIndex == TAB_RAW_BODY) {
703
            return StringUtils.isEmpty(postBodyContent.getText().trim());
704
        }
705
        else {
706
            Arguments element = (Arguments)argsPanel.createTestElement();
707
            return StringUtils.isEmpty(computePostBody(element));
708
        }
709
    }
526
}
710
}
(-)src/core/org/apache/jmeter/resources/messages.properties (+8 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\nto raw body and loose parameters table when you select \nanother node or save test plan, do you confirm ?
1034
web_cannot_convert_parameters_to_raw=Cannot convert parameters to RAW Post body \nbecause one of the parameters has a name
1035
web_cannot_switch_tab=You cannot switch because data cannot be converted\n to target Tab data, empty data to switch
1036
confirm=Confirm
1037
post_as_parameters=Parameters
1038
post_as_rawbody=RAW Body
1039
post_body_raw=Raw Post Body
1040
post_body=Post Body
1033
web_testing2_source_ip=Source IP address:
1041
web_testing2_source_ip=Source IP address:
1034
web_testing2_title=HTTP Request HTTPClient
1042
web_testing2_title=HTTP Request HTTPClient
1035
web_testing_concurrent_download=Use concurrent pool. Size:
1043
web_testing_concurrent_download=Use concurrent pool. Size:

Return to bug 51861