Search in sources :

Example 56 with FileBody

use of org.apache.http.entity.mime.content.FileBody in project streamsx.topology by IBMStreams.

the class StreamingAnalyticsServiceV2 method submitBuild.

@Override
protected JsonObject submitBuild(CloseableHttpClient httpclient, File archive, String buildName, String originator) throws IOException {
    String newBuildURL = getBuildsUrl(httpclient);
    newBuildURL = newBuildURL + "?originator=" + URLEncoder.encode(originator, StandardCharsets.UTF_8.name());
    HttpPost httppost = new HttpPost(newBuildURL);
    httppost.addHeader("Authorization", getAuthorization());
    JsonObject buildParams = new JsonObject();
    buildParams.addProperty("buildName", buildName);
    StringBody paramsBody = new StringBody(buildParams.toString(), ContentType.APPLICATION_JSON);
    FileBody archiveBody = new FileBody(archive, ContentType.create("application/zip"), archive.getName());
    HttpEntity reqEntity = MultipartEntityBuilder.create().addPart("build_options", paramsBody).addPart("archive_file", archiveBody).build();
    httppost.setEntity(reqEntity);
    JsonObject build = StreamsRestUtils.getGsonResponse(httpclient, httppost);
    return build;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) FileBody(org.apache.http.entity.mime.content.FileBody) HttpEntity(org.apache.http.HttpEntity) StringBody(org.apache.http.entity.mime.content.StringBody) JsonObject(com.google.gson.JsonObject)

Example 57 with FileBody

use of org.apache.http.entity.mime.content.FileBody 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 58 with FileBody

use of org.apache.http.entity.mime.content.FileBody 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 59 with FileBody

use of org.apache.http.entity.mime.content.FileBody 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 60 with FileBody

use of org.apache.http.entity.mime.content.FileBody in project acceptance-test-harness by jenkinsci.

the class FormElementPath method uploadPlugin.

private void uploadPlugin(URL url, File file) throws IOException {
    try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
        HttpPost post = new HttpPost(new URL(url, "pluginManager/uploadPlugin").toExternalForm());
        FileBody fileBody = new FileBody(file, ContentType.DEFAULT_BINARY);
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        builder.addPart("upfile", fileBody);
        post.setEntity(builder.build());
        HttpResponse response = httpClient.execute(post, HttpUtils.buildHttpClientContext(url, credentials));
        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != 302) {
            // Redirect to updateCenter
            throw new IOException("Could not upload plugin, received " + statusCode + ": " + EntityUtils.toString(response.getEntity()));
        }
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) MultipartEntityBuilder(org.apache.http.entity.mime.MultipartEntityBuilder) FileBody(org.apache.http.entity.mime.content.FileBody) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) URL(java.net.URL)

Aggregations

FileBody (org.apache.http.entity.mime.content.FileBody)60 HttpPost (org.apache.http.client.methods.HttpPost)45 File (java.io.File)40 MultipartEntity (org.apache.http.entity.mime.MultipartEntity)37 HttpResponse (org.apache.http.HttpResponse)30 StringBody (org.apache.http.entity.mime.content.StringBody)30 HttpEntity (org.apache.http.HttpEntity)17 Test (org.junit.Test)17 IOException (java.io.IOException)16 MultipartEntityBuilder (org.apache.http.entity.mime.MultipartEntityBuilder)15 TestHttpClient (io.undertow.testutils.TestHttpClient)11 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)7 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)6 JsonObject (com.google.gson.JsonObject)5 HttpClient (org.apache.http.client.HttpClient)5 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)5 JSONObject (org.json.JSONObject)5 BlockingHandler (io.undertow.server.handlers.BlockingHandler)4 BufferedReader (java.io.BufferedReader)4 FileInputStream (java.io.FileInputStream)3