Example usage for org.apache.http.protocol HTTP EXPECT_DIRECTIVE

List of usage examples for org.apache.http.protocol HTTP EXPECT_DIRECTIVE

Introduction

In this page you can find the example usage for org.apache.http.protocol HTTP EXPECT_DIRECTIVE.

Prototype

String EXPECT_DIRECTIVE

To view the source code for org.apache.http.protocol HTTP EXPECT_DIRECTIVE.

Click Source Link

Usage

From source file:org.hardisonbrewing.s3j.FileSyncer.java

public void put(String path, InputStream inputStream, long length) throws Exception {

    String url = getUrl(path);//from  w  ww  .j  av  a  2 s.co m

    HttpPut httpRequest = new HttpPut(url);
    httpRequest.addHeader(HTTP.EXPECT_DIRECTIVE, HTTP.EXPECT_CONTINUE);
    addHeaders(httpRequest, path);

    System.out.println("Uploading: " + url);
    HttpUtil.printHeaders(httpRequest);

    if (md5Digest == null) {
        md5Digest = MessageDigest.getInstance("MD5");
    } else {
        md5Digest.reset();
    }

    HttpResponse httpResponse;
    HttpEntity httpEntity = null;

    try {
        inputStream = new ProgressInputStream(inputStream, length);
        inputStream = new DigestInputStream(inputStream, md5Digest);
        httpRequest.setEntity(new InputStreamEntity(inputStream, length));
        httpResponse = httpClient.execute(httpRequest);
        httpEntity = httpResponse.getEntity();
    } finally {
        EntityUtils.consume(httpEntity);
    }

    System.out.println("  Response Headers");
    HttpUtil.printHeaders(httpResponse);

    HttpUtil.validateResponseCode(httpResponse);

    byte[] digest = md5Digest.digest();
    String digestHex = Hex.encodeHexString(digest);

    String etag = getHeaderValue(httpResponse, "ETag");

    if (!etag.equals(digestHex)) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("Uploaded digest (");
        stringBuffer.append(digestHex);
        stringBuffer.append(") does not match ETag (");
        stringBuffer.append(etag);
        stringBuffer.append(").");
        throw new IOException(stringBuffer.toString());
    }
}

From source file:org.opendatakit.http.conn.GaeManagedClientConnection.java

@Override
public void sendRequestHeader(HttpRequest request) throws HttpException, IOException {
    if (reusable && !broken) {
        throw new IllegalStateException("Connection has not yet been opened");
    }/*ww  w. j  a  v  a2s.  c om*/
    this.request = request;
    if (request instanceof HttpEntityEnclosingRequest) {
        HttpEntityEnclosingRequest req = (HttpEntityEnclosingRequest) request;
        expectContinueHeaders = req.getHeaders(HTTP.EXPECT_DIRECTIVE);
        req.removeHeaders(HTTP.EXPECT_DIRECTIVE);
    }
}

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 w  w  w  . j  av  a  2 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.googlecode.sardine.impl.SardineImpl.java

/**
 * Upload the entity using <code>PUT</code>
 * //from w  w w.ja va 2s  .  c om
 * @param url
 *            Resource
 * @param entity
 *            The entity to read from
 * @param contentType
 *            Content Type header
 * @param expectContinue
 *            Add <code>Expect: continue</code> header
 */
public void put(String url, HttpEntity entity, String contentType, boolean expectContinue) throws IOException {
    Map<String, String> headers = new HashMap<String, String>();
    if (contentType != null) {
        headers.put("Content-Type", contentType);
    }
    if (expectContinue) {
        headers.put(HTTP.EXPECT_DIRECTIVE, HTTP.EXPECT_CONTINUE);
    }
    this.put(url, entity, headers);
}

From source file:de.aflx.sardine.impl.SardineImpl.java

/**
 * Upload the entity using <code>PUT</code>
 * /* w  ww . jav  a2s .c  om*/
 * @param url
 *            Resource
 * @param entity
 *            The entity to read from
 * @param headers
 *            Headers to add to request
 */
