Compare commits

..

13 Commits

Author SHA1 Message Date
acb1304650 add CommonRequest.header 2025-11-07 23:38:25 +08:00
56afd301fb add CommonRequest.timeout 2025-11-07 22:50:44 +08:00
a240c00996 add CommonRequest.execute 2025-11-07 00:16:44 +08:00
fbcf21e990 add GsonRequest.body 2025-11-07 00:15:06 +08:00
f3623d03a2 support deploy nexus 2025-10-13 10:53:17 +08:00
e63d1103d8 add ProgressRequestTest 2025-10-13 10:53:00 +08:00
016c0b7e2f rename FormMap to ArgMap then support url arg 2025-10-13 10:52:48 +08:00
016e0b0962 extend CommonRequest for GsonRequest 2025-07-24 18:46:35 +08:00
91ef5e3bc3 fix GsonRequest serialize fail 2025-07-19 17:27:29 +08:00
12bb7603a1 add static get,post for ProgressiveRequest 2025-07-15 16:23:52 +08:00
77c32fb52f add CommonRequest, FileRequest, GsonRequest, TimiRequest 2025-07-15 16:23:34 +08:00
43919851ed add toFile(File) for ProgressiveRequest 2025-07-15 12:01:55 +08:00
9330c75bbc add length() for ProgressiveRequest 2025-07-15 11:57:58 +08:00
8 changed files with 514 additions and 13 deletions

69
pom.xml
View File

@ -16,6 +16,64 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> </properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.1.3</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.3.1</version>
<executions>
<execution>
<id>attach-sources</id>
<phase>package</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.11.2</version>
<executions>
<execution>
<id>attach-javadocs</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<distributionManagement>
<repository>
<id>timi_nexus</id>
<url>https://nexus.imyeyu.com/repository/maven-releases/</url>
</repository>
</distributionManagement>
<repositories>
<repository>
<id>timi_nexus</id>
<url>https://nexus.imyeyu.com/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>com.imyeyu.io</groupId> <groupId>com.imyeyu.io</groupId>
@ -27,5 +85,16 @@
<artifactId>httpclient5-fluent</artifactId> <artifactId>httpclient5-fluent</artifactId>
<version>5.2.1</version> <version>5.2.1</version>
</dependency> </dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -1,5 +1,6 @@
package com.imyeyu.network; package com.imyeyu.network;
import com.imyeyu.utils.Encoder;
import org.apache.hc.client5.http.fluent.Form; import org.apache.hc.client5.http.fluent.Form;
import org.apache.hc.core5.http.NameValuePair; import org.apache.hc.core5.http.NameValuePair;
@ -13,13 +14,27 @@ import java.util.Map;
* @author 夜雨 * @author 夜雨
* @since 2025-06-26 15:41 * @since 2025-06-26 15:41
*/ */
public class FormMap<K, V> extends HashMap<K, V> { public class ArgMap<K, V> extends HashMap<K, V> {
public List<NameValuePair> build() { public List<NameValuePair> toNameValuePair() {
Form form = Form.form(); Form form = Form.form();
for (Map.Entry<K, V> item : entrySet()) { for (Map.Entry<K, V> item : entrySet()) {
form.add(item.getKey().toString(), item.getValue().toString()); form.add(item.getKey().toString(), item.getValue().toString());
} }
return form.build(); return form.build();
} }
public String toURL() {
StringBuilder sb = new StringBuilder();
for (Map.Entry<K, V> item : entrySet()) {
sb.append(item.getKey().toString()).append('=').append(Encoder.urlArg(item.getValue().toString()));
sb.append('&');
}
sb.deleteCharAt(sb.length() - 1);
return sb.toString();
}
public String toURL(String url) {
return url + "?" + toURL();
}
} }

View File

