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.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>/*  w  ww .  ja v  a 2  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.FedoraTransformIT.java

@Test
public void testLdpathWithProgramBody() throws Exception {

    final Session session = repo.login();
    objectService.createObject(session, "/ldpathTestObject");
    session.save();//from w  w w  . j  av a 2  s.  c  o m
    session.logout();

    HttpPost postLdpathProgramRequest = new HttpPost(serverAddress + "/ldpathTestObject/fcr:transform");
    BasicHttpEntity e = new BasicHttpEntity();

    String s = "id      = . :: xsd:string ;\n";

    e.setContent(new ByteArrayInputStream(s.getBytes()));

    postLdpathProgramRequest.setEntity(e);
    postLdpathProgramRequest.setHeader("Content-Type", LDPathTransform.APPLICATION_RDF_LDPATH);
    HttpResponse response = client.execute(postLdpathProgramRequest);
    assertEquals(200, response.getStatusLine().getStatusCode());
    String content = EntityUtils.toString(response.getEntity());
    logger.debug("Retrieved ldpath feed:\n" + content);

    JsonFactory jsonFactory = new JsonFactory();

    ObjectMapper mapper = new ObjectMapper();
    final JsonParser jsonParser = jsonFactory.createJsonParser(content);

    JsonNode rootNode = mapper.readTree(jsonParser);

    assertEquals(serverAddress + "/ldpathTestObject", rootNode.get(0).get("id").getElements().next().asText());

}

From source file:com.solution.backend.LoginServiceTest.java

private HttpResponse makeHttpResponse(int status, String body) {
    HttpResponse response = new BasicHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, status, null));
    BasicHttpEntity e = new BasicHttpEntity();
    e.setContent(new ByteArrayInputStream(body.getBytes()));
    response.setEntity(e);//from w w w .  j ava 2s .  co m
    return response;
}

From source file:org.apache.hadoop.gateway.dispatch.CappedBufferHttpEntityTest.java

@Test
public void testS__C1_FC_IB() throws IOException {
    String data = "0123456789";
    BasicHttpEntity basic;/*  w w  w. j  av  a 2 s. co m*/
    CappedBufferHttpEntity replay;

    basic = new BasicHttpEntity();
    basic.setContent(new ByteArrayInputStream(data.getBytes("UTF-8")));
    replay = new CappedBufferHttpEntity(basic, 20);

    String output;

    output = byteRead(replay.getContent(), -1);
    assertThat(output, is(data));
}

From source file:org.apache.hadoop.gateway.dispatch.PartiallyRepeatableHttpEntityTest.java

@Test
public void testS__C1_FC_IB() throws IOException {
    String data = "0123456789";
    BasicHttpEntity basic;//  ww  w.  j  a  va 2  s .c om
    PartiallyRepeatableHttpEntity replay;

    basic = new BasicHttpEntity();
    basic.setContent(new ByteArrayInputStream(data.getBytes("UTF-8")));
    replay = new PartiallyRepeatableHttpEntity(basic, 20);

    String output;

    output = byteRead(replay.getContent(), -1);
    assertThat(output, is(data));
}

From source file:com.nexmo.client.voice.SendDtmfMethodTest.java

@Test
public void parseResponse() throws Exception {
    HttpWrapper wrapper = new HttpWrapper();
    SendDtmfMethod methodUnderTest = new SendDtmfMethod(wrapper);

    HttpResponse stubResponse = new BasicHttpResponse(
            new BasicStatusLine(new ProtocolVersion("1.1", 1, 1), 200, "OK"));

    String json = "{\"message\": \"DTMF sent\",\"uuid\": \"ssf61863-4a51-ef6b-11e1-w6edebcf93bb\"}";
    InputStream jsonStream = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8));
    BasicHttpEntity entity = new BasicHttpEntity();
    entity.setContent(jsonStream);//  ww  w  .  jav  a 2s. co  m
    stubResponse.setEntity(entity);

    DtmfResponse response = methodUnderTest.parseResponse(stubResponse);
    assertEquals("DTMF sent", response.getMessage());
    assertEquals("ssf61863-4a51-ef6b-11e1-w6edebcf93bb", response.getUuid());
}

From source file:com.nexmo.client.voice.endpoints.ModifyCallMethodTest.java

@Test
public void parseResponse() throws Exception {
    HttpWrapper wrapper = new HttpWrapper();
    ModifyCallMethod methodUnderTest = new ModifyCallMethod(wrapper);

    HttpResponse stubResponse = new BasicHttpResponse(
            new BasicStatusLine(new ProtocolVersion("1.1", 1, 1), 200, "OK"));

    String json = "{\"message\":\"Received\"}";
    InputStream jsonStream = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8));
    BasicHttpEntity entity = new BasicHttpEntity();
    entity.setContent(jsonStream);//from   w  w w .  j  a  v a  2 s . c  om
    stubResponse.setEntity(entity);

    ModifyCallResponse response = methodUnderTest.parseResponse(stubResponse);
    assertEquals("Received", response.getMessage());
}

From source file:org.dataconservancy.dcs.ingest.client.impl.SwordClientManager.java

public DepositInfo deposit(InputStream content, String contentType, String packaging,
        Map<String, String> metadata) throws PackageException {

    if (metadata == null) {
        metadata = new HashMap<String, String>();
    }/*from   ww w .j  av  a 2s  .  c o  m*/

    HttpPost post = new HttpPost(collectionUrl);
    for (Map.Entry<String, String> val : metadata.entrySet()) {
        if (!val.getKey().equals(HttpHeaderUtil.CONTENT_LENGTH)) {
            post.setHeader(val.getKey(), val.getValue());
        }
    }

    post.setHeader(HttpHeaderUtil.CONTENT_TYPE, contentType);
    if (packaging != null) {
        post.setHeader(PACKAGING, packaging);
    }

    if (!metadata.containsKey(HttpHeaderUtil.CONTENT_LENGTH)) {
        BasicHttpEntity entity = new BasicHttpEntity();
        entity.setContent(content);
        post.setEntity(entity);
    } else {
        post.setEntity(new InputStreamEntity(content, new Long(metadata.get(HttpHeaderUtil.CONTENT_LENGTH))));
    }

    return execute(post);
}

From source file:org.apache.axis2.transport.http.server.AxisHttpResponseImpl.java

public void commit() throws IOException, HttpException {
    if (this.commited) {
        return;//from   w  ww  . j av  a 2 s .c  o  m
    }
    this.commited = true;

    this.context.setAttribute(ExecutionContext.HTTP_CONNECTION, this.conn);
    this.context.setAttribute(ExecutionContext.HTTP_RESPONSE, this.response);

    BasicHttpEntity entity = new BasicHttpEntity();
    entity.setChunked(true);
    entity.setContentType(this.contentType);

    this.response.setEntity(entity);

    this.httpproc.process(this.response, this.context);
    this.conn.sendResponse(this.response);
}

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 av a  2 s  . c om

    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;
}