package com.eactive.eai.common.util;

import org.apache.commons.lang3.StringUtils;

import com.eactive.eai.common.property.PropManager;
import com.eactive.eai.common.util.HttpAdapterExtraLogUtil;

public class RestSendBodyLogUtils {
	
	private static final String PROP_LOG_MAX_DATA_SIZE = "log.max.data.size";
	private static final String PROP_GROUP = "RestSendBodyLog";
	
	private RestSendBodyLogUtils() {
		throw new IllegalStateException("Utility class");
	}

	/**
	 * API ID 별 어댑터 body 로그 남기는 지 여부
	 * 
	 * @param apiId
	 * @return
	 */
	public static boolean isBodyLoggingApi(String apiId) {
		if (StringUtils.isEmpty(apiId))
			return false;

		String logging = PropManager.getInstance().getProperty(PROP_GROUP, apiId, "N");
		return StringUtils.equals("Y", logging);
	}

	/**
	 * body 로그에 남길 만큼 자르기
	 * 
	 * @param body
	 * @return
	 */
	public static String getBodyByMaxSize(String body) {
		String maxDataSize = PropManager.getInstance().getProperty(PROP_GROUP, PROP_LOG_MAX_DATA_SIZE);
		int imaxDataSize = HttpAdapterExtraLogUtil.MAX_HEADER_VALUE_SIZE;
		if (maxDataSize != null)
			imaxDataSize = Integer.parseInt(maxDataSize);

		if (body.length() > imaxDataSize)
			return StringUtils.substring(body, 0, imaxDataSize) + "...";

		return body;
	}

}
