Skip to content

Commit 2896ed2

Browse files
authored
feat:Scala code format alarm clear in linkis-httpclient (#3174)
* feat:Scala code format alarm clear in linkis-httpclient * Handling conflicts
1 parent 739adf4 commit 2896ed2

File tree

6 files changed

+67
-57
lines changed

6 files changed

+67
-57
lines changed

linkis-commons/linkis-httpclient/src/main/scala/org/apache/linkis/httpclient/AbstractHttpClient.scala

+56-49
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,9 @@ import org.apache.http.util.EntityUtils
6767

6868
import java.net.URI
6969
import java.util
70+
import java.util.Locale
7071

71-
import scala.collection.JavaConversions._
72+
import scala.collection.JavaConverters._
7273

7374
abstract class AbstractHttpClient(clientConfig: ClientConfig, clientName: String)
7475
extends Client
@@ -86,11 +87,12 @@ abstract class AbstractHttpClient(clientConfig: ClientConfig, clientName: String
8687
.setMaxConnPerRoute(clientConfig.getMaxConnection / 2)
8788
.build
8889

89-
if (clientConfig.getAuthenticationStrategy != null)
90+
if (clientConfig.getAuthenticationStrategy != null) {
9091
clientConfig.getAuthenticationStrategy match {
9192
case auth: AbstractAuthenticationStrategy => auth.setClient(this)
9293
case _ =>
9394
}
95+
}
9496

9597
protected val (discovery, loadBalancer): (Option[Discovery], Option[LoadBalancer]) =
9698
if (this.clientConfig.isDiscoveryEnabled) {
@@ -107,9 +109,9 @@ abstract class AbstractHttpClient(clientConfig: ClientConfig, clientName: String
107109
clientConfig.isLoadbalancerEnabled && this.clientConfig.getLoadbalancerStrategy != null
108110
) {
109111
Some(this.clientConfig.getLoadbalancerStrategy.createLoadBalancer())
110-
} else if (clientConfig.isLoadbalancerEnabled)
112+
} else if (clientConfig.isLoadbalancerEnabled) {
111113
Some(DefaultLoadbalancerStrategy.createLoadBalancer())
112-
else None
114+
} else None
113115
loadBalancer match {
114116
case Some(lb: AbstractLoadBalancer) =>
115117
discovery.foreach(_.addDiscoveryListener(lb))
@@ -158,12 +160,14 @@ abstract class AbstractHttpClient(clientConfig: ClientConfig, clientName: String
158160
}
159161

160162
val response =
161-
if (!clientConfig.isRetryEnabled) addAttempt()
162-
else
163+
if (!clientConfig.isRetryEnabled) {
164+
addAttempt()
165+
} else {
163166
clientConfig.getRetryHandler.retry(
164167
addAttempt(),
165168
action.getClass.getSimpleName + "HttpRequest"
166169
)
170+
}
167171
val beforeDeserializeTime = System.currentTimeMillis
168172
responseToResult(response, action) match {
169173
case metricResult: MetricResult =>
@@ -227,7 +231,7 @@ abstract class AbstractHttpClient(clientConfig: ClientConfig, clientName: String
227231
if (cookies != null && cookies.nonEmpty) cookies.foreach(requestAction.addCookie)
228232
val headers = authAction.authToHeaders
229233
if (headers != null && !headers.isEmpty) {
230-
headers.foreach { case (k, v) =>
234+
headers.asScala.foreach { case (k, v) =>
231235
if (k != null && v != null) requestAction.addHeader(k.toString, v.toString)
232236
}
233237
}
@@ -254,28 +258,28 @@ abstract class AbstractHttpClient(clientConfig: ClientConfig, clientName: String
254258
case delete: DeleteAction =>
255259
val builder = new URIBuilder(realURL)
256260
if (!delete.getParameters.isEmpty) {
257-
delete.getParameters.foreach { case (k, v) =>
261+
delete.getParameters.asScala.foreach { case (k, v) =>
258262
if (k != null && v != null) builder.addParameter(k.toString, v.toString)
259263
}
260264
}
261265
val httpDelete = new HttpDelete(builder.build())
262-
if (requestAction.getHeaders.nonEmpty) {
263-
requestAction.getHeaders.foreach { case (k, v) =>
266+
if (requestAction.getHeaders.asScala.nonEmpty) {
267+
requestAction.getHeaders.asScala.foreach { case (k, v) =>
264268
if (k != null && v != null) httpDelete.addHeader(k.toString, v.toString)
265269
}
266270
}
267271
httpDelete
268272
case put: PutAction =>
269273
val httpPut = new HttpPut(realURL)
270-
if (put.getParameters.nonEmpty || put.getFormParams.nonEmpty) {
274+
if (put.getParameters.asScala.nonEmpty || put.getFormParams.asScala.nonEmpty) {
271275
val nameValuePairs = new util.ArrayList[NameValuePair]
272-
if (put.getParameters.nonEmpty) {
273-
put.getParameters.foreach { case (k, v) =>
276+
if (put.getParameters.asScala.nonEmpty) {
277+
put.getParameters.asScala.foreach { case (k, v) =>
274278
if (v != null) nameValuePairs.add(new BasicNameValuePair(k, v.toString))
275279
}
276280
}
277-
if (put.getFormParams.nonEmpty) {
278-
put.getFormParams.foreach { case (k, v) =>
281+
if (put.getFormParams.asScala.nonEmpty) {
282+
put.getFormParams.asScala.foreach { case (k, v) =>
279283
if (v != null) nameValuePairs.add(new BasicNameValuePair(k, v.toString))
280284
}
281285
}
@@ -289,20 +293,21 @@ abstract class AbstractHttpClient(clientConfig: ClientConfig, clientName: String
289293
httpPut.setEntity(stringEntity)
290294
}
291295

292-
if (requestAction.getHeaders.nonEmpty) {
293-
requestAction.getHeaders.foreach { case (k, v) =>
296+
if (requestAction.getHeaders.asScala.nonEmpty) {
297+
requestAction.getHeaders.asScala.foreach { case (k, v) =>
294298
if (k != null && v != null) httpPut.addHeader(k.toString, v.toString)
295299
}
296300
}
297301
httpPut
298302
case upload: UploadAction =>
299303
val httpPost = new HttpPost(realURL)
300304
val builder = MultipartEntityBuilder.create()
301-
if (upload.inputStreams != null)
302-
upload.inputStreams.foreach { case (k, v) =>
305+
if (upload.inputStreams != null) {
306+
upload.inputStreams.asScala.foreach { case (k, v) =>
303307
builder.addBinaryBody(k, v, ContentType.create("multipart/form-data"), k)
304308
}
305-
upload.binaryBodies.foreach(binaryBody =>
309+
}
310+
upload.binaryBodies.asScala.foreach(binaryBody =>
306311
builder.addBinaryBody(
307312
binaryBody.parameterName,
308313
binaryBody.inputStream,
@@ -312,14 +317,15 @@ abstract class AbstractHttpClient(clientConfig: ClientConfig, clientName: String
312317
)
313318
upload match {
314319
case get: GetAction =>
315-
get.getParameters.retain((k, v) => v != null && k != null).foreach { case (k, v) =>
316-
if (k != null && v != null) builder.addTextBody(k.toString, v.toString)
320+
get.getParameters.asScala.retain((k, v) => v != null && k != null).foreach {
321+
case (k, v) =>
322+
if (k != null && v != null) builder.addTextBody(k.toString, v.toString)
317323
}
318324
case _ =>
319325
}
320326
upload match {
321327
case get: GetAction =>
322-
get.getHeaders.retain((k, v) => v != null && k != null).foreach { case (k, v) =>
328+
get.getHeaders.asScala.retain((k, v) => v != null && k != null).foreach { case (k, v) =>
323329
if (k != null && v != null) httpPost.addHeader(k.toString, v.toString)
324330
}
325331
case _ =>
@@ -329,20 +335,20 @@ abstract class AbstractHttpClient(clientConfig: ClientConfig, clientName: String
329335
httpPost
330336
case post: POSTAction =>
331337
val httpPost = new HttpPost(realURL)
332-
if (post.getParameters.nonEmpty || post.getFormParams.nonEmpty) {
338+
if (post.getParameters.asScala.nonEmpty || post.getFormParams.asScala.nonEmpty) {
333339
val nvps = new util.ArrayList[NameValuePair]
334-
if (post.getParameters.nonEmpty) {
335-
post.getParameters.foreach { case (k, v) =>
340+
if (post.getParameters.asScala.nonEmpty) {
341+
post.getParameters.asScala.foreach { case (k, v) =>
336342
if (v != null) nvps.add(new BasicNameValuePair(k, v.toString))
337343
}
338344
httpPost.setEntity(new UrlEncodedFormEntity(nvps))
339-
} else if (post.getFormParams.nonEmpty) {
340-
post.getFormParams.foreach { case (k, v) =>
345+
} else if (post.getFormParams.asScala.nonEmpty) {
346+
post.getFormParams.asScala.foreach { case (k, v) =>
341347
if (v != null) nvps.add(new BasicNameValuePair(k, v.toString))
342348
}
343349
val entity: HttpEntity = EntityBuilder
344350
.create()
345-
. /*setContentEncoding("UTF-8").*/
351+
. /* setContentEncoding("UTF-8"). */
346352
setContentType(ContentType.create("application/x-www-form-urlencoded", Consts.UTF_8))
347353
.setParameters(nvps)
348354
.build();
@@ -356,22 +362,22 @@ abstract class AbstractHttpClient(clientConfig: ClientConfig, clientName: String
356362
httpPost.setEntity(stringEntity)
357363
}
358364

359-
if (requestAction.getHeaders.nonEmpty) {
360-
requestAction.getHeaders.foreach { case (k, v) =>
365+
if (requestAction.getHeaders.asScala.nonEmpty) {
366+
requestAction.getHeaders.asScala.foreach { case (k, v) =>
361367
if (k != null && v != null) httpPost.addHeader(k.toString, v.toString)
362368
}
363369
}
364370
httpPost
365371
case get: GetAction =>
366372
val builder = new URIBuilder(realURL)
367373
if (!get.getParameters.isEmpty) {
368-
get.getParameters.foreach { case (k, v) =>
374+
get.getParameters.asScala.foreach { case (k, v) =>
369375
if (k != null && v != null) builder.addParameter(k.toString, v.toString)
370376
}
371377
}
372378
val httpGet = new HttpGet(builder.build())
373-
if (requestAction.getHeaders.nonEmpty) {
374-
requestAction.getHeaders.foreach { case (k, v) =>
379+
if (requestAction.getHeaders.asScala.nonEmpty) {
380+
requestAction.getHeaders.asScala.foreach { case (k, v) =>
375381
if (k != null && v != null) httpGet.addHeader(k.toString, v.toString)
376382
}
377383
}
@@ -382,8 +388,8 @@ abstract class AbstractHttpClient(clientConfig: ClientConfig, clientName: String
382388
stringEntity.setContentEncoding(Configuration.BDP_ENCODING.getValue)
383389
stringEntity.setContentType("application/json")
384390
httpost.setEntity(stringEntity)
385-
if (requestAction.getHeaders.nonEmpty) {
386-
requestAction.getHeaders.foreach { case (k, v) =>
391+
if (requestAction.getHeaders.asScala.nonEmpty) {
392+
requestAction.getHeaders.asScala.foreach { case (k, v) =>
387393
if (k != null && v != null) httpost.addHeader(k.toString, v.toString)
388394
}
389395
}
@@ -400,9 +406,9 @@ abstract class AbstractHttpClient(clientConfig: ClientConfig, clientName: String
400406
): CloseableHttpResponse = {
401407
val readTimeOut = waitTime.getOrElse(clientConfig.getReadTimeout)
402408
val connectTimeOut =
403-
if (clientConfig.getConnectTimeout > 1000 || clientConfig.getConnectTimeout < 0)
409+
if (clientConfig.getConnectTimeout > 1000 || clientConfig.getConnectTimeout < 0) {
404410
clientConfig.getConnectTimeout
405-
else CONNECT_TIME_OUT
411+
} else CONNECT_TIME_OUT
406412
val requestConfig = RequestConfig.custom
407413
.setConnectTimeout(connectTimeOut.toInt)
408414
.setConnectionRequestTimeout(connectTimeOut.toInt)
@@ -455,9 +461,9 @@ abstract class AbstractHttpClient(clientConfig: ClientConfig, clientName: String
455461
): CloseableHttpResponse = {
456462
val readTimeOut = waitTime.getOrElse(clientConfig.getReadTimeout)
457463
val connectTimeOut =
458-
if (clientConfig.getConnectTimeout > 1000 || clientConfig.getConnectTimeout < 0)
464+
if (clientConfig.getConnectTimeout > 1000 || clientConfig.getConnectTimeout < 0) {
459465
clientConfig.getConnectTimeout
460-
else CONNECT_TIME_OUT
466+
} else CONNECT_TIME_OUT
461467
val requestConfig = RequestConfig.custom
462468
.setConnectTimeout(connectTimeOut.toInt)
463469
.setConnectionRequestTimeout(connectTimeOut.toInt)
@@ -485,15 +491,16 @@ abstract class AbstractHttpClient(clientConfig: ClientConfig, clientName: String
485491
entity.getContentEncoding != null && StringUtils.isNotBlank(
486492
entity.getContentEncoding.getValue
487493
)
488-
) entity.getContentEncoding.getValue.toLowerCase match {
489-
case "gzip" => new GzipDecompressingEntity(entity).getContent
490-
case "deflate" => new DeflateDecompressingEntity(entity).getContent
491-
case str =>
492-
throw new HttpClientResultException(
493-
s"request failed! Reason: not support decompress type $str."
494-
)
495-
}
496-
else entity.getContent
494+
) {
495+
entity.getContentEncoding.getValue.toLowerCase(Locale.getDefault) match {
496+
case "gzip" => new GzipDecompressingEntity(entity).getContent
497+
case "deflate" => new DeflateDecompressingEntity(entity).getContent
498+
case str =>
499+
throw new HttpClientResultException(
500+
s"request failed! Reason: not support decompress type $str."
501+
)
502+
}
503+
} else entity.getContent
497504
download.write(inputStream, response)
498505
Result()
499506
case heartbeat: HeartbeatAction =>

linkis-commons/linkis-httpclient/src/main/scala/org/apache/linkis/httpclient/authentication/AbstractAuthenticationStrategy.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ abstract class AbstractAuthenticationStrategy extends AuthenticationStrategy wit
6868
val authenticationAction = userNameToAuthentications.get(key)
6969
authenticationAction.updateLastAccessTime()
7070
authenticationAction
71-
} else
71+
} else {
7272
key.intern() synchronized {
7373
var authentication = userNameToAuthentications.get(key)
7474
if (authentication == null || isTimeout(authentication)) {
@@ -78,6 +78,7 @@ abstract class AbstractAuthenticationStrategy extends AuthenticationStrategy wit
7878
}
7979
authentication
8080
}
81+
}
8182
}
8283

8384
def tryLogin(requestAction: Action, serverUrl: String): Authentication = {

linkis-commons/linkis-httpclient/src/main/scala/org/apache/linkis/httpclient/discovery/AbstractDiscovery.scala

+4-3
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,11 @@ abstract class AbstractDiscovery extends Discovery with Closeable with Logging {
9898
logger.info("start Discovery thread")
9999
client.execute(getHeartbeatAction(serverUrl), 3000) match {
100100
case heartbeat: HeartbeatResult =>
101-
if (!heartbeat.isHealthy)
101+
if (!heartbeat.isHealthy) {
102102
throw new DiscoveryException(
103103
s"connect to serverUrl $serverUrl failed! Reason: gateway server is unhealthy!"
104104
)
105-
else discoveryListeners.asScala.foreach(_.onServerDiscovered(serverUrl))
105+
} else discoveryListeners.asScala.foreach(_.onServerDiscovered(serverUrl))
106106
}
107107

108108
Utils.defaultScheduler.scheduleAtFixedRate(
@@ -133,8 +133,9 @@ abstract class AbstractDiscovery extends Discovery with Closeable with Logging {
133133
unhealthyServerInstances synchronized unhealthyServerInstances.remove(serverUrl)
134134
discoveryListeners.asScala.foreach(_.onServerHealthy(serverUrl))
135135
serverInstances synchronized serverInstances.add(serverUrl)
136-
} else if (serverInstances.contains(serverUrl))
136+
} else if (serverInstances.contains(serverUrl)) {
137137
serverInstances synchronized serverInstances.remove(serverUrl)
138+
}
138139
}) { case _: ConnectException =>
139140
unhealthyServerInstances synchronized unhealthyServerInstances.remove(serverUrl)
140141
serverInstances synchronized serverInstances.remove(serverUrl)

linkis-commons/linkis-httpclient/src/main/scala/org/apache/linkis/httpclient/request/UploadAction.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ trait UploadAction extends UserAction {
2929
* The file to be uploaded, the key is the parameter name, and the value is the file path.
3030
* 需要上传的文件,key为参数名,value为文件路径
3131
*/
32-
@Deprecated val files: util.Map[String, String]
32+
@deprecated val files: util.Map[String, String]
3333

3434
/**
3535
* The inputStream that needs to be uploaded, the key is the parameter name, and the value is the
@@ -41,7 +41,7 @@ trait UploadAction extends UserAction {
4141
* The inputStream that needs to be uploaded, the key is the parameter name, and the value is the
4242
* fileName of inputStream. 需要上传的输入流,key为参数名,value为输入流的文件名
4343
*/
44-
@Deprecated def inputStreamNames: util.Map[String, String] = new util.HashMap[String, String]()
44+
@deprecated def inputStreamNames: util.Map[String, String] = new util.HashMap[String, String]()
4545
def binaryBodies: util.List[BinaryBody] = new util.ArrayList[BinaryBody](0)
4646
def user: Option[String] = Option(getUser)
4747

linkis-commons/linkis-httpclient/src/main/scala/org/apache/linkis/httpclient/response/HashMapHttpResult.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,11 @@ class HashMapHttpResult extends HttpResult {
4444
url: String,
4545
contentType: String
4646
): Unit = {
47-
if (statusCode != 200)
47+
if (statusCode != 200) {
4848
throw new HttpClientResultException(
4949
s"URL $url request failed! ResponseBody is $responseBody."
5050
)
51+
}
5152
resultMap = JsonUtils.jackson.readValue(responseBody, classOf[util.Map[String, Object]])
5253
this.responseBody = responseBody
5354
this.statusCode = statusCode

scalastyle-config.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ This file is divided into 3 sections:
9494
</check>
9595

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

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

0 commit comments

Comments
 (0)