Example usage for org.apache.http.entity ByteArrayEntity ByteArrayEntity

List of usage examples for org.apache.http.entity ByteArrayEntity ByteArrayEntity

Introduction

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

Prototype

public ByteArrayEntity(byte[] bArr) 

Source Link

Usage

From source file:com.aliyun.android.oss.task.PutObjectTask.java

/**
 * HttpPut// w  w w .j a  v a  2  s .c om
 */
protected HttpUriRequest generateHttpRequest() {
    // ?Http
    String requestUri = this.getOSSEndPoint()
            + httpTool.generateCanonicalizedResource("/" + OSSHttpTool.encodeUri(objectKey));
    HttpPut httpPut = new HttpPut(requestUri);

    // HttpPut
    String resource = httpTool.generateCanonicalizedResource("/" + bucketName + "/" + objectKey);
    String dateStr = Helper.getGMTDate();
    String xossHeader = OSSHttpTool.generateCanonicalizedHeader(objectMetaData.getAttrs());
    String authorization = OSSHttpTool.generateAuthorization(accessId, accessKey, httpMethod.toString(), "",
            objectMetaData.getContentType(), dateStr, xossHeader, resource);

    httpPut.setHeader(AUTHORIZATION, authorization);
    httpPut.setHeader(DATE, dateStr);

    OSSHttpTool.addHttpRequestHeader(httpPut, CACHE_CONTROL, objectMetaData.getCacheControl());
    OSSHttpTool.addHttpRequestHeader(httpPut, CONTENT_DISPOSITION, objectMetaData.getContentDisposition());
    OSSHttpTool.addHttpRequestHeader(httpPut, CONTENT_ENCODING, objectMetaData.getContentEncoding());
    OSSHttpTool.addHttpRequestHeader(httpPut, CONTENT_TYPE, objectMetaData.getContentType());
    OSSHttpTool.addHttpRequestHeader(httpPut, EXPIRES, Helper.getGMTDate(objectMetaData.getExpirationTime()));

    // header
    for (Entry<String, String> entry : objectMetaData.getAttrs().entrySet()) {
        OSSHttpTool.addHttpRequestHeader(httpPut, entry.getKey(), entry.getValue());
    }

    if (objectMetaData.getContentType().equals(HttpContentType.DIR.toString())) {
        data = new byte[0];
        httpPut.setEntity(new ByteArrayEntity(this.data));
    } else {
        httpPut.setEntity(entity);
    }

    return httpPut;
}

From source file:com.wbtech.dao.NetworkUitlity.java

