Skip to content

Commit

Permalink
Mosip 24879 add event id in the response of the below stories (#613)
Browse files Browse the repository at this point in the history
* added error for invalid user

* added status in 2 apis

* changed response of auth-lock-unlock api

* updated response of validate otp

* added eventid and status in response

* added eventId in response header for download eventid api

* fixed service history pdf file name

* added eventid in response of download card api

* convert eventId to constant

* added eventId for error scenarios

Co-authored-by: Ritik Jain <[email protected]>
  • Loading branch information
RitikJain4108 and Ritik Jain authored Dec 14, 2022
1 parent 82565a8 commit e4e12d3
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ private ResidentConstants() {
public static final String UIN_CARD_NAMING_CONVENTION_PROPERTY = "mosip.resident.uin.card.name.convention";
public static final String VID_CARD_NAMING_CONVENTION_PROPERTY = "mosip.resident.vid.card.name.convention";
public static final String SUCCESS = "Success";
public static final String FAILED = "Failed";
public static final String REGISTRATION_CENTRE_TEMPLATE_PROPERTY = "resident.template.registration.centers.list";
public static final String SUPPORTING_DOCS_TEMPLATE_PROPERTY = "resident.template.support-docs-list";
public static final String FROM_DATE_TIME = "fromDateTime";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
import io.mosip.resident.service.DownloadCardService;
import io.mosip.resident.util.AuditUtil;
import io.mosip.resident.util.EventEnum;
import io.mosip.resident.util.Utilitiy;
import io.mosip.resident.validator.RequestValidator;
import io.swagger.v3.oas.annotations.tags.Tag;
import reactor.util.function.Tuple2;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
Expand All @@ -31,6 +33,7 @@
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayInputStream;
import java.util.Objects;

/**
* @author Kamesh Shekhar Prasad
Expand All @@ -49,6 +52,12 @@ public class DownloadCardController {

@Autowired
DownloadCardService downloadCardService;

@Autowired
private Utilitiy utilitiy;

@Autowired
private Environment environment;

private static final Logger logger = LoggerConfiguration.logConfig(DownloadCardController.class);

Expand Down Expand Up @@ -77,14 +86,18 @@ public ResponseEntity<Object> downloadPersonalizedCard(@Validated @RequestBody M
logger.debug("DownloadCardController::downloadPersonalizedCard()::entry");
auditUtil.setAuditRequestDto(EventEnum.DOWNLOAD_PERSONALIZED_CARD);
requestValidator.validateDownloadPersonalizedCard(downloadPersonalizedCardMainRequestDTO);
byte[] pdfBytes = downloadCardService.downloadPersonalizedCard(downloadPersonalizedCardMainRequestDTO);
InputStreamResource resource = new InputStreamResource(new ByteArrayInputStream(pdfBytes));
if(pdfBytes.length==0){
Tuple2<byte[], String> tupleResponse = downloadCardService.downloadPersonalizedCard(downloadPersonalizedCardMainRequestDTO);
InputStreamResource resource = new InputStreamResource(new ByteArrayInputStream(tupleResponse.getT1()));
if(tupleResponse.getT1().length==0){
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
return ResponseEntity.ok().contentType(MediaType.parseMediaType("application/pdf"))
.header("Content-Disposition", "attachment; filename=\"" +
downloadCardService.getFileName() + ".pdf\"")
.header("Content-Disposition", "attachment; filename=\""
+ utilitiy.getFileName(tupleResponse.getT2(),
Objects.requireNonNull(this.environment.getProperty(
ResidentConstants.DOWNLOAD_PERSONALIZED_CARD_NAMING_CONVENTION_PROPERTY)))
+ ".pdf\"")
.header(ResidentConstants.EVENT_ID, tupleResponse.getT2())
.body(resource);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@
import io.mosip.resident.mock.exception.PaymentCanceledException;
import io.mosip.resident.mock.exception.PaymentFailedException;
import io.mosip.resident.mock.exception.TechnicalErrorException;
import io.mosip.resident.util.ObjectWithMetadata;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.util.MultiValueMap;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
Expand All @@ -40,6 +44,7 @@
import io.mosip.kernel.core.util.DateUtils;
import io.mosip.kernel.core.util.EmptyCheckUtils;
import io.mosip.resident.config.LoggerConfiguration;
import io.mosip.resident.constant.ResidentConstants;
import io.mosip.resident.constant.ResidentErrorCode;

@RestControllerAdvice
Expand Down Expand Up @@ -102,6 +107,13 @@ private ResponseEntity<ResponseWrapper<ServiceError>> getErrorResponseEntity(Htt
ServiceError error = new ServiceError(e.getErrorCode(), e.getErrorText());
ResponseWrapper<ServiceError> errorResponse = setErrors(httpServletRequest);
errorResponse.getErrors().add(error);
if (e instanceof ObjectWithMetadata && ((ObjectWithMetadata) e).getMetadata() != null
&& ((ObjectWithMetadata) e).getMetadata().containsKey(ResidentConstants.EVENT_ID)) {
MultiValueMap<String, String> headers = new HttpHeaders();
headers.add(ResidentConstants.EVENT_ID,
(String) ((ObjectWithMetadata) e).getMetadata().get(ResidentConstants.EVENT_ID));
return new ResponseEntity<>(errorResponse, headers, httpStatus);
}
return new ResponseEntity<>(errorResponse, httpStatus);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package io.mosip.resident.exception;

import java.util.Map;

import io.mosip.kernel.core.exception.BaseUncheckedException;
import io.mosip.resident.constant.ResidentErrorCode;
import io.mosip.resident.util.ObjectWithMetadata;

/**
* The Class ResidentServiceException.
*/
public class ResidentServiceException extends BaseUncheckedException {
public class ResidentServiceException extends BaseUncheckedException implements ObjectWithMetadata {

/** Generated serial version id. */
private static final long serialVersionUID = 8621530697947108810L;

private Map<String,Object> metadata;

/**
* Constructor the initialize Handler exception.
Expand Down Expand Up @@ -54,5 +59,18 @@ public ResidentServiceException(String errorCode, String errorMessage, Throwable
public ResidentServiceException(ResidentErrorCode err, Throwable rootCause) {
this(err.getErrorCode(), err.getErrorMessage(), rootCause);
}

public Map<String, Object> getMetadata() {
return metadata;
}

public void setMetadata(Map<String, Object> metadata) {
this.metadata = metadata;
}

public ResidentServiceException(ResidentErrorCode err, Throwable rootCause, Map<String,Object> metadata) {
this(err, rootCause);
this.metadata=metadata;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
public interface DownloadCardService {
Tuple2<byte[], String> getDownloadCardPDF(MainRequestDTO<DownloadCardRequestDTO> downloadCardRequestDTOMainRequestDTO);

byte[] downloadPersonalizedCard(MainRequestDTO<DownloadPersonalizedCardDto> downloadPersonalizedCardMainRequestDTO);

String getFileName();
Tuple2<byte[], String> downloadPersonalizedCard(MainRequestDTO<DownloadPersonalizedCardDto> downloadPersonalizedCardMainRequestDTO);

ResponseWrapper<VidDownloadCardResponseDto> getVidCardEventId(String vid) throws BaseCheckedException;
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
package io.mosip.resident.service.impl;

import static io.mosip.resident.constant.RegistrationConstants.SUCCESS;
import static io.mosip.resident.constant.TemplateVariablesConstants.NAME;
import static io.mosip.resident.constant.TemplateVariablesConstants.VID;
import static io.mosip.resident.constant.TemplateVariablesConstants.VID_TYPE;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.databind.ObjectMapper;

import io.mosip.kernel.core.exception.BaseCheckedException;
import io.mosip.kernel.core.logger.spi.Logger;
import io.mosip.kernel.core.util.CryptoUtil;
import io.mosip.kernel.core.util.DateUtils;
import io.mosip.resident.config.LoggerConfiguration;
import io.mosip.resident.constant.ApiName;
import io.mosip.resident.constant.EventStatusFailure;
import io.mosip.resident.constant.EventStatusInProgress;
import io.mosip.resident.constant.IdType;
import io.mosip.resident.constant.LoggerFileConstant;
import io.mosip.resident.constant.RequestType;
Expand Down Expand Up @@ -41,25 +61,6 @@
import reactor.util.function.Tuple2;
import reactor.util.function.Tuples;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static io.mosip.resident.constant.RegistrationConstants.SUCCESS;
import static io.mosip.resident.constant.TemplateVariablesConstants.NAME;
import static io.mosip.resident.constant.TemplateVariablesConstants.VID;
import static io.mosip.resident.constant.TemplateVariablesConstants.VID_TYPE;

/**
* @author Kamesh Shekhar Prasad
* This class is used to create service class implementation of download card api.
Expand Down Expand Up @@ -163,26 +164,53 @@ public Tuple2<byte[], String> getDownloadCardPDF(MainRequestDTO<DownloadCardRequ
}

@Override
public byte[] downloadPersonalizedCard(MainRequestDTO<DownloadPersonalizedCardDto> downloadPersonalizedCardMainRequestDTO) {
public Tuple2<byte[], String> downloadPersonalizedCard(MainRequestDTO<DownloadPersonalizedCardDto> downloadPersonalizedCardMainRequestDTO) {
String encodeHtml = downloadPersonalizedCardMainRequestDTO.getRequest().getHtml();
byte[] decodedData;
String password=null;
String eventId = null;
ResidentTransactionEntity residentTransactionEntity = null;
try {
residentTransactionEntity = createResidentTransactionEntity();
if (residentTransactionEntity != null) {
eventId = residentTransactionEntity.getEventId();
}
decodedData = CryptoUtil.decodePlainBase64(encodeHtml);
List<String> attributeValues = getAttributeList();
if(Boolean.parseBoolean(this.environment.getProperty(ResidentConstants.IS_PASSWORD_FLAG_ENABLED))){
password = utilitiy.getPassword(attributeValues);
}
residentTransactionEntity.setRequestSummary(SUCCESS);
residentTransactionEntity.setStatusCode(EventStatusInProgress.NEW.name());
}
catch (Exception e) {
residentTransactionEntity.setRequestSummary(ResidentConstants.FAILED);
residentTransactionEntity.setStatusCode(EventStatusFailure.FAILED.name());
audit.setAuditRequestDto(EventEnum.DOWNLOAD_PERSONALIZED_CARD);
logger.error("Unable to convert html to pdf RootCause- "+e);
throw new ResidentServiceException(ResidentErrorCode.DOWNLOAD_PERSONALIZED_CARD, e);
}
return utilitiy.signPdf(new ByteArrayInputStream(decodedData), password);
throw new ResidentServiceException(ResidentErrorCode.DOWNLOAD_PERSONALIZED_CARD, e, Map.of(ResidentConstants.EVENT_ID, eventId));
} finally {
//if the status code will come as null, it will set it as failed.
if(residentTransactionEntity.getStatusCode()==null) {
residentTransactionEntity.setStatusCode(EventStatusFailure.FAILED.name());
residentTransactionEntity.setRequestSummary(ResidentConstants.FAILED);
}
residentTransactionRepository.save(residentTransactionEntity);
}
return Tuples.of(utilitiy.signPdf(new ByteArrayInputStream(decodedData), password), eventId);
}

private List<String> getAttributeList() throws ApisResourceAccessException, IOException {
private ResidentTransactionEntity createResidentTransactionEntity() throws ApisResourceAccessException, ResidentServiceCheckedException {
ResidentTransactionEntity residentTransactionEntity = utilitiy.createEntity();
String eventId = utilitiy.createEventId();
residentTransactionEntity.setEventId(eventId);
residentTransactionEntity.setRequestTypeCode(RequestType.DOWNLOAD_PERSONALIZED_CARD.name());
residentTransactionEntity.setRefId(utilitiy.convertToMaskDataFormat(identityService.getResidentIndvidualId()));
residentTransactionEntity.setTokenId(identityService.getResidentIdaToken());
return residentTransactionEntity;
}

private List<String> getAttributeList() throws ApisResourceAccessException, IOException {
return getAttributeList(identityService.getResidentIndvidualId());
}

Expand Down Expand Up @@ -227,35 +255,6 @@ private List<String> getAttributeList(String individualId) throws IOException, A
return attributeValues;
}

@Override
public String getFileName() {
ResidentTransactionEntity residentTransactionEntity = utilitiy.createEntity();
String eventId = utilitiy.createEventId();
residentTransactionEntity.setEventId(eventId);
residentTransactionEntity.setRequestTypeCode(RequestType.DOWNLOAD_PERSONALIZED_CARD.name());
try {
residentTransactionEntity.setRefId(utilitiy.convertToMaskDataFormat(identityService.getResidentIndvidualId()));
residentTransactionEntity.setTokenId(identityService.getResidentIdaToken());
} catch (ApisResourceAccessException e) {
logger.error(LoggerFileConstant.SESSIONID.toString(), LoggerFileConstant.APPLICATIONID.toString(),
LoggerFileConstant.APPLICATIONID.toString(),
ResidentErrorCode.API_RESOURCE_UNAVAILABLE.getErrorCode()
+ ResidentErrorCode.API_RESOURCE_UNAVAILABLE.getErrorMessage()
+ ExceptionUtils.getStackTrace(e));
audit.setAuditRequestDto(EventEnum.getEventEnumWithValue(EventEnum.API_NOT_AVAILABLE,
eventId, "Download personalized card"));
throw new ResidentServiceException(ResidentErrorCode.API_RESOURCE_UNAVAILABLE.getErrorCode(),
ResidentErrorCode.API_RESOURCE_UNAVAILABLE.getErrorMessage(), e);
} catch (ResidentServiceCheckedException e) {
throw new RuntimeException(e);
}
residentTransactionEntity.setRequestSummary(SUCCESS);
residentTransactionEntity.setStatusCode(NEW);
residentTransactionRepository.save(residentTransactionEntity);
return utilitiy.getFileName(eventId, Objects.requireNonNull(this.environment.getProperty
(ResidentConstants.DOWNLOAD_PERSONALIZED_CARD_NAMING_CONVENTION_PROPERTY)));
}

@Override
public ResponseWrapper<VidDownloadCardResponseDto> getVidCardEventId(String vid) throws BaseCheckedException {
ResponseWrapper<VidDownloadCardResponseDto> responseWrapper= new ResponseWrapper<>();
Expand Down Expand Up @@ -314,7 +313,7 @@ private String insertDataForVidCard(ResidentCredentialResponseDto responseDto, S
residentTransactionEntity.setRefId(utilitiy.convertToMaskDataFormat(uin));
residentTransactionEntity.setTokenId(identityService.getIDAToken(uin));
residentTransactionEntity.setCredentialRequestId(responseDto.getRequestId());
residentTransactionEntity.setStatusCode(NEW);
residentTransactionEntity.setStatusCode(EventStatusInProgress.NEW.name());
residentTransactionEntity.setRequestSummary(RequestType.VID_CARD_DOWNLOAD.name());
/**
* Here we are setting vid in aid column.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.mosip.resident.util;

import java.util.Map;

/**
* @author Ritik Jain
*/
public interface ObjectWithMetadata {

public Map<String, Object> getMetadata();

public void setMetadata(Map<String, Object> metadata);

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.core.env.Environment;
import org.springframework.http.MediaType;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
Expand All @@ -42,6 +43,7 @@
import io.mosip.resident.service.impl.IdentityServiceImpl;
import io.mosip.resident.test.ResidentTestBootApplication;
import io.mosip.resident.util.AuditUtil;
import io.mosip.resident.util.Utilitiy;
import io.mosip.resident.validator.RequestValidator;
import reactor.util.function.Tuples;

Expand All @@ -60,6 +62,12 @@ public class DownloadCardControllerTest {

@Mock
private AuditUtil audit;

@Mock
private Environment environment;

@Mock
private Utilitiy utilitiy;

@MockBean
private ObjectStoreHelper objectStore;
Expand Down Expand Up @@ -106,6 +114,8 @@ public void setup() throws Exception {
downloadCardRequestDTOMainRequestDTO.setId("mosip.resident.download.uin.card");
reqJson = gson.toJson(downloadCardRequestDTOMainRequestDTO);
pdfbytes = "uin".getBytes();
Mockito.when(utilitiy.getFileName(Mockito.anyString(), Mockito.anyString())).thenReturn("file");
Mockito.when(environment.getProperty(Mockito.anyString())).thenReturn("property");
}

@Test
Expand All @@ -117,7 +127,7 @@ public void testGetCardSuccess() throws Exception {

@Test
public void testDownloadPersonalizedCard() throws Exception {
Mockito.when(downloadCardService.downloadPersonalizedCard(Mockito.any())).thenReturn(pdfbytes);
Mockito.when(downloadCardService.downloadPersonalizedCard(Mockito.any())).thenReturn(Tuples.of(pdfbytes, "12345"));
MainRequestDTO<DownloadPersonalizedCardDto> downloadPersonalizedCardMainRequestDTO =
new MainRequestDTO<>();
DownloadPersonalizedCardDto downloadPersonalizedCardDto =
Expand Down
Loading

0 comments on commit e4e12d3

Please sign in to comment.