Search in sources :

Example 1 with SerializableEntity

use of org.apache.http.entity.SerializableEntity in project RoboZombie by sahan.

the class Entities method resolve.

/**
	  * <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>
	  * 	<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);
    }
}
Also used : FileEntity(org.apache.http.entity.FileEntity) Serializable(java.io.Serializable) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) HttpEntity(org.apache.http.HttpEntity) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) InputStream(java.io.InputStream) SerializableEntity(org.apache.http.entity.SerializableEntity) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) StringEntity(org.apache.http.entity.StringEntity) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) File(java.io.File)

Example 2 with SerializableEntity

use of org.apache.http.entity.SerializableEntity in project RoboZombie by sahan.

the class RequestParamEndpointTest method testSerializableEntity.

/**
	 * <p>Test for a {@link Request} with a {@link Serializable} entity.</p>
	 * 
	 * @since 1.3.0
	 */
@Test
public final void testSerializableEntity() throws ParseException, IOException {
    Robolectric.getFakeHttpLayer().interceptHttpRequests(false);
    String subpath = "/serializableentity";
    User entity = new User(1L, "Eren", "Yeager", 15, false);
    SerializableEntity se = new SerializableEntity(entity, true);
    stubFor(put(urlEqualTo(subpath)).willReturn(aResponse().withStatus(200)));
    requestEndpoint.serializableEntity(entity);
    verify(putRequestedFor(urlEqualTo(subpath)).withRequestBody(equalTo(EntityUtils.toString(se))));
}
Also used : User(com.lonepulse.robozombie.model.User) SerializableEntity(org.apache.http.entity.SerializableEntity) Test(org.junit.Test)

Example 3 with SerializableEntity

use of org.apache.http.entity.SerializableEntity in project RoboZombie by sahan.

the class SerializerEndpointTest method testDetachSerializer.

/**
	 * <p>Test for detachment of the inherited serializer.</p>
	 *
	 * @since 1.3.0
	 */
@Test
public final void testDetachSerializer() throws ParseException, IOException {
    Robolectric.getFakeHttpLayer().interceptHttpRequests(false);
    String subpath = "/detach";
    User user = new User(1, "Roy", "Mustang", 30, false);
    stubFor(put(urlEqualTo(subpath)).willReturn(aResponse().withStatus(200)));
    serializerEndpoint.detachSerializer(user);
    verify(putRequestedFor(urlEqualTo(subpath)).withRequestBody(equalTo(EntityUtils.toString(new SerializableEntity(user, true)))));
}
Also used : User(com.lonepulse.robozombie.model.User) SerializableEntity(org.apache.http.entity.SerializableEntity) Test(org.junit.Test)

Aggregations

SerializableEntity (org.apache.http.entity.SerializableEntity)3 User (com.lonepulse.robozombie.model.User)2 Test (org.junit.Test)2 File (java.io.File)1 InputStream (java.io.InputStream)1 Serializable (java.io.Serializable)1 HttpEntity (org.apache.http.HttpEntity)1 BasicHttpEntity (org.apache.http.entity.BasicHttpEntity)1 BufferedHttpEntity (org.apache.http.entity.BufferedHttpEntity)1 ByteArrayEntity (org.apache.http.entity.ByteArrayEntity)1 FileEntity (org.apache.http.entity.FileEntity)1 StringEntity (org.apache.http.entity.StringEntity)1