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

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

Introduction

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

Prototype

public void setContent(InputStream inputStream) 

Source Link

Usage

From source file:org.springframework.cloud.netflix.ribbon.apache.HttpClientUtils.java

/**
 * Creates an new {@link HttpEntity} by copying the {@link HttpEntity} from the {@link HttpResponse}.
 * This method will close the response after copying the entity.
 * @param response The response to create the {@link HttpEntity} from
 * @return A new {@link HttpEntity}/*from  w  ww  .  j  a v a 2  s .  c om*/
 * @throws IOException thrown if there is a problem closing the response.
 */
public static HttpEntity createEntity(HttpResponse response) throws IOException {
    ByteArrayInputStream is = new ByteArrayInputStream(EntityUtils.toByteArray(response.getEntity()));
    BasicHttpEntity entity = new BasicHttpEntity();
    entity.setContent(is);
    entity.setContentLength(response.getEntity().getContentLength());
    if (CloseableHttpResponse.class.isInstance(response)) {
        ((CloseableHttpResponse) response).close();
    }
    return entity;
}

From source file:com.aerofs.baseline.http.HttpUtils.java

public static BasicHttpEntity writeStringToEntity(String content) {
    BasicHttpEntity basic = new BasicHttpEntity();
    basic.setContent(new ByteArrayInputStream(content.getBytes(Charsets.UTF_8)));
    return basic;
}

From source file:org.fcrepo.auth.xacml.XACMLTestUtil.java

/**
 * Removes the policy link from an object.
 *
 * @param client//from ww w  .  ja  va  2 s.c o m
 * @param serverAddress
 * @param objectPath
 * @throws Exception
 */
public static void unlinkPolicies(final HttpClient client, final String serverAddress, final String objectPath)
        throws Exception {
    final String subjectURI = serverAddress + objectPath;
    final HttpPatch patch = new HttpPatch(subjectURI);
    // setAuth(patch, "fedoraAdmin");
    patch.addHeader("Content-Type", "application/sparql-update");
    final BasicHttpEntity e = new BasicHttpEntity();
    e.setContent(new ByteArrayInputStream(("DELETE { <" + subjectURI
            + "> <http://fedora.info/definitions/v4/authorization#policy> ?o } " + "WHERE { <" + subjectURI
            + "> <http://fedora.info/definitions/v4/authorization#policy> ?o }").getBytes()));
    patch.setEntity(e);
    LOGGER.debug("PATCH: {}", patch.getURI());
    final HttpResponse response = client.execute(patch);
    assertEquals(NO_CONTENT.getStatusCode(), response.getStatusLine().getStatusCode());
}

From source file:com.orange.retrytest.OkHttpStack.java

private static HttpEntity getEntity(Response response) throws IOException {
    BasicHttpEntity entity = new BasicHttpEntity();
    ResponseBody body = response.body();
    entity.setContent(body.byteStream());
    entity.setContentLength(body.contentLength());
    entity.setContentEncoding(response.header("Content-Encoding"));
    if (body.contentType() != null) {
        entity.setContentType(body.contentType().type());
    }//from w ww  .  ja va  2s . c  o  m
    return entity;
}

From source file:com.aix.city.comm.OkHttpStack.java

private static HttpEntity entityFromOkHttpResponse(Response r) throws IOException {
    BasicHttpEntity entity = new BasicHttpEntity();
    ResponseBody body = r.body();//w w w .  j a  va  2s.  c  o  m

    entity.setContent(body.byteStream());
    entity.setContentLength(body.contentLength());
    entity.setContentEncoding(r.header("Content-Encoding"));

    if (body.contentType() != null) {
        entity.setContentType(body.contentType().type());
    }
    return entity;
}

From source file:com.objectivetruth.uoitlibrarybooking.app.networking.OkHttp3Stack.java

private static HttpEntity entityFromOkHttpResponse(Response r) throws IOException {
    BasicHttpEntity entity = new BasicHttpEntity();
    ResponseBody body = r.body();//from   www .java 2s  .  c  o m
    entity.setContent(body.byteStream());
    entity.setContentLength(body.contentLength());
    entity.setContentEncoding(r.header("Content-Encoding"));

    if (body.contentType() != null) {
        entity.setContentType(body.contentType().type());
    }
    return entity;
}

From source file:com.phattn.vnexpressnews.io.OkHttpStack.java

private static HttpEntity entityFromOkHttpResponse(Response response) throws IOException {
    BasicHttpEntity entity = new BasicHttpEntity();
    ResponseBody body = response.body();

    entity.setContent(body.byteStream());
    entity.setContentLength(body.contentLength());
    entity.setContentEncoding(response.header("Content-Encoding"));

    if (body.contentType() != null) {
        entity.setContentType(body.contentType().type());
    }//from   www .  j ava2 s  .  c o  m
    return entity;
}

