diff --git a/src/backend/ci/core/auth/biz-auth-rbac/src/main/kotlin/com/tencent/devops/auth/service/ItsmService.kt b/src/backend/ci/core/auth/biz-auth-rbac/src/main/kotlin/com/tencent/devops/auth/service/ItsmService.kt index 7473d0f562e..a9c21c79c62 100644 --- a/src/backend/ci/core/auth/biz-auth-rbac/src/main/kotlin/com/tencent/devops/auth/service/ItsmService.kt +++ b/src/backend/ci/core/auth/biz-auth-rbac/src/main/kotlin/com/tencent/devops/auth/service/ItsmService.kt @@ -1,7 +1,7 @@ package com.tencent.devops.auth.service +import com.fasterxml.jackson.databind.JavaType import com.fasterxml.jackson.databind.ObjectMapper -import com.fasterxml.jackson.module.kotlin.readValue import com.tencent.bk.sdk.iam.dto.itsm.ItsmAttrs import com.tencent.bk.sdk.iam.dto.itsm.ItsmColumn import com.tencent.bk.sdk.iam.dto.itsm.ItsmContentDTO @@ -39,9 +39,14 @@ class ItsmService @Autowired constructor( private val itsmUrlPrefix: String = "" fun cancelItsmApplication(itsmCancelApplicationInfo: ItsmCancelApplicationInfo): Boolean { - val itsmResponseDTO = executeHttpPost( + val responseType = objectMapper.typeFactory.constructParametricType( + ResponseDTO::class.java, + objectMapper.typeFactory.constructParametricType(List::class.java, Any::class.java) + ) + val itsmResponseDTO: ResponseDTO> = executeHttpPost( urlSuffix = ITSM_APPLICATION_CANCEL_URL_SUFFIX, - body = itsmCancelApplicationInfo + body = itsmCancelApplicationInfo, + responseType = responseType ) if (itsmResponseDTO.message != "success") { logger.warn("cancel itsm application failed!$itsmCancelApplicationInfo") @@ -56,7 +61,11 @@ class ItsmService @Autowired constructor( fun verifyItsmToken(token: String) { val param = mapOf("token" to token) - val itsmResponseDTO = executeHttpPost(ITSM_TOKEN_VERITY_URL_SUFFIX, param) + val responseType = objectMapper.typeFactory.constructParametricType( + ResponseDTO::class.java, + objectMapper.typeFactory.constructParametricType(Map::class.java, Any::class.java, Any::class.java) + ) + val itsmResponseDTO: ResponseDTO> = executeHttpPost(ITSM_TOKEN_VERITY_URL_SUFFIX, param, responseType) val itsmApiResData = itsmResponseDTO.data logger.info("itsmApiResData:$itsmApiResData") @@ -70,11 +79,16 @@ class ItsmService @Autowired constructor( } fun getItsmTicketStatus(sn: String): String { - val itsmResponseDTO = executeHttpGet(String.format(ITSM_TICKET_STATUS_URL_SUFFIX, sn)) + val responseType = objectMapper.typeFactory.constructParametricType( + ResponseDTO::class.java, + objectMapper.typeFactory.constructParametricType(Map::class.java, Any::class.java, Any::class.java) + ) + val itsmResponseDTO: ResponseDTO> = + executeHttpGet(String.format(ITSM_TICKET_STATUS_URL_SUFFIX, sn), responseType) return itsmResponseDTO.data?.get("current_status").toString() } - private fun executeHttpPost(urlSuffix: String, body: Any): ResponseDTO> { + private fun executeHttpPost(urlSuffix: String, body: Any, responseType: JavaType): ResponseDTO { val headerStr = objectMapper.writeValueAsString(mapOf("bk_app_code" to appCode, "bk_app_secret" to appSecret)) .replace("\\s".toRegex(), "") val requestBody = objectMapper.writeValueAsString(body) @@ -86,10 +100,10 @@ class ItsmService @Autowired constructor( .post(requestBody) .addHeader("x-bkapi-authorization", headerStr) .build() - return executeHttpRequest(url, request) + return executeHttpRequest(url, request, responseType) } - private fun executeHttpGet(urlSuffix: String): ResponseDTO> { + private fun executeHttpGet(urlSuffix: String, responseType: JavaType): ResponseDTO { val headerStr = objectMapper.writeValueAsString(mapOf("bk_app_code" to appCode, "bk_app_secret" to appSecret)) .replace("\\s".toRegex(), "") val url = itsmUrlPrefix + urlSuffix @@ -98,17 +112,17 @@ class ItsmService @Autowired constructor( .addHeader("x-bkapi-authorization", headerStr) .get() .build() - return executeHttpRequest(url, request) + return executeHttpRequest(url, request, responseType) } - private fun executeHttpRequest(url: String, request: Request): ResponseDTO> { + private fun executeHttpRequest(url: String, request: Request, responseType: JavaType): ResponseDTO { OkhttpUtils.doHttp(request).use { if (!it.isSuccessful) { logger.warn("request failed, uri:($url)|response: ($it)") throw RemoteServiceException("request failed, response:($it)") } val responseStr = it.body!!.string() - val responseDTO = objectMapper.readValue>>(responseStr) + val responseDTO: ResponseDTO = objectMapper.readValue(responseStr, responseType) if (responseDTO.code != 0L || !responseDTO.result) { // 请求错误 logger.warn("request failed, url:($url)|response:($it)")