Search in sources :

Example 1 with ContentBody

use of org.apache.http.entity.mime.content.ContentBody in project screenbird by adamhub.

the class FileUtil method postData.

public static String[] postData(String data, final JProgressBar pbUpload, String anonymous_token, String csrf_token, String user_id, ProgressBarUploadProgressListener listener, String upload_url) throws IOException {
    HttpClient httpclient = new DefaultHttpClient();
    httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    HttpPost httppost = new HttpPost(upload_url);
    //File file = new File(fileLocation);
    //MultipartEntity mpEntity = new MultipartEntity();
    ProgressMultiPartEntity mpEntity = new ProgressMultiPartEntity(listener);
    //ContentBody cbFile = new FileBody(file, "video/quicktime");
    //mpEntity.addPart("videoupload", cbFile);
    //ContentBody cbToken = new StringBody(title);
    //mpEntity.addPart("csrfmiddlewaretoken", cbToken);
    ContentBody cbUser_id = new StringBody(user_id);
    mpEntity.addPart("user_id", cbUser_id);
    ContentBody cbAnonym_id = new StringBody(MediaUtil.getMacAddress());
    mpEntity.addPart("anonym_id", cbAnonym_id);
    //ContentBody cbTitle = new StringBody(title);
    //mpEntity.addPart("title", cbTitle);
    //        ContentBody cbSlug = new StringBody(slug);
    //        mpEntity.addPart("slug", cbSlug);
    //        ContentBody cbPublic = new StringBody(is_public ? "1" : "0");
    //        mpEntity.addPart("is_public", cbPublic);
    //        ContentBody cbDescription = new StringBody(description);
    //        mpEntity.addPart("description", cbDescription);
    //        ContentBody cbChecksum = new StringBody(checksum);
    //        mpEntity.addPart("checksum", cbChecksum);
    //        ContentBody cbType = new StringBody(video_type);
    //        mpEntity.addPart("video_type", cbType);
    httppost.setEntity(mpEntity);
    System.out.println("===================================================");
    System.out.println("executing request " + httppost.getRequestLine());
    System.out.println("===================================================");
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity resEntity = response.getEntity();
    String[] result = new String[2];
    String status = response.getStatusLine().toString();
    result[0] = (status.indexOf("200") >= 0) ? "200" : status;
    System.out.println("===================================================");
    System.out.println("status from request " + result[0]);
    System.out.println("===================================================");
    if (resEntity != null) {
        result[1] = EntityUtils.toString(resEntity);
        EntityUtils.consume(resEntity);
    }
    httpclient.getConnectionManager().shutdown();
    return result;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) ContentBody(org.apache.http.entity.mime.content.ContentBody) HttpEntity(org.apache.http.HttpEntity) StringBody(org.apache.http.entity.mime.content.StringBody) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) ProgressMultiPartEntity(com.bixly.pastevid.util.view.ProgressMultiPartEntity) HttpResponse(org.apache.http.HttpResponse) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Example 2 with ContentBody

use of org.apache.http.entity.mime.content.ContentBody in project screenbird by adamhub.

the class FileUtil method postFile.

/**
     * Uploads a file via a POST request to a URL.
     * @param fileLocation  path to the file that will be uploaded
     * @param title         title of the video
     * @param slug          slug of the video 
     * @param description   description of the video
     * @param video_type    format of the video (mp4, mpg, avi, etc.)
     * @param checksum      checksum of the file that will be uploaded
     * @param is_public     publicity settings of the file
     * @param pbUpload      progress bar for the upload
     * @param fileSize      the length, in bytes, of the file to be uploaded
     * @param csrf_token    csrf token for the POST request
     * @param user_id       Screenbird user id of the uploader
     * @param channel_id    Screenbird channel id to where the video will be uploaded to
     * @param an_tok        anonymous token identifier if the uploader is not logged in to Screenbird
     * @param listener      listener for the upload
     * @param upload_url    URL where the POST request will be sent to.
     * @return
     * @throws IOException 
     */
