Example usage for org.apache.http.entity.mime MultipartEntity addPart

List of usage examples for org.apache.http.entity.mime MultipartEntity addPart

Introduction

In this page you can find the example usage for org.apache.http.entity.mime MultipartEntity addPart.

Prototype

public void addPart(final String name, final ContentBody contentBody) 

Source Link

Usage

From source file:org.wso2.am.integration.tests.publisher.APIM614AddDocumentationToAnAPIWithDocTypeSampleAndSDKThroughPublisherRestAPITestCase.java

@Test(groups = { "wso2.am" }, description = "Add Documentation To An API With Type Other And"
        + " Source File through the publisher rest API ", dependsOnMethods = "testAddDocumentToAnAPISupportToFile")
public void testAddDocumentToAnAPIOtherFile() throws Exception {

    String fileNameAPIM629 = "APIM629.txt";
    String docName = "APIM629PublisherTestHowTo-File-summary";
    String docType = "Other";
    String sourceType = "file";
    String newType = "Type APIM629";
    String summary = "Testing";
    String mimeType = "text/plain";
    String docUrl = "http://";
    String filePathAPIM629 = TestConfigurationProvider.getResourceLocation() + File.separator + "artifacts"
            + File.separator + "AM" + File.separator + "lifecycletest" + File.separator + fileNameAPIM629;
    String addDocUrl = publisherUrls.getWebAppURLHttp() + "publisher/site/blocks/documentation/ajax/docs.jag";

    //Send Http Post request to add a new file
    HttpPost httppost = new HttpPost(addDocUrl);
    File file = new File(filePathAPIM629);
    FileBody fileBody = new FileBody(file, "text/plain");

    //Create multipart entity to upload file as multipart file
    MultipartEntity multipartEntity = new MultipartEntity();
    multipartEntity.addPart("docLocation", fileBody);
    multipartEntity.addPart("mode", new StringBody(""));
    multipartEntity.addPart("docName", new StringBody(docName));
    multipartEntity.addPart("docUrl", new StringBody(docUrl));
    multipartEntity.addPart("sourceType", new StringBody(sourceType));
    multipartEntity.addPart("summary", new StringBody(summary));
    multipartEntity.addPart("docType", new StringBody(docType));
    multipartEntity.addPart("version", new StringBody(apiVersion));
    multipartEntity.addPart("apiName", new StringBody(apiName));
    multipartEntity.addPart("action", new StringBody("addDocumentation"));
    multipartEntity.addPart("provider", new StringBody(apiProvider));
    multipartEntity.addPart("mimeType", new StringBody(mimeType));
    multipartEntity.addPart("newType", new StringBody(newType));
    multipartEntity.addPart("optionsRadios", new StringBody(docType));
    multipartEntity.addPart("optionsRadios1", new StringBody(sourceType));
    multipartEntity.addPart("optionsRadios1", new StringBody(sourceType));

    httppost.setEntity(multipartEntity);

    //Upload created file and validate
    HttpResponse response = httpClient.execute(httppost);
    HttpEntity entity = response.getEntity();
    JSONObject jsonObject1 = new JSONObject(EntityUtils.toString(entity));
    assertFalse(jsonObject1.getBoolean("error"), "Error when adding files to the API ");
}

From source file:com.brightcove.com.uploader.helper.MediaAPIHelper.java

/**
 * Executes a file upload write api call against the given URI.
 * This is useful for create_video and add_image
 * @param json the json representation of the call you are making
 * @param file the file you are uploading
 * @param uri the api servlet you want to execute against
 * @return json response from api/*from w ww.j  a va 2s . c  om*/
 * @throws BadEnvironmentException
 * @throws MediaAPIError
 */
public JsonNode executeWrite(JsonNode json, File file, URI uri) throws BadEnvironmentException, MediaAPIError {

    mLog.debug("using " + uri.getHost() + " on port " + uri.getPort() + " for api write");

    HttpPost method = new HttpPost(uri);
    MultipartEntity entityIn = new MultipartEntity();
    FileBody fileBody = null;

    if (file != null) {
        fileBody = new FileBody(file);
    }

    try {
        entityIn.addPart("JSON-RPC", new StringBody(json.toString(), Charset.forName("UTF-8")));
    } catch (UnsupportedEncodingException e) {
        throw new BadEnvironmentException("UTF-8 no longer supported");
    }

    if (file != null) {
        entityIn.addPart(file.getName(), fileBody);
    }
    method.setEntity(entityIn);

    return executeCall(method);
}

From source file:edu.scripps.fl.pubchem.web.session.WebSessionBase.java

protected void addFormParts(Node formNode, MultipartEntity entity, Set<String> ignore)
        throws UnsupportedEncodingException {
    FormControlVisitorSupport fcvs = new FormControlVisitorSupport();
    formNode.accept(fcvs);/*from  w w  w . jav  a2s  . c  o  m*/
    for (Map.Entry<String, String> entry : fcvs.getFormParameters().entrySet()) {
        if (!ignore.contains(entry.getKey()))
            entity.addPart(entry.getKey(), new StringBody(entry.getValue()));
    }
}

From source file:org.semantictools.web.upload.AppspotUploadClient.java

