Search in sources :

Example 21 with HTTPFileArg

use of org.apache.jmeter.protocol.http.util.HTTPFileArg in project jmeter by apache.

the class PostWriter method sendPostData.

/**
     * Send POST data from Entry to the open connection.
     *
     * @param connection
     *            the open connection to use for sending data
     * @param sampler
     *            sampler to get information about what to send
     * @return the post body sent. Actual file content is not returned, it is
     *         just shown as a placeholder text "actual file content"
     * @throws IOException when writing data fails
     */
public String sendPostData(URLConnection connection, HTTPSamplerBase sampler) throws IOException {
    // Buffer to hold the post body, except file content
    StringBuilder postedBody = new StringBuilder(1000);
    HTTPFileArg[] files = sampler.getHTTPFiles();
    String contentEncoding = sampler.getContentEncoding();
    if (contentEncoding == null || contentEncoding.length() == 0) {
        contentEncoding = ENCODING;
    }
    // application/x-www-form-urlencoded post request
    if (sampler.getUseMultipartForPost()) {
        OutputStream out = connection.getOutputStream();
        // Write the form data post body, which we have constructed
        // in the setHeaders. This contains the multipart start divider
        // and any form data, i.e. arguments
        out.write(formDataPostBody);
        // Retrieve the formatted data using the same encoding used to create it
        postedBody.append(new String(formDataPostBody, contentEncoding));
        // Add any files
        for (int i = 0; i < files.length; i++) {
            HTTPFileArg file = files[i];
            // First write the start multipart file
            // TODO - charset?
            byte[] header = file.getHeader().getBytes();
            out.write(header);
            // Retrieve the formatted data using the same encoding used to create it
            // TODO - charset?
            postedBody.append(new String(header));
            // Write the actual file content
            writeFileToStream(file.getPath(), out);
            // We just add placeholder text for file content
            // $NON-NLS-1$
            postedBody.append("<actual file content, not shown here>");
            // Write the end of multipart file
            byte[] fileMultipartEndDivider = getFileMultipartEndDivider();
            out.write(fileMultipartEndDivider);
            // Retrieve the formatted data using the same encoding used to create it
            postedBody.append(new String(fileMultipartEndDivider, ENCODING));
            if (i + 1 < files.length) {
                out.write(CRLF);
                postedBody.append(new String(CRLF, SampleResult.DEFAULT_HTTP_ENCODING));
            }
        }
        // Write end of multipart
        byte[] multipartEndDivider = getMultipartEndDivider();
        out.write(multipartEndDivider);
        postedBody.append(new String(multipartEndDivider, ENCODING));
        out.flush();
        out.close();
    } else {
        // If there are no arguments, we can send a file as the body of the request
        if (sampler.getArguments() != null && !sampler.hasArguments() && sampler.getSendFileAsPostBody()) {
            OutputStream out = connection.getOutputStream();
            // we're sure that there is at least one file because of
            // getSendFileAsPostBody method's return value.
            HTTPFileArg file = files[0];
            writeFileToStream(file.getPath(), out);
            out.flush();
            out.close();
            // We just add placeholder text for file content
            // $NON-NLS-1$
            postedBody.append("<actual file content, not shown here>");
        } else if (formDataUrlEncoded != null) {
            // may be null for PUT
            // In an application/x-www-form-urlencoded request, we only support
            // parameters, no file upload is allowed
            OutputStream out = connection.getOutputStream();
            out.write(formDataUrlEncoded);
            out.flush();
            out.close();
            postedBody.append(new String(formDataUrlEncoded, contentEncoding));
        }
    }
    return postedBody.toString();
}
Also used : OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HTTPFileArg(org.apache.jmeter.protocol.http.util.HTTPFileArg)

Example 22 with HTTPFileArg

use of org.apache.jmeter.protocol.http.util.HTTPFileArg in project jmeter by apache.

the class AjpSampler method getHeaderSize.

