|
1 | 1 | package com.datamate.gateway.infrastructure.client.impl; |
2 | 2 |
|
| 3 | +import com.datamate.gateway.common.config.SslIgnoreHttpClientFactory; |
3 | 4 | import com.datamate.gateway.infrastructure.client.OmsExtensionService; |
| 5 | +import com.datamate.gateway.infrastructure.client.dto.ResourceGroup; |
| 6 | +import com.datamate.gateway.infrastructure.client.dto.Resp; |
| 7 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 8 | + |
| 9 | +import lombok.extern.slf4j.Slf4j; |
| 10 | + |
| 11 | +import org.apache.hc.client5.http.classic.methods.HttpGet; |
| 12 | +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; |
| 13 | +import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; |
| 14 | +import org.apache.hc.core5.http.ParseException; |
| 15 | +import org.apache.hc.core5.http.io.entity.EntityUtils; |
| 16 | +import org.springframework.beans.factory.annotation.Value; |
4 | 17 | import org.springframework.stereotype.Service; |
5 | 18 |
|
| 19 | +import java.io.IOException; |
| 20 | +import java.util.List; |
| 21 | + |
6 | 22 | /** |
7 | 23 | * OmsExtensionServiceImpl is an implementation of OmsExtensionService. |
8 | | - * |
9 | | - * @author songyongtan |
10 | | - * @date 2026-03-17 |
| 24 | + * |
| 25 | + * @author MoeexT |
| 26 | + * @since 2026-03-17 |
11 | 27 | */ |
| 28 | +@Slf4j |
12 | 29 | @Service |
13 | 30 | public class OmsExtensionServiceImpl implements OmsExtensionService { |
| 31 | + @Value("${OMS_EXTENSION_URL:https://oms-extension:8021}") |
| 32 | + private final String omsExtensionUrl; |
| 33 | + |
| 34 | + private final ObjectMapper objectMapper; |
| 35 | + |
| 36 | + private CloseableHttpClient httpClient; |
| 37 | + |
| 38 | + public OmsExtensionServiceImpl(@Value("${oms.service.url}") String omsExtensionUrl, ObjectMapper objectMapper, |
| 39 | + SslIgnoreHttpClientFactory sslIgnoreHttpClientFactory) { |
| 40 | + this.omsExtensionUrl = omsExtensionUrl; |
| 41 | + this.objectMapper = objectMapper; |
| 42 | + try { |
| 43 | + this.httpClient = sslIgnoreHttpClientFactory.getHttpClient(); |
| 44 | + } catch (Exception e) { |
| 45 | + log.error("Failed to create SSL ignore HTTP client", e); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Get the group ID of the user's user group |
| 51 | + * |
| 52 | + * @param userName the username |
| 53 | + * @return resource-group-id of this user |
| 54 | + */ |
14 | 55 | @Override |
15 | 56 | public String getUserGroupId(String userName) { |
16 | | - return userName; |
| 57 | + try { |
| 58 | + String fullPath = this.omsExtensionUrl + "/ui/v1/resource-groups/user/" + userName + "/resource-group"; |
| 59 | + HttpGet httpGet = new HttpGet(fullPath); |
| 60 | + CloseableHttpResponse response = httpClient.execute(httpGet); |
| 61 | + String responseBody = EntityUtils.toString(response.getEntity()); |
| 62 | + log.info("response code: {}, response body: {}", response.getCode(), responseBody); |
| 63 | + |
| 64 | + Resp<List<ResourceGroup>> resp = objectMapper.readValue(responseBody, |
| 65 | + objectMapper.getTypeFactory().constructParametricType(Resp.class, List.class, ResourceGroup.class)); |
| 66 | + |
| 67 | + if (resp.data() == null || resp.data().isEmpty()) { |
| 68 | + return null; |
| 69 | + } |
| 70 | + |
| 71 | + return resp.data().getFirst().getId(); |
| 72 | + } catch (IOException | ParseException e) { |
| 73 | + log.error("Failed to get user name from OMS service", e); |
| 74 | + return null; |
| 75 | + } |
17 | 76 | } |
18 | 77 | } |
0 commit comments