@ -0,0 +1,67 @@
package com.imyeyu.network;
import org.apache.hc.client5.http.fluent.Request;
import org.apache.hc.core5.util.Timeout;
import java.io.IOException;
/**
* @author 夜雨
* @since 2025-07-15 14:27
*/
public class CommonRequest {
protected final Request request;
protected CommonRequest(Request request) {
this.request = request;
}
public static CommonRequest wrap(Request request) {
return new CommonRequest(request);
}
public static CommonRequest get(String url) {
return new CommonRequest(Request.get(url));
}
public static CommonRequest post(String url) {
return new CommonRequest(Request.post(url));
}
public CommonRequest timeout(long ms) {
request.connectTimeout(Timeout.ofMilliseconds(ms)).responseTimeout(Timeout.ofMilliseconds(ms));
return this;
}
public CommonRequest header(String key, String value) {
request.addHeader(key, value);
return this;
}
public CommonRequest token(String token) {
request.addHeader("Token", token);
return this;
}
public CommonRequest language(String langHeader) {
request.addHeader("Accept-Language", langHeader);
return this;
}
public String asString() throws IOException {
return request.execute().returnContent().asString();
}
public Number asNumber() throws IOException {
return Double.parseDouble(asString());
}
public byte[] asBytes() throws IOException {
return request.execute().returnContent().asBytes();
}
public void execute() throws IOException {
request.execute();
}
}

View File

@ -0,0 +1,82 @@
package com.imyeyu.network;
import com.imyeyu.io.IO;
import org.apache.hc.client5.http.fluent.Request;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpResponse;
import org.apache.hc.core5.http.ProtocolException;
import javax.naming.NoPermissionException;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
/**
*
* @author 夜雨
* @since 2025-06-26 13:03
*/
public class FileRequest extends CommonRequest {
protected FileRequest(Request request) {
super(request);
}
public static FileRequest wrap(Request request) {
return new FileRequest(request);
}
public static FileRequest get(String url) {
return new FileRequest(Request.get(url));
}
public static FileRequest post(String url) {
return new FileRequest(Request.post(url));
}
@Override
public FileRequest timeout(long ms) {
super.timeout(ms);
return this;
}
public FileRequest header(String key, String value) {
request.addHeader(key, value);
return this;
}
public FileRequest token(String token) {
request.addHeader("Token", token);
return this;
}
public FileRequest language(String langHeader) {
request.addHeader("Accept-Language", langHeader);
return this;
}
public void toFile(String path, String fileName) throws IOException, NoPermissionException {
toFile(new File(IO.fitPath(path) + fileName));
}
public void toFile(Path outputPath) throws IOException, NoPermissionException {
toFile(outputPath.toFile());
}
public void toFile(File file) throws IOException, NoPermissionException {
IO.toFile(file, asBytes());
}
public long length() throws IOException, ProtocolException {
HttpResponse response = request.execute().returnResponse();
final int status = response.getCode();
if (status < 200 || 300 <= status) {
throw new IOException("HTTP error status: " + status);
}
final Header contentLengthHeader = response.getHeader("Content-Length");
if (contentLengthHeader == null) {
throw new IOException("Content-Length header missing");
}
return Long.parseLong(contentLengthHeader.getValue());
}
}

View File

@ -0,0 +1,90 @@
package com.imyeyu.network;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.imyeyu.java.TimiJava;
import org.apache.hc.client5.http.fluent.Request;
import org.apache.hc.core5.http.ContentType;
import java.io.IOException;
/**
* @author 夜雨
* @since 2025-07-15 14:27
*/
public class GsonRequest extends CommonRequest {
private static final Gson GSON = new Gson();
private Gson gson = null;
protected GsonRequest(Request request) {
super(request);
}
protected Gson getGson() {
return TimiJava.firstNotNull(gson, GSON);
}
public static GsonRequest wrap(Request request) {
return new GsonRequest(request);
}
public static GsonRequest get(String url) {
return new GsonRequest(Request.get(url));
}
public static GsonRequest post(String url) {
return new GsonRequest(Request.post(url));
}
public GsonRequest gson(Gson gson) {
this.gson = gson;
return this;
}
@Override
public GsonRequest timeout(long ms) {
super.timeout(ms);
return this;
}
public GsonRequest header(String key, String value) {
request.addHeader(key, value);
return this;
}
public GsonRequest token(String token) {
request.addHeader("Token", token);
return this;
}
public GsonRequest language(String langHeader) {
request.addHeader("Accept-Language", langHeader);
return this;
}
public GsonRequest body(Object object) {
request.bodyString(getGson().toJson(object), ContentType.APPLICATION_JSON);
return this;
}
public <T> T resultAs(Class<T> clazz) throws IOException {
return getGson().fromJson(super.asString(), clazz);
}
public <T> T resultAs(TypeToken<T> typeToken) throws IOException {
return getGson().fromJson(super.asString(), typeToken);
}
public JsonObject asJsonObject() throws IOException {
return JsonParser.parseString(super.asString()).getAsJsonObject();
}
public JsonArray asJsonArray() throws IOException {
return JsonParser.parseString(super.asString()).getAsJsonArray();
}
}

