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

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

Introduction

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

Prototype

public BasicHttpEntity() 

Source Link

Usage

From source file:com.github.seratch.signedrequest4j.SignedRequestApacheHCImpl.java

@Override
public HttpResponse doRequest(String url, HttpMethod method, RequestBody body, String charset)
        throws IOException {

    HttpClient httpClient = new DefaultHttpClient();
    HttpUriRequest request = getRequest(method, url);
    httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectTimeoutMillis);
    httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, readTimeoutMillis);
    httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, USER_AGENT);
    httpClient.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, charset);

    for (String name : headersToOverwrite.keySet()) {
        request.setHeader(name, headersToOverwrite.get(name));
    }//from   ww w.j  a  v  a2  s.com

    String oAuthNonce = String.valueOf(new SecureRandom().nextLong());
    Long oAuthTimestamp = System.currentTimeMillis() / 1000;
    String signature = getSignature(url, method, oAuthNonce, oAuthTimestamp);
    String authorizationHeader = getAuthorizationHeader(signature, oAuthNonce, oAuthTimestamp);
    request.setHeader("Authorization", authorizationHeader);

    if (method == HttpMethod.POST) {
        HttpPost postRequest = (HttpPost) request;
        BasicHttpEntity entity = new BasicHttpEntity();
        entity.setContent(new ByteArrayInputStream(body.getBody()));
        entity.setContentType(body.getContentType());
        postRequest.setEntity(entity);
    } else if (method == HttpMethod.PUT) {
        HttpPut putRequest = (HttpPut) request;
        BasicHttpEntity entity = new BasicHttpEntity();
        entity.setContent(new ByteArrayInputStream(body.getBody()));
        entity.setContentType(body.getContentType());
        putRequest.setEntity(entity);
    }

    org.apache.http.HttpResponse apacheHCResponse = httpClient.execute(request);
    if (apacheHCResponse.getStatusLine().getStatusCode() >= 400) {
        HttpResponse httpResponse = toReturnValue(apacheHCResponse, charset);
        throw new HttpException(apacheHCResponse.getStatusLine().getReasonPhrase(), httpResponse);
    }
    return toReturnValue(apacheHCResponse, charset);
}

From source file:net.kungfoo.grizzly.proxy.impl.ProxyAdapter.java

private static HttpRequest convert(String method, String uri, Request request) {
    HttpRequest req;/*from  w  ww.  ja v  a 2  s .  co  m*/
    final int len = request.getContentLength();
    if (len > 0) {
        req = new BasicHttpEntityEnclosingRequest(method, uri);
        final BasicHttpEntity httpEntity = new BasicHttpEntity();
        httpEntity.setContentLength(len);
        //      httpEntity.setContent(((InternalInputBuffer) request.getInputBuffer()).getInputStream());
        ((BasicHttpEntityEnclosingRequest) req).setEntity(httpEntity);
    } else {
        req = new BasicHttpRequest(method, uri);
    }
    final MimeHeaders mimeHeaders = request.getMimeHeaders();
    final Enumeration names = mimeHeaders.names();
    while (names.hasMoreElements()) {
        String name = (String) names.nextElement();
        req.addHeader(name, mimeHeaders.getHeader(name));
    }
    return req;
}

From source file:com.hackerati.android.user_sdk.volley.HHurlStack.java

/**
 * Initializes an {@link HttpEntity} from the given
 * {@link HttpURLConnection}./*from   www  . j a  va  2 s.com*/
 * 
 * @param connection
 * @return an HttpEntity populated with data from <code>connection</code>.
 */
private static HttpEntity entityFromConnection(final HttpURLConnection connection) {
    final BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream;
    try {
        inputStream = connection.getInputStream();
    } catch (final IOException ioe) {
        inputStream = connection.getErrorStream();
    }
    entity.setContent(inputStream);
    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());
    return entity;
}

From source file:org.zenoss.zep.rest.RestClient.java

private HttpEntity createJsonEntity(Message msg) throws IOException {
    BasicHttpEntity entity = new BasicHttpEntity();
    entity.setContentType(MediaType.APPLICATION_JSON);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    JsonFormat.writeTo(msg, baos);/*from w w w  .  j a v a2 s. c  om*/
    entity.setContent(new ByteArrayInputStream(baos.toByteArray()));
    return entity;
}

From source file:com.overture.questdroid.utility.ExtHttpClientStack.java

private HttpEntity convertEntityNewToOld(HttpEntity ent) throws IllegalStateException, IOException {

    BasicHttpEntity ret = new BasicHttpEntity();
    if (ent != null) {
        ret.setContent(ent.getContent());
        ret.setContentLength(ent.getContentLength());
        Header h;//from  w ww.  j a v  a 2s.  c o  m
        h = ent.getContentEncoding();
        if (h != null) {
            ret.setContentEncoding(convertheaderNewToOld(h));
        }
        h = ent.getContentType();
        if (h != null) {
            ret.setContentType(convertheaderNewToOld(h));
        }
    }

    return ret;
}

From source file:com.lehealth.common.sdk.CCPRestSDK.java

/**
 * ???//w w  w.jav a 2 s  .  c om
 * 
 * @param to
 *            ? ??????????100
 * @param templateId
 *            ? ?Id
 * @param datas
 *            ?? ???{??}
 * @return
 */
