Search in sources :

Example 1 with ByteArrayBody

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);
        }
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) UploadModelResult(org.eclipse.vorto.repository.api.upload.UploadModelResult) HttpEntity(org.apache.http.HttpEntity) ModelPublishException(org.eclipse.vorto.repository.api.upload.ModelPublishException) ByteArrayBody(org.apache.http.entity.mime.content.ByteArrayBody) UploadModelResponse(org.eclipse.vorto.repository.api.upload.UploadModelResponse) HttpPut(org.apache.http.client.methods.HttpPut) StringBody(org.apache.http.entity.mime.content.StringBody) TypeToken(com.google.gson.reflect.TypeToken) ModelId(org.eclipse.vorto.repository.api.ModelId)

Example 2 with ByteArrayBody

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;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) MultipartEntityBuilder(org.apache.http.entity.mime.MultipartEntityBuilder) StringBody(org.apache.http.entity.mime.content.StringBody) ByteArrayBody(org.apache.http.entity.mime.content.ByteArrayBody) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) XPathExpressionException(javax.xml.xpath.XPathExpressionException) CabinetException(cz.metacentrum.perun.cabinet.bl.CabinetException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 3 with ByteArrayBody

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());
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) MultipartEntityBuilder(org.apache.http.entity.mime.MultipartEntityBuilder) HttpEntity(org.apache.http.HttpEntity) StringBody(org.apache.http.entity.mime.content.StringBody) ByteArrayBody(org.apache.http.entity.mime.content.ByteArrayBody) HttpResponse(org.apache.http.HttpResponse) SecurityConstraint(io.undertow.servlet.api.SecurityConstraint) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 4 with ByteArrayBody

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;
}
Also used : ByteArrayBody(org.apache.http.entity.mime.content.ByteArrayBody)

Example 5 with ByteArrayBody

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);
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) MultipartEntityBuilder(org.apache.http.entity.mime.MultipartEntityBuilder) FileBody(org.apache.http.entity.mime.content.FileBody) HttpEntity(org.apache.http.HttpEntity) WildflyClientException(org.guvnor.ala.wildfly.access.exceptions.WildflyClientException) ByteArrayBody(org.apache.http.entity.mime.content.ByteArrayBody) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpResponse(org.apache.http.HttpResponse) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) AuthScope(org.apache.http.auth.AuthScope) ModelNode(org.jboss.dmr.ModelNode)

Aggregations

ByteArrayBody (org.apache.http.entity.mime.content.ByteArrayBody)13 HttpPost (org.apache.http.client.methods.HttpPost)11 HttpEntity (org.apache.http.HttpEntity)6 MultipartEntityBuilder (org.apache.http.entity.mime.MultipartEntityBuilder)6 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)5 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)5 StringBody (org.apache.http.entity.mime.content.StringBody)4 Test (org.junit.Test)4 HttpResponse (org.apache.http.HttpResponse)3 MultipartEntity (org.apache.http.entity.mime.MultipartEntity)3 TypeToken (com.google.gson.reflect.TypeToken)2 File (java.io.File)2 IOException (java.io.IOException)2 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)2 FileBody (org.apache.http.entity.mime.content.FileBody)2 ModelPublishException (org.eclipse.vorto.repository.api.upload.ModelPublishException)2 CabinetException (cz.metacentrum.perun.cabinet.bl.CabinetException)1 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)1 SecurityConstraint (io.undertow.servlet.api.SecurityConstraint)1 TestHttpClient (io.undertow.testutils.TestHttpClient)1