Search in sources :

Example 1 with EntityResolutionFailedException

use of com.lonepulse.robozombie.util.EntityResolutionFailedException in project RoboZombie by sahan.

the class EntityProcessor method process.

/**
	 * <p>Accepts the {@link InvocationContext} of an {@link HttpEntityEnclosingRequest} and inserts 
	 * <b>the</b> request parameter which is annotated with @{@link Entity} into its body.</p>
	 * 
	 * <p><b>Note</b> that it makes no sense to scope multiple entities within the same entity enclosing 
	 * request (HTTP/1.1 <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html">RFC-2616</a>). 
	 * This processor fails for the following scenarios:</p>
	 * 
	 * <ul>
	 * 	<li><b>No entity</b> was found in the endpoint method definition.</li>
	 * 	<li><b>Multiple entities</b> were found in the endpoint method definition.</li>
	 * 	<li>The annotated entity <b>failed to be resolved</b> to a matching {@link HttpEntity}.</li>
	 * </ul>
	 * 
	 * <p>Parameter types are resolved to their {@link HttpEntity} as specified in 
	 * {@link Entities#resolve(Object)}. If an attached @{@link Serialize} is discovered, the entity 
	 * will be serialized using the specified serializer before translation to an {@link HttpEntity}.</p>
	 * 
	 * <p>See {@link AbstractRequestProcessor#process(InvocationContext, HttpRequestBase)}.</p>
	 *
	 * @param context
	 * 			the {@link InvocationContext} which is used to retrieve the entity
	 * <br><br>
	 * @param request
	 * 			an instance of {@link HttpEntityEnclosingRequestBase} which allows the inclusion of an 
	 * 			{@link HttpEntity} in its body
	 * <br><br>
 	 * @return the same instance of {@link HttpRequestBase} which was given for processing entities 
	 * <br><br>
	 * @throws RequestProcessorException
	 * 			if an {@link HttpEntityEnclosingRequestBase} was discovered and yet the entity failed to 
	 * 			be resolved and inserted into the request body
	 * <br><br>
	 * @since 1.3.0
	 */
//welcomes a ClassCastException on misuse of @Serialize(Custom.class)
@Override
//welcomes a ClassCastException on misuse of @Serialize(Custom.class)
@SuppressWarnings("unchecked")
protected HttpRequestBase process(InvocationContext context, HttpRequestBase request) {
    try {
        if (request instanceof HttpEntityEnclosingRequestBase) {
            List<Entry<Entity, Object>> entities = Metadata.onParams(Entity.class, context);
            if (entities.isEmpty()) {
                throw new MissingEntityException(context);
            }
            if (entities.size() > 1) {
                throw new MultipleEntityException(context);
            }
            Object entity = entities.get(0).getValue();
            Serialize metadata = (metadata = context.getRequest().getAnnotation(Serialize.class)) == null ? context.getEndpoint().getAnnotation(Serialize.class) : metadata;
            if (metadata != null && !isDetached(context, Serialize.class)) {
                //no restrictions on custom serializer types with @Serialize
                @SuppressWarnings("rawtypes") AbstractSerializer serializer = (metadata.value() == UNDEFINED) ? Serializers.resolve(metadata.type()) : Serializers.resolve(metadata.value());
                entity = serializer.run(context, entity);
            }
            HttpEntity httpEntity = Entities.resolve(entity);
            ((HttpEntityEnclosingRequestBase) request).setHeader(HttpHeaders.CONTENT_TYPE, ContentType.getOrDefault(httpEntity).getMimeType());
            ((HttpEntityEnclosingRequestBase) request).setEntity(httpEntity);
        }
    } catch (MissingEntityException mee) {
        if (!(request instanceof HttpPost)) {
            //allow leeway for POST requests
            StringBuilder errorContext = new StringBuilder("It is imperative that this request encloses an entity.").append(" Identify exactly one entity by annotating an argument with @").append(Entity.class.getSimpleName());
            throw new RequestProcessorException(errorContext.toString(), mee);
        }
    } catch (MultipleEntityException mee) {
        //violates HTTP 1.1 specification, be more verbose 
        StringBuilder errorContext = new StringBuilder("This request is only able to enclose exactly one entity.").append(" Remove all @").append(Entity.class.getSimpleName()).append(" annotations except for a single entity which is identified by this URI. ");
        throw new RequestProcessorException(errorContext.toString(), mee);
    } catch (EntityResolutionFailedException erfe) {
        //violates HTTP 1.1 specification, be more verbose
        StringBuilder errorContext = new StringBuilder("This request cannot proceed without an enclosing entity.").append(" Ensure that the entity which is annotated with ").append(Entity.class.getSimpleName()).append(" complies with the supported types as documented in ").append(RequestUtils.class.getName()).append("#resolveHttpEntity(Object)");
        throw new RequestProcessorException(errorContext.toString(), erfe);
    } catch (Exception e) {
        throw new RequestProcessorException(context, getClass(), e);
    }
    return request;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpEntity(org.apache.http.HttpEntity) Entity(com.lonepulse.robozombie.annotation.Entity) HttpEntityEnclosingRequestBase(org.apache.http.client.methods.HttpEntityEnclosingRequestBase) Serialize(com.lonepulse.robozombie.annotation.Serialize) HttpEntity(org.apache.http.HttpEntity) EntityResolutionFailedException(com.lonepulse.robozombie.util.EntityResolutionFailedException) EntityResolutionFailedException(com.lonepulse.robozombie.util.EntityResolutionFailedException) Entry(java.util.Map.Entry)

Aggregations

Entity (com.lonepulse.robozombie.annotation.Entity)1 Serialize (com.lonepulse.robozombie.annotation.Serialize)1 EntityResolutionFailedException (com.lonepulse.robozombie.util.EntityResolutionFailedException)1 Entry (java.util.Map.Entry)1 HttpEntity (org.apache.http.HttpEntity)1 HttpEntityEnclosingRequestBase (org.apache.http.client.methods.HttpEntityEnclosingRequestBase)1 HttpPost (org.apache.http.client.methods.HttpPost)1