test master 변경
This commit is contained in:
@@ -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