View File

@ -1,6 +1,7 @@
package com.imyeyu.network; package com.imyeyu.network;
import com.imyeyu.io.IO; import com.imyeyu.io.IO;
import com.imyeyu.java.TimiJava;
import org.apache.hc.client5.http.fluent.Request; import org.apache.hc.client5.http.fluent.Request;
import org.apache.hc.client5.http.fluent.Response; import org.apache.hc.client5.http.fluent.Response;
import org.apache.hc.core5.http.HttpEntity; import org.apache.hc.core5.http.HttpEntity;
@ -9,6 +10,7 @@ import org.apache.hc.core5.http.io.entity.EntityUtils;
import javax.naming.NoPermissionException; import javax.naming.NoPermissionException;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.file.Path; import java.nio.file.Path;
@ -19,13 +21,12 @@ import java.nio.file.Path;
* @author 夜雨 * @author 夜雨
* @since 2025-06-26 13:03 * @since 2025-06-26 13:03
*/ */
public class ProgressiveRequest { public class ProgressiveRequest extends FileRequest {
private final Request request; protected final ProgressiveCallback callback;
private final ProgressiveCallback callback;
private ProgressiveRequest(Request request, ProgressiveCallback callback) { protected ProgressiveRequest(Request request, ProgressiveCallback callback) {
this.request = request; super(request);
this.callback = callback; this.callback = callback;
} }
@ -33,10 +34,51 @@ public class ProgressiveRequest {
return new ProgressiveRequest(request, callback); return new ProgressiveRequest(request, callback);
} }
public static ProgressiveRequest get(String url, ProgressiveCallback callback) {
return new ProgressiveRequest(Request.get(url), callback);
}
public static ProgressiveRequest post(String url, ProgressiveCallback callback) {
return new ProgressiveRequest(Request.post(url), callback);
}
@Override
public ProgressiveRequest timeout(long ms) {
super.timeout(ms);
return this;
}
public ProgressiveRequest header(String key, String value) {
request.addHeader(key, value);
return this;
}
public ProgressiveRequest token(String token) {
request.addHeader("Token", token);
return this;
}
public ProgressiveRequest language(String langHeader) {
request.addHeader("Accept-Language", langHeader);
return this;
}
@Override
public void toFile(Path outputPath) throws IOException, NoPermissionException { public void toFile(Path outputPath) throws IOException, NoPermissionException {
processResponse(request.execute(), IO.getOutputStream(outputPath.toFile())); processResponse(request.execute(), IO.getOutputStream(outputPath.toFile()));
} }
@Override
public void toFile(File file) throws IOException, NoPermissionException {
processResponse(request.execute(), IO.getOutputStream(file));
}
@Override
public String asString() throws IOException {
return new String(asBytes());
}
@Override
public byte[] asBytes() throws IOException { public byte[] asBytes() throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream(); ByteArrayOutputStream os = new ByteArrayOutputStream();
processResponse(request.execute(), os); processResponse(request.execute(), os);
@ -55,6 +97,9 @@ public class ProgressiveRequest {
} }
long length = entity.getContentLength(); long length = entity.getContentLength();
if (TimiJava.isEmpty(callback)) {
IO.toOutputStream(entity.getContent(), os);
} else {
IO.toOutputStream(entity.getContent(), os, new IO.OnWriteCallback() { IO.toOutputStream(entity.getContent(), os, new IO.OnWriteCallback() {
@Override @Override
@ -63,6 +108,7 @@ public class ProgressiveRequest {
return callback.handler(length, total, now); return callback.handler(length, total, now);
} }
}); });
}
EntityUtils.consume(entity); EntityUtils.consume(entity);
return null; return null;
}); });

