From 90019485d73c67f1ac6dab3b7154efb3cda7193d Mon Sep 17 00:00:00 2001 From: Monobikash Das <43202165+MonobikashDas@users.noreply.github.com> Date: Tue, 25 Jan 2022 15:36:55 +0530 Subject: [PATCH] taken latest changes from 1.2.0-rc2 (#56) * Created 1.2.0-rc2 (#23) Co-authored-by: Monobikash Das * MOSIP-15420: Upgraded swagger2.0 to openapi3.0 for packet manager service (#31) Resolved merge conflict * Added logger and fixed test cases (#32) Co-authored-by: Monobikash Das * MOSIP-15420: Generated openapi.json for packet manager service (#34) * MOSIP-15420: Upgraded swagger2.0 to openapi3.0 for packet manager service Resolved merge conflict * MOSIP-15420: Generated openapi.json for packet manager service Resolved merge conflicts * MOSIP-18453 : improved usage of caching (#36) Co-authored-by: Monobikash Das * Added sonar_analysis * MOSIP-18450 : added exception for objects not available in minio (#37) * MOSIP-18450 : added exception for objects not available in minio * Added sonar_analysis * MOSIP-18910 : fixed refNumber issue in DocumentDto * MOSIP-18450 : added exception for objects not available in minio * MOSIP-18910 : fixed refNumber issue in DocumentDto * initial change * MOSIP-18450 : added exception for objects not available in minio * MOSIP-18910 : fixed refNumber issue in DocumentDto * initial change * MOSIP-18793 :Added afterburner + fixes Co-authored-by: Monobikash Das Co-authored-by: Rakshitha650 <76676196+Rakshitha650@users.noreply.github.com> Co-authored-by: Mandeep Dhiman <46880392+mandeepdhiman123@users.noreply.github.com> Co-authored-by: Admin * Mosip:15286 Changed openapi.json name. * updated the Sonar_analysis code and removed the Dskiptests * updated the Sonar_analysis code and removed the Dskiptests * added gpg keys * changes for real bio * Merged with 1.2.0-rc2 * changes for real bio * Auth role issue fixed (#46) * MOR-147 : multithreading issue for create packet api (#52) Co-authored-by: Admin * selfTokenRestTemplate fix for regclient Co-authored-by: Monobikash Das Co-authored-by: kameshsr <47484458+kameshsr@users.noreply.github.com> Co-authored-by: Rakshitha650 <76676196+Rakshitha650@users.noreply.github.com> Co-authored-by: Mandeep Dhiman <46880392+mandeepdhiman123@users.noreply.github.com> Co-authored-by: Admin Co-authored-by: Rakshith650 Co-authored-by: Sasikumar Ganesan Signed-off-by: Sowmya Ujjappa Banakar --- .github/workflows/push_trigger.yml | 2 +- .../commons/packet/audit/AuditLogEntry.java | 1 + .../constants/PacketUtilityErrorCodes.java | 3 +- .../commons/packet/facade/PacketWriter.java | 3 + .../impl/OnlinePacketCryptoServiceImpl.java | 22 +++-- .../commons/packet/impl/PacketWriterImpl.java | 96 ++++++++++--------- .../commons/packet/spi/IPacketWriter.java | 2 + .../commons/packet/util/IdSchemaUtils.java | 1 + .../mosip/commons/packet/util/ZipUtils.java | 73 -------------- .../impl/OfflinePacketCryptoServiceTest.java | 3 +- .../test/impl/PacketWriterImplTest.java | 1 - commons-packet/commons-packet-service/pom.xml | 2 +- .../controller/PacketReaderController.java | 9 -- .../controller/PacketWriterController.java | 1 - .../packetmanager/dto/AuthorizedRolesDTO.java | 2 + .../service/PacketReaderService.java | 17 +++- .../src/main/resources/bootstrap.properties | 4 +- .../PacketManagerControllerTest.java | 1 - .../test/service/PacketReaderServiceTest.java | 3 +- 19 files changed, 100 insertions(+), 146 deletions(-) diff --git a/.github/workflows/push_trigger.yml b/.github/workflows/push_trigger.yml index 069ca0539f..428569faed 100644 --- a/.github/workflows/push_trigger.yml +++ b/.github/workflows/push_trigger.yml @@ -197,7 +197,7 @@ jobs: env: SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }} # required if: failure() # Pick up events even if the job fails or is canceled. - + sonar_analysis: runs-on: ubuntu-latest steps: diff --git a/commons-packet/commons-packet-manager/src/main/java/io/mosip/commons/packet/audit/AuditLogEntry.java b/commons-packet/commons-packet-manager/src/main/java/io/mosip/commons/packet/audit/AuditLogEntry.java index b846060655..68973740e4 100644 --- a/commons-packet/commons-packet-manager/src/main/java/io/mosip/commons/packet/audit/AuditLogEntry.java +++ b/commons-packet/commons-packet-manager/src/main/java/io/mosip/commons/packet/audit/AuditLogEntry.java @@ -30,6 +30,7 @@ public class AuditLogEntry { @Autowired @Lazy + @Qualifier("selfTokenRestTemplate") private RestTemplate restTemplate; @Autowired diff --git a/commons-packet/commons-packet-manager/src/main/java/io/mosip/commons/packet/constants/PacketUtilityErrorCodes.java b/commons-packet/commons-packet-manager/src/main/java/io/mosip/commons/packet/constants/PacketUtilityErrorCodes.java index 586e69b0cb..5b46d90c10 100644 --- a/commons-packet/commons-packet-manager/src/main/java/io/mosip/commons/packet/constants/PacketUtilityErrorCodes.java +++ b/commons-packet/commons-packet-manager/src/main/java/io/mosip/commons/packet/constants/PacketUtilityErrorCodes.java @@ -30,7 +30,8 @@ public enum PacketUtilityErrorCodes { TAG_NOT_FOUND("KER-PUT-024", "Requested tag not present"), SOURCE_NOT_PRESENT("KER-PUT-025", "Invalid source or process."), DELETE_TAGGING_FAILED("KER-PUT-026", "Failed to delete tags."), - OBJECT_DOESNOT_EXISTS("KER-PUT-027", "Object doesnot exists."),; + OBJECT_DOESNOT_EXISTS("KER-PUT-027", "Object doesnot exists."), + UNKNOWN_EXCEPTION("KER-PUT-028", "Unknown Exception."); diff --git a/commons-packet/commons-packet-manager/src/main/java/io/mosip/commons/packet/facade/PacketWriter.java b/commons-packet/commons-packet-manager/src/main/java/io/mosip/commons/packet/facade/PacketWriter.java index ce3c529ba1..b5004a14f8 100644 --- a/commons-packet/commons-packet-manager/src/main/java/io/mosip/commons/packet/facade/PacketWriter.java +++ b/commons-packet/commons-packet-manager/src/main/java/io/mosip/commons/packet/facade/PacketWriter.java @@ -184,6 +184,9 @@ public List createPacket(PacketDto packetDto) { LOGGER.info(PacketManagerLogger.SESSIONID, PacketManagerLogger.REGISTRATIONID, packetDto.getId(), ExceptionUtils.getStackTrace(e)); LOGGER.error(PacketManagerLogger.SESSIONID, PacketManagerLogger.REGISTRATIONID, packetDto.getId(), ExceptionUtils.getStackTrace(e)); + } finally { + // remove object from registration packet hashmap + provider.removePacket(packetDto.getId()); } return packetInfos; } diff --git a/commons-packet/commons-packet-manager/src/main/java/io/mosip/commons/packet/impl/OnlinePacketCryptoServiceImpl.java b/commons-packet/commons-packet-manager/src/main/java/io/mosip/commons/packet/impl/OnlinePacketCryptoServiceImpl.java index 3e2556eb33..046d81cba2 100644 --- a/commons-packet/commons-packet-manager/src/main/java/io/mosip/commons/packet/impl/OnlinePacketCryptoServiceImpl.java +++ b/commons-packet/commons-packet-manager/src/main/java/io/mosip/commons/packet/impl/OnlinePacketCryptoServiceImpl.java @@ -21,6 +21,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.ApplicationContext; import org.springframework.http.HttpEntity; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; @@ -65,7 +66,8 @@ public class OnlinePacketCryptoServiceImpl implements IPacketCryptoService { private boolean isPrependThumbprintEnabled; @Autowired - private RestTemplate restTemplate; + private ApplicationContext applicationContext; + private RestTemplate restTemplate = null; @Autowired private ObjectMapper mapper; @@ -96,7 +98,7 @@ public byte[] sign(byte[] packet) { .parse(DateUtils.getUTCCurrentDateTimeString(DATETIME_PATTERN), format); request.setRequesttime(localdatetime); HttpEntity> httpEntity = new HttpEntity<>(request); - ResponseEntity response = restTemplate.exchange(keymanagerCsSignUrl, HttpMethod.POST, httpEntity, + ResponseEntity response = getRestTemplate().exchange(keymanagerCsSignUrl, HttpMethod.POST, httpEntity, String.class); LinkedHashMap responseMap = (LinkedHashMap) mapper.readValue(response.getBody(), LinkedHashMap.class).get("response"); if (responseMap != null && responseMap.size() > 0) @@ -142,7 +144,7 @@ public byte[] encrypt(String refId, byte[] packet) { request.setVersion(APPLICATION_VERSION); HttpEntity> httpEntity = new HttpEntity<>(request); - ResponseEntity response = restTemplate.exchange(cryptomanagerEncryptUrl, HttpMethod.POST, httpEntity, String.class); + ResponseEntity response = getRestTemplate().exchange(cryptomanagerEncryptUrl, HttpMethod.POST, httpEntity, String.class); CryptomanagerResponseDto responseObject = mapper.readValue(response.getBody(), CryptomanagerResponseDto.class); if (responseObject != null && responseObject.getErrors() != null && !responseObject.getErrors().isEmpty()) { @@ -212,7 +214,7 @@ public byte[] decrypt(String refId, byte[] packet) { request.setVersion(APPLICATION_VERSION); HttpEntity> httpEntity = new HttpEntity<>(request); - ResponseEntity response = restTemplate.exchange(cryptomanagerDecryptUrl, HttpMethod.POST, httpEntity, String.class); + ResponseEntity response = getRestTemplate().exchange(cryptomanagerDecryptUrl, HttpMethod.POST, httpEntity, String.class); CryptomanagerResponseDto responseObject = mapper.readValue(response.getBody(), CryptomanagerResponseDto.class); @@ -269,7 +271,7 @@ public boolean verify(String refId, byte[] packet, byte[] signature) { .parse(DateUtils.getUTCCurrentDateTimeString(DATETIME_PATTERN), format); request.setRequesttime(localdatetime); HttpEntity> httpEntity = new HttpEntity<>(request); - ResponseEntity response = restTemplate.exchange(keymanagerCsverifysignUrl, HttpMethod.POST, httpEntity, + ResponseEntity response = getRestTemplate().exchange(keymanagerCsverifysignUrl, HttpMethod.POST, httpEntity, String.class); LinkedHashMap responseMap = (LinkedHashMap) mapper.readValue(response.getBody(), LinkedHashMap.class).get("response");//.get("signature"); if (responseMap != null && responseMap.size() > 0) { @@ -296,7 +298,7 @@ public boolean verify(String refId, byte[] packet, byte[] signature) { private String getPublicKey(String refId) throws IOException { String machineId = refId.split("_")[1]; - ResponseEntity response = restTemplate.exchange(syncdataGetTpmKeyUrl+machineId, HttpMethod.GET, null, + ResponseEntity response = getRestTemplate().exchange(syncdataGetTpmKeyUrl+machineId, HttpMethod.GET, null, String.class); LinkedHashMap responseMap = (LinkedHashMap) mapper.readValue(response.getBody(), LinkedHashMap.class).get("response");//.get("signature"); if (responseMap != null && responseMap.size() > 0) @@ -307,4 +309,12 @@ private String getPublicKey(String refId) throws IOException { throw new SignatureException(); } } + + private RestTemplate getRestTemplate() { + if (this.restTemplate == null) { + this.restTemplate = (RestTemplate)this.applicationContext.getBean("selfTokenRestTemplate"); + } + + return this.restTemplate; + } } diff --git a/commons-packet/commons-packet-manager/src/main/java/io/mosip/commons/packet/impl/PacketWriterImpl.java b/commons-packet/commons-packet-manager/src/main/java/io/mosip/commons/packet/impl/PacketWriterImpl.java index 833ca47c34..fdad2118f9 100644 --- a/commons-packet/commons-packet-manager/src/main/java/io/mosip/commons/packet/impl/PacketWriterImpl.java +++ b/commons-packet/commons-packet-manager/src/main/java/io/mosip/commons/packet/impl/PacketWriterImpl.java @@ -80,14 +80,15 @@ public class PacketWriterImpl implements IPacketWriter { @Value("${packetmanager.zip.datetime.pattern:yyyyMMddHHmmss}") private String zipDatetimePattern; - private RegistrationPacket registrationPacket = null; + private Map registrationPacketMap = new HashMap<>(); public RegistrationPacket initialize(String id) { - if (this.registrationPacket == null || !registrationPacket.getRegistrationId().equalsIgnoreCase(id)) { - this.registrationPacket = new RegistrationPacket(dateTimePattern); - this.registrationPacket.setRegistrationId(id); + if (registrationPacketMap.get(id) == null) { + RegistrationPacket registrationPacket = new RegistrationPacket(dateTimePattern); + registrationPacket.setRegistrationId(id); + registrationPacketMap.put(id, registrationPacket); } - return registrationPacket; + return registrationPacketMap.get(id); } @Override @@ -134,7 +135,7 @@ public void addMetaInfo(String id, String key, String value) { private List createPacket(String id, String version, String schemaJson, String source, String process, String additionalInfoReqId, String refId, boolean offlineMode) throws PacketCreatorException { LOGGER.info(LoggerFileConstant.SESSIONID.toString(), LoggerFileConstant.ID.toString(), id, "Started packet creation"); - if (this.registrationPacket == null || !registrationPacket.getRegistrationId().equalsIgnoreCase(id)) + if (registrationPacketMap.get(id) == null) throw new PacketCreatorException(ErrorCode.INITIALIZATION_ERROR.getErrorCode(), ErrorCode.INITIALIZATION_ERROR.getErrorMessage()); @@ -195,7 +196,8 @@ private List createPacket(String id, String version, String schemaJs throw new PacketCreatorException(ErrorCode.PKT_ZIP_ERROR.getErrorCode(), ErrorCode.PKT_ZIP_ERROR.getErrorMessage().concat(ExceptionUtils.getStackTrace(e))); } finally { - this.registrationPacket = null; + this.registrationPacketMap.remove(id); + LOGGER.debug("registrationPacketMap size ====================================> " + registrationPacketMap.size()); } LOGGER.info(LoggerFileConstant.SESSIONID.toString(), LoggerFileConstant.ID.toString(), id, "Exiting packet creation"); @@ -205,7 +207,7 @@ private List createPacket(String id, String version, String schemaJs @SuppressWarnings("unchecked") private byte[] createSubpacket(double version, List schemaFields, boolean isDefault, String id, boolean offlineMode) throws PacketCreatorException { - + RegistrationPacket registrationPacket = registrationPacketMap.get(id); ByteArrayOutputStream out = new ByteArrayOutputStream(); try (ZipOutputStream subpacketZip = new ZipOutputStream(new BufferedOutputStream(out))) { LOGGER.info(LoggerFileConstant.SESSIONID.toString(), LoggerFileConstant.ID.toString(), @@ -214,8 +216,8 @@ private byte[] createSubpacket(double version, List schemaFields, boolea Map hashSequences = new HashMap<>(); identity.put(PacketManagerConstants.IDSCHEMA_VERSION, version); - this.registrationPacket.getMetaData().put(PacketManagerConstants.REGISTRATIONID, id); - this.registrationPacket.getMetaData().put(PacketManagerConstants.META_CREATION_DATE, this.registrationPacket.getCreationDate()); + registrationPacket.getMetaData().put(PacketManagerConstants.REGISTRATIONID, id); + registrationPacket.getMetaData().put(PacketManagerConstants.META_CREATION_DATE, registrationPacket.getCreationDate()); for (Object obj : schemaFields) { Map field = (Map) obj; @@ -224,25 +226,25 @@ private byte[] createSubpacket(double version, List schemaFields, boolea id, "Adding field : " + fieldName); switch ((String) field.get(PacketManagerConstants.SCHEMA_TYPE)) { case PacketManagerConstants.BIOMETRICS_TYPE: - if (this.registrationPacket.getBiometrics().get(fieldName) != null) - addBiometricDetailsToZip(fieldName, identity, subpacketZip, hashSequences, offlineMode); + if (registrationPacket.getBiometrics().get(fieldName) != null) + addBiometricDetailsToZip(registrationPacket, fieldName, identity, subpacketZip, hashSequences, offlineMode); break; case PacketManagerConstants.DOCUMENTS_TYPE: - if (this.registrationPacket.getDocuments().get(fieldName) != null) - addDocumentDetailsToZip(fieldName, identity, subpacketZip, hashSequences, offlineMode); + if (registrationPacket.getDocuments().get(fieldName) != null) + addDocumentDetailsToZip(registrationPacket, fieldName, identity, subpacketZip, hashSequences, offlineMode); break; default: - if (this.registrationPacket.getDemographics().get(fieldName) != null) - identity.put(fieldName, this.registrationPacket.getDemographics().get(fieldName)); + if (registrationPacket.getDemographics().get(fieldName) != null) + identity.put(fieldName, registrationPacket.getDemographics().get(fieldName)); break; } } byte[] identityBytes = getIdentity(identity).getBytes(); - addEntryToZip(PacketManagerConstants.IDENTITY_FILENAME_WITH_EXT, identityBytes, subpacketZip); + addEntryToZip(registrationPacket, PacketManagerConstants.IDENTITY_FILENAME_WITH_EXT, identityBytes, subpacketZip); addHashSequenceWithSource(PacketManagerConstants.DEMOGRAPHIC_SEQ, PacketManagerConstants.IDENTITY_FILENAME, identityBytes, hashSequences); - addOtherFilesToZip(isDefault, subpacketZip, hashSequences, offlineMode); + addOtherFilesToZip(registrationPacket, isDefault, subpacketZip, hashSequences, offlineMode); } catch (JsonProcessingException e) { throw new PacketCreatorException(ErrorCode.OBJECT_TO_JSON_ERROR.getErrorCode(), @@ -254,22 +256,22 @@ private byte[] createSubpacket(double version, List schemaFields, boolea return out.toByteArray(); } - private void addDocumentDetailsToZip(String fieldName, Map identity, + private void addDocumentDetailsToZip(RegistrationPacket registrationPacket, String fieldName, Map identity, ZipOutputStream zipOutputStream, Map hashSequences, boolean offlineMode) throws PacketCreatorException { - Document document = this.registrationPacket.getDocuments().get(fieldName); + Document document = registrationPacket.getDocuments().get(fieldName); //filename without extension must be set as value in ID.json identity.put(fieldName, new DocumentType(fieldName, document.getType(), document.getFormat(), document.getRefNumber())); String fileName = String.format("%s.%s", fieldName, document.getFormat()); - addEntryToZip(fileName, document.getDocument(), zipOutputStream); - this.registrationPacket.getMetaData().put(fieldName, document.getType()); + addEntryToZip(registrationPacket, fileName, document.getDocument(), zipOutputStream); + registrationPacket.getMetaData().put(fieldName, document.getType()); addHashSequenceWithSource(PacketManagerConstants.DEMOGRAPHIC_SEQ, fieldName, document.getDocument(), hashSequences); } - private void addBiometricDetailsToZip(String fieldName, Map identity, + private void addBiometricDetailsToZip(RegistrationPacket registrationPacket, String fieldName, Map identity, ZipOutputStream zipOutputStream, Map hashSequences, boolean offlineMode) throws PacketCreatorException { - BiometricRecord birType = this.registrationPacket.getBiometrics().get(fieldName); + BiometricRecord birType = registrationPacket.getBiometrics().get(fieldName); if (birType != null && birType.getSegments() != null && !birType.getSegments().isEmpty()) { byte[] xmlBytes; @@ -280,7 +282,7 @@ private void addBiometricDetailsToZip(String fieldName, Map iden ErrorCode.BIR_TO_XML_ERROR.getErrorMessage().concat(ExceptionUtils.getStackTrace(e))); } - addEntryToZip(String.format(PacketManagerConstants.CBEFF_FILENAME_WITH_EXT, fieldName), xmlBytes, zipOutputStream); + addEntryToZip(registrationPacket, String.format(PacketManagerConstants.CBEFF_FILENAME_WITH_EXT, fieldName), xmlBytes, zipOutputStream); identity.put(fieldName, new BiometricsType(PacketManagerConstants.CBEFF_FILE_FORMAT, PacketManagerConstants.CBEFF_VERSION, String.format(PacketManagerConstants.CBEFF_FILENAME, fieldName))); addHashSequenceWithSource(PacketManagerConstants.BIOMETRIC_SEQ, String.format(PacketManagerConstants.CBEFF_FILENAME, @@ -296,36 +298,36 @@ private void addHashSequenceWithSource(String sequenceType, String name, byte[] hashSequences.get(sequenceType).addHashSource(name, bytes); } - private void addOtherFilesToZip(boolean isDefault, ZipOutputStream zipOutputStream, + private void addOtherFilesToZip(RegistrationPacket registrationPacket, boolean isDefault, ZipOutputStream zipOutputStream, Map hashSequences, boolean offlineMode) throws JsonProcessingException, PacketCreatorException, IOException, NoSuchAlgorithmException { if (isDefault) { - addOperationsBiometricsToZip(PacketManagerConstants.OFFICER, + addOperationsBiometricsToZip(registrationPacket, PacketManagerConstants.OFFICER, zipOutputStream, hashSequences, offlineMode); - addOperationsBiometricsToZip(PacketManagerConstants.SUPERVISOR, + addOperationsBiometricsToZip(registrationPacket, PacketManagerConstants.SUPERVISOR, zipOutputStream, hashSequences, offlineMode); - if (this.registrationPacket.getAudits() == null || this.registrationPacket.getAudits().isEmpty()) + if (registrationPacket.getAudits() == null || registrationPacket.getAudits().isEmpty()) throw new PacketCreatorException(ErrorCode.AUDITS_REQUIRED.getErrorCode(), ErrorCode.AUDITS_REQUIRED.getErrorMessage()); - byte[] auditBytes = JsonUtils.javaObjectToJsonString(this.registrationPacket.getAudits()).getBytes(); - addEntryToZip(PacketManagerConstants.AUDIT_FILENAME_WITH_EXT, auditBytes, zipOutputStream); + byte[] auditBytes = JsonUtils.javaObjectToJsonString(registrationPacket.getAudits()).getBytes(); + addEntryToZip(registrationPacket, PacketManagerConstants.AUDIT_FILENAME_WITH_EXT, auditBytes, zipOutputStream); addHashSequenceWithSource(PacketManagerConstants.OPERATIONS_SEQ, PacketManagerConstants.AUDIT_FILENAME, auditBytes, hashSequences); HashSequenceMetaInfo hashSequenceMetaInfo = hashSequences.get(PacketManagerConstants.OPERATIONS_SEQ); - addEntryToZip(PacketManagerConstants.PACKET_OPER_HASH_FILENAME, + addEntryToZip(registrationPacket, PacketManagerConstants.PACKET_OPER_HASH_FILENAME, PacketManagerHelper.generateHash(hashSequenceMetaInfo.getValue(), hashSequenceMetaInfo.getHashSource()), zipOutputStream); - this.registrationPacket.getMetaData().put(HASHSEQUENCE2, Lists.newArrayList(hashSequenceMetaInfo)); + registrationPacket.getMetaData().put(HASHSEQUENCE2, Lists.newArrayList(hashSequenceMetaInfo)); } - addPacketDataHash(hashSequences, zipOutputStream); - addEntryToZip(PacketManagerConstants.PACKET_META_FILENAME, getIdentity(this.registrationPacket.getMetaData()).getBytes(), zipOutputStream); + addPacketDataHash(registrationPacket, hashSequences, zipOutputStream); + addEntryToZip(registrationPacket, PacketManagerConstants.PACKET_META_FILENAME, getIdentity(registrationPacket.getMetaData()).getBytes(), zipOutputStream); } - private void addPacketDataHash(Map hashSequences, + private void addPacketDataHash(RegistrationPacket registrationPacket, Map hashSequences, ZipOutputStream zipOutputStream) throws PacketCreatorException, IOException, NoSuchAlgorithmException { LinkedList sequence = new LinkedList(); @@ -342,9 +344,9 @@ private void addPacketDataHash(Map hashSequences, hashSequenceMetaInfos.add(hashSequences.get(PacketManagerConstants.DEMOGRAPHIC_SEQ)); } if (hashSequenceMetaInfos.size() > 0) - this.registrationPacket.getMetaData().put(HASHSEQUENCE1, hashSequenceMetaInfos); + registrationPacket.getMetaData().put(HASHSEQUENCE1, hashSequenceMetaInfos); - addEntryToZip(PacketManagerConstants.PACKET_DATA_HASH_FILENAME, PacketManagerHelper.generateHash(sequence, data), + addEntryToZip(registrationPacket, PacketManagerConstants.PACKET_DATA_HASH_FILENAME, PacketManagerHelper.generateHash(sequence, data), zipOutputStream); } @@ -386,10 +388,10 @@ private Map> loadSchemaFields(String schemaJson) throws Pac } - private void addEntryToZip(String fileName, byte[] data, ZipOutputStream zipOutputStream) + private void addEntryToZip(RegistrationPacket registrationPacket, String fileName, byte[] data, ZipOutputStream zipOutputStream) throws PacketCreatorException { LOGGER.info(LoggerFileConstant.SESSIONID.toString(), LoggerFileConstant.ID.toString(), - this.registrationPacket.getRegistrationId(), "Adding file : " + fileName); + registrationPacket.getRegistrationId(), "Adding file : " + fileName); try { if (data != null) { ZipEntry zipEntry = new ZipEntry(fileName); @@ -406,10 +408,10 @@ private String getIdentity(Object object) throws JsonProcessingException { return "{ \"identity\" : " + JsonUtils.javaObjectToJsonString(object) + " } "; } - private void addOperationsBiometricsToZip(String operationType, + private void addOperationsBiometricsToZip(RegistrationPacket registrationPacket, String operationType, ZipOutputStream zipOutputStream, Map hashSequences, boolean offlineMode) throws PacketCreatorException { - BiometricRecord biometrics = this.registrationPacket.getBiometrics().get(operationType); + BiometricRecord biometrics = registrationPacket.getBiometrics().get(operationType); if (biometrics != null && biometrics.getSegments() != null && !biometrics.getSegments().isEmpty()) { byte[] xmlBytes; @@ -422,8 +424,8 @@ private void addOperationsBiometricsToZip(String operationType, if (xmlBytes != null) { String fileName = operationType + PacketManagerConstants.CBEFF_EXT; - addEntryToZip(fileName, xmlBytes, zipOutputStream); - this.registrationPacket.getMetaData().put(String.format("%sBiometricFileName", operationType), fileName); + addEntryToZip(registrationPacket, fileName, xmlBytes, zipOutputStream); + registrationPacket.getMetaData().put(String.format("%sBiometricFileName", operationType), fileName); addHashSequenceWithSource(PacketManagerConstants.OPERATIONS_SEQ, operationType, xmlBytes, hashSequences); } } @@ -446,4 +448,10 @@ private String getcurrentTimeStamp() { return LocalDateTime.now(ZoneId.of("UTC")).format(format); } + @Override + public void removePacket(String id) { + if (registrationPacketMap.get(id) != null) + registrationPacketMap.remove(id); + } + } diff --git a/commons-packet/commons-packet-manager/src/main/java/io/mosip/commons/packet/spi/IPacketWriter.java b/commons-packet/commons-packet-manager/src/main/java/io/mosip/commons/packet/spi/IPacketWriter.java index 5691507d94..39b9fa0211 100644 --- a/commons-packet/commons-packet-manager/src/main/java/io/mosip/commons/packet/spi/IPacketWriter.java +++ b/commons-packet/commons-packet-manager/src/main/java/io/mosip/commons/packet/spi/IPacketWriter.java @@ -33,4 +33,6 @@ public interface IPacketWriter { public List persistPacket(String id, String version, String schemaJson, String source, String process, String additionalInfoReqId, String refId, boolean offlineMode); + + void removePacket(String id); } diff --git a/commons-packet/commons-packet-manager/src/main/java/io/mosip/commons/packet/util/IdSchemaUtils.java b/commons-packet/commons-packet-manager/src/main/java/io/mosip/commons/packet/util/IdSchemaUtils.java index 7f883ccc84..bfcfa33457 100644 --- a/commons-packet/commons-packet-manager/src/main/java/io/mosip/commons/packet/util/IdSchemaUtils.java +++ b/commons-packet/commons-packet-manager/src/main/java/io/mosip/commons/packet/util/IdSchemaUtils.java @@ -84,6 +84,7 @@ public class IdSchemaUtils { private ObjectMapper objMapper; @Autowired + @Qualifier("selfTokenRestTemplate") private RestTemplate restTemplate; diff --git a/commons-packet/commons-packet-manager/src/main/java/io/mosip/commons/packet/util/ZipUtils.java b/commons-packet/commons-packet-manager/src/main/java/io/mosip/commons/packet/util/ZipUtils.java index 92d17a78e4..c761452ca0 100644 --- a/commons-packet/commons-packet-manager/src/main/java/io/mosip/commons/packet/util/ZipUtils.java +++ b/commons-packet/commons-packet-manager/src/main/java/io/mosip/commons/packet/util/ZipUtils.java @@ -59,77 +59,4 @@ public static InputStream unzipAndGetFile(byte[] packet, String file) throws IOE return null; } - - /** - * Method to unzip the file in-memeory and search the required file exists - * - * @param packetStream zip file to be unzipped - * @param file file to search within zip file - * @return return true if found the required file, false otherwise - * @throws IOException if any error occored while unzipping the file - */ - public static boolean unzipAndCheckIsFileExist(InputStream packetStream, String file) throws IOException { - boolean isExist = false; - - try (ZipInputStream zis = new ZipInputStream(packetStream)) { - ZipEntry ze = zis.getNextEntry(); - while (ze != null) { - String fileName = ze.getName(); - String fileNameWithOutExt = FilenameUtils.removeExtension(fileName); - if (FilenameUtils.equals(fileNameWithOutExt, file, true, IOCase.INSENSITIVE)) { - isExist = true; - break; - } - zis.closeEntry(); - ze = zis.getNextEntry(); - } - zis.closeEntry(); - } finally { - packetStream.close(); - } - return isExist; - } - - /** - * Method to unzip the file for passed destination path - * - * @param input zip file to be unzipped - * @param desDir location where to unzip the files - * @throws IOException if any error occurred while unzipping - */ - public static void unZipFromInputStream(InputStream input, String desDir) throws IOException { - byte[] buffer = new byte[1024]; - try (ZipInputStream zis = new ZipInputStream(input)) { - File folder = FileUtils.getFile(desDir); - if (!folder.exists()) { - if (folder.mkdir()) { - - } - } - ZipEntry ze = zis.getNextEntry(); - while (ze != null) { - if (ze.isDirectory()) { - File file = FileUtils.getFile(desDir + ze.getName()); - if (file.mkdir()) { - - } - } else { - String fileName = ze.getName(); - File newFile = FileUtils.getFile(desDir + File.separator + fileName); - if (FileUtils.getFile(newFile.getParent()).mkdirs()) { - FileOutputStream fos = new FileOutputStream(newFile); - int len; - while ((len = zis.read(buffer)) > 0) { - fos.write(buffer, 0, len); - } - fos.close(); - } - } - ze = zis.getNextEntry(); - } - zis.closeEntry(); - } finally { - input.close(); - } - } } diff --git a/commons-packet/commons-packet-manager/src/test/java/io/mosip/commons/packet/test/impl/OfflinePacketCryptoServiceTest.java b/commons-packet/commons-packet-manager/src/test/java/io/mosip/commons/packet/test/impl/OfflinePacketCryptoServiceTest.java index 490516b5e1..5144138ef5 100644 --- a/commons-packet/commons-packet-manager/src/test/java/io/mosip/commons/packet/test/impl/OfflinePacketCryptoServiceTest.java +++ b/commons-packet/commons-packet-manager/src/test/java/io/mosip/commons/packet/test/impl/OfflinePacketCryptoServiceTest.java @@ -11,11 +11,10 @@ import io.mosip.kernel.core.util.JsonUtils; import io.mosip.kernel.cryptomanager.dto.CryptomanagerResponseDto; import io.mosip.kernel.cryptomanager.service.impl.CryptomanagerServiceImpl; -import io.mosip.kernel.signature.dto.ValidatorResponseDto; import io.mosip.kernel.signature.service.SignatureService; import io.mosip.kernel.signature.service.impl.SignatureServiceImpl; import org.apache.commons.io.IOUtils; -import org.apache.commons.lang.ArrayUtils; +import org.apache.commons.lang3.ArrayUtils; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; diff --git a/commons-packet/commons-packet-manager/src/test/java/io/mosip/commons/packet/test/impl/PacketWriterImplTest.java b/commons-packet/commons-packet-manager/src/test/java/io/mosip/commons/packet/test/impl/PacketWriterImplTest.java index 34903b9eff..46e2054021 100644 --- a/commons-packet/commons-packet-manager/src/test/java/io/mosip/commons/packet/test/impl/PacketWriterImplTest.java +++ b/commons-packet/commons-packet-manager/src/test/java/io/mosip/commons/packet/test/impl/PacketWriterImplTest.java @@ -20,7 +20,6 @@ import org.powermock.core.classloader.annotations.PowerMockIgnore; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; -import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.PropertySource; import org.springframework.test.util.ReflectionTestUtils; diff --git a/commons-packet/commons-packet-service/pom.xml b/commons-packet/commons-packet-service/pom.xml index d0bcc573b1..f261d8741d 100644 --- a/commons-packet/commons-packet-service/pom.xml +++ b/commons-packet/commons-packet-service/pom.xml @@ -46,7 +46,7 @@ **/dto/**,**/entity/**,**/config/** 1.2.0-SNAPSHOT 1.5.10 - + diff --git a/commons-packet/commons-packet-service/src/main/java/io/mosip/commons/packetmanager/controller/PacketReaderController.java b/commons-packet/commons-packet-service/src/main/java/io/mosip/commons/packetmanager/controller/PacketReaderController.java index f045971f35..9814123ca8 100644 --- a/commons-packet/commons-packet-service/src/main/java/io/mosip/commons/packetmanager/controller/PacketReaderController.java +++ b/commons-packet/commons-packet-service/src/main/java/io/mosip/commons/packetmanager/controller/PacketReaderController.java @@ -53,7 +53,6 @@ public class PacketReaderController { @Autowired private PacketReaderService packetReaderService; @PreAuthorize("hasAnyRole(@authorizedRoles.getPostsearchfield())") - //@PreAuthorize("hasAnyRole('DATA_READ')") @ResponseFilter @PostMapping(path = "/searchField", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) @Operation(summary = "searchField", description = "searchField", tags = { "packet-reader-controller" }) @@ -77,7 +76,6 @@ public ResponseWrapper searchField(@RequestBody(required = tru return response; } - //@PreAuthorize("hasAnyRole('DATA_READ')") @PreAuthorize("hasAnyRole(@authorizedRoles.getPostsearchfields())") @ResponseFilter @PostMapping(path = "/searchFields", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) @@ -107,7 +105,6 @@ public ResponseWrapper searchFields(@RequestBody(required = tr return response; } - //@PreAuthorize("hasAnyRole('DOCUMENT_READ')") @ResponseFilter @PreAuthorize("hasAnyRole(@authorizedRoles.getPostdocument())") @PostMapping(path = "/document", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) @@ -129,7 +126,6 @@ public ResponseWrapper getDocument(@RequestBody(required = true) Reque return response; } - //@PreAuthorize("hasAnyRole('BIOMETRIC_READ')") @ResponseFilter @PreAuthorize("hasAnyRole(@authorizedRoles.getPostbiometrics())") @PostMapping(path = "/biometrics", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) @@ -152,7 +148,6 @@ public ResponseWrapper getBiometrics(@RequestBody(required = tr return response; } - //@PreAuthorize("hasAnyRole('METADATA_READ')") @ResponseFilter @PreAuthorize("hasAnyRole(@authorizedRoles.getPostmetainfo())") @PostMapping(path = "/metaInfo", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) @@ -173,7 +168,6 @@ public ResponseWrapper getMetaInfo(@RequestBody(required = tru return response; } - //@PreAuthorize("hasAnyRole('REGISTRATION_PROCESSOR')") @ResponseFilter @PreAuthorize("hasAnyRole(@authorizedRoles.getPostaudits())") @PostMapping(path = "/audits", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) @@ -200,7 +194,6 @@ public ResponseWrapper> getAudits(@RequestBody(required = return response; } - //@PreAuthorize("hasAnyRole('REGISTRATION_PROCESSOR')") @ResponseFilter @PreAuthorize("hasAnyRole(@authorizedRoles.getPostvalidatepacket())") @PostMapping(path = "/validatePacket", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) @@ -219,7 +212,6 @@ public ResponseWrapper validatePacket(@RequestBody(requi return response; } - //@PreAuthorize("hasAnyRole('REGISTRATION_PROCESSOR')") @PreAuthorize("hasAnyRole(@authorizedRoles.getPostgettags())") @ResponseFilter @PostMapping(path = "/getTags", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) @@ -238,7 +230,6 @@ public ResponseWrapper getTags( return response; } - //@PreAuthorize("hasAnyRole('REGISTRATION_PROCESSOR')") @ResponseFilter @PreAuthorize("hasAnyRole(@authorizedRoles.getPostinfo())") @PostMapping(path = "/info", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) diff --git a/commons-packet/commons-packet-service/src/main/java/io/mosip/commons/packetmanager/controller/PacketWriterController.java b/commons-packet/commons-packet-service/src/main/java/io/mosip/commons/packetmanager/controller/PacketWriterController.java index 773e31d3bc..d9193d0099 100644 --- a/commons-packet/commons-packet-service/src/main/java/io/mosip/commons/packetmanager/controller/PacketWriterController.java +++ b/commons-packet/commons-packet-service/src/main/java/io/mosip/commons/packetmanager/controller/PacketWriterController.java @@ -41,7 +41,6 @@ public class PacketWriterController { @Autowired private PacketWriterService packetWriterService; @PreAuthorize("hasAnyRole(@authorizedRoles.getPutcreatepacket())") - //@PreAuthorize("hasAnyRole('REGISTRATION_PROCESSOR')") @ResponseFilter @PutMapping(path = "/createPacket", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) @Operation(summary = "createPacket", description = "createPacket", tags = { "packet-writer-controller" }) diff --git a/commons-packet/commons-packet-service/src/main/java/io/mosip/commons/packetmanager/dto/AuthorizedRolesDTO.java b/commons-packet/commons-packet-service/src/main/java/io/mosip/commons/packetmanager/dto/AuthorizedRolesDTO.java index e16892053c..9d8fdaa7b0 100644 --- a/commons-packet/commons-packet-service/src/main/java/io/mosip/commons/packetmanager/dto/AuthorizedRolesDTO.java +++ b/commons-packet/commons-packet-service/src/main/java/io/mosip/commons/packetmanager/dto/AuthorizedRolesDTO.java @@ -40,6 +40,8 @@ public class AuthorizedRolesDTO { private List postaddorupdatetag; private List postdeletetag; + + private List putcreatepacket; } diff --git a/commons-packet/commons-packet-service/src/main/java/io/mosip/commons/packetmanager/service/PacketReaderService.java b/commons-packet/commons-packet-service/src/main/java/io/mosip/commons/packetmanager/service/PacketReaderService.java index b7f65dd94e..8831c9c915 100644 --- a/commons-packet/commons-packet-service/src/main/java/io/mosip/commons/packetmanager/service/PacketReaderService.java +++ b/commons-packet/commons-packet-service/src/main/java/io/mosip/commons/packetmanager/service/PacketReaderService.java @@ -80,6 +80,7 @@ public class PacketReaderService { private PacketReader packetReader; @Autowired + @Qualifier("selfTokenRestTemplate") private RestTemplate restTemplate; @Autowired @@ -147,7 +148,17 @@ private InfoResponseDto infoInternal(String id) { return infoResponseDto; } catch (Exception e) { LOGGER.error(PacketManagerLogger.SESSIONID, PacketManagerLogger.REGISTRATIONID, id, ExceptionUtils.getStackTrace(e)); - throw new BaseUncheckedException(e.getMessage()); + + if (e instanceof BaseUncheckedException) { + BaseUncheckedException ex = (BaseUncheckedException) e; + throw ex; + } + else if (e instanceof BaseCheckedException) { + BaseCheckedException ex = (BaseCheckedException) e; + throw new BaseUncheckedException(ex.getErrorCode(), ex.getMessage(), ex); + } + else + throw new BaseUncheckedException(PacketUtilityErrorCodes.UNKNOWN_EXCEPTION.getErrorCode(), e.getMessage(), e); } } @@ -213,7 +224,7 @@ public ContainerInfoDto findPriority(String field, List info) } private ContainerInfoDto getContainerInfoByDefaultPriority(String field, List info) { - if (org.apache.commons.lang.StringUtils.isNotEmpty(defaultPriority)) { + if (StringUtils.isNotEmpty(defaultPriority)) { String[] val = defaultPriority.split(","); if (val != null && val.length > 0) { for (String value : val) { @@ -247,7 +258,7 @@ private ContainerInfoDto getContainerInfoBySourceAndProcess(String field, String } private String getDefaultSource(String process) { - if (org.apache.commons.lang.StringUtils.isNotEmpty(defaultPriority)) { + if (StringUtils.isNotEmpty(defaultPriority)) { String[] val = defaultPriority.split(","); if (val != null && val.length > 0) { for (String value : val) { diff --git a/commons-packet/commons-packet-service/src/main/resources/bootstrap.properties b/commons-packet/commons-packet-service/src/main/resources/bootstrap.properties index 89ac215c39..6d7e2f43a3 100644 --- a/commons-packet/commons-packet-service/src/main/resources/bootstrap.properties +++ b/commons-packet/commons-packet-service/src/main/resources/bootstrap.properties @@ -1,8 +1,8 @@ spring.cloud.config.uri=http://localhost:51000/config spring.cloud.config.label=master spring.profiles.active=mz -spring.application.name=packet-manager,application -spring.cloud.config.name=packet-manager,application +spring.application.name=application,packet-manager +spring.cloud.config.name=application,packet-manager server.port=8093 #disabling health check so that client doesnt try to load properties from sprint config server every # 5 minutes (should not be done in production) diff --git a/commons-packet/commons-packet-service/src/test/java/io/mosip/commons/packetmanager/test/controller/PacketManagerControllerTest.java b/commons-packet/commons-packet-service/src/test/java/io/mosip/commons/packetmanager/test/controller/PacketManagerControllerTest.java index 9ffa2a4187..8daf396a52 100644 --- a/commons-packet/commons-packet-service/src/test/java/io/mosip/commons/packetmanager/test/controller/PacketManagerControllerTest.java +++ b/commons-packet/commons-packet-service/src/test/java/io/mosip/commons/packetmanager/test/controller/PacketManagerControllerTest.java @@ -15,7 +15,6 @@ import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; import org.springframework.beans.factory.annotation.Autowired; diff --git a/commons-packet/commons-packet-service/src/test/java/io/mosip/commons/packetmanager/test/service/PacketReaderServiceTest.java b/commons-packet/commons-packet-service/src/test/java/io/mosip/commons/packetmanager/test/service/PacketReaderServiceTest.java index 85f0bb353b..e8967af9f5 100644 --- a/commons-packet/commons-packet-service/src/test/java/io/mosip/commons/packetmanager/test/service/PacketReaderServiceTest.java +++ b/commons-packet/commons-packet-service/src/test/java/io/mosip/commons/packetmanager/test/service/PacketReaderServiceTest.java @@ -16,6 +16,7 @@ import java.util.Map; import java.util.Set; +import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonMappingException; import org.assertj.core.util.Lists; import org.json.simple.JSONObject; @@ -124,7 +125,7 @@ public void testInfoSuccess() { @Test(expected = BaseUncheckedException.class) public void testException() throws IOException { - Mockito.when(objectMapper.readValue(anyString(), any(Class.class))).thenThrow(new JsonMappingException("IO Exception")); + Mockito.when(objectMapper.readValue(anyString(), any(Class.class))).thenThrow(new JsonMappingException("Mapping Exception")); packetReaderService.info(id); }