use of org.apache.http.entity.mime.content.ByteArrayBody 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.ByteArrayBody in project vorto by eclipse.
the class DefaultModelPublisher method uploadModelImage.
@Override
public void uploadModelImage(ModelId modelId, String imageBas64) throws ModelPublishException {
String uploadImageUrl = String.format("%s/rest/model/image?namespace=%s&name=%s&version=%s", getRequestContext().getBaseUrl(), modelId.getNamespace(), modelId.getName(), modelId.getVersion());
HttpPost uploadImage = new HttpPost(uploadImageUrl);
HttpEntity entity = MultipartEntityBuilder.create().addPart("fileName", new StringBody("vortomodel.png", ContentType.DEFAULT_TEXT)).addPart("fileDescription", new StringBody("", ContentType.DEFAULT_TEXT)).addPart("file", new ByteArrayBody(Base64.getDecoder().decode(imageBas64.getBytes()), ContentType.APPLICATION_OCTET_STREAM, "vortomodel.png")).build();
uploadImage.setEntity(entity);
try {
execute(uploadImage, new TypeToken<Void>() {
}.getType());
} catch (Throwable ex) {
if (!(ex instanceof ModelPublishException)) {
throw new RuntimeException(ex);
} else {
throw ((ModelPublishException) ex);
}
}
}
use of org.apache.http.entity.mime.content.ByteArrayBody in project cxf by apache.
the class Client method uploadToCatalog.
private static void uploadToCatalog(final String url, final CloseableHttpClient httpClient, final String filename) throws IOException {
System.out.println("Sent HTTP POST request to upload the file into catalog: " + filename);
final HttpPost post = new HttpPost(url);
MultipartEntity entity = new MultipartEntity();
byte[] bytes = IOUtils.readBytesFromStream(Client.class.getResourceAsStream("/" + filename));
entity.addPart(filename, new ByteArrayBody(bytes, filename));
post.setEntity(entity);
try {
CloseableHttpResponse response = httpClient.execute(post);
if (response.getStatusLine().getStatusCode() == 201) {
System.out.println(response.getFirstHeader("Location"));
} else if (response.getStatusLine().getStatusCode() == 409) {
System.out.println("Document already exists: " + filename);
}
} finally {
post.releaseConnection();
}
}
use of org.apache.http.entity.mime.content.ByteArrayBody in project cxf by apache.
the class JAXRSMultipartTest method testMultipartRequestTooLarge.
@Test
public void testMultipartRequestTooLarge() throws Exception {
CloseableHttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost("http://localhost:" + PORT + "/bookstore/books/image");
String ct = "multipart/mixed";
post.setHeader("Content-Type", ct);
HttpEntity entity = MultipartEntityBuilder.create().addPart("image", new ByteArrayBody(new byte[1024 * 11], "testfile.png")).build();
post.setEntity(entity);
try {
CloseableHttpResponse response = client.execute(post);
assertEquals(413, response.getStatusLine().getStatusCode());
} finally {
// Release current connection to the connection pool once you are done
post.releaseConnection();
}
}
use of org.apache.http.entity.mime.content.ByteArrayBody in project cxf by apache.
the class JAXRSMultipartTest method testMultipartRequestTooLargeManyParts.
@Test
public void testMultipartRequestTooLargeManyParts() throws Exception {
CloseableHttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost("http://localhost:" + PORT + "/bookstore/books/image");
String ct = "multipart/mixed";
post.setHeader("Content-Type", ct);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
HttpEntity entity = builder.addPart("image", new ByteArrayBody(new byte[1024 * 9], "testfile.png")).addPart("image", new ByteArrayBody(new byte[1024 * 11], "testfile2.png")).build();
post.setEntity(entity);
try {
CloseableHttpResponse response = client.execute(post);
assertEquals(413, response.getStatusLine().getStatusCode());
} finally {
// Release current connection to the connection pool once you are done
post.releaseConnection();
}
}
Aggregations