Example usage for org.apache.http.entity InputStreamEntity setContentType

List of usage examples for org.apache.http.entity InputStreamEntity setContentType

Introduction

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

Prototype

public void setContentType(Header header) 

Source Link

Usage

From source file:com.funambol.platform.HttpConnectionAdapter.java

/**
 * Execute the http post operation with the given input stream to be read to
 * fetch body content.//from   w  w  w .ja va2  s. co  m
 *
 * @throws IOException if the output stream cannot be opened.
 */
public void execute(InputStream is, long length) throws IOException {

    Log.debug(TAG_LOG, "Opening url: " + url);
    Log.debug(TAG_LOG, "Opening with method " + requestMethod);

    String contentType = null;
    if (requestHeaders != null) {
        contentType = requestHeaders.get("Content-Type");
    }
    if (contentType == null) {
        contentType = "binary/octet-stream";
    }

    if (POST.equals(requestMethod)) {
        request = new HttpPost(url);
        if (is != null) {
            InputStreamEntity reqEntity = new InputStreamEntity(is, length);
            reqEntity.setContentType(contentType);
            if (chunkLength > 0) {
                reqEntity.setChunked(true);
            }
            ((HttpPost) request).setEntity(reqEntity);
        }
    } else if (PUT.equals(requestMethod)) {
        request = new HttpPut(url);
        if (is != null) {
            InputStreamEntity reqEntity = new InputStreamEntity(is, length);
            reqEntity.setContentType(contentType);
            if (chunkLength > 0) {
                reqEntity.setChunked(true);
            }
            ((HttpPost) request).setEntity(reqEntity);
        }
    } else {
        request = new HttpGet(url);
    }

    performRequest(request);
}

From source file:org.gitana.platform.client.support.RemoteImpl.java

@Override
public void upload(String uri, InputStream in, long length, String mimetype) throws Exception {
    InputStreamEntity entity = new InputStreamEntity(in, length);
    entity.setContentType(mimetype);

    String URL = buildURL(uri, false);
    HttpPost httpPost = new HttpPost(URL);
    httpPost.setEntity(entity);//from   ww w. ja  v a 2 s .c  o  m

    HttpResponse httpResponse = invoker.execute(httpPost);
    if (!HttpUtil.isOk(httpResponse)) {
        throw new RuntimeException("Upload failed: " + EntityUtils.toString(httpResponse.getEntity()));
    }

    // consume the response fully so that the client connection can be reused
    EntityUtils.consume(httpResponse.getEntity());
}

From source file:sit.web.client.HttpHelper.java

/**
 *
 *
 * @param method//  w ww. j  a va2s  .  c om
 * @param host
 * @param port
 * @param path
 * @param payload
 * @param contentType content type e.g. "application/json"
 * @param isHTTPS
 * @param unamePword64
 * @return
 * @throws MalformedURLException
 * @throws ProtocolException
 * @throws IOException
 * @throws URISyntaxException
 */
public HTTPResponse doApacheHTTPRequest(String method, String host, int port, String path, byte[] payload,
        String contentType, boolean isHTTPS, String unamePword64)
        throws MalformedURLException, ProtocolException, IOException, URISyntaxException {

    if (payload == null) { //make sure payload is initialized
        payload = new byte[0];
    }

    URI url = getURI(host, port, path, isHTTPS);

    HttpClient httpclient;
    if (isHTTPS) {
        httpclient = HTTPTrustHelper.getNewHttpClient(Charset.defaultCharset(), port);
    } else {
        httpclient = new DefaultHttpClient();
    }

    HTTPResponse result = null;
    HttpResponse response = null;

    if (method.equalsIgnoreCase(HttpConstants.HTTP_COMMAND_GET)) {
        HttpGet request = new HttpGet(url);
        request.setHeader("Authorization", "Basic " + unamePword64);
        response = httpclient.execute(request);
    } else if (method.equalsIgnoreCase(HttpConstants.HTTP_COMMAND_POST)) {
        HttpPost request = new HttpPost(url);
        request.setHeader("Authorization", "Basic " + unamePword64);
        InputStreamEntity reqEntity = new InputStreamEntity(new ByteArrayInputStream(payload), payload.length);
        request.setEntity(reqEntity);
        reqEntity.setContentType(contentType);
        response = httpclient.execute(request);
    } else if (method.equalsIgnoreCase(HttpConstants.HTTP_COMMAND_PUT)) {
        HttpPut request = new HttpPut(url);
        request.setHeader("Authorization", "Basic " + unamePword64);
        InputStreamEntity reqEntity = new InputStreamEntity(new ByteArrayInputStream(payload), payload.length);
        request.setEntity(reqEntity);
        reqEntity.setContentType(contentType);
        response = httpclient.execute(request);
    } else if (method.equalsIgnoreCase(HttpConstants.HTTP_COMMAND_DELETE)) {
        HttpDelete request = new HttpDelete(url);
        request.setHeader("Authorization", "Basic " + unamePword64);
        response = httpclient.execute(request);
    } else {
        throw new RuntimeException("HTTP method not supported! Method:" + method);
    }

    result = new HTTPResponse(path, payload, Charset.defaultCharset());
    HttpEntity responseEntity = response.getEntity();
    if (responseEntity != null) {
        result.reply = EntityUtils.toString(responseEntity);
    }
    result.code = response.getStatusLine().getStatusCode();
    result.message = response.getStatusLine().getReasonPhrase();

    httpclient.getConnectionManager().shutdown();
    return result;
}

