|
@@ -17,13 +17,6 @@ import java.util.regex.Pattern;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
|
-import com.chelvc.framework.base.context.JacksonContextHolder;
|
|
|
-import com.chelvc.framework.base.context.SessionContextHolder;
|
|
|
-import com.chelvc.framework.base.model.Platform;
|
|
|
-import com.chelvc.framework.base.model.Result;
|
|
|
-import com.chelvc.framework.base.model.Session;
|
|
|
-import com.chelvc.framework.base.model.Terminal;
|
|
|
-import com.fasterxml.jackson.core.JsonEncoding;
|
|
|
import com.google.common.collect.Maps;
|
|
|
import lombok.NonNull;
|
|
|
import org.springframework.http.HttpStatus;
|
|
@@ -36,11 +29,6 @@ import org.springframework.http.MediaType;
|
|
|
* @date 2023/4/5
|
|
|
*/
|
|
|
public final class HttpUtils {
|
|
|
- /**
|
|
|
- * 请求结果统一封装响应头
|
|
|
- */
|
|
|
- public static final String UNIFIED_RESPONSE_HEADER = "Unified-Response-Body";
|
|
|
-
|
|
|
/**
|
|
|
* 资源分隔符匹配模式
|
|
|
*/
|
|
@@ -165,35 +153,26 @@ public final class HttpUtils {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 初始化请求结果统一封装响应头
|
|
|
- *
|
|
|
- * @param response Http响应对象
|
|
|
- */
|
|
|
- public static void initializeUnifiedResponseHeader(@NonNull HttpServletResponse response) {
|
|
|
- response.setHeader(UNIFIED_RESPONSE_HEADER, String.valueOf(true));
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 响应文件流
|
|
|
+ * 响应文件
|
|
|
*
|
|
|
* @param response Http响应对象
|
|
|
* @param file 文件对象
|
|
|
* @throws IOException I/O异常
|
|
|
*/
|
|
|
- public static void writeFileResponse(@NonNull HttpServletResponse response, @NonNull File file) throws IOException {
|
|
|
- writeFileResponse(response, file, file.getName());
|
|
|
+ public static void write(@NonNull HttpServletResponse response, @NonNull File file) throws IOException {
|
|
|
+ write(response, file, file.getName());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 响应文件流
|
|
|
+ * 响应文件
|
|
|
*
|
|
|
* @param response Http响应对象
|
|
|
* @param file 文件对象
|
|
|
* @param filename 文件名称
|
|
|
* @throws IOException I/O异常
|
|
|
*/
|
|
|
- public static void writeFileResponse(@NonNull HttpServletResponse response, @NonNull File file,
|
|
|
- @NonNull String filename) throws IOException {
|
|
|
+ public static void write(@NonNull HttpServletResponse response, @NonNull File file, @NonNull String filename)
|
|
|
+ throws IOException {
|
|
|
// 设置响应头
|
|
|
response.reset();
|
|
|
response.addHeader("Content-Length", String.valueOf(file.length()));
|
|
@@ -213,37 +192,18 @@ public final class HttpUtils {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 返回统一封装请求结果
|
|
|
- *
|
|
|
- * @param response Http响应对象
|
|
|
- * @param result 请求结果
|
|
|
- * @throws IOException I/O异常
|
|
|
- */
|
|
|
- public static void writeUnifiedResponse(@NonNull HttpServletResponse response, @NonNull Result<?> result)
|
|
|
- throws IOException {
|
|
|
- response.setStatus(result.getCode());
|
|
|
- response.setContentType(MediaType.APPLICATION_JSON_VALUE);
|
|
|
- response.setCharacterEncoding(JsonEncoding.UTF8.getJavaName());
|
|
|
- initializeUnifiedResponseHeader(response);
|
|
|
- OutputStream outputStream = response.getOutputStream();
|
|
|
- JacksonContextHolder.getGlobalSerializer().writeValue(outputStream, result);
|
|
|
- outputStream.flush();
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 对象URL编码
|
|
|
*
|
|
|
- * @param object 编码对象
|
|
|
+ * @param original 原始信息
|
|
|
* @return 对象编码结果
|
|
|
*/
|
|
|
- public static String encode(Object object) {
|
|
|
- String serialize = JacksonContextHolder.serialize(object);
|
|
|
- if (StringUtils.isEmpty(serialize)) {
|
|
|
+ public static String encode(String original) {
|
|
|
+ if (StringUtils.isEmpty(original)) {
|
|
|
return StringUtils.EMPTY;
|
|
|
}
|
|
|
try {
|
|
|
- return URLEncoder.encode(serialize, "UTF-8");
|
|
|
+ return URLEncoder.encode(original, "UTF-8");
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
throw new RuntimeException(e);
|
|
|
}
|
|
@@ -290,29 +250,6 @@ public final class HttpUtils {
|
|
|
return domain.concat(StringUtils.URI_SEPARATOR).concat(uri);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 构建Http请求消息
|
|
|
- *
|
|
|
- * @param request Http请求对象
|
|
|
- * @param type 消息类型
|
|
|
- * @param message 消息内容
|
|
|
- * @return 消息
|
|
|
- */
|
|
|
- public static String message(HttpServletRequest request, Object type, String message) {
|
|
|
- Session session = SessionContextHolder.getSession(false);
|
|
|
- Long id = ObjectUtils.ifNull(session, Session::getId);
|
|
|
- String host = ObjectUtils.ifNull(session, Session::getHost);
|
|
|
- String device = ObjectUtils.ifNull(session, Session::getDevice);
|
|
|
- Platform platform = ObjectUtils.ifNull(session, Session::getPlatform);
|
|
|
- Terminal terminal = ObjectUtils.ifNull(session, Session::getTerminal);
|
|
|
- String version = ObjectUtils.ifNull(session, Session::getVersion);
|
|
|
- String uri = ObjectUtils.ifNull(request, HttpServletRequest::getRequestURI);
|
|
|
- String method = ObjectUtils.ifNull(request, HttpServletRequest::getMethod);
|
|
|
- return String.format("[%s] [%s] [%s] [%s] [%s] [%d] [%s %s] [%s]: %s",
|
|
|
- host, platform, terminal, version, device, id, method, uri, type, message
|
|
|
- );
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* Http请求参数序列化
|
|
|
*
|
|
@@ -358,30 +295,4 @@ public final class HttpUtils {
|
|
|
return buffer.length() == 0 ? (key + "=") : buffer.toString();
|
|
|
}, ordering);
|
|
|
}
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取异常消息
|
|
|
- *
|
|
|
- * @param request Http请求对象
|
|
|
- * @param status Http状态码
|
|
|
- * @param exception 异常信息
|
|
|
- * @return 异常消息
|
|
|
- */
|
|
|
- public static String getExceptionMessage(@NonNull HttpServletRequest request, @NonNull HttpStatus status,
|
|
|
- @NonNull Exception exception) {
|
|
|
- return getExceptionMessage(request, status, exception.getMessage());
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取异常消息
|
|
|
- *
|
|
|
- * @param request Http请求对象
|
|
|
- * @param status Http状态码
|
|
|
- * @param exception 异常信息
|
|
|
- * @return 异常消息
|
|
|
- */
|
|
|
- public static String getExceptionMessage(@NonNull HttpServletRequest request, @NonNull HttpStatus status,
|
|
|
- String exception) {
|
|
|
- return message(request, status.value(), exception);
|
|
|
- }
|
|
|
}
|