Files
usdt/luqj-common/src/main/java/com/luqj/util/RequestHelper.java
T
2025-12-02 23:07:47 +08:00

129 lines
5.6 KiB
Java

package com.luqj.util;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.SettableFuture;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Modifier;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class RequestHelper {
private static final String DEFAULT_CONTENT_TYPE = "application/octet-stream";
public static Map<String, String> fileToContentTypeMap = new HashMap<String, String>();
static {
fileToContentTypeMap.put(".atom", "application/atom+xml");
fileToContentTypeMap.put(".rss", "application/rss+xml");
fileToContentTypeMap.put(".xml", "application/xml");
fileToContentTypeMap.put(".csv", "text/csv");
fileToContentTypeMap.put(".txt", "text/plain");
}
public static String getContentTypeForFile(File file) {
String contentType = fileToContentTypeMap.get(getFileExtension(file));
if (contentType != null) return contentType;
try {
contentType = Files.probeContentType(file.toPath());
} catch (IOException ignored) {
}
return contentType != null ? contentType : DEFAULT_CONTENT_TYPE;
}
private static String getFileExtension(File file) {
String fileName = file.getName();
int index = fileName.lastIndexOf('.');
if (index == -1) return "";
return fileName.substring(index, fileName.length());
}
private static String convertToString(Object input) {
if (input == null) {
return "null";
} else if (input instanceof Map) {
Gson gson = new GsonBuilder()
.excludeFieldsWithModifiers(Modifier.STATIC)
.excludeFieldsWithModifiers(Modifier.PROTECTED)
.disableHtmlEscaping()
.create();
return gson.toJson((Map) input);
} else if (input instanceof List) {
Gson gson = new GsonBuilder()
.excludeFieldsWithModifiers(Modifier.STATIC)
.excludeFieldsWithModifiers(Modifier.PROTECTED)
.disableHtmlEscaping()
.create();
return gson.toJson((List) input);
} else {
return input.toString();
}
}
public static int getContentLength(Map<String, Object> allParams, String boundary) throws IOException {
int contentLength = 0;
for (Map.Entry entry : allParams.entrySet()) {
contentLength += ("--" + boundary + "\r\n").length();
if (entry.getValue() instanceof File) {
File file = (File) entry.getValue();
String contentType = getContentTypeForFile(file);
contentLength += getLengthAndLog( "Content-Disposition: form-data; name=\"" + entry.getKey() + "\"; filename=\"" + file.getName() + "\"\r\n");
if (contentType != null) {
contentLength += getLengthAndLog( "Content-Type: " + contentType + "\r\n");
}
contentLength += getLengthAndLog( "\r\n");
contentLength += file.length();
contentLength += getLengthAndLog( "\r\n");
} else if (entry.getValue() instanceof byte[]) {
byte[] bytes = (byte[]) entry.getValue();
contentLength += getLengthAndLog( "Content-Disposition: form-data; name=\"" + entry.getKey() + "\"; filename=\"" + "chunkfile" + "\"\r\n");
contentLength += bytes.length;
contentLength += getLengthAndLog( "\r\n");
} else {
contentLength += getLengthAndLog( "Content-Disposition: form-data; name=\"" + entry.getKey() + "\"\r\n\r\n");
contentLength += getLengthAndLog( convertToString(entry.getValue()));
contentLength += getLengthAndLog( "\r\n");
}
}
contentLength += getLengthAndLog( "--" + boundary + "--\r\n");
return contentLength;
}
private static int getLengthAndLog(String input) throws IOException {
return input.getBytes("UTF-8").length;
}
public static String constructUrlString(String apiUrl, Map<String, Object> allParams) throws IOException {
StringBuilder urlString = new StringBuilder(apiUrl);
boolean firstEntry = true;
for (Map.Entry entry : allParams.entrySet()) {
urlString.append((firstEntry ? "?" : "&") + URLEncoder.encode(entry.getKey().toString(), "UTF-8") + "=" + URLEncoder.encode(convertToString(entry.getValue()), "UTF-8"));
firstEntry = false;
}
return urlString.toString();
}
public static ListenableFuture<ResponseWrapper> invoke(okhttp3.OkHttpClient client, okhttp3.Request request) {
final SettableFuture<ResponseWrapper> future = SettableFuture.create();
client.newCall(request).enqueue(
new okhttp3.Callback() {
@Override
public void onFailure(final okhttp3.Call call, IOException e) {
future.setException(
new Exception(e)
);
}
@Override
public void onResponse(okhttp3.Call call, final okhttp3.Response response) throws IOException {
future.set(new ResponseWrapper(response.body().string(), convertToString(response.headers())));
}
});
return future;
}
}