From 4470052c3b6bdeb18e45987f8aa293d1e93d0552 Mon Sep 17 00:00:00 2001
From: Ludc <2870569285@qq.com>
Date: 星期二, 18 十一月 2025 11:59:12 +0800
Subject: [PATCH] 所有文件上传接口增加文件安全校验逻辑。
---
Source/BladeX-Tool/blade-starter-http/src/main/java/org/springblade/core/http/Exchange.java | 209 ++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 209 insertions(+), 0 deletions(-)
diff --git a/Source/BladeX-Tool/blade-starter-http/src/main/java/org/springblade/core/http/Exchange.java b/Source/BladeX-Tool/blade-starter-http/src/main/java/org/springblade/core/http/Exchange.java
new file mode 100644
index 0000000..3b03618
--- /dev/null
+++ b/Source/BladeX-Tool/blade-starter-http/src/main/java/org/springblade/core/http/Exchange.java
@@ -0,0 +1,209 @@
+/*
+ * Copyright (c) 2018-2028, DreamLu All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * Neither the name of the dreamlu.net developer nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ * Author: DreamLu 鍗㈡槬姊� (596392912@qq.com)
+ */
+package org.springblade.core.http;
+
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.JsonNode;
+import lombok.RequiredArgsConstructor;
+import okhttp3.Call;
+import okhttp3.Request;
+import org.springblade.core.tool.utils.Exceptions;
+
+import javax.annotation.Nullable;
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Path;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.function.BiConsumer;
+import java.util.function.Function;
+
+/**
+ * Exchange
+ *
+ * @author L.cm
+ */
+@RequiredArgsConstructor
+public class Exchange {
+ private BiConsumer<Request, IOException> failedBiConsumer = (r, e) -> {};
+ private final Call call;
+
+ public Exchange onFailed(BiConsumer<Request, IOException> failConsumer) {
+ this.failedBiConsumer = failConsumer;
+ return this;
+ }
+
+ public <R> R onResponse(Function<ResponseSpec, R> func) {
+ try (HttpResponse response = new HttpResponse(call.execute())) {
+ return func.apply(response);
+ } catch (IOException e) {
+ throw Exceptions.unchecked(e);
+ }
+ }
+
+ @Nullable
+ public <R> R onSuccess(Function<ResponseSpec, R> func) {
+ try (HttpResponse response = new HttpResponse(call.execute())) {
+ return func.apply(response);
+ } catch (IOException e) {
+ failedBiConsumer.accept(call.request(), e);
+ return null;
+ }
+ }
+
+ @Nullable
+ public <R> R onSuccessful(Function<ResponseSpec, R> func) {
+ try (HttpResponse response = new HttpResponse(call.execute())) {
+ if (response.isOk()) {
+ return func.apply(response);
+ } else {
+ failedBiConsumer.accept(call.request(), new IOException(response.toString()));
+ }
+ } catch (IOException e) {
+ failedBiConsumer.accept(call.request(), e);
+ }
+ return null;
+ }
+
+ public <R> Optional<R> onSuccessOpt(Function<ResponseSpec, R> func) {
+ return Optional.ofNullable(this.onSuccess(func));
+ }
+
+ public <R> Optional<R> onSuccessfulOpt(Function<ResponseSpec, R> func) {
+ return Optional.ofNullable(this.onSuccessful(func));
+ }
+
+ /**
+ * Returns body String.
+ *
+ * @return body String
+ */
+ public String asString() {
+ return onResponse(ResponseSpec::asString);
+ }
+
+ /**
+ * Returns body to byte arrays.
+ *
+ * @return byte arrays
+ */
+ public byte[] asBytes() {
+ return onResponse(ResponseSpec::asBytes);
+ }
+
+ /**
+ * Returns body to JsonNode.
+ *
+ * @return JsonNode
+ */
+ public JsonNode asJsonNode() {
+ return onResponse(ResponseSpec::asJsonNode);
+ }
+
+ /**
+ * Returns body to Object.
+ *
+ * @param valueType value value type
+ * @return Object
+ */
+ public <T> T asValue(Class<T> valueType) {
+ return onResponse(responseSpec -> responseSpec.asValue(valueType));
+ }
+
+ /**
+ * Returns body to Object.
+ *
+ * @param typeReference value Type Reference
+ * @return Object
+ */
+ public <T> T asValue(TypeReference<T> typeReference) {
+ return onResponse(responseSpec -> responseSpec.asValue(typeReference));
+ }
+
+ /**
+ * Returns body to List.
+ *
+ * @param valueType value type
+ * @return List
+ */
+ public <T> List<T> asList(Class<T> valueType) {
+ return onResponse(responseSpec -> responseSpec.asList(valueType));
+ }
+
+ /**
+ * Returns body to Map.
+ *
+ * @param keyClass key type
+ * @param valueType value type
+ * @return Map
+ */
+ public <K, V> Map<K, V> asMap(Class<?> keyClass, Class<?> valueType) {
+ return onResponse(responseSpec -> responseSpec.asMap(keyClass, valueType));
+ }
+
+ /**
+ * Returns body to Map.
+ *
+ * @param valueType value 绫诲瀷
+ * @return Map
+ */
+ public <V> Map<String, V> asMap(Class<?> valueType) {
+ return onResponse(responseSpec -> responseSpec.asMap(valueType));
+ }
+
+ /**
+ * 灏� xml銆乭eml 杞垚瀵硅薄
+ *
+ * @param valueType 瀵硅薄绫�
+ * @param <T> 娉涘瀷
+ * @return 瀵硅薄
+ */
+ public <T> T asDomValue(Class<T> valueType) {
+ return onResponse(responseSpec -> responseSpec.asDomValue(valueType));
+ }
+
+ /**
+ * 灏� xml銆乭eml 杞垚瀵硅薄
+ *
+ * @param valueType 瀵硅薄绫�
+ * @param <T> 娉涘瀷
+ * @return 瀵硅薄闆嗗悎
+ */
+ public <T> List<T> asDomList(Class<T> valueType) {
+ return onResponse(responseSpec -> responseSpec.asDomList(valueType));
+ }
+
+ /**
+ * toFile.
+ *
+ * @param file File
+ */
+ public File toFile(File file) {
+ return onResponse(responseSpec -> responseSpec.toFile(file));
+ }
+
+ /**
+ * toFile.
+ *
+ * @param path Path
+ */
+ public Path toFile(Path path) {
+ return onResponse(responseSpec -> responseSpec.toFile(path));
+ }
+
+}
--
Gitblit v1.9.3