From 8affe53c8d1e35fb5012b489d8ba3d57ae3038fb Mon Sep 17 00:00:00 2001 From: 0007 <0007@qq.com> Date: Wed, 27 Aug 2025 19:58:39 +0800 Subject: [PATCH] Add File --- .../document/loader/HttpDocumentLoader.java | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 agents-flex-core/src/main/java/com/agentsflex/core/document/loader/HttpDocumentLoader.java diff --git a/agents-flex-core/src/main/java/com/agentsflex/core/document/loader/HttpDocumentLoader.java b/agents-flex-core/src/main/java/com/agentsflex/core/document/loader/HttpDocumentLoader.java new file mode 100644 index 0000000..8f38b5d --- /dev/null +++ b/agents-flex-core/src/main/java/com/agentsflex/core/document/loader/HttpDocumentLoader.java @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2023-2025, Agents-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.agentsflex.core.document.loader; + +import com.agentsflex.core.document.DocumentParser; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; +import okhttp3.ResponseBody; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Map; +import java.util.concurrent.TimeUnit; + +public class HttpDocumentLoader extends AbstractDocumentLoader { + + private String url; + private Map headers; + + private final OkHttpClient okHttpClient; + + + public HttpDocumentLoader(DocumentParser documentParser, String url) { + super(documentParser); + this.url = url; + this.okHttpClient = new OkHttpClient.Builder() + .connectTimeout(3, TimeUnit.MINUTES) + .readTimeout(3, TimeUnit.MINUTES) + .build(); + } + + + public HttpDocumentLoader(DocumentParser documentParser, String url, Map headers) { + super(documentParser); + this.url = url; + this.headers = headers; + this.okHttpClient = new OkHttpClient.Builder() + .connectTimeout(3, TimeUnit.MINUTES) + .readTimeout(3, TimeUnit.MINUTES) + .build(); + } + + + @Override + public InputStream loadInputStream() { + Request.Builder builder = new Request.Builder() + .url(url); + + if (headers != null && !headers.isEmpty()) { + headers.forEach(builder::addHeader); + } + + // get method + Request request = builder.get().build(); + + try (Response response = okHttpClient.newCall(request).execute()) { + ResponseBody body = response.body(); + if (body != null) { + return body.byteStream(); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + + return null; + } +}