요청 관리 -> variable 적용
This commit is contained in:
@@ -39,12 +39,17 @@ public class MockApiController {
|
||||
public ResponseEntity<String> handleRequest(HttpServletRequest httpRequest) throws IOException {
|
||||
MockRequest request = new MockRequest(httpRequest);
|
||||
AntPathMatcher pathMatcher = new AntPathMatcher();
|
||||
logger.debug("===========================");
|
||||
logger.debug("Request: " + request.getMethod() + " " + request.getUri());
|
||||
logger.debug("Headers:" + request.getHeaders().toString());
|
||||
logger.debug("Params: " + request.getParams().toString());
|
||||
logger.debug("Body: ");
|
||||
logger.debug(request.getBody());
|
||||
logger.debug("===========================");
|
||||
|
||||
for (MockRoute mockRoute : mockRouteService.getAllRoutes()) {
|
||||
if (isRouteMatch(mockRoute, request)) {
|
||||
logger.debug("===========================");
|
||||
logger.debug(request.getBody());
|
||||
logger.debug("===========================");
|
||||
|
||||
Map<String, String> pathVariables = pathMatcher.extractUriTemplateVariables(mockRoute.getPathPattern(), request.getUri());
|
||||
MockResponse matchingResponse = findMatchingResponse(mockRoute, request, pathVariables);
|
||||
if (matchingResponse != null) {
|
||||
|
||||
@@ -12,6 +12,7 @@ import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
@@ -29,7 +30,7 @@ public class ApiClientController {
|
||||
|
||||
@PostMapping("/mgmt/api/test.do")
|
||||
@ResponseBody
|
||||
public ApiResponse handleApiRequest(@RequestBody ApiRequestDTO request) throws InterruptedException, ExecutionException {
|
||||
public ApiResponse handleApiRequest(@RequestBody ApiRequestDTO request) throws InterruptedException, ExecutionException, InvocationTargetException, IllegalAccessException, InstantiationException, NoSuchMethodException {
|
||||
|
||||
return nettyApiClient.handleRequest(request).get();
|
||||
}
|
||||
|
||||
@@ -17,6 +17,9 @@ public class ApiRequestDTO {
|
||||
private List<ApiRequestKeyValueDTO> headers = new ArrayList<>();
|
||||
|
||||
private List<ApiRequestKeyValueDTO> queryParams = new ArrayList<>();
|
||||
|
||||
private List<ApiRequestKeyValueDTO> variables = new ArrayList<>();
|
||||
|
||||
private String requestBody;
|
||||
|
||||
public String getId() {
|
||||
@@ -75,6 +78,14 @@ public class ApiRequestDTO {
|
||||
this.queryParams = queryParams;
|
||||
}
|
||||
|
||||
public List<ApiRequestKeyValueDTO> getVariables() {
|
||||
return variables;
|
||||
}
|
||||
|
||||
public void setVariables(List<ApiRequestKeyValueDTO> variables) {
|
||||
this.variables = variables;
|
||||
}
|
||||
|
||||
public String getRequestBody() {
|
||||
return requestBody;
|
||||
}
|
||||
|
||||
@@ -34,6 +34,9 @@ public class ApiRequestInfo {
|
||||
@Lob
|
||||
private String queryParams;
|
||||
|
||||
@Lob
|
||||
private String variables;
|
||||
|
||||
@Lob
|
||||
private String requestBody;
|
||||
|
||||
@@ -101,6 +104,14 @@ public class ApiRequestInfo {
|
||||
this.queryParams = queryParams;
|
||||
}
|
||||
|
||||
public String getVariables() {
|
||||
return variables;
|
||||
}
|
||||
|
||||
public void setVariables(String variables) {
|
||||
this.variables = variables;
|
||||
}
|
||||
|
||||
public String getRequestBody() {
|
||||
return requestBody;
|
||||
}
|
||||
|
||||
@@ -67,6 +67,7 @@ public class ApiRequestMgmtService {
|
||||
existing.setPath(api.getPath());
|
||||
existing.setHeaders(api.getHeaders());
|
||||
existing.setQueryParams(api.getQueryParams());
|
||||
existing.setVariables(api.getVariables());
|
||||
existing.setServer(api.getServer());
|
||||
existing.setRequestBody(api.getRequestBody());
|
||||
apiRequestRepository.save(existing);
|
||||
|
||||
@@ -20,11 +20,13 @@ import io.netty.handler.ssl.SslContext;
|
||||
import io.netty.handler.ssl.SslContextBuilder;
|
||||
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
|
||||
import io.netty.util.CharsetUtil;
|
||||
import org.apache.commons.beanutils.BeanUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.net.ssl.SSLException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@@ -34,8 +36,8 @@ public class NettyApiClient {
|
||||
@Autowired
|
||||
ServerRepository serverRepository;
|
||||
|
||||
public CompletableFuture<ApiResponse> handleRequest(ApiRequestDTO apiRequest) throws InterruptedException {
|
||||
|
||||
public CompletableFuture<ApiResponse> handleRequest(ApiRequestDTO originalRequest) throws InterruptedException, InvocationTargetException, IllegalAccessException, InstantiationException, NoSuchMethodException {
|
||||
ApiRequestDTO apiRequest = replacePlaceholdersWithVariables(originalRequest);
|
||||
Server server = serverRepository.findById(apiRequest.getServer()).orElseThrow(() -> new ServerNotFoundException("Server not found"));
|
||||
String host = server.getHostname();
|
||||
int port = server.getPort();
|
||||
@@ -103,5 +105,35 @@ public class NettyApiClient {
|
||||
return responseFuture;
|
||||
}
|
||||
|
||||
private ApiRequestDTO replacePlaceholdersWithVariables(ApiRequestDTO original) throws InvocationTargetException, IllegalAccessException, InstantiationException, NoSuchMethodException {
|
||||
ApiRequestDTO request = (ApiRequestDTO) BeanUtils.cloneBean(original);
|
||||
if (request.getVariables() != null) {
|
||||
for (ApiRequestKeyValueDTO variable : request.getVariables()) {
|
||||
if (variable.isEnabled()) {
|
||||
String placeholder = "{{" + variable.getKey() + "}}";
|
||||
// Replace in requestBody
|
||||
request.setPath(request.getPath().replace(placeholder, variable.getValue()));
|
||||
if (request.getRequestBody() != null) {
|
||||
request.setRequestBody(request.getRequestBody().replace(placeholder, variable.getValue()));
|
||||
}
|
||||
// Replace in headers
|
||||
if (request.getHeaders() != null) {
|
||||
for (ApiRequestKeyValueDTO header : request.getHeaders()) {
|
||||
header.setKey(header.getKey().replace(placeholder, variable.getValue()));
|
||||
header.setValue(header.getValue().replace(placeholder, variable.getValue()));
|
||||
}
|
||||
}
|
||||
// Replace in queryParams
|
||||
if (request.getQueryParams() != null) {
|
||||
for (ApiRequestKeyValueDTO queryParam : request.getQueryParams()) {
|
||||
queryParam.setKey(queryParam.getKey().replace(placeholder, variable.getValue()));
|
||||
queryParam.setValue(queryParam.getValue().replace(placeholder, variable.getValue()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return request;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user