Example usage for org.apache.http.entity ContentType MULTIPART_FORM_DATA

List of usage examples for org.apache.http.entity ContentType MULTIPART_FORM_DATA

Introduction

In this page you can find the example usage for org.apache.http.entity ContentType MULTIPART_FORM_DATA.

Prototype

ContentType MULTIPART_FORM_DATA

To view the source code for org.apache.http.entity ContentType MULTIPART_FORM_DATA.

Click Source Link

Usage

From source file:com.adobe.aem.demomachine.communities.Loader.java

private static ContentType getContentType(String fileName) {
    ContentType ct = ContentType.MULTIPART_FORM_DATA;
    if (fileName.indexOf(".mp4") > 0) {
        ct = ContentType.create("video/mp4", MIME.UTF8_CHARSET);
    } else if (fileName.indexOf(".jpg") > 0 || fileName.indexOf(".jpeg") > 0) {
        ct = ContentType.create("image/jpeg", MIME.UTF8_CHARSET);
    } else if (fileName.indexOf(".png") > 0) {
        ct = ContentType.create("image/png", MIME.UTF8_CHARSET);
    } else if (fileName.indexOf(".pdf") > 0) {
        ct = ContentType.create("application/pdf", MIME.UTF8_CHARSET);
    } else if (fileName.indexOf(".css") > 0) {
        ct = ContentType.create("text/css", MIME.UTF8_CHARSET);
    } else if (fileName.indexOf(".zip") > 0) {
        ct = ContentType.create("application/zip", MIME.UTF8_CHARSET);
    }/*from  w ww  .  j  a  v a  2  s. c  o  m*/
    return ct;
}

From source file:org.rapidoid.http.HttpClient.java

public Future<byte[]> request(String verb, String uri, Map<String, String> headers, Map<String, String> data,
        Map<String, String> files, byte[] body, String contentType, Callback<byte[]> callback,
        boolean fullResponse) {

    headers = U.safe(headers);//from   w  w  w  .j  a  v  a 2 s .  com
    data = U.safe(data);
    files = U.safe(files);

    HttpRequestBase req;
    boolean canHaveBody = false;

    if ("GET".equalsIgnoreCase(verb)) {
        req = new HttpGet(uri);
    } else if ("DELETE".equalsIgnoreCase(verb)) {
        req = new HttpDelete(uri);
    } else if ("OPTIONS".equalsIgnoreCase(verb)) {
        req = new HttpOptions(uri);
    } else if ("HEAD".equalsIgnoreCase(verb)) {
        req = new HttpHead(uri);
    } else if ("TRACE".equalsIgnoreCase(verb)) {
        req = new HttpTrace(uri);
    } else if ("POST".equalsIgnoreCase(verb)) {
        req = new HttpPost(uri);
        canHaveBody = true;
    } else if ("PUT".equalsIgnoreCase(verb)) {
        req = new HttpPut(uri);
        canHaveBody = true;
    } else if ("PATCH".equalsIgnoreCase(verb)) {
        req = new HttpPatch(uri);
        canHaveBody = true;
    } else {
        throw U.illegalArg("Illegal HTTP verb: " + verb);
    }

    for (Entry<String, String> e : headers.entrySet()) {
        req.addHeader(e.getKey(), e.getValue());
    }

    if (canHaveBody) {
        HttpEntityEnclosingRequestBase entityEnclosingReq = (HttpEntityEnclosingRequestBase) req;

        if (body != null) {

            NByteArrayEntity entity = new NByteArrayEntity(body);

            if (contentType != null) {
                entity.setContentType(contentType);
            }

            entityEnclosingReq.setEntity(entity);
        } else {

            MultipartEntityBuilder builder = MultipartEntityBuilder.create();

            for (Entry<String, String> entry : files.entrySet()) {
                String filename = entry.getValue();
                File file = IO.file(filename);
                builder = builder.addBinaryBody(entry.getKey(), file, ContentType.DEFAULT_BINARY, filename);
            }

            for (Entry<String, String> entry : data.entrySet()) {
                builder = builder.addTextBody(entry.getKey(), entry.getValue(), ContentType.DEFAULT_TEXT);
            }

            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            try {
                builder.build().writeTo(stream);
            } catch (IOException e) {
                throw U.rte(e);
            }

            byte[] bytes = stream.toByteArray();
            NByteArrayEntity entity = new NByteArrayEntity(bytes, ContentType.MULTIPART_FORM_DATA);

            entityEnclosingReq.setEntity(entity);
        }
    }

    Log.debug("Starting HTTP request", "request", req.getRequestLine());

    return execute(client, req, callback, fullResponse);
}

