Commit 5c4be730 by inrgihc

反馈问题修复

parent 2674df7a
......@@ -9,14 +9,20 @@
/////////////////////////////////////////////////////////////
package com.gitee.sqlrest.common.exception;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class CommonException extends RuntimeException {
private ResponseErrorCode code;
private String message;
public CommonException(ResponseErrorCode code, String message) {
super(message);
this.code = code;
}
public CommonException(ResponseErrorCode code, Throwable cause) {
super(cause);
this.code = code;
}
}
......@@ -9,7 +9,6 @@
/////////////////////////////////////////////////////////////
package com.gitee.sqlrest.core.exec;
import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.gitee.sqlrest.cache.CacheFactory;
import com.gitee.sqlrest.cache.DistributedCache;
......@@ -87,12 +86,9 @@ public class ApiExecuteService {
throw new CommonException(ResponseErrorCode.ERROR_INVALID_ARGUMENT, convertInvalidArgs(invalidArgs));
}
return execute(config, paramValues);
} catch (CommonException e) {
log.warn("Failed to valid parameters for {}, error:{}", resourceName, e.getMessage());
return ResultEntity.failed(e.getCode(), e.getMessage());
} catch (Throwable t) {
log.warn("Failed to execute for {}, error:{}", resourceName, t.getMessage());
return ResultEntity.failed(ResponseErrorCode.ERROR_INTERNAL_ERROR, ExceptionUtil.getMessage(t));
} catch (IOException e) {
log.warn("Failed read input body parameters for {}, error:{}", resourceName, e.getMessage());
throw new CommonException(ResponseErrorCode.ERROR_INTERNAL_ERROR, e);
}
}
......
......@@ -111,6 +111,9 @@ public class UnifyAlarmOpsService {
private ResponseEntity<String> handleAlarm(UnifyAlarmEntity config, Map<String, String> dataModel) {
StrSubstitutor strSubstitutor = new StrSubstitutor(escape(dataModel));
String bodyStr = strSubstitutor.replace(config.getInputTemplate());
if (log.isDebugEnabled()) {
log.debug("Send alarm message body:\n{}", bodyStr);
}
ResponseEntity<String> ret = sentAlarm(config, bodyStr);
if (ret.getStatusCodeValue() != HttpStatus.OK.value()) {
log.warn("Error when send alarm message http status: {} ,body: {}", ret.getStatusCode(), ret.getBody());
......@@ -124,7 +127,7 @@ public class UnifyAlarmOpsService {
HttpHeaders headers = new HttpHeaders();
MediaType type = MediaType.parseMediaType(config.getContentType().replace(";", "") + "; charset=UTF-8");
headers.setContentType(type);
headers.add("Accept", MediaType.ALL_VALUE.toString());
headers.add("Accept", MediaType.ALL_VALUE);
HttpEntity<String> httpEntity = new HttpEntity<>(bodyStr, headers);
return restTemplate.exchange(config.getEndpoint(), HttpMethod.POST, httpEntity, String.class);
}
......
......@@ -171,12 +171,12 @@ public class AuthenticationFilter implements Filter {
dataModel.put("method", apiConfigEntity.getMethod().name());
dataModel.put("contentType", apiConfigEntity.getContentType());
dataModel.put("name", apiConfigEntity.getName());
dataModel.put("description", apiConfigEntity.getDescription());
dataModel.put("description", StringUtils.defaultString(apiConfigEntity.getDescription()));
dataModel.put("open", apiConfigEntity.getOpen().toString());
dataModel.put("clientKey", accessRecord.getClientKey());
dataModel.put("ipAddr", accessRecord.getIpAddr());
dataModel.put("userAgent", accessRecord.getUserAgent());
dataModel.put("exception", accessRecord.getException());
dataModel.put("clientKey", StringUtils.defaultString(accessRecord.getClientKey()));
dataModel.put("ipAddr", StringUtils.defaultString(accessRecord.getIpAddr()));
dataModel.put("userAgent", StringUtils.defaultString(accessRecord.getUserAgent()));
dataModel.put("exception", StringUtils.defaultString(accessRecord.getException()));
dataModel.put("accessTime", sdFormatter.format(new Date(accessTimestamp)));
unifyAlarmOpsService.triggerAlarm(dataModel);
......
......@@ -38,9 +38,6 @@ public class ApiServletService {
String path = request.getRequestURI().substring(Constants.API_PATH_PREFIX.length() + 2);
ApiAssignmentEntity apiConfigEntity = apiAssignmentDao.getByUk(method, path);
ResultEntity result = apiExecuteService.execute(apiConfigEntity, request);
if (0 != result.getCode()) {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
String json = JacksonUtils.toJsonStr(result, apiConfigEntity.getResponseFormat());
response.getWriter().append(json);
}
......
......@@ -12,6 +12,7 @@ package com.gitee.sqlrest.core.servlet;
import com.gitee.sqlrest.common.consts.Constants;
import com.gitee.sqlrest.common.dto.BaseParam;
import com.gitee.sqlrest.common.dto.ItemParam;
import com.gitee.sqlrest.common.dto.OutParam;
import com.gitee.sqlrest.common.enums.HttpMethodEnum;
import com.gitee.sqlrest.common.enums.ParamTypeEnum;
import com.gitee.sqlrest.persistence.dao.ApiAssignmentDao;
......@@ -248,7 +249,7 @@ public class ApiSwaggerService {
}
// 响应
operation.setResponses(getApiResponses());
operation.setResponses(getApiResponses(assignment.getOutputs()));
operation.security(Collections.singletonList(new SecurityRequirement().addList(AUTHORIZATION)));
openAPI.path(path, pathItem);
......@@ -326,16 +327,54 @@ public class ApiSwaggerService {
return pathItem;
}
private ApiResponses getApiResponses() {
private ApiResponses getApiResponses(List<OutParam> outputs) {
Schema rootSchema = new ObjectSchema()
.addProperties("code", new NumberSchema())
.addProperties("message", new StringSchema());
if (!CollectionUtils.isEmpty(outputs)) {
ObjectSchema objectSchema = new ObjectSchema();
for (OutParam param : outputs) {
ParamTypeEnum typeItem = param.getType();
if (Optional.ofNullable(param.getIsArray()).orElse(false)) {
Schema subSchema = new Schema().type(param.getType().getJsType())
.description(param.getRemark())
.format(getTypeFormat(param.getType()));
ArraySchema subArraySchema = new ArraySchema().items(subSchema);
objectSchema.addProperties(param.getName(), subArraySchema);
} else {
Schema propertiesItem;
if (Optional.ofNullable(typeItem.isObject()).orElse(false)) {
propertiesItem = new ObjectSchema().description(param.getRemark());
if (!CollectionUtils.isEmpty(param.getChildren())) {
for (OutParam subParam : param.getChildren()) {
Schema subSchema = new Schema().type(subParam.getType().getJsType())
.description(subParam.getRemark())
.format(getTypeFormat(subParam.getType()));
if (Optional.ofNullable(subParam.getIsArray()).orElse(false)) {
ArraySchema subArraySchema = new ArraySchema().items(subSchema);
propertiesItem.addProperties(subParam.getName(), subArraySchema);
} else {
propertiesItem.addProperties(subParam.getName(), subSchema);
}
}
}
} else {
propertiesItem = new Schema().type(typeItem.getJsType())
.description(param.getRemark())
.format(getTypeFormat(typeItem));
}
objectSchema.addProperties(param.getName(), propertiesItem);
}
}
rootSchema.addProperties("data", objectSchema);
}
ApiResponses apiResponses = new ApiResponses();
apiResponses.addApiResponse("200",
new ApiResponse().description("OK").content(
new Content().addMediaType(
"*/*",
new MediaType().schema(
new Schema().name("type")
.type("object")
)
new MediaType().schema(rootSchema)
)
)
);
......@@ -353,4 +392,26 @@ public class ApiSwaggerService {
);
return apiResponses;
}
private String getTypeFormat(ParamTypeEnum type) {
String format = null;
switch (type) {
case LONG:
format = "int64";
break;
case DOUBLE:
format = "float32";
break;
case DATE:
format = "date-time";
break;
case TIME:
format = "time";
break;
case BOOLEAN:
default:
break;
}
return format;
}
}
......@@ -9,6 +9,7 @@
/////////////////////////////////////////////////////////////
package com.gitee.sqlrest.manager.model;
import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
......@@ -116,13 +117,13 @@ public class McpToolCallHandler {
public CallToolResult executeTool(McpSyncServerExchange exchange, Map<String, Object> arguments) {
prepareArgumentsPageSizeParameter(arguments);
try {
ResultEntity<Object> resultEntity = apiExecuteService.execute(config, arguments);
if (0 == resultEntity.getCode()) {
String json = JacksonUtils.toJsonStr(resultEntity.getData(), config.getResponseFormat());
McpSchema.TextContent content = new McpSchema.TextContent("操作成功,JSON格式的响应数据为:\n " + json);
return new McpSchema.CallToolResult(Lists.newArrayList(content), false);
} else {
String message = resultEntity.getMessage();
} catch (Throwable t) {
String message = null != t.getMessage() ? t.getMessage() : ExceptionUtil.getRootCauseMessage(t);
McpSchema.TextContent content = new McpSchema.TextContent("操作异常,JSON格式的响应数据为:\n " + message);
return new McpSchema.CallToolResult(Lists.newArrayList(content), true);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment