Example usage for org.apache.http.entity.mime MultipartEntityBuilder build

List of usage examples for org.apache.http.entity.mime MultipartEntityBuilder build

Introduction

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

Prototype

public HttpEntity build() 

Source Link

Usage

From source file:net.monofraps.gradlecurse.tasks.CurseDeployTask.java

private void uploadArtifact(final Deployment deployment) {
    getLogger().lifecycle("Curse Upload: " + "Uploading to Curse...");
    getLogger().lifecycle("Curse Upload: " + deployment.toString());

    //TODO: binary or app/zip, maybe an option or auto-detect from file extension ?!
    final FileBody fileBody = new FileBody(deployment.getSourceFile(), ContentType.DEFAULT_BINARY,
            deployment.getUploadFileName());

    final MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
    multipartEntityBuilder.addTextBody("name", deployment.getUploadFileName());
    multipartEntityBuilder.addTextBody("file_type", deployment.getFileType().toString());
    multipartEntityBuilder.addTextBody("change_log", deployment.getChangeLog());
    multipartEntityBuilder.addTextBody("change_markup_type", deployment.getChangeLogMarkup().toString());
    multipartEntityBuilder.addTextBody("known_caveats", deployment.getKnownCaveats());
    multipartEntityBuilder.addTextBody("caveats_markup_type", deployment.getCaveatMarkup().toString());
    multipartEntityBuilder.addPart("file", fileBody);
    multipartEntityBuilder.addTextBody("game_versions", StringUtils.join(deployment.getGameVersions(), ","));

    try {/*from   ww w  .ja  v a  2 s. co m*/
        final HttpPost httpPost = new HttpPost(probeForRedirect(deployment));
        httpPost.addHeader("User-Agent", "GradleCurse Uploader/1.0");
        httpPost.addHeader("X-API-Key", deployment.getApiKey());
        httpPost.setEntity(multipartEntityBuilder.build());

        final HttpClient httpClient = HttpClientBuilder.create().build();
        final HttpResponse httpResponse = httpClient.execute(httpPost);

        getLogger().lifecycle("Curse Upload: " + httpResponse.getStatusLine());
        getLogger().debug(EntityUtils.toString(httpResponse.getEntity()));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.qmetry.qaf.automation.integration.qmetry.qmetry7.QMetryRestWebservice.java

public int attachTestLogs(long tcVersionIdASEntityId, File filePath) {
    try {/*  w w w .  j  a  v  a2 s.  c  o  m*/
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-HHmmss");

        final String CurrentDate = format.format(new Date());
        Path path = Paths.get(filePath.toURI());
        byte[] outFileArray = Files.readAllBytes(path);

        if (outFileArray != null) {
            CloseableHttpClient httpclient = HttpClients.createDefault();
            try {
                HttpPost httppost = new HttpPost(serviceUrl + "/rest/attachments");

                MultipartEntityBuilder builder = MultipartEntityBuilder.create();
                builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

                FileBody bin = new FileBody(filePath);
                builder.addPart("file", bin);

                HttpEntity reqEntity = builder.build();
                httppost.setEntity(reqEntity);
                httppost.addHeader("usertoken", token);
                httppost.addHeader("scope", scope);

                CloseableHttpResponse response = httpclient.execute(httppost);
                String str = null;
                try {
                    str = EntityUtils.toString(response.getEntity());
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                }
                JsonElement gson = new Gson().fromJson(str, JsonElement.class);
                JsonElement data = gson.getAsJsonObject().get("data");
                int id = Integer.parseInt(data.getAsJsonArray().get(0).getAsJsonObject().get("id").toString());
                return id;
            } finally {
                httpclient.close();
            }
        } else {
            System.out.println(filePath + " file does not exists");
        }
    } catch (Exception ex) {
        System.out.println("Error in attaching file - " + filePath);
        System.out.println(ex.getMessage());
    }
    return 0;
}

From source file:org.wso2.carbon.ml.integration.common.utils.MLHttpClient.java

/**
 * @throws MLHttpClientException/*from w  w  w  . j a  v a  2  s.  c om*/
 */
public CloseableHttpResponse predictFromCSV(long modelId, String resourcePath) throws MLHttpClientException {
    CloseableHttpClient httpClient = HttpClients.createDefault();
    try {
        HttpPost httpPost = new HttpPost(getServerUrlHttps() + "/api/models/predict");
        httpPost.setHeader(MLIntegrationTestConstants.AUTHORIZATION_HEADER, getBasicAuthKey());

        MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
        multipartEntityBuilder.addPart("modelId", new StringBody(modelId + "", ContentType.TEXT_PLAIN));
        multipartEntityBuilder.addPart("dataFormat", new StringBody("CSV", ContentType.TEXT_PLAIN));

        if (resourcePath != null) {
            File file = new File(getResourceAbsolutePath(resourcePath));
            multipartEntityBuilder.addBinaryBody("file", file, ContentType.APPLICATION_OCTET_STREAM,
                    "IndiansDiabetesPredict.csv");
        }
        httpPost.setEntity(multipartEntityBuilder.build());
        return httpClient.execute(httpPost);
    } catch (Exception e) {
        throw new MLHttpClientException("Failed to predict from csv " + resourcePath, e);
    }
}

From source file:io.swagger.client.api.CameraApi.java

/**
* Capture a photo//w w  w .java 2s  .c  o m
* 
        
*/
public void capturePhotoPost(final Response.Listener<String> responseListener,
        final Response.ErrorListener errorListener) {
    Object postBody = null;

    // create path and map variables
    String path = "/capture/photo".replaceAll("\\{format\\}", "json");

    // query params
    List<Pair> queryParams = new ArrayList<Pair>();
    // header params
    Map<String, String> headerParams = new HashMap<String, String>();
    // form params
    Map<String, String> formParams = new HashMap<String, String>();

    String[] contentTypes = {

    };
    String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";

    if (contentType.startsWith("multipart/form-data")) {
        // file uploading
        MultipartEntityBuilder localVarBuilder = MultipartEntityBuilder.create();

        HttpEntity httpEntity = localVarBuilder.build();
        postBody = httpEntity;
    } else {
        // normal form params
    }

    String[] authNames = new String[] {};

    try {
        apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, formParams,
                contentType, authNames, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String localVarResponse) {
                        responseListener.onResponse(localVarResponse);
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        errorListener.onErrorResponse(error);
                    }
                });
    } catch (ApiException ex) {
        errorListener.onErrorResponse(new VolleyError(ex));
    }
}

