diff --git a/docs/사용자정의함수.md b/docs/사용자정의함수.md new file mode 100644 index 0000000..7656ed2 --- /dev/null +++ b/docs/사용자정의함수.md @@ -0,0 +1,70 @@ +# 사용자 정의 변환 함수 + +## 1. substringrequestmessage + +최초요청데이터를 substring + +| 항목 | 값 | +|------|-----| +| 변환함수명 | substringrequestmessage | +| 변환함수반환유형ID | [2101] STRING | +| 변환함수유형ID | [2302] GENERIC | +| 변환함수생성클래스 | com.eactive.eai.custom.transformer.function.userdefined.SubStringRequestMessage | + +### 파라미터 + +| 일련번호 | 파라미터명 | 유형 | 설명 | +|---------|-----------|------|------| +| 1 | start | NUMBER | 시작 위치 | +| 2 | end | NUMBER | 종료 위치 | + +### 사용 예시 + +``` +substringrequestmessage(AGW_TESTCASE019S1_TGTR.status,3,7) +``` + +**샘플 메시지:** +``` +ABCDEFGHIJK +``` + +**예상 결과:** +``` +DEFG +``` + +--- + +## 2. jsonpathextractrequestmessage + +최초요청(100)이 JSON 일 경우 특정 PATH의 데이터를 추출 하는 함수 + +| 항목 | 값 | +|------|-----| +| 변환함수명 | jsonpathextractrequestmessage | +| 변환함수반환유형ID | [2116] OBJECT | +| 변환함수유형ID | [2302] GENERIC | +| 변환함수생성클래스 | com.eactive.eai.custom.transformer.function.userdefined.JsonPathExtractRequestMessage | + +### 파라미터 + +| 일련번호 | 파라미터명 | 유형 | 설명 | +|---------|-----------|------|------| +| 1 | jsonPath | STRING | 대상 PATH | + +### 사용 예시 + +``` +jsonpathextractrequestmessage(AGW_TESTCASE022S1_TGTR.transactionId,"$.group1.field2") +``` + +**샘플 메시지:** +```json +{"group1":{"field1":"value1","field2":"value2"},"field3":"value3"} +``` + +**예상 결과:** +``` +value2 +``` diff --git a/src/main/java/com/eactive/eai/custom/transformer/function/userdefined/JsonPathExtractRequestMessage.java b/src/main/java/com/eactive/eai/custom/transformer/function/userdefined/JsonPathExtractRequestMessage.java new file mode 100644 index 0000000..4447414 --- /dev/null +++ b/src/main/java/com/eactive/eai/custom/transformer/function/userdefined/JsonPathExtractRequestMessage.java @@ -0,0 +1,74 @@ +package com.eactive.eai.custom.transformer.function.userdefined; + +import com.eactive.eai.common.context.ElinkTransactionContext; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.jayway.jsonpath.Configuration; +import com.jayway.jsonpath.JsonPath; +import com.jayway.jsonpath.spi.json.JacksonJsonNodeJsonProvider; +import com.jayway.jsonpath.spi.json.JacksonJsonProvider; +import org.nfunk.jep.ParseException; +import org.nfunk.jep.function.PostfixMathCommand; + +import java.util.Stack; + +public class JsonPathExtractRequestMessage extends PostfixMathCommand { + + private static final ObjectMapper objectMapper = new ObjectMapper(); + + public JsonPathExtractRequestMessage() { + numberOfParameters = -1; + } + + public void run(Stack inStack) throws ParseException { + checkStack(inStack); + + String bizData = ElinkTransactionContext.getRequestBizData(); + + Object p1 = inStack.pop(); + String path = String.valueOf(p1); + + inStack.clear(); + + try { + // Jackson을 JsonPath의 JSON 프로바이더로 설정 + Configuration config = Configuration.builder() + .jsonProvider(new JacksonJsonNodeJsonProvider()) + .build(); + + JsonNode resultNode = JsonPath.using(config).parse(bizData).read(path); + + if (resultNode == null) { + inStack.push(null); + } else if (resultNode.isValueNode()) { + // 단순 값(문자열, 숫자, boolean 등)은 텍스트로 반환 + inStack.push(resultNode.asText()); + } else { + // 객체/배열은 JsonNode 그대로 반환 + inStack.push(resultNode); + } + } catch (Exception e) { + throw new ParseException("JsonPath extract error: " + e.getMessage()); + } + } + + private JsonNode getNodeAtPath(JsonNode rootNode, String path) { + String[] tokens = path.split("/"); + JsonNode currentNode = rootNode; + + for (String token : tokens) { + if (token.isEmpty()) continue; + + if (token.matches("\\d+")) { + currentNode = currentNode.get(Integer.parseInt(token)); + } else { + currentNode = currentNode.get(token); + } + + if (currentNode == null) { + return null; + } + } + return currentNode; + } +}