public void put(String url, HttpEntity entity, Map<String, String> headers) throws IOException {

    HttpPut put = new HttpPut(url);
    _currentRequest = put;
    _isAborted = false;

    put.setEntity(entity);
    for (String header : headers.keySet()) {
        put.addHeader(header, headers.get(header));
    }
    if (!put.containsHeader("Content-Type")) {
        put.addHeader("Content-Type", HTTP.DEFAULT_CONTENT_TYPE);
    }
    try {
        this.execute(put, new VoidResponseHandler());
    } catch (HttpResponseException e) {
        if (e.getStatusCode() == HttpStatus.SC_EXPECTATION_FAILED) {
            // Retry with the Expect header removed
            put.removeHeaders(HTTP.EXPECT_DIRECTIVE);
            if (entity.isRepeatable()) {
                this.execute(put, new VoidResponseHandler());
                return;
            }
        }

        throw e;
    }
}

From source file:com.googlecode.sardine.impl.SardineImpl.java

/**
 * Upload the entity using <code>PUT</code>
 * //  w ww . j  a v  a2  s.c  o  m
 * @param url
 *            Resource
 * @param entity
 *            The entity to read from
 * @param headers
 *            Headers to add to request
 */
public void put(String url, HttpEntity entity, Map<String, String> headers) throws IOException {
    HttpPut put = new HttpPut(url);
    put.setEntity(entity);
    for (String header : headers.keySet()) {
        put.addHeader(header, headers.get(header));
    }
    if (!put.containsHeader("Content-Type")) {
        put.addHeader("Content-Type", HTTP.DEFAULT_CONTENT_TYPE);
    }
    try {
        this.execute(put, new VoidResponseHandler());
    } catch (HttpResponseException e) {
        if (e.getStatusCode() == HttpStatus.SC_EXPECTATION_FAILED) {
            // Retry with the Expect header removed
            put.removeHeaders(HTTP.EXPECT_DIRECTIVE);
            if (entity.isRepeatable()) {
                this.execute(put, new VoidResponseHandler());
                return;
            }
        }
        throw e;
    }
}

From source file:com.github.sardine.impl.SardineImpl.java

/**
 * Upload the entity using <code>PUT</code>
 *
 * @param url            Resource/*from www . j a va2s  . co  m*/
 * @param entity         The entity to read from
 * @param contentType    Content Type header
 * @param expectContinue Add <code>Expect: continue</code> header
 */
public void put(String url, HttpEntity entity, String contentType, boolean expectContinue) throws IOException {
    Map<String, String> headers = new HashMap<String, String>();
    if (contentType != null) {
        headers.put(HttpHeaders.CONTENT_TYPE, contentType);
    }
    if (expectContinue) {
        headers.put(HTTP.EXPECT_DIRECTIVE, HTTP.EXPECT_CONTINUE);
    }
    this.put(url, entity, headers);
}

From source file:com.github.sardine.impl.SardineImpl.java

public <T> T put(String url, HttpEntity entity, Map<String, String> headers, ResponseHandler<T> handler)
        throws IOException {
    HttpPut put = new HttpPut(url);
    put.setEntity(entity);//  ww  w . j a va2 s  .  com
    for (String header : headers.keySet()) {
        put.addHeader(header, headers.get(header));
    }
    if (entity.getContentType() == null && !put.containsHeader(HttpHeaders.CONTENT_TYPE)) {
        put.addHeader(HttpHeaders.CONTENT_TYPE, HTTP.DEF_CONTENT_CHARSET.name());
    }
    try {
        return this.execute(put, handler);
    } catch (HttpResponseException e) {
        if (e.getStatusCode() == HttpStatus.SC_EXPECTATION_FAILED) {
            // Retry with the Expect header removed
            put.removeHeaders(HTTP.EXPECT_DIRECTIVE);
            if (entity.isRepeatable()) {
                return this.execute(put, handler);
            }
        }
        throw e;
    }
}