Search in sources :

Example 51 with MultipartEntity

use of org.apache.http.entity.mime.MultipartEntity in project OpenMEAP by OpenMEAP.

the class FileHandlingHttpRequestExecuterImpl method postData.

@Override
public HttpResponse postData(String url, Hashtable getParams, Hashtable postParams) throws HttpRequestException {
    // test to determine whether this is a file upload or not.
    Boolean isFileUpload = false;
    for (Object o : postParams.values()) {
        if (o instanceof File) {
            isFileUpload = true;
            break;
        }
    }
    if (isFileUpload) {
        try {
            HttpPost httpPost = new HttpPost(createUrl(url, getParams));
            httpPost.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
            MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
            for (Object o : postParams.entrySet()) {
                Map.Entry<String, Object> entry = (Map.Entry<String, Object>) o;
                if (entry.getValue() instanceof File) {
                    // For File parameters
                    File file = (File) entry.getValue();
                    FileNameMap fileNameMap = URLConnection.getFileNameMap();
                    String type = fileNameMap.getContentTypeFor(file.toURL().toString());
                    entity.addPart(entry.getKey(), new FileBody(((File) entry.getValue()), type));
                } else {
                    // For usual String parameters
                    entity.addPart(entry.getKey(), new StringBody(entry.getValue().toString(), "text/plain", Charset.forName(FormConstants.CHAR_ENC_DEFAULT)));
                }
            }
            httpPost.setEntity(entity);
            return execute(httpPost);
        } catch (Exception e) {
            throw new HttpRequestException(e);
        }
    } else {
        return super.postData(url, getParams, postParams);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) FileBody(org.apache.http.entity.mime.content.FileBody) FileNameMap(java.net.FileNameMap) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) StringBody(org.apache.http.entity.mime.content.StringBody) File(java.io.File) FileNameMap(java.net.FileNameMap) Map(java.util.Map)

Example 52 with MultipartEntity

use of org.apache.http.entity.mime.MultipartEntity in project ninja by ninjaframework.

the class NinjaTestBrowser method uploadFileWithForm.

public String uploadFileWithForm(String url, String paramName, File fileToUpload, Map<String, String> formParameters) {
    String response = null;
    try {
        httpClient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
        HttpPost post = new HttpPost(url);
        MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        // For File parameters
        entity.addPart(paramName, new FileBody((File) fileToUpload));
        // add form parameters:
        if (formParameters != null) {
            for (Entry<String, String> parameter : formParameters.entrySet()) {
                entity.addPart(parameter.getKey(), new StringBody(parameter.getValue()));
            }
        }
        post.setEntity(entity);
        // Here we go!
        response = EntityUtils.toString(httpClient.execute(post).getEntity(), "UTF-8");
        post.releaseConnection();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return response;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) FileBody(org.apache.http.entity.mime.content.FileBody) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) StringBody(org.apache.http.entity.mime.content.StringBody) File(java.io.File) IOException(java.io.IOException)

Example 53 with MultipartEntity

use of org.apache.http.entity.mime.MultipartEntity in project ninja by ninjaframework.

the class NinjaTestBrowser method uploadFiles.

public String uploadFiles(String url, String[] paramNames, File[] fileToUploads) {
    String response = null;
    try {
        httpClient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
        HttpPost post = new HttpPost(url);
        MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        // For File parameters
        for (int i = 0; i < paramNames.length; i++) {
            entity.addPart(paramNames[i], new FileBody(fileToUploads[i]));
        }
        post.setEntity(entity);
        // Here we go!
        response = EntityUtils.toString(httpClient.execute(post).getEntity(), "UTF-8");
        post.releaseConnection();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return response;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) FileBody(org.apache.http.entity.mime.content.FileBody) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) IOException(java.io.IOException)

Example 54 with MultipartEntity

use of org.apache.http.entity.mime.MultipartEntity in project undertow by undertow-io.

