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

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

Introduction

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

Prototype

ContentType DEFAULT_BINARY

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

Click Source Link

Usage

From source file:cn.vlabs.duckling.vwb.service.ddl.RestClient.java

private HttpEntity buildMultiPartForm(String dataFieldName, String filename, InputStream stream,
        String... params) {/* w ww  .jav a 2 s .  c om*/
    MultipartEntityBuilder builder = MultipartEntityBuilder.create()
            .setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
            .addBinaryBody(dataFieldName, stream, ContentType.DEFAULT_BINARY, filename);

    if (params != null) {
        for (int i = 0; i < params.length / 2; i++) {
            StringBody contentBody = new StringBody(params[i * 2 + 1], DEFAULT_CONTENT_TYPE);
            builder.addPart(params[i * 2], contentBody);
        }
    }

    HttpEntity reqEntity = builder.setCharset(DEFAULT_CHARSET).build();
    return reqEntity;
}

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 {/*  w  ww .j ava  2  s  .  c  o 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.cognifide.qa.bb.aem.content.ContentInstaller.java

/**
 * This method uploads, installs and replicates the package indicated by the name
 * provided as the method's parameter. For each of these actions activateAemPackage constructs
 * and sends a POST request to AEM instance. If any of the POST requests lead to NOK response,
 * activateAemPackage will throw an exception.
 * <br>/*from w ww  .  j  a  v a  2s .com*/
 * Method will look for content in the location indicated by the content.path property.
 * The content path defaults to "src/main/content".
 *
 * @param packageName Name of the package to be activated.
 * @throws IOException Thrown when AEM instance returns NOK response.
 */
public void activateAemPackage(String packageName) throws IOException {
    HttpPost upload = builder.createUploadRequest();
    MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
    entityBuilder.addBinaryBody("package", new File(CONTENT_PATH, packageName), ContentType.DEFAULT_BINARY,
            packageName);
    entityBuilder.addTextBody("force", "true");
    upload.setEntity(entityBuilder.build());
    JsonObject result = sender.sendCrxRequest(upload);
    String path = result.get("path").getAsString();

    HttpPost install = builder.createInstallRequest(path);
    sender.sendCrxRequest(install);

    HttpPost replicate = builder.createReplicateRequest(path);

    sender.sendCrxRequest(replicate);
}

From source file:com.redhat.jenkins.plugins.bayesian.Bayesian.java

public BayesianStepResponse submitStackForAnalysis(Collection<FilePath> manifests) throws BayesianException {
    String stackAnalysesUrl = getApiUrl() + "/stack-analyses";
    HttpPost httpPost = new HttpPost(stackAnalysesUrl);

    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    for (FilePath manifest : manifests) {
        byte[] content = null;
        try (InputStream in = manifest.read()) {
            content = ByteStreams.toByteArray(in);
            builder.addBinaryBody("manifest[]", content, ContentType.DEFAULT_BINARY, manifest.getName());
        } catch (IOException | InterruptedException e) {
            throw new BayesianException(e);
        } finally {
            content = null;//from   ww w. j ava 2  s  . c om
        }
    }
    HttpEntity multipart = builder.build();
    builder = null;
    httpPost.setEntity(multipart);
    httpPost.setHeader("Authorization", "Bearer " + getAuthToken());

    BayesianResponse responseObj = null;
    Gson gson;
    try (CloseableHttpClient client = HttpClients.createDefault();
            CloseableHttpResponse response = client.execute(httpPost)) {
        HttpEntity entity = response.getEntity();
        // Yeah, the endpoint actually returns 200 from some reason;
        // I wonder what happened to the good old-fashioned 202 :)
        if (response.getStatusLine().getStatusCode() != 200) {
            throw new BayesianException("Bayesian error: " + response.getStatusLine().getStatusCode());
        }

        Charset charset = ContentType.get(entity).getCharset();
        try (InputStream is = entity.getContent();
                Reader reader = new InputStreamReader(is,
                        charset != null ? charset : HTTP.DEF_CONTENT_CHARSET)) {
            gson = new GsonBuilder().create();
            responseObj = gson.fromJson(reader, BayesianResponse.class);
            String analysisUrl = stackAnalysesUrl + "/" + responseObj.getId();
            return new BayesianStepResponse(responseObj.getId(), "", analysisUrl, true);
        }
    } catch (IOException e) {
        throw new BayesianException("Bayesian error", e);
    } finally {
        // just to be sure...
        responseObj = null;
        httpPost = null;
        multipart = null;
        gson = null;
    }
}

From source file:org.jenkinsci.plugins.relution_publisher.net.requests.ZeroCopyFileRequestProducer.java

private String getContentType(final File file) {

    try {/*w ww. ja  v a 2  s .  co m*/
        final Tika tika = new Tika();
        return tika.detect(file);

    } catch (final IOException e) {
        return ContentType.DEFAULT_BINARY.toString();
    }
}

From source file:com.nextdoor.bender.ipc.http.HttpTransport.java

protected HttpResponse sendBatchCompressed(HttpPost httpPost, byte[] raw) throws TransportException {
    /*// www. j a  v a  2  s  . c om
     * Write gzip data to Entity and set content encoding to gzip
     */
    ByteArrayEntity entity = new ByteArrayEntity(raw, ContentType.DEFAULT_BINARY);
    entity.setContentEncoding("gzip");

    httpPost.addHeader(new BasicHeader("Accept-Encoding", "gzip"));
    httpPost.setEntity(entity);

    /*
     * Make call
     */
    HttpResponse resp = null;
    try {
        resp = this.client.execute(httpPost);
    } catch (IOException e) {
        throw new TransportException("failed to make call", e);
    }

    return resp;
}

From source file:org.lokra.seaweedfs.core.VolumeWrapperTest.java

@Test
public void getFileStatusHeader() throws Exception {
    AssignFileKeyParams params = new AssignFileKeyParams();
    AssignFileKeyResult result = masterWrapper.assignFileKey(params);

    volumeWrapper.uploadFile(result.getUrl(), result.getFid(), "test.txt",
            new ByteArrayInputStream("@getFileStatusHeader".getBytes()), null, ContentType.DEFAULT_BINARY);

    Assert.assertTrue(volumeWrapper.getFileStatusHeader(result.getUrl(), result.getFid())
            .getLastHeader("Content-Length").getValue().equals("44"));
}

From source file:org.apache.heron.uploader.http.HttpUploader.java

private URI uploadPackageAndGetURI(final CloseableHttpClient httpclient)
        throws IOException, URISyntaxException {
    File file = new File(this.topologyPackageLocation);
    String uploaderUri = HttpUploaderContext.getHeronUploaderHttpUri(this.config);
    post = new HttpPost(uploaderUri);
    FileBody fileBody = new FileBody(file, ContentType.DEFAULT_BINARY);
    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    builder.addPart(FILE, fileBody);//w w  w .  j a  v  a  2  s  .co m
    HttpEntity entity = builder.build();
    post.setEntity(entity);
    HttpResponse response = execute(httpclient);
    String responseString = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8.name());
    LOG.fine("Topology package download URI: " + responseString);

    return new URI(responseString);
}

From source file:com.github.piotrkot.resources.CompilerResource.java

/**
 * Upload source for compiler./*from   w  w w  . j  a v a 2s  .  com*/
 * @param user Logged in user.
 * @param stream File input stream.
 * @param details Form data details.
 * @return Compiler result view.
 */
@Path("/source")
@POST
@Timed
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_HTML)
public Response uploadSource(@Auth final User user, @FormDataParam("inputFile") final InputStream stream,
        @FormDataParam("inputFile") final FormDataContentDisposition details) {
    Response response = Response.serverError().build();
    try {
        final String request = Request.Post(String.format("%s/source", this.conf.getCompiler()))
                .setHeader(this.authorization(user))
                .body(MultipartEntityBuilder.create()
                        .addBinaryBody("file", stream, ContentType.DEFAULT_BINARY, details.getName()).build())
                .execute().returnContent().asString();
        response = Response.seeOther(URI.create(String.format("/compiler/result/%s", request))).build();
    } catch (final IOException ex) {
        log.error("Cannot connect to the server", ex);
    }
    return response;
}