This is the HKExternalPluginServiceImpl class:
package com.sc.we.idv.plugin.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.sc.we.idv.client.zoloz.service.ZolozService;
import com.sc.we.idv.common.model.InitializeRequest;
import com.sc.we.idv.common.model.LivelinessRequest;
import com.sc.we.idv.common.model.LivelinessResponse;
import com.sc.we.idv.common.model.BiometricVerificationRequest;
import com.sc.we.idv.common.model.BiometricVerificationResponse;
import com.sc.we.idv.model.IDVBO;
import com.sc.we.idv.model.IDVTransactionBO;
import com.sc.we.idv.plugin.client.zoloz.model.DeleteCustomerResponse;
import com.sc.we.idv.plugin.service.IDVExternalPluginService;
@Component
public class HKExternalPluginServiceImpl implements IDVExternalPluginService {
@Autowired
ZolozService zolozService;
@Override
public IDVBO getDemographics(IDVBO idvBo) throws Exception {
return zolozService.getDemographics(idvBo);
}
@Override
public IDVTransactionBO initialize(InitializeRequest req) throws Exception {
return zolozService.initialize(req);
}
@Override
public DeleteCustomerResponse deleteCustomerDataFromVendor(String vendorTransactionId) throws Exception {
return zolozService.deleteCustomerDataFromVendor(vendorTransactionId);
}
@Override
public BiometricVerificationResponse verifyFingerPrint(BiometricVerificationRequest request) throws Exception {
// TODO Auto-generated method stub
return null;
}
@Override
public IDVBO verifyFaceMatch(IDVBO idvBo) throws Exception {
// TODO Auto-generated method stub
return null;
}
}
This is the IDVExternalPluginService class.
import com.sc.we.idv.common.model.InitializeRequest;
import com.sc.we.idv.common.model.LivelinessRequest;
import com.sc.we.idv.common.model.LivelinessResponse;
import com.sc.we.idv.common.model.BiometricVerificationRequest;
import com.sc.we.idv.common.model.BiometricVerificationResponse;
import com.sc.we.idv.model.IDVBO;
import com.sc.we.idv.model.IDVTransactionBO;
import com.sc.we.idv.plugin.client.zoloz.model.DeleteCustomerResponse;
public interface IDVExternalPluginService {
public IDVBO getDemographics(IDVBO idvBo) throws Exception;
public IDVTransactionBO initialize(InitializeRequest req) throws Exception;
public DeleteCustomerResponse deleteCustomerDataFromVendor(String vendorTransactionId) throws Exception;
public BiometricVerificationResponse verifyFingerPrint(BiometricVerificationRequest request) throws Exception;
public IDVBO verifyFaceMatch(IDVBO idvBo) throws Exception;
}
This is the controller class:
package com.sc.we.idv.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sc.we.idv.common.model.BiometricVerificationRequest;
import com.sc.we.idv.common.model.BiometricVerificationResponse;
import com.sc.we.idv.common.model.InitializeRequest;
import com.sc.we.idv.common.model.InitializeResponse;
import com.sc.we.idv.common.model.LivelinessRequest;
import com.sc.we.idv.common.model.LivelinessResponse;
import com.sc.we.idv.common.model.ServiceInfo;
import com.sc.we.idv.config.IDVConfig;
import com.sc.we.idv.logger.LoggerContextService;
import com.sc.we.idv.model.CvRequest;
import com.sc.we.idv.model.CvResponse;
import com.sc.we.idv.model.DemographicRequest;
import com.sc.we.idv.model.DemographicResponse;
import com.sc.we.idv.model.EnquiryRequest;
import com.sc.we.idv.model.EnquiryResponse;
import com.sc.we.idv.model.IDVBO;
import com.sc.we.idv.model.converter.DemographicRequestToIDVBOConverter;
import com.sc.we.idv.model.converter.LivelinessRequestToIDVBOConverter;
import com.sc.we.idv.service.IDVService;
import com.sc.we.idv.utils.RequestFieldValidator;
import com.sc.we.idv.model.VerificationResult;
import static com.sc.we.idv.common.constants.IDVConstants.*;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@RestController
@RequestMapping("${context.path}")
public class IDVController {
private static Logger log = LoggerFactory.getLogger(IDVController.class);
@Autowired
private LoggerContextService loggerContextService;
@Autowired
private IDVService idvService;
@Autowired
private RequestFieldValidator requestFieldValidator;
@Autowired
private IDVConfig config;
@Value("${stub.enabled}")
private boolean isStubEnabled;
@Value("${cv.mockresponse.enable}")
private boolean isCVMockResponseEnabled;
private boolean shouldStubInitialize = false;
@PostMapping("/getDemographics")
public ResponseEntity<DemographicResponse> getDemographics(@RequestBody DemographicRequest request) throws Exception {
log.info("------------------Inside IDVController getDemographics------------------");
loggerContextService.logAPIStart(request);
requestFieldValidator.validateGetDemographicsRequest(request);
DemographicRequestToIDVBOConverter conveter = new DemographicRequestToIDVBOConverter();
IDVBO idvBO = conveter.convertReqToBusinessObject(request);
DemographicResponse res = idvService.getDemographics(idvBO);
loggerContextService.logAPIEnd();
log.info("------------------Exiting IDVController getDemographics------------------");
return new ResponseEntity<DemographicResponse>(res, HttpStatus.OK);
}
@PostMapping("/initialize")
public ResponseEntity<InitializeResponse> initialize(@RequestHeader(value = "stub", required = false, defaultValue = "n") String stubHeader, @RequestBody InitializeRequest request) throws Exception {
log.info("--------------Inside Initialize API-----------------");
if(isStubEnabled && "y".equalsIgnoreCase(stubHeader) && config.isStubEnabled(request.getInternalInfo().getSourceCountry(),
request.getInternalInfo().getSystemIdentifier())) {
log.info("--------------Stubbing Initialize response-----------------");
shouldStubInitialize=true;
}
loggerContextService.logAPIStart(request);
requestFieldValidator.validateInitializeRequest(request);
InitializeResponse initialize = idvService.initialize(request,shouldStubInitialize);
loggerContextService.logAPIEnd();
log.info("------------------Exiting IDVController Initialize API------------------");
return new ResponseEntity<InitializeResponse>(initialize, HttpStatus.OK);
}
@PostMapping("/verify/customer")
public ResponseEntity<CvResponse> verifyCustomer(@RequestBody CvRequest cvRequest) throws Exception {
log.info("--------------Inside Customer Verification API-----------------");
loggerContextService.logAPIStart(cvRequest);
RequestFieldValidator.validateMandatoryFieldsCustomerVerificationAPI(cvRequest);
requestFieldValidator.invalidAttrtibutesCustomerVerification(cvRequest);
CvResponse cvResponse = null;
cvResponse = idvService.verifyCustomerData(cvRequest);
if(isCVMockResponseEnabled && config.isCVMockResponseEnabled(cvRequest.getInternalInfo().getSourceCountry(),
cvRequest.getInternalInfo().getSystemIdentifier())) {
cvResponse = buildMockResponse(cvRequest.getServiceInfo());
}
loggerContextService.logAPIEnd();
log.info("--------------Exiting IDVController Customer Verification API-----------------");
return new ResponseEntity<CvResponse>(cvResponse, HttpStatus.OK);
}
private CvResponse buildMockResponse(ServiceInfo serviceInfo) {
CvResponse res = new CvResponse();
res.setServiceInfo(serviceInfo);
res.setVerificationResult(buildVerificationResult());
return res;
}
private VerificationResult buildVerificationResult() {
VerificationResult res = new VerificationResult();
res.overallStatus(VERIFICATION_STATUS_SUCCESS);
Timestamp timeStamp = new Timestamp(new Date().getTime());
res.verificationDateTime(timeStamp.toString());
res.setDocumentVerification(VERIFICATION_STATUS_SUCCESS);
res.setFacialMatchResult(VERIFICATION_STATUS_SUCCESS);
res.setPoiMatchStatus(VERIFICATION_STATUS_SUCCESS);
res.setPoiFailureReason(getEmptyList());
res.setNameOfVerification(VERIFICATION_STATUS_SUCCESS);
res.setDateOfBirthVerification(VERIFICATION_STATUS_SUCCESS);
res.setIdNumberVerification(VERIFICATION_STATUS_SUCCESS);
res.setPoaMatchStatus(VERIFICATION_STATUS_SUCCESS);
res.setPoaFailureReason(getEmptyList());
res.setDocumentFacialFailureReason(getEmptyList());
return res;
}
private List<String> getEmptyList() {
List<String> list = new ArrayList<String>();
list.add("");
return list;
}
@PostMapping("/enquiry")
public ResponseEntity<EnquiryResponse> handleEnquiryRequest(@RequestBody EnquiryRequest request) throws Exception {
log.info("--------------Inside Enquiry API-----------------");
RequestFieldValidator.validateMandatoryFieldsEnquiry(request);
requestFieldValidator.invalidAttrtibutesEnquiry(request);
EnquiryResponse response = idvService.enquiryIDV(request);
if(isCVMockResponseEnabled && config.isCVMockResponseEnabled(request.getInternalInfo().getSourceCountry(),
request.getInternalInfo().getSystemIdentifier())) {
CvResponse cvResponse = buildMockResponse(request.getServiceInfo());
response.setVerificationResult(cvResponse.getVerificationResult());
}
log.info("--------------Exiting IDVController Enquiry API-----------------");
return new ResponseEntity<EnquiryResponse>(response, HttpStatus.OK);
}
@PostMapping("/verify/biometric")
public ResponseEntity<BiometricVerificationResponse> verifyFingerPrint(
@RequestBody BiometricVerificationRequest request) throws Exception {
log.info("--------------Starting Fingerprint verification API-----------------");
requestFieldValidator.validateBiometricVerificationRequest(request);
BiometricVerificationResponse response = idvService.verifyFingerPrint(request);
log.info("--------------Exiting Fingerprint verification API-----------------");
return new ResponseEntity<BiometricVerificationResponse>(response, HttpStatus.OK);
}
@PostMapping("/verify/liveliness")
public ResponseEntity<LivelinessResponse> verifyFaceMatch(@RequestBody LivelinessRequest request) throws Exception {
log.info("--------------Starting Liveliness API-----------------");
RequestFieldValidator.validateMandatoryFieldsLiveliness(request);
requestFieldValidator.validateLivelinessVerificationRequest(request);
LivelinessRequestToIDVBOConverter converter = new LivelinessRequestToIDVBOConverter();
IDVBO idvBo = converter.convertLivelinessRequestToIDVBO(request);
LivelinessResponse response = idvService.verifyFaceMatch(idvBo);
log.info("--------------Exiting Liveliness API-----------------");
return new ResponseEntity<LivelinessResponse>(response, HttpStatus.OK);
}
}
I have to use the value of shouldStubInitialize in ZolozClient, how can I use without dependency Injection,
package com.sc.we.idv.plugin.client.zoloz;
import java.io.FileNotFoundException;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.URLEncoder;
import java.util.Map;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.HttpServerErrorException.ServiceUnavailable;
import org.springframework.web.client.RestTemplate;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.sc.we.idv.common.constants.IDVConstants;
import com.sc.we.idv.common.utils.Utils;
import com.sc.we.idv.controller.IDVController;
import com.sc.we.idv.common.exceptions.DownstreamSystemErrorException;
import com.sc.we.idv.exceptions.TimeoutErrorException;
import com.sc.we.idv.plugin.client.zoloz.model.DeleteCustomerRequest;
import com.sc.we.idv.plugin.client.zoloz.model.DeleteCustomerResponse;
import com.sc.we.idv.plugin.client.zoloz.model.EncryptRequestDTO;
import com.sc.we.idv.plugin.client.zoloz.model.ZolozCheckResultRequest;
import com.sc.we.idv.plugin.client.zoloz.model.ZolozCheckResultResponse;
import com.sc.we.idv.plugin.client.zoloz.model.ZolozInitializeRequest;
import com.sc.we.idv.plugin.client.zoloz.model.ZolozInitializeResponse;
import com.sc.we.idv.plugin.client.zoloz.model.ZolozResponseData;
@Component
public class ZolozClient {
private Logger log = LoggerFactory.getLogger(ZolozClient.class);
@Value("${zoloz.url}")
private String ZOLOZ_URL;
@Value("${zoloz.client.id}")
private String clientId;
@Value("${stub.virtual-service-url}")
private String virtualServiceUrl;
@Value("${stub.real-service-url}")
private String realServiceUrl;
@Autowired
@Qualifier("RestTemplate")
RestTemplate restTemplate;
@Autowired
private ZolozEncryptAndGenSignService zolozEncryptAndSignService;
@Autowired
private ZolozCheckSignAndDecryptService checkSignatureAndDecryptService;
public ZolozClient(IDVController idvController)
{
this.idvController= idvController;
}
public ZolozCheckResultResponse getDemographics(ZolozCheckResultRequest req)
throws DownstreamSystemErrorException, Exception {
String res_json = callZolozService(req.getTransactionId(), getRequestJSON(req),
IDVConstants.GET_DEMOGRAPHIC_ZOLOZ_ENDPOINT);
log.info("Zoloz Check Request String : " + getRequestJSON(req));
log.info("Zoloz Check Response String : " + res_json);
return getResponse(res_json, ZolozCheckResultResponse.class);
}
public ZolozInitializeResponse initilize(ZolozInitializeRequest req)
throws DownstreamSystemErrorException, Exception {
String res_json = callZolozService(req.getBizId(), getRequestJSON(req),
IDVConstants.GET_INITIALIZE_ZOLOZ_ENDPOINT);
log.info("Zoloz Initialize Request String : " + getRequestJSON(req));
log.info("Zoloz Initialize Response String : " + res_json);
return getResponse(res_json, ZolozInitializeResponse.class);
}
public DeleteCustomerResponse deleteCustomerDataFromVendor(DeleteCustomerRequest req)
throws DownstreamSystemErrorException, Exception {
String res_json = callZolozService(req.getTransactionId(), getRequestJSON(req),
IDVConstants.DELETE_CUSTOMER_ZOLOZ_ENDPOINT);
log.info("Zoloz Delete Request String : " + getRequestJSON(req));
log.info("Zoloz Delete Response String : " + res_json);
return getResponse(res_json, DeleteCustomerResponse.class);
}
private String callZolozService(String transactionId, String requestJson, String zolozApiName)
throws DownstreamSystemErrorException, Exception {
try {
log.info("1. ==== Encrypting the request ====");
EncryptRequestDTO encryptRequestDTO = zolozEncryptAndSignService.encryptService(requestJson);
log.info("2. ==== Generate signature and add in the request ====");
encryptRequestDTO = zolozEncryptAndSignService.generateSignature(encryptRequestDTO, zolozApiName);
log.info("3. ====Call Zoloz API Rest Template ==== ZOLOZ API NAME" + zolozApiName);
ZOLOZ_URL = idvController.isShouldStubInitialize() ? realServiceUrl : virtualServiceUrl;
System.out.println(idvController.isShouldStubInitialize());
ZolozResponseData data = doCall(transactionId, generateUrlFromMethodName(ZOLOZ_URL, zolozApiName),
encryptRequestDTO.getEncryptedkey(), encryptRequestDTO.getReqTime(),
encryptRequestDTO.getSignature(), encryptRequestDTO.getEncryptedRequest(), clientId);
log.info("4. ====Check if signature is valid====");
String resultContent = checkSignatureAndDecryptService.checkSignatureAndReturnData(data, zolozApiName);
log.info("5. ==== Decrypt and get the response data ====");
return checkSignatureAndDecryptService.decryptResponse(data, encryptRequestDTO, resultContent);
} catch (Exception e) {
if (e.getCause() instanceof SocketTimeoutException) {
throw new TimeoutErrorException(transactionId, "Time Out" + e.getMessage());
} else if (e.getCause() instanceof ServiceUnavailable || e.getCause() instanceof ConnectException) {
throw new DownstreamSystemErrorException(transactionId, "Services Down");
} else if (e.getCause() instanceof FileNotFoundException) {
throw new DownstreamSystemErrorException(transactionId, "Endpoint not found");
} else {
throw new DownstreamSystemErrorException(transactionId, e.getMessage().toString());
}
}
}
protected ZolozResponseData doCall(String transactionId, String baseUrl, String encryptKey, String reqTime,
String signature, String request, String clientId) throws Exception, DownstreamSystemErrorException {
ZolozResponseData data = new ZolozResponseData();
try {
log.info("=========zoloz calling rest template==========");
HttpHeaders headers = new HttpHeaders();
if (encryptKey != null) {
headers.add(HttpHeaders.CONTENT_TYPE, IDVConstants.CONTENT_TYPE_TEXT);
} else {
headers.add(HttpHeaders.CONTENT_TYPE, IDVConstants.CONTENT_TYPE_JSON);
}
headers.add(IDVConstants.HEADER_CLIENT_ID, clientId);
headers.add(IDVConstants.HEADER_REQUEST_TIME, reqTime);
headers.add(IDVConstants.HEADER_CONSUMER_COMPONENT, IDVConstants.RTOB_CHANNELS);
headers.add(IDVConstants.HEADER_TRACKING_ID, Utils.generateRequestID());
if (signature != null) {
headers.add(IDVConstants.HEADER_SIGNATURE,
IDVConstants.HEADER_ALGO_SIGNATURE + URLEncoder.encode(signature, IDVConstants.UTF8));
}
if (encryptKey != null) {
headers.add(IDVConstants.ENCRYPT,
IDVConstants.HEADER_ALGO_SYMMETRIC + URLEncoder.encode(encryptKey, IDVConstants.UTF8));
}
HttpEntity<String> httpRequest = new HttpEntity<>(request, headers);
ResponseEntity<String> response = restTemplate.exchange(baseUrl, HttpMethod.POST, httpRequest,
String.class);
data.setHeader(response.getHeaders().entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)));
data.setContent(response.getBody());
if (response.getStatusCode().equals(HttpStatus.OK)) {
if (response.getBody() == null) {
throw new DownstreamSystemErrorException(transactionId, "No data from the External system");
}
} else {
throw new DownstreamSystemErrorException(transactionId, "No data from the External system");
}
} catch (DownstreamSystemErrorException e) {
log.error("Excepton in ZolozClient doCall ", e);
throw new DownstreamSystemErrorException(transactionId, e.getMessage().toString());
}
return data;
}
private String generateUrlFromMethodName(String hostUrl, String zolozApiName) {
String url = hostUrl + "/" + IDVConstants.API + "/" + zolozApiName.replaceAll("\\.", "/");
log.info("API URL = " + url);
return url;
}
private <T> T getResponse(String jsonResponse, Class<T> classType)
throws JsonMappingException, JsonProcessingException {
ObjectMapper mapper = JsonMapper.builder().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.build();
return mapper.readValue(jsonResponse, classType);
}
String getRequestJSON(Object RequestObject) throws JsonProcessingException {
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
return ow.writeValueAsString(RequestObject);
}
}