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

import com.enactor.core.json.serializer.IJsonSerializerUtil;
import com.enactor.core.json.serializer.JsonParseException;
import com.enactor.core.json.serializer.JsonSerializerUtil;
import com.enactor.core.json.serializer.JsonSerializerUtil.Format;
import com.enactor.core.webService.epr.IRestEndpoint;
import com.enactor.core.webService.rest.IRestRequestMessageHandler;
import com.enactor.core.webService.rest.RestServiceInvocationException;

public class CustomRestJsonRequestMessageHandler implements IRestRequestMessageHandler {
	
	private Format jsonSerializerFormat = Format.ENACTOR;
	

	public CustomRestJsonRequestMessageHandler() {
	}
	
	public CustomRestJsonRequestMessageHandler(Format jsonSerializerFormat) {
		this.jsonSerializerFormat = jsonSerializerFormat;
	}

	@Override
	public Object processRequest(IRestEndpoint restEndpoint, Object requestObject)
			throws RestServiceInvocationException {
		if (requestObject == null) {
			return null;
		}

		try {
			IJsonSerializerUtil jsonSerializerUtil = JsonSerializerUtil.getInstance(jsonSerializerFormat);
			return jsonSerializerUtil.getUnformattedJSON(requestObject);
		} catch (JsonParseException e) {
			throw new RestServiceInvocationException(
					"Failed to convert requestObject of class: " + requestObject.getClass() + " into JSON object", e);
		}
	}

}
