Skip to content

Commit

Permalink
feat:Scala code format alarm clear in linkis-httpclient (apache#3174)
Browse files Browse the repository at this point in the history
* feat:Scala code format alarm clear in linkis-httpclient

* Handling conflicts
  • Loading branch information
ruY9527 authored Sep 7, 2022
1 parent 739adf4 commit 2896ed2
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ import org.apache.http.util.EntityUtils

import java.net.URI
import java.util
import java.util.Locale

import scala.collection.JavaConversions._
import scala.collection.JavaConverters._

abstract class AbstractHttpClient(clientConfig: ClientConfig, clientName: String)
extends Client
Expand All @@ -86,11 +87,12 @@ abstract class AbstractHttpClient(clientConfig: ClientConfig, clientName: String
.setMaxConnPerRoute(clientConfig.getMaxConnection / 2)
.build

if (clientConfig.getAuthenticationStrategy != null)
if (clientConfig.getAuthenticationStrategy != null) {
clientConfig.getAuthenticationStrategy match {
case auth: AbstractAuthenticationStrategy => auth.setClient(this)
case _ =>
}
}

protected val (discovery, loadBalancer): (Option[Discovery], Option[LoadBalancer]) =
if (this.clientConfig.isDiscoveryEnabled) {
Expand All @@ -107,9 +109,9 @@ abstract class AbstractHttpClient(clientConfig: ClientConfig, clientName: String
clientConfig.isLoadbalancerEnabled && this.clientConfig.getLoadbalancerStrategy != null
) {
Some(this.clientConfig.getLoadbalancerStrategy.createLoadBalancer())
} else if (clientConfig.isLoadbalancerEnabled)
} else if (clientConfig.isLoadbalancerEnabled) {
Some(DefaultLoadbalancerStrategy.createLoadBalancer())
else None
} else None
loadBalancer match {
case Some(lb: AbstractLoadBalancer) =>
discovery.foreach(_.addDiscoveryListener(lb))
Expand Down Expand Up @@ -158,12 +160,14 @@ abstract class AbstractHttpClient(clientConfig: ClientConfig, clientName: String
}

val response =
if (!clientConfig.isRetryEnabled) addAttempt()
else
if (!clientConfig.isRetryEnabled) {
addAttempt()
} else {
clientConfig.getRetryHandler.retry(
addAttempt(),
action.getClass.getSimpleName + "HttpRequest"
)
}
val beforeDeserializeTime = System.currentTimeMillis
responseToResult(response, action) match {
case metricResult: MetricResult =>
Expand Down Expand Up @@ -227,7 +231,7 @@ abstract class AbstractHttpClient(clientConfig: ClientConfig, clientName: String
if (cookies != null && cookies.nonEmpty) cookies.foreach(requestAction.addCookie)
val headers = authAction.authToHeaders
if (headers != null && !headers.isEmpty) {
headers.foreach { case (k, v) =>
headers.asScala.foreach { case (k, v) =>
if (k != null && v != null) requestAction.addHeader(k.toString, v.toString)
}
}
Expand All @@ -254,28 +258,28 @@ abstract class AbstractHttpClient(clientConfig: ClientConfig, clientName: String
case delete: DeleteAction =>
val builder = new URIBuilder(realURL)
if (!delete.getParameters.isEmpty) {
delete.getParameters.foreach { case (k, v) =>
delete.getParameters.asScala.foreach { case (k, v) =>
if (k != null && v != null) builder.addParameter(k.toString, v.toString)
}
}
val httpDelete = new HttpDelete(builder.build())
if (requestAction.getHeaders.nonEmpty) {
requestAction.getHeaders.foreach { case (k, v) =>
if (requestAction.getHeaders.asScala.nonEmpty) {
requestAction.getHeaders.asScala.foreach { case (k, v) =>
if (k != null && v != null) httpDelete.addHeader(k.toString, v.toString)
}
}
httpDelete
case put: PutAction =>
val httpPut = new HttpPut(realURL)
if (put.getParameters.nonEmpty || put.getFormParams.nonEmpty) {
if (put.getParameters.asScala.nonEmpty || put.getFormParams.asScala.nonEmpty) {
val nameValuePairs = new util.ArrayList[NameValuePair]
if (put.getParameters.nonEmpty) {
put.getParameters.foreach { case (k, v) =>
if (put.getParameters.asScala.nonEmpty) {
put.getParameters.asScala.foreach { case (k, v) =>
if (v != null) nameValuePairs.add(new BasicNameValuePair(k, v.toString))
}
}
if (put.getFormParams.nonEmpty) {
put.getFormParams.foreach { case (k, v) =>
if (put.getFormParams.asScala.nonEmpty) {
put.getFormParams.asScala.foreach { case (k, v) =>
if (v != null) nameValuePairs.add(new BasicNameValuePair(k, v.toString))
}
}
Expand All @@ -289,20 +293,21 @@ abstract class AbstractHttpClient(clientConfig: ClientConfig, clientName: String
httpPut.setEntity(stringEntity)
}

if (requestAction.getHeaders.nonEmpty) {
requestAction.getHeaders.foreach { case (k, v) =>
if (requestAction.getHeaders.asScala.nonEmpty) {
requestAction.getHeaders.asScala.foreach { case (k, v) =>
if (k != null && v != null) httpPut.addHeader(k.toString, v.toString)
}
}
httpPut
case upload: UploadAction =>
val httpPost = new HttpPost(realURL)
val builder = MultipartEntityBuilder.create()
if (upload.inputStreams != null)
upload.inputStreams.foreach { case (k, v) =>
if (upload.inputStreams != null) {
upload.inputStreams.asScala.foreach { case (k, v) =>
builder.addBinaryBody(k, v, ContentType.create("multipart/form-data"), k)
}
upload.binaryBodies.foreach(binaryBody =>
}
upload.binaryBodies.asScala.foreach(binaryBody =>
builder.addBinaryBody(
binaryBody.parameterName,
binaryBody.inputStream,
Expand All @@ -312,14 +317,15 @@ abstract class AbstractHttpClient(clientConfig: ClientConfig, clientName: String
)
upload match {
case get: GetAction =>
get.getParameters.retain((k, v) => v != null && k != null).foreach { case (k, v) =>
if (k != null && v != null) builder.addTextBody(k.toString, v.toString)
get.getParameters.asScala.retain((k, v) => v != null && k != null).foreach {
case (k, v) =>
if (k != null && v != null) builder.addTextBody(k.toString, v.toString)
}
case _ =>
}
upload match {
case get: GetAction =>
get.getHeaders.retain((k, v) => v != null && k != null).foreach { case (k, v) =>
get.getHeaders.asScala.retain((k, v) => v != null && k != null).foreach { case (k, v) =>
if (k != null && v != null) httpPost.addHeader(k.toString, v.toString)
}
case _ =>
Expand All @@ -329,20 +335,20 @@ abstract class AbstractHttpClient(clientConfig: ClientConfig, clientName: String
httpPost
case post: POSTAction =>
val httpPost = new HttpPost(realURL)
if (post.getParameters.nonEmpty || post.getFormParams.nonEmpty) {
if (post.getParameters.asScala.nonEmpty || post.getFormParams.asScala.nonEmpty) {
val nvps = new util.ArrayList[NameValuePair]
if (post.getParameters.nonEmpty) {
post.getParameters.foreach { case (k, v) =>
if (post.getParameters.asScala.nonEmpty) {
post.getParameters.asScala.foreach { case (k, v) =>
if (v != null) nvps.add(new BasicNameValuePair(k, v.toString))
}
httpPost.setEntity(new UrlEncodedFormEntity(nvps))
} else if (post.getFormParams.nonEmpty) {
post.getFormParams.foreach { case (k, v) =>
} else if (post.getFormParams.asScala.nonEmpty) {
post.getFormParams.asScala.foreach { case (k, v) =>
if (v != null) nvps.add(new BasicNameValuePair(k, v.toString))
}
val entity: HttpEntity = EntityBuilder
.create()
. /*setContentEncoding("UTF-8").*/
. /* setContentEncoding("UTF-8"). */
setContentType(ContentType.create("application/x-www-form-urlencoded", Consts.UTF_8))
.setParameters(nvps)
.build();
Expand All @@ -356,22 +362,22 @@ abstract class AbstractHttpClient(clientConfig: ClientConfig, clientName: String
httpPost.setEntity(stringEntity)
}

if (requestAction.getHeaders.nonEmpty) {
requestAction.getHeaders.foreach { case (k, v) =>
if (requestAction.getHeaders.asScala.nonEmpty) {
requestAction.getHeaders.asScala.foreach { case (k, v) =>
if (k != null && v != null) httpPost.addHeader(k.toString, v.toString)
}
}
httpPost
case get: GetAction =>
val builder = new URIBuilder(realURL)
if (!get.getParameters.isEmpty) {
get.getParameters.foreach { case (k, v) =>
get.getParameters.asScala.foreach { case (k, v) =>
if (k != null && v != null) builder.addParameter(k.toString, v.toString)
}
}
val httpGet = new HttpGet(builder.build())
if (requestAction.getHeaders.nonEmpty) {
requestAction.getHeaders.foreach { case (k, v) =>
if (requestAction.getHeaders.asScala.nonEmpty) {
requestAction.getHeaders.asScala.foreach { case (k, v) =>
if (k != null && v != null) httpGet.addHeader(k.toString, v.toString)
}
}
Expand All @@ -382,8 +388,8 @@ abstract class AbstractHttpClient(clientConfig: ClientConfig, clientName: String
stringEntity.setContentEncoding(Configuration.BDP_ENCODING.getValue)
stringEntity.setContentType("application/json")
httpost.setEntity(stringEntity)
if (requestAction.getHeaders.nonEmpty) {
requestAction.getHeaders.foreach { case (k, v) =>
if (requestAction.getHeaders.asScala.nonEmpty) {
requestAction.getHeaders.asScala.foreach { case (k, v) =>
if (k != null && v != null) httpost.addHeader(k.toString, v.toString)
}
}
Expand All @@ -400,9 +406,9 @@ abstract class AbstractHttpClient(clientConfig: ClientConfig, clientName: String
): CloseableHttpResponse = {
val readTimeOut = waitTime.getOrElse(clientConfig.getReadTimeout)
val connectTimeOut =
if (clientConfig.getConnectTimeout > 1000 || clientConfig.getConnectTimeout < 0)
if (clientConfig.getConnectTimeout > 1000 || clientConfig.getConnectTimeout < 0) {
clientConfig.getConnectTimeout
else CONNECT_TIME_OUT
} else CONNECT_TIME_OUT
val requestConfig = RequestConfig.custom
.setConnectTimeout(connectTimeOut.toInt)
.setConnectionRequestTimeout(connectTimeOut.toInt)
Expand Down Expand Up @@ -455,9 +461,9 @@ abstract class AbstractHttpClient(clientConfig: ClientConfig, clientName: String
): CloseableHttpResponse = {
val readTimeOut = waitTime.getOrElse(clientConfig.getReadTimeout)
val connectTimeOut =
if (clientConfig.getConnectTimeout > 1000 || clientConfig.getConnectTimeout < 0)
if (clientConfig.getConnectTimeout > 1000 || clientConfig.getConnectTimeout < 0) {
clientConfig.getConnectTimeout
else CONNECT_TIME_OUT
} else CONNECT_TIME_OUT
val requestConfig = RequestConfig.custom
.setConnectTimeout(connectTimeOut.toInt)
.setConnectionRequestTimeout(connectTimeOut.toInt)
Expand Down Expand Up @@ -485,15 +491,16 @@ abstract class AbstractHttpClient(clientConfig: ClientConfig, clientName: String
entity.getContentEncoding != null && StringUtils.isNotBlank(
entity.getContentEncoding.getValue
)
) entity.getContentEncoding.getValue.toLowerCase match {
case "gzip" => new GzipDecompressingEntity(entity).getContent
case "deflate" => new DeflateDecompressingEntity(entity).getContent
case str =>
throw new HttpClientResultException(
s"request failed! Reason: not support decompress type $str."
)
}
else entity.getContent
) {
entity.getContentEncoding.getValue.toLowerCase(Locale.getDefault) match {
case "gzip" => new GzipDecompressingEntity(entity).getContent
case "deflate" => new DeflateDecompressingEntity(entity).getContent
case str =>
throw new HttpClientResultException(
s"request failed! Reason: not support decompress type $str."
)
}
} else entity.getContent
download.write(inputStream, response)
Result()
case heartbeat: HeartbeatAction =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ abstract class AbstractAuthenticationStrategy extends AuthenticationStrategy wit
val authenticationAction = userNameToAuthentications.get(key)
authenticationAction.updateLastAccessTime()
authenticationAction
} else
} else {
key.intern() synchronized {
var authentication = userNameToAuthentications.get(key)
if (authentication == null || isTimeout(authentication)) {
Expand All @@ -78,6 +78,7 @@ abstract class AbstractAuthenticationStrategy extends AuthenticationStrategy wit
}
authentication
}
}
}

def tryLogin(requestAction: Action, serverUrl: String): Authentication = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ abstract class AbstractDiscovery extends Discovery with Closeable with Logging {
logger.info("start Discovery thread")
client.execute(getHeartbeatAction(serverUrl), 3000) match {
case heartbeat: HeartbeatResult =>
if (!heartbeat.isHealthy)
if (!heartbeat.isHealthy) {
throw new DiscoveryException(
s"connect to serverUrl $serverUrl failed! Reason: gateway server is unhealthy!"
)
else discoveryListeners.asScala.foreach(_.onServerDiscovered(serverUrl))
} else discoveryListeners.asScala.foreach(_.onServerDiscovered(serverUrl))
}

Utils.defaultScheduler.scheduleAtFixedRate(
Expand Down Expand Up @@ -133,8 +133,9 @@ abstract class AbstractDiscovery extends Discovery with Closeable with Logging {
unhealthyServerInstances synchronized unhealthyServerInstances.remove(serverUrl)
discoveryListeners.asScala.foreach(_.onServerHealthy(serverUrl))
serverInstances synchronized serverInstances.add(serverUrl)
} else if (serverInstances.contains(serverUrl))
} else if (serverInstances.contains(serverUrl)) {
serverInstances synchronized serverInstances.remove(serverUrl)
}
}) { case _: ConnectException =>
unhealthyServerInstances synchronized unhealthyServerInstances.remove(serverUrl)
serverInstances synchronized serverInstances.remove(serverUrl)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,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为文件路径
*/
@Deprecated 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
Expand All @@ -41,7 +41,7 @@ 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为输入流的文件名
*/
@Deprecated 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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ class HashMapHttpResult extends HttpResult {
url: String,
contentType: String
): Unit = {
if (statusCode != 200)
if (statusCode != 200) {
throw new HttpClientResultException(
s"URL $url request failed! ResponseBody is $responseBody."
)
}
resultMap = JsonUtils.jackson.readValue(responseBody, classOf[util.Map[String, Object]])
this.responseBody = responseBody
this.statusCode = statusCode
Expand Down
2 changes: 1 addition & 1 deletion scalastyle-config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ This file is divided into 3 sections:
</check>

<check customId="argcount" level="error" class="org.scalastyle.scalariform.ParameterNumberChecker" enabled="true">
<parameters><parameter name="maxParameters"><![CDATA[10]]></parameter></parameters>
<parameters><parameter name="maxParameters"><![CDATA[15]]></parameter></parameters>
</check>

<check level="error" class="org.scalastyle.scalariform.NoFinalizeChecker" enabled="true"></check>
Expand Down

0 comments on commit 2896ed2

Please sign in to comment.