Search in sources :

Example 6 with InputStreamRequestHttpRetryHandler

use of com.zimbra.common.httpclient.InputStreamRequestHttpRetryHandler in project zm-mailbox by Zimbra.

the class HttpStoreManager method writeStreamToStore.

@Override
public String writeStreamToStore(InputStream in, long actualSize, Mailbox mbox) throws IOException, ServiceException {
    MessageDigest digest;
    try {
        digest = MessageDigest.getInstance("SHA-256");
    } catch (NoSuchAlgorithmException e) {
        throw ServiceException.FAILURE("SHA-256 digest not found", e);
    }
    ByteUtil.PositionInputStream pin = new ByteUtil.PositionInputStream(new DigestInputStream(in, digest));
    HttpClientBuilder clientBuilder = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
    clientBuilder.setRetryHandler(new InputStreamRequestHttpRetryHandler());
    HttpClient client = clientBuilder.build();
    HttpPost post = new HttpPost(getPostUrl(mbox));
    try {
        InputStreamEntity entity = new InputStreamEntity(pin, actualSize, ContentType.APPLICATION_OCTET_STREAM);
        post.setEntity(entity);
        HttpResponse httpResp = HttpClientUtil.executeMethod(client, post);
        int statusCode = httpResp.getStatusLine().getStatusCode();
        if (statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_CREATED || statusCode == HttpStatus.SC_NO_CONTENT) {
            return getLocator(post, ByteUtil.encodeFSSafeBase64(digest.digest()), pin.getPosition(), mbox, httpResp);
        } else {
            throw ServiceException.FAILURE("error POSTing blob: " + httpResp.getStatusLine().getReasonPhrase(), null);
        }
    } catch (HttpException e) {
        throw new IOException("error POSTing blob: " + e.getMessage());
    } finally {
        post.releaseConnection();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) ByteUtil(com.zimbra.common.util.ByteUtil) DigestInputStream(java.security.DigestInputStream) HttpResponse(org.apache.http.HttpResponse) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) IOException(java.io.IOException) InputStreamEntity(org.apache.http.entity.InputStreamEntity) InputStreamRequestHttpRetryHandler(com.zimbra.common.httpclient.InputStreamRequestHttpRetryHandler) HttpClient(org.apache.http.client.HttpClient) HttpException(org.apache.http.HttpException) MessageDigest(java.security.MessageDigest)

Aggregations

InputStreamRequestHttpRetryHandler (com.zimbra.common.httpclient.InputStreamRequestHttpRetryHandler)6 IOException (java.io.IOException)6 HttpException (org.apache.http.HttpException)6 HttpResponse (org.apache.http.HttpResponse)6 HttpClient (org.apache.http.client.HttpClient)6 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)6 HttpPost (org.apache.http.client.methods.HttpPost)5 SocketConfig (org.apache.http.config.SocketConfig)3 InputStreamEntity (org.apache.http.entity.InputStreamEntity)3 URI (java.net.URI)2 HttpEntity (org.apache.http.HttpEntity)2 MultipartEntityBuilder (org.apache.http.entity.mime.MultipartEntityBuilder)2 ServiceException (com.zimbra.common.service.ServiceException)1 ByteUtil (com.zimbra.common.util.ByteUtil)1 Pair (com.zimbra.common.util.Pair)1 AccountServiceException (com.zimbra.cs.account.AccountServiceException)1 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)1 Mountpoint (com.zimbra.cs.mailbox.Mountpoint)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1