276 lines
11 KiB
Java
276 lines
11 KiB
Java
package com.example.processor;
|
|
|
|
import com.example.model.AdvancedSearchRequest;
|
|
import com.example.model.PageRequest;
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.serializer.SerializeConfig;
|
|
import com.alibaba.fastjson.serializer.SerializerFeature;
|
|
|
|
import org.apache.camel.Exchange;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import java.util.Date;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
/**
|
|
* 统一请求处理器
|
|
* 适用于Camel Karavan低代码平台配置
|
|
* 通用处理器,不依赖于特定实体类型
|
|
*/
|
|
public class RequestProcessor {
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(RequestProcessor.class);
|
|
private final SerializeConfig serializeConfig;
|
|
|
|
public RequestProcessor() {
|
|
// 配置Fastjson序列化
|
|
serializeConfig = new SerializeConfig();
|
|
// 配置日期序列化为时间戳
|
|
serializeConfig.put(Date.class, (serializer, object, fieldName, fieldType, features) -> {
|
|
serializer.write(((Date) object).getTime());
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 从请求头中提取ID参数
|
|
*/
|
|
public Long extractId(Exchange exchange) {
|
|
String idStr = exchange.getIn().getHeader("id", String.class);
|
|
return Long.parseLong(idStr);
|
|
}
|
|
|
|
/**
|
|
* 从请求体解析分页参数
|
|
*/
|
|
public Map<String, Object> extractPageCountParams(Exchange exchange) throws Exception {
|
|
Object body = exchange.getIn().getBody();
|
|
PageRequest pageRequest = null;
|
|
if(body instanceof String){
|
|
String bodyStr = (String) body;
|
|
LOGGER.info("分页拿到的请求body: {}", bodyStr);
|
|
pageRequest = JSON.parseObject(bodyStr, PageRequest.class);
|
|
// 保存请求对象供后续使用
|
|
exchange.setProperty("pageRequest", pageRequest);
|
|
|
|
}else if(body instanceof Map){
|
|
@SuppressWarnings("unchecked")
|
|
Map<String, Object> bodyMap = (Map<String, Object>) body;
|
|
LOGGER.info("分页拿到的请求body: {}", bodyMap);
|
|
pageRequest = JSONObject.toJavaObject(new JSONObject(bodyMap), PageRequest.class);
|
|
}else{
|
|
// 处理其他类型或null
|
|
LOGGER.warn("无法处理的请求体类型: {}", body != null ? body.getClass().getName() : "null");
|
|
pageRequest = new PageRequest();
|
|
pageRequest.setPage(1);
|
|
pageRequest.setSize(10);
|
|
}
|
|
// 保存请求对象供后续使用
|
|
exchange.setProperty("pageRequest", pageRequest);
|
|
|
|
Map<String, Object> params = new HashMap<>();
|
|
params.put("offset", pageRequest.getOffset());
|
|
params.put("limit", pageRequest.getLimit());
|
|
params.put("filters", pageRequest.getFilters());
|
|
params.put("sortField", pageRequest.getSortField());
|
|
params.put("sortOrder", pageRequest.getSortOrder());
|
|
|
|
return params;
|
|
}
|
|
|
|
public Map<String, Object> extractPageParams(Exchange exchange) throws Exception {
|
|
Map<String, Object> params = new HashMap<>();
|
|
PageRequest pageRequest = (PageRequest) exchange.getProperty("pageRequest");
|
|
if(pageRequest == null){
|
|
throw new Exception("分页请求对象为空");
|
|
}
|
|
params.put("offset", pageRequest.getOffset());
|
|
params.put("limit", pageRequest.getLimit());
|
|
params.put("filters", pageRequest.getFilters());
|
|
params.put("sortField", pageRequest.getSortField());
|
|
params.put("sortOrder", pageRequest.getSortOrder());
|
|
return params;
|
|
}
|
|
|
|
/**
|
|
* 从请求体解析高级搜索数量参数
|
|
*/
|
|
public Map<String, Object> extractSearchCountParams(Exchange exchange) throws Exception {
|
|
Object body = exchange.getIn().getBody();
|
|
AdvancedSearchRequest searchRequest = null;
|
|
if(body instanceof String){
|
|
String bodyStr = (String) body;
|
|
LOGGER.info("高级搜索拿到的请求body: {}", bodyStr);
|
|
searchRequest = JSON.parseObject(bodyStr, AdvancedSearchRequest.class);
|
|
}else if(body instanceof Map){
|
|
@SuppressWarnings("unchecked")
|
|
Map<String, Object> bodyMap = (Map<String, Object>) body;
|
|
LOGGER.info("高级搜索拿到的请求body: {}", bodyMap);
|
|
searchRequest = JSONObject.toJavaObject(new JSONObject(bodyMap), AdvancedSearchRequest.class);
|
|
}else if(body instanceof JSONObject){
|
|
searchRequest = JSONObject.toJavaObject((JSONObject)body, AdvancedSearchRequest.class);
|
|
}else{
|
|
LOGGER.warn("无法处理的请求体类型: {}", body != null ? body.getClass().getName() : "null");
|
|
searchRequest = new AdvancedSearchRequest();
|
|
}
|
|
|
|
// 保存请求对象供后续使用
|
|
exchange.setProperty("searchRequest", searchRequest);
|
|
|
|
// 使用日期序列化为时间戳的配置序列化对象
|
|
String searchRequestStr = JSON.toJSONString(searchRequest, serializeConfig, SerializerFeature.WriteMapNullValue);
|
|
@SuppressWarnings("unchecked")
|
|
Map<String, Object> params = JSON.parseObject(searchRequestStr, Map.class);
|
|
|
|
return params;
|
|
}
|
|
|
|
/**
|
|
* 从请求体解析高级搜索参数
|
|
*/
|
|
public Map<String, Object> extractSearchParams(Exchange exchange) throws Exception {
|
|
Map<String, Object> params = new HashMap<>();
|
|
AdvancedSearchRequest searchRequest = (AdvancedSearchRequest) exchange.getProperty("searchRequest");
|
|
if(searchRequest == null){
|
|
throw new Exception("高级搜索请求对象为空");
|
|
}
|
|
params.put("offset", searchRequest.getOffset());
|
|
params.put("limit", searchRequest.getLimit());
|
|
params.put("criteria", searchRequest.getCriteria());
|
|
params.put("sortField", searchRequest.getSortField());
|
|
params.put("sortOrder", searchRequest.getSortOrder());
|
|
|
|
return params;
|
|
}
|
|
|
|
/**
|
|
* 准备实体创建参数,通用方法
|
|
* @param <T> 实体类型
|
|
* @param exchange Camel交换对象
|
|
* @param entityClass 实体类Class
|
|
* @return 准备好的实体对象
|
|
*/
|
|
public <T> T prepareEntity(Exchange exchange, Class<T> entityClass) throws Exception {
|
|
String body = exchange.getIn().getBody(String.class);
|
|
T entity = JSON.parseObject(body, entityClass);
|
|
|
|
// 通过反射设置创建和更新时间(如果实体有这些字段)
|
|
try {
|
|
// 尝试设置创建时间
|
|
try {
|
|
java.lang.reflect.Method setCreatedAt = entity.getClass().getMethod("setCreatedAt", Date.class);
|
|
if (setCreatedAt != null) {
|
|
setCreatedAt.invoke(entity, new Date());
|
|
}
|
|
} catch (NoSuchMethodException e) {
|
|
// 实体没有这个方法,忽略
|
|
}
|
|
|
|
// 尝试设置更新时间
|
|
try {
|
|
java.lang.reflect.Method setUpdatedAt = entity.getClass().getMethod("setUpdatedAt", Date.class);
|
|
if (setUpdatedAt != null) {
|
|
setUpdatedAt.invoke(entity, new Date());
|
|
}
|
|
} catch (NoSuchMethodException e) {
|
|
// 实体没有这个方法,忽略
|
|
}
|
|
|
|
// 尝试设置默认激活状态
|
|
try {
|
|
java.lang.reflect.Method getActive = entity.getClass().getMethod("getActive");
|
|
java.lang.reflect.Method setActive = entity.getClass().getMethod("setActive", Boolean.class);
|
|
|
|
if (getActive != null && setActive != null) {
|
|
Boolean active = (Boolean) getActive.invoke(entity);
|
|
if (active == null) {
|
|
setActive.invoke(entity, true);
|
|
}
|
|
}
|
|
} catch (NoSuchMethodException e) {
|
|
// 实体没有这些方法,忽略
|
|
}
|
|
} catch (Exception e) {
|
|
LOGGER.warn("设置实体属性时出错", e);
|
|
}
|
|
|
|
return entity;
|
|
}
|
|
|
|
/**
|
|
* 准备实体更新参数,通用方法
|
|
* @param <T> 实体类型
|
|
* @param exchange Camel交换对象
|
|
* @param entityClass 实体类Class
|
|
* @return 准备好的实体对象
|
|
*/
|
|
public <T> T prepareEntityWithId(Exchange exchange, Class<T> entityClass) throws Exception {
|
|
String idStr = exchange.getIn().getHeader("id", String.class);
|
|
Long id = Long.parseLong(idStr);
|
|
|
|
String body = exchange.getIn().getBody(String.class);
|
|
T entity = JSON.parseObject(body, entityClass);
|
|
|
|
// 通过反射设置ID和更新时间
|
|
try {
|
|
// 设置ID
|
|
try {
|
|
java.lang.reflect.Method setId = entity.getClass().getMethod("setId", Long.class);
|
|
if (setId != null) {
|
|
setId.invoke(entity, id);
|
|
}
|
|
} catch (NoSuchMethodException e) {
|
|
LOGGER.warn("实体类 {} 没有 setId 方法", entityClass.getName());
|
|
}
|
|
|
|
// 设置更新时间
|
|
try {
|
|
java.lang.reflect.Method setUpdatedAt = entity.getClass().getMethod("setUpdatedAt", Date.class);
|
|
if (setUpdatedAt != null) {
|
|
setUpdatedAt.invoke(entity, new Date());
|
|
}
|
|
} catch (NoSuchMethodException e) {
|
|
// 实体没有这个方法,忽略
|
|
}
|
|
} catch (Exception e) {
|
|
LOGGER.warn("设置实体属性时出错", e);
|
|
}
|
|
|
|
return entity;
|
|
}
|
|
|
|
/**
|
|
* 创建实体不存在的错误响应
|
|
*/
|
|
public Map<String, String> createNotFoundResponse() {
|
|
Map<String, String> response = new HashMap<>();
|
|
response.put("error", "记录不存在");
|
|
return response;
|
|
}
|
|
|
|
/**
|
|
* 创建不存在的错误响应(指定实体名称)
|
|
*/
|
|
public Map<String, String> createNotFoundResponse(String entityName) {
|
|
Map<String, String> response = new HashMap<>();
|
|
response.put("error", entityName + "不存在");
|
|
return response;
|
|
}
|
|
|
|
/**
|
|
* 创建冲突的错误响应
|
|
*/
|
|
public Map<String, String> createConflictResponse(String field) {
|
|
Map<String, String> response = new HashMap<>();
|
|
response.put("error", field + "已存在");
|
|
return response;
|
|
}
|
|
|
|
/**
|
|
* 设置HTTP状态码
|
|
*/
|
|
public void setStatusCode(Exchange exchange, int code) {
|
|
exchange.getIn().setHeader(Exchange.HTTP_RESPONSE_CODE, code);
|
|
}
|
|
} |