sonar 대응
This commit is contained in:
@@ -19,6 +19,7 @@ import org.springframework.web.bind.annotation.RestController;
|
|||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
@@ -51,7 +52,9 @@ public class MockApiController {
|
|||||||
try {
|
try {
|
||||||
Thread.sleep(matchingResponse.getDelay());
|
Thread.sleep(matchingResponse.getDelay());
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
logger.error("Error while delaying response", e);
|
logger.error("Error while delaying response", e);
|
||||||
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error processing request due to interruption.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
HttpHeaders responseHeaders = new HttpHeaders();
|
HttpHeaders responseHeaders = new HttpHeaders();
|
||||||
@@ -84,7 +87,7 @@ public class MockApiController {
|
|||||||
|
|
||||||
MockResponse defaultResponse = mockRoute.getResponses().stream().filter(MockResponse::isDefaultResponse).findFirst().orElse(null);
|
MockResponse defaultResponse = mockRoute.getResponses().stream().filter(MockResponse::isDefaultResponse).findFirst().orElse(null);
|
||||||
List<MockResponse> responses = mockRoute.getResponses().stream().filter(response -> !response.isDefaultResponse()).collect(Collectors.toList());
|
List<MockResponse> responses = mockRoute.getResponses().stream().filter(response -> !response.isDefaultResponse()).collect(Collectors.toList());
|
||||||
if(defaultResponse!=null){
|
if (defaultResponse != null) {
|
||||||
responses.add(defaultResponse);
|
responses.add(defaultResponse);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,34 +104,27 @@ public class MockApiController {
|
|||||||
if (conditions == null) {
|
if (conditions == null) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
List<Boolean> results = new ArrayList<>();
|
||||||
|
|
||||||
for (MockCondition condition : conditions) {
|
for (MockCondition condition : conditions) {
|
||||||
switch (condition.getType().toLowerCase()) {
|
switch (condition.getType().toLowerCase()) {
|
||||||
case "query":
|
case "query":
|
||||||
if (!isQueryMatching(condition, request)) {
|
results.add(isQueryMatching(condition, request));
|
||||||
return false;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case "header":
|
case "header":
|
||||||
if (!isHeaderMatching(condition, request)) {
|
results.add(isHeaderMatching(condition, request));
|
||||||
return false;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case "path":
|
case "path":
|
||||||
if (!isPathMatching(condition, pathVariables)) {
|
results.add(isPathMatching(condition, pathVariables));
|
||||||
return false;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case "jsonpath":
|
case "jsonpath":
|
||||||
if (!isJsonPathMatching(condition, request)) {
|
results.add(isJsonPathMatching(condition, request));
|
||||||
return false;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return !results.contains(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isQueryMatching(MockCondition condition, MockRequest request) {
|
private boolean isQueryMatching(MockCondition condition, MockRequest request) {
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import org.springframework.web.bind.annotation.RequestBody;
|
|||||||
import org.springframework.web.bind.annotation.ResponseBody;
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
public class ApiClientController {
|
public class ApiClientController {
|
||||||
@@ -26,13 +27,9 @@ public class ApiClientController {
|
|||||||
|
|
||||||
@PostMapping("/mgmt/api/test.do")
|
@PostMapping("/mgmt/api/test.do")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public ApiResponse handleApiRequest(@RequestBody ApiRequestDTO request) {
|
public ApiResponse handleApiRequest(@RequestBody ApiRequestDTO request) throws InterruptedException, ExecutionException {
|
||||||
|
|
||||||
try {
|
return nettyApiClient.handleRequest(request).get();
|
||||||
return nettyApiClient.handleRequest(request).get();
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/mgmt/api_tester.do")
|
@GetMapping("/mgmt/api_tester.do")
|
||||||
|
|||||||
@@ -49,15 +49,11 @@ public class NettyApiClient {
|
|||||||
.option(ChannelOption.TCP_NODELAY, true)
|
.option(ChannelOption.TCP_NODELAY, true)
|
||||||
.handler(new ChannelInitializer<Channel>() {
|
.handler(new ChannelInitializer<Channel>() {
|
||||||
@Override
|
@Override
|
||||||
public void initChannel(Channel ch) {
|
public void initChannel(Channel ch) throws SSLException {
|
||||||
if (server.getScheme().equalsIgnoreCase("https")) {
|
if (server.getScheme().equalsIgnoreCase("https")) {
|
||||||
try {
|
final SslContext sslCtx = SslContextBuilder.forClient()
|
||||||
final SslContext sslCtx = SslContextBuilder.forClient()
|
.trustManager(InsecureTrustManagerFactory.INSTANCE).build();
|
||||||
.trustManager(InsecureTrustManagerFactory.INSTANCE).build();
|
ch.pipeline().addLast(sslCtx.newHandler(ch.alloc(), host, port));// Add SSL handler
|
||||||
ch.pipeline().addLast(sslCtx.newHandler(ch.alloc(), host, port));// Add SSL handler
|
|
||||||
} catch (SSLException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ch.pipeline().addLast(new HttpClientCodec());
|
ch.pipeline().addLast(new HttpClientCodec());
|
||||||
@@ -91,7 +87,7 @@ public class NettyApiClient {
|
|||||||
if (!apiRequest.getHeaders().isEmpty()) {
|
if (!apiRequest.getHeaders().isEmpty()) {
|
||||||
HttpHeaders customHeaders = new DefaultHttpHeaders();
|
HttpHeaders customHeaders = new DefaultHttpHeaders();
|
||||||
apiRequest.getHeaders().stream().filter(ApiRequestKeyValueDTO::isEnabled).forEach(header -> {
|
apiRequest.getHeaders().stream().filter(ApiRequestKeyValueDTO::isEnabled).forEach(header -> {
|
||||||
if(StringUtils.isNotEmpty(header.getKey()) && StringUtils.isNotEmpty(header.getValue())){
|
if (StringUtils.isNotEmpty(header.getKey()) && StringUtils.isNotEmpty(header.getValue())) {
|
||||||
customHeaders.set(header.getKey(), header.getValue());
|
customHeaders.set(header.getKey(), header.getValue());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user