public JSONObject sendTemplateSMS(String to, String templateId, String[] datas, String domain, String appId,
        String sid, String token) {
    JSONObject errorInfo = this.accountValidate(domain, appId, sid, token);
    if (errorInfo != null) {
        return errorInfo;
    }

    if (StringUtils.isBlank(to) || StringUtils.isBlank(appId) || StringUtils.isBlank(templateId)) {
        throw new IllegalArgumentException("?:" + (StringUtils.isBlank(to) ? " ?? " : "")
                + (StringUtils.isBlank(templateId) ? " ?Id " : "") + "");
    }

    String timestamp = DateFormatUtils.format(new Date(), Constant.dateFormat_yyyymmddhhmmss);
    String acountName = sid;
    String sig = sid + token + timestamp;
    String acountType = "Accounts";
    String signature = DigestUtils.md5Hex(sig);
    String url = getBaseUrl(domain).append("/" + acountType + "/").append(acountName)
            .append("/" + TemplateSMS + "?sig=").append(signature).toString();

    String src = acountName + ":" + timestamp;
    String auth = Base64.encodeBase64String(src.getBytes());
    Map<String, String> header = new HashMap<String, String>();
    header.put("Accept", "application/json");
    header.put("Content-Type", "application/json;charset=utf-8");
    header.put("Authorization", auth);

    JSONObject json = new JSONObject();
    json.accumulate("appId", appId);
    json.accumulate("to", to);
    json.accumulate("templateId", templateId);
    if (datas != null) {
        JSONArray Jarray = new JSONArray();
        for (String s : datas) {
            Jarray.add(s);
        }
        json.accumulate("datas", Jarray);
    }
    String requestBody = json.toString();

    DefaultHttpClient httpclient = null;
    HttpPost postMethod = null;
    try {
        httpclient = new CcopHttpClient().registerSSL(domain, NumberUtils.toInt(SERVER_PORT), "TLS", "https");
        postMethod = new HttpPost(url);
        if (header != null && !header.isEmpty()) {
            for (Entry<String, String> e : header.entrySet()) {
                postMethod.setHeader(e.getKey(), e.getValue());
            }
        }
        postMethod.setConfig(PoolConnectionManager.requestConfig);
        BasicHttpEntity requestEntity = new BasicHttpEntity();
        requestEntity.setContent(new ByteArrayInputStream(requestBody.getBytes("UTF-8")));
        requestEntity.setContentLength(requestBody.getBytes("UTF-8").length);
        postMethod.setEntity(requestEntity);

        HttpResponse response = httpclient.execute(postMethod);

        HttpEntity entity = response.getEntity();
        if (entity != null) {
            String result = EntityUtils.toString(entity, "UTF-8");
            EntityUtils.consume(entity);
            return JSONObject.fromObject(result);
        }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (postMethod != null) {
            postMethod.releaseConnection();
        }
        if (httpclient != null) {
            httpclient.getConnectionManager().shutdown();
        }
    }
    return getMyError("999999", "");
}

From source file:com.nxt.zyl.data.volley.toolbox.HurlStack.java

/**
 * Initializes an {@link org.apache.http.HttpEntity} from the given {@link java.net.HttpURLConnection}.
 * @param connection/*from   w  ww.  j  a v  a2 s . com*/
 * @return an HttpEntity populated with data from <code>connection</code>.
 */
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream;
    try {
        inputStream = connection.getInputStream();
    } catch (IOException ioe) {
        inputStream = connection.getErrorStream();
    }
    entity.setContent(inputStream);
    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());

    return entity;
}

From source file:carleton150.edu.carleton.carleton150.CertificateManagement.ExtHttpClientStack.java

private org.apache.http.HttpEntity convertEntityNewToOld(HttpEntity ent)
        throws IllegalStateException, IOException {

    BasicHttpEntity ret = new BasicHttpEntity();
    if (ent != null) {
        ret.setContent(ent.getContent());
        ret.setContentLength(ent.getContentLength());
        Header h;/*w ww.  j  av  a  2  s.  c  o  m*/
        h = ent.getContentEncoding();
        if (h != null) {
            ret.setContentEncoding(convertheaderNewToOld(h));
        }
        h = ent.getContentType();
        if (h != null) {
            ret.setContentType(convertheaderNewToOld(h));
        }
    }

    return ret;
}

From source file:com.android.volley.toolbox.https.ExtHttpClientStack.java

private HttpEntity convertEntityNewToOld(HttpEntity ent) throws IllegalStateException, IOException {

    BasicHttpEntity ret = new BasicHttpEntity();
    if (ent != null) {
        ret.setContent(ent.getContent());
        ret.setContentLength(ent.getContentLength());
        Header h;//from   ww w  .j ava2  s  . co  m
        h = (Header) ent.getContentEncoding();
        if (h != null) {
            ret.setContentEncoding(convertheaderNewToOld(h));
        }
        h = ent.getContentType();
        if (h != null) {
            ret.setContentType(convertheaderNewToOld(h));
        }
    }

    return ret;
}

From source file:org.fiware.apps.repository.it.IntegrationTestHelper.java

public HttpResponse putResourceMeta(String resourceId, String resource, List<Header> headers)
        throws IOException {
    String finalURL = collectionServiceUrl + resourceId + ".meta";
    HttpPut request = new HttpPut(finalURL);

    //Add Headers
    for (Header header : headers) {
        request.setHeader(header);//from  w ww .  j  a v a2s  .  c  o m
    }

    //Add Entity
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream stream = new ByteArrayInputStream(resource.getBytes());

    entity.setContent(stream);
    request.setEntity(entity);

    HttpResponse response = client.execute(request);
    request.releaseConnection();
    return response;
}