Search in sources :

Example 1 with Deserialize

use of com.lonepulse.robozombie.annotation.Deserialize in project RoboZombie by sahan.

the class EntityProcessor method process.

/**
	 * <p>Accepts the {@link InvocationContext} along with the {@link HttpResponse} and retrieves the 
	 * {@link HttpEntity} form the response. This is then converted to an instance of the required request 
	 * return type by consulting the @{@link Deserialize} metadata on the endpoint definition.</p>
	 * 
	 * <p>If the desired return type is {@link HttpResponse} or {@link HttpEntity} the response or entity 
	 * is simply returned without any further processing.</p>
	 * 
	 * <p><b>Note</b> that this processor returns {@code null} for successful responses with the status 
	 * codes {@code 205} or {@code 205}.</p>
	 * 
	 * @param context
	 * 			the {@link InvocationContext} which is used to discover deserializer metadata 
	 * <br><br>
	 * @param response
	 * 			the {@link HttpResponse} whose response content is deserialized to the desired output type
	 * <br><br>
	 * @return the deserialized response content which conforms to the expected type
	 * <br><br> 
	 * @throws ResponseProcessorException
	 * 			if deserializer instantiation or execution failed for the response entity
	 * <br><br>
	 * @since 1.3.0
	 */
@Override
protected Object process(InvocationContext context, HttpResponse response, Object content) {
    if (response.getEntity() == null) {
        return content;
    }
    HttpEntity entity = response.getEntity();
    Method request = context.getRequest();
    Class<?> responseType = request.getReturnType();
    try {
        if (successful(response) && !status(response, 204, 205)) {
            if (HttpResponse.class.isAssignableFrom(responseType)) {
                return response;
            }
            if (HttpEntity.class.isAssignableFrom(responseType)) {
                return response.getEntity();
            }
            boolean responseExpected = !(responseType.equals(void.class) || responseType.equals(Void.class));
            boolean handleAsync = async(context);
            if (handleAsync || responseExpected) {
                Class<?> endpoint = context.getEndpoint();
                AbstractDeserializer<?> deserializer = null;
                Deserialize metadata = (metadata = request.getAnnotation(Deserialize.class)) == null ? endpoint.getAnnotation(Deserialize.class) : metadata;
                if (metadata != null & !isDetached(context, Deserialize.class)) {
                    deserializer = (metadata.value() == ContentType.UNDEFINED) ? Deserializers.resolve(metadata.type()) : Deserializers.resolve(metadata.value());
                } else if (handleAsync || CharSequence.class.isAssignableFrom(responseType)) {
                    deserializer = Deserializers.resolve(ContentType.PLAIN);
                } else {
                    throw new DeserializerUndefinedException(endpoint, request);
                }
                return deserializer.run(context, response);
            }
        }
    } catch (Exception e) {
        throw (e instanceof ResponseProcessorException) ? (ResponseProcessorException) e : new ResponseProcessorException(getClass(), context, e);
    } finally {
        if (!(HttpResponse.class.isAssignableFrom(responseType) || HttpEntity.class.isAssignableFrom(responseType))) {
            EntityUtils.consumeQuietly(entity);
        }
    }
    return content;
}
Also used : HttpEntity(org.apache.http.HttpEntity) Deserialize(com.lonepulse.robozombie.annotation.Deserialize) HttpResponse(org.apache.http.HttpResponse) Method(java.lang.reflect.Method)

Aggregations

Deserialize (com.lonepulse.robozombie.annotation.Deserialize)1 Method (java.lang.reflect.Method)1 HttpEntity (org.apache.http.HttpEntity)1 HttpResponse (org.apache.http.HttpResponse)1