package com.enactor.training.paymentService.service.actions.gcash;

import com.enactor.core.json.rest.RestJsonMessageHandler;
import com.enactor.core.json.serializer.JsonSerializerUtil.Format;
import com.enactor.core.webService.epr.IRestEndpoint;
import com.enactor.core.webService.rest.RestServiceInvocationException;

/**
 * A message handler that converts the supplied object into JSON using Jackson.
 *
 * The class performs the same conversion (in reverse) on the response.
 *
 * The expected responseObjectClass must be supplied in the constructor so
 * that it can be used when converting from JSON back into an object.
 *
 * There is also a constructor that can accept a Json serializer
 * format (SIMPLE when @class types should not be included, ENACTOR
 * when they should).
 *
 */
public class CustomRestJsonMessageHandler<T> extends RestJsonMessageHandler {

	/**
	 * Default no-args constructor
	 */
	public CustomRestJsonMessageHandler() {
	}

	/**
	 * RestJsonMessageHandler constructor. Defaults jsonSerializerFormat to Format.ENACTOR.
	 * @param responseObjectClass The class type of the object that is expected to be created from the response json.
	 */
	public CustomRestJsonMessageHandler(Class<T> responseObjectClass) {
		this(responseObjectClass, Format.ENACTOR);
	}

	/**
	 * RestJsonMessageHandler constructor that accepts a jsonSerializerFormat.
	 *
	 * @param responseObjectClass The class type of the object that is expected to be created from the response json.
	 * @param jsonSerializerFormat
	 */
	public CustomRestJsonMessageHandler(Class<T> responseObjectClass, Format jsonSerializerFormat) {
		super(responseObjectClass, jsonSerializerFormat);
	}

	/**
	 * RestJsonMessageHandler constructor that accepts an object class and an error class.
	 *
	 * @param responseObjectClass The class type of the object that is expected to be created from
	 *            the response json.
	 * @param responseErrorClass The class type of the object that is expected to be created from an
	 *            error response json.
	 */
	public CustomRestJsonMessageHandler(Class<T> responseObjectClass, Class<T> responseErrorClass) {
		super(responseObjectClass, responseErrorClass);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String processRequest(IRestEndpoint restEndpoint,
			Object requestObject) throws RestServiceInvocationException {
		CustomRestJsonRequestMessageHandler requestHandler = new CustomRestJsonRequestMessageHandler();
		return (String) requestHandler.processRequest(restEndpoint, requestObject);
	}

}