From source file:com.lonepulse.robozombie.util.Entities.java

/**
 * <p>Discovers which implementation of {@link HttpEntity} is suitable for wrapping the given object. 
 * This discovery proceeds in the following order by checking the runtime-type of the object:</p> 
 *
 * <ol>/* ww  w  .  j  ava2 s.  c o m*/
 *    <li>org.apache.http.{@link HttpEntity} --&gt; returned as-is.</li> 
 *    <li>{@code byte[]}, {@link Byte}[] --&gt; {@link ByteArrayEntity}</li> 
 *     <li>java.io.{@link File} --&gt; {@link FileEntity}</li>
 *    <li>java.io.{@link InputStream} --&gt; {@link BufferedHttpEntity}</li>
 *    <li>{@link CharSequence} --&gt; {@link StringEntity}</li>
 *    <li>java.io.{@link Serializable} --&gt; {@link SerializableEntity} (with an internal buffer)</li>
 * </ol>
 *
 * @param genericEntity
 *          a generic reference to an object whose concrete {@link HttpEntity} is to be resolved 
 * <br><br>
 * @return the resolved concrete {@link HttpEntity} implementation
 * <br><br>
 * @throws NullPointerException
 *          if the supplied generic type was {@code null}
 * <br><br>
 * @throws EntityResolutionFailedException
 *          if the given generic instance failed to be translated to an {@link HttpEntity} 
 * <br><br>
 * @since 1.3.0
 */
public static final HttpEntity resolve(Object genericEntity) {

    assertNotNull(genericEntity);

    try {

        if (genericEntity instanceof HttpEntity) {

            return (HttpEntity) genericEntity;
        } else if (byte[].class.isAssignableFrom(genericEntity.getClass())) {

            return new ByteArrayEntity((byte[]) genericEntity);
        } else if (Byte[].class.isAssignableFrom(genericEntity.getClass())) {

            Byte[] wrapperBytes = (Byte[]) genericEntity;
            byte[] primitiveBytes = new byte[wrapperBytes.length];

            for (int i = 0; i < wrapperBytes.length; i++) {

                primitiveBytes[i] = wrapperBytes[i].byteValue();
            }

            return new ByteArrayEntity(primitiveBytes);
        } else if (genericEntity instanceof File) {

            return new FileEntity((File) genericEntity, null);
        } else if (genericEntity instanceof InputStream) {

            BasicHttpEntity basicHttpEntity = new BasicHttpEntity();
            basicHttpEntity.setContent((InputStream) genericEntity);

            return new BufferedHttpEntity(basicHttpEntity);
        } else if (genericEntity instanceof CharSequence) {

            return new StringEntity(((CharSequence) genericEntity).toString());
        } else if (genericEntity instanceof Serializable) {

            return new SerializableEntity((Serializable) genericEntity, true);
        } else {

            throw new EntityResolutionFailedException(genericEntity);
        }
    } catch (Exception e) {

        throw (e instanceof EntityResolutionFailedException) ? (EntityResolutionFailedException) e
                : new EntityResolutionFailedException(genericEntity, e);
    }
}

From source file:org.fcrepo.integration.AbstractResourceIT.java

protected static void addMixin(final String pid, final String mixinUrl) throws IOException {
    final HttpPatch updateObjectGraphMethod = new HttpPatch(serverAddress + pid);
    updateObjectGraphMethod.addHeader(CONTENT_TYPE, "application/sparql-update");
    final BasicHttpEntity e = new BasicHttpEntity();

    e.setContent(new ByteArrayInputStream(
            ("INSERT DATA { <> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <" + mixinUrl + "> . } ")
                    .getBytes()));//  w  w  w.  j ava 2  s. c  o  m
    updateObjectGraphMethod.setEntity(e);
    final HttpResponse response = client.execute(updateObjectGraphMethod);
    assertEquals(NO_CONTENT.getStatusCode(), response.getStatusLine().getStatusCode());
}

From source file:com.yangcong345.android.phone.manager.OkHttpStack.java

/**
 * Initializes an {@link HttpEntity} from the given {@link HttpURLConnection}.
 *
 * @return an HttpEntity populated with data from <code>connection</code>.
 *///from  w ww  .  j  a  v a 2s  .  c  om
private static HttpEntity entityFromOkHttp(Response okResponse) {
    ResponseBody rb = okResponse.body();

    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream = rb.byteStream();
    entity.setContent(inputStream);
    entity.setContentLength(rb.contentLength());
    entity.setContentType(rb.contentType().type());
    return entity;
}