public static String[] postFile(String fileLocation, String title, String slug, String description, String video_type, String checksum, Boolean is_public, final JProgressBar pbUpload, long fileSize, String csrf_token, String user_id, String channel_id, String an_tok, ProgressBarUploadProgressListener listener, String upload_url) throws IOException {
    HttpClient httpclient = new DefaultHttpClient();
    httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    HttpPost httppost = new HttpPost(upload_url);
    File file = new File(fileLocation);
    ProgressMultiPartEntity mpEntity = new ProgressMultiPartEntity(listener);
    ContentBody cbFile = new FileBody(file, "video/quicktime");
    mpEntity.addPart("videoupload", cbFile);
    ContentBody cbToken = new StringBody(title);
    mpEntity.addPart("csrfmiddlewaretoken", cbToken);
    ContentBody cbUser_id = new StringBody(user_id);
    mpEntity.addPart("user_id", cbUser_id);
    ContentBody cbChannel_id = new StringBody(channel_id);
    mpEntity.addPart("channel_id", cbChannel_id);
    ContentBody cbAnonym_id = new StringBody(an_tok);
    mpEntity.addPart("an_tok", cbAnonym_id);
    ContentBody cbTitle = new StringBody(title);
    mpEntity.addPart("title", cbTitle);
    ContentBody cbSlug = new StringBody(slug);
    mpEntity.addPart("slug", cbSlug);
    ContentBody cbPublic = new StringBody(is_public ? "1" : "0");
    mpEntity.addPart("is_public", cbPublic);
    ContentBody cbDescription = new StringBody(description);
    mpEntity.addPart("description", cbDescription);
    ContentBody cbChecksum = new StringBody(checksum);
    mpEntity.addPart("checksum", cbChecksum);
    ContentBody cbType = new StringBody(video_type);
    mpEntity.addPart("video_type", cbType);
    httppost.setEntity(mpEntity);
    log("===================================================");
    log("executing request " + httppost.getRequestLine());
    log("===================================================");
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity resEntity = response.getEntity();
    String[] result = new String[2];
    String status = response.getStatusLine().toString();
    result[0] = (status.indexOf("200") >= 0) ? "200" : status;
    log("===================================================");
    log("response " + response.toString());
    log("===================================================");
    if (resEntity != null) {
        result[1] = EntityUtils.toString(resEntity);
        EntityUtils.consume(resEntity);
    }
    httpclient.getConnectionManager().shutdown();
    return result;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) ContentBody(org.apache.http.entity.mime.content.ContentBody) FileBody(org.apache.http.entity.mime.content.FileBody) HttpEntity(org.apache.http.HttpEntity) StringBody(org.apache.http.entity.mime.content.StringBody) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) ProgressMultiPartEntity(com.bixly.pastevid.screencap.components.progressbar.ProgressMultiPartEntity) HttpResponse(org.apache.http.HttpResponse) File(java.io.File) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Example 3 with ContentBody

use of org.apache.http.entity.mime.content.ContentBody in project iaf by ibissource.

the class MultipartForm method getTotalLength.

/**
 * Determines the total length of the multipart content (content length of
 * individual parts plus that of extra elements required to delimit the parts
 * from one another). If any of the @{link BodyPart}s contained in this object
 * is of a streaming entity of unknown length the total length is also unknown.
 * <p>
 * This method buffers only a small amount of data in order to determine the
 * total length of the entire entity. The content of individual parts is not
 * buffered.
 * </p>
 *
 * @return total length of the multipart entity if known, {@code -1} otherwise.
 */