View File

@ -0,0 +1,91 @@
package com.imyeyu.network;
import com.google.gson.reflect.TypeToken;
import com.imyeyu.java.bean.timi.TimiResponse;
import org.apache.hc.client5.http.fluent.Request;
import java.io.IOException;
/**
* @author 夜雨
* @since 2025-07-15 14:34
*/
public class TimiRequest extends GsonRequest {
protected TimiRequest(Request request) {
super(request);
}
public static TimiRequest wrap(Request request) {
return new TimiRequest(request);
}
public static TimiRequest get(String url) {
return new TimiRequest(Request.get(url));
}
public static TimiRequest post(String url) {
return new TimiRequest(Request.post(url));
}
@Override
public TimiRequest timeout(long ms) {
super.timeout(ms);
return this;
}
public TimiRequest header(String key, String value) {
request.addHeader(key, value);
return this;
}
public TimiRequest token(String token) {
request.addHeader("Token", token);
return this;
}
public TimiRequest language(String langHeader) {
request.addHeader("Accept-Language", langHeader);
return this;
}
@Override
public TimiRequest body(Object object) {
super.body(object);
return this;
}
@Override
public String asString() throws IOException {
return resultAs(String.class);
}
@Override
public Number asNumber() throws IOException {
return Double.parseDouble(asString());
}
public <T> T resultAs(Class<T> clazz) throws IOException {
TimiResponse<T> resp = getGson().fromJson(asJsonObject(), TypeToken.getParameterized(TimiResponse.class, clazz).getType());
if (resp.isFail()) {
throw resp.toException();
}
return resp.getData();
}
public <T> T resultAs(TypeToken<T> typeToken) throws IOException {
TimiResponse<T> resp = getGson().fromJson(asJsonObject(), TypeToken.getParameterized(TimiResponse.class, typeToken.getType()).getType());
if (resp.isFail()) {
throw resp.toException();
}
return resp.getData();
}
@Override
public void execute() throws IOException {
TimiResponse<?> resp = getGson().fromJson(asJsonObject(), TypeToken.getParameterized(TimiResponse.class, Object.class).getType());
if (resp.isFail()) {
throw resp.toException();
}
}
}

View File

@ -0,0 +1,41 @@
package com.imyeyu.network.test;
import com.imyeyu.io.IOSize;
import com.imyeyu.network.FileRequest;
import com.imyeyu.network.ProgressiveRequest;
import org.apache.hc.client5.http.fluent.Request;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpResponse;
import org.junit.Test;
/**
* @author 夜雨
* @since 2025-07-14 15:30
*/
public class ProgressRequestTest {
private static final String URL1 = "http://localhost:8091/system/file/download/video/test92MB.mp4?key=GS60MF78GbPhImiCN4fsi3T2E5tDfP1scxufVhbuccaxldba0okpQPgKq4ntyr0J";
private static final String URL2 = "https://git.imyeyu.com/test92MB.mp4";
private static final String URL3 = "http://localhost:8092/test/download";
@Test
public void testGet() throws Exception {
ProgressiveRequest.get(URL1, (total, read, now) -> {
System.out.println(1D * read / total);
return true;
}).asBytes();
}
@Test
public void testLength() throws Exception {
long length = FileRequest.wrap(Request.head(URL2)).length();
System.out.println(IOSize.format(length));
}
@Test
public void testLength2() throws Exception {
HttpResponse response = Request.head(URL3).execute().returnResponse();
final Header contentLengthHeader = response.getHeader("Content-Length");
System.out.println(contentLengthHeader.getValue());
}
}