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

(-)bin/jmeter.properties (-1 / +4 lines)
Lines 912-915 Link Here
912
909
913
# Should JMeter automatically load additional system properties?
910
# Should JMeter automatically load additional system properties?
914
# File name to look for (comment to disable)
911
# File name to look for (comment to disable)
915
system.properties=system.properties
912
system.properties=system.properties
913
914
# Content type for Java Serialisation 
915
java_serial_content_types=application/hraccess,application/octet-stream
(-)src/core/org/apache/jmeter/resources/messages.properties (+2 lines)
Lines 404-409 Link Here
404
jar_file=Jar Files
404
jar_file=Jar Files
405
java_request=Java Request
405
java_request=Java Request
406
java_request_defaults=Java Request Defaults
406
java_request_defaults=Java Request Defaults
407
java_serialized_sampler_title=Java Serialized
407
javascript_expression=JavaScript expression to evaluate
408
javascript_expression=JavaScript expression to evaluate
408
jexl_expression=JEXL expression to evaluate
409
jexl_expression=JEXL expression to evaluate
409
jms_auth_required=Required
410
jms_auth_required=Required
Lines 1227-1232 Link Here
1227
xpath_tidy_quiet=Quiet
1228
xpath_tidy_quiet=Quiet
1228
xpath_tidy_report_errors=Report errors
1229
xpath_tidy_report_errors=Report errors
1229
xpath_tidy_show_warnings=Show warnings
1230
xpath_tidy_show_warnings=Show warnings
1231
xstream_serialized_object=Xstream Serialized Data
1230
you_must_enter_a_valid_number=You must enter a valid number
1232
you_must_enter_a_valid_number=You must enter a valid number
1231
zh_cn=Chinese (Simplified)
1233
zh_cn=Chinese (Simplified)
1232
zh_tw=Chinese (Traditional)
1234
zh_tw=Chinese (Traditional)
(-)src/protocol/http/org/apache/jmeter/protocol/http/control/gui/JavaSerializedSamplerGui.java (+198 lines)
Line 0 Link Here
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.control.gui;
20
21
import java.awt.BorderLayout;
22
import java.awt.Dimension;
23
import java.awt.GridBagLayout;
24
import java.awt.GridBagConstraints;
25
import java.awt.event.ActionEvent;
26
import java.awt.event.ActionListener;
27
28
29
import javax.swing.JPanel;
30
import javax.swing.event.ChangeListener;
31
import javax.swing.event.ChangeEvent;
32
33
import java.io.FileInputStream;
34
import java.io.ObjectInputStream;
35
import java.io.IOException;
36
37
import com.thoughtworks.xstream.XStream;
38
import com.thoughtworks.xstream.io.xml.DomDriver;
39
40
import org.apache.jmeter.protocol.http.sampler.JavaSerializedSampler;
41
import org.apache.jmeter.samplers.gui.AbstractSamplerGui;
42
import org.apache.jmeter.testelement.TestElement;
43
import org.apache.jmeter.util.JMeterUtils;
44
import org.apache.jmeter.gui.GuiPackage;
45
import org.apache.jmeter.gui.util.FilePanel;
46
import org.apache.jorphan.gui.JLabeledTextArea;
47
import org.apache.jorphan.gui.JLabeledTextField;
48
import org.apache.jorphan.util.JOrphanUtils;
49
50
public class JavaSerializedSamplerGui extends AbstractSamplerGui implements
51
        ActionListener {
52
    private static final long serialVersionUID = 240L;
53
54
    private JLabeledTextField urlField;
55
    private JLabeledTextArea javaSerial;
56
57
    private static final String[] EXTS = { "" }; // $NON-NLS-1$
58
    private FilePanel javaSerialFile = new FilePanel("", EXTS);
59
60
    public JavaSerializedSamplerGui() {
61
        init();
62
    }
63
64
    public String getLabelResource() {
65
        return "java_serialized_sampler_title"; //$NON-NLS-1$
66
    }
67
68
    /**
69
     * {@inheritDoc}
70
     */
71
    public TestElement createTestElement() {
72
        JavaSerializedSampler sampler = new JavaSerializedSampler();
73
        modifyTestElement(sampler);
74
        return sampler;
75
    }
76
77
    /**
78
     * Modifies a given TestElement to mirror the data in the gui components.
79
     * 
80
     * @see org.apache.jmeter.gui.JMeterGUIComponent#modifyTestElement(TestElement)
81
     */
82
    public void modifyTestElement(TestElement s) {
83
        this.configureTestElement(s);
84
        if (s instanceof JavaSerializedSampler) {
85
            JavaSerializedSampler sampler = (JavaSerializedSampler) s;
86
            sampler.setUrlData(urlField.getText());
87
            sampler.setXstreamData(javaSerial.getText());
88
        }
89
    }
90
91
    /**
92
     * Implements JMeterGUIComponent.clearGui
93
     */
94
    @Override
95
    public void clearGui() {
96
        super.clearGui();
97
98
        urlField.setText(""); //$NON-NLS-1$
99
        javaSerial.setText(""); //$NON-NLS-1$
100
        javaSerialFile.setFilename(""); //$NON-NLS-1$
101
    }
102
103
    private void init() {
104
        setLayout(new BorderLayout());
105
        setBorder(makeBorder());
106
107
        add(makeTitlePanel(), BorderLayout.NORTH);
108
109
        urlField = new JLabeledTextField(JMeterUtils.getResString("url"), 10); //$NON-NLS-1$
110
        javaSerial = new JLabeledTextArea(
111
                JMeterUtils.getResString("xstream_serialized_object")); //$NON-NLS-1$
112
113
        javaSerialFile.addChangeListener(new ChangeListener() {
114
            public void stateChanged(ChangeEvent ev) {
115
                loadfile(javaSerialFile);
116
            }
117
        });
118
        JPanel mainPanel = new JPanel(new BorderLayout());
119
        JPanel soapActionPanel = new JPanel();
120
        soapActionPanel.setLayout(new GridBagLayout());
121
        GridBagConstraints c = new GridBagConstraints();
122
        c.fill = GridBagConstraints.HORIZONTAL;
123
        c.gridwidth = 2;
124
        c.gridx = 0;
125
        c.gridy = 0;
126
        c.weightx = 1;
127
        soapActionPanel.add(urlField, c);
128
        c.fill = GridBagConstraints.NONE;
129
        c.gridwidth = 1;
130
        c.gridy = 1;
131
        c.weightx = 0;
132
133
        c.gridx = 1;
134
        c.fill = GridBagConstraints.HORIZONTAL;
135
        c.weightx = 1;
136
137
        c.fill = GridBagConstraints.HORIZONTAL;
138
        c.gridwidth = 2;
139
        c.gridy = 2;
140
        c.gridx = 0;
141
142
        mainPanel.add(soapActionPanel, BorderLayout.NORTH);
143
        mainPanel.add(javaSerial, BorderLayout.CENTER);
144
        mainPanel.add(javaSerialFile, BorderLayout.SOUTH);
145
146
        add(mainPanel, BorderLayout.CENTER);
147
    }
148
149
    /**
150
     * {@inheritDoc}
151
     */
152
    @Override
153
    public void configure(TestElement el) {
154
        super.configure(el);
155
        JavaSerializedSampler sampler = (JavaSerializedSampler) el;
156
        urlField.setText(sampler.getUrlData());
157
        javaSerial.setText(sampler.getXstreamData());
158
    }
159
160
    public void actionPerformed(ActionEvent event) {
161
        final Object eventSource = event.getSource();
162
        if (eventSource == javaSerialFile) {
163
164
        }
165
    }
166
167
    /**
168
     * {@inheritDoc}
169
     */
170
    @Override
171
    public Dimension getPreferredSize() {
172
        return getMinimumSize();
173
    }
174
175
    private void loadfile(FilePanel fp) {
176
        String filename = fp.getFilename();
177
        ObjectInputStream ois = null;
178
        try {
179
            XStream xstream = new XStream(new DomDriver());
180
            FileInputStream fichier = new FileInputStream(filename);
181
            ois = new ObjectInputStream(fichier);
182
            Object o = ois.readObject();
183
            javaSerial.setText(xstream.toXML(o));
184
            ois.close();
185
        } catch (IOException e) {
186
            GuiPackage.showErrorMessage(
187
                    "Error loading results file - could not open file",
188
                    "Result file loader");
189
        } catch (ClassNotFoundException e) {
190
            GuiPackage.showErrorMessage(
191
                    "Error loading results file - Class not found",
192
                    "Result file loader");
193
        } finally {
194
            JOrphanUtils.closeQuietly(ois);
195
        }
196
    }
197
}
198
*
(-)src/protocol/http/org/apache/jmeter/protocol/http/proxy/JavaSerialSamplerCreator.java (+89 lines)
Line 0 Link Here
1
package org.apache.jmeter.protocol.http.proxy;
2
3
import com.thoughtworks.xstream.XStream;
4
import com.thoughtworks.xstream.io.xml.DomDriver;
5
6
import java.io.IOException;
7
import java.util.Map;
8
9
import java.io.ObjectInputStream;
10
import java.io.ByteArrayInputStream;
11
12
import org.apache.jmeter.protocol.http.control.gui.JavaSerializedSamplerGui;
13
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase;
14
import org.apache.jmeter.protocol.http.sampler.JavaSerializedSampler;
15
import org.apache.jmeter.protocol.http.util.HTTPConstants;
16
import org.apache.jmeter.testelement.TestElement;
17
import org.apache.jmeter.util.JMeterUtils;
18
import org.apache.jorphan.logging.LoggingManager;
19
import org.apache.log.Logger;
20
21
public class JavaSerialSamplerCreator extends DefaultSamplerCreator {
22
    private static final Logger log = LoggingManager.getLoggerForClass();
23
24
    public String[] getManagedContentTypes() {
25
        String content_type_list = 
26
                JMeterUtils.getPropDefault("java_serial_content_types",""); // $NON-NLS-1$ 
27
        String[] s = content_type_list.split(",");
28
        log.error(s.toString());
29
        return s;
30
    }
31
32
    public HTTPSamplerBase createSampler(HttpRequestHdr request,
33
            Map<String, String> pageEncodings, Map<String, String> formEncodings) {
34
        String xmlstream = extractXstreamStream(request);
35
        HTTPSamplerBase sampler = null;
36
        /*
37
         * If the POST contains a valid JAVA Object, create a specific sampler
38
         * If not, use the default Sampler creator
39
         */
40
        if (xmlstream != null) {
41
            sampler = new JavaSerializedSampler();
42
            sampler.setProperty(TestElement.GUI_CLASS,
43
                    JavaSerializedSamplerGui.class.getName());
44
            ((JavaSerializedSampler) sampler).setXstreamData(xmlstream);
45
            ((JavaSerializedSampler) sampler).setUrlData(request.getUrl());
46
        }
47
        else {
48
            sampler = super
49
                    .createSampler(request, pageEncodings, formEncodings);
50
        }
51
        // Defaults
52
        sampler.setFollowRedirects(false);
53
        sampler.setUseKeepAlive(true);
54
        if (log.isDebugEnabled()) {
55
            log.debug("getSampler: sampler path = " + sampler.getPath()); // $NON-NLS-1$
56
        }
57
        return sampler;
58
    }
59
60
    protected void computeFromPostBody(HTTPSamplerBase sampler,
61
            HttpRequestHdr request) throws Exception {
62
        if (!(sampler instanceof JavaSerializedSampler)) {
63
            super.computeFromPostBody(sampler, request);
64
        }
65
    }
66
67
    protected String extractXstreamStream(HttpRequestHdr request) {
68
        String xmlstream = null;
69
        if (HTTPConstants.POST.equals(request.getMethod())) {
70
            byte[] postData = null;
71
72
            postData = request.getRawPostData();
73
            ByteArrayInputStream bais = new ByteArrayInputStream(postData);
74
            try {
75
                XStream xstream = new XStream(new DomDriver());
76
                ObjectInputStream ois = new ObjectInputStream(bais);
77
                Object o = ois.readObject();
78
                xmlstream = xstream.toXML(o);
79
            } catch (IOException e) {
80
                log.error("Error to extract JAVA Serialized Object", e); // $NON-NLS-1$
81
            } catch (ClassNotFoundException e) {
82
                log.error("Error to extract JAVA Serialized Object", e); // $NON-NLS-1$
83
            }
84
        }
85
        return xmlstream;
86
    }
87
88
}
89
*
(-)src/protocol/http/org/apache/jmeter/protocol/http/sampler/JavaSerializedSampler.java (+262 lines)
Line 0 Link Here
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.sampler;
20
21
22
import java.io.ByteArrayOutputStream;
23
24
import java.io.IOException;
25
import java.io.InputStream;
26
27
import java.io.ObjectOutputStream;
28
29
import java.net.MalformedURLException;
30
import java.net.URL;
31
import java.util.zip.GZIPInputStream;
32
33
import com.thoughtworks.xstream.XStream;
34
import com.thoughtworks.xstream.io.xml.DomDriver;
35
36
import org.apache.commons.httpclient.HttpClient;
37
import org.apache.commons.httpclient.methods.PostMethod;
38
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
39
import org.apache.jmeter.protocol.http.control.CacheManager;
40
import org.apache.jmeter.protocol.http.control.Header;
41
import org.apache.jmeter.protocol.http.control.HeaderManager;
42
import org.apache.jmeter.protocol.http.util.HTTPConstants;
43
import org.apache.jmeter.samplers.Interruptible;
44
45
import org.apache.jorphan.logging.LoggingManager;
46
import org.apache.jorphan.util.JOrphanUtils;
47
import org.apache.log.Logger;
48
49
import com.thoughtworks.xstream.XStream;
50
51
/**
52
 * Commons HTTPClient based Java Serialization sampler
53
 */
