|
15 | 15 | */ |
16 | 16 | package com.oceanbase.odc.service.integration.client; |
17 | 17 |
|
| 18 | +import java.util.HashMap; |
| 19 | +import java.util.Map; |
| 20 | + |
18 | 21 | import javax.annotation.PostConstruct; |
19 | 22 |
|
20 | 23 | import org.apache.http.client.HttpClient; |
|
25 | 28 | import org.springframework.beans.factory.annotation.Value; |
26 | 29 | import org.springframework.stereotype.Component; |
27 | 30 |
|
| 31 | +import com.oceanbase.odc.common.util.MapUtils; |
28 | 32 | import com.oceanbase.odc.core.shared.Verify; |
29 | 33 | import com.oceanbase.odc.core.shared.constant.ErrorCodes; |
30 | 34 | import com.oceanbase.odc.core.shared.exception.ExternalServiceError; |
31 | 35 | import com.oceanbase.odc.core.shared.exception.UnexpectedException; |
32 | 36 | import com.oceanbase.odc.service.integration.HttpOperationService; |
33 | 37 | import com.oceanbase.odc.service.integration.model.ApprovalProperties; |
34 | 38 | import com.oceanbase.odc.service.integration.model.Encryption; |
| 39 | +import com.oceanbase.odc.service.integration.model.IntegrationProperties; |
| 40 | +import com.oceanbase.odc.service.integration.model.IntegrationProperties.ApiProperties; |
35 | 41 | import com.oceanbase.odc.service.integration.model.IntegrationProperties.HttpProperties; |
36 | 42 | import com.oceanbase.odc.service.integration.model.OdcIntegrationResponse; |
| 43 | +import com.oceanbase.odc.service.integration.model.SqlCheckResult; |
37 | 44 | import com.oceanbase.odc.service.integration.model.SqlCheckStatus; |
38 | 45 | import com.oceanbase.odc.service.integration.model.SqlInterceptorProperties; |
| 46 | +import com.oceanbase.odc.service.integration.model.SqlInterceptorProperties.CallBackProperties; |
39 | 47 | import com.oceanbase.odc.service.integration.model.SqlInterceptorProperties.CheckProperties; |
40 | 48 | import com.oceanbase.odc.service.integration.model.TemplateVariables; |
41 | 49 | import com.oceanbase.odc.service.integration.model.TemplateVariables.Variable; |
@@ -82,40 +90,65 @@ public void init() { |
82 | 90 | * @param variables Template variables for building request, more details reference {@link Variable} |
83 | 91 | * @return The check result {@link SqlCheckStatus} of SQL content |
84 | 92 | */ |
85 | | - public SqlCheckStatus check(@NonNull SqlInterceptorProperties properties, TemplateVariables variables) { |
| 93 | + public SqlCheckResult check(@NonNull SqlInterceptorProperties properties, TemplateVariables variables) { |
86 | 94 | CheckProperties check = properties.getApi().getCheck(); |
87 | 95 | HttpProperties http = properties.getHttp(); |
88 | 96 | Encryption encryption = properties.getEncryption(); |
89 | | - HttpUriRequest request; |
90 | | - try { |
91 | | - request = httpService.buildHttpRequest(check, http, encryption, variables); |
92 | | - } catch (Exception e) { |
93 | | - throw new UnexpectedException("Build request failed: " + e.getMessage()); |
94 | | - } |
95 | | - OdcIntegrationResponse response; |
96 | | - try { |
97 | | - response = httpClient.execute(request, new OdcIntegrationResponseHandler()); |
98 | | - } catch (Exception e) { |
99 | | - throw new ExternalServiceError(ErrorCodes.ExternalServiceError, |
100 | | - "Request execute failed: " + e.getMessage()); |
101 | | - } |
102 | | - response.setContent(EncryptionUtil.decrypt(response.getContent(), encryption)); |
| 97 | + OdcIntegrationResponse response = getIntegrationResponse(http, check, variables, encryption); |
103 | 98 | try { |
| 99 | + SqlCheckStatus sqlCheckStatus = null; |
104 | 100 | String expression = check.getRequestSuccessExpression(); |
105 | 101 | boolean valid = httpService.extractHttpResponse(response, expression, Boolean.class); |
106 | 102 | Verify.verify(valid, "Response is invalid, except: " + expression + ", response body: " + response); |
107 | 103 | if (httpService.extractHttpResponse(response, check.getInWhiteListExpression(), Boolean.class)) { |
108 | | - return SqlCheckStatus.IN_WHITE_LIST; |
| 104 | + sqlCheckStatus = SqlCheckStatus.IN_WHITE_LIST; |
109 | 105 | } else if (httpService.extractHttpResponse(response, check.getInBlackListExpression(), Boolean.class)) { |
110 | | - return SqlCheckStatus.IN_BLACK_LIST; |
| 106 | + sqlCheckStatus = SqlCheckStatus.IN_BLACK_LIST; |
111 | 107 | } else if (httpService.extractHttpResponse(response, check.getNeedReviewExpression(), Boolean.class)) { |
112 | | - return SqlCheckStatus.NEED_REVIEW; |
| 108 | + sqlCheckStatus = SqlCheckStatus.NEED_REVIEW; |
113 | 109 | } else { |
114 | 110 | throw new RuntimeException( |
115 | 111 | "Response mismatch any check result expression, response body: " + response.getContent()); |
116 | 112 | } |
| 113 | + // try extract value from response for future request |
| 114 | + Map<String, String> extractedResponse = new HashMap<>(); |
| 115 | + CallBackProperties callBackProperties = check.getCallback(); |
| 116 | + if (null != callBackProperties && !MapUtils.isEmpty(callBackProperties.getResponseExtractExpressions())) { |
| 117 | + for (Map.Entry<String, String> responseExtractExpressionEntrySet : callBackProperties |
| 118 | + .getResponseExtractExpressions().entrySet()) { |
| 119 | + String key = responseExtractExpressionEntrySet.getKey(); |
| 120 | + String responseExtractExpression = responseExtractExpressionEntrySet.getValue(); |
| 121 | + String value = extractedResponse.put(key, |
| 122 | + httpService.extractHttpResponse(response, responseExtractExpression, String.class)); |
| 123 | + if (null != value) { |
| 124 | + extractedResponse.put(key, value); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + return new SqlCheckResult(extractedResponse, sqlCheckStatus); |
117 | 129 | } catch (Exception e) { |
118 | 130 | throw new UnexpectedException("Extract SQL check result failed: " + e.getMessage()); |
119 | 131 | } |
120 | 132 | } |
| 133 | + |
| 134 | + public OdcIntegrationResponse getIntegrationResponse(HttpProperties http, |
| 135 | + @NonNull IntegrationProperties.ApiProperties api, TemplateVariables variables, Encryption encryption) { |
| 136 | + HttpUriRequest request; |
| 137 | + try { |
| 138 | + request = httpService.buildHttpRequest(api, http, encryption, variables); |
| 139 | + } catch (Exception e) { |
| 140 | + throw new UnexpectedException("Build request failed: " + e.getMessage()); |
| 141 | + } |
| 142 | + OdcIntegrationResponse response; |
| 143 | + try { |
| 144 | + response = httpClient.execute(request, new OdcIntegrationResponseHandler()); |
| 145 | + } catch (Exception e) { |
| 146 | + throw new ExternalServiceError(ErrorCodes.ExternalServiceError, |
| 147 | + "Request execute failed: " + e.getMessage()); |
| 148 | + } |
| 149 | + response.setContent(EncryptionUtil.decrypt(response.getContent(), encryption)); |
| 150 | + log.info("sqlInterceptorClient getIntegrationResponse request = {}, response ={}", request, |
| 151 | + response.getContent()); |
| 152 | + return response; |
| 153 | + } |
121 | 154 | } |
0 commit comments