All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.lonepulse.zombielink.response.AbstractResponseProcessor Maven / Gradle / Ivy

The newest version!
package com.lonepulse.zombielink.response;

/*
 * #%L
 * ZombieLink
 * %%
 * Copyright (C) 2013 - 2014 Lonepulse
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import static com.lonepulse.zombielink.util.Assert.assertAssignable;
import static com.lonepulse.zombielink.util.Assert.assertLength;
import static com.lonepulse.zombielink.util.Assert.assertNotNull;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpRequestBase;

import com.lonepulse.zombielink.processor.Processor;
import com.lonepulse.zombielink.proxy.InvocationContext;

/**
 * 

This is an abstract implementation of {@link Processor} which specifies a template for processing * the response of a request execution by referencing the metadata on a proxy endpoint * request. It includes an implementation of {@link Processor#run(Object...)} that checks the * preconditions for executing {@link #process(InvocationContext, HttpRequestBase)}.

* *

All implementations must be aware of the {@link InvocationContext} which can be used to discover * information about the endpoint and the request declaration. This information can be queried based on * the targeting criteria for this response processor and the resulting information should be used * to deserialize the given {@link HttpResponse}.

* *

It is advised to adhere to RFC 2616 of * HTTP 1.1 when designing an implementation.

* * @version 1.1.0 *

* @since 1.3.0 *

* @author Lahiru Sahan Jayasinghe */ abstract class AbstractResponseProcessor implements Processor { /** *

Accepts an {@link InvocationContext} and an {@link HttpResponse}, validates all preconditions * and uses the metadata contained within the configuration to process and subsequently parse the * request. Any implementations that wish to check additional preconditions or those that wish to * alter this basic approach should override this method.

* *

Note that this method is expected to return the deserialized response entity of * the type specified by the request definition. This is passed along to all successive processors * in the chain via the processor arguments.

* *

Delegates to {@link #process(InvocationContext, HttpResponse, Object)}.

* *

See {@link Processor#run(Object...)}.

* * @param args * a array of length 2 or more with an {@link HttpResponse}, an {@link InvocationContext} * and possibly the result of the deserialized response entity *

* @return the deserialized response entity, which may be {@code null} for endpoint request definitions * which do not declare a return type or for those which the return type is {@link Void} *

* @throws IllegalArgumentException * if the supplied arguments array is {@code null} or if the number of arguments is less than 2, * or if the arguments are not of the expected type *

* @throws RequestProcessorException * if response processing failed for the given {@link InvocationContext} and {@link HttpResponse} *

* @since 1.3.0 */ @Override public Object run(Object... args) { assertLength(args, 2, 3); return process(assertAssignable(assertNotNull(args[0]), InvocationContext.class), assertAssignable(assertNotNull(args[1]), HttpResponse.class), (args.length > 2)? args[2] :null); } /** *

Takes the {@link InvocationContext} for the given {@link HttpResponse} and uses the metadata * contained within the configuration to deserialize the response body and perform * additional processing based on the response headers.

* *

The provided {@link HttpResponse} may contain a response entity which should be deserialized * to the correct type and it may contain certain essential response headers which should be processed. * Any implementation may wish to perform processing conditionally based on the response code. Refer * Section 9 of the HTTP 1.1 * for more information.

* * @param context * the {@link InvocationContext} which is used to discover any annotated metadata for the * request declaration which may be required for response processing *

* @param response * the {@link HttpResponse} received as result of a request execution; the response body * should be deserialized to the correct type and all response headers should be processed *

* @param deserializedResponse * the deserialized response content which will be passed along to all processors in the chain *

* @return the deserialized response entity of the type associated with the endpoint request definition *

* @throws RequestProcessorException * if the processor finds an {@link HttpResponse} which it should act upon and yet fails * to perform the necessary processing *

* @since 1.3.0 */ protected abstract Object process(InvocationContext context, HttpResponse response, Object deserializedResponse); }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy