use of org.apache.http.entity.mime.content.FileBody in project kie-wb-common by kiegroup.
the class WildflyClient method deploy.
/*
* Deploys a new WAR file to the Wildfly Instance
* @param File to be deployed, it must be a deployable file
* @return the 200 on successful deployment
* @throw a WildflyClientException with the status code on failure
* @throw a WildflyClientException with the throwable in case of an internal exception
*/
public int deploy(File file) throws WildflyClientException {
// the digest auth backend
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(host, managementPort), new UsernamePasswordCredentials(user, password));
CloseableHttpClient httpclient = custom().setDefaultCredentialsProvider(credsProvider).build();
HttpPost post = new HttpPost("http://" + host + ":" + managementPort + "/management-upload");
post.addHeader("X-Management-Client-Name", "HAL");
// the file to be uploaded
FileBody fileBody = new FileBody(file);
// the DMR operation
ModelNode operation = new ModelNode();
operation.get("address").add("deployment", file.getName());
operation.get("operation").set("add");
operation.get("runtime-name").set(file.getName());
operation.get("enabled").set(true);
// point to the multipart index used
operation.get("content").add().get("input-stream-index").set(0);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
try {
operation.writeBase64(bout);
} catch (IOException ex) {
getLogger(WildflyClient.class.getName()).log(SEVERE, null, ex);
}
// the multipart
MultipartEntityBuilder builder = create();
builder.setMode(BROWSER_COMPATIBLE);
builder.addPart("uploadFormElement", fileBody);
builder.addPart("operation", new ByteArrayBody(bout.toByteArray(), create("application/dmr-encoded"), "blob"));
HttpEntity entity = builder.build();
post.setEntity(entity);
try {
HttpResponse response = httpclient.execute(post);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != 200) {
throw new WildflyClientException("Error Deploying App Status Code: " + statusCode);
}
return statusCode;
} catch (IOException ex) {
LOG.error("Error Deploying App : " + ex.getMessage(), ex);
throw new WildflyClientException("Error Deploying App : " + ex.getMessage(), ex);
}
}
use of org.apache.http.entity.mime.content.FileBody in project scheduling by ow2-proactive.
the class RestSchedulerJobTaskTest method testSubmit.
@Test
public void testSubmit() throws Exception {
String schedulerUrl = getResourceUrl("submit");
HttpPost httpPost = new HttpPost(schedulerUrl);
setSessionHeader(httpPost);
File jobFile = RestFuncTHelper.getDefaultJobXmlfile();
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create().addPart("file", new FileBody(jobFile, ContentType.APPLICATION_XML));
httpPost.setEntity(multipartEntityBuilder.build());
HttpResponse response = executeUriRequest(httpPost);
assertHttpStatusOK(response);
JSONObject jsonObj = toJsonObject(response);
assertNotNull(jsonObj.get("id").toString());
}
use of org.apache.http.entity.mime.content.FileBody in project scheduling by ow2-proactive.
the class RestSchedulerJobTaskTest method testUrlMatrixParamsShouldReplaceJobVariables.
@Test
public void testUrlMatrixParamsShouldReplaceJobVariables() throws Exception {
File jobFile = new File(RestSchedulerJobTaskTest.class.getResource("config/job_matrix_params.xml").toURI());
String schedulerUrl = getResourceUrl("submit;var=matrix_param_val");
HttpPost httpPost = new HttpPost(schedulerUrl);
setSessionHeader(httpPost);
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create().addPart("file", new FileBody(jobFile, ContentType.APPLICATION_XML));
httpPost.setEntity(multipartEntityBuilder.build());
HttpResponse response = executeUriRequest(httpPost);
assertHttpStatusOK(response);
JSONObject jsonObj = toJsonObject(response);
final String jobId = jsonObj.get("id").toString();
assertNotNull(jobId);
waitJobState(jobId, JobStatus.FINISHED, TimeUnit.MINUTES.toMillis(1));
}
use of org.apache.http.entity.mime.content.FileBody in project RESTdoclet by IG-Group.
the class FileUploader method upload.
/**
* Method to upload (post) a jar to the RESTDoclet web app at the given url,
* and store it on the server in the given location.
*
* @param url the RESTDoclet web app url to which to post
* @param deployDir the RESTDoclet web app directory where the jar will be
* extracted
* @param file the jar to be uploaded
*/
public static void upload(String url, String deployDir, File file) {
LOG.debug("Uploading " + file.getName() + " to " + url);
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
FileBody bin = new FileBody(file);
reqEntity.addPart("attachment_field", bin);
httppost.setEntity(reqEntity);
httppost.setHeader(RESTDOCLET_DEPLOY, deployDir);
HttpResponse response = httpclient.execute(httppost);
if (response.getStatusLine().getStatusCode() != 200) {
LOG.error("Failed to upload " + file.getName() + " to " + url + " : " + response.getStatusLine());
}
} catch (Exception e) {
LOG.error("Failed to upload " + file.getName() + " to " + url, e);
}
}
use of org.apache.http.entity.mime.content.FileBody in project streamsx.topology by IBMStreams.
the class RestUtils method postJob.
/**
* Submit an application bundle to execute as a job.
*/
public static JsonObject postJob(CloseableHttpClient httpClient, JsonObject service, File bundle, JsonObject jobConfigOverlay) throws ClientProtocolException, IOException {
final String serviceName = jstring(service, "name");
final JsonObject credentials = service.getAsJsonObject("credentials");
String url = getJobSubmitURL(credentials, bundle);
HttpPost postJobWithConfig = new HttpPost(url);
postJobWithConfig.addHeader("accept", ContentType.APPLICATION_JSON.getMimeType());
postJobWithConfig.addHeader(AUTH.WWW_AUTH_RESP, getAPIKey(credentials));
FileBody bundleBody = new FileBody(bundle, ContentType.APPLICATION_OCTET_STREAM);
StringBody configBody = new StringBody(jobConfigOverlay.toString(), ContentType.APPLICATION_JSON);
HttpEntity reqEntity = MultipartEntityBuilder.create().addPart("sab", bundleBody).addPart(DeployKeys.JOB_CONFIG_OVERLAYS, configBody).build();
postJobWithConfig.setEntity(reqEntity);
JsonObject jsonResponse = getGsonResponse(httpClient, postJobWithConfig);
RemoteContext.REMOTE_LOGGER.info("Streaming Analytics service (" + serviceName + "): submit job response:" + jsonResponse.toString());
return jsonResponse;
}
Aggregations