test master 변경
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
package com.eactive.testmaster.client.controller;
|
||||
|
||||
import com.eactive.testmaster.client.dto.ApiRequestDTO;
|
||||
import com.eactive.testmaster.client.dto.ApiResponse;
|
||||
import com.eactive.testmaster.client.service.NettyApiClient;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.validation.Validator;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
@RestController
|
||||
public class ApiClientController {
|
||||
|
||||
private final NettyApiClient nettyApiClient;
|
||||
private final Validator validator;
|
||||
|
||||
public ApiClientController(NettyApiClient nettyApiClient, Validator validator) {
|
||||
this.nettyApiClient = nettyApiClient;
|
||||
this.validator = validator;
|
||||
}
|
||||
|
||||
@PostMapping("/mgmt/api/test.do")
|
||||
public ApiResponse handleApiRequest(@RequestBody ApiRequestDTO request, BindingResult bindingResult) throws InterruptedException, ExecutionException {
|
||||
|
||||
validator.validate(request, bindingResult);
|
||||
|
||||
if (bindingResult.hasErrors()) {
|
||||
throw new IllegalArgumentException(bindingResult.getAllErrors().get(0).getDefaultMessage());
|
||||
}
|
||||
|
||||
return nettyApiClient.handleRequest(request).get();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.eactive.testmaster.client.controller;
|
||||
|
||||
import com.eactive.testmaster.server.entity.Server;
|
||||
import com.eactive.testmaster.server.entity.ServerRepository;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.ModelMap;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Controller
|
||||
public class ApiClientPageController {
|
||||
|
||||
private final ServerRepository serverRepository;
|
||||
|
||||
public ApiClientPageController(ServerRepository serverRepository) {
|
||||
this.serverRepository = serverRepository;
|
||||
}
|
||||
|
||||
@GetMapping("/mgmt/api_tester.do")
|
||||
public String testView(ModelMap model) {
|
||||
|
||||
List<Server> servers = serverRepository.findAllByEnabledIs(true);
|
||||
model.addAttribute("servers", servers);
|
||||
|
||||
return "page/tester/apiTester";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package com.eactive.testmaster.client.controller;
|
||||
|
||||
import com.eactive.testmaster.client.dto.ApiCollectionDTO;
|
||||
import com.eactive.testmaster.client.dto.ApiRequestDTO;
|
||||
import com.eactive.testmaster.client.entity.ApiCollection;
|
||||
import com.eactive.testmaster.client.entity.ApiRequestInfo;
|
||||
import com.eactive.testmaster.client.mapper.ApiRequestMapper;
|
||||
import com.eactive.testmaster.client.service.ApiRequestMgmtService;
|
||||
import com.eactive.testmaster.common.util.SecurityUtil;
|
||||
import com.eactive.testmaster.user.entity.StaffUser;
|
||||
import com.eactive.testmaster.user.service.UserService;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
public class ApiRequestMgmtController {
|
||||
|
||||
private static final String SUCCESS = "success";
|
||||
private final ApiRequestMgmtService apiRequestMgmtService;
|
||||
|
||||
private final ApiRequestMapper apiRequestMapper;
|
||||
|
||||
private final 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() {
|
||||
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(
|
||||
@RequestBody ApiCollectionDTO dto) {
|
||||
|
||||
ApiCollection collection = apiRequestMapper.map(dto);
|
||||
StaffUser user = userService.findByEsntlId(SecurityUtil.getCurrentUserEsntlId());
|
||||
collection.setOwner(user);
|
||||
|
||||
apiRequestMgmtService.createNewApiCollection(collection);
|
||||
return ResponseEntity.ok(SUCCESS);
|
||||
}
|
||||
|
||||
|
||||
@PostMapping("/mgmt/collections/update.do")
|
||||
public ResponseEntity<String> updateApiCollection(@RequestBody ApiCollectionDTO dto) {
|
||||
ApiCollection updated = apiRequestMapper.map(dto);
|
||||
apiRequestMgmtService.updateApiCollectionName(updated.getId(), updated);
|
||||
return ResponseEntity.ok(SUCCESS);
|
||||
}
|
||||
|
||||
@PostMapping("/mgmt/collections/delete.do")
|
||||
public ResponseEntity<String> deleteApiCollection(@RequestBody ApiCollectionDTO dto) {
|
||||
apiRequestMgmtService.deleteApiCollection(dto.getId());
|
||||
return ResponseEntity.ok(SUCCESS);
|
||||
}
|
||||
|
||||
@PostMapping("/mgmt/collections/{id}/apis/save.do")
|
||||
public ResponseEntity<String> addApiToCollection(@PathVariable(name = "id") String id,
|
||||
@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);
|
||||
}
|
||||
|
||||
@PostMapping("/mgmt/collections/{id}/apis/delete.do")
|
||||
public ResponseEntity<String> removeApiFromCollection(@PathVariable(name = "id") String id,
|
||||
@RequestBody ApiRequestDTO dto) {
|
||||
apiRequestMgmtService.removeApiFromCollection(id, dto.getId());
|
||||
return ResponseEntity.ok(SUCCESS);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package com.eactive.testmaster.client.controller;
|
||||
|
||||
import com.eactive.testmaster.client.dto.ApiScenarioDTO;
|
||||
import com.eactive.testmaster.client.entity.ApiScenario;
|
||||
import com.eactive.testmaster.client.mapper.ApiScenarioMapper;
|
||||
import com.eactive.testmaster.client.service.ApiScenarioMgmtService;
|
||||
import com.eactive.testmaster.common.util.SecurityUtil;
|
||||
import com.eactive.testmaster.user.entity.StaffUser;
|
||||
import com.eactive.testmaster.user.service.UserService;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/mgmt/api_scenario")
|
||||
public class ApiScenarioMgmtController {
|
||||
|
||||
private final ApiScenarioMgmtService apiScenarioMgmtService;
|
||||
|
||||
private final ApiScenarioMapper apiScenarioMapper;
|
||||
|
||||
private final UserService userService;
|
||||
|
||||
public ApiScenarioMgmtController(ApiScenarioMgmtService apiScenarioMgmtService, ApiScenarioMapper apiScenarioMapper, UserService userService) {
|
||||
this.apiScenarioMgmtService = apiScenarioMgmtService;
|
||||
this.apiScenarioMapper = apiScenarioMapper;
|
||||
this.userService = userService;
|
||||
}
|
||||
|
||||
@GetMapping("/list.do")
|
||||
public ResponseEntity<List<ApiScenarioDTO>> listScenarios() {
|
||||
List<ApiScenario> scenarios = apiScenarioMgmtService.findAll();
|
||||
return ResponseEntity.ok(apiScenarioMapper.toDtoList(scenarios));
|
||||
}
|
||||
|
||||
@PostMapping("/create.do")
|
||||
public ResponseEntity<ApiScenarioDTO> createScenario(@RequestBody ApiScenarioDTO apiScenarioDto) {
|
||||
ApiScenario apiScenario = apiScenarioMapper.map(apiScenarioDto);
|
||||
StaffUser user = userService.findByEsntlId(SecurityUtil.getCurrentUserEsntlId());
|
||||
apiScenario.setOwner(user);
|
||||
ApiScenario createdScenario = apiScenarioMgmtService.createApiScenario(apiScenario);
|
||||
return new ResponseEntity<>(apiScenarioMapper.map(createdScenario), HttpStatus.CREATED);
|
||||
}
|
||||
|
||||
|
||||
@PostMapping("/update.do")
|
||||
public ResponseEntity<ApiScenarioDTO> updateScenario(@RequestBody ApiScenarioDTO apiScenarioDto) {
|
||||
ApiScenario updatedApiScenario = apiScenarioMapper.map(apiScenarioDto);
|
||||
ApiScenario updatedScenario = apiScenarioMgmtService.updateApiScenario(apiScenarioDto.getId(), updatedApiScenario);
|
||||
return ResponseEntity.ok(apiScenarioMapper.map(updatedScenario));
|
||||
}
|
||||
|
||||
@PostMapping("/delete.do")
|
||||
public ResponseEntity<Void> deleteScenario(@RequestBody ApiScenarioDTO apiScenarioDto) {
|
||||
apiScenarioMgmtService.deleteApiScenario(apiScenarioDto.getId());
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.eactive.testmaster.client.dto;
|
||||
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ApiCollectionDTO {
|
||||
|
||||
private String id;
|
||||
|
||||
@NotEmpty
|
||||
private String name;
|
||||
|
||||
private List<ApiRequestDTO> apis = new ArrayList<>();
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void setApis(List<ApiRequestDTO> apis) {
|
||||
this.apis = apis;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public List<ApiRequestDTO> getApis() {
|
||||
return apis;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
package com.eactive.testmaster.client.dto;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ApiRequestDTO {
|
||||
|
||||
private String id;
|
||||
|
||||
private String name;
|
||||
|
||||
private String path;
|
||||
private String method;
|
||||
|
||||
@NotNull(message = "서버를 선택해주세요.")
|
||||
private Long server;
|
||||
|
||||
private List<ApiRequestKeyValueDTO> headers = new ArrayList<>();
|
||||
|
||||
private List<ApiRequestKeyValueDTO> queryParams = new ArrayList<>();
|
||||
|
||||
private List<ApiRequestKeyValueDTO> variables = new ArrayList<>();
|
||||
|
||||
private String requestBody;
|
||||
|
||||
private String preRequestScript;
|
||||
|
||||
private String postRequestScript;
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getPath() {
|
||||
return path;
|
||||
}
|
||||
|
||||
public void setPath(String path) {
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
public Long getServer() {
|
||||
return server;
|
||||
}
|
||||
|
||||
public void setServer(Long server) {
|
||||
this.server = server;
|
||||
}
|
||||
|
||||
public String getMethod() {
|
||||
return method;
|
||||
}
|
||||
|
||||
public void setMethod(String method) {
|
||||
this.method = method;
|
||||
}
|
||||
|
||||
public List<ApiRequestKeyValueDTO> getHeaders() {
|
||||
return headers;
|
||||
}
|
||||
|
||||
public void setHeaders(List<ApiRequestKeyValueDTO> headers) {
|
||||
this.headers = headers;
|
||||
}
|
||||
|
||||
public List<ApiRequestKeyValueDTO> getQueryParams() {
|
||||
return queryParams;
|
||||
}
|
||||
|
||||
public void setQueryParams(List<ApiRequestKeyValueDTO> queryParams) {
|
||||
this.queryParams = queryParams;
|
||||
}
|
||||
|
||||
public List<ApiRequestKeyValueDTO> getVariables() {
|
||||
return variables;
|
||||
}
|
||||
|
||||
public void setVariables(List<ApiRequestKeyValueDTO> variables) {
|
||||
this.variables = variables;
|
||||
}
|
||||
|
||||
public String getRequestBody() {
|
||||
return requestBody;
|
||||
}
|
||||
|
||||
public void setRequestBody(String requestBody) {
|
||||
this.requestBody = requestBody;
|
||||
}
|
||||
|
||||
public String getPreRequestScript() {
|
||||
return preRequestScript;
|
||||
}
|
||||
|
||||
public void setPreRequestScript(String preRequestScript) {
|
||||
this.preRequestScript = preRequestScript;
|
||||
}
|
||||
|
||||
public String getPostRequestScript() {
|
||||
return postRequestScript;
|
||||
}
|
||||
|
||||
public void setPostRequestScript(String postRequestScript) {
|
||||
this.postRequestScript = postRequestScript;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.eactive.testmaster.client.dto;
|
||||
|
||||
public class ApiRequestKeyValueDTO {
|
||||
|
||||
private boolean enabled;
|
||||
|
||||
private String key;
|
||||
private String value;
|
||||
|
||||
public boolean isEnabled() {
|
||||
return enabled;
|
||||
}
|
||||
|
||||
public void setEnabled(boolean enabled) {
|
||||
this.enabled = enabled;
|
||||
}
|
||||
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public void setKey(String key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return (enabled?"":"//")+ key + "::" + value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.eactive.testmaster.client.dto;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ApiResponse {
|
||||
|
||||
private int status;
|
||||
|
||||
private List<ApiRequestKeyValueDTO> headers;
|
||||
|
||||
private String body;
|
||||
|
||||
private int size;
|
||||
|
||||
public int getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(int status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public List<ApiRequestKeyValueDTO> getHeaders() {
|
||||
return headers;
|
||||
}
|
||||
|
||||
public void setHeaders(List<ApiRequestKeyValueDTO> headers) {
|
||||
this.headers = headers;
|
||||
}
|
||||
|
||||
public String getBody() {
|
||||
return body;
|
||||
}
|
||||
|
||||
public void setBody(String body) {
|
||||
this.body = body;
|
||||
}
|
||||
|
||||
public int getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public void setSize(int size) {
|
||||
this.size = size;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.eactive.testmaster.client.dto;
|
||||
|
||||
public class ApiScenarioDTO {
|
||||
|
||||
private String id;
|
||||
|
||||
private String name;
|
||||
|
||||
private String description;
|
||||
|
||||
private String scenario;
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public String getScenario() {
|
||||
return scenario;
|
||||
}
|
||||
|
||||
public void setScenario(String scenario) {
|
||||
this.scenario = scenario;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package com.eactive.testmaster.client.entity;
|
||||
|
||||
import com.eactive.testmaster.user.entity.StaffUser;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
@Table(name = "API_COLLECTION")
|
||||
public class ApiCollection {
|
||||
|
||||
@Id
|
||||
private String id; //uuid
|
||||
|
||||
private String name;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "USER_ID")
|
||||
private StaffUser owner;
|
||||
|
||||
@ManyToMany
|
||||
@JoinTable(
|
||||
name = "API_COLLECTION_API_REQUEST_INFO",
|
||||
joinColumns = @JoinColumn(name = "API_COLLECTION_ID"),
|
||||
inverseJoinColumns = @JoinColumn(name = "API_REQUEST_INFO_ID")
|
||||
)
|
||||
private List<ApiRequestInfo> apis = new ArrayList<>();
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public List<ApiRequestInfo> getApis() {
|
||||
return apis;
|
||||
}
|
||||
|
||||
public void setApis(List<ApiRequestInfo> apis) {
|
||||
this.apis = apis;
|
||||
}
|
||||
|
||||
public StaffUser getOwner() {
|
||||
return owner;
|
||||
}
|
||||
|
||||
public void setOwner(StaffUser owner) {
|
||||
this.owner = owner;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
package com.eactive.testmaster.client.entity;
|
||||
|
||||
import com.eactive.testmaster.server.entity.Server;
|
||||
import com.eactive.testmaster.user.entity.StaffUser;
|
||||
import org.hibernate.annotations.Type;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
public class ApiRequestHistory {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.SEQUENCE)
|
||||
private Long id;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "SERVER_ID")
|
||||
private Server server;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "USER_ID")
|
||||
private StaffUser owner;
|
||||
|
||||
private String method;
|
||||
|
||||
private String uri;
|
||||
|
||||
private String requestHeaders;
|
||||
|
||||
private String requestBody;
|
||||
|
||||
private String responseBody;
|
||||
|
||||
private String responseHeaders;
|
||||
|
||||
@Type(type = "org.hibernate.type.LocalDateTimeType")
|
||||
private LocalDateTime requestTime;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Server getServer() {
|
||||
return server;
|
||||
}
|
||||
|
||||
public void setServer(Server server) {
|
||||
this.server = server;
|
||||
}
|
||||
|
||||
public StaffUser getOwner() {
|
||||
return owner;
|
||||
}
|
||||
|
||||
public void setOwner(StaffUser owner) {
|
||||
this.owner = owner;
|
||||
}
|
||||
|
||||
public String getMethod() {
|
||||
return method;
|
||||
}
|
||||
|
||||
public void setMethod(String method) {
|
||||
this.method = method;
|
||||
}
|
||||
|
||||
public String getUri() {
|
||||
return uri;
|
||||
}
|
||||
|
||||
public void setUri(String uri) {
|
||||
this.uri = uri;
|
||||
}
|
||||
|
||||
public String getRequestHeaders() {
|
||||
return requestHeaders;
|
||||
}
|
||||
|
||||
public void setRequestHeaders(String requestHeaders) {
|
||||
this.requestHeaders = requestHeaders;
|
||||
}
|
||||
|
||||
public String getRequestBody() {
|
||||
return requestBody;
|
||||
}
|
||||
|
||||
public void setRequestBody(String requestBody) {
|
||||
this.requestBody = requestBody;
|
||||
}
|
||||
|
||||
public String getResponseBody() {
|
||||
return responseBody;
|
||||
}
|
||||
|
||||
public void setResponseBody(String responseBody) {
|
||||
this.responseBody = responseBody;
|
||||
}
|
||||
|
||||
public String getResponseHeaders() {
|
||||
return responseHeaders;
|
||||
}
|
||||
|
||||
public void setResponseHeaders(String responseHeaders) {
|
||||
this.responseHeaders = responseHeaders;
|
||||
}
|
||||
|
||||
public LocalDateTime getRequestTime() {
|
||||
return requestTime;
|
||||
}
|
||||
|
||||
public void setRequestTime(LocalDateTime requestTime) {
|
||||
this.requestTime = requestTime;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
package com.eactive.testmaster.client.entity;
|
||||
|
||||
import com.eactive.testmaster.server.entity.Server;
|
||||
import com.eactive.testmaster.user.entity.StaffUser;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.Objects;
|
||||
|
||||
|
||||
@Entity
|
||||
@Table(name = "API_REQUEST_INFO")
|
||||
public class ApiRequestInfo {
|
||||
|
||||
@Id
|
||||
private String id; //uuid
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "SERVER_ID")
|
||||
private Server server;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "USER_ID")
|
||||
private StaffUser owner;
|
||||
|
||||
private String name;
|
||||
|
||||
private String method;
|
||||
|
||||
private String path;
|
||||
|
||||
@Lob
|
||||
private String headers;
|
||||
|
||||
@Lob
|
||||
private String queryParams;
|
||||
|
||||
@Lob
|
||||
private String variables;
|
||||
|
||||
@Lob
|
||||
private String requestBody;
|
||||
|
||||
@Lob
|
||||
private String preRequestScript;
|
||||
|
||||
@Lob
|
||||
private String postRequestScript;
|
||||
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Server getServer() {
|
||||
return server;
|
||||
}
|
||||
|
||||
public void setServer(Server server) {
|
||||
this.server = server;
|
||||
}
|
||||
|
||||
public StaffUser getOwner() {
|
||||
return owner;
|
||||
}
|
||||
|
||||
public void setOwner(StaffUser owner) {
|
||||
this.owner = owner;
|
||||
}
|
||||
|
||||
public String getMethod() {
|
||||
return method;
|
||||
}
|
||||
|
||||
public void setMethod(String method) {
|
||||
this.method = method;
|
||||
}
|
||||
|
||||
public String getPath() {
|
||||
return path;
|
||||
}
|
||||
|
||||
public void setPath(String path) {
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
public String getHeaders() {
|
||||
return headers;
|
||||
}
|
||||
|
||||
public void setHeaders(String headers) {
|
||||
this.headers = headers;
|
||||
}
|
||||
|
||||
public String getQueryParams() {
|
||||
return queryParams;
|
||||
}
|
||||
|
||||
public void setQueryParams(String queryParams) {
|
||||
this.queryParams = queryParams;
|
||||
}
|
||||
|
||||
public String getVariables() {
|
||||
return variables;
|
||||
}
|
||||
|
||||
public void setVariables(String variables) {
|
||||
this.variables = variables;
|
||||
}
|
||||
|
||||
public String getRequestBody() {
|
||||
return requestBody;
|
||||
}
|
||||
|
||||
public void setRequestBody(String requestBody) {
|
||||
this.requestBody = requestBody;
|
||||
}
|
||||
|
||||
public String getPreRequestScript() {
|
||||
return preRequestScript;
|
||||
}
|
||||
|
||||
public void setPreRequestScript(String preRequestScript) {
|
||||
this.preRequestScript = preRequestScript;
|
||||
}
|
||||
|
||||
public String getPostRequestScript() {
|
||||
return postRequestScript;
|
||||
}
|
||||
|
||||
public void setPostRequestScript(String postRequestScript) {
|
||||
this.postRequestScript = postRequestScript;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
ApiRequestInfo that = (ApiRequestInfo) o;
|
||||
return Objects.equals(id, that.id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id, server, owner, method, path, headers, queryParams, requestBody, preRequestScript, postRequestScript);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.eactive.testmaster.client.entity;
|
||||
|
||||
import com.eactive.testmaster.user.entity.StaffUser;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table(name = "API_SCENARIO")
|
||||
public class ApiScenario {
|
||||
|
||||
@Id
|
||||
private String id; //uuid
|
||||
|
||||
private String name;
|
||||
|
||||
private String description;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "USER_ID")
|
||||
private StaffUser owner;
|
||||
|
||||
@Lob
|
||||
private String scenario;
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public String getScenario() {
|
||||
return scenario;
|
||||
}
|
||||
|
||||
public void setScenario(String scenario) {
|
||||
this.scenario = scenario;
|
||||
}
|
||||
|
||||
public StaffUser getOwner() {
|
||||
return owner;
|
||||
}
|
||||
|
||||
public void setOwner(StaffUser owner) {
|
||||
this.owner = owner;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.eactive.testmaster.client.mapper;
|
||||
|
||||
import com.eactive.testmaster.client.dto.ApiCollectionDTO;
|
||||
import com.eactive.testmaster.client.dto.ApiRequestDTO;
|
||||
import com.eactive.testmaster.client.entity.ApiCollection;
|
||||
import com.eactive.testmaster.client.entity.ApiRequestInfo;
|
||||
import com.eactive.testmaster.common.mapper.CommonMapper;
|
||||
import com.eactive.testmaster.server.mapper.ServerMapper;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.ReportingPolicy;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@Mapper(componentModel = "spring",
|
||||
uses = {CommonMapper.class, ServerMapper.class},
|
||||
unmappedTargetPolicy = ReportingPolicy.IGNORE)
|
||||
@Component
|
||||
public interface ApiRequestMapper {
|
||||
ApiCollection map(ApiCollectionDTO dto);
|
||||
|
||||
ApiRequestInfo map(ApiRequestDTO dto);
|
||||
|
||||
|
||||
ApiRequestDTO map(ApiRequestInfo entity);
|
||||
|
||||
|
||||
|
||||
List<ApiRequestInfo> mapApis(List<ApiRequestDTO> apis);
|
||||
|
||||
List<ApiCollectionDTO> mapCollections(List<ApiCollection> apis);
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.eactive.testmaster.client.mapper;
|
||||
|
||||
import com.eactive.testmaster.client.dto.ApiScenarioDTO;
|
||||
import com.eactive.testmaster.client.entity.ApiScenario;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.ReportingPolicy;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper(componentModel = "spring",
|
||||
unmappedTargetPolicy = ReportingPolicy.IGNORE)
|
||||
@Component
|
||||
public interface ApiScenarioMapper {
|
||||
|
||||
ApiScenario map(ApiScenarioDTO dto);
|
||||
|
||||
ApiScenarioDTO map(ApiScenario scenario);
|
||||
|
||||
List<ApiScenarioDTO> toDtoList(List<ApiScenario> scenarios);
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.eactive.testmaster.client.repository;
|
||||
|
||||
import com.eactive.testmaster.client.entity.ApiCollection;
|
||||
import com.eactive.testmaster.user.entity.StaffUser;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface ApiCollectionRepository extends JpaRepository<ApiCollection, String>, JpaSpecificationExecutor<ApiCollection> {
|
||||
|
||||
List<ApiCollection> findAllByOwner(StaffUser owner);
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.eactive.testmaster.client.repository;
|
||||
|
||||
import com.eactive.testmaster.client.entity.ApiRequestInfo;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
|
||||
public interface ApiRequestRepository extends JpaRepository<ApiRequestInfo, String>, JpaSpecificationExecutor<ApiRequestInfo> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.eactive.testmaster.client.repository;
|
||||
|
||||
import com.eactive.testmaster.client.entity.ApiScenario;
|
||||
import com.eactive.testmaster.user.entity.StaffUser;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface ApiScenarioRepository extends JpaRepository<ApiScenario, String>, JpaSpecificationExecutor<ApiScenario> {
|
||||
|
||||
List<ApiScenario> findAllByOwner(StaffUser owner);
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package com.eactive.testmaster.client.service;
|
||||
|
||||
import com.eactive.testmaster.client.entity.ApiCollection;
|
||||
import com.eactive.testmaster.client.entity.ApiRequestInfo;
|
||||
import com.eactive.testmaster.client.repository.ApiCollectionRepository;
|
||||
import com.eactive.testmaster.client.repository.ApiRequestRepository;
|
||||
import com.eactive.testmaster.common.exception.NotFoundException;
|
||||
import com.eactive.testmaster.user.entity.StaffUser;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.thymeleaf.util.StringUtils;
|
||||
|
||||
import javax.transaction.Transactional;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@Service
|
||||
@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;
|
||||
|
||||
@Autowired
|
||||
ApiRequestRepository apiRequestRepository;
|
||||
|
||||
|
||||
public List<ApiCollection> getMyApis(StaffUser user) {
|
||||
return apiCollectionRepository.findAllByOwner(user);
|
||||
}
|
||||
|
||||
public void deleteApiCollection(String id) {
|
||||
ApiCollection collection = apiCollectionRepository.findById(id).orElseThrow(() -> new NotFoundException(String.format(API_COLLECTION_NOT_FOUND, id)));
|
||||
|
||||
if (!collection.getApis().isEmpty()) {
|
||||
throw new RuntimeException("api collection[" + id + "] is not empty");
|
||||
}
|
||||
apiCollectionRepository.deleteById(id);
|
||||
}
|
||||
|
||||
public void createNewApiCollection(ApiCollection collection) {
|
||||
collection.setId(UUID.randomUUID().toString());
|
||||
apiCollectionRepository.save(collection);
|
||||
}
|
||||
|
||||
public void updateApiCollectionName(String id, ApiCollection updated) {
|
||||
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(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(String.format(API_NOT_FOUND, apiId)));
|
||||
existing.setName(api.getName());
|
||||
existing.setMethod(api.getMethod());
|
||||
existing.setPath(api.getPath());
|
||||
existing.setHeaders(api.getHeaders());
|
||||
existing.setQueryParams(api.getQueryParams());
|
||||
existing.setVariables(api.getVariables());
|
||||
existing.setServer(api.getServer());
|
||||
existing.setRequestBody(api.getRequestBody());
|
||||
existing.setPreRequestScript(api.getPreRequestScript());
|
||||
existing.setPostRequestScript(api.getPostRequestScript());
|
||||
|
||||
apiRequestRepository.save(existing);
|
||||
api = existing;
|
||||
}
|
||||
|
||||
|
||||
if (!collection.getApis().contains(api)) {
|
||||
collection.getApis().add(api);
|
||||
apiCollectionRepository.save(collection);
|
||||
}
|
||||
|
||||
return api.getId();
|
||||
}
|
||||
|
||||
public void removeApiFromCollection(String id, String apiId) {
|
||||
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);
|
||||
apiRequestRepository.deleteById(apiId);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.eactive.testmaster.client.service;
|
||||
|
||||
import com.eactive.testmaster.client.repository.ApiRequestRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import javax.transaction.Transactional;
|
||||
import java.sql.Connection;
|
||||
import java.sql.Statement;
|
||||
|
||||
|
||||
@Service
|
||||
@Transactional
|
||||
public class ApiRequestMigrationService {
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
@Autowired
|
||||
ApiRequestRepository apiRequestRepository;
|
||||
|
||||
@EventListener(ApplicationReadyEvent.class)
|
||||
public void migrate() {
|
||||
// Fetch DataSource from Spring Context
|
||||
DataSource dataSource = applicationContext.getBean(DataSource.class);
|
||||
|
||||
try (Connection connection = dataSource.getConnection()) {
|
||||
String sql1 = String.format("ALTER TABLE %s ALTER COLUMN %s %s;", "API_REQUEST_INFO", "HEADERS", "CLOB");
|
||||
try (Statement statement = connection.createStatement()) {
|
||||
statement.execute(sql1);
|
||||
}
|
||||
String sql2 = String.format("ALTER TABLE %s ALTER COLUMN %s %s;", "API_REQUEST_INFO", "QUERY_PARAMS", "CLOB");
|
||||
try (Statement statement = connection.createStatement()) {
|
||||
statement.execute(sql2);
|
||||
}
|
||||
String sql3 = String.format("ALTER TABLE %s ALTER COLUMN %s %s;", "API_REQUEST_INFO", "REQUEST_BODY", "CLOB");
|
||||
try (Statement statement = connection.createStatement()) {
|
||||
statement.execute(sql3);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
apiRequestRepository.findAll().forEach(apiRequest -> {
|
||||
|
||||
apiRequest.setHeaders(apiRequest.getHeaders().replace("=", "::"));
|
||||
apiRequest.setQueryParams(apiRequest.getQueryParams().replace("=", "::"));
|
||||
|
||||
apiRequestRepository.save(apiRequest);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.eactive.testmaster.client.service;
|
||||
|
||||
import com.eactive.testmaster.client.entity.ApiScenario;
|
||||
import com.eactive.testmaster.client.repository.ApiScenarioRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.transaction.Transactional;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@Service
|
||||
@Transactional
|
||||
public class ApiScenarioMgmtService {
|
||||
|
||||
@Autowired
|
||||
ApiScenarioRepository apiScenarioRepository;
|
||||
|
||||
|
||||
public List<ApiScenario> findAll() {
|
||||
return apiScenarioRepository.findAll();
|
||||
}
|
||||
|
||||
public ApiScenario createApiScenario(ApiScenario apiScenario) {
|
||||
apiScenario.setId(UUID.randomUUID().toString());
|
||||
return apiScenarioRepository.save(apiScenario);
|
||||
}
|
||||
|
||||
|
||||
public ApiScenario updateApiScenario(String id, ApiScenario updatedApiScenario) {
|
||||
return apiScenarioRepository.findById(id)
|
||||
.map(apiScenario -> {
|
||||
apiScenario.setName(updatedApiScenario.getName());
|
||||
apiScenario.setDescription(updatedApiScenario.getDescription());
|
||||
apiScenario.setScenario(updatedApiScenario.getScenario());
|
||||
return apiScenarioRepository.save(apiScenario);
|
||||
}).orElseGet(() -> {
|
||||
// If the ApiScenario with the given id doesn't exist, create a new one
|
||||
updatedApiScenario.setId(id);
|
||||
return apiScenarioRepository.save(updatedApiScenario);
|
||||
});
|
||||
}
|
||||
|
||||
// Delete operation
|
||||
public void deleteApiScenario(String id) {
|
||||
apiScenarioRepository.deleteById(id);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package com.eactive.testmaster.client.service;
|
||||
|
||||
import com.eactive.testmaster.client.dto.ApiRequestKeyValueDTO;
|
||||
import com.eactive.testmaster.client.dto.ApiResponse;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.SimpleChannelInboundHandler;
|
||||
import io.netty.handler.codec.http.FullHttpResponse;
|
||||
import io.netty.handler.codec.http.HttpHeaders;
|
||||
import io.netty.util.CharsetUtil;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public class HttpHandler extends SimpleChannelInboundHandler<FullHttpResponse> {
|
||||
|
||||
private final CompletableFuture<ApiResponse> responseFuture;
|
||||
|
||||
public HttpHandler(CompletableFuture<ApiResponse> responseFuture) {
|
||||
this.responseFuture = responseFuture;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void channelRead0(ChannelHandlerContext ctx, FullHttpResponse response) {
|
||||
|
||||
ApiResponse apiResponse = new ApiResponse();
|
||||
apiResponse.setStatus(response.status().code());
|
||||
apiResponse.setHeaders(convertHttpHeadersToList(response.headers()));
|
||||
apiResponse.setBody(response.content().toString(CharsetUtil.UTF_8));
|
||||
|
||||
int headerSize = calculateHeaderSize(response.headers());
|
||||
int contentSize = response.content().readableBytes();
|
||||
int totalSize = headerSize + contentSize;
|
||||
apiResponse.setSize(totalSize);
|
||||
|
||||
responseFuture.complete(apiResponse);
|
||||
}
|
||||
|
||||
private int calculateHeaderSize(HttpHeaders headers) {
|
||||
int headerSize = 0;
|
||||
for (Map.Entry<String, String> entry : headers.entries()) {
|
||||
headerSize += entry.getKey().length() + entry.getValue().length() + 4; // Add 2 for ': ' and 2 for '\r\n'
|
||||
}
|
||||
|
||||
// Add 2 for the final '\r\n' after the headers
|
||||
headerSize += 2;
|
||||
|
||||
return headerSize;
|
||||
}
|
||||
|
||||
public List<ApiRequestKeyValueDTO> convertHttpHeadersToList(HttpHeaders headers) {
|
||||
List<ApiRequestKeyValueDTO> headerList = new ArrayList<>();
|
||||
for (Map.Entry<String, String> entry : headers.entries()) {
|
||||
ApiRequestKeyValueDTO header = new ApiRequestKeyValueDTO();
|
||||
header.setKey(entry.getKey());
|
||||
header.setValue(entry.getValue());
|
||||
headerList.add(header);
|
||||
}
|
||||
|
||||
return headerList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
|
||||
responseFuture.completeExceptionally(cause);
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
package com.eactive.testmaster.client.service;
|
||||
|
||||
import com.eactive.testmaster.client.dto.ApiRequestDTO;
|
||||
import com.eactive.testmaster.client.dto.ApiRequestKeyValueDTO;
|
||||
import com.eactive.testmaster.client.dto.ApiResponse;
|
||||
import com.eactive.testmaster.common.exception.ServerNotFoundException;
|
||||
import com.eactive.testmaster.server.entity.Server;
|
||||
import com.eactive.testmaster.server.entity.ServerRepository;
|
||||
import io.netty.bootstrap.Bootstrap;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelInitializer;
|
||||
import io.netty.channel.ChannelOption;
|
||||
import io.netty.channel.EventLoopGroup;
|
||||
import io.netty.channel.nio.NioEventLoopGroup;
|
||||
import io.netty.channel.socket.nio.NioSocketChannel;
|
||||
import io.netty.handler.codec.http.*;
|
||||
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.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.net.ssl.SSLException;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@Service
|
||||
public class NettyApiClient {
|
||||
|
||||
@Autowired
|
||||
ServerRepository serverRepository;
|
||||
|
||||
public CompletableFuture<ApiResponse> handleRequest(ApiRequestDTO originalRequest) {
|
||||
ApiRequestDTO apiRequest = originalRequest;
|
||||
Server server = serverRepository.findById(apiRequest.getServer()).orElseThrow(() -> new ServerNotFoundException("Server not found"));
|
||||
String host = server.getHostname();
|
||||
int port = server.getPort();
|
||||
|
||||
CompletableFuture<ApiResponse> responseFuture = new CompletableFuture<>();
|
||||
EventLoopGroup group = new NioEventLoopGroup();
|
||||
try {
|
||||
Bootstrap b = new Bootstrap()
|
||||
.group(group)
|
||||
.channel(NioSocketChannel.class)
|
||||
.option(ChannelOption.TCP_NODELAY, true)
|
||||
.handler(new ChannelInitializer<Channel>() {
|
||||
@Override
|
||||
public void initChannel(Channel ch) throws SSLException {
|
||||
if (server.getScheme().equalsIgnoreCase("https")) {
|
||||
final SslContext sslCtx = SslContextBuilder.forClient()
|
||||
.trustManager(InsecureTrustManagerFactory.INSTANCE).build();
|
||||
ch.pipeline().addLast(sslCtx.newHandler(ch.alloc(), host, port));// Add SSL handler
|
||||
}
|
||||
|
||||
ch.pipeline().addLast(new HttpClientCodec());
|
||||
ch.pipeline().addLast(new HttpObjectAggregator(65536));
|
||||
ch.pipeline().addLast(new HttpContentDecompressor());
|
||||
ch.pipeline().addLast(new HttpHandler(responseFuture));
|
||||
}
|
||||
});
|
||||
|
||||
Channel ch = b.connect(host, port).sync().channel();
|
||||
ByteBuf content = null;
|
||||
if (StringUtils.isNotEmpty(apiRequest.getRequestBody())) {
|
||||
content = Unpooled.copiedBuffer(apiRequest.getRequestBody(), CharsetUtil.UTF_8);
|
||||
} else {
|
||||
content = Unpooled.EMPTY_BUFFER;
|
||||
}
|
||||
|
||||
String fullPath = server.getBasePath() + apiRequest.getPath();
|
||||
|
||||
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);
|
||||
request.headers().set("Accept-Encoding", HttpHeaderValues.GZIP);
|
||||
|
||||
if (StringUtils.isNotEmpty(apiRequest.getRequestBody())) {
|
||||
request.headers().set(HttpHeaderNames.CONTENT_LENGTH, content.readableBytes());
|
||||
}
|
||||
|
||||
if (!apiRequest.getHeaders().isEmpty()) {
|
||||
HttpHeaders customHeaders = new DefaultHttpHeaders();
|
||||
apiRequest.getHeaders().stream().filter(ApiRequestKeyValueDTO::isEnabled).forEach(header -> {
|
||||
if (StringUtils.isNotEmpty(header.getKey()) && StringUtils.isNotEmpty(header.getValue())) {
|
||||
customHeaders.set(header.getKey(), header.getValue());
|
||||
}
|
||||
});
|
||||
request.headers().add(customHeaders);
|
||||
}
|
||||
|
||||
ch.writeAndFlush(request);
|
||||
ch.closeFuture().sync();
|
||||
} catch (Exception e) {
|
||||
ApiResponse response = new ApiResponse();
|
||||
response.setBody("An error occurred: " + e.getMessage());
|
||||
return CompletableFuture.completedFuture(response);
|
||||
} finally {
|
||||
group.shutdownGracefully();
|
||||
}
|
||||
|
||||
return responseFuture;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user