public void upload(String contentType, String path, File file) throws IOException {

    if (file.getName().equals(CHECKSUM_PROPERTIES)) {
        return;//from   w w w  . jav  a2 s  . c  o m
    }

    if (!path.startsWith("/")) {
        path = "/" + path;
    }

    // Do not upload if we can confirm that we previously uploaded
    // the same content.

    String checksumKey = path.concat(".sha1");
    String checksumValue = null;
    try {
        checksumValue = Checksum.sha1(file);
        String prior = checksumProperties.getProperty(checksumKey);
        if (checksumValue.equals(prior)) {
            return;
        }

    } catch (NoSuchAlgorithmException e) {
        // Ignore.
    }

    logger.debug("uploading... " + path);

    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(servletURL);
    post.setHeader("CONTENT-TYPE", "multipart/form-data; boundary=xxxBOUNDARYxxx");
    MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, "xxxBOUNDARYxxx",
            Charset.forName("UTF-8"));

    FileBody body = new FileBody(file, contentType);

    entity.addPart(CONTENT_TYPE, new StringBody(contentType));
    entity.addPart(PATH, new StringBody(path));
    if (version != null) {
        entity.addPart(VERSION, new StringBody(version));
    }
    entity.addPart(FILE_UPLOAD, body);

    post.setEntity(entity);

    String response = EntityUtils.toString(client.execute(post).getEntity(), "UTF-8");

    client.getConnectionManager().shutdown();

    if (checksumValue != null) {
        checksumProperties.put(checksumKey, checksumValue);
    }

}

From source file:pl.llp.aircasting.util.http.HttpBuilder.java

private HttpEntity prepareMultipart() throws UnsupportedEncodingException {
    MultipartEntity result = new MultipartEntity();

    for (Parameter parameter : parameters) {
        result.addPart(parameter.getKey(), parameter.toBody());
    }//from w w  w .j a va 2  s .c o m

    return result;
}

From source file:org.opentraces.metatracker.net.OpenTracesClient.java

public void uploadFile(String fileName) {
    try {/*from   w w  w . j av  a 2s.  c o m*/
        DefaultHttpClient httpclient = new DefaultHttpClient();
        File f = new File(fileName);

        HttpPost httpost = new HttpPost("http://local.geotracing.com/tland/media.srv");
        MultipartEntity entity = new MultipartEntity();
        entity.addPart("myIdentifier", new StringBody("somevalue"));
        entity.addPart("myFile", new FileBody(f));
        httpost.setEntity(entity);

        HttpResponse response;

        response = httpclient.execute(httpost);

        Log.d(LOG_TAG, "Upload result: " + response.getStatusLine());

        if (entity != null) {
            entity.consumeContent();
        }

        httpclient.getConnectionManager().shutdown();

    } catch (Throwable ex) {
        Log.d(LOG_TAG, "Upload failed: " + ex.getMessage() + " Stacktrace: " + ex.getStackTrace());
    }

}

From source file:truesculpt.ui.panels.WebFilePanel.java

private void uploadPicture(File imagefile, File zipobjectfile, String uploadURL, String title,
        String description) throws ParseException, IOException, URISyntaxException {
    HttpClient httpclient = new DefaultHttpClient();

    HttpPost httppost = new HttpPost(uploadURL);
    httppost.addHeader("title", title);
    httppost.addHeader("description", description);
    httppost.addHeader("installationID", UtilsManager.Installation.id(this));

    MultipartEntity mpEntity = new MultipartEntity(HttpMultipartMode.STRICT);
    ContentBody cbImageFile = new FileBody(imagefile, "image/png");
    ContentBody cbObjectFile = new FileBody(zipobjectfile, "application/zip");

    mpEntity.addPart("imagefile", cbImageFile);
    mpEntity.addPart("objectfile", cbObjectFile);

    httppost.setEntity(mpEntity);/*from   ww w.j a  v a2s  .  c o m*/

    System.out.println("executing request " + httppost.getRequestLine());
    HttpResponse httpResponse = httpclient.execute(httppost);

    String myHeader = "displayURL";
    Header[] headers = httpResponse.getHeaders(myHeader);
    for (int i = 0; i < headers.length; i++) {
        Header header = headers[i];
        if (header.getName().equals(myHeader)) {
            String newURL = mStrBaseWebSite + header.getValue();
            Log.d("WEB", "Loading web site " + newURL);
            mWebView.loadUrl(newURL);
        }
    }

}

From source file:com.brightcove.zartan.common.helper.MediaAPIHelper.java

/**
 * Executes a file upload write api call against the given URI. This is useful for create_video
 * and add_image// w w  w. ja v a2s.  c o  m
 * 
 * @param json the json representation of the call you are making
 * @param file the file you are uploading
 * @param uri the api servlet you want to execute against
 * @return json response from api
 * @throws BadEnvironmentException
 * @throws MediaAPIException
 */
public JsonNode executeWrite(JsonNode json, File file, URI uri)
        throws BadEnvironmentException, MediaAPIException {

    mLog.debug("using " + uri.getHost() + " on port " + uri.getPort() + " for api write");

    HttpPost method = new HttpPost(uri);
    MultipartEntity entityIn = new MultipartEntity();
    FileBody fileBody = null;

    if (file != null) {
        fileBody = new FileBody(file);
    }

    try {
        entityIn.addPart("JSON-RPC", new StringBody(json.toString(), Charset.forName("UTF-8")));
    } catch (UnsupportedEncodingException e) {
        throw new BadEnvironmentException("UTF-8 no longer supported");
    }

    if (file != null) {
        entityIn.addPart(file.getName(), fileBody);
    }
    method.setEntity(entityIn);

    return executeCall(method);
}

From source file:net.rcarz.jiraclient.RestClient.java

private JSON request(HttpEntityEnclosingRequestBase req, File file) throws RestException, IOException {
    if (file != null) {
        File fileUpload = file;//  w ww .  j  a v a2  s  .  c  om
        req.setHeader("X-Atlassian-Token", "nocheck");
        MultipartEntity ent = new MultipartEntity();
        ent.addPart("file", new FileBody(fileUpload));
        req.setEntity(ent);
    }
    return request(req);
}