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} --> returned as-is.</li>
* <li>{@code byte[]}, {@link Byte}[] --> {@link ByteArrayEntity}</li>
* <li>java.io.{@link File} --> {@link FileEntity}</li>
* <li>java.io.{@link InputStream} --> {@link BufferedHttpEntity}</li>
* <li>{@link CharSequence} --> {@link StringEntity}</li>
* <li>java.io.{@link Serializable} --> {@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);
}
}
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))));
}
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)))));
}
Aggregations