Search in sources :

Example 6 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)

Example 7 with ByteArrayBody

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

Example 8 with ByteArrayBody

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();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) ByteArrayBody(org.apache.http.entity.mime.content.ByteArrayBody) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient)

Example 9 with ByteArrayBody

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();
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) HttpEntity(org.apache.http.HttpEntity) ByteArrayBody(org.apache.http.entity.mime.content.ByteArrayBody) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Test(org.junit.Test)

Example 10 with ByteArrayBody

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();
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) MultipartEntityBuilder(org.apache.http.entity.mime.MultipartEntityBuilder) HttpEntity(org.apache.http.HttpEntity) ByteArrayBody(org.apache.http.entity.mime.content.ByteArrayBody) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Test(org.junit.Test)

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