use of org.apache.http.entity.mime.content.ByteArrayBody in project vorto by eclipse.
the class DefaultModelPublisher method publish.
@Override
public ModelId publish(ModelType type, String content) throws ModelPublishException {
String uploadModelsUrl = String.format("%s/rest/secure", getRequestContext().getBaseUrl());
HttpPost query = new HttpPost(uploadModelsUrl);
HttpEntity entity = MultipartEntityBuilder.create().addPart("fileName", new StringBody("vortomodel" + type.getExtension(), ContentType.DEFAULT_TEXT)).addPart("fileDescription", new StringBody("", ContentType.DEFAULT_TEXT)).addPart("file", new ByteArrayBody(content.getBytes(), ContentType.APPLICATION_OCTET_STREAM, "vortomodel" + type.getExtension())).build();
query.setEntity(entity);
try {
CompletableFuture<UploadModelResponse> response = execute(query, new TypeToken<UploadModelResponse>() {
}.getType());
List<UploadModelResult> result = response.get().getObj();
if (response.get().getIsSuccess()) {
String checkinModelUrl = String.format("%s/rest/secure/%s", getRequestContext().getBaseUrl(), result.get(0).getHandleId());
HttpPut checkInModel = new HttpPut(checkinModelUrl);
CompletableFuture<ModelId> checkedInResult = execute(checkInModel, new TypeToken<ModelId>() {
}.getType());
return (ModelId) checkedInResult.get();
} else {
throw new ModelPublishException(result.get(0));
}
} 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 perun by CESNET.
the class MUStrategy method getHttpRequest.
@Override
public HttpUriRequest getHttpRequest(String uco, int yearSince, int yearTill, PublicationSystem ps) {
// prepare request body
MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
try {
entityBuilder.addPart("typ", new StringBody("xml", ContentType.create("text/plain", Consts.UTF_8)));
entityBuilder.addPart("kodovani", new StringBody(StandardCharsets.UTF_8.toString(), ContentType.create("text/plain", Consts.UTF_8)));
entityBuilder.addPart("keyfile", new ByteArrayBody(buildRequestKeyfile(Integer.parseInt(uco), yearSince, yearTill).getBytes(), "template.xml"));
} catch (Exception e) {
throw new RuntimeException(e);
}
// prepare post request
HttpPost post = new HttpPost(ps.getUrl());
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(ps.getUsername(), ps.getPassword());
// cred, enc, proxy
post.addHeader(BasicScheme.authenticate(credentials, StandardCharsets.UTF_8.toString(), false));
post.setEntity(entityBuilder.build());
return post;
}
use of org.apache.http.entity.mime.content.ByteArrayBody in project undertow by undertow-io.
the class ServletCertAndDigestAuthTestCase method testMultipartRequest.
@Test
public void testMultipartRequest() throws Exception {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 2000; i++) {
sb.append("0123456789");
}
try (TestHttpClient client = new TestHttpClient()) {
// create POST request
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addPart("part1", new ByteArrayBody(sb.toString().getBytes(), "file.txt"));
builder.addPart("part2", new StringBody("0123456789", ContentType.TEXT_HTML));
HttpEntity entity = builder.build();
client.setSSLContext(clientSSLContext);
String url = DefaultServer.getDefaultServerSSLAddress() + BASE_PATH + "multipart";
HttpPost post = new HttpPost(url);
post.setEntity(entity);
post.addHeader(AUTHORIZATION.toString(), BASIC + " " + FlexBase64.encodeString(("user1" + ":" + "password1").getBytes(StandardCharsets.UTF_8), false));
HttpResponse result = client.execute(post);
assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
}
}
use of org.apache.http.entity.mime.content.ByteArrayBody in project zm-mailbox by Zimbra.
the class ZMailbox method createAttachmentPart.
/**
* Creates an <tt>HttpClient FilePart</tt> from the given filename and content.
*/
public ByteArrayBody createAttachmentPart(String filename, byte[] content) {
String contentType = URLConnection.getFileNameMap().getContentTypeFor(filename);
ByteArrayBody part = new ByteArrayBody(content, ContentType.create(contentType), filename);
return part;
}
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);
}
}
Aggregations