ApiTesterFilter 개선 및 KJBank 흔적 제거
- "/api/call-api" 경로 필터링 로직 추가 - KJBank 관련 문자열 DJBank로 변경
This commit is contained in:
@@ -56,97 +56,99 @@ public class ApiTesterFilter implements Filter {
|
|||||||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
|
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
|
||||||
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
|
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
|
||||||
|
|
||||||
if (httpServletRequest.getRequestURI().contains("/api/call-api")) {
|
// "/api/call-api" 경로가 아닌 경우 필터링하지 않고 다음 필터로 넘어감
|
||||||
ApiService apiSpecInfoDtoService = ApplicationContextUtil.getContext().getBean(ApiService.class);
|
if (!httpServletRequest.getRequestURI().contains("/api/call-api")) {
|
||||||
String url = httpServletRequest.getHeader("original-url");
|
chain.doFilter(request, response);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (url.contains("/api/v1/oauth/token")){
|
ApiService apiSpecInfoDtoService = ApplicationContextUtil.getContext().getBean(ApiService.class);
|
||||||
StringBuilder sb = new StringBuilder();
|
String url = httpServletRequest.getHeader("original-url");
|
||||||
BufferedReader reader = httpServletRequest.getReader();
|
|
||||||
String line;
|
|
||||||
while ((line = reader.readLine()) != null) {
|
|
||||||
sb.append(line);
|
|
||||||
}
|
|
||||||
String body = sb.toString();
|
|
||||||
|
|
||||||
// Parse request parameters
|
if (url.contains("/api/v1/oauth/token")){
|
||||||
Map<String, String> params = new HashMap<>();
|
StringBuilder sb = new StringBuilder();
|
||||||
String[] pairs = body.split("&");
|
BufferedReader reader = httpServletRequest.getReader();
|
||||||
for (String pair : pairs) {
|
String line;
|
||||||
String[] keyValue = pair.split("=");
|
while ((line = reader.readLine()) != null) {
|
||||||
if (keyValue.length == 2) {
|
sb.append(line);
|
||||||
params.put(keyValue[0], keyValue[1]);
|
}
|
||||||
}
|
String body = sb.toString();
|
||||||
}
|
|
||||||
String scope = params.getOrDefault("scope", "default");
|
|
||||||
|
|
||||||
String token = "{\n" +
|
// Parse request parameters
|
||||||
" \"access_token\": \"kjbank_gw_sample_token\",\n" +
|
Map<String, String> params = new HashMap<>();
|
||||||
" \"token_type\": \"bearer\",\n" +
|
String[] pairs = body.split("&");
|
||||||
" \"expires_in\": 86400,\n" +
|
for (String pair : pairs) {
|
||||||
" \"scope\": \""+scope +"\",\n" +
|
String[] keyValue = pair.split("=");
|
||||||
" \"jti\": \""+ UUID.randomUUID().toString()+"\"\n" +
|
if (keyValue.length == 2) {
|
||||||
"}";
|
params.put(keyValue[0], keyValue[1]);
|
||||||
|
|
||||||
response.setContentType("application/json");
|
|
||||||
response.getWriter().println(token);
|
|
||||||
|
|
||||||
} else {
|
|
||||||
ApiSpecInfoDto apiSpecInfoDto = apiSpecInfoDtoService.selectDetailByURLAndMethod(parseUri(url), httpServletRequest.getMethod());
|
|
||||||
|
|
||||||
if (apiSpecInfoDto.getResponseType() == null || apiSpecInfoDto.getResponseType().equals("sample")) {
|
|
||||||
response.setContentType("application/json");
|
|
||||||
response.getWriter().println(apiSpecInfoDto.getSampleResponse());
|
|
||||||
} else {
|
|
||||||
String mockUrl = apiSpecInfoDto.getMockUrl();
|
|
||||||
String method = apiSpecInfoDto.getApiMethod();
|
|
||||||
Enumeration<String> headerNames = httpServletRequest.getHeaderNames();
|
|
||||||
|
|
||||||
Map<String, String> headers = new HashMap<>();
|
|
||||||
while (headerNames.hasMoreElements()) {
|
|
||||||
String header = headerNames.nextElement();
|
|
||||||
headers.put(header, httpServletRequest.getHeader(header));
|
|
||||||
}
|
|
||||||
Map<String, String[]> paramMap = new HashMap<>();
|
|
||||||
|
|
||||||
String originalUrl = headers.get("original-url");
|
|
||||||
//sprlit original url with ? get the second part then split with & put into paramMap
|
|
||||||
|
|
||||||
String[] originalUrlArr = originalUrl.split("\\?");
|
|
||||||
if (originalUrlArr.length > 1) {
|
|
||||||
String[] paramArr = originalUrlArr[1].split("&");
|
|
||||||
for (String param : paramArr) {
|
|
||||||
String[] paramKeyValue = param.split("=");
|
|
||||||
if (paramKeyValue.length > 1) {
|
|
||||||
paramMap.put(paramKeyValue[0], new String[]{paramKeyValue[1]});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
headers.remove("original-url");
|
|
||||||
headers.remove("original-api-id");
|
|
||||||
APISender apiSender = ApplicationContextUtil.getContext().getBean(APISender.class);
|
|
||||||
|
|
||||||
String responseStr = "";
|
|
||||||
if (method.equalsIgnoreCase("post")) {
|
|
||||||
StringBuilder sb = new StringBuilder();
|
|
||||||
BufferedReader reader = httpServletRequest.getReader();
|
|
||||||
String line;
|
|
||||||
while ((line = reader.readLine()) != null) {
|
|
||||||
sb.append(line);
|
|
||||||
}
|
|
||||||
String body = sb.toString();
|
|
||||||
responseStr = apiSender.requestPost(mockUrl, headers, paramMap, body);
|
|
||||||
|
|
||||||
} else {
|
|
||||||
responseStr = apiSender.requestGet(mockUrl, headers, paramMap);
|
|
||||||
}
|
|
||||||
response.setContentType("application/json");
|
|
||||||
response.getWriter().println(responseStr);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
String scope = params.getOrDefault("scope", "default");
|
||||||
|
|
||||||
|
String token = "{\n" +
|
||||||
|
" \"access_token\": \"djbank_gw_sample_token\",\n" +
|
||||||
|
" \"token_type\": \"bearer\",\n" +
|
||||||
|
" \"expires_in\": 86400,\n" +
|
||||||
|
" \"scope\": \""+scope +"\",\n" +
|
||||||
|
" \"jti\": \""+ UUID.randomUUID().toString()+"\"\n" +
|
||||||
|
"}";
|
||||||
|
|
||||||
|
response.setContentType("application/json");
|
||||||
|
response.getWriter().println(token);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
chain.doFilter(request, response);
|
ApiSpecInfoDto apiSpecInfoDto = apiSpecInfoDtoService.selectDetailByURLAndMethod(parseUri(url), httpServletRequest.getMethod());
|
||||||
|
|
||||||
|
if (apiSpecInfoDto.getResponseType() == null || apiSpecInfoDto.getResponseType().equals("sample")) {
|
||||||
|
response.setContentType("application/json");
|
||||||
|
response.getWriter().println(apiSpecInfoDto.getSampleResponse());
|
||||||
|
} else {
|
||||||
|
String mockUrl = apiSpecInfoDto.getMockUrl();
|
||||||
|
String method = apiSpecInfoDto.getApiMethod();
|
||||||
|
Enumeration<String> headerNames = httpServletRequest.getHeaderNames();
|
||||||
|
|
||||||
|
Map<String, String> headers = new HashMap<>();
|
||||||
|
while (headerNames.hasMoreElements()) {
|
||||||
|
String header = headerNames.nextElement();
|
||||||
|
headers.put(header, httpServletRequest.getHeader(header));
|
||||||
|
}
|
||||||
|
Map<String, String[]> paramMap = new HashMap<>();
|
||||||
|
|
||||||
|
String originalUrl = headers.get("original-url");
|
||||||
|
//sprlit original url with ? get the second part then split with & put into paramMap
|
||||||
|
|
||||||
|
String[] originalUrlArr = originalUrl.split("\\?");
|
||||||
|
if (originalUrlArr.length > 1) {
|
||||||
|
String[] paramArr = originalUrlArr[1].split("&");
|
||||||
|
for (String param : paramArr) {
|
||||||
|
String[] paramKeyValue = param.split("=");
|
||||||
|
if (paramKeyValue.length > 1) {
|
||||||
|
paramMap.put(paramKeyValue[0], new String[]{paramKeyValue[1]});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
headers.remove("original-url");
|
||||||
|
headers.remove("original-api-id");
|
||||||
|
APISender apiSender = ApplicationContextUtil.getContext().getBean(APISender.class);
|
||||||
|
|
||||||
|
String responseStr = "";
|
||||||
|
if (method.equalsIgnoreCase("post")) {
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
BufferedReader reader = httpServletRequest.getReader();
|
||||||
|
String line;
|
||||||
|
while ((line = reader.readLine()) != null) {
|
||||||
|
sb.append(line);
|
||||||
|
}
|
||||||
|
String body = sb.toString();
|
||||||
|
responseStr = apiSender.requestPost(mockUrl, headers, paramMap, body);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
responseStr = apiSender.requestGet(mockUrl, headers, paramMap);
|
||||||
|
}
|
||||||
|
response.setContentType("application/json");
|
||||||
|
response.getWriter().println(responseStr);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user