Example usage for org.apache.http.client.fluent Request bodyStream

List of usage examples for org.apache.http.client.fluent Request bodyStream

Introduction

In this page you can find the example usage for org.apache.http.client.fluent Request bodyStream.

Prototype

public Request bodyStream(final InputStream instream) 

Source Link

Usage

From source file:org.eclipse.userstorage.internal.Session.java

public boolean updateBlob(String applicationToken, String key, final Map<String, String> properties,
        final InputStream in, ICredentialsProvider credentialsProvider) throws IOException, ConflictException {
    URI uri = StringUtil.newURI(service.getServiceURI(), "api/blob/" + applicationToken + "/" + key);

    return new RequestTemplate<Boolean>(uri) {
        @Override/* w  w w.java  2 s.  c o  m*/
        protected Request prepareRequest() throws IOException {
            Request request = configureRequest(Request.Put(uri), uri);

            String eTag = properties.get(Blob.ETAG);

            if (DEBUG) {
                System.out.println("Updating etag = " + eTag);
            }

            if (!StringUtil.isEmpty(eTag)) {
                request.setHeader(IF_MATCH, "\"" + eTag + "\"");
            }

            body = JSONUtil.build(Collections.singletonMap("value", in));
            request.bodyStream(body);
            return request;
        }

        @Override
        protected Boolean handleResponse(HttpResponse response, HttpEntity responseEntity) throws IOException {
            String eTag = getETag(response);

            int statusCode = getStatusCode("PUT", uri, response, OK, CREATED, CONFLICT);

            if (statusCode == CONFLICT) {
                StatusLine statusLine = response.getStatusLine();
                throw new ConflictException("PUT", uri, getProtocolVersion(statusLine),
                        statusLine.getReasonPhrase(), eTag);
            }

            if (eTag == null) {
                throw new ProtocolException("PUT", uri, getProtocolVersion(response.getStatusLine()),
                        BAD_RESPONSE, "Bad Response : No ETag");
            }

            if (DEBUG) {
                System.out.println("Updated etag = " + eTag);
            }

            properties.put(Blob.ETAG, eTag);
            return statusCode == CREATED;
        }
    }.send(credentialsProvider);
}