From source file:io.swagger.client.api.CameraApi.java

/**
* Start recording a movie//from w w  w .jav a2  s.  c om
* 
        
*/
public void captureStartPost(final Response.Listener<String> responseListener,
        final Response.ErrorListener errorListener) {
    Object postBody = null;

    // create path and map variables
    String path = "/capture/start".replaceAll("\\{format\\}", "json");

    // query params
    List<Pair> queryParams = new ArrayList<Pair>();
    // header params
    Map<String, String> headerParams = new HashMap<String, String>();
    // form params
    Map<String, String> formParams = new HashMap<String, String>();

    String[] contentTypes = {

    };
    String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";

    if (contentType.startsWith("multipart/form-data")) {
        // file uploading
        MultipartEntityBuilder localVarBuilder = MultipartEntityBuilder.create();

        HttpEntity httpEntity = localVarBuilder.build();
        postBody = httpEntity;
    } else {
        // normal form params
    }

    String[] authNames = new String[] {};

    try {
        apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, formParams,
                contentType, authNames, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String localVarResponse) {
                        responseListener.onResponse(localVarResponse);
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        errorListener.onErrorResponse(error);
                    }
                });
    } catch (ApiException ex) {
        errorListener.onErrorResponse(new VolleyError(ex));
    }
}

From source file:io.swagger.client.api.CameraApi.java

/**
* Stop recording a movie/*from w w w. ja  v  a2 s.  c  om*/
* 
        
*/
public void captureStopPost(final Response.Listener<String> responseListener,
        final Response.ErrorListener errorListener) {
    Object postBody = null;

    // create path and map variables
    String path = "/capture/stop".replaceAll("\\{format\\}", "json");

    // query params
    List<Pair> queryParams = new ArrayList<Pair>();
    // header params
    Map<String, String> headerParams = new HashMap<String, String>();
    // form params
    Map<String, String> formParams = new HashMap<String, String>();

    String[] contentTypes = {

    };
    String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";

    if (contentType.startsWith("multipart/form-data")) {
        // file uploading
        MultipartEntityBuilder localVarBuilder = MultipartEntityBuilder.create();

        HttpEntity httpEntity = localVarBuilder.build();
        postBody = httpEntity;
    } else {
        // normal form params
    }

    String[] authNames = new String[] {};

    try {
        apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, formParams,
                contentType, authNames, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String localVarResponse) {
                        responseListener.onResponse(localVarResponse);
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        errorListener.onErrorResponse(error);
                    }
                });
    } catch (ApiException ex) {
        errorListener.onErrorResponse(new VolleyError(ex));
    }
}

From source file:io.swagger.client.api.CameraApi.java

