sonar 대응
This commit is contained in:
@@ -2,9 +2,9 @@ package com.eactive.httpmockserver.client.controller;
|
||||
|
||||
import com.eactive.httpmockserver.client.dto.ApiRequestDTO;
|
||||
import com.eactive.httpmockserver.client.dto.ApiResponse;
|
||||
import com.eactive.httpmockserver.client.service.NettyApiClient;
|
||||
import com.eactive.httpmockserver.server.entity.Server;
|
||||
import com.eactive.httpmockserver.server.entity.ServerRepository;
|
||||
import com.eactive.httpmockserver.client.service.NettyApiClient;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.ModelMap;
|
||||
|
||||
+20
-12
@@ -6,9 +6,9 @@ import com.eactive.httpmockserver.client.entity.ApiCollection;
|
||||
import com.eactive.httpmockserver.client.entity.ApiRequestInfo;
|
||||
import com.eactive.httpmockserver.client.mapper.ApiRequestMapper;
|
||||
import com.eactive.httpmockserver.client.service.ApiRequestMgmtService;
|
||||
import com.eactive.httpmockserver.common.security.CurrentUser;
|
||||
import com.eactive.httpmockserver.common.util.SecurityUtil;
|
||||
import com.eactive.httpmockserver.user.entity.StaffUser;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import com.eactive.httpmockserver.user.service.UserService;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@@ -17,28 +17,36 @@ import java.util.List;
|
||||
@RestController
|
||||
public class ApiRequestMgmtController {
|
||||
|
||||
@Autowired
|
||||
private static final String SUCCESS = "success";
|
||||
ApiRequestMgmtService apiRequestMgmtService;
|
||||
|
||||
@Autowired
|
||||
ApiRequestMapper apiRequestMapper;
|
||||
|
||||
UserService userService;
|
||||
|
||||
public ApiRequestMgmtController(ApiRequestMgmtService apiRequestMgmtService, ApiRequestMapper apiRequestMapper, UserService userService) {
|
||||
this.apiRequestMgmtService = apiRequestMgmtService;
|
||||
this.apiRequestMapper = apiRequestMapper;
|
||||
this.userService = userService;
|
||||
}
|
||||
|
||||
@GetMapping("/mgmt/collections/list.do")
|
||||
public ResponseEntity<List<ApiCollectionDTO>> listApiRequests(@CurrentUser StaffUser user) {
|
||||
public ResponseEntity<List<ApiCollectionDTO>> listApiRequests() {
|
||||
StaffUser user = userService.findByEsntlId(SecurityUtil.getCurrentUserEsntlId());
|
||||
List<ApiCollection> collections = apiRequestMgmtService.getMyApis(user);
|
||||
return ResponseEntity.ok(apiRequestMapper.mapCollections(collections));
|
||||
}
|
||||
|
||||
@PostMapping("/mgmt/collections/create.do")
|
||||
public ResponseEntity<String> createApiCollection(
|
||||
@CurrentUser StaffUser user,
|
||||
@RequestBody ApiCollectionDTO dto) {
|
||||
|
||||
ApiCollection collection = apiRequestMapper.map(dto);
|
||||
StaffUser user = userService.findByEsntlId(SecurityUtil.getCurrentUserEsntlId());
|
||||
collection.setOwner(user);
|
||||
|
||||
apiRequestMgmtService.createNewApiCollection(collection);
|
||||
return ResponseEntity.ok("success");
|
||||
return ResponseEntity.ok(SUCCESS);
|
||||
}
|
||||
|
||||
|
||||
@@ -46,20 +54,20 @@ public class ApiRequestMgmtController {
|
||||
public ResponseEntity<String> updateApiCollection(@RequestBody ApiCollectionDTO dto) {
|
||||
ApiCollection updated = apiRequestMapper.map(dto);
|
||||
apiRequestMgmtService.updateApiCollection(updated.getId(), updated);
|
||||
return ResponseEntity.ok("success");
|
||||
return ResponseEntity.ok(SUCCESS);
|
||||
}
|
||||
|
||||
@PostMapping("/mgmt/collections/delete.do")
|
||||
public ResponseEntity<String> deleteApiCollection(@RequestBody ApiCollectionDTO dto) {
|
||||
apiRequestMgmtService.deleteApiCollection(dto.getId());
|
||||
return ResponseEntity.ok("success");
|
||||
return ResponseEntity.ok(SUCCESS);
|
||||
}
|
||||
|
||||
@PostMapping("/mgmt/collections/{id}/apis/save.do")
|
||||
public ResponseEntity<String> addApiToCollection(@PathVariable(name = "id") String id,
|
||||
@RequestBody ApiRequestDTO dto,
|
||||
@CurrentUser StaffUser user) {
|
||||
@RequestBody ApiRequestDTO dto) {
|
||||
ApiRequestInfo apiRequestInfo = apiRequestMapper.map(dto);
|
||||
StaffUser user = userService.findByEsntlId(SecurityUtil.getCurrentUserEsntlId());
|
||||
apiRequestInfo.setOwner(user);
|
||||
String apiId = apiRequestMgmtService.saveApiToCollection(id, apiRequestInfo);
|
||||
return ResponseEntity.ok(apiId);
|
||||
@@ -69,7 +77,7 @@ public class ApiRequestMgmtController {
|
||||
public ResponseEntity<String> removeApiFromCollection(@PathVariable(name = "id") String id,
|
||||
@RequestBody ApiRequestDTO dto) {
|
||||
apiRequestMgmtService.removeApiFromCollection(id, dto.getId());
|
||||
return ResponseEntity.ok("success");
|
||||
return ResponseEntity.ok(SUCCESS);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -119,12 +119,4 @@ public class ApiRequestInfo {
|
||||
return Objects.hash(id, server, owner, method, path, headers, queryParams, requestBody);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
ApiRequestInfo a= new ApiRequestInfo();
|
||||
a.setId("a");
|
||||
ApiRequestInfo b = new ApiRequestInfo();
|
||||
a.setId("a");
|
||||
|
||||
System.out.println(a.equals(b));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,9 @@
|
||||
package com.eactive.httpmockserver.client.repository;
|
||||
|
||||
import com.eactive.httpmockserver.client.entity.ApiCollection;
|
||||
import com.eactive.httpmockserver.client.entity.ApiRequestInfo;
|
||||
import com.eactive.httpmockserver.user.entity.StaffUser;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface ApiRequestRepository extends JpaRepository<ApiRequestInfo, String>, JpaSpecificationExecutor<ApiRequestInfo> {
|
||||
|
||||
}
|
||||
|
||||
@@ -18,6 +18,10 @@ import java.util.UUID;
|
||||
@Transactional
|
||||
public class ApiRequestMgmtService {
|
||||
|
||||
private static final String API_COLLECTION_NOT_FOUND = "api collection[%s] not found";
|
||||
|
||||
private static final String API_NOT_FOUND = "api collection[%s] not found";
|
||||
|
||||
@Autowired
|
||||
ApiCollectionRepository apiCollectionRepository;
|
||||
|
||||
@@ -30,9 +34,9 @@ public class ApiRequestMgmtService {
|
||||
}
|
||||
|
||||
public void deleteApiCollection(String id) {
|
||||
ApiCollection collection = apiCollectionRepository.findById(id).orElseThrow(() -> new NotFoundException("api collection[" + id + "] not found"));
|
||||
ApiCollection collection = apiCollectionRepository.findById(id).orElseThrow(() -> new NotFoundException(String.format(API_COLLECTION_NOT_FOUND, id)));
|
||||
|
||||
if (collection.getApis().size()>0){
|
||||
if (!collection.getApis().isEmpty()) {
|
||||
throw new RuntimeException("api collection[" + id + "] is not empty");
|
||||
}
|
||||
apiCollectionRepository.deleteById(id);
|
||||
@@ -44,20 +48,20 @@ public class ApiRequestMgmtService {
|
||||
}
|
||||
|
||||
public void updateApiCollection(String id, ApiCollection updated) {
|
||||
ApiCollection collection = apiCollectionRepository.findById(id).orElseThrow(() -> new NotFoundException("api collection[" + id + "] not found"));
|
||||
ApiCollection collection = apiCollectionRepository.findById(id).orElseThrow(() -> new NotFoundException(String.format(API_COLLECTION_NOT_FOUND, id)));
|
||||
collection.setName(updated.getName());
|
||||
apiCollectionRepository.save(collection);
|
||||
}
|
||||
|
||||
public String saveApiToCollection(String id, ApiRequestInfo api) {
|
||||
ApiCollection collection = apiCollectionRepository.findById(id).orElseThrow(() -> new NotFoundException("api collection[" + id + "] not found"));
|
||||
ApiCollection collection = apiCollectionRepository.findById(id).orElseThrow(() -> new NotFoundException(String.format(API_COLLECTION_NOT_FOUND, id)));
|
||||
|
||||
if (StringUtils.isEmpty(api.getId())) {
|
||||
api.setId(UUID.randomUUID().toString());
|
||||
apiRequestRepository.save(api);
|
||||
} else {
|
||||
String apiId = api.getId();
|
||||
ApiRequestInfo existing = apiRequestRepository.findById(apiId).orElseThrow(() -> new NotFoundException("api[" + apiId + "] not found"));
|
||||
ApiRequestInfo existing = apiRequestRepository.findById(apiId).orElseThrow(() -> new NotFoundException(String.format(API_NOT_FOUND, apiId)));
|
||||
existing.setName(api.getName());
|
||||
existing.setMethod(api.getMethod());
|
||||
existing.setPath(api.getPath());
|
||||
@@ -79,8 +83,8 @@ public class ApiRequestMgmtService {
|
||||
}
|
||||
|
||||
public void removeApiFromCollection(String id, String apiId) {
|
||||
ApiCollection collection = apiCollectionRepository.findById(id).orElseThrow(() -> new NotFoundException("api collection[" + id + "] not found"));
|
||||
ApiRequestInfo api = apiRequestRepository.findById(apiId).orElseThrow(() -> new NotFoundException("api[" + apiId + "] not found"));
|
||||
ApiCollection collection = apiCollectionRepository.findById(id).orElseThrow(() -> new NotFoundException(String.format(API_COLLECTION_NOT_FOUND, id)));
|
||||
ApiRequestInfo api = apiRequestRepository.findById(apiId).orElseThrow(() -> new NotFoundException(String.format(API_NOT_FOUND, apiId)));
|
||||
|
||||
collection.getApis().remove(api);
|
||||
apiCollectionRepository.save(collection);
|
||||
|
||||
+2
-2
@@ -20,8 +20,8 @@ public class ApiRequestMigrationService {
|
||||
public void migrate() {
|
||||
apiRequestRepository.findAll().forEach(apiRequest -> {
|
||||
|
||||
apiRequest.setHeaders(apiRequest.getHeaders().replaceAll("=", "::"));
|
||||
apiRequest.setQueryParams(apiRequest.getQueryParams().replaceAll("=", "::"));
|
||||
apiRequest.setHeaders(apiRequest.getHeaders().replace("=", "::"));
|
||||
apiRequest.setQueryParams(apiRequest.getQueryParams().replace("=", "::"));
|
||||
|
||||
apiRequestRepository.save(apiRequest);
|
||||
});
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
package com.eactive.httpmockserver.client.service;
|
||||
|
||||
import com.eactive.httpmockserver.client.dto.ApiRequestKeyValueDTO;
|
||||
import com.eactive.httpmockserver.client.dto.ApiRequestDTO;
|
||||
import com.eactive.httpmockserver.client.dto.ApiRequestKeyValueDTO;
|
||||
import com.eactive.httpmockserver.client.dto.ApiResponse;
|
||||
import com.eactive.httpmockserver.common.exception.ServerNotFoundException;
|
||||
import com.eactive.httpmockserver.server.entity.Server;
|
||||
import com.eactive.httpmockserver.server.entity.ServerRepository;
|
||||
import io.netty.bootstrap.Bootstrap;
|
||||
@@ -24,6 +25,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.net.ssl.SSLException;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@Service
|
||||
@@ -32,9 +34,9 @@ public class NettyApiClient {
|
||||
@Autowired
|
||||
ServerRepository serverRepository;
|
||||
|
||||
public CompletableFuture<ApiResponse> handleRequest(ApiRequestDTO apiRequest) throws Exception {
|
||||
public CompletableFuture<ApiResponse> handleRequest(ApiRequestDTO apiRequest) throws InterruptedException {
|
||||
|
||||
Server server = serverRepository.findById(apiRequest.getServer()).orElseThrow(() -> new Exception("Server not found"));
|
||||
Server server = serverRepository.findById(apiRequest.getServer()).orElseThrow(() -> new ServerNotFoundException("Server not found"));
|
||||
String host = server.getHostname();
|
||||
int port = server.getPort();
|
||||
|
||||
@@ -74,12 +76,9 @@ public class NettyApiClient {
|
||||
}
|
||||
|
||||
String basePath = server.getBasePath();
|
||||
if(!apiRequest.getPath().startsWith("/")){
|
||||
basePath = basePath + "/";
|
||||
}
|
||||
String fullPath = Paths.get(basePath, apiRequest.getPath()).toString();
|
||||
|
||||
HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.valueOf(apiRequest.getMethod()),
|
||||
(basePath + apiRequest.getPath()).replaceAll("//", "/"), content);
|
||||
HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.valueOf(apiRequest.getMethod()), fullPath, content);
|
||||
|
||||
request.headers().set("Host", host);
|
||||
request.headers().set("Connection", HttpHeaderValues.CLOSE);
|
||||
@@ -89,9 +88,9 @@ public class NettyApiClient {
|
||||
request.headers().set(HttpHeaderNames.CONTENT_LENGTH, content.readableBytes());
|
||||
}
|
||||
|
||||
if (apiRequest.getHeaders().size() > 0) {
|
||||
if (!apiRequest.getHeaders().isEmpty()) {
|
||||
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())){
|
||||
customHeaders.set(header.getKey(), header.getValue());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user