private int getHeaderSize(String method, URL url) {
    HeaderManager headers = getHeaderManager();
    CookieManager cookies = getCookieManager();
    AuthManager auth = getAuthManager();
    // Host always
    int hsz = 1;
    if (method.equals(HTTPConstants.POST)) {
        HTTPFileArg[] hfa = getHTTPFiles();
        if (hfa.length > 0) {
            hsz += 3;
        } else {
            hsz += 2;
        }
    }
    if (headers != null) {
        hsz += headers.size();
    }
    if (cookies != null) {
        hsz += cookies.getCookieCount();
    }
    if (auth != null) {
        String authHeader = auth.getAuthHeaderForURL(url);
        if (authHeader != null) {
            ++hsz;
        }
    }
    return hsz;
}
Also used : AuthManager(org.apache.jmeter.protocol.http.control.AuthManager) HTTPFileArg(org.apache.jmeter.protocol.http.util.HTTPFileArg) CookieManager(org.apache.jmeter.protocol.http.control.CookieManager) HeaderManager(org.apache.jmeter.protocol.http.control.HeaderManager)

Example 23 with HTTPFileArg

use of org.apache.jmeter.protocol.http.util.HTTPFileArg in project jmeter by apache.

the class HTTPFileArgsPanel method addFile.

/**
     * Add a new file row to the table.
     */
private void addFile(String path) {
    // If a table cell is being edited, we should accept the current value
    // and stop the editing before adding a new row.
    GuiUtils.stopTableEditing(table);
    tableModel.addRow(new HTTPFileArg(path));
    checkDeleteAndBrowseStatus();
    // Highlight (select) the appropriate row.
    int rowToSelect = tableModel.getRowCount() - 1;
    table.setRowSelectionInterval(rowToSelect, rowToSelect);
}
Also used : HTTPFileArg(org.apache.jmeter.protocol.http.util.HTTPFileArg)

Example 24 with HTTPFileArg

use of org.apache.jmeter.protocol.http.util.HTTPFileArg in project jmeter by apache.

the class HTTPFileArgsPanel method configure.

/**
     * A newly created component can be initialized with the contents of a
     * HTTPSamplerBase object by calling this method. The component is responsible for
     * querying the Test Element object for the relevant information to display
     * in its GUI.
     *
     * @param testElement the HTTPSamplerBase to be used to configure the GUI
     */
public void configure(TestElement testElement) {
    if (testElement instanceof HTTPSamplerBase) {
        HTTPSamplerBase base = (HTTPSamplerBase) testElement;
        tableModel.clearData();
        for (HTTPFileArg file : base.getHTTPFiles()) {
            tableModel.addRow(file);
        }
        checkDeleteAndBrowseStatus();
    }
}
Also used : HTTPFileArg(org.apache.jmeter.protocol.http.util.HTTPFileArg) HTTPSamplerBase(org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase)

Aggregations

HTTPFileArg (org.apache.jmeter.protocol.http.util.HTTPFileArg)24 Test (org.junit.Test)9 File (java.io.File)6 JMeterProperty (org.apache.jmeter.testelement.property.JMeterProperty)6 Arguments (org.apache.jmeter.config.Arguments)5 HTTPArgument (org.apache.jmeter.protocol.http.util.HTTPArgument)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 HTTPFileArgs (org.apache.jmeter.protocol.http.util.HTTPFileArgs)4 HTTPSamplerBase (org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase)3 URLConnection (java.net.URLConnection)2 HttpEntity (org.apache.http.HttpEntity)2 FileEntity (org.apache.http.entity.FileEntity)2 StringEntity (org.apache.http.entity.StringEntity)2 Argument (org.apache.jmeter.config.Argument)2 AuthManager (org.apache.jmeter.protocol.http.control.AuthManager)2 HeaderManager (org.apache.jmeter.protocol.http.control.HeaderManager)2 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1