54
public class JavaSerializedSampler extends HTTPSampler2 implements
55
        Interruptible { // Implemented by parent class
56
    private static final Logger log = LoggingManager.getLoggerForClass();
57
58
    private static final long serialVersionUID = 240L;
59
60
    public static final String URL_DATA = "JavaSerializedSampler.url";//$NON-NLS-1$
61
62
    public static final String XSTREAM_DATA = "JavaSerializedSampler.xstream_data"; //$NON-NLS-1$
63
64
    private static final String ENCODING = "utf-8"; //$NON-NLS-1$ 
65
66
    public void setUrlData(String data) {
67
        setProperty(URL_DATA, data);
68
    }
69
70
    public String getUrlData() {
71
        return getPropertyAsString(URL_DATA);
72
    }
73
74
    public void setXstreamData(String data) {
75
        setProperty(XSTREAM_DATA, data);
76
    }
77
78
    public String getXstreamData() {
79
        return getPropertyAsString(XSTREAM_DATA);
80
    }
81
82
    protected void setPostHeaders(PostMethod post) {
83
        int length = 0;// Take length from file
84
        if (getHeaderManager() != null) {
85
            // headerManager was set, so let's set the connection
86
            // to use it.
87
            HeaderManager mngr = getHeaderManager();
88
            int headerSize = mngr.size();
89
            for (int idx = 0; idx < headerSize; idx++) {
90
                Header hd = mngr.getHeader(idx);
91
                if (HTTPConstants.HEADER_CONTENT_LENGTH.equalsIgnoreCase(hd
92
                        .getName())) {// Use this to override file length
93
                    length = Integer.parseInt(hd.getValue());
94
                }
95
96
            }
97
        }
98
    }
99
100
    /**
101
     * Send POST data from <code>Entry</code> to the open connection.
102
     * 
103
     * @param post
104
     * @throws IOException
105
     *             if an I/O exception occurs
106
     */
107
    private String sendPostData(PostMethod post) {
108
        // Buffer to hold the post body, except file content
109
        StringBuilder postedBody = new StringBuilder(1000);
110
        XStream xstream = new XStream(new DomDriver());
111
        Object o = xstream.fromXML(getXstreamData());
112
        ByteArrayOutputStream bao = new ByteArrayOutputStream();
113
        ObjectOutputStream oos = null;
114
        byte[] serialized_data = null;
115
        try {
116
            oos = new ObjectOutputStream(bao);
117
            oos.writeObject(o);
118
            serialized_data = bao.toByteArray();
119
            postedBody.append(serialized_data);
120
            post.setRequestEntity(new ByteArrayRequestEntity(serialized_data));
121
        } catch (IOException e) {
122
123
        }
124
        return postedBody.toString();
125
    }
126
127
    protected HTTPSampleResult sample(URL url, String method,
128
            boolean areFollowingRedirect, int frameDepth) {
129
        String urlStr = url.toString();
130
131
        log.debug("Start : sample " + urlStr);
132
133
        PostMethod httpMethod;
134
        httpMethod = new PostMethod(urlStr);
135
136
        HTTPSampleResult res = new HTTPSampleResult();
137
        res.setMonitor(false);
138
139
        res.setSampleLabel(urlStr); // May be replaced later
140
        res.setHTTPMethod(HTTPConstants.POST);
141
        res.setURL(url);
142
        res.sampleStart(); // Count the retries as well in the time
143
        HttpClient client = null;
144
        InputStream instream = null;
145
        try {
146
            setPostHeaders(httpMethod);
147
            client = setupConnection(url, httpMethod, res);
148
            setSavedClient(client);
149
            res.setQueryString(sendPostData(httpMethod));
150
            int statusCode = client.executeMethod(httpMethod);
151
            // Some headers are set by executeMethod()
152
            res.setRequestHeaders(getConnectionHeaders(httpMethod));
153
154
            // Request sent. Now get the response:
155
            instream = httpMethod.getResponseBodyAsStream();
156
157
            if (instream != null) {// will be null for HEAD
158
159
                org.apache.commons.httpclient.Header responseHeader = httpMethod
160
                        .getResponseHeader(HTTPConstants.HEADER_CONTENT_ENCODING);
161
                if (responseHeader != null
162
                        && HTTPConstants.ENCODING_GZIP.equals(responseHeader
163
                                .getValue())) {
164
                    instream = new GZIPInputStream(instream);
165
                }
166
167
                // int contentLength = httpMethod.getResponseContentLength();Not
168
                // visible ...
169
                // TODO size ouststream according to actual content length
170
                ByteArrayOutputStream outstream = new ByteArrayOutputStream(
171
                        4 * 1024);
172
                // contentLength > 0 ? contentLength :
173
                // DEFAULT_INITIAL_BUFFER_SIZE);
174
                byte[] buffer = new byte[4096];
175
                int len;
176
                boolean first = true;// first response
177
                while ((len = instream.read(buffer)) > 0) {
178
                    if (first) { // save the latency
179
                        res.latencyEnd();
180
                        first = false;
181
                    }
182
                    outstream.write(buffer, 0, len);
183
                }
184
185
                res.setResponseData(outstream.toByteArray());
186
                outstream.close();
187
188
            }
189
190
            res.sampleEnd();
191
            // Done with the sampling proper.
192
193
            // Now collect the results into the HTTPSampleResult:
194
195
            res.setSampleLabel(httpMethod.getURI().toString());
196
            // Pick up Actual path (after redirects)
197
198
            res.setResponseCode(Integer.toString(statusCode));
199
            res.setSuccessful(isSuccessCode(statusCode));
200
201
            res.setResponseMessage(httpMethod.getStatusText());
202
203
            // Set up the defaults (may be overridden below)
204
            res.setDataEncoding(ENCODING);
205
            String ct = null;
206
            org.apache.commons.httpclient.Header h = httpMethod
207
                    .getResponseHeader(HTTPConstants.HEADER_CONTENT_TYPE);
208
            if (h != null)// Can be missing, e.g. on redirect
209
            {
210
                ct = h.getValue();
211
                res.setContentType(ct);// e.g. text/html; charset=ISO-8859-1
212
                res.setEncodingAndType(ct);
213
            }
214
215
            res.setResponseHeaders(getResponseHeaders(httpMethod));
216
            if (res.isRedirect()) {
217
                res.setRedirectLocation(httpMethod.getResponseHeader(
218
                        HTTPConstants.HEADER_LOCATION).getValue());
219
            }
220
221
            // If we redirected automatically, the URL may have changed
222
            if (getAutoRedirects()) {
223
                res.setURL(new URL(httpMethod.getURI().toString()));
224
            }
225
226
            // Store any cookies received in the cookie manager:
227
            saveConnectionCookies(httpMethod, res.getURL(), getCookieManager());
228
229
            // Save cache information
230
            final CacheManager cacheManager = getCacheManager();
231
            if (cacheManager != null) {
232
                cacheManager.saveDetails(httpMethod, res);
233
            }
234
235
            // Follow redirects and download page resources if appropriate:
236
            res = resultProcessing(areFollowingRedirect, frameDepth, res);
237
238
            log.debug("End : sample");
239
            httpMethod.releaseConnection();
240
            return res;
241
        } catch (IllegalArgumentException e)// e.g. some kinds of invalid URL
242
        {
243
            res.sampleEnd();
244
            errorResult(e, res);
245
            return res;
246
        } catch (IOException e) {
247
            res.sampleEnd();
248
            errorResult(e, res);
249
            return res;
250
        } finally {
251
            JOrphanUtils.closeQuietly(instream);
252
            setSavedClient(null);
253
            httpMethod.releaseConnection();
254
        }
255
    }
256
257
    @Override
258
    public URL getUrl() throws MalformedURLException {
259
        return new URL(getUrlData());
260
    }
261
}
262
*

Return to bug 54205