Example usage for org.apache.http.entity FileEntity FileEntity

List of usage examples for org.apache.http.entity FileEntity FileEntity

Introduction

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

Prototype

public FileEntity(File file, ContentType contentType) 

Source Link

Usage

From source file:org.jfrog.build.client.ArtifactoryBuildInfoClient.java

private ArtifactoryUploadResponse uploadFile(DeployDetails details, String uploadUrl) throws IOException {
    ArtifactoryUploadResponse response = tryChecksumDeploy(details, uploadUrl);
    if (response != null) {
        // Checksum deploy was performed:
        return response;
    }//from www  . j a  v  a2  s  .  c o m

    HttpPut httpPut = createHttpPutMethod(details, uploadUrl);
    // add the 100 continue directive
    httpPut.addHeader(HTTP.EXPECT_DIRECTIVE, HTTP.EXPECT_CONTINUE);

    FileEntity fileEntity = new FileEntity(details.file, "binary/octet-stream");

    response = httpClient.upload(httpPut, fileEntity);
    int statusCode = response.getStatusLine().getStatusCode();

    //Accept both 200, and 201 for backwards-compatibility reasons
    if ((statusCode != HttpStatus.SC_CREATED) && (statusCode != HttpStatus.SC_OK)) {
        throwHttpIOException("Failed to deploy file:", response.getStatusLine());
    }

    return response;
}

From source file:com.sangupta.jerry.http.WebRequest.java

/**
* Set the body from given file for the given content type.
* 
* @param file/* w w  w  .j a v a  2  s .  co m*/
*            the {@link File} to read from
* 
* @param contentType
*            the {@link ContentType} of the file
* 
* @return this very {@link WebRequest}
*/
public WebRequest bodyFile(final File file, final ContentType contentType) {
    return body(new FileEntity(file, contentType));
}

From source file:com.mxhero.plugin.cloudstorage.onedrive.api.Items.java

/**
 * Simple upload.//from w w w . ja  v a2 s .  c  om
 *
 * @param path the path
 * @param file the file
 * @param conflictBehavior the conflict behavior
 * @return the item
 * @throws IllegalArgumentException the illegal argument exception
 */
private Item simpleUpload(final String path, final File file, final ConflictBehavior conflictBehavior) {
    final Command<Item> command = this.commandFactory.create();
    command.validate(Validator.builder().file().name(file.getName()).build());
    return command.excecute(new CommandHandler<Item>() {

        @Override
        public Item response(HttpResponse response) {
            try {
                if (response.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED
                        || response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                    return OneDrive.JACKSON.readValue(EntityUtils.toString(response.getEntity()), Item.class);
                }
                if (response.getStatusLine().getStatusCode() > 399) {
                    throw new ApiException(response);
                }
                throw new IllegalArgumentException(
                        "error reading response body with code " + response.getStatusLine().getStatusCode());
            } catch (Exception e) {
                throw new IllegalArgumentException("error reading response", e);
            }
        }

        /* (non-Javadoc)
         * @see com.mxhero.plugin.cloudstorage.onedrive.api.command.CommandHandler#request()
         */
        @Override
        public HttpUriRequest request() {
            try {
                URIBuilder builder = new URIBuilder(command.baseUrl() + path);
                builder.addParameter("@name.conflictBehavior", conflictBehavior.name());
                HttpPut httpPut = new HttpPut(builder.build().toString());
                httpPut.setHeader("Content-Type", "text/plain");
                httpPut.setEntity(new FileEntity(file, ContentType.TEXT_PLAIN));
                return httpPut;
            } catch (Exception e) {
                throw new IllegalArgumentException("couldn't create query", e);
            }
        }
    });
}

From source file:com.buffalokiwi.api.API.java

/**
 * Perform a patch-based request to some endpoint
 * @param url URL/*from  w  ww .  j  ava 2  s .  c o  m*/
 * @param file file to send 
 * @param headers additional headers 
 * @return response 
 * @throws APIException 
 */
@Override
public IAPIResponse patch(final String url, final PostFile file, Map<String, String> headers)
        throws APIException {
    final FileEntity entity = new FileEntity(file.getFile(), file.getContentType());
    if (file.hasContentEncoding())
        entity.setContentEncoding(file.getContentEncoding());

    //..Create the new patch request
    final HttpPatch patch = (HttpPatch) createRequest(HttpMethod.PUT, url, headers);

    //..Set the patch payload
    patch.setEntity(entity);

    APILog.trace(LOG, entity.toString());

    //..Execute the request
    return executeRequest(patch);

}

From source file:android.webkit.cts.CtsTestServer.java

private static FileEntity createFileEntity(String downloadId, int numBytes) throws IOException {
    String storageState = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equalsIgnoreCase(storageState)) {
        File storageDir = Environment.getExternalStorageDirectory();
        File file = new File(storageDir, downloadId + ".bin");
        BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(file));
        byte data[] = new byte[1024];
        for (int i = 0; i < data.length; i++) {
            data[i] = 1;/*  w w  w .  j  a v  a  2s .co m*/
        }
        try {
            for (int i = 0; i < numBytes / data.length; i++) {
                stream.write(data);
            }
            stream.write(data, 0, numBytes % data.length);
            stream.flush();
        } finally {
            stream.close();
        }
        return new FileEntity(file, "application/octet-stream");
    } else {
        throw new IllegalStateException("External storage must be mounted for this test!");
    }
}

From source file:ti.modules.titanium.network.TiHTTPClient.java

private Object titaniumFileAsPutData(Object value) {
    if (value instanceof TiBaseFile && !(value instanceof TiResourceFile)) {
        TiBaseFile baseFile = (TiBaseFile) value;
        return new FileEntity(baseFile.getNativeFile(), TiMimeTypeHelper.getMimeType(baseFile.nativePath()));
    } else if (value instanceof TiBlob || value instanceof TiResourceFile) {
        try {/*from   www  . java  2  s. c  om*/
            TiBlob blob;
            if (value instanceof TiBlob) {
                blob = (TiBlob) value;
            } else {
                blob = ((TiResourceFile) value).read();
            }
            String mimeType = blob.getMimeType();
            File tmpFile = File.createTempFile("tixhr",
                    "." + TiMimeTypeHelper.getFileExtensionFromMimeType(mimeType, "txt"));
            FileOutputStream fos = new FileOutputStream(tmpFile);
            fos.write(blob.getBytes());
            fos.close();

            tmpFiles.add(tmpFile);
            return new FileEntity(tmpFile, mimeType);
        } catch (IOException e) {
            Log.e(TAG, "Error adding put data: " + e.getMessage());
        }
    }
    return value;
}

From source file:edu.isi.karma.er.helper.TripleStoreUtil.java

/**
 * @param filePath//from   www .j a va2 s.c  o m
 *            : the url of the file from where the RDF is read
 * @param tripleStoreURL
 *            : the triple store URL
 * @param context
 *            : The graph context for the RDF
 * @param replaceFlag
 *            : Whether to replace the contents of the graph deleteSrcFile
 *            default : false rdfType default: Turtle
 * @param baseUri
 *            : The graph context for the RDF
 * 
 * */
public boolean saveToStoreFromFile(String filePath, String tripleStoreURL, String context, boolean replaceFlag,
        String baseUri) throws KarmaException {
    File file = new File(filePath);
    FileEntity entity = new FileEntity(file,
            ContentType.create(mime_types.get(RDF_Types.Turtle.name()), "UTF-8"));
    return saveToStore(entity, tripleStoreURL, context, replaceFlag, RDF_Types.Turtle.name(), baseUri);
}