From c830ba892cf746db11f28ca6e17f6c0651817298 Mon Sep 17 00:00:00 2001 From: Sowmya Ujjappa Banakar Date: Mon, 4 Mar 2024 15:22:42 +0530 Subject: [PATCH 1/2] MOSIP-31696 intial code changes Signed-off-by: Sowmya Ujjappa Banakar --- .../stage/CredentialRequestorStage.java | 971 +++++++++--------- .../util/CredentialPartnerUtil.java | 455 ++++---- 2 files changed, 727 insertions(+), 699 deletions(-) diff --git a/registration-processor/post-processor/registration-processor-credential-requestor-stage/src/main/java/io/mosip/registration/processor/credentialrequestor/stage/CredentialRequestorStage.java b/registration-processor/post-processor/registration-processor-credential-requestor-stage/src/main/java/io/mosip/registration/processor/credentialrequestor/stage/CredentialRequestorStage.java index 2932fe84bff..50810572c81 100644 --- a/registration-processor/post-processor/registration-processor-credential-requestor-stage/src/main/java/io/mosip/registration/processor/credentialrequestor/stage/CredentialRequestorStage.java +++ b/registration-processor/post-processor/registration-processor-credential-requestor-stage/src/main/java/io/mosip/registration/processor/credentialrequestor/stage/CredentialRequestorStage.java @@ -1,478 +1,495 @@ -package io.mosip.registration.processor.credentialrequestor.stage; - -import com.fasterxml.jackson.databind.ObjectMapper; -import io.mosip.kernel.core.exception.BaseUncheckedException; -import io.mosip.kernel.core.exception.ServiceError; -import io.mosip.kernel.core.logger.spi.Logger; -import io.mosip.kernel.core.util.DateUtils; -import io.mosip.registration.processor.core.abstractverticle.*; -import io.mosip.registration.processor.core.code.*; -import io.mosip.registration.processor.core.code.EventId; -import io.mosip.registration.processor.core.code.EventName; -import io.mosip.registration.processor.core.code.EventType; -import io.mosip.registration.processor.core.common.rest.dto.ErrorDTO; -import io.mosip.registration.processor.core.constant.*; -import io.mosip.registration.processor.core.exception.ApisResourceAccessException; -import io.mosip.registration.processor.core.exception.util.PlatformErrorMessages; -import io.mosip.registration.processor.core.exception.util.PlatformSuccessMessages; -import io.mosip.registration.processor.core.http.RequestWrapper; -import io.mosip.registration.processor.core.http.ResponseWrapper; -import io.mosip.registration.processor.core.idrepo.dto.CredentialRequestDto; -import io.mosip.registration.processor.core.idrepo.dto.CredentialResponseDto; -import io.mosip.registration.processor.core.idrepo.dto.VidInfoDTO; -import io.mosip.registration.processor.core.idrepo.dto.VidsInfosDTO; -import io.mosip.registration.processor.core.logger.LogDescription; -import io.mosip.registration.processor.core.logger.RegProcessorLogger; -import io.mosip.registration.processor.core.spi.restclient.RegistrationProcessorRestClientService; -import io.mosip.registration.processor.core.status.util.StatusUtil; -import io.mosip.registration.processor.core.status.util.TrimExceptionMessage; -import io.mosip.registration.processor.core.util.JsonUtil; -import io.mosip.registration.processor.credentialrequestor.dto.CredentialPartner; -import io.mosip.registration.processor.credentialrequestor.stage.exception.VidNotAvailableException; -import io.mosip.registration.processor.credentialrequestor.util.CredentialPartnerUtil; -import io.mosip.registration.processor.packet.storage.utils.Utilities; -import io.mosip.registration.processor.rest.client.audit.builder.AuditLogRequestBuilder; -import io.mosip.registration.processor.status.code.RegistrationStatusCode; -import io.mosip.registration.processor.status.dto.InternalRegistrationStatusDto; -import io.mosip.registration.processor.status.dto.RegistrationStatusDto; -import io.mosip.registration.processor.status.service.RegistrationStatusService; -import org.apache.commons.collections.CollectionUtils; -import org.apache.commons.lang.StringUtils; -import org.apache.commons.lang3.exception.ExceptionUtils; -import org.json.JSONArray; -import org.json.simple.JSONObject; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.cloud.context.config.annotation.RefreshScope; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; -import org.springframework.core.env.Environment; -import org.springframework.http.MediaType; -import org.springframework.scheduling.annotation.EnableScheduling; -import org.springframework.scheduling.annotation.Scheduled; -import org.springframework.stereotype.Service; - -import java.io.IOException; -import java.security.NoSuchAlgorithmException; -import java.security.SecureRandom; -import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; -import java.util.*; -import java.util.stream.Collectors; - -/** - * The Class PrintStage. - * - * @author M1048358 Alok - * @author Ranjitha Siddegowda - * @author Sowmya - */ -@RefreshScope -@Service -@Configuration -@EnableScheduling -@ComponentScan(basePackages = { "${mosip.auth.adapter.impl.basepackage}", - "io.mosip.registration.processor.core.config", - "io.mosip.registration.processor.stages.config", - "io.mosip.registration.processor.credentialrequestor.config", - "io.mosip.registrationprocessor.stages.config", - "io.mosip.registration.processor.status.config", - "io.mosip.registration.processor.rest.client.config", - "io.mosip.registration.processor.packet.storage.config", - "io.mosip.registration.processor.packet.manager.config", - "io.mosip.kernel.idobjectvalidator.config", - "io.mosip.registration.processor.core.kernel.beans" }) -public class CredentialRequestorStage extends MosipVerticleAPIManager { - - private static final String STAGE_PROPERTY_PREFIX = "mosip.regproc.credentialrequestor."; - private Random sr = null; - private static final int max = 999999; - private static final int min = 100000; - - /** The reg proc logger. */ - private static Logger regProcLogger = RegProcessorLogger.getLogger(CredentialRequestorStage.class); - - /** The cluster manager url. */ - @Value("${vertx.cluster.configuration}") - private String clusterManagerUrl; - - - /** The core audit request builder. */ - @Autowired - private AuditLogRequestBuilder auditLogRequestBuilder; - - /** The mosip event bus. */ - private MosipEventBus mosipEventBus; - - /** The registration status service. */ - @Autowired - RegistrationStatusService registrationStatusService; - - /** worker pool size. */ - @Value("${worker.pool.size}") - private Integer workerPoolSize; - - /** After this time intervel, message should be considered as expired (In seconds). */ - @Value("${mosip.regproc.credentialrequestor.message.expiry-time-limit}") - private Long messageExpiryTimeLimit; - - @Value("${mosip.registration.processor.encrypt:false}") - private boolean encrypt; - - /** Mosip router for APIs */ - @Autowired - MosipRouter router; - - private static final String SEPERATOR = "::"; - - @Autowired - private RegistrationProcessorRestClientService restClientService; - - @Autowired - private ObjectMapper mapper; - - @Autowired - private Environment env; - - private static final String DATETIME_PATTERN = "mosip.registration.processor.datetime.pattern"; - - private static final String ISSUERS = "mosip.registration.processor.issuer"; - - @Value("#{T(java.util.Arrays).asList('${mosip.registration.processor.credential.default.partner-ids:}')}") - private List defaultPartners; - - private static String COMMA = ","; - private static String HASH_DELIMITER = "#"; - - @Autowired - private Utilities utilities; - - @Autowired - private CredentialPartnerUtil credentialPartnerUtil; - - @Override - protected String getPropertyPrefix() { - return STAGE_PROPERTY_PREFIX; - } - - /** - * Deploy verticle. - */ - public void deployVerticle() { - mosipEventBus = this.getEventBus(this, clusterManagerUrl, workerPoolSize); - this.consumeAndSend(mosipEventBus, MessageBusAddress.PRINTING_BUS_IN, MessageBusAddress.PRINTING_BUS_OUT, - messageExpiryTimeLimit); - } - - /* - * (non-Javadoc) - * - * @see - * io.mosip.registration.processor.core.spi.eventbus.EventBusManager#process( - * java.lang.Object) - */ - @Override - public MessageDTO process(MessageDTO object) { - TrimExceptionMessage trimeExpMessage = new TrimExceptionMessage(); - object.setMessageBusAddress(MessageBusAddress.PRINTING_BUS_IN); - object.setInternalError(Boolean.FALSE); - object.setIsValid(Boolean.FALSE); - LogDescription description = new LogDescription(); - - boolean isTransactionSuccessful = false; - String uin = null; - String refIds = null; - String regId = object.getRid(); - regProcLogger.debug(LoggerFileConstant.SESSIONID.toString(), LoggerFileConstant.REGISTRATIONID.toString(), - regId, "PrintStage::process()::entry"); - - InternalRegistrationStatusDto registrationStatusDto = null; - RequestWrapper requestWrapper = new RequestWrapper<>(); - ResponseWrapper responseWrapper = null; - CredentialResponseDto credentialResponseDto; - try { - registrationStatusDto = registrationStatusService.getRegistrationStatus( - regId, object.getReg_type(), object.getIteration(), object.getWorkflowInstanceId()); - registrationStatusDto - .setLatestTransactionTypeCode(RegistrationTransactionTypeCode.PRINT_SERVICE.toString()); - registrationStatusDto.setRegistrationStageName(getStageName()); - JSONObject jsonObject = utilities.idrepoRetrieveIdentityByRid(regId); - uin = JsonUtil.getJSONValue(jsonObject, IdType.UIN.toString()); - if (uin == null) { - regProcLogger.error(LoggerFileConstant.SESSIONID.toString(), - LoggerFileConstant.REGISTRATIONID.toString(), null, - PlatformErrorMessages.RPR_PRT_UIN_NOT_FOUND_IN_DATABASE.name()); - object.setIsValid(Boolean.FALSE); - isTransactionSuccessful = false; - description.setMessage(PlatformErrorMessages.RPR_PRT_PRINT_REQUEST_FAILED.getMessage()); - description.setCode(PlatformErrorMessages.RPR_PRT_PRINT_REQUEST_FAILED.getCode()); - - registrationStatusDto.setStatusComment( - StatusUtil.UIN_NOT_FOUND_IN_DATABASE.getMessage()); - registrationStatusDto.setSubStatusCode(StatusUtil.UIN_NOT_FOUND_IN_DATABASE.getCode()); - registrationStatusDto - .setLatestTransactionStatusCode(RegistrationTransactionStatusCode.FAILED.toString()); - registrationStatusDto - .setLatestTransactionTypeCode(RegistrationTransactionTypeCode.PRINT_SERVICE.toString()); - - } else { - requestWrapper.setId(env.getProperty("mosip.registration.processor.credential.request.service.id")); - DateTimeFormatter format = DateTimeFormatter.ofPattern(env.getProperty(DATETIME_PATTERN)); - requestWrapper.setVersion("1.0"); - List allIssuerList = credentialPartnerUtil.getAllCredentialPartners().getPartners(); - // filtering with default partner ids and process - List filteredPartners = allIssuerList.stream() - .filter(issuer -> defaultPartners.contains(issuer.getId())) - .filter(issuer -> (issuer.getProcess() == null) || (issuer.getProcess().contains(object.getReg_type()))) - .collect(Collectors.toList()); - filteredPartners.addAll(credentialPartnerUtil.getCredentialPartners( - regId, registrationStatusDto.getRegistrationType(), jsonObject)); - for (CredentialPartner key : filteredPartners) { - CredentialRequestDto credentialRequestDto = getCredentialRequestDto(regId, registrationStatusDto.getRegistrationType(), key); - LocalDateTime localdatetime = LocalDateTime.parse( - DateUtils.getUTCCurrentDateTimeString(env.getProperty(DATETIME_PATTERN)), format); - requestWrapper.setRequesttime(localdatetime); - requestWrapper.setRequest(credentialRequestDto); - // issuers with appIdBasedCredentialIdSuffix is calling v1 api and for others stage is calling v2 api for credential - if (StringUtils.isNotEmpty(key.getAppIdBasedCredentialIdSuffix())) { - List pathsegments = new ArrayList<>(); - pathsegments.add(regId + key.getAppIdBasedCredentialIdSuffix()); // #PDF suffix is added to identify the requested credential via rid - responseWrapper = (ResponseWrapper) restClientService.postApi(ApiName.CREDENTIALREQUESTV2, MediaType.APPLICATION_JSON, pathsegments, null, - null, requestWrapper, ResponseWrapper.class); - } else { - responseWrapper = (ResponseWrapper) restClientService.postApi(ApiName.CREDENTIALREQUEST, null, null, - requestWrapper, ResponseWrapper.class, MediaType.APPLICATION_JSON); - } - if (responseWrapper.getErrors() != null && !responseWrapper.getErrors().isEmpty()) { - ErrorDTO error = responseWrapper.getErrors().get(0); - object.setIsValid(Boolean.FALSE); - isTransactionSuccessful = false; - registrationStatusDto.setRefId(refIds); - description.setMessage(PlatformErrorMessages.RPR_PRT_PRINT_REQUEST_FAILED.getMessage()); - description.setCode(PlatformErrorMessages.RPR_PRT_PRINT_REQUEST_FAILED.getCode()); - - registrationStatusDto.setStatusComment( - StatusUtil.PRINT_REQUEST_FAILED.getMessage() + SEPERATOR + error.getMessage()); - registrationStatusDto.setSubStatusCode(StatusUtil.PRINT_REQUEST_FAILED.getCode()); - registrationStatusDto - .setLatestTransactionStatusCode(RegistrationTransactionStatusCode.REPROCESS.toString()); - registrationStatusDto - .setLatestTransactionTypeCode(RegistrationTransactionTypeCode.PRINT_SERVICE.toString()); - break; - } else { - credentialResponseDto = mapper.readValue(mapper.writeValueAsString(responseWrapper.getResponse()), - CredentialResponseDto.class); - refIds = credentialResponseDto.getRequestId(); - isTransactionSuccessful = true; - } - } - if (isTransactionSuccessful) { - registrationStatusDto.setRefId(refIds); - object.setIsValid(Boolean.TRUE); - description.setMessage(PlatformSuccessMessages.RPR_PRINT_STAGE_REQUEST_SUCCESS.getMessage()); - description.setCode(PlatformSuccessMessages.RPR_PRINT_STAGE_REQUEST_SUCCESS.getCode()); - registrationStatusDto.setStatusComment( - trimeExpMessage.trimExceptionMessage(StatusUtil.PRINT_REQUEST_SUCCESS.getMessage())); - registrationStatusDto.setSubStatusCode(StatusUtil.PRINT_REQUEST_SUCCESS.getCode()); - registrationStatusDto - .setLatestTransactionStatusCode(RegistrationTransactionStatusCode.PROCESSED.toString()); - registrationStatusDto - .setLatestTransactionTypeCode(RegistrationTransactionTypeCode.PRINT_SERVICE.toString()); - - regProcLogger.debug(LoggerFileConstant.SESSIONID.toString(), - LoggerFileConstant.REGISTRATIONID.toString(), regId, "PrintStage::process()::exit"); - } - } - } catch (ApisResourceAccessException e) { - regProcLogger.error(LoggerFileConstant.SESSIONID.toString(), LoggerFileConstant.REGISTRATIONID.toString(), - regId, PlatformErrorMessages.RPR_PRT_PRINT_REQUEST_FAILED.name() + e.getMessage() - + ExceptionUtils.getStackTrace(e)); - registrationStatusDto - .setLatestTransactionStatusCode(RegistrationTransactionStatusCode.REPROCESS.toString()); - registrationStatusDto.setStatusComment(trimeExpMessage.trimExceptionMessage( - StatusUtil.API_RESOUCE_ACCESS_FAILED.getMessage() + SEPERATOR + e.getMessage())); - registrationStatusDto.setSubStatusCode(StatusUtil.API_RESOUCE_ACCESS_FAILED.getCode()); - description.setMessage(PlatformErrorMessages.RPR_PRT_PRINT_REQUEST_FAILED.getMessage()); - description.setCode(PlatformErrorMessages.RPR_PRT_PRINT_REQUEST_FAILED.getCode()); - object.setInternalError(Boolean.TRUE); - } catch (IOException e) { - regProcLogger.error(LoggerFileConstant.SESSIONID.toString(), LoggerFileConstant.REGISTRATIONID.toString(), - regId, PlatformErrorMessages.RPR_PRT_PRINT_REQUEST_FAILED.name() + e.getMessage() - + ExceptionUtils.getStackTrace(e)); - registrationStatusDto - .setLatestTransactionStatusCode(RegistrationTransactionStatusCode.REPROCESS.toString()); - registrationStatusDto.setStatusComment( - trimeExpMessage.trimExceptionMessage(StatusUtil.IO_EXCEPTION.getMessage() + e.getMessage())); - registrationStatusDto.setSubStatusCode(StatusUtil.IO_EXCEPTION.getCode()); - description.setMessage(PlatformErrorMessages.RPR_PRT_PRINT_REQUEST_FAILED.getMessage()); - description.setCode(PlatformErrorMessages.RPR_PRT_PRINT_REQUEST_FAILED.getCode()); - object.setInternalError(Boolean.TRUE); - } catch (Exception e) { - regProcLogger.error(LoggerFileConstant.SESSIONID.toString(), LoggerFileConstant.REGISTRATIONID.toString(), - regId, PlatformErrorMessages.RPR_PRT_PRINT_REQUEST_FAILED.name() + e.getMessage() - + ExceptionUtils.getStackTrace(e)); - registrationStatusDto - .setLatestTransactionStatusCode(RegistrationTransactionStatusCode.REPROCESS.toString()); - registrationStatusDto.setStatusComment( - trimeExpMessage.trimExceptionMessage(StatusUtil.UNKNOWN_EXCEPTION_OCCURED.getMessage())); - registrationStatusDto.setSubStatusCode(StatusUtil.UNKNOWN_EXCEPTION_OCCURED.getCode()); - description.setMessage(PlatformErrorMessages.RPR_PRT_PRINT_REQUEST_FAILED.getMessage()); - description.setCode(PlatformErrorMessages.RPR_PRT_PRINT_REQUEST_FAILED.getCode()); - object.setInternalError(Boolean.TRUE); - } - finally { - if (object.getInternalError()) { - updateErrorFlags(registrationStatusDto, object); - } - String eventId = ""; - String eventName = ""; - String eventType = ""; - eventId = isTransactionSuccessful ? EventId.RPR_402.toString() : EventId.RPR_405.toString(); - eventName = eventId.equalsIgnoreCase(EventId.RPR_402.toString()) ? EventName.UPDATE.toString() - : EventName.EXCEPTION.toString(); - eventType = eventId.equalsIgnoreCase(EventId.RPR_402.toString()) ? EventType.BUSINESS.toString() - : EventType.SYSTEM.toString(); - /** Module-Id can be Both Success/Error code */ - String moduleId = isTransactionSuccessful - ? PlatformSuccessMessages.RPR_PRINT_STAGE_REQUEST_SUCCESS.getCode() - : description.getCode(); - String moduleName = ModuleName.PRINT_STAGE.toString(); - registrationStatusService.updateRegistrationStatus(registrationStatusDto, moduleId, moduleName); - - auditLogRequestBuilder.createAuditRequestBuilder(description.getMessage(), eventId, eventName, eventType, - moduleId, moduleName, regId); - - } - return object; - } - - private CredentialRequestDto getCredentialRequestDto(String regId, String process, CredentialPartner key) { - CredentialRequestDto credentialRequestDto = new CredentialRequestDto(); - Map additionalAttributes=new HashMap<>(); - - credentialRequestDto.setCredentialType(key.getCredentialType()); - credentialRequestDto.setEncrypt(encrypt); - - credentialRequestDto.setId(regId); - - credentialRequestDto.setIssuer(key.getPartnerId()); - - credentialRequestDto.setEncryptionKey(generatePin()); - additionalAttributes.put("templateTypeCode", key.getTemplate()); - additionalAttributes.put("registrationId", regId); - if (CollectionUtils.isNotEmpty(key.getMetaInfoFields())) - getAdditionalCredentialFields(regId, process, key.getMetaInfoFields(), additionalAttributes); - credentialRequestDto.setAdditionalData(additionalAttributes); - - return credentialRequestDto; - } - - private void getAdditionalCredentialFields(String regId, String process, - List metaInfoFields, - Map additionalAttributes) { - try { - Map metaInfo = utilities.getPacketManagerService().getMetaInfo(regId, process, ProviderStageName.CREDENTIAL_REQUESTOR); - JSONArray metadata = new JSONArray(metaInfo.get(JsonConstant.METADATA)); - for(int i=0; i pathsegments = new ArrayList<>(); - pathsegments.add(uin); - String vid = null; - - VidsInfosDTO vidsInfosDTO; - - regProcLogger.debug(LoggerFileConstant.SESSIONID.toString(), LoggerFileConstant.REGISTRATIONID.toString(), "", - "PrintServiceImpl::getVid():: get GETVIDSBYUIN service call started with request data : " - ); - - vidsInfosDTO = (VidsInfosDTO) restClientService.getApi(ApiName.GETVIDSBYUIN, - pathsegments, "", "", VidsInfosDTO.class); - - if (vidsInfosDTO.getErrors() != null && !vidsInfosDTO.getErrors().isEmpty()) { - ServiceError error = vidsInfosDTO.getErrors().get(0); - throw new VidNotAvailableException(PlatformErrorMessages.RPR_PRT_VID_NOT_AVAILABLE_EXCEPTION.getCode(), - error.getMessage()); - - } else { - if(vidsInfosDTO.getResponse()!=null && !vidsInfosDTO.getResponse().isEmpty()) { - for (VidInfoDTO VidInfoDTO : vidsInfosDTO.getResponse()) { - if (VidType.PERPETUAL.name().equalsIgnoreCase(VidInfoDTO.getVidType())) { - vid = VidInfoDTO.getVid(); - break; - } - } - if (vid == null) { - throw new VidNotAvailableException( - PlatformErrorMessages.RPR_PRT_VID_NOT_AVAILABLE_EXCEPTION.getCode(), - PlatformErrorMessages.RPR_PRT_VID_NOT_AVAILABLE_EXCEPTION.getMessage()); - } - regProcLogger.debug(LoggerFileConstant.SESSIONID.toString(), - LoggerFileConstant.REGISTRATIONID.toString(), "", - "PrintServiceImpl::getVid():: get GETVIDSBYUIN service call ended successfully"); - - }else { - throw new VidNotAvailableException(PlatformErrorMessages.RPR_PRT_VID_NOT_AVAILABLE_EXCEPTION.getCode(), - PlatformErrorMessages.RPR_PRT_VID_NOT_AVAILABLE_EXCEPTION.getMessage()); - } - - } - - return vid; - } - - private void updateErrorFlags(InternalRegistrationStatusDto registrationStatusDto, MessageDTO object) { - object.setInternalError(true); - if (registrationStatusDto.getLatestTransactionStatusCode() - .equalsIgnoreCase(RegistrationTransactionStatusCode.REPROCESS.toString())) { - object.setIsValid(true); - } else { - object.setIsValid(false); - } - } - - @Scheduled(fixedDelayString = "${mosip.regproc.printstage.pingeneration.refresh.millisecs:1800000}", - initialDelayString = "${mosip.regproc.printstage.pingeneration.refresh.delay-on-startup.millisecs:5000}") - private void instantiate() { - regProcLogger.debug("Instantiating SecureRandom for credential pin generation............"); - try { - sr = SecureRandom.getInstance("SHA1PRNG"); - } catch (NoSuchAlgorithmException e) { - regProcLogger.error("Could not instantiate SecureRandom for credential pin generation", e); - } - } +package io.mosip.registration.processor.credentialrequestor.stage; + +import java.io.IOException; +import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.stream.Collectors; + +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.json.JSONArray; +import org.json.simple.JSONObject; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.cloud.context.config.annotation.RefreshScope; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.Environment; +import org.springframework.http.MediaType; +import org.springframework.scheduling.annotation.EnableScheduling; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Service; + +import com.fasterxml.jackson.databind.ObjectMapper; + +import io.mosip.kernel.core.exception.BaseUncheckedException; +import io.mosip.kernel.core.exception.ServiceError; +import io.mosip.kernel.core.logger.spi.Logger; +import io.mosip.kernel.core.util.DateUtils; +import io.mosip.registration.processor.core.abstractverticle.MessageBusAddress; +import io.mosip.registration.processor.core.abstractverticle.MessageDTO; +import io.mosip.registration.processor.core.abstractverticle.MosipEventBus; +import io.mosip.registration.processor.core.abstractverticle.MosipRouter; +import io.mosip.registration.processor.core.abstractverticle.MosipVerticleAPIManager; +import io.mosip.registration.processor.core.code.ApiName; +import io.mosip.registration.processor.core.code.EventId; +import io.mosip.registration.processor.core.code.EventName; +import io.mosip.registration.processor.core.code.EventType; +import io.mosip.registration.processor.core.code.ModuleName; +import io.mosip.registration.processor.core.code.RegistrationTransactionStatusCode; +import io.mosip.registration.processor.core.code.RegistrationTransactionTypeCode; +import io.mosip.registration.processor.core.common.rest.dto.ErrorDTO; +import io.mosip.registration.processor.core.constant.IdType; +import io.mosip.registration.processor.core.constant.JsonConstant; +import io.mosip.registration.processor.core.constant.LoggerFileConstant; +import io.mosip.registration.processor.core.constant.ProviderStageName; +import io.mosip.registration.processor.core.constant.VidType; +import io.mosip.registration.processor.core.exception.ApisResourceAccessException; +import io.mosip.registration.processor.core.exception.util.PlatformErrorMessages; +import io.mosip.registration.processor.core.exception.util.PlatformSuccessMessages; +import io.mosip.registration.processor.core.http.RequestWrapper; +import io.mosip.registration.processor.core.http.ResponseWrapper; +import io.mosip.registration.processor.core.idrepo.dto.CredentialRequestDto; +import io.mosip.registration.processor.core.idrepo.dto.CredentialResponseDto; +import io.mosip.registration.processor.core.idrepo.dto.VidInfoDTO; +import io.mosip.registration.processor.core.idrepo.dto.VidsInfosDTO; +import io.mosip.registration.processor.core.logger.LogDescription; +import io.mosip.registration.processor.core.logger.RegProcessorLogger; +import io.mosip.registration.processor.core.spi.restclient.RegistrationProcessorRestClientService; +import io.mosip.registration.processor.core.status.util.StatusUtil; +import io.mosip.registration.processor.core.status.util.TrimExceptionMessage; +import io.mosip.registration.processor.core.util.JsonUtil; +import io.mosip.registration.processor.credentialrequestor.dto.CredentialPartner; +import io.mosip.registration.processor.credentialrequestor.stage.exception.VidNotAvailableException; +import io.mosip.registration.processor.credentialrequestor.util.CredentialPartnerUtil; +import io.mosip.registration.processor.packet.storage.utils.Utilities; +import io.mosip.registration.processor.rest.client.audit.builder.AuditLogRequestBuilder; +import io.mosip.registration.processor.status.code.RegistrationStatusCode; +import io.mosip.registration.processor.status.dto.InternalRegistrationStatusDto; +import io.mosip.registration.processor.status.dto.RegistrationStatusDto; +import io.mosip.registration.processor.status.service.RegistrationStatusService; + +/** + * The Class PrintStage. + * + * @author M1048358 Alok + * @author Ranjitha Siddegowda + * @author Sowmya + */ +@RefreshScope +@Service +@Configuration +@EnableScheduling +@ComponentScan(basePackages = { "${mosip.auth.adapter.impl.basepackage}", + "io.mosip.registration.processor.core.config", + "io.mosip.registration.processor.stages.config", + "io.mosip.registration.processor.credentialrequestor.config", + "io.mosip.registrationprocessor.stages.config", + "io.mosip.registration.processor.status.config", + "io.mosip.registration.processor.rest.client.config", + "io.mosip.registration.processor.packet.storage.config", + "io.mosip.registration.processor.packet.manager.config", + "io.mosip.kernel.idobjectvalidator.config", + "io.mosip.registration.processor.core.kernel.beans" }) +public class CredentialRequestorStage extends MosipVerticleAPIManager { + + private static final String STAGE_PROPERTY_PREFIX = "mosip.regproc.credentialrequestor."; + private Random sr = null; + private static final int max = 999999; + private static final int min = 100000; + + /** The reg proc logger. */ + private static Logger regProcLogger = RegProcessorLogger.getLogger(CredentialRequestorStage.class); + + /** The cluster manager url. */ + @Value("${vertx.cluster.configuration}") + private String clusterManagerUrl; + + + /** The core audit request builder. */ + @Autowired + private AuditLogRequestBuilder auditLogRequestBuilder; + + /** The mosip event bus. */ + private MosipEventBus mosipEventBus; + + /** The registration status service. */ + @Autowired + RegistrationStatusService registrationStatusService; + + /** worker pool size. */ + @Value("${worker.pool.size}") + private Integer workerPoolSize; + + /** After this time intervel, message should be considered as expired (In seconds). */ + @Value("${mosip.regproc.credentialrequestor.message.expiry-time-limit}") + private Long messageExpiryTimeLimit; + + @Value("${mosip.registration.processor.encrypt:false}") + private boolean encrypt; + + /** Mosip router for APIs */ + @Autowired + MosipRouter router; + + private static final String SEPERATOR = "::"; + + @Autowired + private RegistrationProcessorRestClientService restClientService; + + @Autowired + private ObjectMapper mapper; + + @Autowired + private Environment env; + + private static final String DATETIME_PATTERN = "mosip.registration.processor.datetime.pattern"; + + private static final String ISSUERS = "mosip.registration.processor.issuer"; + + @Value("#{T(java.util.Arrays).asList('${mosip.registration.processor.credential.default.partner-ids:}')}") + private List defaultPartners; + + private static String COMMA = ","; + private static String HASH_DELIMITER = "#"; + + @Autowired + private Utilities utilities; + + @Autowired + private CredentialPartnerUtil credentialPartnerUtil; + + @Override + protected String getPropertyPrefix() { + return STAGE_PROPERTY_PREFIX; + } + + /** + * Deploy verticle. + */ + public void deployVerticle() { + mosipEventBus = this.getEventBus(this, clusterManagerUrl, workerPoolSize); + this.consumeAndSend(mosipEventBus, MessageBusAddress.PRINTING_BUS_IN, MessageBusAddress.PRINTING_BUS_OUT, + messageExpiryTimeLimit); + } + + /* + * (non-Javadoc) + * + * @see + * io.mosip.registration.processor.core.spi.eventbus.EventBusManager#process( + * java.lang.Object) + */ + @Override + public MessageDTO process(MessageDTO object) { + TrimExceptionMessage trimeExpMessage = new TrimExceptionMessage(); + object.setMessageBusAddress(MessageBusAddress.PRINTING_BUS_IN); + object.setInternalError(Boolean.FALSE); + object.setIsValid(Boolean.FALSE); + LogDescription description = new LogDescription(); + + boolean isTransactionSuccessful = false; + String uin = null; + String refIds = null; + String regId = object.getRid(); + regProcLogger.debug(LoggerFileConstant.SESSIONID.toString(), LoggerFileConstant.REGISTRATIONID.toString(), + regId, "PrintStage::process()::entry"); + + InternalRegistrationStatusDto registrationStatusDto = null; + RequestWrapper requestWrapper = new RequestWrapper<>(); + ResponseWrapper responseWrapper = null; + CredentialResponseDto credentialResponseDto; + try { + registrationStatusDto = registrationStatusService.getRegistrationStatus( + regId, object.getReg_type(), object.getIteration(), object.getWorkflowInstanceId()); + registrationStatusDto + .setLatestTransactionTypeCode(RegistrationTransactionTypeCode.PRINT_SERVICE.toString()); + registrationStatusDto.setRegistrationStageName(getStageName()); + JSONObject jsonObject = utilities.idrepoRetrieveIdentityByRid(regId); + uin = JsonUtil.getJSONValue(jsonObject, IdType.UIN.toString()); + if (uin == null) { + regProcLogger.error(LoggerFileConstant.SESSIONID.toString(), + LoggerFileConstant.REGISTRATIONID.toString(), null, + PlatformErrorMessages.RPR_PRT_UIN_NOT_FOUND_IN_DATABASE.name()); + object.setIsValid(Boolean.FALSE); + isTransactionSuccessful = false; + description.setMessage(PlatformErrorMessages.RPR_PRT_PRINT_REQUEST_FAILED.getMessage()); + description.setCode(PlatformErrorMessages.RPR_PRT_PRINT_REQUEST_FAILED.getCode()); + + registrationStatusDto.setStatusComment( + StatusUtil.UIN_NOT_FOUND_IN_DATABASE.getMessage()); + registrationStatusDto.setSubStatusCode(StatusUtil.UIN_NOT_FOUND_IN_DATABASE.getCode()); + registrationStatusDto + .setLatestTransactionStatusCode(RegistrationTransactionStatusCode.FAILED.toString()); + registrationStatusDto + .setLatestTransactionTypeCode(RegistrationTransactionTypeCode.PRINT_SERVICE.toString()); + + } else { + requestWrapper.setId(env.getProperty("mosip.registration.processor.credential.request.service.id")); + DateTimeFormatter format = DateTimeFormatter.ofPattern(env.getProperty(DATETIME_PATTERN)); + requestWrapper.setVersion("1.0"); + List allIssuerList = credentialPartnerUtil.getAllCredentialPartners().getPartners(); + // filtering with default partner ids and process + List filteredPartners = allIssuerList.stream() + .filter(issuer -> defaultPartners.contains(issuer.getId())) + .filter(issuer -> (issuer.getProcess() == null) || (issuer.getProcess().contains(object.getReg_type()))) + .collect(Collectors.toList()); + filteredPartners.addAll(credentialPartnerUtil.getCredentialPartners( + regId, registrationStatusDto.getRegistrationType(), jsonObject)); + for (CredentialPartner key : filteredPartners) { + CredentialRequestDto credentialRequestDto = getCredentialRequestDto(regId, registrationStatusDto.getRegistrationType(), key); + LocalDateTime localdatetime = LocalDateTime.parse( + DateUtils.getUTCCurrentDateTimeString(env.getProperty(DATETIME_PATTERN)), format); + requestWrapper.setRequesttime(localdatetime); + requestWrapper.setRequest(credentialRequestDto); + // issuers with appIdBasedCredentialIdSuffix is calling v1 api and for others stage is calling v2 api for credential + if (StringUtils.isNotEmpty(key.getAppIdBasedCredentialIdSuffix())) { + List pathsegments = new ArrayList<>(); + pathsegments.add(regId + key.getAppIdBasedCredentialIdSuffix()); // #PDF suffix is added to identify the requested credential via rid + responseWrapper = (ResponseWrapper) restClientService.postApi(ApiName.CREDENTIALREQUESTV2, MediaType.APPLICATION_JSON, pathsegments, null, + null, requestWrapper, ResponseWrapper.class); + } else { + responseWrapper = (ResponseWrapper) restClientService.postApi(ApiName.CREDENTIALREQUEST, null, null, + requestWrapper, ResponseWrapper.class, MediaType.APPLICATION_JSON); + } + if (responseWrapper.getErrors() != null && !responseWrapper.getErrors().isEmpty()) { + ErrorDTO error = responseWrapper.getErrors().get(0); + object.setIsValid(Boolean.FALSE); + isTransactionSuccessful = false; + registrationStatusDto.setRefId(refIds); + description.setMessage(PlatformErrorMessages.RPR_PRT_PRINT_REQUEST_FAILED.getMessage()); + description.setCode(PlatformErrorMessages.RPR_PRT_PRINT_REQUEST_FAILED.getCode()); + + registrationStatusDto.setStatusComment( + StatusUtil.PRINT_REQUEST_FAILED.getMessage() + SEPERATOR + error.getMessage()); + registrationStatusDto.setSubStatusCode(StatusUtil.PRINT_REQUEST_FAILED.getCode()); + registrationStatusDto + .setLatestTransactionStatusCode(RegistrationTransactionStatusCode.REPROCESS.toString()); + registrationStatusDto + .setLatestTransactionTypeCode(RegistrationTransactionTypeCode.PRINT_SERVICE.toString()); + break; + } else { + credentialResponseDto = mapper.readValue(mapper.writeValueAsString(responseWrapper.getResponse()), + CredentialResponseDto.class); + refIds = credentialResponseDto.getRequestId(); + isTransactionSuccessful = true; + } + } + if (isTransactionSuccessful) { + registrationStatusDto.setRefId(refIds); + object.setIsValid(Boolean.TRUE); + description.setMessage(PlatformSuccessMessages.RPR_PRINT_STAGE_REQUEST_SUCCESS.getMessage()); + description.setCode(PlatformSuccessMessages.RPR_PRINT_STAGE_REQUEST_SUCCESS.getCode()); + registrationStatusDto.setStatusComment( + trimeExpMessage.trimExceptionMessage(StatusUtil.PRINT_REQUEST_SUCCESS.getMessage())); + registrationStatusDto.setSubStatusCode(StatusUtil.PRINT_REQUEST_SUCCESS.getCode()); + registrationStatusDto + .setLatestTransactionStatusCode(RegistrationTransactionStatusCode.PROCESSED.toString()); + registrationStatusDto + .setLatestTransactionTypeCode(RegistrationTransactionTypeCode.PRINT_SERVICE.toString()); + + regProcLogger.debug(LoggerFileConstant.SESSIONID.toString(), + LoggerFileConstant.REGISTRATIONID.toString(), regId, "PrintStage::process()::exit"); + } + } + } catch (ApisResourceAccessException e) { + regProcLogger.error(LoggerFileConstant.SESSIONID.toString(), LoggerFileConstant.REGISTRATIONID.toString(), + regId, PlatformErrorMessages.RPR_PRT_PRINT_REQUEST_FAILED.name() + e.getMessage() + + ExceptionUtils.getStackTrace(e)); + registrationStatusDto + .setLatestTransactionStatusCode(RegistrationTransactionStatusCode.REPROCESS.toString()); + registrationStatusDto.setStatusComment(trimeExpMessage.trimExceptionMessage( + StatusUtil.API_RESOUCE_ACCESS_FAILED.getMessage() + SEPERATOR + e.getMessage())); + registrationStatusDto.setSubStatusCode(StatusUtil.API_RESOUCE_ACCESS_FAILED.getCode()); + description.setMessage(PlatformErrorMessages.RPR_PRT_PRINT_REQUEST_FAILED.getMessage()); + description.setCode(PlatformErrorMessages.RPR_PRT_PRINT_REQUEST_FAILED.getCode()); + object.setInternalError(Boolean.TRUE); + } catch (IOException e) { + regProcLogger.error(LoggerFileConstant.SESSIONID.toString(), LoggerFileConstant.REGISTRATIONID.toString(), + regId, PlatformErrorMessages.RPR_PRT_PRINT_REQUEST_FAILED.name() + e.getMessage() + + ExceptionUtils.getStackTrace(e)); + registrationStatusDto + .setLatestTransactionStatusCode(RegistrationTransactionStatusCode.REPROCESS.toString()); + registrationStatusDto.setStatusComment( + trimeExpMessage.trimExceptionMessage(StatusUtil.IO_EXCEPTION.getMessage() + e.getMessage())); + registrationStatusDto.setSubStatusCode(StatusUtil.IO_EXCEPTION.getCode()); + description.setMessage(PlatformErrorMessages.RPR_PRT_PRINT_REQUEST_FAILED.getMessage()); + description.setCode(PlatformErrorMessages.RPR_PRT_PRINT_REQUEST_FAILED.getCode()); + object.setInternalError(Boolean.TRUE); + } catch (Exception e) { + regProcLogger.error(LoggerFileConstant.SESSIONID.toString(), LoggerFileConstant.REGISTRATIONID.toString(), + regId, PlatformErrorMessages.RPR_PRT_PRINT_REQUEST_FAILED.name() + e.getMessage() + + ExceptionUtils.getStackTrace(e)); + registrationStatusDto + .setLatestTransactionStatusCode(RegistrationTransactionStatusCode.REPROCESS.toString()); + registrationStatusDto.setStatusComment( + trimeExpMessage.trimExceptionMessage(StatusUtil.UNKNOWN_EXCEPTION_OCCURED.getMessage())); + registrationStatusDto.setSubStatusCode(StatusUtil.UNKNOWN_EXCEPTION_OCCURED.getCode()); + description.setMessage(PlatformErrorMessages.RPR_PRT_PRINT_REQUEST_FAILED.getMessage()); + description.setCode(PlatformErrorMessages.RPR_PRT_PRINT_REQUEST_FAILED.getCode()); + object.setInternalError(Boolean.TRUE); + } + finally { + if (object.getInternalError()) { + updateErrorFlags(registrationStatusDto, object); + } + String eventId = ""; + String eventName = ""; + String eventType = ""; + eventId = isTransactionSuccessful ? EventId.RPR_402.toString() : EventId.RPR_405.toString(); + eventName = eventId.equalsIgnoreCase(EventId.RPR_402.toString()) ? EventName.UPDATE.toString() + : EventName.EXCEPTION.toString(); + eventType = eventId.equalsIgnoreCase(EventId.RPR_402.toString()) ? EventType.BUSINESS.toString() + : EventType.SYSTEM.toString(); + /** Module-Id can be Both Success/Error code */ + String moduleId = isTransactionSuccessful + ? PlatformSuccessMessages.RPR_PRINT_STAGE_REQUEST_SUCCESS.getCode() + : description.getCode(); + String moduleName = ModuleName.PRINT_STAGE.toString(); + registrationStatusService.updateRegistrationStatus(registrationStatusDto, moduleId, moduleName); + + auditLogRequestBuilder.createAuditRequestBuilder(description.getMessage(), eventId, eventName, eventType, + moduleId, moduleName, regId); + + } + return object; + } + + private CredentialRequestDto getCredentialRequestDto(String regId, String process, CredentialPartner key) { + CredentialRequestDto credentialRequestDto = new CredentialRequestDto(); + Map additionalAttributes=new HashMap<>(); + + credentialRequestDto.setCredentialType(key.getCredentialType()); + credentialRequestDto.setEncrypt(encrypt); + + credentialRequestDto.setId(regId); + + credentialRequestDto.setIssuer(key.getPartnerId()); + + credentialRequestDto.setEncryptionKey(generatePin()); + additionalAttributes.put("templateTypeCode", key.getTemplate()); + additionalAttributes.put("registrationId", regId); + if (CollectionUtils.isNotEmpty(key.getMetaInfoFields())) + getAdditionalCredentialFields(regId, process, key.getMetaInfoFields(), additionalAttributes); + credentialRequestDto.setAdditionalData(additionalAttributes); + + return credentialRequestDto; + } + + private void getAdditionalCredentialFields(String regId, String process, + List metaInfoFields, + Map additionalAttributes) { + try { + Map metaInfo = utilities.getPacketManagerService().getMetaInfo(regId, process, ProviderStageName.CREDENTIAL_REQUESTOR); + JSONArray metadata = new JSONArray(metaInfo.get(JsonConstant.METADATA)); + for(int i=0; i pathsegments = new ArrayList<>(); + pathsegments.add(uin); + String vid = null; + + VidsInfosDTO vidsInfosDTO; + + regProcLogger.debug(LoggerFileConstant.SESSIONID.toString(), LoggerFileConstant.REGISTRATIONID.toString(), "", + "PrintServiceImpl::getVid():: get GETVIDSBYUIN service call started with request data : " + ); + + vidsInfosDTO = (VidsInfosDTO) restClientService.getApi(ApiName.GETVIDSBYUIN, + pathsegments, "", "", VidsInfosDTO.class); + + if (vidsInfosDTO.getErrors() != null && !vidsInfosDTO.getErrors().isEmpty()) { + ServiceError error = vidsInfosDTO.getErrors().get(0); + throw new VidNotAvailableException(PlatformErrorMessages.RPR_PRT_VID_NOT_AVAILABLE_EXCEPTION.getCode(), + error.getMessage()); + + } else { + if(vidsInfosDTO.getResponse()!=null && !vidsInfosDTO.getResponse().isEmpty()) { + for (VidInfoDTO VidInfoDTO : vidsInfosDTO.getResponse()) { + if (VidType.PERPETUAL.name().equalsIgnoreCase(VidInfoDTO.getVidType())) { + vid = VidInfoDTO.getVid(); + break; + } + } + if (vid == null) { + throw new VidNotAvailableException( + PlatformErrorMessages.RPR_PRT_VID_NOT_AVAILABLE_EXCEPTION.getCode(), + PlatformErrorMessages.RPR_PRT_VID_NOT_AVAILABLE_EXCEPTION.getMessage()); + } + regProcLogger.debug(LoggerFileConstant.SESSIONID.toString(), + LoggerFileConstant.REGISTRATIONID.toString(), "", + "PrintServiceImpl::getVid():: get GETVIDSBYUIN service call ended successfully"); + + }else { + throw new VidNotAvailableException(PlatformErrorMessages.RPR_PRT_VID_NOT_AVAILABLE_EXCEPTION.getCode(), + PlatformErrorMessages.RPR_PRT_VID_NOT_AVAILABLE_EXCEPTION.getMessage()); + } + + } + + return vid; + } + + private void updateErrorFlags(InternalRegistrationStatusDto registrationStatusDto, MessageDTO object) { + object.setInternalError(true); + if (registrationStatusDto.getLatestTransactionStatusCode() + .equalsIgnoreCase(RegistrationTransactionStatusCode.REPROCESS.toString())) { + object.setIsValid(true); + } else { + object.setIsValid(false); + } + } + + @Scheduled(fixedDelayString = "${mosip.regproc.printstage.pingeneration.refresh.millisecs:1800000}", + initialDelayString = "${mosip.regproc.printstage.pingeneration.refresh.delay-on-startup.millisecs:5000}") + private void instantiate() { + regProcLogger.debug("Instantiating SecureRandom for credential pin generation............"); + try { + sr = SecureRandom.getInstance("SHA1PRNG"); + } catch (NoSuchAlgorithmException e) { + regProcLogger.error("Could not instantiate SecureRandom for credential pin generation", e); + } + } } \ No newline at end of file diff --git a/registration-processor/post-processor/registration-processor-credential-requestor-stage/src/main/java/io/mosip/registration/processor/credentialrequestor/util/CredentialPartnerUtil.java b/registration-processor/post-processor/registration-processor-credential-requestor-stage/src/main/java/io/mosip/registration/processor/credentialrequestor/util/CredentialPartnerUtil.java index 4495ae6e7ed..5cd509ff627 100644 --- a/registration-processor/post-processor/registration-processor-credential-requestor-stage/src/main/java/io/mosip/registration/processor/credentialrequestor/util/CredentialPartnerUtil.java +++ b/registration-processor/post-processor/registration-processor-credential-requestor-stage/src/main/java/io/mosip/registration/processor/credentialrequestor/util/CredentialPartnerUtil.java @@ -1,222 +1,233 @@ -package io.mosip.registration.processor.credentialrequestor.util; - -import com.fasterxml.jackson.databind.ObjectMapper; -import io.mosip.kernel.core.exception.BaseCheckedException; -import io.mosip.kernel.core.exception.ExceptionUtils; -import io.mosip.kernel.core.logger.spi.Logger; -import io.mosip.kernel.core.util.exception.JsonProcessingException; -import io.mosip.registration.processor.core.constant.*; -import io.mosip.registration.processor.core.exception.ApisResourceAccessException; -import io.mosip.registration.processor.core.exception.PacketManagerException; -import io.mosip.registration.processor.core.exception.RegistrationProcessorCheckedException; -import io.mosip.registration.processor.core.exception.util.PlatformErrorMessages; -import io.mosip.registration.processor.core.logger.RegProcessorLogger; -import io.mosip.registration.processor.core.packet.dto.FieldValue; -import io.mosip.registration.processor.core.util.JsonUtil; -import io.mosip.registration.processor.credentialrequestor.dto.CredentialPartner; -import io.mosip.registration.processor.credentialrequestor.dto.CredentialPartnersList; -import io.mosip.registration.processor.packet.storage.exception.ParsingException; -import io.mosip.registration.processor.packet.storage.utils.IdSchemaUtil; -import io.mosip.registration.processor.packet.storage.utils.Utilities; -import org.apache.commons.collections.MapUtils; -import org.assertj.core.util.Lists; -import org.json.JSONArray; -import org.json.JSONException; -import org.json.simple.JSONObject; -import org.mvel2.MVEL; -import org.mvel2.ParserContext; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.core.env.Environment; -import org.springframework.stereotype.Component; -import org.springframework.util.StringUtils; - -import javax.annotation.PostConstruct; -import java.io.IOException; -import java.util.*; -import java.util.stream.Collectors; - -@Component -public class CredentialPartnerUtil { - - private static final Logger regProcLogger = RegProcessorLogger.getLogger(CredentialPartnerUtil.class); - private static final String VALUE_LABEL = "value"; - private static final String LANGUAGE = "language"; - - private CredentialPartnersList credentialPartners; - - @Autowired - private ObjectMapper objectMapper; - - @Autowired - private Environment env; - - @Autowired - private Utilities utilities; - - @Autowired - IdSchemaUtil idSchemaUtil; - - @Value("${mosip.registration.processor.credential.partner-profiles}") - private String partnerProfileFileName; - - @Value("#{T(java.util.Arrays).asList('${mosip.mandatory-languages:}')}") - private List mandatoryLanguages; - - @Value("${mosip.registration.processor.credential.conditional.no-match-partner-ids}") - private String noMatchIssuer; - - @Value("#{${mosip.registration.processor.credential.conditional.partner-id-map:{}}}") - private Map credentialPartnerExpression; - - @Value("${config.server.file.storage.uri}") - private String configServerFileStorageURL; - - /** - * This map will hold the actual field names after resolving, using mapping JSON as keys and - * configured field names as values - */ - private Map requiredIDObjectFieldNamesMap; - /** - * Configured Id object fields - */ - private List requiredIdObjectFieldNames; - - @PostConstruct - private void getIdObjectFieldNames() throws BaseCheckedException { - regProcLogger.info( "CredentialPartnerUtil::getIdObjectFieldNames()::PostConstruct"); - try { - org.json.simple.JSONObject identityMappingJson = - utilities.getRegistrationProcessorMappingJson(MappingJsonConstants.IDENTITY); - requiredIDObjectFieldNamesMap = new HashMap<>(); - for(Map.Entry expressionEntry : credentialPartnerExpression.entrySet()) { - ParserContext parserContext = ParserContext.create(); - MVEL.analysisCompile(expressionEntry.getValue(), parserContext); - Map expressionVariablesMap = parserContext.getInputs(); - for(Map.Entry variableEntry: expressionVariablesMap.entrySet()) { - String actualFieldName = JsonUtil.getJSONValue( - JsonUtil.getJSONObject(identityMappingJson, variableEntry.getKey()), - VALUE_LABEL); - if(actualFieldName == null) - throw new BaseCheckedException( - PlatformErrorMessages.RPR_PCM_FIELD_NAME_NOT_AVAILABLE_IN_MAPPING_JSON.getCode(), - PlatformErrorMessages.RPR_PCM_FIELD_NAME_NOT_AVAILABLE_IN_MAPPING_JSON.getMessage()); - requiredIDObjectFieldNamesMap.put(actualFieldName, variableEntry.getKey()); - } - } - requiredIdObjectFieldNames = requiredIDObjectFieldNamesMap.keySet().stream().collect(Collectors.toList()); - } catch (IOException e) { - throw new BaseCheckedException( - PlatformErrorMessages.RPR_PCM_ACCESSING_IDOBJECT_MAPPING_FILE_FAILED.getCode(), - PlatformErrorMessages.RPR_PCM_ACCESSING_IDOBJECT_MAPPING_FILE_FAILED.getMessage(), e); - } - } - - public List getCredentialPartners(String regId, String registrationType, JSONObject identity) throws PacketManagerException, JSONException, ApisResourceAccessException, IOException, JsonProcessingException { - - regProcLogger.info(LoggerFileConstant.SESSIONID.toString(), LoggerFileConstant.REGISTRATIONID.toString(), - regId, "CredentialPartnerUtil::getCredentialPartners()::entry"); - - List filteredPartners = new ArrayList<>(); - if (credentialPartnerExpression == null || credentialPartnerExpression.isEmpty()) { - regProcLogger.debug(LoggerFileConstant.SESSIONID.toString(), - LoggerFileConstant.REGISTRATIONID.toString(), regId, - PlatformErrorMessages.RPR_PRT_ISSUER_NOT_FOUND_IN_PROPERTY.name()); - return Lists.emptyList(); - } - - Map identityFieldValueMap = new HashMap<>(); - requiredIdObjectFieldNames.forEach(field -> identityFieldValueMap.put(field, JsonUtil.getJSONValue(identity, field))); - - Map context = new HashMap<>(); - for (Map.Entry identityAttribute: identityFieldValueMap.entrySet()) { - JSONObject attributeObject = new JSONObject(identityFieldValueMap); - try { - if (identityAttribute.getKey() != null && identityAttribute.getValue() != null) { - Object obj = attributeObject.get(identityAttribute.getKey()); - if (obj instanceof org.json.simple.JSONArray) { - org.json.simple.JSONArray attributeArray = (org.json.simple.JSONArray) obj; - for (int i = 0; i < attributeArray.size(); i++) { - JSONObject jsonObject = (JSONObject) attributeArray.get(i); - if (mandatoryLanguages.get(0).equalsIgnoreCase((String) jsonObject.get(LANGUAGE))) { - context.put(identityAttribute.getKey(), jsonObject.get(VALUE_LABEL)); - } - } - } else { - if (obj != null) { - context.put(identityAttribute.getKey(), obj.toString()); - } - } - } else - context.put(identityAttribute.getKey(), identityAttribute.getValue()); - } catch (Exception e) { - regProcLogger.error(LoggerFileConstant.SESSIONID.toString(), LoggerFileConstant.REGISTRATIONID.toString(), "", - ExceptionUtils.getStackTrace(e)); - throw new ParsingException(PlatformErrorMessages.RPR_PRT_DATA_VALIDATION_FAILED.getCode(), e); - } - } - - // adding additional metadata so that it can be used for MVEL expression - Map metaInfo = utilities.getPacketManagerService().getMetaInfo(regId, registrationType, ProviderStageName.CREDENTIAL_REQUESTOR); - if (MapUtils.isNotEmpty(metaInfo)) { - String metadata = metaInfo.get(JsonConstant.METADATA); - if (!StringUtils.isEmpty(metadata)) { - JSONArray jsonArray = new JSONArray(metadata); - addToMap(jsonArray, context); - } - } - - regProcLogger.info(LoggerFileConstant.SESSIONID.toString(), LoggerFileConstant.REGISTRATIONID.toString(), - regId, "CredentialPartnerUtil::CredentialPartnerExpression::" + credentialPartnerExpression.toString()); - - for(Map.Entry entry : credentialPartnerExpression.entrySet()) { - Boolean result = (Boolean) MVEL.eval(entry.getValue(), context); - if (result) { - filteredPartners.add(entry.getKey()); - } - } - if (StringUtils.hasText(noMatchIssuer) && filteredPartners.isEmpty()) { - filteredPartners.add(noMatchIssuer); - } - regProcLogger.info(LoggerFileConstant.SESSIONID.toString(), LoggerFileConstant.REGISTRATIONID.toString(), - regId, "CredentialPartnerUtil::FilteredPartners::" + filteredPartners.toString()); - - List finalList = new ArrayList<>(); - if (!filteredPartners.isEmpty()) { - filteredPartners.forEach( - p -> { - Optional partner = credentialPartners.getPartners() - .stream().filter(pr -> pr.getId().equalsIgnoreCase(p)).findAny(); - if (partner.isPresent()) - finalList.add(partner.get()); - }); - } - return finalList; - } - - private void addToMap(JSONArray jsonArray, Map allMap) throws JSONException, IOException { - for (int i =0; i < jsonArray.length(); i++) { - org.json.JSONObject jsonObject = (org.json.JSONObject) jsonArray.get(i); - FieldValue fieldValue = objectMapper.readValue(jsonObject.toString(), FieldValue.class); - allMap.put(fieldValue.getLabel(), fieldValue.getValue()); - } - } - - @PostConstruct - public void loadPartnerDetails() throws RegistrationProcessorCheckedException { - try { - String partners = Utilities.getJson(configServerFileStorageURL, partnerProfileFileName); - credentialPartners = JsonUtil.readValueWithUnknownProperties(partners, CredentialPartnersList.class); - } catch (Exception e) { - regProcLogger.error("Error loading credential Partners", e); - throw new RegistrationProcessorCheckedException(PlatformErrorMessages.RPR_SYS_IO_EXCEPTION.getCode(), - PlatformErrorMessages.RPR_SYS_IO_EXCEPTION.getMessage(), e); - } - } - - public CredentialPartnersList getAllCredentialPartners() throws RegistrationProcessorCheckedException { - if (credentialPartners == null) - loadPartnerDetails(); - return credentialPartners; - } -} +package io.mosip.registration.processor.credentialrequestor.util; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; + +import javax.annotation.PostConstruct; + +import org.apache.commons.collections.MapUtils; +import org.assertj.core.util.Lists; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.simple.JSONObject; +import org.mvel2.MVEL; +import org.mvel2.ParserContext; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Component; +import org.springframework.util.StringUtils; + +import com.fasterxml.jackson.databind.ObjectMapper; + +import io.mosip.kernel.core.exception.BaseCheckedException; +import io.mosip.kernel.core.exception.ExceptionUtils; +import io.mosip.kernel.core.logger.spi.Logger; +import io.mosip.kernel.core.util.exception.JsonProcessingException; +import io.mosip.registration.processor.core.constant.JsonConstant; +import io.mosip.registration.processor.core.constant.LoggerFileConstant; +import io.mosip.registration.processor.core.constant.MappingJsonConstants; +import io.mosip.registration.processor.core.constant.ProviderStageName; +import io.mosip.registration.processor.core.exception.ApisResourceAccessException; +import io.mosip.registration.processor.core.exception.PacketManagerException; +import io.mosip.registration.processor.core.exception.RegistrationProcessorCheckedException; +import io.mosip.registration.processor.core.exception.util.PlatformErrorMessages; +import io.mosip.registration.processor.core.logger.RegProcessorLogger; +import io.mosip.registration.processor.core.packet.dto.FieldValue; +import io.mosip.registration.processor.core.util.JsonUtil; +import io.mosip.registration.processor.credentialrequestor.dto.CredentialPartner; +import io.mosip.registration.processor.credentialrequestor.dto.CredentialPartnersList; +import io.mosip.registration.processor.packet.storage.exception.ParsingException; +import io.mosip.registration.processor.packet.storage.utils.IdSchemaUtil; +import io.mosip.registration.processor.packet.storage.utils.Utilities; + +@Component +public class CredentialPartnerUtil { + + private static final Logger regProcLogger = RegProcessorLogger.getLogger(CredentialPartnerUtil.class); + private static final String VALUE_LABEL = "value"; + private static final String LANGUAGE = "language"; + + private CredentialPartnersList credentialPartners; + + @Autowired + private ObjectMapper objectMapper; + + @Autowired + private Environment env; + + @Autowired + private Utilities utilities; + + @Autowired + IdSchemaUtil idSchemaUtil; + + @Value("${mosip.registration.processor.credential.partner-profiles}") + private String partnerProfileFileName; + + @Value("#{T(java.util.Arrays).asList('${mosip.mandatory-languages:}')}") + private List mandatoryLanguages; + + @Value("${mosip.registration.processor.credential.conditional.no-match-partner-ids:}") + private String noMatchIssuer; + + @Value("#{${mosip.registration.processor.credential.conditional.partner-id-map:{:}}}") + private Map credentialPartnerExpression; + + @Value("${config.server.file.storage.uri}") + private String configServerFileStorageURL; + + /** + * This map will hold the actual field names after resolving, using mapping JSON as keys and + * configured field names as values + */ + private Map requiredIDObjectFieldNamesMap; + + + @PostConstruct + private void getIdObjectFieldNames() throws BaseCheckedException { + regProcLogger.info( "CredentialPartnerUtil::getIdObjectFieldNames()::PostConstruct"); + try { + org.json.simple.JSONObject identityMappingJson = + utilities.getRegistrationProcessorMappingJson(MappingJsonConstants.IDENTITY); + requiredIDObjectFieldNamesMap = new HashMap<>(); + for(Map.Entry expressionEntry : credentialPartnerExpression.entrySet()) { + ParserContext parserContext = ParserContext.create(); + MVEL.analysisCompile(expressionEntry.getValue(), parserContext); + Map expressionVariablesMap = parserContext.getInputs(); + for(Map.Entry variableEntry: expressionVariablesMap.entrySet()) { + String actualFieldName = JsonUtil.getJSONValue( + JsonUtil.getJSONObject(identityMappingJson, variableEntry.getKey()), + VALUE_LABEL); + if(actualFieldName == null) + throw new BaseCheckedException( + PlatformErrorMessages.RPR_PCM_FIELD_NAME_NOT_AVAILABLE_IN_MAPPING_JSON.getCode(), + PlatformErrorMessages.RPR_PCM_FIELD_NAME_NOT_AVAILABLE_IN_MAPPING_JSON.getMessage()); + requiredIDObjectFieldNamesMap.put(actualFieldName, variableEntry.getKey()); + } + } + } catch (IOException e) { + throw new BaseCheckedException( + PlatformErrorMessages.RPR_PCM_ACCESSING_IDOBJECT_MAPPING_FILE_FAILED.getCode(), + PlatformErrorMessages.RPR_PCM_ACCESSING_IDOBJECT_MAPPING_FILE_FAILED.getMessage(), e); + } + } + + public List getCredentialPartners(String regId, String registrationType, JSONObject identity) throws PacketManagerException, JSONException, ApisResourceAccessException, IOException, JsonProcessingException { + + regProcLogger.info(LoggerFileConstant.SESSIONID.toString(), LoggerFileConstant.REGISTRATIONID.toString(), + regId, "CredentialPartnerUtil::getCredentialPartners()::entry"); + + List filteredPartners = new ArrayList<>(); + if (credentialPartnerExpression == null || credentialPartnerExpression.isEmpty()) { + regProcLogger.debug(LoggerFileConstant.SESSIONID.toString(), + LoggerFileConstant.REGISTRATIONID.toString(), regId, + PlatformErrorMessages.RPR_PRT_ISSUER_NOT_FOUND_IN_PROPERTY.name()); + return Lists.emptyList(); + } + + + Map identityFieldValueMap = new HashMap<>(); + requiredIDObjectFieldNamesMap + .forEach( + (field, fieldName) -> identityFieldValueMap.put(fieldName, + JsonUtil.getJSONValue(identity, field))); + + Map context = new HashMap<>(); + for (Map.Entry identityAttribute: identityFieldValueMap.entrySet()) { + JSONObject attributeObject = new JSONObject(identityFieldValueMap); + try { + if (identityAttribute.getKey() != null && identityAttribute.getValue() != null) { + Object obj = attributeObject.get(identityAttribute.getKey()); + if (obj instanceof org.json.simple.JSONArray) { + org.json.simple.JSONArray attributeArray = (org.json.simple.JSONArray) obj; + for (int i = 0; i < attributeArray.size(); i++) { + JSONObject jsonObject = (JSONObject) attributeArray.get(i); + if (mandatoryLanguages.get(0).equalsIgnoreCase((String) jsonObject.get(LANGUAGE))) { + context.put(identityAttribute.getKey(), jsonObject.get(VALUE_LABEL)); + } + } + } else { + if (obj != null) { + context.put(identityAttribute.getKey(), obj.toString()); + } + } + } else + context.put(identityAttribute.getKey(), identityAttribute.getValue()); + } catch (Exception e) { + regProcLogger.error(LoggerFileConstant.SESSIONID.toString(), LoggerFileConstant.REGISTRATIONID.toString(), "", + ExceptionUtils.getStackTrace(e)); + throw new ParsingException(PlatformErrorMessages.RPR_PRT_DATA_VALIDATION_FAILED.getCode(), e); + } + } + + // adding additional metadata so that it can be used for MVEL expression + Map metaInfo = utilities.getPacketManagerService().getMetaInfo(regId, registrationType, ProviderStageName.CREDENTIAL_REQUESTOR); + if (MapUtils.isNotEmpty(metaInfo)) { + String metadata = metaInfo.get(JsonConstant.METADATA); + if (!StringUtils.isEmpty(metadata)) { + JSONArray jsonArray = new JSONArray(metadata); + addToMap(jsonArray, context); + } + } + + regProcLogger.info(LoggerFileConstant.SESSIONID.toString(), LoggerFileConstant.REGISTRATIONID.toString(), + regId, "CredentialPartnerUtil::CredentialPartnerExpression::" + credentialPartnerExpression.toString()); + + for(Map.Entry entry : credentialPartnerExpression.entrySet()) { + Boolean result = (Boolean) MVEL.eval(entry.getValue(), context); + if (result) { + filteredPartners.add(entry.getKey()); + } + } + if (StringUtils.hasText(noMatchIssuer) && filteredPartners.isEmpty()) { + filteredPartners.add(noMatchIssuer); + } + regProcLogger.info(LoggerFileConstant.SESSIONID.toString(), LoggerFileConstant.REGISTRATIONID.toString(), + regId, "CredentialPartnerUtil::FilteredPartners::" + filteredPartners.toString()); + + List finalList = new ArrayList<>(); + if (!filteredPartners.isEmpty()) { + filteredPartners.forEach( + p -> { + Optional partner = credentialPartners.getPartners() + .stream().filter(pr -> pr.getId().equalsIgnoreCase(p)) + .filter(pr -> (pr.getProcess() == null) || (pr.getProcess().contains(registrationType))) + .findAny(); + if (partner.isPresent()) + finalList.add(partner.get()); + }); + } + return finalList; + } + + private void addToMap(JSONArray jsonArray, Map allMap) throws JSONException, IOException { + for (int i =0; i < jsonArray.length(); i++) { + org.json.JSONObject jsonObject = (org.json.JSONObject) jsonArray.get(i); + FieldValue fieldValue = objectMapper.readValue(jsonObject.toString(), FieldValue.class); + allMap.put(fieldValue.getLabel(), fieldValue.getValue()); + } + } + + @PostConstruct + public void loadPartnerDetails() throws RegistrationProcessorCheckedException { + try { + String partners = Utilities.getJson(configServerFileStorageURL, partnerProfileFileName); + credentialPartners = JsonUtil.readValueWithUnknownProperties(partners, CredentialPartnersList.class); + } catch (Exception e) { + regProcLogger.error("Error loading credential Partners", e); + throw new RegistrationProcessorCheckedException(PlatformErrorMessages.RPR_SYS_IO_EXCEPTION.getCode(), + PlatformErrorMessages.RPR_SYS_IO_EXCEPTION.getMessage(), e); + } + } + + public CredentialPartnersList getAllCredentialPartners() throws RegistrationProcessorCheckedException { + if (credentialPartners == null) + loadPartnerDetails(); + return credentialPartners; + } +} From 7fcd91d46160b90558edd3684c039084489ea2df Mon Sep 17 00:00:00 2001 From: Sowmya Ujjappa Banakar Date: Fri, 22 Mar 2024 17:05:33 +0530 Subject: [PATCH 2/2] organize import disabled Signed-off-by: Sowmya Ujjappa Banakar --- .../stage/CredentialRequestorStage.java | 58 ++++++++----------- 1 file changed, 24 insertions(+), 34 deletions(-) diff --git a/registration-processor/post-processor/registration-processor-credential-requestor-stage/src/main/java/io/mosip/registration/processor/credentialrequestor/stage/CredentialRequestorStage.java b/registration-processor/post-processor/registration-processor-credential-requestor-stage/src/main/java/io/mosip/registration/processor/credentialrequestor/stage/CredentialRequestorStage.java index b790193c459..58d937d355b 100644 --- a/registration-processor/post-processor/registration-processor-credential-requestor-stage/src/main/java/io/mosip/registration/processor/credentialrequestor/stage/CredentialRequestorStage.java +++ b/registration-processor/post-processor/registration-processor-credential-requestor-stage/src/main/java/io/mosip/registration/processor/credentialrequestor/stage/CredentialRequestorStage.java @@ -1,35 +1,6 @@ package io.mosip.registration.processor.credentialrequestor.stage; -import java.io.IOException; -import java.security.NoSuchAlgorithmException; -import java.security.SecureRandom; -import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Random; -import java.util.stream.Collectors; - -import org.apache.commons.collections.CollectionUtils; -import org.apache.commons.lang.StringUtils; -import org.apache.commons.lang3.exception.ExceptionUtils; -import org.json.JSONArray; -import org.json.simple.JSONObject; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.cloud.context.config.annotation.RefreshScope; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; -import org.springframework.core.env.Environment; -import org.springframework.http.MediaType; -import org.springframework.scheduling.annotation.EnableScheduling; -import org.springframework.scheduling.annotation.Scheduled; -import org.springframework.stereotype.Service; - import com.fasterxml.jackson.databind.ObjectMapper; - import io.mosip.kernel.core.exception.BaseUncheckedException; import io.mosip.kernel.core.exception.ServiceError; import io.mosip.kernel.core.logger.spi.Logger; @@ -40,11 +11,7 @@ import io.mosip.registration.processor.core.code.EventType; import io.mosip.registration.processor.core.code.*; import io.mosip.registration.processor.core.common.rest.dto.ErrorDTO; -import io.mosip.registration.processor.core.constant.IdType; -import io.mosip.registration.processor.core.constant.JsonConstant; -import io.mosip.registration.processor.core.constant.LoggerFileConstant; -import io.mosip.registration.processor.core.constant.ProviderStageName; -import io.mosip.registration.processor.core.constant.VidType; +import io.mosip.registration.processor.core.constant.*; import io.mosip.registration.processor.core.exception.ApisResourceAccessException; import io.mosip.registration.processor.core.exception.util.PlatformErrorMessages; import io.mosip.registration.processor.core.exception.util.PlatformSuccessMessages; @@ -69,6 +36,29 @@ import io.mosip.registration.processor.status.dto.InternalRegistrationStatusDto; import io.mosip.registration.processor.status.dto.RegistrationStatusDto; import io.mosip.registration.processor.status.service.RegistrationStatusService; +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.json.JSONArray; +import org.json.simple.JSONObject; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.cloud.context.config.annotation.RefreshScope; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.Environment; +import org.springframework.http.MediaType; +import org.springframework.scheduling.annotation.EnableScheduling; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Service; + +import java.io.IOException; +import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.*; +import java.util.stream.Collectors; /** * The Class PrintStage.