/**
* Start liveview//from w w  w  .  j  a  v  a2  s  .  co  m
* 
        
*/
public void liveviewStartPost(final Response.Listener<String> responseListener,
        final Response.ErrorListener errorListener) {
    Object postBody = null;

    // create path and map variables
    String path = "/liveview/start".replaceAll("\\{format\\}", "json");

    // query params
    List<Pair> queryParams = new ArrayList<Pair>();
    // header params
    Map<String, String> headerParams = new HashMap<String, String>();
    // form params
    Map<String, String> formParams = new HashMap<String, String>();

    String[] contentTypes = {

    };
    String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";

    if (contentType.startsWith("multipart/form-data")) {
        // file uploading
        MultipartEntityBuilder localVarBuilder = MultipartEntityBuilder.create();

        HttpEntity httpEntity = localVarBuilder.build();
        postBody = httpEntity;
    } else {
        // normal form params
    }

    String[] authNames = new String[] {};

    try {
        apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, formParams,
                contentType, authNames, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String localVarResponse) {
                        responseListener.onResponse(localVarResponse);
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        errorListener.onErrorResponse(error);
                    }
                });
    } catch (ApiException ex) {
        errorListener.onErrorResponse(new VolleyError(ex));
    }
}

From source file:io.swagger.client.api.CameraApi.java

/**
* Stop liveview//from w w  w .j  a v a2  s . c o m
* 
        
*/
public void liveviewStopPost(final Response.Listener<String> responseListener,
        final Response.ErrorListener errorListener) {
    Object postBody = null;

    // create path and map variables
    String path = "/liveview/stop".replaceAll("\\{format\\}", "json");

    // query params
    List<Pair> queryParams = new ArrayList<Pair>();
    // header params
    Map<String, String> headerParams = new HashMap<String, String>();
    // form params
    Map<String, String> formParams = new HashMap<String, String>();

    String[] contentTypes = {

    };
    String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";

    if (contentType.startsWith("multipart/form-data")) {
        // file uploading
        MultipartEntityBuilder localVarBuilder = MultipartEntityBuilder.create();

        HttpEntity httpEntity = localVarBuilder.build();
        postBody = httpEntity;
    } else {
        // normal form params
    }

    String[] authNames = new String[] {};

    try {
        apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, formParams,
                contentType, authNames, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String localVarResponse) {
                        responseListener.onResponse(localVarResponse);
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        errorListener.onErrorResponse(error);
                    }
                });
    } catch (ApiException ex) {
        errorListener.onErrorResponse(new VolleyError(ex));
    }
}

From source file:io.swagger.client.api.CameraApi.java

/**
* Set camera settings/*from   ww w .j  av  a2 s . co  m*/
* Allow to set camera settings
* @param body order placed for purchasing the pet
*/
public void settingsPost(CameraSettings body, final Response.Listener<String> responseListener,
        final Response.ErrorListener errorListener) {
    Object postBody = body;

    // create path and map variables
    String path = "/settings".replaceAll("\\{format\\}", "json");

    // query params
    List<Pair> queryParams = new ArrayList<Pair>();
    // header params
    Map<String, String> headerParams = new HashMap<String, String>();
    // form params
    Map<String, String> formParams = new HashMap<String, String>();

    String[] contentTypes = {

    };
    String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";

    if (contentType.startsWith("multipart/form-data")) {
        // file uploading
        MultipartEntityBuilder localVarBuilder = MultipartEntityBuilder.create();

        HttpEntity httpEntity = localVarBuilder.build();
        postBody = httpEntity;
    } else {
        // normal form params
    }

    String[] authNames = new String[] {};

    try {
        apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, formParams,
                contentType, authNames, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String localVarResponse) {
                        responseListener.onResponse(localVarResponse);
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        errorListener.onErrorResponse(error);
                    }
                });
    } catch (ApiException ex) {
        errorListener.onErrorResponse(new VolleyError(ex));
    }
}

From source file:io.swagger.client.api.CameraApi.java

/**
* Camera info//  w ww.  java  2 s  .  c  o  m
* Return information about the camera 
        
*/
public void infoGet(final Response.Listener<CameraInfo> responseListener,
        final Response.ErrorListener errorListener) {
    Object postBody = null;

    // create path and map variables
    String path = "/info".replaceAll("\\{format\\}", "json");

    // query params
    List<Pair> queryParams = new ArrayList<Pair>();
    // header params
    Map<String, String> headerParams = new HashMap<String, String>();
    // form params
    Map<String, String> formParams = new HashMap<String, String>();

    String[] contentTypes = {

    };
    String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";

    if (contentType.startsWith("multipart/form-data")) {
        // file uploading
        MultipartEntityBuilder localVarBuilder = MultipartEntityBuilder.create();

        HttpEntity httpEntity = localVarBuilder.build();
        postBody = httpEntity;
    } else {
        // normal form params
    }

    String[] authNames = new String[] {};

    try {
        apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, formParams,
                contentType, authNames, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String localVarResponse) {
                        try {
                            responseListener.onResponse((CameraInfo) ApiInvoker.deserialize(localVarResponse,
                                    "", CameraInfo.class));
                        } catch (ApiException exception) {
                            errorListener.onErrorResponse(new VolleyError(exception));
                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        errorListener.onErrorResponse(error);
                    }
                });
    } catch (ApiException ex) {
        errorListener.onErrorResponse(new VolleyError(ex));
    }
}