From source file:org.wso2.bps.integration.common.clients.bpmn.ActivitiRestClient.java

/**
 * This Method is used to deploy BPMN packages to the BPMN Server
 *
 * @param fileName The name of the Package to be deployed
 * @param filePath The location of the BPMN package to be deployed
 * @throws java.io.IOException// ww  w  . ja v a 2 s. co  m
 * @throws org.json.JSONException
 * @returns String array with status, deploymentID and Name
 */
public String[] deployBPMNPackage(String filePath, String fileName) throws Exception {
    String url = serviceURL + "repository/deployments";

    HttpHost target = new HttpHost(hostname, port, "http");

    DefaultHttpClient httpClient = new DefaultHttpClient();
    httpClient.getCredentialsProvider().setCredentials(new AuthScope(target.getHostName(), target.getPort()),
            new UsernamePasswordCredentials(username, password));

    HttpPost httpPost = new HttpPost(url);

    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    builder.addBinaryBody("file", new File(filePath), ContentType.MULTIPART_FORM_DATA, fileName);
    HttpEntity multipart = builder.build();
    httpPost.setEntity(multipart);
    HttpResponse response = httpClient.execute(httpPost);
    String status = response.getStatusLine().toString();
    String responseData = EntityUtils.toString(response.getEntity());
    JSONObject jsonResponseObject = new JSONObject(responseData);
    if (status.contains(Integer.toString(HttpStatus.SC_CREATED))
            || status.contains(Integer.toString(HttpStatus.SC_OK))) {
        String deploymentID = jsonResponseObject.getString("id");
        String name = jsonResponseObject.getString("name");
        return new String[] { status, deploymentID, name };
    } else if (status.contains(Integer.toString(HttpStatus.SC_INTERNAL_SERVER_ERROR))) {

        String errorMessage = jsonResponseObject.getString("errorMessage");
        throw new RestClientException(errorMessage);
        //            return new String[]{status, errorMessage};
    } else {
        throw new RestClientException("Failed to deploy package " + fileName);
    }
}

From source file:org.wso2.ei.businessprocess.integration.common.clients.bpmn.ActivitiRestClient.java

/**
 * This Method is used to deploy BPMN packages to the BPMN Server
 *
 * @param fileName The name of the Package to be deployed
 * @param filePath The location of the BPMN package to be deployed
 * @throws java.io.IOException//from   w  ww  . java 2s .  c  om
 * @throws org.json.JSONException
 * @returns String array with status, deploymentID and Name
 */
public String[] deployBPMNPackage(String filePath, String fileName)
        throws RestClientException, IOException, JSONException {
    String url = serviceURL + "repository/deployments";
    DefaultHttpClient httpClient = getHttpClient();
    HttpPost httpPost = new HttpPost(url);
    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    builder.addBinaryBody("file", new File(filePath), ContentType.MULTIPART_FORM_DATA, fileName);
    HttpEntity multipart = builder.build();
    httpPost.setEntity(multipart);
    HttpResponse response = httpClient.execute(httpPost);
    String status = response.getStatusLine().toString();
    String responseData = EntityUtils.toString(response.getEntity());
    JSONObject jsonResponseObject = new JSONObject(responseData);
    if (status.contains(Integer.toString(HttpStatus.SC_CREATED))
            || status.contains(Integer.toString(HttpStatus.SC_OK))) {
        String deploymentID = jsonResponseObject.getString(ID);
        String name = jsonResponseObject.getString(NAME);
        return new String[] { status, deploymentID, name };
    } else if (status.contains(Integer.toString(HttpStatus.SC_INTERNAL_SERVER_ERROR))) {
        String errorMessage = jsonResponseObject.getString("errorMessage");
        throw new RestClientException(errorMessage);
    } else {
        throw new RestClientException("Failed to deploy package " + fileName);
    }
}