the class MultiPartTestCase method testMultiPartRequest.

@Test
public void testMultiPartRequest() throws IOException {
    TestHttpClient client = new TestHttpClient();
    try {
        String uri = DefaultServer.getDefaultServerURL() + "/servletContext/1";
        HttpPost post = new HttpPost(uri);
        MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, null, StandardCharsets.UTF_8);
        entity.addPart("formValue", new StringBody("myValue", "text/plain", StandardCharsets.UTF_8));
        entity.addPart("file", new FileBody(new File(MultiPartTestCase.class.getResource("uploadfile.txt").getFile())));
        post.setEntity(entity);
        HttpResponse result = client.execute(post);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        final String response = HttpClientUtils.readResponse(result);
        Assert.assertEquals("PARAMS:\r\n" + "parameter count: 1\r\n" + "parameter name count: 1\r\n" + "name: formValue\r\n" + "filename: null\r\n" + "content-type: null\r\n" + "Content-Disposition: form-data; name=\"formValue\"\r\n" + "size: 7\r\n" + "content: myValue\r\n" + "name: file\r\n" + "filename: uploadfile.txt\r\n" + "content-type: application/octet-stream\r\n" + "Content-Disposition: form-data; name=\"file\"; filename=\"uploadfile.txt\"\r\n" + "Content-Type: application/octet-stream\r\n" + "size: 13\r\n" + "content: file contents\r\n", response);
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) FileBody(org.apache.http.entity.mime.content.FileBody) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) StringBody(org.apache.http.entity.mime.content.StringBody) HttpResponse(org.apache.http.HttpResponse) File(java.io.File) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 55 with MultipartEntity

use of org.apache.http.entity.mime.MultipartEntity in project undertow by undertow-io.

the class MultiPartTestCase method testMultiPartRequestWithNoMultipartConfig.

@Test
public void testMultiPartRequestWithNoMultipartConfig() throws IOException {
    TestHttpClient client = new TestHttpClient();
    try {
        String uri = DefaultServer.getDefaultServerURL() + "/servletContext/0";
        HttpPost post = new HttpPost(uri);
        MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        entity.addPart("formValue", new StringBody("myValue", "text/plain", StandardCharsets.UTF_8));
        entity.addPart("file", new FileBody(new File(MultiPartTestCase.class.getResource("uploadfile.txt").getFile())));
        post.setEntity(entity);
        HttpResponse result = client.execute(post);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        final String response = HttpClientUtils.readResponse(result);
        Assert.assertEquals("EXCEPTION: class java.lang.IllegalStateException", response);
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) FileBody(org.apache.http.entity.mime.content.FileBody) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) StringBody(org.apache.http.entity.mime.content.StringBody) HttpResponse(org.apache.http.HttpResponse) File(java.io.File) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Aggregations

MultipartEntity (org.apache.http.entity.mime.MultipartEntity)62 HttpPost (org.apache.http.client.methods.HttpPost)41 StringBody (org.apache.http.entity.mime.content.StringBody)40 FileBody (org.apache.http.entity.mime.content.FileBody)37 File (java.io.File)31 HttpResponse (org.apache.http.HttpResponse)28 Test (org.junit.Test)24 TestHttpClient (io.undertow.testutils.TestHttpClient)13 IOException (java.io.IOException)13 InputStreamBody (org.apache.http.entity.mime.content.InputStreamBody)7 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)7 JSONObject (org.json.JSONObject)7 HashMap (java.util.HashMap)5 Map (java.util.Map)5 HttpEntity (org.apache.http.HttpEntity)5 ByteArrayBody (org.apache.http.entity.mime.content.ByteArrayBody)5 RequestBuilder (org.apache.sling.testing.tools.http.RequestBuilder)5 JSONException (org.json.JSONException)5 BlockingHandler (io.undertow.server.handlers.BlockingHandler)4 InputStream (java.io.InputStream)4