From 5cd3a91ef82618b86a7ceb4f9e5d061123e7d58b Mon Sep 17 00:00:00 2001 From: Sudeep7353 <103890430+Sudeep7353@users.noreply.github.com> Date: Thu, 3 Oct 2024 13:59:28 +0530 Subject: [PATCH] ES-836 : Get oauth client endpoint should return client name language map. (#901) * ES-836 : Get oauth client endpoint should return client name language map. Signed-off-by: sudeep * ES-836 : Get oauth client endpoint should return client name language map. Signed-off-by: sudeep --------- Signed-off-by: sudeep --- .../impl/ClientManagementServiceImpl.java | 60 ++++++++++++++++--- 1 file changed, 51 insertions(+), 9 deletions(-) diff --git a/partner/partner-management-service/src/main/java/io/mosip/pms/oauth/client/service/impl/ClientManagementServiceImpl.java b/partner/partner-management-service/src/main/java/io/mosip/pms/oauth/client/service/impl/ClientManagementServiceImpl.java index ea4c4aab9c..146ab6be89 100644 --- a/partner/partner-management-service/src/main/java/io/mosip/pms/oauth/client/service/impl/ClientManagementServiceImpl.java +++ b/partner/partner-management-service/src/main/java/io/mosip/pms/oauth/client/service/impl/ClientManagementServiceImpl.java @@ -2,6 +2,7 @@ import java.util.*; +import com.fasterxml.jackson.databind.JsonNode; import io.mosip.kernel.core.authmanager.authadapter.model.AuthUserDetails; import io.mosip.pms.common.entity.*; import io.mosip.pms.common.entity.ClientDetail; @@ -74,6 +75,8 @@ public class ClientManagementServiceImpl implements ClientManagementService { public static final String ACTIVE = "ACTIVE"; public static final String BLANK_STRING = ""; public static final String VERSION = "1.0"; + public static final String NONE_LANG_KEY = "@none"; + public static final String ENG_KEY = "eng"; @Value("${mosip.pms.api.id.oauth.clients.get}") private String getClientsId; @@ -123,19 +126,24 @@ public ClientDetailResponse createOIDCClient(ClientDetailCreateRequest createReq response.setStatus(clientDetail.getStatus()); return response; } - + @Override public ClientDetailResponse createOAuthClient(ClientDetailCreateRequestV2 createRequest) throws Exception { ProcessedClientDetail processedClientDetail = processCreateOIDCClient(createRequest); ClientDetail clientDetail = processedClientDetail.getClientDetail(); callEsignetService(clientDetail, environment.getProperty("mosip.pms.esignet.oauth-client-create-url"), true, createRequest.getClientNameLangMap()); + String clientName=getClientNameLanguageMapAsJsonString( + createRequest.getClientNameLangMap(), + createRequest.getName() + ); + clientDetail.setName(clientName); publishClientData(processedClientDetail.getPartner(), processedClientDetail.getPolicy(), clientDetail); clientDetailRepository.save(clientDetail); var response = new ClientDetailResponse(); response.setClientId(clientDetail.getId()); - response.setStatus(clientDetail.getStatus()); + response.setStatus(clientDetail.getStatus()); return response; - + } public ProcessedClientDetail processCreateOIDCClient(ClientDetailCreateRequest createRequest) throws NoSuchAlgorithmException { @@ -448,22 +456,27 @@ public ClientDetailResponse updateOIDCClient(String clientId, ClientDetailUpdate response.setStatus(clientDetail.getStatus()); notify(MapperUtils.mapClientDataToPublishDto(clientDetail), EventType.OIDC_CLIENT_UPDATED); return response; - } - - + } + + @Override public ClientDetailResponse updateOAuthClient(String clientId, ClientDetailUpdateRequestV2 updateRequest) throws Exception { - + ClientDetail clientDetail = processUpdateOIDCClient(clientId,updateRequest); makeUpdateEsignetServiceCall(clientDetail, environment.getProperty("mosip.pms.esignet.oauth-client-update-url"), true, updateRequest.getClientNameLangMap()); + String clientName=getClientNameLanguageMapAsJsonString( + updateRequest.getClientNameLangMap(), + updateRequest.getClientName() + ); + clientDetail.setName(clientName); clientDetail = clientDetailRepository.save(clientDetail); var response = new ClientDetailResponse(); response.setClientId(clientDetail.getId()); response.setStatus(clientDetail.getStatus()); notify(MapperUtils.mapClientDataToPublishDto(clientDetail), EventType.OIDC_CLIENT_UPDATED); return response; - } + } @@ -615,6 +628,12 @@ public io.mosip.pms.oauth.client.dto.ClientDetail getClientDetails(String client return dto; } + private String getClientNameLanguageMapAsJsonString(Map clientNameMap, String clientName) { + clientNameMap.put(NONE_LANG_KEY, clientName); + JSONObject clientNameObject = new JSONObject(clientNameMap); + return clientNameObject.toString(); + } + @Override public ResponseWrapperV2> getClients() { ResponseWrapperV2> responseWrapper = new ResponseWrapperV2<>(); @@ -648,7 +667,7 @@ public ResponseWrapperV2> getClients() { oauthClientDto.setPartnerId(partnerId); oauthClientDto.setUserId(userId); oauthClientDto.setClientId(clientDetail.getId()); - oauthClientDto.setClientName(clientDetail.getName()); + oauthClientDto.setClientName(getClientName(clientDetail.getName())); oauthClientDto.setPolicyGroupId(policyGroup.getId()); oauthClientDto.setPolicyGroupName(policyGroup.getName()); oauthClientDto.setPolicyGroupDescription(policyGroup.getDesc()); @@ -694,6 +713,29 @@ private String getUserId() { private AuthUserDetails authUserDetails() { return (AuthUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); } + + private String getClientName(String jsonString) { + try { + JsonNode jsonNode = objectMapper.readTree(jsonString); + + JsonNode engNode = jsonNode.get(ENG_KEY); + if (engNode != null && engNode.isTextual()) { + return engNode.asText(); + } + + JsonNode noneNode = jsonNode.get(NONE_LANG_KEY); + if (noneNode != null && noneNode.isTextual()) { + return noneNode.asText(); + } + + // If neither "eng" nor "@none" is present, return the original string + return jsonString; + } catch (JsonProcessingException e) { + // If the string is not a valid JSON, return it as is + return jsonString; + } + } + /** * * @param commaSeparatedString