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;
}
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);
}
}
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;
}
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;
}
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()));
}
}
}
Aggregations