public long getTotalLength() {
    long contentLen = 0;
    for (final FormBodyPart part : getBodyParts()) {
        final ContentBody body = part.getBody();
        final long len = body.getContentLength();
        if (len >= 0) {
            contentLen += len;
        } else {
            return -1;
        }
    }
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        doWriteTo(out, false);
        final byte[] extra = out.toByteArray();
        return contentLen + extra.length;
    } catch (final IOException ex) {
        // Should never happen
        return -1;
    }
}
Also used : FormBodyPart(org.apache.http.entity.mime.FormBodyPart) ContentBody(org.apache.http.entity.mime.content.ContentBody) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 4 with ContentBody

use of org.apache.http.entity.mime.content.ContentBody in project opencast by opencast.

the class WorkingFileRepositoryRemoteImpl method putInCollection.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.workingfilerepository.api.WorkingFileRepository#putInCollection(java.lang.String,
 *      java.lang.String, java.io.InputStream)
 */
@Override
public URI putInCollection(String collectionId, String fileName, InputStream in) {
    String url = UrlSupport.concat(new String[] { COLLECTION_PATH_PREFIX, collectionId });
    HttpPost post = new HttpPost(url);
    MultipartEntity entity = new MultipartEntity();
    ContentBody body = new InputStreamBody(in, fileName);
    entity.addPart("file", body);
    post.setEntity(entity);
    HttpResponse response = getResponse(post);
    try {
        if (response != null) {
            String content = EntityUtils.toString(response.getEntity());
            return new URI(content);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        closeConnection(response);
    }
    throw new RuntimeException("Unable to put file in collection");
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) ContentBody(org.apache.http.entity.mime.content.ContentBody) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) InputStreamBody(org.apache.http.entity.mime.content.InputStreamBody) HttpResponse(org.apache.http.HttpResponse) URI(java.net.URI) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException)

Example 5 with ContentBody

use of org.apache.http.entity.mime.content.ContentBody in project opencast by opencast.

the class WorkingFileRepositoryRemoteImpl method put.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.workingfilerepository.api.WorkingFileRepository#put(java.lang.String, java.lang.String,
 *      java.lang.String, java.io.InputStream)
 */
@Override
public URI put(String mediaPackageID, String mediaPackageElementID, String filename, InputStream in) {
    String url = UrlSupport.concat(new String[] { MEDIAPACKAGE_PATH_PREFIX, mediaPackageID, mediaPackageElementID });
    HttpPost post = new HttpPost(url);
    MultipartEntity entity = new MultipartEntity();
    ContentBody body = new InputStreamBody(in, filename);
    entity.addPart("file", body);
    post.setEntity(entity);
    HttpResponse response = getResponse(post);
    try {
        if (response != null) {
            String content = EntityUtils.toString(response.getEntity());
            return new URI(content);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        closeConnection(response);
    }
    throw new RuntimeException("Unable to put file");
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) ContentBody(org.apache.http.entity.mime.content.ContentBody) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) InputStreamBody(org.apache.http.entity.mime.content.InputStreamBody) HttpResponse(org.apache.http.HttpResponse) URI(java.net.URI) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException)

Aggregations

ContentBody (org.apache.http.entity.mime.content.ContentBody)10 HttpPost (org.apache.http.client.methods.HttpPost)7 HttpResponse (org.apache.http.HttpResponse)6 IOException (java.io.IOException)5 MultipartEntityBuilder (org.apache.http.entity.mime.MultipartEntityBuilder)4 StringBody (org.apache.http.entity.mime.content.StringBody)4 InputStream (java.io.InputStream)3 File (java.io.File)2 URI (java.net.URI)2 Charset (java.nio.charset.Charset)2 ArrayList (java.util.ArrayList)2 Iterator (java.util.Iterator)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Entry (java.util.Map.Entry)2 HttpEntity (org.apache.http.HttpEntity)2 HttpEntityEnclosingRequest (org.apache.http.HttpEntityEnclosingRequest)2 ClientProtocolException (org.apache.http.client.ClientProtocolException)2 HttpClient (org.apache.http.client.HttpClient)2 HttpHead (org.apache.http.client.methods.HttpHead)2