From 84317a0af2c52525c930e57178067cd92d17a3e5 Mon Sep 17 00:00:00 2001 From: peacewong Date: Fri, 16 Jul 2021 11:41:05 +0800 Subject: [PATCH] Support setting of content type when the linkis HTTP client uploads files close #906 --- .../linkis/common/utils/Utils.scala | 2 +- .../httpclient/AbstractHttpClient.scala | 1 + .../httpclient/request/UploadAction.scala | 19 +++++++++++++++++-- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/linkis-commons/linkis-common/src/main/scala/com/webank/wedatasphere/linkis/common/utils/Utils.scala b/linkis-commons/linkis-common/src/main/scala/com/webank/wedatasphere/linkis/common/utils/Utils.scala index 6975cdff16..5e6b4489cb 100644 --- a/linkis-commons/linkis-common/src/main/scala/com/webank/wedatasphere/linkis/common/utils/Utils.scala +++ b/linkis-commons/linkis-common/src/main/scala/com/webank/wedatasphere/linkis/common/utils/Utils.scala @@ -47,7 +47,7 @@ object Utils extends Logging { System.exit(-1) throw e case er: Error => - error("Throw error", er.getCause) + error("Throw error", er) throw er case t => catchOp(t) } diff --git a/linkis-commons/linkis-httpclient/src/main/scala/com/webank/wedatasphere/linkis/httpclient/AbstractHttpClient.scala b/linkis-commons/linkis-httpclient/src/main/scala/com/webank/wedatasphere/linkis/httpclient/AbstractHttpClient.scala index def7421159..97c5b6aa08 100644 --- a/linkis-commons/linkis-httpclient/src/main/scala/com/webank/wedatasphere/linkis/httpclient/AbstractHttpClient.scala +++ b/linkis-commons/linkis-httpclient/src/main/scala/com/webank/wedatasphere/linkis/httpclient/AbstractHttpClient.scala @@ -199,6 +199,7 @@ abstract class AbstractHttpClient(clientConfig: ClientConfig, clientName: String upload.inputStreams.foreach { case (k, v) => builder.addBinaryBody(k, v, ContentType.create("multipart/form-data"), k) } + upload.binaryBodies.foreach(binaryBody => builder.addBinaryBody(binaryBody.parameterName, binaryBody.inputStream, binaryBody.contentType, binaryBody.fileName)) upload match { case get: GetAction => get.getParameters. retain((k, v) => v != null && k != null). diff --git a/linkis-commons/linkis-httpclient/src/main/scala/com/webank/wedatasphere/linkis/httpclient/request/UploadAction.scala b/linkis-commons/linkis-httpclient/src/main/scala/com/webank/wedatasphere/linkis/httpclient/request/UploadAction.scala index a1ddb70ab8..badf261332 100644 --- a/linkis-commons/linkis-httpclient/src/main/scala/com/webank/wedatasphere/linkis/httpclient/request/UploadAction.scala +++ b/linkis-commons/linkis-httpclient/src/main/scala/com/webank/wedatasphere/linkis/httpclient/request/UploadAction.scala @@ -18,6 +18,8 @@ package com.webank.wedatasphere.linkis.httpclient.request import java.util +import org.apache.http.entity.ContentType + import scala.tools.nsc.interpreter.InputStream @@ -27,7 +29,7 @@ trait UploadAction extends UserAction { * The file to be uploaded, the key is the parameter name, and the value is the file path. * 需要上传的文件,key为参数名,value为文件路径 */ - val files: util.Map[String, String] + @Deprecated val files: util.Map[String, String] /** * The inputStream that needs to be uploaded, the key is the parameter name, and the value is the input stream. * 需要上传的输入流,key为参数名,value为输入流 @@ -37,7 +39,20 @@ trait UploadAction extends UserAction { * The inputStream that needs to be uploaded, the key is the parameter name, and the value is the fileName of inputStream. * 需要上传的输入流,key为参数名,value为输入流的文件名 */ - def inputStreamNames: util.Map[String, String] = new util.HashMap[String, String]() + @Deprecated def inputStreamNames: util.Map[String, String] = new util.HashMap[String, String]() + def binaryBodies: util.List[BinaryBody] = new util.ArrayList[BinaryBody](0) def user: Option[String] = Option(getUser) } + +case class BinaryBody(parameterName: String, inputStream: InputStream, fileName: String, contentType: ContentType) +object BinaryBody { + def apply(parameterName: String, inputStream: InputStream, fileName: String, contentType: String): BinaryBody = + new BinaryBody(parameterName, inputStream, fileName, ContentType.create(contentType)) + + def apply(parameterName: String, inputStream: InputStream): BinaryBody = + new BinaryBody(parameterName, inputStream, null, ContentType.DEFAULT_BINARY) + + def apply(parameterName: String, inputStream: InputStream, fileName: String): BinaryBody = + new BinaryBody(parameterName, inputStream, fileName, ContentType.DEFAULT_BINARY) +} \ No newline at end of file