diff --git a/src/main/java/com/eactive/eai/common/util/MessageUtil.java b/src/main/java/com/eactive/eai/common/util/MessageUtil.java index fc5c9a9..4304ff1 100644 --- a/src/main/java/com/eactive/eai/common/util/MessageUtil.java +++ b/src/main/java/com/eactive/eai/common/util/MessageUtil.java @@ -1,5 +1,25 @@ package com.eactive.eai.common.util; +import java.io.BufferedInputStream; +import java.io.UnsupportedEncodingException; +import java.nio.charset.Charset; +import java.util.Iterator; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathFactory; + +import org.apache.commons.lang3.StringUtils; +import org.apache.tools.ant.filters.StringInputStream; +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; +import org.json.simple.JSONValue; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.w3c.dom.Document; + import com.eactive.eai.common.EAIKeys; import com.eactive.eai.common.message.MessageType; import com.eactive.eai.common.monitor.EAIServiceMonitor; @@ -10,30 +30,16 @@ import com.eactive.eai.transformer.util.ISO8583DumpUtil; import com.ext.eai.common.stdmessage.STDMessageErrorKeys; import com.jayway.jsonpath.JsonPath; import com.solab.iso8583.IsoMessage; -import org.apache.commons.lang3.StringUtils; -import org.apache.tools.ant.filters.StringInputStream; -import org.json.simple.JSONArray; -import org.json.simple.JSONObject; -import org.json.simple.JSONValue; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.w3c.dom.Document; - -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.xpath.XPath; -import javax.xml.xpath.XPathConstants; -import javax.xml.xpath.XPathFactory; -import java.io.BufferedInputStream; -import java.io.UnsupportedEncodingException; -import java.nio.charset.Charset; -import java.util.Iterator; -import java.util.List; -import java.util.Map; public final class MessageUtil { static Logger logger = LoggerFactory.getLogger(MessageUtil.class); + public static final String ERROR_MESSAGE_DEFAULT_FORMAT = "{\"error\":{\"code\":\"%s\",\"message\":\"%s\"}}"; + + public static final String ERROR_CODE_AP_ERROR = "E.GW.AP_ERROR"; + public static final String ERROR_CODE_AUTH_FAIL = "E.GW.AUTH_FAIL"; + public static final String ERROR_CODE_SERVICE_NOT_FOUND = "E.GW.SERVICE_NOT_FOUND"; + private MessageUtil() { } @@ -492,12 +498,10 @@ public final class MessageUtil { public static String makeErrorMessageByMessageType(String msgType, String msgCharset, String code, String msg, String errorFormat) { - if (StringUtils.equals(msgType, MessageType.JSON)) { - return makeJsonErrorMessage(code, msg, errorFormat); - } else if (StringUtils.equals(msgType, MessageType.XML)) { + if (StringUtils.equals(msgType, MessageType.XML)) { return makeXmlErrorMessage(code, msg, msgCharset, errorFormat); } else { - return "[errCd]" + code + "[/errCd]" + "[errMsg]" + msg + "[/errMsg]"; + return makeJsonErrorMessage(code, msg, errorFormat); } } @@ -510,14 +514,17 @@ public final class MessageUtil { } public static String makeJsonErrorMessage(String code, String msg, String errorFormat) { - if (StringUtils.isNotBlank(errorFormat)) { - try { - return String.format(errorFormat, code, JSONValue.escape(msg)); - } catch (Exception e) { - logger.error(e.getMessage()); - } + if (StringUtils.isBlank(errorFormat)) { + errorFormat = ERROR_MESSAGE_DEFAULT_FORMAT; + } + + // errorFormat에 %s가 1개만 있으면 msg만, 2개 이상이면 code, msg 전달 + int paramCount = StringUtils.countMatches(errorFormat, "%s"); + if (paramCount == 1) { + return String.format(errorFormat, JSONValue.escape(msg)); + } else { + return String.format(errorFormat, code, JSONValue.escape(msg)); } - return String.format("{\"error\":\"%s\",\"error_description\":\"%s\"}", code, JSONValue.escape(msg)); } public static String makeXmlErrorMessage(String code, String msg, String charset, String errorFormat) {