|
@@ -1,11 +1,20 @@
|
|
|
package cn.nosum.http.proxy;
|
|
|
|
|
|
+import cn.nosum.http.apache.InputStreamResponseHandler;
|
|
|
+import cn.nosum.http.apache.Utf8ResponseHandler;
|
|
|
+import cn.nosum.http.entity.DownloadResponse;
|
|
|
import cn.nosum.http.exception.HttpErrorException;
|
|
|
import jodd.http.HttpResponse;
|
|
|
import okhttp3.Response;
|
|
|
import org.apache.http.Header;
|
|
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
+import org.apache.http.entity.ContentType;
|
|
|
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.OutputStream;
|
|
|
+import java.util.Objects;
|
|
|
import java.util.regex.Matcher;
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
@@ -53,6 +62,91 @@ public class HttpResponseProxy {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 获取下载文件响应流.
|
|
|
+ *
|
|
|
+ * @return 文件响应流
|
|
|
+ * @throws IOException IO 异常
|
|
|
+ */
|
|
|
+ public OutputStream getOutputStream() throws IOException {
|
|
|
+ if (this.apacheHttpResponse != null) {
|
|
|
+ try (InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(this.apacheHttpResponse)) {
|
|
|
+ return transfer(inputStream);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (this.joddHttpResponse != null) {
|
|
|
+ return transfer(joddHttpResponse.bodyBytes());
|
|
|
+ }
|
|
|
+ if (this.okHttpResponse != null) {
|
|
|
+ return transfer(this.okHttpResponse.body().bytes());
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取响应类型.
|
|
|
+ *
|
|
|
+ * @return 响应类型
|
|
|
+ * @throws HttpErrorException HTTP 异常
|
|
|
+ */
|
|
|
+ public String getContentType() throws HttpErrorException, IOException {
|
|
|
+ String contentType = null;
|
|
|
+
|
|
|
+ // application/json; encoding=utf-8 下载媒体文件出错
|
|
|
+ final String mimeType = ContentType.APPLICATION_JSON.getMimeType();
|
|
|
+
|
|
|
+ if (this.apacheHttpResponse != null) {
|
|
|
+ Header[] contentTypeHeader = apacheHttpResponse.getHeaders("Content-Type");
|
|
|
+ if (contentTypeHeader[0].getValue().startsWith(mimeType)) {
|
|
|
+ throw new HttpErrorException(Utf8ResponseHandler.INSTANCE.handleResponse(this.apacheHttpResponse));
|
|
|
+ }
|
|
|
+ contentType = contentTypeHeader[0].getValue();
|
|
|
+ }
|
|
|
+
|
|
|
+ if (this.joddHttpResponse != null) {
|
|
|
+ contentType = this.joddHttpResponse.header("Content-Type");
|
|
|
+ if (contentType != null && contentType.startsWith(mimeType)) {
|
|
|
+ throw new HttpErrorException(this.joddHttpResponse.bodyText());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (this.okHttpResponse != null) {
|
|
|
+ contentType = this.okHttpResponse.header("Content-Type");
|
|
|
+ if (contentType != null && contentType.startsWith(mimeType)) {
|
|
|
+ throw new HttpErrorException(Objects.requireNonNull(this.okHttpResponse.body()).string());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return contentType;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 转换响应流为下载文件响应.
|
|
|
+ *
|
|
|
+ * @return 下载文件响应
|
|
|
+ * @throws HttpErrorException HTTP 异常
|
|
|
+ * @throws IOException IO 异常
|
|
|
+ */
|
|
|
+ public DownloadResponse transferDownload() throws HttpErrorException, IOException {
|
|
|
+ return DownloadResponse.builder().contentType(getContentType()).outputStream(getOutputStream()).fileName(getFileName()).build();
|
|
|
+ }
|
|
|
+
|
|
|
+ private OutputStream transfer(byte[] bytes) throws IOException {
|
|
|
+ ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
|
|
+ byteArrayOutputStream.write(bytes);
|
|
|
+ return byteArrayOutputStream;
|
|
|
+ }
|
|
|
+
|
|
|
+ private OutputStream transfer(InputStream inputStream) throws IOException {
|
|
|
+ OutputStream output = new ByteArrayOutputStream();
|
|
|
+ byte[] buffer = new byte[4096];
|
|
|
+ int n;
|
|
|
+ while (-1 != (n = inputStream.read(buffer))) {
|
|
|
+ output.write(buffer, 0, n);
|
|
|
+ }
|
|
|
+ return output;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* 获取文件名称
|
|
|
*
|
|
|
* @param response apache 文件响应
|