In 필터 개발 : 데이터부에 상태코드 값을 응답 HTTP 상태코드에 설정하는 필터
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
package com.eactive.eai.adapter.http.dynamic.filter;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import com.eactive.eai.common.property.PropManager;
|
||||
import com.eactive.eai.common.util.Logger;
|
||||
import com.eactive.eai.util.JsonPathUtil;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
|
||||
public class JsonToSetStatusFilter implements HttpAdapterFilter {
|
||||
private static final String PROPGROUP = "JsonToSetStatusFilter";
|
||||
static Logger logger = Logger.getLogger(Logger.LOGGER_ADAPTER);
|
||||
/** HTTP 상태코드 유효 범위 (RFC 7231) */
|
||||
private static final int MIN_HTTP_STATUS = 100;
|
||||
private static final int MAX_HTTP_STATUS = 599;
|
||||
|
||||
@Override
|
||||
public Object doPreFilter(String adptGrpName, String adptName, Object message, Properties prop,
|
||||
HttpServletRequest request, HttpServletResponse response) throws Exception {
|
||||
// do nothing
|
||||
return message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object doPostFilter(String adptGrpName, String adptName, Object resultMessage, Properties prop,
|
||||
HttpServletRequest request, HttpServletResponse response) throws Exception {
|
||||
String fieldName = "";
|
||||
String statusParam = "";
|
||||
try {
|
||||
JsonNode rootNode = parseJson(adptGrpName, resultMessage);
|
||||
fieldName = getFieldName(adptGrpName);
|
||||
if (rootNode != null && StringUtils.isNotBlank(fieldName) && rootNode.has(fieldName)) {
|
||||
statusParam = rootNode.get(fieldName).asText();
|
||||
int httpStatus = Integer.parseInt(statusParam);
|
||||
if (isValidHttpStatus(httpStatus)) {
|
||||
response.setStatus(httpStatus);
|
||||
} else {
|
||||
logger.warn("유효하지 않은 HTTP 상태코드. 상태코드를 설정하지 않음. fieldName={}, value={}", fieldName, statusParam);
|
||||
}
|
||||
} else {
|
||||
logger.warn("설정과 맞지 않는 메시지. 상태코드를 설정하지 않음. fieldName={}", fieldName);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.warn("상태코드 추출 실패. fieldName={}, value={}", fieldName, statusParam, e);
|
||||
}
|
||||
|
||||
return resultMessage;
|
||||
}
|
||||
|
||||
/** HTTP 상태코드로 사용 가능한 값인지 확인한다. (100 ~ 599) */
|
||||
private boolean isValidHttpStatus(int httpStatus) {
|
||||
return httpStatus >= MIN_HTTP_STATUS && httpStatus <= MAX_HTTP_STATUS;
|
||||
}
|
||||
|
||||
private String getFieldName(String adptGrpName) {
|
||||
return PropManager.getInstance().getProperty(PROPGROUP, adptGrpName);
|
||||
}
|
||||
|
||||
private JsonNode parseJson(String adptGrpName, Object message) throws Exception {
|
||||
return JsonPathUtil.toTree(message);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user