public static AbstractHttpEntity initEntity(byte[] paramArrayOfByte) {
    ByteArrayEntity localByteArrayEntity = null;
    ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
    GZIPOutputStream localGZIPOutputStream;
    if (paramArrayOfByte.length < paramleng) {
        localByteArrayEntity = new ByteArrayEntity(paramArrayOfByte);
    } else {//from   ww w. ja  va  2  s.  c om

        try {
            localGZIPOutputStream = new GZIPOutputStream(localByteArrayOutputStream);
            localGZIPOutputStream.write(paramArrayOfByte);
            localGZIPOutputStream.close();
            localByteArrayEntity = new ByteArrayEntity(localByteArrayOutputStream.toByteArray());
            localByteArrayEntity.setContentEncoding("gzip");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return localByteArrayEntity;
}

From source file:com.linkedin.multitenant.db.RocksdbDatabase.java

@Override
public DatabaseResult doInsert(Query q) {
    HttpPut put = new HttpPut(m_connStr + q.getKey());

    ByteArrayEntity bae = new ByteArrayEntity(q.getValue());
    bae.setContentType("octet-stream");
    put.setEntity(bae);/*from   w w  w.j  a va  2  s  .  co  m*/

    try {
        String responseBody = m_client.execute(put, m_handler);
        m_log.debug("Write response: " + responseBody);
        return DatabaseResult.OK;
    } catch (Exception e) {
        m_log.error("Error in executing doInsert", e);
        return DatabaseResult.FAIL;
    }
}

From source file:com.basho.riak.client.http.util.ClientHelper.java

/**
 * See//from w ww  .ja  va  2 s.  com
 * {@link RiakClient#setBucketSchema(String, com.basho.riak.client.http.RiakBucketInfo, RequestMeta)}
 */
public HttpResponse setBucketSchema(String bucket, JSONObject schema, RequestMeta meta) {
    if (schema == null) {
        schema = new JSONObject();
    }
    if (meta == null) {
        meta = new RequestMeta();
    }

    meta.setHeader(Constants.HDR_ACCEPT, Constants.CTYPE_JSON);

    HttpPut put = new HttpPut(ClientUtils.makeURI(config, bucket));
    ByteArrayEntity entity = new ByteArrayEntity(utf8StringToBytes(schema.toString()));
    entity.setContentType(Constants.CTYPE_JSON_UTF8);
    put.setEntity(entity);

    return executeMethod(bucket, null, put, meta);
}

From source file:org.greencheek.spring.rest.SSLCachingHttpComponentsClientHttpRequest.java

protected ClientHttpResponse executeInternal(HttpHeaders headers, byte[] bufferedOutput) throws IOException {
    for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
        String headerName = entry.getKey();
        if (!headerName.equalsIgnoreCase(HTTP.CONTENT_LEN)
                && !headerName.equalsIgnoreCase(HTTP.TRANSFER_ENCODING)) {
            for (String headerValue : entry.getValue()) {
                this.httpRequest.addHeader(headerName, headerValue);
            }//from w ww . j a va  2 s  .co  m
        }
    }
    if (this.httpRequest instanceof HttpEntityEnclosingRequest) {
        HttpEntityEnclosingRequest entityEnclosingRequest = (HttpEntityEnclosingRequest) this.httpRequest;
        HttpEntity requestEntity = new ByteArrayEntity(bufferedOutput);
        entityEnclosingRequest.setEntity(requestEntity);
    }

    setSSLPrinciple(this.httpContext);
    HttpResponse httpResponse = this.httpClient.execute(this.httpRequest, this.httpContext);
    saveSSLPrinciple(this.httpContext);
    return new SSLCachingHttpComponentsClientHttpResponse(httpResponse, this.httpContext);
}

From source file:com.mr.http.toolbox.MR_HttpClientStack.java

private static void setEntityIfNonEmptyBody(HttpEntityEnclosingRequestBase httpRequest, MR_Request<?> request)
        throws MR_AuthFailureError {
    byte[] body = request.getBody();
    if (body != null) {
        HttpEntity entity = new ByteArrayEntity(body);
        httpRequest.setEntity(entity);/*from  ww  w .j ava 2 s  .  c  om*/
    }
}

From source file:com.navercorp.volleyextensions.volleyer.multipart.stack.MultipartHttpClientStack.java

/**
 * Creates the appropriate subclass of HttpUriRequest for passed in request.
 *//*from   w  w  w.ja v a  2 s.c o m*/
@SuppressWarnings("deprecation")
/* protected */ static HttpUriRequest createHttpRequest(Request<?> request,
        Map<String, String> additionalHeaders) throws AuthFailureError {
    switch (request.getMethod()) {
    case Method.DEPRECATED_GET_OR_POST: {
        // This is the deprecated way that needs to be handled for backwards compatibility.
        // If the request's post body is null, then the assumption is that the request is
        // GET.  Otherwise, it is assumed that the request is a POST.
        byte[] postBody = request.getPostBody();
        if (postBody != null) {
            HttpPost postRequest = new HttpPost(request.getUrl());
            postRequest.addHeader(HEADER_CONTENT_TYPE, request.getPostBodyContentType());
            HttpEntity entity;
            entity = new ByteArrayEntity(postBody);
            postRequest.setEntity(entity);
            return postRequest;
        } else {
            return new HttpGet(request.getUrl());
        }
    }
    case Method.GET:
        return new HttpGet(request.getUrl());
    case Method.DELETE:
        return new HttpDelete(request.getUrl());
    case Method.POST: {
        HttpPost postRequest = new HttpPost(request.getUrl());
        setEntityIfNonEmptyMultipart(postRequest, request);
        return postRequest;
    }
    case Method.PUT: {
        HttpPut putRequest = new HttpPut(request.getUrl());
        setEntityIfNonEmptyMultipart(putRequest, request);
        return putRequest;
    }
    case Method.HEAD:
        return new HttpHead(request.getUrl());
    case Method.OPTIONS:
        return new HttpOptions(request.getUrl());
    case Method.TRACE:
        return new HttpTrace(request.getUrl());
    case Method.PATCH: {
        HttpPatch patchRequest = new HttpPatch(request.getUrl());
        patchRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
        setEntityIfNonEmptyMultipart(patchRequest, request);
        return patchRequest;
    }
    default:
        throw new IllegalStateException("Unknown request method.");
    }
}

From source file:YexTool.java

private static OpenRtb.BidResponse sendBidRequest(String dspServerAddress, OpenRtb.BidRequest bidRequest,
        String dataFormat) throws Exception {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpPost post = new HttpPost(dspServerAddress);
    switch (dataFormat) {
    case "pb":
        post.setEntity(new ByteArrayEntity(bidRequest.toByteArray()));
        post.setHeader("Content-Type", "application/x-protobuf");
        break;//from ww  w .j a va 2s  . c o m
    case "js":
        post.setEntity(new StringEntity(openRtbJsonFactory.newWriter().writeBidRequest(bidRequest)));
        post.setHeader("Content-Type", "application/json");
        break;
    case "jo":
        post.setEntity(new StringEntity(yexOpenRtbJsonFactory.newWriter().writeBidRequest(bidRequest)));
        post.setHeader("Content-Type", "application/json");
        break;
    }

    logger.info("Sending BidRequest to URL: " + dspServerAddress);
    HttpResponse response = httpclient.execute(post);

    OpenRtb.BidResponse bidResponse = null;
    if (response.getStatusLine().getStatusCode() == 200) {
        try {
            switch (dataFormat) {
            case "pb":
                bidResponse = OpenRtb.BidResponse.parseFrom(response.getEntity().getContent(),
                        extensionRegistry);
                break;
            case "js":
                bidResponse = openRtbJsonFactory.newReader().readBidResponse(response.getEntity().getContent());
                OpenRtb.BidResponse.Builder bidResponseBuilder = bidResponse.toBuilder().clearSeatbid();
                for (OpenRtb.BidResponse.SeatBid seatBid : bidResponse.getSeatbidList()) {
                    OpenRtb.BidResponse.SeatBid.Builder seatBidBuilder = seatBid.toBuilder().clearBid();
                    for (OpenRtb.BidResponse.SeatBid.Bid bid : seatBid.getBidList()) {
                        seatBidBuilder.addBid(bid.toBuilder().clearAdm().setAdmNative(
                                openRtbJsonFactory.newNativeReader().readNativeResponse(bid.getAdm())));
                    }
                    bidResponseBuilder.addSeatbid(seatBidBuilder);
                }
                bidResponse = bidResponseBuilder.build();
                break;
            case "jo":
                bidResponse = yexOpenRtbJsonFactory.newReader()
                        .readBidResponse(response.getEntity().getContent());
                break;
            }
        } catch (Exception e) {
            throw new Exception("Error while parse HttpResponse to BidResponse!", e);
        }
        bidResponse = electValidBidsInBidResponse(bidRequest, bidResponse);
    } else {
        logger.error("Error Response Code: " + response.getStatusLine().getStatusCode());
    }

    return bidResponse;
}

From source file:cn.dacas.emmclient.security.ssl.SslHttpStack.java

/**
 * Creates the appropriate subclass of HttpUriRequest for passed in request.
 *///  ww w . ja v  a  2s . com
@SuppressWarnings("deprecation")
/* protected */ static HttpUriRequest createHttpRequest(Request<?> request,
        Map<String, String> additionalHeaders) throws AuthFailureError {
    switch (request.getMethod()) {
    case Method.DEPRECATED_GET_OR_POST: {
        // This is the deprecated way that needs to be handled for backwards compatibility.
        // If the request's post body is null, then the assumption is that the request is
        // GET.  Otherwise, it is assumed that the request is a POST.
        byte[] postBody = request.getPostBody();
        if (postBody != null) {
            HttpPost postRequest = new HttpPost(request.getUrl());
            postRequest.addHeader(HEADER_CONTENT_TYPE, request.getPostBodyContentType());
            HttpEntity entity;
            entity = new ByteArrayEntity(postBody);
            postRequest.setEntity(entity);
            return postRequest;
        } else {
            return new HttpGet(request.getUrl());
        }
    }
    case Method.GET:
        return new HttpGet(request.getUrl());
    case Method.DELETE:
        return new HttpDelete(request.getUrl());
    case Method.POST: {
        HttpPost postRequest = new HttpPost(request.getUrl());
        postRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
        setMultiPartBody(postRequest, request);
        setEntityIfNonEmptyBody(postRequest, request);
        return postRequest;
    }
    case Method.PUT: {
        HttpPut putRequest = new HttpPut(request.getUrl());
        putRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
        setMultiPartBody(putRequest, request);
        setEntityIfNonEmptyBody(putRequest, request);
        return putRequest;
    }
    // Added in source code of Volley libray.
    //            case Method.PATCH: {
    //               HttpPatch patchRequest = new HttpPatch(request.getUrl());
    //               patchRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
    //                setEntityIfNonEmptyBody(patchRequest, request);
    //                return patchRequest;
    //            }
    default:
        throw new IllegalStateException("Unknown request method.");
    }
}