From source file:com.semagia.cassa.client.GraphClient.java

private boolean _createGraph(final URI graphURI, final InputStream in, final MediaType mediaType)
        throws IOException {
    final HttpPut put = new HttpPut(getGraphURI(graphURI));
    final InputStreamEntity entity = new InputStreamEntity(in, -1);
    entity.setContentType(mediaType != null ? mediaType.toString() : null);
    put.setEntity(entity);/*  ww w.j a  v  a 2s  .c  o m*/
    final int status = getStatusCode(put);
    return status == 200 || status == 201 || status == 204;
}

From source file:com.semagia.cassa.client.GraphClient.java

private boolean _updateGraph(final URI graphURI, final InputStream in, final MediaType mediaType)
        throws IOException {
    final HttpPost post = new HttpPost(getGraphURI(graphURI));
    final InputStreamEntity entity = new InputStreamEntity(in, -1);
    entity.setContentType(mediaType != null ? mediaType.toString() : null);
    post.setEntity(entity);/*from w w  w .  j  a  v a  2  s  .  com*/
    final int status = getStatusCode(post);
    return status == 201 || status == 204;
}

From source file:org.lightcouch.CouchDbClientBase.java

/**
 * Performs a HTTP PUT request, saves an attachment.
 * @return {@link Response}//  w w  w .  j a  v  a 2  s . c  o m
 */
Response put(URI uri, InputStream instream, String contentType) {
    HttpResponse response = null;
    try {
        HttpPut httpPut = new HttpPut(uri);
        InputStreamEntity entity = new InputStreamEntity(instream, -1);
        entity.setContentType(contentType);
        httpPut.setEntity(entity);
        response = executeRequest(httpPut);
        return getResponse(response);
    } finally {
        close(response);
    }
}

From source file:com.semagia.cassa.client.GraphClient.java

private URI _createGraph(final InputStream in, final MediaType mediaType) throws IOException {
    final HttpPost request = new HttpPost(_endpoint);
    final InputStreamEntity entity = new InputStreamEntity(in, -1);
    entity.setContentType(mediaType != null ? mediaType.toString() : null);
    request.setEntity(entity);//from w  w w  .  j av  a2 s .c o  m
    final HttpResponse response = execute(request);
    URI graphURI = null;
    if (response.getStatusLine().getStatusCode() == 201) {
        final Header location = response.getFirstHeader("location");
        graphURI = location != null ? URI.create(location.getValue()) : null;
    }
    request.abort();
    return graphURI;
}

From source file:org.gitana.platform.client.support.RemoteImpl.java

@Override
public void upload(String uri, byte[] bytes, String mimetype) throws Exception {
    InputStreamEntity entity = new InputStreamEntity(new ByteArrayInputStream(bytes), bytes.length);
    entity.setContentType(mimetype);

    String URL = buildURL(uri, false);

    HttpPost httpPost = new HttpPost(URL);
    httpPost.setEntity(entity);//from  w w  w .  j av a2s . c  o m

    HttpResponse httpResponse = invoker.execute(httpPost);
    if (!HttpUtil.isOk(httpResponse)) {
        throw new RuntimeException("Upload failed: " + EntityUtils.toString(httpResponse.getEntity()));
    }

    // consume the response fully so that the client connection can be reused
    EntityUtils.consume(httpResponse.getEntity());
}

From source file:org.apache.abdera2.common.protocol.Session.java

/**
 * Sends the specified method request to the specified URI. This can be used to send extension HTTP methods to a
 * server (e.g. PATCH, LOCK, etc)//from w  w w  . j a  v  a2 s. c o m
 * 
 * @param method The HTTP method
 * @param uri The request URI
 * @param in An InputStream providing the payload of the request
 * @param options The Request Options
 */
public <T extends ClientResponse> T execute(String method, String uri, InputStream in, RequestOptions options) {
    if (options == null)
        options = getDefaultRequestOptions().get();
    InputStreamEntity re = new InputStreamEntity(in, -1);
    re.setContentType(options.getContentType().toString());
    return (T) wrap(execute(method, uri, re, options));
}

From source file:com.hoccer.http.AsyncHttpRequestWithBody.java

public void setBody(StreamableContent pStreamableData) throws Exception {

    final long streamLength = pStreamableData.getNewStreamLength();

    InputStreamEntity entity = new InputStreamEntity(
            new MonitoredInputStream(pStreamableData.openNewInputStream()) {

                @Override/* w ww  . jav  a  2 s  .  co  m*/
                public void onBytesRead(long totalNumBytesRead) {

                    double progress = (totalNumBytesRead / (double) streamLength) * 100;
                    setUploadProgress((int) progress);
                }

            }, pStreamableData.getNewStreamLength());

    getRequest().addHeader("Content-Disposition",
            " attachment; filename=\"" + pStreamableData.getFilename() + "\"");
    entity.setContentType(pStreamableData.getContentType());
    getRequest().setEntity(entity);
    getRequest().addHeader("Content-Type", pStreamableData.getContentType());
}