From 1e3b3512041edd2ffc07cd6407ef747ee982974c Mon Sep 17 00:00:00 2001 From: Aishwarya <42641684+aishwa8141@users.noreply.github.com> Date: Tue, 6 Oct 2020 13:41:23 +0530 Subject: [PATCH 01/39] SC-2049: upgrade Play Framework to 2.7.2 (#63) * SC-2049: upgrade Play Framework to 2.7.2 * SC-2049: Using Play-Guice module for initialising and injecting the AKKA actors * SC-2049: Using Play-Guice module for initialising and injecting the AKKA actors * SC-2049 : exclude netty dependency from cassandra-unit --- all-actors/pom.xml | 10 +- .../main/java/org/sunbird/Application.java | 102 ------------- .../sunbird/actor/CertBackgroundActor.java | 9 -- .../org/sunbird/actor/CertificationActor.java | 19 ++- .../org/sunbird/service/ICertService.java | 5 +- .../sunbird/serviceimpl/CertsServiceImpl.java | 19 +-- .../sunbird/utilities/CertificateUtil.java | 13 +- .../src/main/resources/application.conf | 129 ----------------- .../sunbird/actor/CertificationActorTest.java | 2 +- cassandra-utils/pom.xml | 12 ++ pom.xml | 8 +- sb-actor/pom.xml | 33 ----- .../org/sunbird/actor/core/ActorCache.java | 35 ----- .../org/sunbird/actor/core/ActorConfig.java | 23 --- .../org/sunbird/actor/core/ActorService.java | 114 --------------- sb-es-utils/pom.xml | 2 +- service/app/controllers/BaseController.java | 9 +- .../controllers/CertificateController.java | 41 ++++-- service/app/controllers/RequestHandler.java | 12 +- .../controllers/health/HealthController.java | 18 +-- service/app/utils/ApplicationStart.java | 82 +++++++++-- service/app/utils/module/ACTOR_NAMES.java | 26 ++++ .../app/utils/module/ActorStartModule.java | 24 +++ service/conf/application.conf | 137 +++++++++++++++--- service/conf/routes | 20 +-- service/pom.xml | 26 +++- .../test/controllers/BaseApplicationTest.java | 80 +++------- .../test/controllers/BaseControllerTest.java | 45 ------ .../CertificateControllerTest.java | 7 +- service/test/controllers/TestHelper.java | 13 +- .../health/HealthControllerTest.java | 42 ++---- 31 files changed, 405 insertions(+), 712 deletions(-) delete mode 100644 all-actors/src/main/java/org/sunbird/Application.java delete mode 100644 all-actors/src/main/resources/application.conf delete mode 100644 sb-actor/pom.xml delete mode 100644 sb-actor/src/main/java/org/sunbird/actor/core/ActorCache.java delete mode 100644 sb-actor/src/main/java/org/sunbird/actor/core/ActorConfig.java delete mode 100644 sb-actor/src/main/java/org/sunbird/actor/core/ActorService.java create mode 100644 service/app/utils/module/ACTOR_NAMES.java create mode 100644 service/app/utils/module/ActorStartModule.java delete mode 100644 service/test/controllers/BaseControllerTest.java diff --git a/all-actors/pom.xml b/all-actors/pom.xml index 14a7b80..d7b41f9 100644 --- a/all-actors/pom.xml +++ b/all-actors/pom.xml @@ -11,9 +11,9 @@ 1.0.0 - org.sunbird - sb-actor - 1.0.0 + com.typesafe.akka + akka-actor_${scala.major.version} + ${akka.x.version} org.sunbird @@ -44,8 +44,8 @@ com.typesafe.akka - akka-testkit_2.12 - 2.6.0-M5 + akka-testkit_${scala.major.version} + 2.5.22 test diff --git a/all-actors/src/main/java/org/sunbird/Application.java b/all-actors/src/main/java/org/sunbird/Application.java deleted file mode 100644 index 593a298..0000000 --- a/all-actors/src/main/java/org/sunbird/Application.java +++ /dev/null @@ -1,102 +0,0 @@ -package org.sunbird; - -import akka.actor.ActorRef; -import org.apache.commons.lang3.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sunbird.actor.core.ActorCache; -import org.sunbird.actor.core.ActorService; -import org.sunbird.helper.CassandraConnectionManager; -import org.sunbird.helper.CassandraConnectionMngrFactory; -import org.sunbird.message.IResponseMessage; -import org.sunbird.message.Localizer; -import org.sunbird.message.ResponseCode; - -import java.util.ArrayList; -import java.util.List; -import java.util.Locale; - -/** - * this class is used to instantiate the actor system and open saber. - * @author Amit Kumar - */ -public class Application { - private final static String actorSystemName = "certActorSystem"; - private static Application instance = new Application(); - private static Localizer localizer = Localizer.getInstance(); - private static Logger logger = LoggerFactory.getLogger(Application.class); - - - // private constructor restricted to this class itself - private Application() { - } - - // static method to create instance of ActorService class - public static Application getInstance() { - return instance; - } - - // instantiate actor system and actors - public void init() throws BaseException { - List actorClassPaths = new ArrayList<>(); - actorClassPaths.add("org.sunbird"); - ActorService.getInstance().init(actorSystemName, actorClassPaths); - createCassandraConnection(JsonKeys.SUNBIRD); - } - - - /** - * this method is used to get the reference of actor from in memory cache. - * @param operation - * @return - */ - public ActorRef getActorRef(String operation) { - return ActorCache.getActorRef(operation); - } - - /** - * This method will read the configuration from System variable. - * - * @return boolean - */ - public static boolean createCassandraConnection(String keyspace) throws BaseException { - boolean response = false; - String ips = System.getenv(JsonKeys.SUNBIRD_CASSANDRA_IP); - String envPort = System.getenv(JsonKeys.SUNBIRD_CASSANDRA_PORT); - CassandraConnectionManager cassandraConnectionManager = - CassandraConnectionMngrFactory.getObject(JsonKeys.STANDALONE_MODE); - - if (StringUtils.isBlank(ips) || StringUtils.isBlank(envPort)) { - logger.info("Configuration value is not coming form System variable."); - return false; - } - String[] portList = envPort.split(","); - String userName = System.getenv(JsonKeys.SUNBIRD_CASSANDRA_USER_NAME); - String password = System.getenv(JsonKeys.SUNBIRD_CASSANDRA_PASSWORD); - try { - boolean result = - cassandraConnectionManager.createConnection(ips, portList[0], userName, password, keyspace); - if (result) { - response = true; - logger.info( - "CONNECTION CREATED SUCCESSFULLY FOR IP's: " + ips + " : KEYSPACE :" + keyspace); - } else { - logger.info( - "CONNECTION CREATION FAILED FOR IP: " + ips + " : KEYSPACE :" + keyspace); - } - } catch (BaseException ex) { - logger.error("Application:createCassandraConnection: Exception occurred with message = " + ex.getMessage()); - } - if (!response) { - throw new BaseException( - IResponseMessage.INVALID_CONFIGURATION, - getLocalizedMessage(IResponseMessage.INVALID_CONFIGURATION,null), - ResponseCode.SERVER_ERROR.hashCode()); - } - return response; - } - - private static String getLocalizedMessage(String key, Locale locale){ - return localizer.getMessage(key, locale); - } -} diff --git a/all-actors/src/main/java/org/sunbird/actor/CertBackgroundActor.java b/all-actors/src/main/java/org/sunbird/actor/CertBackgroundActor.java index 6ff4335..88f44a2 100644 --- a/all-actors/src/main/java/org/sunbird/actor/CertBackgroundActor.java +++ b/all-actors/src/main/java/org/sunbird/actor/CertBackgroundActor.java @@ -2,24 +2,15 @@ import org.sunbird.BaseActor; import org.sunbird.BaseException; -import org.sunbird.BaseLogger; import org.sunbird.JsonKeys; -import org.sunbird.actor.core.ActorConfig; import org.sunbird.cassandra.CassandraOperation; import org.sunbird.common.ElasticSearchHelper; import org.sunbird.common.factory.EsClientFactory; import org.sunbird.common.inf.ElasticSearchService; import org.sunbird.helper.ServiceFactory; import org.sunbird.request.Request; - -import java.util.HashMap; import java.util.Map; -@ActorConfig( - tasks = {"add_cert_es","delete_cert_cassandra"}, - dispatcher = "", - asyncTasks = {} -) public class CertBackgroundActor extends BaseActor { private ElasticSearchService elasticSearchService = getESService(); private CassandraOperation cassandraOperation = getCassandraOperation(); diff --git a/all-actors/src/main/java/org/sunbird/actor/CertificationActor.java b/all-actors/src/main/java/org/sunbird/actor/CertificationActor.java index a541a70..536e9c6 100644 --- a/all-actors/src/main/java/org/sunbird/actor/CertificationActor.java +++ b/all-actors/src/main/java/org/sunbird/actor/CertificationActor.java @@ -1,26 +1,25 @@ package org.sunbird.actor; +import akka.actor.ActorRef; import org.sunbird.BaseActor; import org.sunbird.BaseException; -import org.sunbird.BaseLogger; import org.sunbird.JsonKeys; -import org.sunbird.actor.core.ActorConfig; import org.sunbird.request.Request; import org.sunbird.response.Response; import org.sunbird.service.ICertService; import org.sunbird.serviceimpl.CertsServiceImpl; -import java.util.HashMap; -import java.util.Map; +import javax.inject.Inject; +import javax.inject.Named; -@ActorConfig( - tasks = {"add","validate","download","generate","verify","search","read", "addV2", "downloadV2"}, - dispatcher = "", - asyncTasks = {} -) public class CertificationActor extends BaseActor { private ICertService certService = getCertServiceImpl(); + @Inject + @Named("certificate_background_actor") + private ActorRef certBackgroundActorRef; + + private ICertService getCertServiceImpl(){ return new CertsServiceImpl(); } @@ -63,7 +62,7 @@ public void onReceive(Request request) throws BaseException { } private void add(Request request) throws BaseException { - String id = certService.add(request); + String id = certService.add(request, certBackgroundActorRef); Response response = new Response(); response.put(JsonKeys.ID, id); sender().tell(response, self()); diff --git a/all-actors/src/main/java/org/sunbird/service/ICertService.java b/all-actors/src/main/java/org/sunbird/service/ICertService.java index 6b0abd6..f39a9aa 100644 --- a/all-actors/src/main/java/org/sunbird/service/ICertService.java +++ b/all-actors/src/main/java/org/sunbird/service/ICertService.java @@ -1,6 +1,7 @@ package org.sunbird.service; +import akka.actor.ActorRef; import org.sunbird.BaseException; import org.sunbird.request.Request; import org.sunbird.response.Response; @@ -12,9 +13,9 @@ */ public interface ICertService{ - Response delete(Request request) throws BaseException; + Response delete(Request request, ActorRef certBackgroundActorRef) throws BaseException; - String add(Request request) throws BaseException; + String add(Request request, ActorRef certBackgroundActorRef) throws BaseException; Response validate(Request request) throws BaseException; diff --git a/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java b/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java index 8e20b17..094b605 100644 --- a/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java +++ b/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java @@ -1,5 +1,6 @@ package org.sunbird.serviceimpl; +import akka.actor.ActorRef; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.mashape.unirest.http.HttpResponse; @@ -50,11 +51,11 @@ public class CertsServiceImpl implements ICertService { } @Override - public Response delete(Request request) throws BaseException { + public Response delete(Request request, ActorRef certBackgroundActorRef) throws BaseException { Map certAddReqMap = request.getRequest(); Response response = new Response(); if(StringUtils.isNotBlank((String)certAddReqMap.get(JsonKeys.OLD_ID))){ - boolean bool = CertificateUtil.deleteRecord((String)certAddReqMap.get(JsonKeys.OLD_ID)); + boolean bool = CertificateUtil.deleteRecord((String)certAddReqMap.get(JsonKeys.OLD_ID), certBackgroundActorRef); response.getResult().put(JsonKeys.RESPONSE,bool); logger.info("CertsServiceImpl:delete Deleted the record from cert_registry table for id "+certAddReqMap.get(JsonKeys.OLD_ID)); } @@ -62,21 +63,21 @@ public Response delete(Request request) throws BaseException { } @Override - public String add(Request request) throws BaseException { + public String add(Request request, ActorRef certBackgroundActorRef) throws BaseException { Map reqMap = request.getRequest(); if(isPresentRecipientIdAndCertId(request)){ validateCertAndRecipientId(reqMap); - deleteOldCertificate((String) reqMap.get(JsonKeys.OLD_ID)); + deleteOldCertificate((String) reqMap.get(JsonKeys.OLD_ID),certBackgroundActorRef); } Map certAddReqMap = request.getRequest(); assureUniqueCertId((String) certAddReqMap.get(JsonKeys.ID)); - processRecord(certAddReqMap,(String) request.getContext().get(JsonKeys.VERSION)); + processRecord(certAddReqMap,(String) request.getContext().get(JsonKeys.VERSION), certBackgroundActorRef); logger.info("CertsServiceImpl:add:record successfully processed with request:"+certAddReqMap.get(JsonKeys.ID)); return (String)certAddReqMap.get(JsonKeys.ID); } - private void deleteOldCertificate(String oldCertId) throws BaseException { - CertificateUtil.deleteRecord(oldCertId); + private void deleteOldCertificate(String oldCertId, ActorRef certBackgroundActorRef) throws BaseException { + CertificateUtil.deleteRecord(oldCertId, certBackgroundActorRef); } private void validateCertAndRecipientId(Map reqMap) throws BaseException { @@ -117,13 +118,13 @@ private void assureUniqueCertId(String certificatedId) throws BaseException { } - private Response processRecord(Map certReqAddMap, String version) throws BaseException { + private Response processRecord(Map certReqAddMap, String version, ActorRef certBackgroundActorRef) throws BaseException { Certificate certificate=getCertificate(certReqAddMap); if(version.equalsIgnoreCase(JsonKeys.VERSION_1)) { certificate.setPdfUrl((String)certReqAddMap.get(JsonKeys.PDF_URL)); } MaprecordMap= requestMapper.convertValue(certificate,Map.class); - return CertificateUtil.insertRecord(recordMap); + return CertificateUtil.insertRecord(recordMap, certBackgroundActorRef); } private Certificate getCertificate(Map certReqAddMap) { Certificate certificate = new Certificate.Builder() diff --git a/all-actors/src/main/java/org/sunbird/utilities/CertificateUtil.java b/all-actors/src/main/java/org/sunbird/utilities/CertificateUtil.java index 0d97959..d88d292 100644 --- a/all-actors/src/main/java/org/sunbird/utilities/CertificateUtil.java +++ b/all-actors/src/main/java/org/sunbird/utilities/CertificateUtil.java @@ -10,7 +10,6 @@ import org.slf4j.LoggerFactory; import org.slf4j.MDC; import org.sunbird.ActorOperations; -import org.sunbird.Application; import org.sunbird.BaseException; import org.sunbird.JsonKeys; import org.sunbird.cassandra.CassandraOperation; @@ -26,7 +25,9 @@ import org.sunbird.response.Response; import java.sql.Timestamp; -import java.util.*; +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; import java.util.concurrent.Future; @@ -60,7 +61,7 @@ public static Response getCertRecordByID(String id) throws BaseException { return cassandraOperation.getRecordById(JsonKeys.SUNBIRD,JsonKeys.CERT_REGISTRY,id); } - public static Boolean deleteRecord(String id) throws BaseException { + public static Boolean deleteRecord(String id, ActorRef certBackgroundActorRef) throws BaseException { Boolean bool = (Boolean)ElasticSearchHelper.getResponseFromFuture(elasticSearchService.delete(JsonKeys.CERT_ALIAS,id)); logger.info("Data deleted from ES for id "+id); //Delete the data from cassandra @@ -70,11 +71,11 @@ public static Boolean deleteRecord(String id) throws BaseException { RequestParams params = new RequestParams(); params.setMsgid(MDC.get(JsonKeys.REQUEST_MESSAGE_ID)); req.setParams(params); - Application.getInstance().getActorRef(ActorOperations.DELETE_CERT_CASSANDRA.getOperation()).tell(req, ActorRef.noSender()); + certBackgroundActorRef.tell(req, ActorRef.noSender()); return bool; } - public static Response insertRecord(MapcertAddReqMap) throws BaseException { + public static Response insertRecord(MapcertAddReqMap, ActorRef certBackgroundActorRef) throws BaseException { MapcertMap = new HashMap<>(); long createdAt = System.currentTimeMillis(); certAddReqMap.put(JsonKeys.CREATED_AT,createdAt); @@ -106,7 +107,7 @@ public static Response insertRecord(MapcertAddReqMap) throws Base req.setParams(params); req.setOperation(ActorOperations.ADD_CERT_ES.getOperation()); req.getRequest().put(JsonKeys.REQUEST,certAddReqMap); - Application.getInstance().getActorRef(ActorOperations.ADD_CERT_ES.getOperation()).tell(req, ActorRef.noSender()); + certBackgroundActorRef.tell(req, ActorRef.noSender()); return response; } diff --git a/all-actors/src/main/resources/application.conf b/all-actors/src/main/resources/application.conf deleted file mode 100644 index 3b6fcd0..0000000 --- a/all-actors/src/main/resources/application.conf +++ /dev/null @@ -1,129 +0,0 @@ -certActorSystem { - default-dispatcher { - type = "Dispatcher" - executor = "fork-join-executor" - fork-join-executor { - parallelism-min = 8 - parallelism-factor = 32.0 - parallelism-max = 64 - } - # Throughput for default Dispatcher, set to 1 for as fair as possible - throughput = 1 - } - router-dispatcher { - type = "Dispatcher" - executor = "fork-join-executor" - fork-join-executor { - parallelism-min = 8 - parallelism-factor = 32.0 - parallelism-max = 64 - } - # Throughput for default Dispatcher, set to 1 for as fair as possible - throughput = 1 - } - supervisor-dispatcher { - type = "Dispatcher" - executor = "fork-join-executor" - fork-join-executor { - parallelism-min = 8 - parallelism-factor = 32.0 - parallelism-max = 64 - } - # Throughput for default Dispatcher, set to 1 for as fair as possible - throughput = 1 - } - user-dispatcher { - type = "Dispatcher" - executor = "fork-join-executor" - fork-join-executor { - parallelism-min = 8 - parallelism-factor = 32.0 - parallelism-max = 64 - } - # Throughput for default Dispatcher, set to 1 for as fair as possible - throughput = 1 - } - search-read-dispatcher { - type = "Dispatcher" - executor = "fork-join-executor" - fork-join-executor { - parallelism-min = 12 - parallelism-factor = 32.0 - parallelism-max = 64 - } - # Throughput for default Dispatcher, set to 1 for as fair as possible - throughput = 1 - } - org-dispatcher { - type = "Dispatcher" - executor = "fork-join-executor" - fork-join-executor { - parallelism-min = 8 - parallelism-factor = 32.0 - parallelism-max = 64 - } - # Throughput for default Dispatcher, set to 1 for as fair as possible - throughput = 1 - } - - akka { - - loggers = ["akka.event.slf4j.Slf4jLogger"] - loglevel = "INFO" - stdout-loglevel = "DEBUG" - logging-filter = "akka.event.slf4j.Slf4jLoggingFilter" - - - actor { - akka.actor.allow-java-serialization = off - debug { - # enable DEBUG logging of all AutoReceiveMessages (Kill, PoisonPill etc.) - autoreceive = on - # enable DEBUG logging of actor lifecycle changes - lifecycle = on - # enable DEBUG logging of unhandled messages - unhandled = on - # enable DEBUG logging of all LoggingFSMs for events, transitions and timers - fsm = on - # enable DEBUG logging of subscription changes on the eventStream - event-stream = on - } - - default-dispatcher { - type = "Dispatcher" - executor = "fork-join-executor" - fork-join-executor { - parallelism-min = 8 - parallelism-factor = 32.0 - parallelism-max = 64 - } - # Throughput for default Dispatcher, set to 1 for as fair as possible - throughput = 1 - } - deployment { - /CertificationActor - { - router = smallest-mailbox-pool - nr-of-instances = 5 - dispatcher = user-dispatcher - } - /CertBackgroundActor - { - router = smallest-mailbox-pool - nr-of-instances = 5 - dispatcher = user-dispatcher - } - } - } - remote { - maximum-payload-bytes = 30000000 bytes - netty.tcp { - port = 8088 - message-frame-size = 30000000b - send-buffer-size = 30000000b - receive-buffer-size = 30000000b - maximum-frame-size = 30000000b - } - } - } -} diff --git a/all-actors/src/test/java/org/sunbird/actor/CertificationActorTest.java b/all-actors/src/test/java/org/sunbird/actor/CertificationActorTest.java index c78a060..4f3c741 100644 --- a/all-actors/src/test/java/org/sunbird/actor/CertificationActorTest.java +++ b/all-actors/src/test/java/org/sunbird/actor/CertificationActorTest.java @@ -90,7 +90,7 @@ public void beforeTestSetUp() throws Exception { when(CertificateUtil.isIdPresent(Mockito.anyString())).thenReturn(false); CertsServiceImpl certsService = PowerMockito.mock(CertsServiceImpl.class); PowerMockito.whenNew(CertsServiceImpl.class).withNoArguments().thenReturn(certsService); - when(certsService.add(Mockito.any(Request.class))).thenReturn("id"); + when(certsService.add(Mockito.any(Request.class), Mockito.any(ActorRef.class))).thenReturn("id"); when(certsService.validate(Mockito.any(Request.class))).thenReturn(getValidateCertResponse()); Map map = new HashMap<>(); diff --git a/cassandra-utils/pom.xml b/cassandra-utils/pom.xml index f0193f4..5180a06 100644 --- a/cassandra-utils/pom.xml +++ b/cassandra-utils/pom.xml @@ -20,6 +20,12 @@ org.cassandraunit cassandra-unit 3.11.2.0 + + + io.netty + * + + com.datastax.cassandra @@ -39,6 +45,12 @@ com.datastax.cassandra cassandra-driver-mapping ${cassandra.driver.version} + + + com.datastax.cassandra + cassandra-driver-core + + com.fasterxml.jackson.core diff --git a/pom.xml b/pom.xml index 12fd31d..bd532ef 100644 --- a/pom.xml +++ b/pom.xml @@ -15,7 +15,7 @@ 1.8 1.8 2.9.10 - 2.6.0-M2 + 2.5.22 4.12 2.3.1 @@ -24,8 +24,9 @@ 1.0.7 UTF-8 - 2.6.5 - 2.12.8 + 2.7.2 + 2.11.8 + 2.11 1.6.5 4.5.1 @@ -35,7 +36,6 @@ cassandra-utils sb-es-utils all-actors - sb-actor service diff --git a/sb-actor/pom.xml b/sb-actor/pom.xml deleted file mode 100644 index 0502b37..0000000 --- a/sb-actor/pom.xml +++ /dev/null @@ -1,33 +0,0 @@ - - - - certification-service - org.sunbird - 1.2.0 - - 4.0.0 - - sb-actor - 1.0.0 - - - com.typesafe.akka - akka-actor_2.12 - ${akka.x.version} - - - org.reflections - reflections - 0.9.10 - - - org.apache.commons - commons-lang3 - 3.9 - - - - - diff --git a/sb-actor/src/main/java/org/sunbird/actor/core/ActorCache.java b/sb-actor/src/main/java/org/sunbird/actor/core/ActorCache.java deleted file mode 100644 index 7b733e9..0000000 --- a/sb-actor/src/main/java/org/sunbird/actor/core/ActorCache.java +++ /dev/null @@ -1,35 +0,0 @@ -package org.sunbird.actor.core; - -import akka.actor.ActorRef; - -import java.util.HashMap; -import java.util.Map; - -/** - * This class will maintain the cache of actor reference. - * NOTE:: Today does not handle remote actor references - * @author Amit Kumar - */ -public class ActorCache { - - private ActorCache(){} - - private static Map actorRefCache = new HashMap<>(); - - /** - * This method will return the map of actor operation and actor reference. - * @return Map of string and actor reference - */ - public static Map getActorCache() { - return actorRefCache; - } - - /** - * This method will return the actor reference based on actor operation - * @param actorOperation operation performed by actor - * @return ActorRef actor reference - */ - public static ActorRef getActorRef(String actorOperation){ - return actorRefCache.get(actorOperation); - } -} diff --git a/sb-actor/src/main/java/org/sunbird/actor/core/ActorConfig.java b/sb-actor/src/main/java/org/sunbird/actor/core/ActorConfig.java deleted file mode 100644 index 5ec3f15..0000000 --- a/sb-actor/src/main/java/org/sunbird/actor/core/ActorConfig.java +++ /dev/null @@ -1,23 +0,0 @@ -package org.sunbird.actor.core; - - -import java.lang.annotation.ElementType; -import java.lang.annotation.Inherited; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * @author Amit Kumar - */ - -@Target(ElementType.TYPE) -@Retention(RetentionPolicy.RUNTIME) -@Inherited -public @interface ActorConfig { - String[] tasks(); - - String[] asyncTasks(); - - String dispatcher() default ""; -} diff --git a/sb-actor/src/main/java/org/sunbird/actor/core/ActorService.java b/sb-actor/src/main/java/org/sunbird/actor/core/ActorService.java deleted file mode 100644 index cb9e1b3..0000000 --- a/sb-actor/src/main/java/org/sunbird/actor/core/ActorService.java +++ /dev/null @@ -1,114 +0,0 @@ -package org.sunbird.actor.core; - -import akka.actor.ActorRef; -import akka.actor.ActorSystem; -import akka.actor.Props; -import akka.routing.FromConfig; -import com.typesafe.config.Config; -import com.typesafe.config.ConfigFactory; -import org.apache.commons.lang3.StringUtils; -import org.reflections.Reflections; - -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; - -/** - * This class contains method to instantiate actor system and actors. - * @author Amit Kumar - */ -public class ActorService { - - private static ActorSystem system; - private static Config config = - ConfigFactory.systemEnvironment().withFallback(ConfigFactory.load()); - private static Map actorRefCache = ActorCache.getActorCache(); - - // static variable instance of type ActorService - private static ActorService instance = null; - - // private constructor restricted to this class itself - private ActorService() { } - - // static method to create instance of ActorService class - public static ActorService getInstance() - { - if (instance == null) - instance = new ActorService(); - - return instance; - } - - // instantiate actor system and actors - public void init(String actorSystemName, List actorsClassPathList) { - - getActorSystem(actorSystemName); - initActors(actorsClassPathList); - - } - - /** - * This method will instantiate actor system - * @return actor system - */ - private ActorSystem getActorSystem(String actorSystemName) { - - if (null == system) { - Config conf = config.getConfig(actorSystemName); - system = ActorSystem.create(actorSystemName, conf); - } - - return system; - - } - - /** - * initialize the actors - */ - private void initActors(List actorsClassPathList) { - Set> actors = getActors(actorsClassPathList); - for (Class actor : actors) { - ActorConfig routerDetails = actor.getAnnotation(ActorConfig.class); - if (null != routerDetails) { - String[] operations = routerDetails.tasks(); - String dispatcher = (StringUtils.isNotBlank(routerDetails.dispatcher())) ? routerDetails.dispatcher() : "default-dispatcher"; - createActor(actor,operations, dispatcher); - } - } - } - - - private Set> getActors(List actorsClassPathList) { - synchronized (ActorService.class) { - Reflections reflections = null; - Set> actors = new HashSet<>(); - for(String classpath : actorsClassPathList){ - reflections = new Reflections(classpath); - actors.addAll(reflections.getTypesAnnotatedWith(ActorConfig.class)); - } - return actors; - } - } - - - private void createActor(Class actor, - String[] operations, - String dispatcher) { - - if (null != operations && operations.length > 0) { - Props props; - if (StringUtils.isNotBlank(dispatcher)) { - props = Props.create(actor).withDispatcher(dispatcher); - } else { - props = Props.create(actor); - } - ActorRef actorRef = - system.actorOf(FromConfig.getInstance().props(props), actor.getSimpleName()); - for (String operation : operations) { - actorRefCache.put(operation, actorRef); - } - } - } - -} diff --git a/sb-es-utils/pom.xml b/sb-es-utils/pom.xml index 1a21a81..d02a911 100755 --- a/sb-es-utils/pom.xml +++ b/sb-es-utils/pom.xml @@ -53,7 +53,7 @@ com.typesafe.akka - akka-actor_2.12 + akka-actor_${scala.major.version} ${akka.x.version} compile diff --git a/service/app/controllers/BaseController.java b/service/app/controllers/BaseController.java index 6baceb7..77f8305 100644 --- a/service/app/controllers/BaseController.java +++ b/service/app/controllers/BaseController.java @@ -10,7 +10,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.sunbird.Application; import org.sunbird.BaseException; import org.sunbird.message.IResponseMessage; import org.sunbird.message.Localizer; @@ -83,10 +82,6 @@ public void endTrace(String tag) { logger.info("Method call ended."); } - protected ActorRef getActorRef(String operation) throws BaseException { - return Application.getInstance().getActorRef(operation); - } - /** * this method will take play.mv.http request and a validation function and * lastly operation(Actor operation) this method is validating the request and , @@ -99,7 +94,7 @@ protected ActorRef getActorRef(String operation) throws BaseException { * @param operation * @return */ - public CompletionStage handleRequest(play.mvc.Http.Request req, RequestValidatorFunction validatorFunction, + public CompletionStage handleRequest(ActorRef actorRef, play.mvc.Http.Request req, RequestValidatorFunction validatorFunction, String operation) { try { Request request = new Request(); @@ -109,7 +104,7 @@ public CompletionStage handleRequest(play.mvc.Http.Request req, RequestV if (validatorFunction != null) { validatorFunction.apply(request); } - return new RequestHandler().handleRequest(request, operation,req); + return new RequestHandler().handleRequest(request, actorRef, operation, req); } catch (Exception ex) { return CompletableFuture.supplyAsync(() -> { return RequestHandler.handleFailureResponse(ex,req); diff --git a/service/app/controllers/CertificateController.java b/service/app/controllers/CertificateController.java index 22b172e..d644d76 100644 --- a/service/app/controllers/CertificateController.java +++ b/service/app/controllers/CertificateController.java @@ -1,10 +1,14 @@ package controllers; +import akka.actor.ActorRef; import org.sunbird.JsonKeys; import org.sunbird.request.Request; +import play.mvc.Http; import play.mvc.Result; import validators.*; +import javax.inject.Inject; +import javax.inject.Named; import java.util.HashMap; import java.util.Map; import java.util.concurrent.CompletionStage; @@ -15,14 +19,19 @@ */ public class CertificateController extends BaseController { + + @Inject + @Named("certification_actor") + private ActorRef certificationActorRef; + /** * this action method will be called for adding certificate * @return CompletionStage of Result */ - public CompletionStage add() + public CompletionStage add(Http.Request httpRequest) { IRequestValidator requestValidator=new CertAddRequestValidator(); - return handleRequest(request(), + return handleRequest(certificationActorRef, httpRequest, request -> { Request req = (Request) request; Map context = new HashMap<>(); @@ -37,10 +46,10 @@ public CompletionStage add() * this action method will be called for adding certificate * @return CompletionStage of Result */ - public CompletionStage addV2() + public CompletionStage addV2(Http.Request httpRequest) { IRequestValidator requestValidator=new CertAddRequestValidator(); - return handleRequest(request(), + return handleRequest(certificationActorRef, httpRequest, request -> { Request req = (Request) request; Map context = new HashMap<>(); @@ -55,10 +64,10 @@ public CompletionStage addV2() * this action method will be called for verifying certificate * @return CompletionStage of Result */ - public CompletionStage validate() + public CompletionStage validate(Http.Request httpRequest) { IRequestValidator requestValidator=new CertValidateRequestValidator(); - return handleRequest(request(), + return handleRequest(certificationActorRef, httpRequest, request -> { Request req = (Request) request; requestValidator.validate(req); @@ -70,10 +79,10 @@ public CompletionStage validate() * this action method will be called for downloading certificate * @return CompletionStage of Result */ - public CompletionStage download() + public CompletionStage download(Http.Request httpRequest) { IRequestValidator requestValidator=new CertDownloadRequestValidator(); - return handleRequest(request(), + return handleRequest(certificationActorRef, httpRequest, request -> { Request req = (Request) request; requestValidator.validate(req); @@ -87,9 +96,9 @@ public CompletionStage download() * * @return CompletionStage of Result */ - public CompletionStage downloadV2(String id) { + public CompletionStage downloadV2(String id, Http.Request httpRequest) { IRequestValidator requestValidator = new CertDownloadV2RequestValidator(); - return handleRequest(request(), + return handleRequest(certificationActorRef, httpRequest, request -> { Request req = (Request) request; req.getRequest().put(JsonKeys.ID, id); @@ -102,10 +111,10 @@ public CompletionStage downloadV2(String id) { * this action method will be called for verify certificate * @return CompletionStage of Result */ - public CompletionStage verify() + public CompletionStage verify(Http.Request httpRequest) { IRequestValidator requestValidator=new CertVerifyRequestValidator(); - return handleRequest(request(), + return handleRequest(certificationActorRef, httpRequest, request -> { Request req = (Request) request; requestValidator.validate(req); @@ -117,9 +126,9 @@ public CompletionStage verify() * this action method will be called for reading certificate with id * @return CompletionStage of Result */ - public CompletionStage read(String id) { + public CompletionStage read(String id, Http.Request httpRequest) { IRequestValidator requestValidator = new CertReadRequestValidator(); - return handleRequest(request(), + return handleRequest(certificationActorRef, httpRequest, request -> { Request req = (Request) request; req.getRequest().put(JsonKeys.ID, id); @@ -131,10 +140,10 @@ public CompletionStage read(String id) { } - public CompletionStage search() + public CompletionStage search(Http.Request httpRequest) { IRequestValidator requestValidator=new CertSearchRequestValidator(); - return handleRequest(request(), + return handleRequest(certificationActorRef, httpRequest, request -> { Request req = (Request) request; requestValidator.validate(req); diff --git a/service/app/controllers/RequestHandler.java b/service/app/controllers/RequestHandler.java index 0ace81d..f8ba711 100644 --- a/service/app/controllers/RequestHandler.java +++ b/service/app/controllers/RequestHandler.java @@ -1,5 +1,7 @@ package controllers; +import akka.actor.ActorRef; +import akka.actor.ActorSelection; import akka.pattern.Patterns; import akka.util.Timeout; import java.util.concurrent.CompletionStage; @@ -7,7 +9,6 @@ import java.util.function.Function; import org.apache.commons.lang3.StringUtils; import org.sunbird.BaseException; -import org.sunbird.JsonKeys; import org.sunbird.message.IResponseMessage; import org.sunbird.message.ResponseCode; import org.sunbird.request.Request; @@ -35,11 +36,16 @@ public class RequestHandler extends BaseController { * @return CompletionStage * @throws Exception */ - public CompletionStage handleRequest(Request request, String operation, play.mvc.Http.Request req) throws Exception { + public CompletionStage handleRequest(Request request, Object actorRef, String operation, play.mvc.Http.Request req) throws Exception { request.setOperation(operation); Function fn = object -> handleResponse(object, req); + Future future; Timeout t = new Timeout(Long.valueOf(request.getTimeout()), TimeUnit.SECONDS); - Future future = Patterns.ask(getActorRef(operation), request, t); + if (actorRef instanceof ActorRef) { + future = Patterns.ask((ActorRef) actorRef, request, t); + } else { + future = Patterns.ask((ActorSelection) actorRef, request, t); + } return FutureConverters.toJava(future).thenApplyAsync(fn); } diff --git a/service/app/controllers/health/HealthController.java b/service/app/controllers/health/HealthController.java index 8d14dd2..dc818f9 100644 --- a/service/app/controllers/health/HealthController.java +++ b/service/app/controllers/health/HealthController.java @@ -1,17 +1,9 @@ package controllers.health; import controllers.BaseController; -import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionStage; - -import org.sunbird.BaseException; -import org.sunbird.message.IResponseMessage; -import org.sunbird.message.ResponseCode; +import play.mvc.Http; import play.mvc.Result; -import play.mvc.Results; -import utils.module.SignalHandler; - -import javax.inject.Inject; /** * This controller class will responsible to check health of the services. @@ -25,8 +17,8 @@ public class HealthController extends BaseController { * * @return a CompletableFuture of success response */ - public CompletionStage getHealth() { - CompletionStage response = handleRequest(request()); + public CompletionStage getHealth(Http.Request httpRequest) { + CompletionStage response = handleRequest(httpRequest); return response; } /** @@ -34,8 +26,8 @@ public CompletionStage getHealth() { * * @return a CompletableFuture of success response */ - public CompletionStage getServiceHealth(String service) { - CompletionStage response = handleRequest(request()); + public CompletionStage getServiceHealth(String service, Http.Request httpRequest) { + CompletionStage response = handleRequest(httpRequest); return response; } diff --git a/service/app/utils/ApplicationStart.java b/service/app/utils/ApplicationStart.java index 1296dca..322d752 100644 --- a/service/app/utils/ApplicationStart.java +++ b/service/app/utils/ApplicationStart.java @@ -1,14 +1,23 @@ package utils; -import org.sunbird.Application; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.sunbird.BaseException; +import org.sunbird.JsonKeys; +import org.sunbird.helper.CassandraConnectionManager; +import org.sunbird.helper.CassandraConnectionMngrFactory; +import org.sunbird.message.IResponseMessage; +import org.sunbird.message.Localizer; +import org.sunbird.message.ResponseCode; import play.api.Environment; import play.api.inject.ApplicationLifecycle; import javax.inject.Inject; import javax.inject.Singleton; -import java.io.IOException; +import java.util.Locale; +import java.util.concurrent.CompletableFuture; /** * This class will be called after on application startup. only one instance of this class will be @@ -18,20 +27,71 @@ */ @Singleton public class ApplicationStart { - /** + + private static Logger logger = LoggerFactory.getLogger(ApplicationStart.class); + private static Localizer localizer = Localizer.getInstance(); + + /** * All one time initialization which required during server startup will fall here. * @param lifecycle ApplicationLifecycle * @param environment Environment */ @Inject public ApplicationStart(ApplicationLifecycle lifecycle, Environment environment) throws BaseException { - //instantiate actor system and initialize all the actors - - Application.getInstance().init(); - // Shut-down hook -// lifecycle.addStopHook( -// () -> { -// return CompletableFuture.completedFuture(null); -// }); + logger.info("ApplicationStart: Start"); + createCassandraConnection(JsonKeys.SUNBIRD); + // Shut-down hook + lifecycle.addStopHook( + () -> { + return CompletableFuture.completedFuture(null); + }); + logger.info("ApplicationStart: End"); } + + /** + * This method will read the configuration from System variable. + * + * @return boolean + */ + public static boolean createCassandraConnection(String keyspace) throws BaseException { + boolean response = false; + String ips = System.getenv(JsonKeys.SUNBIRD_CASSANDRA_IP); + String envPort = System.getenv(JsonKeys.SUNBIRD_CASSANDRA_PORT); + CassandraConnectionManager cassandraConnectionManager = + CassandraConnectionMngrFactory.getObject(JsonKeys.STANDALONE_MODE); + + if (StringUtils.isBlank(ips) || StringUtils.isBlank(envPort)) { + logger.info("Configuration value is not coming form System variable."); + return false; + } + String[] portList = envPort.split(","); + String userName = System.getenv(JsonKeys.SUNBIRD_CASSANDRA_USER_NAME); + String password = System.getenv(JsonKeys.SUNBIRD_CASSANDRA_PASSWORD); + try { + boolean result = + cassandraConnectionManager.createConnection(ips, portList[0], userName, password, keyspace); + if (result) { + response = true; + logger.info( + "CONNECTION CREATED SUCCESSFULLY FOR IP's: " + ips + " : KEYSPACE :" + keyspace); + } else { + logger.info( + "CONNECTION CREATION FAILED FOR IP: " + ips + " : KEYSPACE :" + keyspace); + } + } catch (BaseException ex) { + logger.error("Application:createCassandraConnection: Exception occurred with message = " + ex.getMessage()); + } + if (!response) { + throw new BaseException( + IResponseMessage.INVALID_CONFIGURATION, + getLocalizedMessage(IResponseMessage.INVALID_CONFIGURATION,null), + ResponseCode.SERVER_ERROR.hashCode()); + } + return response; + } + + private static String getLocalizedMessage(String key, Locale locale){ + return localizer.getMessage(key, locale); + } + } diff --git a/service/app/utils/module/ACTOR_NAMES.java b/service/app/utils/module/ACTOR_NAMES.java new file mode 100644 index 0000000..7904882 --- /dev/null +++ b/service/app/utils/module/ACTOR_NAMES.java @@ -0,0 +1,26 @@ +package utils.module; + + +import org.sunbird.actor.CertBackgroundActor; +import org.sunbird.actor.CertificationActor; + +public enum ACTOR_NAMES { + CERTIFICATION_ACTOR(CertificationActor.class, "certification_actor"), + CERTIFICATE_BACKGROUND_ACTOR(CertBackgroundActor.class, "certificate_background_actor"); + + ACTOR_NAMES(Class clazz, String name) { + actorClass = clazz; + actorName = name; + } + + private Class actorClass; + private String actorName; + + public Class getActorClass() { + return actorClass; + } + + public String getActorName() { + return actorName; + } +} diff --git a/service/app/utils/module/ActorStartModule.java b/service/app/utils/module/ActorStartModule.java new file mode 100644 index 0000000..316a0b0 --- /dev/null +++ b/service/app/utils/module/ActorStartModule.java @@ -0,0 +1,24 @@ +package utils.module; + +import akka.routing.FromConfig; +import akka.routing.RouterConfig; +import com.google.inject.AbstractModule; +import play.libs.akka.AkkaGuiceSupport; + +public class ActorStartModule extends AbstractModule implements AkkaGuiceSupport { + + @Override + protected void configure() { + System.out.println("binding actors for dependency injection"); + final RouterConfig config = new FromConfig(); + for (ACTOR_NAMES actor : ACTOR_NAMES.values()) { + bindActor( + actor.getActorClass(), + actor.getActorName(), + (props) -> { + return props.withRouter(config); + }); + } + System.out.println("binding completed"); + } +} diff --git a/service/conf/application.conf b/service/conf/application.conf index 42e32f2..ebf7e63 100755 --- a/service/conf/application.conf +++ b/service/conf/application.conf @@ -22,17 +22,88 @@ # Play uses Akka internally and exposes Akka Streams and actors in Websockets and # other streaming HTTP responses. akka { - # "akka.log-config-on-start" is extraordinarly useful because it log the complete - # configuration at INFO level, including defaults and overrides, so it s worth - # putting at the very top. - # - # Put the following in your conf/logback.xml file: - # - # - # - # And then uncomment this line to debug the configuration. - # - #log-config-on-start = true + loggers = ["akka.event.slf4j.Slf4jLogger"] + loglevel = "INFO" + stdout-loglevel = "DEBUG" + logging-filter = "akka.event.slf4j.Slf4jLoggingFilter" + log-config-on-start = off + + actor { + provider = "akka.actor.LocalActorRefProvider" + serializers { + java = "akka.serialization.JavaSerializer" + } + serialization-bindings { + "org.sunbird.request.Request" = java + "org.sunbird.response.Response" = java + } + default-dispatcher { + # This will be used if you have set "executor = "fork-join-executor"" + fork-join-executor { + # Min number of threads to cap factor-based parallelism number to + parallelism-min = 8 + + # The parallelism factor is used to determine thread pool size using the + # following formula: ceil(available processors * factor). Resulting size + # is then bounded by the parallelism-min and parallelism-max values. + parallelism-factor = 32.0 + + # Max number of threads to cap factor-based parallelism number to + parallelism-max = 64 + + # Setting to "FIFO" to use queue like peeking mode which "poll" or "LIFO" to use stack + # like peeking mode which "pop". + task-peeking-mode = "FIFO" + } + } + router-dispatcher { + type = "Dispatcher" + executor = "fork-join-executor" + fork-join-executor { + parallelism-min = 8 + parallelism-factor = 32.0 + parallelism-max = 64 + } + # Throughput for default Dispatcher, set to 1 for as fair as possible + throughput = 1 + } + cert-dispatcher { + type = "Dispatcher" + executor = "fork-join-executor" + fork-join-executor { + parallelism-min = 8 + parallelism-factor = 32.0 + parallelism-max = 64 + } + # Throughput for default Dispatcher, set to 1 for as fair as possible + throughput = 1 + } + deployment { + /certification_actor + { + router = smallest-mailbox-pool + nr-of-instances = 5 + dispatcher = cert-dispatcher + } + + /certificate_background_actor + { + router = smallest-mailbox-pool + nr-of-instances = 5 + dispatcher = cert-dispatcher + } + } + } + remote { + maximum-payload-bytes = 30000000 bytes + netty.tcp { + port = 8088 + message-frame-size = 30000000b + send-buffer-size = 30000000b + receive-buffer-size = 30000000b + maximum-frame-size = 30000000b + } + } } ## Secret key @@ -58,6 +129,7 @@ play.modules { # explicitly below. # If there are any built-in modules that you want to disable, you can list them here. enabled += utils.module.StartModule + enabled += utils.module.ActorStartModule # If there are any built-in modules that you want to disable, you can list them here. #disabled += "" @@ -153,15 +225,42 @@ play.http { ## Netty Provider # https://www.playframework.com/documentation/latest/SettingsNetty # ~~~~~ -play.server.netty { - # Whether the Netty wire should be logged - #log.wire = true - - # If you run Play on Linux, you can use Netty's native socket transport - # for higher performance with less garbage. - #transport = "native" +play.server { + # The server provider class name + provider = "play.core.server.NettyServerProvider" + netty { + # The number of event loop threads. 0 means let Netty decide, which by default will select 2 times the number of + # available processors. + eventLoopThreads = 30 + + # The transport to use, either jdk or native. + # Native socket transport has higher performance and produces less garbage but are only available on linux + transport = "native" + + # If you run Play on Linux, you can use Netty's native socket transport + # for higher performance with less garbage. + #transport = "native" + maxChunkSize = 30000000 + option { + + # Set whether connections should use TCP keep alive + # child.keepAlive = true + + # Set whether the TCP no delay flag is set + # child.tcpNoDelay = false + + # Set the size of the backlog of TCP connections. The default and exact meaning of this parameter is JDK specific. + # backlog = 100 + } + } } + +## WS (HTTP Client) +# ~~~~~ +libraryDependencies += javaWs + + ## WS (HTTP Client) # https://www.playframework.com/documentation/latest/ScalaWS#Configuring-WS # ~~~~~ @@ -201,7 +300,7 @@ play.ws { # Play comes with an integrated cache API that can reduce the operational # overhead of repeated requests. You must enable this by adding to build.sbt: # -# libraryDependencies += cache +libraryDependencies += cache # play.cache { # If you want to bind several caches, you can bind the individually diff --git a/service/conf/routes b/service/conf/routes index de98469..133c3c0 100755 --- a/service/conf/routes +++ b/service/conf/routes @@ -3,19 +3,19 @@ # ~~~~ #Health check -GET /health @controllers.health.HealthController.getHealth() -GET /:service/health @controllers.health.HealthController.getServiceHealth(service:String) +GET /health @controllers.health.HealthController.getHealth(request: play.mvc.Http.Request) +GET /:service/health @controllers.health.HealthController.getServiceHealth(service:String, request: play.mvc.Http.Request) # CERT APIs -POST /certs/v1/registry/add @controllers.CertificateController.add() -POST /certs/v2/registry/add @controllers.CertificateController.addV2() -POST /certs/v1/registry/validate @controllers.CertificateController.validate() -POST /certs/v1/registry/download @controllers.CertificateController.download() -GET /certs/v2/registry/download/:id @controllers.CertificateController.downloadV2(id:String) -POST /certs/v1/registry/verify @controllers.CertificateController.verify() -GET /certs/v1/registry/read/:id @controllers.CertificateController.read(id:String) -POST /certs/v1/registry/search @controllers.CertificateController.search() +POST /certs/v1/registry/add @controllers.CertificateController.add(request: play.mvc.Http.Request) +POST /certs/v2/registry/add @controllers.CertificateController.addV2(request: play.mvc.Http.Request) +POST /certs/v1/registry/validate @controllers.CertificateController.validate(request: play.mvc.Http.Request) +POST /certs/v1/registry/download @controllers.CertificateController.download(request: play.mvc.Http.Request) +GET /certs/v2/registry/download/:id @controllers.CertificateController.downloadV2(id:String, request: play.mvc.Http.Request) +POST /certs/v1/registry/verify @controllers.CertificateController.verify(request: play.mvc.Http.Request) +GET /certs/v1/registry/read/:id @controllers.CertificateController.read(id:String, request: play.mvc.Http.Request) +POST /certs/v1/registry/search @controllers.CertificateController.search(request: play.mvc.Http.Request) diff --git a/service/pom.xml b/service/pom.xml index d5c0134..19df638 100755 --- a/service/pom.xml +++ b/service/pom.xml @@ -23,6 +23,12 @@ + + com.typesafe.play + play-netty-server_${scala.major.version} + ${play2.version} + runtime + com.fasterxml.jackson.core jackson-databind @@ -35,7 +41,7 @@ com.typesafe.play - play_2.12 + play_${scala.major.version} ${play2.version} @@ -46,11 +52,15 @@ org.scala-lang scala-java8-compat_2.11 + + com.google.guava + guava + com.typesafe.play - play-guice_2.12 + play-guice_${scala.major.version} ${play2.version} @@ -65,18 +75,18 @@ com.typesafe.play - play-akka-http-server_2.12 + play-akka-http-server_${scala.major.version} ${play2.version} runtime com.typesafe.akka - akka-remote_2.12 + akka-remote_${scala.major.version} ${akka.x.version} com.typesafe.play - filters-helpers_2.12 + filters-helpers_${scala.major.version} ${play2.version} @@ -107,8 +117,8 @@ com.typesafe.play - play-test_2.12 - 2.6.7 + play-test_${scala.major.version} + ${play2.version} test @@ -134,7 +144,7 @@ com.google.code.play2-maven-plugin play2-maven-plugin - 1.0.0-rc1 + 1.0.0-rc5 true diff --git a/service/test/controllers/BaseApplicationTest.java b/service/test/controllers/BaseApplicationTest.java index 9a63950..7ab9801 100644 --- a/service/test/controllers/BaseApplicationTest.java +++ b/service/test/controllers/BaseApplicationTest.java @@ -1,23 +1,11 @@ package controllers; -import akka.actor.ActorRef; -import akka.actor.ActorSystem; -import akka.actor.Props; -import akka.dispatch.Futures; -import akka.pattern.Patterns; -import akka.util.Timeout; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.commons.lang3.StringUtils; import org.junit.runner.RunWith; -import org.mockito.Mockito; -import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PowerMockIgnore; -import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; -import org.sunbird.BaseException; -import org.sunbird.message.ResponseCode; -import org.sunbird.request.Request; import org.sunbird.response.Response; import org.sunbird.response.ResponseParams; import play.Application; @@ -27,70 +15,48 @@ import play.mvc.Http; import play.mvc.Result; import play.test.Helpers; -import scala.concurrent.Await; -import scala.concurrent.Future; -import scala.concurrent.duration.FiniteDuration; -import utils.module.OnRequestHandler; +import utils.module.ACTOR_NAMES; import utils.module.StartModule; import java.io.File; import java.io.IOException; -import java.util.HashMap; +import java.util.List; import java.util.Map; -import static org.powermock.api.mockito.PowerMockito.when; +import static play.inject.Bindings.bind; @RunWith(PowerMockRunner.class) -@PrepareForTest({org.sunbird.Application.class, BaseController.class, ActorRef.class, Await.class, org.sunbird.Application.class, Patterns.class}) @PowerMockIgnore({"javax.management.*", "javax.net.ssl.*", "javax.security.*"}) public abstract class BaseApplicationTest { protected Application application; - private ActorSystem system; - private Props props; - private org.sunbird.Application app; - private static ActorRef actorRef; - private static BaseController baseController; - public void setup(Class actorClass) { + public void setup(Class actorClass, ACTOR_NAMES actor) { try { application = - new GuiceApplicationBuilder() - .in(new File("path/to/app")) - .in(Mode.TEST) - .disable(StartModule.class) - .build(); + new GuiceApplicationBuilder() + .in(new File("path/to/app")) + .in(Mode.TEST) + .disable(StartModule.class) +// .disable(ActorStartModule.class) + .overrides(bind(actor.getActorClass()).to(actorClass)) + .build(); Helpers.start(application); - system = ActorSystem.create("system"); - props = Props.create(actorClass); - actorRef = system.actorOf(props); - baseController = Mockito.mock(BaseController.class); - applicationSetUp(); - Mockito.when(baseController.getActorRef(Mockito.anyString())).thenReturn(actorRef); - PowerMockito.mockStatic(OnRequestHandler.class); - PowerMockito.mockStatic(Patterns.class); - Futuref1= Futures.successful(getResponseObject()); - when(Patterns.ask(Mockito.any(ActorRef.class),Mockito.any(Request.class),Mockito.any(Timeout.class))).thenReturn(f1); } catch (Exception e) { } } - public void applicationSetUp() throws BaseException { - app = PowerMockito.mock(org.sunbird.Application.class); - PowerMockito.mockStatic(org.sunbird.Application.class); - PowerMockito.when(org.sunbird.Application.getInstance()).thenReturn(app); - app.init(); - } - private Response getResponseObject() { - - Response response = new Response(); - response.put("ResponseCode", "success"); - return response; - } - - public Result performTest(String url, String method) { - Http.RequestBuilder req = new Http.RequestBuilder().uri(url).method(method); - Result result = Helpers.route(application, req); - return result; + public void setup(List actors, Class actorClass) { + GuiceApplicationBuilder applicationBuilder = + new GuiceApplicationBuilder() + .in(new File("path/to/app")) + .in(Mode.TEST) + .disable(StartModule.class); +// .disable(ActorStartModule.class); + for (ACTOR_NAMES actor : actors) { + applicationBuilder = applicationBuilder.overrides(bind(actor.getActorClass()).to(actorClass)); + } + application = applicationBuilder.build(); + Helpers.start(application); } public String getResponseCode(Result result) { diff --git a/service/test/controllers/BaseControllerTest.java b/service/test/controllers/BaseControllerTest.java deleted file mode 100644 index 9c7bc87..0000000 --- a/service/test/controllers/BaseControllerTest.java +++ /dev/null @@ -1,45 +0,0 @@ -package controllers; - -import akka.actor.ActorRef; -import org.apache.commons.lang3.StringUtils; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PowerMockIgnore; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; -import org.sunbird.BaseException; -import org.sunbird.response.Response; -import scala.concurrent.Await; -import utils.JsonKey; - -import static org.junit.Assert.assertEquals; - - -@RunWith(PowerMockRunner.class) -@PrepareForTest({org.sunbird.Application.class, BaseController.class, ActorRef.class, Await.class, org.sunbird.Application.class}) -@PowerMockIgnore({"javax.management.*", "javax.net.ssl.*", "javax.security.*"}) - -public class BaseControllerTest { - private org.sunbird.Application application; - - public BaseControllerTest() throws BaseException { - baseControllerTestsetUp(); - } - - public void baseControllerTestsetUp() throws BaseException { - application = PowerMockito.mock(org.sunbird.Application.class); - PowerMockito.mockStatic(org.sunbird.Application.class); - PowerMockito.when(org.sunbird.Application.getInstance()).thenReturn(application); - application.init(); - } - - @Test - public void testJsonifyResponseFailure() { - Response response = new Response(); - BaseController controller = new BaseController(); - response.put(JsonKey.MESSAGE, response.getResult()); - String jsonifyResponse = "";//controller.jsonify(response); - assertEquals(StringUtils.EMPTY, jsonifyResponse); - } -} \ No newline at end of file diff --git a/service/test/controllers/CertificateControllerTest.java b/service/test/controllers/CertificateControllerTest.java index 32ba2e0..a6962ce 100644 --- a/service/test/controllers/CertificateControllerTest.java +++ b/service/test/controllers/CertificateControllerTest.java @@ -8,19 +8,16 @@ import org.junit.Test; import org.powermock.core.classloader.annotations.PrepareForTest; import org.sunbird.JsonKeys; -import org.sunbird.message.ResponseCode; -import org.sunbird.request.HeaderParam; import play.libs.Json; import play.mvc.Http; import play.mvc.Result; import play.test.Helpers; -import utils.JsonKey; +import utils.module.ACTOR_NAMES; import utils.module.OnRequestHandler; import java.io.IOException; import java.util.Arrays; import java.util.HashMap; -import java.util.List; import java.util.Map; import static org.junit.Assert.assertEquals; @@ -29,7 +26,7 @@ public class CertificateControllerTest extends BaseApplicationTest { @Before public void before() { - setup(DummyActor.class); + setup(Arrays.asList(ACTOR_NAMES.CERTIFICATION_ACTOR),DummyActor.class); } @Test diff --git a/service/test/controllers/TestHelper.java b/service/test/controllers/TestHelper.java index 3e102a4..7982200 100644 --- a/service/test/controllers/TestHelper.java +++ b/service/test/controllers/TestHelper.java @@ -6,7 +6,9 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.IOException; +import java.util.Arrays; import java.util.HashMap; +import java.util.List; import java.util.Map; import org.apache.commons.lang3.StringUtils; import play.libs.Json; @@ -42,7 +44,8 @@ public Result performTest(String url, String method, Map requestMap, Map headerM } else { req = new Http.RequestBuilder().uri(url).method(method); } - req.headers(headerMap); + Http.Headers headers = new Http.Headers(headerMap); + req.headers(headers); Result result = route(fakeApplication(), req); return result; } @@ -80,10 +83,10 @@ public int getResponseStatus(Result result) { * * @return */ - public Map getHeaderMap() { - Map headerMap = new HashMap<>(); - headerMap.put("x-authenticated-user-token", new String[] {"Some authenticated user ID"}); - headerMap.put("Authorization", new String[] {"Bearer ...."}); + public Map> getHeaderMap() { + Map> headerMap = new HashMap<>(); + headerMap.put("x-authenticated-user-token", Arrays.asList("Some authenticated user ID")); + headerMap.put("Authorization", Arrays.asList("Bearer ....")); return headerMap; } } \ No newline at end of file diff --git a/service/test/controllers/health/HealthControllerTest.java b/service/test/controllers/health/HealthControllerTest.java index d7362ea..fa6dbd3 100644 --- a/service/test/controllers/health/HealthControllerTest.java +++ b/service/test/controllers/health/HealthControllerTest.java @@ -1,56 +1,38 @@ package controllers.health; -import controllers.BaseControllerTest; -import controllers.TestHelper; -import org.junit.After; +import controllers.BaseApplicationTest; +import controllers.DummyActor; import org.junit.Before; import org.junit.Test; -import org.sunbird.BaseException; -import play.Application; import play.mvc.Result; -import play.test.Helpers; +import utils.module.ACTOR_NAMES; import javax.ws.rs.core.Response; +import java.util.Arrays; import java.util.HashMap; import java.util.Map; -import static org.junit.Assert.*; +import static org.junit.Assert.assertTrue; -public class HealthControllerTest extends BaseControllerTest { - TestHelper testHelper; - public static Application app; - public static Map headerMap; - - public HealthControllerTest() throws BaseException { - super(); - } +public class HealthControllerTest extends BaseApplicationTest { @Before - public void setUp(){ - testHelper = new TestHelper(); - app = Helpers.fakeApplication(); - Helpers.start(app); - headerMap = testHelper.getHeaderMap(); + public void before() { + setup(Arrays.asList(ACTOR_NAMES.CERTIFICATION_ACTOR), DummyActor.class); } - @After - public void tearDown(){ - headerMap = null; - app = null; - testHelper = null; - } @Test public void testGetHealthSuccess() { Map reqMap = new HashMap<>(); reqMap.put("accept", "yes"); - Result result = testHelper.performTest("/health", "GET", reqMap, headerMap); - assertTrue(testHelper.getResponseStatus(result) == Response.Status.OK.getStatusCode()); + Result result = performTest("/health", "GET", reqMap); + assertTrue(getResponseStatus(result) == Response.Status.OK.getStatusCode()); } @Test public void testGetHealthFailure() { Map reqMap = new HashMap<>(); reqMap.put("accept", "yes"); - Result result = testHelper.performTest("/health", "POST", reqMap, headerMap); - assertTrue(testHelper.getResponseStatus(result) == Response.Status.NOT_FOUND.getStatusCode()); + Result result = performTest("/health", "POST", reqMap); + assertTrue(getResponseStatus(result) == Response.Status.NOT_FOUND.getStatusCode()); } } \ No newline at end of file From a47c10a21cd2f27f3ff74c8e5775b7fad5c6821b Mon Sep 17 00:00:00 2001 From: aishwa8141 Date: Tue, 6 Oct 2020 16:07:06 +0530 Subject: [PATCH 02/39] SC-2049: Language Upgrade - Java 11 --- .circleci/config.yml | 2 +- Dockerfile | 2 +- all-actors/pom.xml | 2 +- .../org/sunbird/actor/CertificationActorTest.java | 2 +- cassandra-utils/pom.xml | 12 ++++++++---- .../cassandra/helper/ConnectionManagerImplTest.java | 2 +- .../cassandraimpl/CassandraOperationImplTest.java | 2 +- pom.xml | 5 +++-- sb-es-utils/pom.xml | 2 +- sb-utils/pom.xml | 6 +++--- service/pom.xml | 5 +++-- service/test/controllers/BaseApplicationTest.java | 2 +- 12 files changed, 25 insertions(+), 19 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index b789ae0..98fede3 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -2,7 +2,7 @@ version: 2.1 jobs: build: docker: - - image: circleci/openjdk:8-jdk + - image: circleci/openjdk:11-jdk-buster-node-browsers-legacy working_directory: ~/project steps: - checkout diff --git a/Dockerfile b/Dockerfile index 5ee86fe..fd8aa1c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM openjdk:8-jre-alpine +FROM sunbird/openjdk-java11-alpine:latest RUN apk update \ && apk add unzip \ && apk add curl \ diff --git a/all-actors/pom.xml b/all-actors/pom.xml index d7b41f9..2b30a4d 100644 --- a/all-actors/pom.xml +++ b/all-actors/pom.xml @@ -54,7 +54,7 @@ org.jacoco jacoco-maven-plugin - 0.7.5.201505241946 + ${jacoco.maven.plugin.version} ${basedir}/target/coverage-reports/jacoco-unit.exec ${basedir}/target/coverage-reports/jacoco-unit.exec diff --git a/all-actors/src/test/java/org/sunbird/actor/CertificationActorTest.java b/all-actors/src/test/java/org/sunbird/actor/CertificationActorTest.java index 4f3c741..731ee11 100644 --- a/all-actors/src/test/java/org/sunbird/actor/CertificationActorTest.java +++ b/all-actors/src/test/java/org/sunbird/actor/CertificationActorTest.java @@ -64,7 +64,7 @@ CertVars.class, IOUtils.class, URL.class}) -@PowerMockIgnore("javax.management.*") +@PowerMockIgnore({"javax.management.*", "jdk.internal.reflect.*"}) public class CertificationActorTest { diff --git a/cassandra-utils/pom.xml b/cassandra-utils/pom.xml index 5180a06..7825ee9 100644 --- a/cassandra-utils/pom.xml +++ b/cassandra-utils/pom.xml @@ -5,7 +5,11 @@ cassandra-utils 1.0-SNAPSHOT Cassandra Utils - + + certification-service + org.sunbird + 1.2.0 + 2.3.1 1.8 @@ -73,7 +77,7 @@ org.powermock powermock-module-junit4 - 1.6.5 + ${powermock.version} test @@ -85,7 +89,7 @@ org.powermock powermock-api-mockito - 1.6.5 + ${powermock.version} test @@ -129,7 +133,7 @@ org.jacoco jacoco-maven-plugin - 0.7.5.201505241946 + ${jacoco.maven.plugin.version} ${basedir}/target/coverage-reports/jacoco-unit.exec ${basedir}/target/coverage-reports/jacoco-unit.exec diff --git a/cassandra-utils/src/test/java/org/sunbird/cassandra/helper/ConnectionManagerImplTest.java b/cassandra-utils/src/test/java/org/sunbird/cassandra/helper/ConnectionManagerImplTest.java index 86fb207..a6c4281 100644 --- a/cassandra-utils/src/test/java/org/sunbird/cassandra/helper/ConnectionManagerImplTest.java +++ b/cassandra-utils/src/test/java/org/sunbird/cassandra/helper/ConnectionManagerImplTest.java @@ -36,7 +36,7 @@ Host.class, InetAddress.class }) -@PowerMockIgnore("javax.management.*") +@PowerMockIgnore({"javax.management.*", "jdk.internal.reflect.*"}) public class ConnectionManagerImplTest { PropertiesCache cache = null; diff --git a/cassandra-utils/src/test/java/org/sunbird/cassandraimpl/CassandraOperationImplTest.java b/cassandra-utils/src/test/java/org/sunbird/cassandraimpl/CassandraOperationImplTest.java index 0296fc6..6592bc1 100644 --- a/cassandra-utils/src/test/java/org/sunbird/cassandraimpl/CassandraOperationImplTest.java +++ b/cassandra-utils/src/test/java/org/sunbird/cassandraimpl/CassandraOperationImplTest.java @@ -69,7 +69,7 @@ Session.class }) -@PowerMockIgnore("javax.management.*") +@PowerMockIgnore({"javax.management.*", "jdk.internal.reflect.*"}) public class CassandraOperationImplTest { Localizer localizer = Localizer.getInstance(); private static Cluster cluster; diff --git a/pom.xml b/pom.xml index bd532ef..6d99929 100644 --- a/pom.xml +++ b/pom.xml @@ -25,10 +25,11 @@ UTF-8 2.7.2 - 2.11.8 + 2.11.12 2.11 - 1.6.5 + 1.7.4 4.5.1 + 0.8.5 diff --git a/sb-es-utils/pom.xml b/sb-es-utils/pom.xml index d02a911..bb4ec53 100755 --- a/sb-es-utils/pom.xml +++ b/sb-es-utils/pom.xml @@ -99,7 +99,7 @@ org.jacoco jacoco-maven-plugin - 0.7.5.201505241946 + ${jacoco.maven.plugin.version} ${basedir}/target/coverage-reports/jacoco-unit.exec ${basedir}/target/coverage-reports/jacoco-unit.exec diff --git a/sb-utils/pom.xml b/sb-utils/pom.xml index c630e28..2a8d228 100644 --- a/sb-utils/pom.xml +++ b/sb-utils/pom.xml @@ -39,14 +39,14 @@ org.powermock powermock-module-junit4 - 1.6.5 + ${powermock.version} org.powermock powermock-api-mockito - 1.6.5 + ${powermock.version} @@ -55,7 +55,7 @@ org.jacoco jacoco-maven-plugin - 0.7.5.201505241946 + ${jacoco.maven.plugin.version} ${basedir}/target/coverage-reports/jacoco-unit.exec ${basedir}/target/coverage-reports/jacoco-unit.exec diff --git a/service/pom.xml b/service/pom.xml index 19df638..7b04292 100755 --- a/service/pom.xml +++ b/service/pom.xml @@ -159,10 +159,11 @@ org.apache.maven.plugins maven-compiler-plugin - 2.3.2 + 3.8.1 1.8 1.8 + 11 @@ -179,7 +180,7 @@ org.jacoco jacoco-maven-plugin - 0.7.5.201505241946 + ${jacoco.maven.plugin.version} ${basedir}/target/coverage-reports/jacoco-unit.exec ${basedir}/target/coverage-reports/jacoco-unit.exec diff --git a/service/test/controllers/BaseApplicationTest.java b/service/test/controllers/BaseApplicationTest.java index 7ab9801..899b439 100644 --- a/service/test/controllers/BaseApplicationTest.java +++ b/service/test/controllers/BaseApplicationTest.java @@ -26,7 +26,7 @@ import static play.inject.Bindings.bind; @RunWith(PowerMockRunner.class) -@PowerMockIgnore({"javax.management.*", "javax.net.ssl.*", "javax.security.*"}) +@PowerMockIgnore({"javax.management.*", "javax.net.ssl.*", "javax.security.*", "jdk.internal.reflect.*"}) public abstract class BaseApplicationTest { protected Application application; From dcaae6557627187a01f1eece6a8bb70a3f19509d Mon Sep 17 00:00:00 2001 From: aishwa8141 Date: Tue, 6 Oct 2020 17:29:24 +0530 Subject: [PATCH 03/39] SC-2049: jenkinsfile changes - added java_home --- Jenkinsfile | 2 ++ auto_build_deploy | 2 ++ 2 files changed, 4 insertions(+) diff --git a/Jenkinsfile b/Jenkinsfile index 276534f..293feb7 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -7,6 +7,7 @@ node('build-slave') { String ANSI_YELLOW = "\u001B[33m" ansiColor('xterm') { + withEnv(["JAVA_HOME=${JAVA11_HOME}"]) { stage('Checkout') { if (!env.hub_org) { println(ANSI_BOLD + ANSI_RED + "Uh Oh! Please set a Jenkins environment variable named hub_org with value as registery/sunbidrded" + ANSI_NORMAL) @@ -51,6 +52,7 @@ node('build-slave') { archiveArtifacts "metadata.json" currentBuild.description = "${build_tag}" } + } } } catch (err) { diff --git a/auto_build_deploy b/auto_build_deploy index f689eb9..955b0f2 100644 --- a/auto_build_deploy +++ b/auto_build_deploy @@ -8,6 +8,7 @@ node('build-slave') { String ANSI_YELLOW = "\u001B[33m" ansiColor('xterm') { + withEnv(["JAVA_HOME=${JAVA11_HOME}"]) { stage('Checkout') { tag_name = env.JOB_NAME.split("/")[-1] pre_checks() @@ -52,6 +53,7 @@ node('build-slave') { slack_notify(currentBuild.result, tag_name) email_notify() auto_build_deploy() + } } catch (err) { currentBuild.result = "FAILURE" From 5f51705814f26296711f7f5a5aa9d013136e8fa5 Mon Sep 17 00:00:00 2001 From: aishwa8141 Date: Thu, 8 Oct 2020 12:10:30 +0530 Subject: [PATCH 04/39] SC-2049: review changes --- service/pom.xml | 2 -- 1 file changed, 2 deletions(-) diff --git a/service/pom.xml b/service/pom.xml index 7b04292..5be9acb 100755 --- a/service/pom.xml +++ b/service/pom.xml @@ -161,8 +161,6 @@ maven-compiler-plugin 3.8.1 - 1.8 - 1.8 11 From 677d1c69509e03ef23585e8795fafc515a4ab183 Mon Sep 17 00:00:00 2001 From: Pradyumna Nagendra Date: Wed, 7 Apr 2021 16:36:34 +0530 Subject: [PATCH 05/39] Github jira integration. --- .github/pull_request_template.md | 32 +++++++++++++++++++ .github/workflows/jira-description-action.yml | 16 ++++++++++ 2 files changed, 48 insertions(+) create mode 100644 .github/pull_request_template.md create mode 100644 .github/workflows/jira-description-action.yml diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md new file mode 100644 index 0000000..6c7880a --- /dev/null +++ b/.github/pull_request_template.md @@ -0,0 +1,32 @@ +Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change. + +### Type of change + +Please choose appropriate options. + +- [ ] Bug fix (non-breaking change which fixes an issue) +- [ ] New feature (non-breaking change which adds functionality) +- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) +- [ ] This change requires a documentation update + +### How Has This Been Tested? + +Please describe the tests that you ran to verify your changes in the below checkboxes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration + +- [ ] Ran Test A +- [ ] Ran Test B + +**Test Configuration**: +* Software versions: Java-11, play2-2.7.2 +* Hardware versions: 2 CPU/ 4GB RAM + +### Checklist: + +- [ ] My code follows the style guidelines of this project +- [ ] I have performed a self-review of my own code +- [ ] I have commented my code, particularly in hard-to-understand areas +- [ ] I have made corresponding changes to the documentation +- [ ] My changes generate no new warnings +- [ ] I have added tests that prove my fix is effective or that my feature works +- [ ] New and existing unit tests pass locally with my changes +- [ ] Any dependent changes have been merged and published in downstream modules diff --git a/.github/workflows/jira-description-action.yml b/.github/workflows/jira-description-action.yml new file mode 100644 index 0000000..76d3fa7 --- /dev/null +++ b/.github/workflows/jira-description-action.yml @@ -0,0 +1,16 @@ +name: jira-description-action +on: + pull_request_target: + types: [opened, labeled] +jobs: + add-jira-description: + runs-on: ubuntu-latest + steps: + - uses: project-sunbird/jira-description-action@v0.4.0 + name: jira-description-action + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + jira-token: ${{ secrets.JIRA_TOKEN }} + jira-base-url: ${{ secrets.JIRA_BASE_URL }} + fail-when-jira-issue-not-found: ${{ secrets.FAIL_WHEN_JIRA_ISSUE_NOT_FOUND }} + use: both From f83f03a33a573dc9de76ef163aa6e90bc9dc8c9c Mon Sep 17 00:00:00 2001 From: pritha-tarento Date: Tue, 1 Feb 2022 16:22:10 +0530 Subject: [PATCH 06/39] SB-28062 log4j fix - ES upgrade --- sb-es-utils/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sb-es-utils/pom.xml b/sb-es-utils/pom.xml index bb4ec53..64a3e2b 100755 --- a/sb-es-utils/pom.xml +++ b/sb-es-utils/pom.xml @@ -23,7 +23,7 @@ org.elasticsearch.client elasticsearch-rest-high-level-client - 6.4.0 + 6.8.22 org.scala-lang From d721947839d7c74723c0a8b27af2d60136a6229b Mon Sep 17 00:00:00 2001 From: Harikumar Palemkota Date: Tue, 8 Mar 2022 22:55:41 +0530 Subject: [PATCH 07/39] SB-29068 modified certificate download api wrto sunbird-rc integration --- .../java/org/sunbird/RegistryCredential.java | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 all-actors/src/main/java/org/sunbird/RegistryCredential.java diff --git a/all-actors/src/main/java/org/sunbird/RegistryCredential.java b/all-actors/src/main/java/org/sunbird/RegistryCredential.java new file mode 100644 index 0000000..bc333a3 --- /dev/null +++ b/all-actors/src/main/java/org/sunbird/RegistryCredential.java @@ -0,0 +1,49 @@ +package org.sunbird; + +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * this class will help in getting the env values for calling the cert service + * @author anmolgupta + */ +public class CertVars { + + public static final String CERT_SERVICE_BASE_URL= "cert_service_base_url"; + private static Logger logger= LoggerFactory.getLogger(CertVars.class); + private static final String SERVICE_BASE_URL=getPropsFromEnvs(CERT_SERVICE_BASE_URL); + private static final String DOWNLOAD_URI= "/v1/user/certs/download"; + private static final String GENERATE_URI="/v1/certs/generate"; + private static final String VERIFY_URI="/v1/certs/verify"; + public static final String SUNBIRD_ES_IP = "sunbird_es_host"; + + + public static String getVerifyUri() { return VERIFY_URI; } + public static String getGenerateUri() { + return GENERATE_URI; + } + + public static String getSERVICE_BASE_URL() { + if(StringUtils.isBlank(SERVICE_BASE_URL)){ + logger.error("CertVars:getPropsFromEnvs:no suitable host found for downloadUri"); + System.exit(-1); + } + return SERVICE_BASE_URL; + } + public static String getDOWNLOAD_URI() { + return DOWNLOAD_URI; + } + + private static String getPropsFromEnvs(String props){ + String propValue=System.getenv(props); + return propValue; + } + + + public static String getEsSearchUri(){ + String esApi=String.format("http://%s:9200/%s/_search",getPropsFromEnvs(SUNBIRD_ES_IP).split(",")[0],JsonKeys.CERT_ALIAS); + logger.info("CertVars:getEsSearchUri:es uri formed:"+esApi); + return esApi; + } +} From 0ccfc5df281f5eea36c82290efa29c1f8e98ea49 Mon Sep 17 00:00:00 2001 From: Harikumar Palemkota Date: Tue, 8 Mar 2022 23:01:16 +0530 Subject: [PATCH 08/39] SB-29068 modified certificate download api wrto sunbird-rc integration --- .../java/org/sunbird/RegistryCredential.java | 24 ++++++---------- .../org/sunbird/service/ICertService.java | 5 +++- .../sunbird/serviceimpl/CertsServiceImpl.java | 28 ++++++++++++++++--- .../sunbird/utilities/CertificateUtil.java | 10 ++++++- 4 files changed, 45 insertions(+), 22 deletions(-) diff --git a/all-actors/src/main/java/org/sunbird/RegistryCredential.java b/all-actors/src/main/java/org/sunbird/RegistryCredential.java index bc333a3..b20def6 100644 --- a/all-actors/src/main/java/org/sunbird/RegistryCredential.java +++ b/all-actors/src/main/java/org/sunbird/RegistryCredential.java @@ -8,25 +8,17 @@ * this class will help in getting the env values for calling the cert service * @author anmolgupta */ -public class CertVars { +public class RegistryCredential { - public static final String CERT_SERVICE_BASE_URL= "cert_service_base_url"; - private static Logger logger= LoggerFactory.getLogger(CertVars.class); - private static final String SERVICE_BASE_URL=getPropsFromEnvs(CERT_SERVICE_BASE_URL); - private static final String DOWNLOAD_URI= "/v1/user/certs/download"; - private static final String GENERATE_URI="/v1/certs/generate"; - private static final String VERIFY_URI="/v1/certs/verify"; + public static final String REGISTRY_CREDENTIAL_SERVICE_BASE_URL= "registry_credential_service_base_url"; + private static Logger logger= LoggerFactory.getLogger(RegistryCredential.class); + private static final String SERVICE_BASE_URL=getPropsFromEnvs(REGISTRY_CREDENTIAL_SERVICE_BASE_URL); + private static final String DOWNLOAD_URI= "/rc/certificate/v1/download"; public static final String SUNBIRD_ES_IP = "sunbird_es_host"; - - public static String getVerifyUri() { return VERIFY_URI; } - public static String getGenerateUri() { - return GENERATE_URI; - } - public static String getSERVICE_BASE_URL() { if(StringUtils.isBlank(SERVICE_BASE_URL)){ - logger.error("CertVars:getPropsFromEnvs:no suitable host found for downloadUri"); + logger.error("RegistryCredential:getPropsFromEnvs:no suitable host found for downloadUri"); System.exit(-1); } return SERVICE_BASE_URL; @@ -42,8 +34,8 @@ private static String getPropsFromEnvs(String props){ public static String getEsSearchUri(){ - String esApi=String.format("http://%s:9200/%s/_search",getPropsFromEnvs(SUNBIRD_ES_IP).split(",")[0],JsonKeys.CERT_ALIAS); - logger.info("CertVars:getEsSearchUri:es uri formed:"+esApi); + String esApi=String.format("http://%s:9200/%s/search",getPropsFromEnvs(SUNBIRD_ES_IP).split(",")[0]," rc/certificate/v1"); + logger.info("RegistryCredential:getEsSearchUri:es uri formed:"+esApi); return esApi; } } diff --git a/all-actors/src/main/java/org/sunbird/service/ICertService.java b/all-actors/src/main/java/org/sunbird/service/ICertService.java index f39a9aa..37a727e 100644 --- a/all-actors/src/main/java/org/sunbird/service/ICertService.java +++ b/all-actors/src/main/java/org/sunbird/service/ICertService.java @@ -2,10 +2,13 @@ import akka.actor.ActorRef; +import com.fasterxml.jackson.core.JsonProcessingException; import org.sunbird.BaseException; import org.sunbird.request.Request; import org.sunbird.response.Response; +import java.util.concurrent.ExecutionException; + /** * this is an interface class for implementing certificate related operations * @author anmolgupta @@ -21,7 +24,7 @@ public interface ICertService{ Response download(Request request) throws BaseException; - Response downloadV2(Request request) throws BaseException; + Response downloadV2(Request request) throws BaseException, JsonProcessingException, ExecutionException, InterruptedException; Response generate(Request request) throws BaseException; diff --git a/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java b/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java index 094b605..fa41e51 100644 --- a/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java +++ b/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java @@ -1,6 +1,7 @@ package org.sunbird.serviceimpl; import akka.actor.ActorRef; +import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.mashape.unirest.http.HttpResponse; @@ -15,6 +16,7 @@ import org.sunbird.BaseException; import org.sunbird.CertVars; import org.sunbird.JsonKeys; +import org.sunbird.RegistryCredential; import org.sunbird.builders.Certificate; import org.sunbird.builders.Recipient; import org.sunbird.message.IResponseMessage; @@ -34,6 +36,7 @@ import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; @@ -205,7 +208,7 @@ public Response download(Request request) throws BaseException { String signedUrl=jsonResponse.getBody().getObject().getJSONObject(JsonKeys.RESULT).getString(JsonKeys.SIGNED_URL); response.put(JsonKeys.SIGNED_URL,signedUrl); } else { - throw new BaseException(IResponseMessage.INVALID_REQUESTED_DATA, MessageFormat.format(getLocalizedMessage(IResponseMessage.INVALID_PROVIDED_URL,null),(String)request.getRequest().get(JsonKeys.PDF_URL)), ResponseCode.CLIENT_ERROR.getCode()); + throw new BaseException(IResponseMessage.INVALID_REQUESTED_DATA, MessageFormat.format(getLocalizedMessage(IResponseMessage.INVALID_PROVIDED_URL, null), (String) request.getRequest().get(JsonKeys.PDF_URL)), ResponseCode.CLIENT_ERROR.getCode()); } } catch (Exception e) { @@ -216,17 +219,17 @@ public Response download(Request request) throws BaseException { } @Override - public Response downloadV2(Request request) throws BaseException { + public Response downloadV2(Request request) throws BaseException, ExecutionException, InterruptedException { String certId = (String) request.getRequest().get(JsonKeys.ID); logger.info("CertServiceImpl:downloadV2:idProvided:" + certId); Response certData = CertificateUtil.getCertRecordByID(certId); Response response = new Response(); List> resultList = (List>) certData.getResult().get(JsonKeys.RESPONSE); + String printUri; if (CollectionUtils.isNotEmpty(resultList) && MapUtils.isNotEmpty(resultList.get(0))) { Map certInfo = resultList.get(0); try { String jsonUrl = (String) certInfo.get(JsonKeys.JSON_URL); - String printUri; //in-some cases jsonUrl was not filled(1.5.0 prior to fix), After fix jsonUrl is being filled (because of svg content growth,now we are uploading cert to cloud) if (StringUtils.isEmpty(jsonUrl)) { logger.info("getJsonSignedUrl: jsonUrl is empty , print uri is present in data object"); @@ -246,8 +249,25 @@ public Response downloadV2(Request request) throws BaseException { throw new BaseException(IResponseMessage.INTERNAL_ERROR, getLocalizedMessage(IResponseMessage.INTERNAL_ERROR, null), ResponseCode.SERVER_ERROR.getCode()); } } else { - throw new BaseException(IResponseMessage.RESOURCE_NOT_FOUND, localizer.getMessage(IResponseMessage.RESOURCE_NOT_FOUND, null), ResponseCode.RESOURCE_NOT_FOUND.getCode()); + Map headerMap = new HashMap<>(); + headerMap.put("Accept", "application/vc+ld+json"); + String rcTemplateApi = RegistryCredential.getSERVICE_BASE_URL().concat(RegistryCredential.getDOWNLOAD_URI())+"/"+certId; + Future> rcResponseFuture=CertificateUtil.makeAsyncGetCall(rcTemplateApi,headerMap); + HttpResponse rcJsonResponse = rcResponseFuture.get(); + if (rcJsonResponse != null && rcJsonResponse.getStatus() == HttpStatus.SC_OK) { + String templateUrl = rcJsonResponse.getBody().getObject().getJSONObject(JsonKeys.RESULT).getString("templateurl"); + String rcDownloadApi = RegistryCredential.getSERVICE_BASE_URL().concat(RegistryCredential.getDOWNLOAD_URI())+"/"+certId; + headerMap.put("Accept", "image/svg+xml"); + headerMap.put("templateurl", templateUrl); + Future> rcDownloadResFuture=CertificateUtil.makeAsyncGetCall(rcDownloadApi,headerMap); + HttpResponse rcDownloadJsonResponse = rcDownloadResFuture.get(); + printUri = rcDownloadJsonResponse.getBody().getObject().getJSONObject(JsonKeys.RESULT).getString(JsonKeys.PRINT_URI); + response.put(JsonKeys.PRINT_URI, printUri); + } else { + throw new BaseException(IResponseMessage.RESOURCE_NOT_FOUND, localizer.getMessage(IResponseMessage.RESOURCE_NOT_FOUND, null), ResponseCode.RESOURCE_NOT_FOUND.getCode()); + } } + return response; } diff --git a/all-actors/src/main/java/org/sunbird/utilities/CertificateUtil.java b/all-actors/src/main/java/org/sunbird/utilities/CertificateUtil.java index d88d292..f578808 100644 --- a/all-actors/src/main/java/org/sunbird/utilities/CertificateUtil.java +++ b/all-actors/src/main/java/org/sunbird/utilities/CertificateUtil.java @@ -128,7 +128,15 @@ public static Future> makeAsyncPostCall(String apiToCall, .asJsonAsync(); return jsonResponse; } - + + public static Future> makeAsyncGetCall(String apiToCall, MapheaderMap){ + logger.info("CertificateUtil:makePostCall:get request to make post call for API:"+apiToCall); + Future> jsonResponse + = Unirest.get(apiToCall) + .headers(headerMap) + .asJsonAsync(); + return jsonResponse; + } private static String getLocalizedMessage(String key, Locale locale){ return localizer.getMessage(key, locale); } From e2ed42ba577163209b76654ab88edabbe4cfdbd6 Mon Sep 17 00:00:00 2001 From: Harikumar Palemkota Date: Thu, 10 Mar 2022 08:24:24 +0530 Subject: [PATCH 09/39] SB-29068 added changes for getting details from sunbird-rc --- .../sunbird/serviceimpl/CertsServiceImpl.java | 39 +++++++++++++++---- .../sunbird/utilities/ESResponseMapper.java | 8 ++++ .../src/main/java/org.sunbird/JsonKeys.java | 3 +- 3 files changed, 42 insertions(+), 8 deletions(-) diff --git a/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java b/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java index fa41e51..c49557c 100644 --- a/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java +++ b/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; import com.mashape.unirest.http.HttpResponse; import com.mashape.unirest.http.JsonNode; import org.apache.commons.collections.CollectionUtils; @@ -32,10 +33,7 @@ import java.net.URL; import java.nio.charset.StandardCharsets; import java.text.MessageFormat; -import java.util.HashMap; -import java.util.List; -import java.util.Locale; -import java.util.Map; +import java.util.*; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; @@ -399,16 +397,43 @@ public Response search(Request request) throws BaseException{ logger.info("CertsServiceImpl:search:request body found."); String apiToCall = CertVars.getEsSearchUri(); logger.info("CertsServiceImpl:search:complete url found:" + apiToCall); + + String rcSearchApiCall = RegistryCredential.getEsSearchUri(); + logger.info("RegistryCredential:search:complete url found:" + rcSearchApiCall); Future> responseFuture = CertificateUtil.makeAsyncPostCall(apiToCall, requestBody, headerMap); HttpResponse jsonResponse = responseFuture.get(); + ESResponseMapper mappedResponse = null; if (jsonResponse != null && jsonResponse.getStatus() == HttpStatus.SC_OK) { String jsonArray = jsonResponse.getBody().getObject().getJSONObject(JsonKeys.HITS).toString(); - Map apiResp=requestMapper.readValue(jsonArray,Map.class); - ESResponseMapper mappedResponse = new ObjectMapper().convertValue(apiResp,ESResponseMapper.class); - response.put(JsonKeys.RESPONSE, mappedResponse); + Map apiResp = requestMapper.readValue(jsonArray, Map.class); + mappedResponse = new ObjectMapper().convertValue(apiResp, ESResponseMapper.class); + } + Map req = request.getRequest(); + ObjectMapper mapper = new ObjectMapper(); + ObjectNode jsonNode = mapper.convertValue(req, ObjectNode.class); + ObjectNode jsonNode1 = (ObjectNode) jsonNode.at("/query/match_phrase/"); + Iterator> fields = jsonNode1.fields(); + Map filters = new HashMap<>(); + Map fieldKeyMap = new HashMap<>(); + fields.forEachRemaining(field -> { + Map fieldValueMap = new HashMap<>(); + fieldValueMap.put("eq", field.getValue().asText()); + fieldKeyMap.put(field.getKey(), fieldValueMap); + }); + filters.put(JsonKeys.FILTERS, fieldKeyMap); + Future> rcResponseFuture = CertificateUtil.makeAsyncPostCall(rcSearchApiCall, requestBody, headerMap); + HttpResponse rcJsonResponse = rcResponseFuture.get(); + if ((mappedResponse !=null && rcJsonResponse != null) && rcJsonResponse.getStatus() == HttpStatus.SC_OK) { + String rcJsonArray = rcJsonResponse.getBody().getObject().getJSONObject(JsonKeys.HITS).toString(); + Map rcSearchApiResp = requestMapper.readValue(rcJsonArray, Map.class); + ESResponseMapper rcmappedResponse = new ObjectMapper().convertValue(rcSearchApiResp, ESResponseMapper.class); + mappedResponse.setCount(mappedResponse.getCount() + rcmappedResponse.getCount()); + mappedResponse.getContent().addAll(rcmappedResponse.getContent()); } else { throw new BaseException(IResponseMessage.INVALID_REQUESTED_DATA,jsonResponse.getBody().toString(), ResponseCode.CLIENT_ERROR.getCode()); } + response.put(JsonKeys.RESPONSE, mappedResponse); + } catch (Exception e) { logger.error("CertsServiceImpl:search:exception occurred:" + e); throw new BaseException(IResponseMessage.INTERNAL_ERROR, IResponseMessage.INTERNAL_ERROR, ResponseCode.SERVER_ERROR.getCode()); diff --git a/all-actors/src/main/java/org/sunbird/utilities/ESResponseMapper.java b/all-actors/src/main/java/org/sunbird/utilities/ESResponseMapper.java index 46c71a1..48a0625 100644 --- a/all-actors/src/main/java/org/sunbird/utilities/ESResponseMapper.java +++ b/all-actors/src/main/java/org/sunbird/utilities/ESResponseMapper.java @@ -27,4 +27,12 @@ public List> getContent() { public int getCount() { return count; } + + public void setContent(List> content) { + this.content = content; + } + + public void setCount(int count) { + this.count = count; + } } \ No newline at end of file diff --git a/sb-utils/src/main/java/org.sunbird/JsonKeys.java b/sb-utils/src/main/java/org.sunbird/JsonKeys.java index 0ccd32c..004bc28 100644 --- a/sb-utils/src/main/java/org.sunbird/JsonKeys.java +++ b/sb-utils/src/main/java/org.sunbird/JsonKeys.java @@ -75,5 +75,6 @@ public class JsonKeys { public static final String REQ_ID ="reqId"; public static final String REQUEST_MESSAGE_ID = "msgId"; public static final String X_REQUEST_ID = "X-Request-ID"; - + + public static final String FILTERS = "filters"; } From bc354f563ee8ce9c4cda6b23a80751651f8ded47 Mon Sep 17 00:00:00 2001 From: Harikumar Palemkota Date: Fri, 11 Mar 2022 17:59:42 +0530 Subject: [PATCH 10/39] SB-29166 added new download api to support for older versions app and portal --- service/conf/routes | 2 ++ 1 file changed, 2 insertions(+) diff --git a/service/conf/routes b/service/conf/routes index 133c3c0..c9d1776 100755 --- a/service/conf/routes +++ b/service/conf/routes @@ -13,9 +13,11 @@ POST /certs/v2/registry/add @controllers.CertificateCo POST /certs/v1/registry/validate @controllers.CertificateController.validate(request: play.mvc.Http.Request) POST /certs/v1/registry/download @controllers.CertificateController.download(request: play.mvc.Http.Request) GET /certs/v2/registry/download/:id @controllers.CertificateController.downloadV2(id:String, request: play.mvc.Http.Request) +GET /certs/v3/registry/download/:id @controllers.CertificateController.downloadV2(id:String, request: play.mvc.Http.Request) POST /certs/v1/registry/verify @controllers.CertificateController.verify(request: play.mvc.Http.Request) GET /certs/v1/registry/read/:id @controllers.CertificateController.read(id:String, request: play.mvc.Http.Request) POST /certs/v1/registry/search @controllers.CertificateController.search(request: play.mvc.Http.Request) + From e0099b3bf64c053673a3bcb48fdaf3819b280c9a Mon Sep 17 00:00:00 2001 From: Harikumar Palemkota Date: Fri, 11 Mar 2022 18:27:45 +0530 Subject: [PATCH 11/39] SB-29166 added new search api to support for older versions app and portal --- service/conf/routes | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/conf/routes b/service/conf/routes index c9d1776..87e2df3 100755 --- a/service/conf/routes +++ b/service/conf/routes @@ -13,10 +13,10 @@ POST /certs/v2/registry/add @controllers.CertificateCo POST /certs/v1/registry/validate @controllers.CertificateController.validate(request: play.mvc.Http.Request) POST /certs/v1/registry/download @controllers.CertificateController.download(request: play.mvc.Http.Request) GET /certs/v2/registry/download/:id @controllers.CertificateController.downloadV2(id:String, request: play.mvc.Http.Request) -GET /certs/v3/registry/download/:id @controllers.CertificateController.downloadV2(id:String, request: play.mvc.Http.Request) POST /certs/v1/registry/verify @controllers.CertificateController.verify(request: play.mvc.Http.Request) GET /certs/v1/registry/read/:id @controllers.CertificateController.read(id:String, request: play.mvc.Http.Request) POST /certs/v1/registry/search @controllers.CertificateController.search(request: play.mvc.Http.Request) +POST /certs/v2/registry/search @controllers.CertificateController.search(request: play.mvc.Http.Request) From 326e1d56e7d82ee3e91114681c473e66b9621d85 Mon Sep 17 00:00:00 2001 From: Harikumar Palemkota Date: Sun, 13 Mar 2022 11:42:29 +0530 Subject: [PATCH 12/39] SB-29166 jenkins file corrected --- Jenkinsfile | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 293feb7..ce7cc2c 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -16,18 +16,9 @@ node('build-slave') { println(ANSI_BOLD + ANSI_GREEN + "Found environment variable named hub_org with value as: " + hub_org + ANSI_NORMAL) } cleanWs() - if (params.github_release_tag == "") { - checkout scm - commit_hash = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim() - branch_name = sh(script: 'git name-rev --name-only HEAD | rev | cut -d "/" -f1| rev', returnStdout: true).trim() - build_tag = branch_name + "" + commit_hash + "" + env.BUILD_NUMBER - println(ANSI_BOLD + ANSI_YELLOW + "github_release_tag not specified, using the latest commit hash: " + commit_hash + ANSI_NORMAL) - } else { - def scmVars = checkout scm - checkout scm: [$class: 'GitSCM', branches: [[name: "refs/tags/$params.github_release_tag"]], userRemoteConfigs: [[url: scmVars.GIT_URL]]] - build_tag = params.github_release_tag + "_" + env.BUILD_NUMB - println(ANSI_BOLD + ANSI_YELLOW + "github_release_tag specified, building from tag: " + params.github_release_tag + ANSI_NORMAL) - } + checkout scm + commit_hash = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim() + build_tag = sh(script: "echo " + params.github_release_tag.split('/')[-1] + "_" + commit_hash + "_" + env.BUILD_NUMBER, returnStdout: true).trim() echo "build_tag: " + build_tag stage('Build') { From 9c3c55fbb3843025cd19f1783adc0aa7ea72c1a3 Mon Sep 17 00:00:00 2001 From: Harikumar Palemkota Date: Wed, 16 Mar 2022 15:46:59 +0530 Subject: [PATCH 13/39] SB-29068 added changes to search api --- Jenkinsfile | 2 +- .../src/main/java/org/sunbird/CertVars.java | 2 +- .../java/org/sunbird/RegistryCredential.java | 21 +++--- .../org/sunbird/actor/CertificationActor.java | 11 +++- .../org/sunbird/service/ICertService.java | 4 +- .../sunbird/serviceimpl/CertsServiceImpl.java | 65 ++++++++++++++----- .../src/main/java/org.sunbird/JsonKeys.java | 1 + .../controllers/CertificateController.java | 11 ++++ service/conf/routes | 2 +- 9 files changed, 87 insertions(+), 32 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index ce7cc2c..14bc8e2 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -31,7 +31,7 @@ node('build-slave') { sh 'mvn clean install' } stage('Package') { - // Create a deployment package + // Create a deployment packageCertificationActor dir('service') { sh 'mvn play2:dist' sh 'cp target/service-1.0.0-SNAPSHOT-dist.zip ../' diff --git a/all-actors/src/main/java/org/sunbird/CertVars.java b/all-actors/src/main/java/org/sunbird/CertVars.java index bc333a3..7bceb13 100644 --- a/all-actors/src/main/java/org/sunbird/CertVars.java +++ b/all-actors/src/main/java/org/sunbird/CertVars.java @@ -43,7 +43,7 @@ private static String getPropsFromEnvs(String props){ public static String getEsSearchUri(){ String esApi=String.format("http://%s:9200/%s/_search",getPropsFromEnvs(SUNBIRD_ES_IP).split(",")[0],JsonKeys.CERT_ALIAS); - logger.info("CertVars:getEsSearchUri:es uri formed:"+esApi); + logger.info("CertVars:getRCSearchUri:es uri formed:"+esApi); return esApi; } } diff --git a/all-actors/src/main/java/org/sunbird/RegistryCredential.java b/all-actors/src/main/java/org/sunbird/RegistryCredential.java index b20def6..ee19dfd 100644 --- a/all-actors/src/main/java/org/sunbird/RegistryCredential.java +++ b/all-actors/src/main/java/org/sunbird/RegistryCredential.java @@ -12,30 +12,31 @@ public class RegistryCredential { public static final String REGISTRY_CREDENTIAL_SERVICE_BASE_URL= "registry_credential_service_base_url"; private static Logger logger= LoggerFactory.getLogger(RegistryCredential.class); - private static final String SERVICE_BASE_URL=getPropsFromEnvs(REGISTRY_CREDENTIAL_SERVICE_BASE_URL); - private static final String DOWNLOAD_URI= "/rc/certificate/v1/download"; - public static final String SUNBIRD_ES_IP = "sunbird_es_host"; + private static final String SERVICE_BASE_URL = getPropsFromEnvs(REGISTRY_CREDENTIAL_SERVICE_BASE_URL); + private static final String CERTIFICATE_TYPE = getPropsFromEnvs("certificate_type"); + private static final String API = "/api/v1/%s"; public static String getSERVICE_BASE_URL() { if(StringUtils.isBlank(SERVICE_BASE_URL)){ - logger.error("RegistryCredential:getPropsFromEnvs:no suitable host found for downloadUri"); + logger.error("RegistryCredential:getPropsFromEnvs:no suitable host found"); System.exit(-1); } return SERVICE_BASE_URL; } public static String getDOWNLOAD_URI() { - return DOWNLOAD_URI; + return String.format(API, CERTIFICATE_TYPE); } private static String getPropsFromEnvs(String props){ - String propValue=System.getenv(props); + String propValue = System.getenv(props); return propValue; } - public static String getEsSearchUri(){ - String esApi=String.format("http://%s:9200/%s/search",getPropsFromEnvs(SUNBIRD_ES_IP).split(",")[0]," rc/certificate/v1"); - logger.info("RegistryCredential:getEsSearchUri:es uri formed:"+esApi); - return esApi; + public static String getRCSearchUri(){ + String apiUrl = String.format(API, CERTIFICATE_TYPE); + String rcSearchApi = "https://dev.sunbirded.org/api/rc/certificate/v1/search";//String.format("http://%s/%s/search", getSERVICE_BASE_URL().split(",")[0], apiUrl); + logger.info("RegistryCredential:getRCSearchUri:es uri formed: "+rcSearchApi); + return rcSearchApi; } } diff --git a/all-actors/src/main/java/org/sunbird/actor/CertificationActor.java b/all-actors/src/main/java/org/sunbird/actor/CertificationActor.java index 536e9c6..d656654 100644 --- a/all-actors/src/main/java/org/sunbird/actor/CertificationActor.java +++ b/all-actors/src/main/java/org/sunbird/actor/CertificationActor.java @@ -1,6 +1,7 @@ package org.sunbird.actor; import akka.actor.ActorRef; +import com.fasterxml.jackson.core.JsonProcessingException; import org.sunbird.BaseActor; import org.sunbird.BaseException; import org.sunbird.JsonKeys; @@ -11,6 +12,7 @@ import javax.inject.Inject; import javax.inject.Named; +import java.util.concurrent.ExecutionException; public class CertificationActor extends BaseActor { private ICertService certService = getCertServiceImpl(); @@ -25,7 +27,7 @@ private ICertService getCertServiceImpl(){ } @Override - public void onReceive(Request request) throws BaseException { + public void onReceive(Request request) throws BaseException, InterruptedException, ExecutionException, JsonProcessingException { logger.info("CertificationActor:onReceive:request arrived with operation" + request.getOperation()); String operation = request.getOperation(); switch (operation) { @@ -56,6 +58,9 @@ public void onReceive(Request request) throws BaseException { case "downloadV2" : downloadV2(request); break; + case "searchV2": + searchV2(request); + break; default: onReceiveUnsupportedMessage("CertificationActor"); } @@ -92,6 +97,10 @@ private void read(Request request) throws BaseException { private void search(Request request) throws BaseException{ sender().tell(certService.search(request),self()); } + + private void searchV2(Request request) throws BaseException{ + sender().tell(certService.searchV2(request),self()); + } private void downloadV2(Request request) throws BaseException { sender().tell(certService.downloadV2(request), self()); diff --git a/all-actors/src/main/java/org/sunbird/service/ICertService.java b/all-actors/src/main/java/org/sunbird/service/ICertService.java index 37a727e..5639693 100644 --- a/all-actors/src/main/java/org/sunbird/service/ICertService.java +++ b/all-actors/src/main/java/org/sunbird/service/ICertService.java @@ -24,7 +24,7 @@ public interface ICertService{ Response download(Request request) throws BaseException; - Response downloadV2(Request request) throws BaseException, JsonProcessingException, ExecutionException, InterruptedException; + Response downloadV2(Request request) throws BaseException; Response generate(Request request) throws BaseException; @@ -33,5 +33,7 @@ public interface ICertService{ Response read(Request request) throws BaseException; Response search(Request request) throws BaseException; + + Response searchV2(Request request) throws BaseException; } diff --git a/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java b/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java index c49557c..dbdf166 100644 --- a/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java +++ b/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java @@ -1,7 +1,6 @@ package org.sunbird.serviceimpl; import akka.actor.ActorRef; -import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; @@ -217,7 +216,7 @@ public Response download(Request request) throws BaseException { } @Override - public Response downloadV2(Request request) throws BaseException, ExecutionException, InterruptedException { + public Response downloadV2(Request request) throws BaseException { String certId = (String) request.getRequest().get(JsonKeys.ID); logger.info("CertServiceImpl:downloadV2:idProvided:" + certId); Response certData = CertificateUtil.getCertRecordByID(certId); @@ -248,20 +247,25 @@ public Response downloadV2(Request request) throws BaseException, ExecutionExcep } } else { Map headerMap = new HashMap<>(); - headerMap.put("Accept", "application/vc+ld+json"); + headerMap.put("Accept", "ap" + + "plication/vc+ld+json"); String rcTemplateApi = RegistryCredential.getSERVICE_BASE_URL().concat(RegistryCredential.getDOWNLOAD_URI())+"/"+certId; Future> rcResponseFuture=CertificateUtil.makeAsyncGetCall(rcTemplateApi,headerMap); - HttpResponse rcJsonResponse = rcResponseFuture.get(); - if (rcJsonResponse != null && rcJsonResponse.getStatus() == HttpStatus.SC_OK) { - String templateUrl = rcJsonResponse.getBody().getObject().getJSONObject(JsonKeys.RESULT).getString("templateurl"); - String rcDownloadApi = RegistryCredential.getSERVICE_BASE_URL().concat(RegistryCredential.getDOWNLOAD_URI())+"/"+certId; - headerMap.put("Accept", "image/svg+xml"); - headerMap.put("templateurl", templateUrl); - Future> rcDownloadResFuture=CertificateUtil.makeAsyncGetCall(rcDownloadApi,headerMap); - HttpResponse rcDownloadJsonResponse = rcDownloadResFuture.get(); - printUri = rcDownloadJsonResponse.getBody().getObject().getJSONObject(JsonKeys.RESULT).getString(JsonKeys.PRINT_URI); - response.put(JsonKeys.PRINT_URI, printUri); - } else { + try { + HttpResponse rcJsonResponse = rcResponseFuture.get(); + if (rcJsonResponse != null && rcJsonResponse.getStatus() == HttpStatus.SC_OK) { + String templateUrl = rcJsonResponse.getBody().getObject().getJSONObject(JsonKeys.RESULT).getString("templateurl"); + String rcDownloadApi = RegistryCredential.getSERVICE_BASE_URL().concat(RegistryCredential.getDOWNLOAD_URI()) + "/" + certId; + headerMap.put("Accept", "image/svg+xml"); + headerMap.put("templateurl", templateUrl); + Future> rcDownloadResFuture = CertificateUtil.makeAsyncGetCall(rcDownloadApi, headerMap); + HttpResponse rcDownloadJsonResponse = rcDownloadResFuture.get(); + printUri = rcDownloadJsonResponse.getBody().getObject().getJSONObject(JsonKeys.RESULT).getString(JsonKeys.PRINT_URI); + response.put(JsonKeys.PRINT_URI, printUri); + } else { + throw new BaseException(IResponseMessage.RESOURCE_NOT_FOUND, localizer.getMessage(IResponseMessage.RESOURCE_NOT_FOUND, null), ResponseCode.RESOURCE_NOT_FOUND.getCode()); + } + } catch (ExecutionException | InterruptedException e) { throw new BaseException(IResponseMessage.RESOURCE_NOT_FOUND, localizer.getMessage(IResponseMessage.RESOURCE_NOT_FOUND, null), ResponseCode.RESOURCE_NOT_FOUND.getCode()); } } @@ -392,14 +396,16 @@ private static String getLocalizedMessage(String key, Locale locale){ @Override public Response search(Request request) throws BaseException{ Response response = new Response(); + Map headerMap = new HashMap<>(); + headerMap.put("Content-Type", "application/json"); try { String requestBody = requestMapper.writeValueAsString(request.getRequest()); logger.info("CertsServiceImpl:search:request body found."); String apiToCall = CertVars.getEsSearchUri(); logger.info("CertsServiceImpl:search:complete url found:" + apiToCall); - String rcSearchApiCall = RegistryCredential.getEsSearchUri(); - logger.info("RegistryCredential:search:complete url found:" + rcSearchApiCall); + String rcSearchApiCall = RegistryCredential.getRCSearchUri(); + logger.info("RegistryCredential:rcSearchApiCall:complete url found:" + rcSearchApiCall); Future> responseFuture = CertificateUtil.makeAsyncPostCall(apiToCall, requestBody, headerMap); HttpResponse jsonResponse = responseFuture.get(); ESResponseMapper mappedResponse = null; @@ -423,7 +429,7 @@ public Response search(Request request) throws BaseException{ filters.put(JsonKeys.FILTERS, fieldKeyMap); Future> rcResponseFuture = CertificateUtil.makeAsyncPostCall(rcSearchApiCall, requestBody, headerMap); HttpResponse rcJsonResponse = rcResponseFuture.get(); - if ((mappedResponse !=null && rcJsonResponse != null) && rcJsonResponse.getStatus() == HttpStatus.SC_OK) { + if ((mappedResponse != null && rcJsonResponse != null) && rcJsonResponse.getStatus() == HttpStatus.SC_OK) { String rcJsonArray = rcJsonResponse.getBody().getObject().getJSONObject(JsonKeys.HITS).toString(); Map rcSearchApiResp = requestMapper.readValue(rcJsonArray, Map.class); ESResponseMapper rcmappedResponse = new ObjectMapper().convertValue(rcSearchApiResp, ESResponseMapper.class); @@ -440,4 +446,29 @@ public Response search(Request request) throws BaseException{ } return response; } + + @Override + public Response searchV2(Request request) throws BaseException{ + Response response = new Response(); + try { + String requestBody = requestMapper.writeValueAsString(request.getRequest()); + logger.info("CertsServiceImpl:search:request body found."); + String apiToCall = CertVars.getEsSearchUri(); + logger.info("CertsServiceImpl:search:complete url found:" + apiToCall); + Future> responseFuture = CertificateUtil.makeAsyncPostCall(apiToCall, requestBody, headerMap); + HttpResponse jsonResponse = responseFuture.get(); + if (jsonResponse != null && jsonResponse.getStatus() == HttpStatus.SC_OK) { + String jsonArray = jsonResponse.getBody().getObject().getJSONObject(JsonKeys.HITS).toString(); + Map apiResp = requestMapper.readValue(jsonArray, Map.class); + ESResponseMapper mappedResponse = new ObjectMapper().convertValue(apiResp, ESResponseMapper.class); + response.put(JsonKeys.RESPONSE, mappedResponse); + } else { + throw new BaseException(IResponseMessage.INVALID_REQUESTED_DATA,jsonResponse.getBody().toString(), ResponseCode.CLIENT_ERROR.getCode()); + } + } catch (Exception e) { + logger.error("CertsServiceImpl:search:exception occurred:" + e); + throw new BaseException(IResponseMessage.INTERNAL_ERROR, IResponseMessage.INTERNAL_ERROR, ResponseCode.SERVER_ERROR.getCode()); + } + return response; + } } \ No newline at end of file diff --git a/sb-utils/src/main/java/org.sunbird/JsonKeys.java b/sb-utils/src/main/java/org.sunbird/JsonKeys.java index 004bc28..c10b100 100644 --- a/sb-utils/src/main/java/org.sunbird/JsonKeys.java +++ b/sb-utils/src/main/java/org.sunbird/JsonKeys.java @@ -58,6 +58,7 @@ public class JsonKeys { public static final String OLD_ID="oldId"; public static final String READ = "read"; public static final String SEARCH = "search"; + public static final String SEARCH_V2 = "searchV2"; public static final String HITS ="hits"; public static final String CERT_ALIAS ="certs"; public static final String READ_CERT_META_DATA = "readCertMetaData"; diff --git a/service/app/controllers/CertificateController.java b/service/app/controllers/CertificateController.java index d644d76..c930245 100644 --- a/service/app/controllers/CertificateController.java +++ b/service/app/controllers/CertificateController.java @@ -150,5 +150,16 @@ public CompletionStage search(Http.Request httpRequest) return null; }, JsonKeys.SEARCH); } + + public CompletionStage searchV2(Http.Request httpRequest) + { + IRequestValidator requestValidator=new CertSearchRequestValidator(); + return handleRequest(certificationActorRef, httpRequest, + request -> { + Request req = (Request) request; + requestValidator.validate(req); + return null; + }, JsonKeys.SEARCH_V2); + } } diff --git a/service/conf/routes b/service/conf/routes index 87e2df3..93e0e27 100755 --- a/service/conf/routes +++ b/service/conf/routes @@ -16,7 +16,7 @@ GET /certs/v2/registry/download/:id @controllers.Certificat POST /certs/v1/registry/verify @controllers.CertificateController.verify(request: play.mvc.Http.Request) GET /certs/v1/registry/read/:id @controllers.CertificateController.read(id:String, request: play.mvc.Http.Request) POST /certs/v1/registry/search @controllers.CertificateController.search(request: play.mvc.Http.Request) -POST /certs/v2/registry/search @controllers.CertificateController.search(request: play.mvc.Http.Request) +POST /certs/v2/registry/search @controllers.CertificateController.searchV2(request: play.mvc.Http.Request) From cfa9460c9da73eb94801a046a203fb39c0be0104 Mon Sep 17 00:00:00 2001 From: Harikumar Palemkota Date: Thu, 17 Mar 2022 13:04:39 +0530 Subject: [PATCH 14/39] code review changes --- .../src/main/java/org/sunbird/CertVars.java | 2 +- .../java/org/sunbird/RegistryCredential.java | 9 ++- .../sunbird/serviceimpl/CertsServiceImpl.java | 70 +++++++++---------- .../sunbird/utilities/CertificateUtil.java | 9 +++ .../sunbird/utilities/ESResponseMapper.java | 2 + .../src/main/java/org.sunbird/JsonKeys.java | 10 ++- 6 files changed, 60 insertions(+), 42 deletions(-) diff --git a/all-actors/src/main/java/org/sunbird/CertVars.java b/all-actors/src/main/java/org/sunbird/CertVars.java index 7bceb13..bc333a3 100644 --- a/all-actors/src/main/java/org/sunbird/CertVars.java +++ b/all-actors/src/main/java/org/sunbird/CertVars.java @@ -43,7 +43,7 @@ private static String getPropsFromEnvs(String props){ public static String getEsSearchUri(){ String esApi=String.format("http://%s:9200/%s/_search",getPropsFromEnvs(SUNBIRD_ES_IP).split(",")[0],JsonKeys.CERT_ALIAS); - logger.info("CertVars:getRCSearchUri:es uri formed:"+esApi); + logger.info("CertVars:getEsSearchUri:es uri formed:"+esApi); return esApi; } } diff --git a/all-actors/src/main/java/org/sunbird/RegistryCredential.java b/all-actors/src/main/java/org/sunbird/RegistryCredential.java index ee19dfd..473cf35 100644 --- a/all-actors/src/main/java/org/sunbird/RegistryCredential.java +++ b/all-actors/src/main/java/org/sunbird/RegistryCredential.java @@ -9,11 +9,10 @@ * @author anmolgupta */ public class RegistryCredential { - - public static final String REGISTRY_CREDENTIAL_SERVICE_BASE_URL= "registry_credential_service_base_url"; + private static Logger logger= LoggerFactory.getLogger(RegistryCredential.class); - private static final String SERVICE_BASE_URL = getPropsFromEnvs(REGISTRY_CREDENTIAL_SERVICE_BASE_URL); - private static final String CERTIFICATE_TYPE = getPropsFromEnvs("certificate_type"); + private static final String SERVICE_BASE_URL = getPropsFromEnvs(JsonKeys.REGISTRY_CREDENTIAL_SERVICE_BASE_URL); + private static final String CERTIFICATE_TYPE = getPropsFromEnvs(JsonKeys.RC_ENTITY); private static final String API = "/api/v1/%s"; public static String getSERVICE_BASE_URL() { @@ -35,7 +34,7 @@ private static String getPropsFromEnvs(String props){ public static String getRCSearchUri(){ String apiUrl = String.format(API, CERTIFICATE_TYPE); - String rcSearchApi = "https://dev.sunbirded.org/api/rc/certificate/v1/search";//String.format("http://%s/%s/search", getSERVICE_BASE_URL().split(",")[0], apiUrl); + String rcSearchApi = String.format("http://%s/%s/search", getSERVICE_BASE_URL().split(",")[0], apiUrl); logger.info("RegistryCredential:getRCSearchUri:es uri formed: "+rcSearchApi); return rcSearchApi; } diff --git a/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java b/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java index dbdf166..6f0128c 100644 --- a/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java +++ b/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java @@ -247,20 +247,19 @@ public Response downloadV2(Request request) throws BaseException { } } else { Map headerMap = new HashMap<>(); - headerMap.put("Accept", "ap" + - "plication/vc+ld+json"); + headerMap.put(JsonKeys.ACCEPT, JsonKeys.APPLICATION_VC_LD_JSON); String rcTemplateApi = RegistryCredential.getSERVICE_BASE_URL().concat(RegistryCredential.getDOWNLOAD_URI())+"/"+certId; Future> rcResponseFuture=CertificateUtil.makeAsyncGetCall(rcTemplateApi,headerMap); try { HttpResponse rcJsonResponse = rcResponseFuture.get(); if (rcJsonResponse != null && rcJsonResponse.getStatus() == HttpStatus.SC_OK) { - String templateUrl = rcJsonResponse.getBody().getObject().getJSONObject(JsonKeys.RESULT).getString("templateurl"); + String templateUrl = rcJsonResponse.getBody().getObject().getJSONObject(JsonKeys.RESULT).getString(JsonKeys.TEMPLATE_URL); String rcDownloadApi = RegistryCredential.getSERVICE_BASE_URL().concat(RegistryCredential.getDOWNLOAD_URI()) + "/" + certId; - headerMap.put("Accept", "image/svg+xml"); + headerMap.put(JsonKeys.ACCEPT, JsonKeys.IMAGE_SVG_XML); headerMap.put("templateurl", templateUrl); - Future> rcDownloadResFuture = CertificateUtil.makeAsyncGetCall(rcDownloadApi, headerMap); - HttpResponse rcDownloadJsonResponse = rcDownloadResFuture.get(); - printUri = rcDownloadJsonResponse.getBody().getObject().getJSONObject(JsonKeys.RESULT).getString(JsonKeys.PRINT_URI); + Future> rcDownloadResFuture = CertificateUtil.makeAsyncGetCallString(rcDownloadApi, headerMap); + HttpResponse rcDownloadJsonResponse = rcDownloadResFuture.get(); + printUri = rcDownloadJsonResponse.getBody(); response.put(JsonKeys.PRINT_URI, printUri); } else { throw new BaseException(IResponseMessage.RESOURCE_NOT_FOUND, localizer.getMessage(IResponseMessage.RESOURCE_NOT_FOUND, null), ResponseCode.RESOURCE_NOT_FOUND.getCode()); @@ -397,27 +396,16 @@ private static String getLocalizedMessage(String key, Locale locale){ public Response search(Request request) throws BaseException{ Response response = new Response(); Map headerMap = new HashMap<>(); - headerMap.put("Content-Type", "application/json"); + ObjectMapper mapper = new ObjectMapper(); + headerMap.put(JsonKeys.CONTENT_TYPE, JsonKeys.APPLICATION_JSON); try { - String requestBody = requestMapper.writeValueAsString(request.getRequest()); - logger.info("CertsServiceImpl:search:request body found."); - String apiToCall = CertVars.getEsSearchUri(); - logger.info("CertsServiceImpl:search:complete url found:" + apiToCall); - + ESResponseMapper mappedResponse = null; + mappedResponse = searchEsPostCall(request); String rcSearchApiCall = RegistryCredential.getRCSearchUri(); logger.info("RegistryCredential:rcSearchApiCall:complete url found:" + rcSearchApiCall); - Future> responseFuture = CertificateUtil.makeAsyncPostCall(apiToCall, requestBody, headerMap); - HttpResponse jsonResponse = responseFuture.get(); - ESResponseMapper mappedResponse = null; - if (jsonResponse != null && jsonResponse.getStatus() == HttpStatus.SC_OK) { - String jsonArray = jsonResponse.getBody().getObject().getJSONObject(JsonKeys.HITS).toString(); - Map apiResp = requestMapper.readValue(jsonArray, Map.class); - mappedResponse = new ObjectMapper().convertValue(apiResp, ESResponseMapper.class); - } Map req = request.getRequest(); - ObjectMapper mapper = new ObjectMapper(); ObjectNode jsonNode = mapper.convertValue(req, ObjectNode.class); - ObjectNode jsonNode1 = (ObjectNode) jsonNode.at("/query/match_phrase/"); + ObjectNode jsonNode1 = (ObjectNode) jsonNode.at(JsonKeys.QUERY_MATCH_PHRASE); Iterator> fields = jsonNode1.fields(); Map filters = new HashMap<>(); Map fieldKeyMap = new HashMap<>(); @@ -427,16 +415,22 @@ public Response search(Request request) throws BaseException{ fieldKeyMap.put(field.getKey(), fieldValueMap); }); filters.put(JsonKeys.FILTERS, fieldKeyMap); - Future> rcResponseFuture = CertificateUtil.makeAsyncPostCall(rcSearchApiCall, requestBody, headerMap); + String filterString = mapper.writeValueAsString(filters); + Future> rcResponseFuture = CertificateUtil.makeAsyncPostCall(rcSearchApiCall, filterString, headerMap); HttpResponse rcJsonResponse = rcResponseFuture.get(); - if ((mappedResponse != null && rcJsonResponse != null) && rcJsonResponse.getStatus() == HttpStatus.SC_OK) { - String rcJsonArray = rcJsonResponse.getBody().getObject().getJSONObject(JsonKeys.HITS).toString(); - Map rcSearchApiResp = requestMapper.readValue(rcJsonArray, Map.class); - ESResponseMapper rcmappedResponse = new ObjectMapper().convertValue(rcSearchApiResp, ESResponseMapper.class); - mappedResponse.setCount(mappedResponse.getCount() + rcmappedResponse.getCount()); - mappedResponse.getContent().addAll(rcmappedResponse.getContent()); + if (rcJsonResponse != null && rcJsonResponse.getStatus() == HttpStatus.SC_OK) { + String rcJsonArray = rcJsonResponse.getBody().getArray().toString(); + List> rcSearchApiResp = requestMapper.readValue(rcJsonArray, List.class); + if(mappedResponse != null) { + mappedResponse.setCount(mappedResponse.getCount() + rcSearchApiResp.size()); + mappedResponse.getContent().addAll(rcSearchApiResp); + } else { + mappedResponse = new ESResponseMapper(); + mappedResponse.setCount(rcSearchApiResp.size()); + mappedResponse.setContent(rcSearchApiResp); + } } else { - throw new BaseException(IResponseMessage.INVALID_REQUESTED_DATA,jsonResponse.getBody().toString(), ResponseCode.CLIENT_ERROR.getCode()); + throw new BaseException(IResponseMessage.INVALID_REQUESTED_DATA,rcJsonResponse.getBody().toString(), ResponseCode.CLIENT_ERROR.getCode()); } response.put(JsonKeys.RESPONSE, mappedResponse); @@ -450,6 +444,13 @@ public Response search(Request request) throws BaseException{ @Override public Response searchV2(Request request) throws BaseException{ Response response = new Response(); + ESResponseMapper mappedResponse = searchEsPostCall(request); + response.put(JsonKeys.RESPONSE, mappedResponse); + return response; + } + + private ESResponseMapper searchEsPostCall(Request request) throws BaseException { + ESResponseMapper mappedResponse = null; try { String requestBody = requestMapper.writeValueAsString(request.getRequest()); logger.info("CertsServiceImpl:search:request body found."); @@ -460,15 +461,14 @@ public Response searchV2(Request request) throws BaseException{ if (jsonResponse != null && jsonResponse.getStatus() == HttpStatus.SC_OK) { String jsonArray = jsonResponse.getBody().getObject().getJSONObject(JsonKeys.HITS).toString(); Map apiResp = requestMapper.readValue(jsonArray, Map.class); - ESResponseMapper mappedResponse = new ObjectMapper().convertValue(apiResp, ESResponseMapper.class); - response.put(JsonKeys.RESPONSE, mappedResponse); + mappedResponse = new ObjectMapper().convertValue(apiResp, ESResponseMapper.class); } else { - throw new BaseException(IResponseMessage.INVALID_REQUESTED_DATA,jsonResponse.getBody().toString(), ResponseCode.CLIENT_ERROR.getCode()); + throw new BaseException(IResponseMessage.INVALID_REQUESTED_DATA, jsonResponse.getBody().toString(), ResponseCode.CLIENT_ERROR.getCode()); } } catch (Exception e) { logger.error("CertsServiceImpl:search:exception occurred:" + e); throw new BaseException(IResponseMessage.INTERNAL_ERROR, IResponseMessage.INTERNAL_ERROR, ResponseCode.SERVER_ERROR.getCode()); } - return response; + return mappedResponse; } } \ No newline at end of file diff --git a/all-actors/src/main/java/org/sunbird/utilities/CertificateUtil.java b/all-actors/src/main/java/org/sunbird/utilities/CertificateUtil.java index f578808..ef7fc37 100644 --- a/all-actors/src/main/java/org/sunbird/utilities/CertificateUtil.java +++ b/all-actors/src/main/java/org/sunbird/utilities/CertificateUtil.java @@ -129,6 +129,15 @@ public static Future> makeAsyncPostCall(String apiToCall, return jsonResponse; } + public static Future> makeAsyncGetCallString(String apiToCall, MapheaderMap){ + logger.info("CertificateUtil:makePostCall:get request to make post call for API:"+apiToCall); + Future> jsonResponse + = Unirest.get(apiToCall) + .headers(headerMap) + .asStringAsync(); + return jsonResponse; + } + public static Future> makeAsyncGetCall(String apiToCall, MapheaderMap){ logger.info("CertificateUtil:makePostCall:get request to make post call for API:"+apiToCall); Future> jsonResponse diff --git a/all-actors/src/main/java/org/sunbird/utilities/ESResponseMapper.java b/all-actors/src/main/java/org/sunbird/utilities/ESResponseMapper.java index 48a0625..1aa8d21 100644 --- a/all-actors/src/main/java/org/sunbird/utilities/ESResponseMapper.java +++ b/all-actors/src/main/java/org/sunbird/utilities/ESResponseMapper.java @@ -19,6 +19,8 @@ public ESResponseMapper( this.content = content; this.count = count; } + + public ESResponseMapper() {} public List> getContent() { return content; diff --git a/sb-utils/src/main/java/org.sunbird/JsonKeys.java b/sb-utils/src/main/java/org.sunbird/JsonKeys.java index c10b100..54a1cd5 100644 --- a/sb-utils/src/main/java/org.sunbird/JsonKeys.java +++ b/sb-utils/src/main/java/org.sunbird/JsonKeys.java @@ -76,6 +76,14 @@ public class JsonKeys { public static final String REQ_ID ="reqId"; public static final String REQUEST_MESSAGE_ID = "msgId"; public static final String X_REQUEST_ID = "X-Request-ID"; - + public static final String ACCEPT = "Accept"; + public static final String CONTENT_TYPE = "Content-Type"; + public static final String IMAGE_SVG_XML = "image/svg+xml"; + public static final String APPLICATION_VC_LD_JSON = "application/vc+ld+json"; + public static final String TEMPLATE_URL = "templateUrl"; + public static final String REGISTRY_CREDENTIAL_SERVICE_BASE_URL= "registry_credential_service_base_url"; + public static final String RC_ENTITY = "rc_entity"; + public static final String APPLICATION_JSON = "application/json"; + public static final String QUERY_MATCH_PHRASE = "/query/match_phrase"; public static final String FILTERS = "filters"; } From 3304d0810e3047cb5079a63dbd4e21b813f81870 Mon Sep 17 00:00:00 2001 From: Harikumar Palemkota Date: Thu, 17 Mar 2022 18:36:25 +0530 Subject: [PATCH 15/39] Modified test-cases --- all-actors/src/main/java/org/sunbird/ActorOperations.java | 1 + .../test/java/org/sunbird/actor/CertificationActorTest.java | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/all-actors/src/main/java/org/sunbird/ActorOperations.java b/all-actors/src/main/java/org/sunbird/ActorOperations.java index 79bbdf8..f0a1150 100644 --- a/all-actors/src/main/java/org/sunbird/ActorOperations.java +++ b/all-actors/src/main/java/org/sunbird/ActorOperations.java @@ -15,6 +15,7 @@ public enum ActorOperations { DELETE_CERT_CASSANDRA("delete_cert_cassandra"), READ("read"), SEARCH("search"), + SEARCHV2("searchV2"), READ_CERT_META_DATA("readCertMetaData"), DOWNLOADV2("downloadV2"); diff --git a/all-actors/src/test/java/org/sunbird/actor/CertificationActorTest.java b/all-actors/src/test/java/org/sunbird/actor/CertificationActorTest.java index 731ee11..4d824e3 100644 --- a/all-actors/src/test/java/org/sunbird/actor/CertificationActorTest.java +++ b/all-actors/src/test/java/org/sunbird/actor/CertificationActorTest.java @@ -116,6 +116,10 @@ public void beforeTestSetUp() throws Exception { String signedUrl ="http://localhost:9000/dev-e-credentials/0125450863553740809/4d88c2e4-212b-4c00-aa83-1cd3fde7b447.json"; when(object2.getString(JsonKeys.SIGNED_URL)).thenReturn(signedUrl); when(object2.get(JsonKeys.RESPONSE)).thenReturn(map); + + final Future> mockedGetFuture = Mockito.mock(Future.class); + when(CertificateUtil.makeAsyncGetCall(Mockito.anyString(),Mockito.anyMap())).thenReturn(mockedGetFuture); + when(mockedGetFuture.get()).thenReturn(null); when(certsService.download(Mockito.any(Request.class))).thenReturn(getValidateCertResponse()); when(CertificateUtil.getCertificate(Mockito.anyString())).thenReturn(map); @@ -191,7 +195,7 @@ public void testReadCertificate() throws Exception { @Test public void testSearchCertificate() throws Exception { Request request = createDownloadCertRequest(); - request.setOperation(ActorOperations.SEARCH.getOperation()); + request.setOperation(ActorOperations.SEARCHV2.getOperation()); beforeTestSetUp(); TestKit testKit = new TestKit(system); ActorRef actorRef = system.actorOf(props); From e0dd648402c8d7d398eb4f7ca7c608acbdf213b3 Mon Sep 17 00:00:00 2001 From: Harikumar Palemkota Date: Thu, 17 Mar 2022 18:57:59 +0530 Subject: [PATCH 16/39] Modified test-cases --- .../java/org/sunbird/actor/CertificationActorTest.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/all-actors/src/test/java/org/sunbird/actor/CertificationActorTest.java b/all-actors/src/test/java/org/sunbird/actor/CertificationActorTest.java index 4d824e3..43f76a7 100644 --- a/all-actors/src/test/java/org/sunbird/actor/CertificationActorTest.java +++ b/all-actors/src/test/java/org/sunbird/actor/CertificationActorTest.java @@ -20,10 +20,7 @@ import org.powermock.core.classloader.annotations.PowerMockIgnore; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; -import org.sunbird.ActorOperations; -import org.sunbird.BaseException; -import org.sunbird.CertVars; -import org.sunbird.JsonKeys; +import org.sunbird.*; import org.sunbird.cassandra.CassandraOperation; import org.sunbird.cassandraimpl.CassandraDACImpl; import org.sunbird.common.ElasticSearchRestHighImpl; @@ -62,6 +59,7 @@ CassandraOperation.class, CassandraDACImpl.class, CertVars.class, + RegistryCredential.class, IOUtils.class, URL.class}) @PowerMockIgnore({"javax.management.*", "jdk.internal.reflect.*"}) @@ -78,6 +76,7 @@ public static void setUp() throws Exception { JSONObject object2 = null; public void beforeTestSetUp() throws Exception { PowerMockito.mockStatic(CertVars.class); + PowerMockito.mockStatic(RegistryCredential.class); PowerMockito.mockStatic(EsClientFactory.class); ElasticSearchRestHighImpl elasticSearchRestHigh = PowerMockito.mock(ElasticSearchRestHighImpl.class); PowerMockito.whenNew(ElasticSearchRestHighImpl.class).withNoArguments().thenReturn(elasticSearchRestHigh); @@ -99,6 +98,9 @@ public void beforeTestSetUp() throws Exception { when(CertVars.getSERVICE_BASE_URL()).thenReturn("service_base_url"); when(CertVars.getDOWNLOAD_URI()).thenReturn("download_url"); when(CertVars.getEsSearchUri()).thenReturn("es_search_uri"); + when(RegistryCredential.getSERVICE_BASE_URL()).thenReturn("service_base_url"); + when(RegistryCredential.getDOWNLOAD_URI()).thenReturn("download_url"); + when(RegistryCredential.getRCSearchUri()).thenReturn("es_search_uri"); final Future> mockedFuture = Mockito.mock(Future.class); when(CertificateUtil.makeAsyncPostCall(Mockito.anyString(),Mockito.anyString(),Mockito.anyMap())).thenReturn(mockedFuture); final HttpResponse mockedResponse = Mockito.mock(HttpResponse.class); From dd058c7804201b751429ab7d2c108db810f6a5aa Mon Sep 17 00:00:00 2001 From: Harikumar Palemkota Date: Fri, 18 Mar 2022 09:33:10 +0530 Subject: [PATCH 17/39] SB-29068 download api changes --- .../org/sunbird/serviceimpl/CertsServiceImpl.java | 11 +++++------ sb-utils/src/main/java/org.sunbird/JsonKeys.java | 1 - 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java b/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java index 6f0128c..51d1ee6 100644 --- a/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java +++ b/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java @@ -247,17 +247,16 @@ public Response downloadV2(Request request) throws BaseException { } } else { Map headerMap = new HashMap<>(); - headerMap.put(JsonKeys.ACCEPT, JsonKeys.APPLICATION_VC_LD_JSON); - String rcTemplateApi = RegistryCredential.getSERVICE_BASE_URL().concat(RegistryCredential.getDOWNLOAD_URI())+"/"+certId; - Future> rcResponseFuture=CertificateUtil.makeAsyncGetCall(rcTemplateApi,headerMap); + headerMap.put(JsonKeys.ACCEPT, JsonKeys.APPLICATION_JSON); + String rcApi = RegistryCredential.getSERVICE_BASE_URL().concat(RegistryCredential.getDOWNLOAD_URI())+"/"+certId; + Future> rcResponseFuture=CertificateUtil.makeAsyncGetCall(rcApi,headerMap); try { HttpResponse rcJsonResponse = rcResponseFuture.get(); if (rcJsonResponse != null && rcJsonResponse.getStatus() == HttpStatus.SC_OK) { - String templateUrl = rcJsonResponse.getBody().getObject().getJSONObject(JsonKeys.RESULT).getString(JsonKeys.TEMPLATE_URL); - String rcDownloadApi = RegistryCredential.getSERVICE_BASE_URL().concat(RegistryCredential.getDOWNLOAD_URI()) + "/" + certId; + String templateUrl = rcJsonResponse.getBody().getObject().getString(JsonKeys.TEMPLATE_URL); headerMap.put(JsonKeys.ACCEPT, JsonKeys.IMAGE_SVG_XML); headerMap.put("templateurl", templateUrl); - Future> rcDownloadResFuture = CertificateUtil.makeAsyncGetCallString(rcDownloadApi, headerMap); + Future> rcDownloadResFuture = CertificateUtil.makeAsyncGetCallString(rcApi, headerMap); HttpResponse rcDownloadJsonResponse = rcDownloadResFuture.get(); printUri = rcDownloadJsonResponse.getBody(); response.put(JsonKeys.PRINT_URI, printUri); diff --git a/sb-utils/src/main/java/org.sunbird/JsonKeys.java b/sb-utils/src/main/java/org.sunbird/JsonKeys.java index 54a1cd5..98bb44e 100644 --- a/sb-utils/src/main/java/org.sunbird/JsonKeys.java +++ b/sb-utils/src/main/java/org.sunbird/JsonKeys.java @@ -79,7 +79,6 @@ public class JsonKeys { public static final String ACCEPT = "Accept"; public static final String CONTENT_TYPE = "Content-Type"; public static final String IMAGE_SVG_XML = "image/svg+xml"; - public static final String APPLICATION_VC_LD_JSON = "application/vc+ld+json"; public static final String TEMPLATE_URL = "templateUrl"; public static final String REGISTRY_CREDENTIAL_SERVICE_BASE_URL= "registry_credential_service_base_url"; public static final String RC_ENTITY = "rc_entity"; From 4311ba9ac1426d19733ee3c4fcbd1a68ea45b2d3 Mon Sep 17 00:00:00 2001 From: Harikumar Palemkota Date: Fri, 18 Mar 2022 12:39:59 +0530 Subject: [PATCH 18/39] search is throwing error --- .../src/main/java/org/sunbird/RegistryCredential.java | 2 +- .../java/org/sunbird/serviceimpl/CertsServiceImpl.java | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/all-actors/src/main/java/org/sunbird/RegistryCredential.java b/all-actors/src/main/java/org/sunbird/RegistryCredential.java index 473cf35..59270fe 100644 --- a/all-actors/src/main/java/org/sunbird/RegistryCredential.java +++ b/all-actors/src/main/java/org/sunbird/RegistryCredential.java @@ -13,7 +13,7 @@ public class RegistryCredential { private static Logger logger= LoggerFactory.getLogger(RegistryCredential.class); private static final String SERVICE_BASE_URL = getPropsFromEnvs(JsonKeys.REGISTRY_CREDENTIAL_SERVICE_BASE_URL); private static final String CERTIFICATE_TYPE = getPropsFromEnvs(JsonKeys.RC_ENTITY); - private static final String API = "/api/v1/%s"; + private static final String API = "api/v1/%s"; public static String getSERVICE_BASE_URL() { if(StringUtils.isBlank(SERVICE_BASE_URL)){ diff --git a/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java b/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java index 51d1ee6..55cf5b1 100644 --- a/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java +++ b/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java @@ -401,7 +401,7 @@ public Response search(Request request) throws BaseException{ ESResponseMapper mappedResponse = null; mappedResponse = searchEsPostCall(request); String rcSearchApiCall = RegistryCredential.getRCSearchUri(); - logger.info("RegistryCredential:rcSearchApiCall:complete url found:" + rcSearchApiCall); + logger.info("RegistryCredential:rcSearchApiCall:complete url found: " + rcSearchApiCall); Map req = request.getRequest(); ObjectNode jsonNode = mapper.convertValue(req, ObjectNode.class); ObjectNode jsonNode1 = (ObjectNode) jsonNode.at(JsonKeys.QUERY_MATCH_PHRASE); @@ -429,12 +429,13 @@ public Response search(Request request) throws BaseException{ mappedResponse.setContent(rcSearchApiResp); } } else { + logger.info("CertsServiceImpl:search:exception occurred "); throw new BaseException(IResponseMessage.INVALID_REQUESTED_DATA,rcJsonResponse.getBody().toString(), ResponseCode.CLIENT_ERROR.getCode()); } response.put(JsonKeys.RESPONSE, mappedResponse); } catch (Exception e) { - logger.error("CertsServiceImpl:search:exception occurred:" + e); + logger.error("CertsServiceImpl:search:exception occurred: " + e); throw new BaseException(IResponseMessage.INTERNAL_ERROR, IResponseMessage.INTERNAL_ERROR, ResponseCode.SERVER_ERROR.getCode()); } return response; @@ -454,7 +455,7 @@ private ESResponseMapper searchEsPostCall(Request request) throws BaseException String requestBody = requestMapper.writeValueAsString(request.getRequest()); logger.info("CertsServiceImpl:search:request body found."); String apiToCall = CertVars.getEsSearchUri(); - logger.info("CertsServiceImpl:search:complete url found:" + apiToCall); + logger.info("CertsServiceImpl:search:complete url found: " + apiToCall); Future> responseFuture = CertificateUtil.makeAsyncPostCall(apiToCall, requestBody, headerMap); HttpResponse jsonResponse = responseFuture.get(); if (jsonResponse != null && jsonResponse.getStatus() == HttpStatus.SC_OK) { @@ -465,7 +466,7 @@ private ESResponseMapper searchEsPostCall(Request request) throws BaseException throw new BaseException(IResponseMessage.INVALID_REQUESTED_DATA, jsonResponse.getBody().toString(), ResponseCode.CLIENT_ERROR.getCode()); } } catch (Exception e) { - logger.error("CertsServiceImpl:search:exception occurred:" + e); + logger.error("CertsServiceImpl:search:exception occurred: " + e); throw new BaseException(IResponseMessage.INTERNAL_ERROR, IResponseMessage.INTERNAL_ERROR, ResponseCode.SERVER_ERROR.getCode()); } return mappedResponse; From 952fcefa94ab3f120d4b59be1f5b8abb5f3f7d9f Mon Sep 17 00:00:00 2001 From: Harikumar Palemkota Date: Fri, 18 Mar 2022 15:27:52 +0530 Subject: [PATCH 19/39] added logs --- all-actors/src/main/java/org/sunbird/BaseActor.java | 2 +- .../java/org/sunbird/serviceimpl/CertsServiceImpl.java | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/all-actors/src/main/java/org/sunbird/BaseActor.java b/all-actors/src/main/java/org/sunbird/BaseActor.java index 4f83b14..151fa92 100644 --- a/all-actors/src/main/java/org/sunbird/BaseActor.java +++ b/all-actors/src/main/java/org/sunbird/BaseActor.java @@ -60,7 +60,7 @@ public void onReceive(Object message) throws Throwable { * @throws Exception */ protected void onReceiveException(String callerName, Exception exception) throws Exception { - logger.error("Exception in message processing for: " + callerName + " :: message: " + exception.getMessage(), exception); + logger.error("Exception in message processing for: " + callerName + " :: message: " + exception, exception); sender().tell(exception, self()); } diff --git a/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java b/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java index 55cf5b1..94c8f13 100644 --- a/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java +++ b/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java @@ -242,7 +242,7 @@ public Response downloadV2(Request request) throws BaseException { } response.put(JsonKeys.PRINT_URI, printUri); } catch (Exception e) { - logger.error("CertsServiceImpl:downloadV2:exception occurred:" + e); + logger.error("CertsServiceImpl:downloadV2:exception occurred: " + e); throw new BaseException(IResponseMessage.INTERNAL_ERROR, getLocalizedMessage(IResponseMessage.INTERNAL_ERROR, null), ResponseCode.SERVER_ERROR.getCode()); } } else { @@ -261,9 +261,11 @@ public Response downloadV2(Request request) throws BaseException { printUri = rcDownloadJsonResponse.getBody(); response.put(JsonKeys.PRINT_URI, printUri); } else { + logger.error("CertsServiceImpl:downloadV2:resource not found"); throw new BaseException(IResponseMessage.RESOURCE_NOT_FOUND, localizer.getMessage(IResponseMessage.RESOURCE_NOT_FOUND, null), ResponseCode.RESOURCE_NOT_FOUND.getCode()); } } catch (ExecutionException | InterruptedException e) { + logger.error("CertsServiceImpl:downloadV2:exception occurred while calling sunbird-rc registry: " + e); throw new BaseException(IResponseMessage.RESOURCE_NOT_FOUND, localizer.getMessage(IResponseMessage.RESOURCE_NOT_FOUND, null), ResponseCode.RESOURCE_NOT_FOUND.getCode()); } } @@ -429,7 +431,7 @@ public Response search(Request request) throws BaseException{ mappedResponse.setContent(rcSearchApiResp); } } else { - logger.info("CertsServiceImpl:search:exception occurred "); + logger.info("CertsServiceImpl:search:invalid request data"); throw new BaseException(IResponseMessage.INVALID_REQUESTED_DATA,rcJsonResponse.getBody().toString(), ResponseCode.CLIENT_ERROR.getCode()); } response.put(JsonKeys.RESPONSE, mappedResponse); @@ -463,10 +465,11 @@ private ESResponseMapper searchEsPostCall(Request request) throws BaseException Map apiResp = requestMapper.readValue(jsonArray, Map.class); mappedResponse = new ObjectMapper().convertValue(apiResp, ESResponseMapper.class); } else { + logger.error("CertsServiceImpl:searchEsPostCall: Invalid request data "); throw new BaseException(IResponseMessage.INVALID_REQUESTED_DATA, jsonResponse.getBody().toString(), ResponseCode.CLIENT_ERROR.getCode()); } } catch (Exception e) { - logger.error("CertsServiceImpl:search:exception occurred: " + e); + logger.error("CertsServiceImpl:searchEsPostCall:exception occurred: " + e); throw new BaseException(IResponseMessage.INTERNAL_ERROR, IResponseMessage.INTERNAL_ERROR, ResponseCode.SERVER_ERROR.getCode()); } return mappedResponse; From f10377cfcbe0520db8c46bc6fa82d34d710cd810 Mon Sep 17 00:00:00 2001 From: Harikumar Palemkota Date: Fri, 18 Mar 2022 15:47:06 +0530 Subject: [PATCH 20/39] search is throwing error --- all-actors/src/main/java/org/sunbird/RegistryCredential.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/all-actors/src/main/java/org/sunbird/RegistryCredential.java b/all-actors/src/main/java/org/sunbird/RegistryCredential.java index 59270fe..fb2986f 100644 --- a/all-actors/src/main/java/org/sunbird/RegistryCredential.java +++ b/all-actors/src/main/java/org/sunbird/RegistryCredential.java @@ -34,7 +34,7 @@ private static String getPropsFromEnvs(String props){ public static String getRCSearchUri(){ String apiUrl = String.format(API, CERTIFICATE_TYPE); - String rcSearchApi = String.format("http://%s/%s/search", getSERVICE_BASE_URL().split(",")[0], apiUrl); + String rcSearchApi = String.format("%s/%s/search", getSERVICE_BASE_URL().split(",")[0], apiUrl); logger.info("RegistryCredential:getRCSearchUri:es uri formed: "+rcSearchApi); return rcSearchApi; } From f1887964066417442849d3fcd12d626840c0c6ae Mon Sep 17 00:00:00 2001 From: Harikumar Palemkota Date: Fri, 18 Mar 2022 19:16:51 +0530 Subject: [PATCH 21/39] SB-29068 search api request body issue --- .../main/java/org/sunbird/serviceimpl/CertsServiceImpl.java | 3 ++- sb-utils/src/main/java/org.sunbird/JsonKeys.java | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java b/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java index 94c8f13..ecd31a4 100644 --- a/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java +++ b/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java @@ -3,6 +3,7 @@ import akka.actor.ActorRef; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; import com.mashape.unirest.http.HttpResponse; import com.mashape.unirest.http.JsonNode; @@ -406,7 +407,7 @@ public Response search(Request request) throws BaseException{ logger.info("RegistryCredential:rcSearchApiCall:complete url found: " + rcSearchApiCall); Map req = request.getRequest(); ObjectNode jsonNode = mapper.convertValue(req, ObjectNode.class); - ObjectNode jsonNode1 = (ObjectNode) jsonNode.at(JsonKeys.QUERY_MATCH_PHRASE); + ObjectNode jsonNode1 = (ObjectNode) jsonNode.at(JsonKeys.QUERY_MUST_PHRASE).get(0).at(JsonKeys.MATCH_PHRASE); Iterator> fields = jsonNode1.fields(); Map filters = new HashMap<>(); Map fieldKeyMap = new HashMap<>(); diff --git a/sb-utils/src/main/java/org.sunbird/JsonKeys.java b/sb-utils/src/main/java/org.sunbird/JsonKeys.java index 98bb44e..17fb57b 100644 --- a/sb-utils/src/main/java/org.sunbird/JsonKeys.java +++ b/sb-utils/src/main/java/org.sunbird/JsonKeys.java @@ -83,6 +83,7 @@ public class JsonKeys { public static final String REGISTRY_CREDENTIAL_SERVICE_BASE_URL= "registry_credential_service_base_url"; public static final String RC_ENTITY = "rc_entity"; public static final String APPLICATION_JSON = "application/json"; - public static final String QUERY_MATCH_PHRASE = "/query/match_phrase"; + public static final String QUERY_MUST_PHRASE = "/query/bool/must"; + public static final String MATCH_PHRASE = "/match_phrase"; public static final String FILTERS = "filters"; } From 730df7075234b4d984bbdb35a07f12e51fb81523 Mon Sep 17 00:00:00 2001 From: Harikumar Palemkota Date: Fri, 18 Mar 2022 19:18:52 +0530 Subject: [PATCH 22/39] SB-29068 search api request body issue --- .../src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java | 1 - 1 file changed, 1 deletion(-) diff --git a/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java b/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java index ecd31a4..ea51b5f 100644 --- a/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java +++ b/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java @@ -3,7 +3,6 @@ import akka.actor.ActorRef; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; import com.mashape.unirest.http.HttpResponse; import com.mashape.unirest.http.JsonNode; From 98e4362b7767789de941612d6bbddba3479ad55d Mon Sep 17 00:00:00 2001 From: Harikumar Palemkota Date: Mon, 21 Mar 2022 13:52:00 +0530 Subject: [PATCH 23/39] Adding logs to the download api --- .../main/java/org/sunbird/serviceimpl/CertsServiceImpl.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java b/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java index ea51b5f..88af70b 100644 --- a/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java +++ b/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java @@ -202,6 +202,11 @@ public Response download(Request request) throws BaseException { Future>responseFuture=CertificateUtil.makeAsyncPostCall(apiToCall,requestBody,headerMap); HttpResponse jsonResponse = responseFuture.get(); if (jsonResponse != null && jsonResponse.getStatus() == HttpStatus.SC_OK) { + if(jsonResponse.getBody().isArray()) { + logger.info("CertsServiceImpl:download:json response occurred:" + jsonResponse.getBody().getArray()); + } else { + logger.info("CertsServiceImpl:download:exception occurred:" + jsonResponse.getBody().getObject()); + } String signedUrl=jsonResponse.getBody().getObject().getJSONObject(JsonKeys.RESULT).getString(JsonKeys.SIGNED_URL); response.put(JsonKeys.SIGNED_URL,signedUrl); } else { From 47c54f638894d8d91dc34d797d43366d1f83cc8f Mon Sep 17 00:00:00 2001 From: Harikumar Palemkota Date: Mon, 21 Mar 2022 13:53:38 +0530 Subject: [PATCH 24/39] Adding logs to the download api --- .../main/java/org/sunbird/serviceimpl/CertsServiceImpl.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java b/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java index 88af70b..4eabb90 100644 --- a/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java +++ b/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java @@ -203,9 +203,9 @@ public Response download(Request request) throws BaseException { HttpResponse jsonResponse = responseFuture.get(); if (jsonResponse != null && jsonResponse.getStatus() == HttpStatus.SC_OK) { if(jsonResponse.getBody().isArray()) { - logger.info("CertsServiceImpl:download:json response occurred:" + jsonResponse.getBody().getArray()); + logger.info("CertsServiceImpl:download:json response : " + jsonResponse.getBody().getArray()); } else { - logger.info("CertsServiceImpl:download:exception occurred:" + jsonResponse.getBody().getObject()); + logger.info("CertsServiceImpl:download:json response : " + jsonResponse.getBody().getObject()); } String signedUrl=jsonResponse.getBody().getObject().getJSONObject(JsonKeys.RESULT).getString(JsonKeys.SIGNED_URL); response.put(JsonKeys.SIGNED_URL,signedUrl); From 221792407e50f51c61136582a47fec3a969dbb2e Mon Sep 17 00:00:00 2001 From: Harikumar Palemkota Date: Mon, 21 Mar 2022 22:38:09 +0530 Subject: [PATCH 25/39] Adding logs to the download api --- .../java/org/sunbird/serviceimpl/CertsServiceImpl.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java b/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java index 4eabb90..c989047 100644 --- a/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java +++ b/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java @@ -203,9 +203,9 @@ public Response download(Request request) throws BaseException { HttpResponse jsonResponse = responseFuture.get(); if (jsonResponse != null && jsonResponse.getStatus() == HttpStatus.SC_OK) { if(jsonResponse.getBody().isArray()) { - logger.info("CertsServiceImpl:download:json response : " + jsonResponse.getBody().getArray()); + logger.info("CertsServiceImpl:download:json response : " + jsonResponse.getBody().getArray().toString()); } else { - logger.info("CertsServiceImpl:download:json response : " + jsonResponse.getBody().getObject()); + logger.info("CertsServiceImpl:download:json response : " + jsonResponse.getBody().getObject().toString()); } String signedUrl=jsonResponse.getBody().getObject().getJSONObject(JsonKeys.RESULT).getString(JsonKeys.SIGNED_URL); response.put(JsonKeys.SIGNED_URL,signedUrl); @@ -407,7 +407,7 @@ public Response search(Request request) throws BaseException{ try { ESResponseMapper mappedResponse = null; mappedResponse = searchEsPostCall(request); - String rcSearchApiCall = RegistryCredential.getRCSearchUri(); + String rcSearchApiCall = "https://dev.sunbirded.org/api/certreg/v1/certs/search"; //RegistryCredential.getRCSearchUri(); logger.info("RegistryCredential:rcSearchApiCall:complete url found: " + rcSearchApiCall); Map req = request.getRequest(); ObjectNode jsonNode = mapper.convertValue(req, ObjectNode.class); @@ -422,6 +422,7 @@ public Response search(Request request) throws BaseException{ }); filters.put(JsonKeys.FILTERS, fieldKeyMap); String filterString = mapper.writeValueAsString(filters); + headerMap.put("Authorization", "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJlNmI4Nzg2MmVmNjg0MWU0OWE5YzM5MGEzOTZmNTk0NyJ9.WkIHjIn77-OkC48a3KresX5Bh21RtlwHobTWUDVPEsQ"); Future> rcResponseFuture = CertificateUtil.makeAsyncPostCall(rcSearchApiCall, filterString, headerMap); HttpResponse rcJsonResponse = rcResponseFuture.get(); if (rcJsonResponse != null && rcJsonResponse.getStatus() == HttpStatus.SC_OK) { @@ -471,7 +472,7 @@ private ESResponseMapper searchEsPostCall(Request request) throws BaseException mappedResponse = new ObjectMapper().convertValue(apiResp, ESResponseMapper.class); } else { logger.error("CertsServiceImpl:searchEsPostCall: Invalid request data "); - throw new BaseException(IResponseMessage.INVALID_REQUESTED_DATA, jsonResponse.getBody().toString(), ResponseCode.CLIENT_ERROR.getCode()); + //throw new BaseException(IResponseMessage.INVALID_REQUESTED_DATA, jsonResponse.getBody().toString(), ResponseCode.CLIENT_ERROR.getCode()); } } catch (Exception e) { logger.error("CertsServiceImpl:searchEsPostCall:exception occurred: " + e); From c20ee1bb5bbbcda8cccf9403064a8a9e58b8c2cc Mon Sep 17 00:00:00 2001 From: Harikumar Palemkota Date: Wed, 23 Mar 2022 11:17:14 +0530 Subject: [PATCH 26/39] SB-29068 download error rectified in staging --- .../src/main/java/org/sunbird/RegistryCredential.java | 2 +- .../java/org/sunbird/serviceimpl/CertsServiceImpl.java | 10 ++-------- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/all-actors/src/main/java/org/sunbird/RegistryCredential.java b/all-actors/src/main/java/org/sunbird/RegistryCredential.java index fb2986f..de6dcf8 100644 --- a/all-actors/src/main/java/org/sunbird/RegistryCredential.java +++ b/all-actors/src/main/java/org/sunbird/RegistryCredential.java @@ -13,7 +13,7 @@ public class RegistryCredential { private static Logger logger= LoggerFactory.getLogger(RegistryCredential.class); private static final String SERVICE_BASE_URL = getPropsFromEnvs(JsonKeys.REGISTRY_CREDENTIAL_SERVICE_BASE_URL); private static final String CERTIFICATE_TYPE = getPropsFromEnvs(JsonKeys.RC_ENTITY); - private static final String API = "api/v1/%s"; + private static final String API = "/api/v1/%s"; public static String getSERVICE_BASE_URL() { if(StringUtils.isBlank(SERVICE_BASE_URL)){ diff --git a/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java b/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java index c989047..ea51b5f 100644 --- a/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java +++ b/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java @@ -202,11 +202,6 @@ public Response download(Request request) throws BaseException { Future>responseFuture=CertificateUtil.makeAsyncPostCall(apiToCall,requestBody,headerMap); HttpResponse jsonResponse = responseFuture.get(); if (jsonResponse != null && jsonResponse.getStatus() == HttpStatus.SC_OK) { - if(jsonResponse.getBody().isArray()) { - logger.info("CertsServiceImpl:download:json response : " + jsonResponse.getBody().getArray().toString()); - } else { - logger.info("CertsServiceImpl:download:json response : " + jsonResponse.getBody().getObject().toString()); - } String signedUrl=jsonResponse.getBody().getObject().getJSONObject(JsonKeys.RESULT).getString(JsonKeys.SIGNED_URL); response.put(JsonKeys.SIGNED_URL,signedUrl); } else { @@ -407,7 +402,7 @@ public Response search(Request request) throws BaseException{ try { ESResponseMapper mappedResponse = null; mappedResponse = searchEsPostCall(request); - String rcSearchApiCall = "https://dev.sunbirded.org/api/certreg/v1/certs/search"; //RegistryCredential.getRCSearchUri(); + String rcSearchApiCall = RegistryCredential.getRCSearchUri(); logger.info("RegistryCredential:rcSearchApiCall:complete url found: " + rcSearchApiCall); Map req = request.getRequest(); ObjectNode jsonNode = mapper.convertValue(req, ObjectNode.class); @@ -422,7 +417,6 @@ public Response search(Request request) throws BaseException{ }); filters.put(JsonKeys.FILTERS, fieldKeyMap); String filterString = mapper.writeValueAsString(filters); - headerMap.put("Authorization", "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJlNmI4Nzg2MmVmNjg0MWU0OWE5YzM5MGEzOTZmNTk0NyJ9.WkIHjIn77-OkC48a3KresX5Bh21RtlwHobTWUDVPEsQ"); Future> rcResponseFuture = CertificateUtil.makeAsyncPostCall(rcSearchApiCall, filterString, headerMap); HttpResponse rcJsonResponse = rcResponseFuture.get(); if (rcJsonResponse != null && rcJsonResponse.getStatus() == HttpStatus.SC_OK) { @@ -472,7 +466,7 @@ private ESResponseMapper searchEsPostCall(Request request) throws BaseException mappedResponse = new ObjectMapper().convertValue(apiResp, ESResponseMapper.class); } else { logger.error("CertsServiceImpl:searchEsPostCall: Invalid request data "); - //throw new BaseException(IResponseMessage.INVALID_REQUESTED_DATA, jsonResponse.getBody().toString(), ResponseCode.CLIENT_ERROR.getCode()); + throw new BaseException(IResponseMessage.INVALID_REQUESTED_DATA, jsonResponse.getBody().toString(), ResponseCode.CLIENT_ERROR.getCode()); } } catch (Exception e) { logger.error("CertsServiceImpl:searchEsPostCall:exception occurred: " + e); From b87d6a463656a8354eb7911a743804648f2f1c91 Mon Sep 17 00:00:00 2001 From: Harikumar Palemkota Date: Wed, 23 Mar 2022 18:50:25 +0530 Subject: [PATCH 27/39] SB-29068 Correcting download url for v2 for sunbird-rc --- .../src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java b/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java index ea51b5f..9ae12e2 100644 --- a/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java +++ b/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java @@ -255,7 +255,7 @@ public Response downloadV2(Request request) throws BaseException { if (rcJsonResponse != null && rcJsonResponse.getStatus() == HttpStatus.SC_OK) { String templateUrl = rcJsonResponse.getBody().getObject().getString(JsonKeys.TEMPLATE_URL); headerMap.put(JsonKeys.ACCEPT, JsonKeys.IMAGE_SVG_XML); - headerMap.put("templateurl", templateUrl); + headerMap.put("template", templateUrl); Future> rcDownloadResFuture = CertificateUtil.makeAsyncGetCallString(rcApi, headerMap); HttpResponse rcDownloadJsonResponse = rcDownloadResFuture.get(); printUri = rcDownloadJsonResponse.getBody(); From 33ee958536a688729b195993d25e8ad27edd0616 Mon Sep 17 00:00:00 2001 From: Harikumar Palemkota Date: Thu, 24 Mar 2022 18:33:35 +0530 Subject: [PATCH 28/39] SB-29068 some certs returning empty body --- .../java/org/sunbird/serviceimpl/CertsServiceImpl.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java b/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java index 9ae12e2..0e2e761 100644 --- a/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java +++ b/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java @@ -258,8 +258,13 @@ public Response downloadV2(Request request) throws BaseException { headerMap.put("template", templateUrl); Future> rcDownloadResFuture = CertificateUtil.makeAsyncGetCallString(rcApi, headerMap); HttpResponse rcDownloadJsonResponse = rcDownloadResFuture.get(); - printUri = rcDownloadJsonResponse.getBody(); - response.put(JsonKeys.PRINT_URI, printUri); + String responseBody = rcDownloadJsonResponse.getBody(); + if(responseBody != null || !responseBody.isEmpty()) { + response.put(JsonKeys.PRINT_URI, responseBody); + } else { + logger.error("CertsServiceImpl:downloadV2:response body is empty for the certificate-id : "+certId); + throw new BaseException(IResponseMessage.RESOURCE_NOT_FOUND, localizer.getMessage(IResponseMessage.RESOURCE_NOT_FOUND, null), ResponseCode.RESOURCE_NOT_FOUND.getCode()); + } } else { logger.error("CertsServiceImpl:downloadV2:resource not found"); throw new BaseException(IResponseMessage.RESOURCE_NOT_FOUND, localizer.getMessage(IResponseMessage.RESOURCE_NOT_FOUND, null), ResponseCode.RESOURCE_NOT_FOUND.getCode()); From 121d2bdaca474af0b403b175b671c5c9408a51bd Mon Sep 17 00:00:00 2001 From: Harikumar Palemkota Date: Thu, 24 Mar 2022 19:28:26 +0530 Subject: [PATCH 29/39] SB-29068 some certs returning empty body --- .../org/sunbird/serviceimpl/CertsServiceImpl.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java b/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java index 0e2e761..2cf8729 100644 --- a/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java +++ b/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java @@ -248,7 +248,8 @@ public Response downloadV2(Request request) throws BaseException { } else { Map headerMap = new HashMap<>(); headerMap.put(JsonKeys.ACCEPT, JsonKeys.APPLICATION_JSON); - String rcApi = RegistryCredential.getSERVICE_BASE_URL().concat(RegistryCredential.getDOWNLOAD_URI())+"/"+certId; + headerMap.put("authorization","Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJSbTBGOTlnMnd4SmZKelJwa2YzTnRlaE5CYlJnZXRCaCJ9.CjDN6GufxylpAM5J_6j9fD0wq7S2qr1F6FOzAZtQ6XU"); + String rcApi = "https://staging.sunbirded.org/api/rc/certificate/v1/download/1-68a71795-a0fd-4b32-a492-81e0ce4da041";//RegistryCredential.getSERVICE_BASE_URL().concat(RegistryCredential.getDOWNLOAD_URI())+"/"+certId; Future> rcResponseFuture=CertificateUtil.makeAsyncGetCall(rcApi,headerMap); try { HttpResponse rcJsonResponse = rcResponseFuture.get(); @@ -257,16 +258,16 @@ public Response downloadV2(Request request) throws BaseException { headerMap.put(JsonKeys.ACCEPT, JsonKeys.IMAGE_SVG_XML); headerMap.put("template", templateUrl); Future> rcDownloadResFuture = CertificateUtil.makeAsyncGetCallString(rcApi, headerMap); - HttpResponse rcDownloadJsonResponse = rcDownloadResFuture.get(); - String responseBody = rcDownloadJsonResponse.getBody(); - if(responseBody != null || !responseBody.isEmpty()) { + if (rcDownloadResFuture != null && rcDownloadResFuture.get().getStatus() == HttpStatus.SC_OK) { + HttpResponse rcDownloadJsonResponse = rcDownloadResFuture.get(); + String responseBody = rcDownloadJsonResponse.getBody(); response.put(JsonKeys.PRINT_URI, responseBody); } else { - logger.error("CertsServiceImpl:downloadV2:response body is empty for the certificate-id : "+certId); + logger.error("CertsServiceImpl:downloadV2:resource image is not found for certificate-id : " +certId); throw new BaseException(IResponseMessage.RESOURCE_NOT_FOUND, localizer.getMessage(IResponseMessage.RESOURCE_NOT_FOUND, null), ResponseCode.RESOURCE_NOT_FOUND.getCode()); } } else { - logger.error("CertsServiceImpl:downloadV2:resource not found"); + logger.error("CertsServiceImpl:downloadV2:resource template json not found for certificate-id : " +certId); throw new BaseException(IResponseMessage.RESOURCE_NOT_FOUND, localizer.getMessage(IResponseMessage.RESOURCE_NOT_FOUND, null), ResponseCode.RESOURCE_NOT_FOUND.getCode()); } } catch (ExecutionException | InterruptedException e) { From 4f07ea5c556ae0c2fc84a25fb71c6d132b24488f Mon Sep 17 00:00:00 2001 From: Harikumar Palemkota Date: Thu, 24 Mar 2022 19:29:57 +0530 Subject: [PATCH 30/39] SB-29068 some certs returning empty body --- .../main/java/org/sunbird/serviceimpl/CertsServiceImpl.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java b/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java index 2cf8729..a59f0f5 100644 --- a/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java +++ b/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java @@ -248,8 +248,7 @@ public Response downloadV2(Request request) throws BaseException { } else { Map headerMap = new HashMap<>(); headerMap.put(JsonKeys.ACCEPT, JsonKeys.APPLICATION_JSON); - headerMap.put("authorization","Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJSbTBGOTlnMnd4SmZKelJwa2YzTnRlaE5CYlJnZXRCaCJ9.CjDN6GufxylpAM5J_6j9fD0wq7S2qr1F6FOzAZtQ6XU"); - String rcApi = "https://staging.sunbirded.org/api/rc/certificate/v1/download/1-68a71795-a0fd-4b32-a492-81e0ce4da041";//RegistryCredential.getSERVICE_BASE_URL().concat(RegistryCredential.getDOWNLOAD_URI())+"/"+certId; + String rcApi = RegistryCredential.getSERVICE_BASE_URL().concat(RegistryCredential.getDOWNLOAD_URI())+"/"+certId; Future> rcResponseFuture=CertificateUtil.makeAsyncGetCall(rcApi,headerMap); try { HttpResponse rcJsonResponse = rcResponseFuture.get(); From f5c4e3743a0e3f8b900a10a6585a9c4f39b10099 Mon Sep 17 00:00:00 2001 From: Harikumar Palemkota Date: Thu, 7 Apr 2022 21:09:58 +0530 Subject: [PATCH 31/39] SB-29463 added logs --- .../main/java/org/sunbird/serviceimpl/CertsServiceImpl.java | 4 ++++ .../src/main/java/org/sunbird/utilities/CertificateUtil.java | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java b/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java index a59f0f5..e2e5649 100644 --- a/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java +++ b/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java @@ -248,17 +248,21 @@ public Response downloadV2(Request request) throws BaseException { } else { Map headerMap = new HashMap<>(); headerMap.put(JsonKeys.ACCEPT, JsonKeys.APPLICATION_JSON); + String rcApi = RegistryCredential.getSERVICE_BASE_URL().concat(RegistryCredential.getDOWNLOAD_URI())+"/"+certId; Future> rcResponseFuture=CertificateUtil.makeAsyncGetCall(rcApi,headerMap); try { HttpResponse rcJsonResponse = rcResponseFuture.get(); if (rcJsonResponse != null && rcJsonResponse.getStatus() == HttpStatus.SC_OK) { String templateUrl = rcJsonResponse.getBody().getObject().getString(JsonKeys.TEMPLATE_URL); + logger.info("CertsServiceImpl:downloadV2: templateUrl: "+templateUrl); headerMap.put(JsonKeys.ACCEPT, JsonKeys.IMAGE_SVG_XML); headerMap.put("template", templateUrl); Future> rcDownloadResFuture = CertificateUtil.makeAsyncGetCallString(rcApi, headerMap); if (rcDownloadResFuture != null && rcDownloadResFuture.get().getStatus() == HttpStatus.SC_OK) { + logger.info("CertsServiceImpl:downloadV2: success: "); HttpResponse rcDownloadJsonResponse = rcDownloadResFuture.get(); + logger.info("CertsServiceImpl:downloadV2: success: rcDownloadJsonResponse :"+rcDownloadJsonResponse.getBody()); String responseBody = rcDownloadJsonResponse.getBody(); response.put(JsonKeys.PRINT_URI, responseBody); } else { diff --git a/all-actors/src/main/java/org/sunbird/utilities/CertificateUtil.java b/all-actors/src/main/java/org/sunbird/utilities/CertificateUtil.java index ef7fc37..eac8fcd 100644 --- a/all-actors/src/main/java/org/sunbird/utilities/CertificateUtil.java +++ b/all-actors/src/main/java/org/sunbird/utilities/CertificateUtil.java @@ -130,7 +130,7 @@ public static Future> makeAsyncPostCall(String apiToCall, } public static Future> makeAsyncGetCallString(String apiToCall, MapheaderMap){ - logger.info("CertificateUtil:makePostCall:get request to make post call for API:"+apiToCall); + logger.info("CertificateUtil:makeAsyncGetCallString:get request to make get call for API:"+apiToCall); Future> jsonResponse = Unirest.get(apiToCall) .headers(headerMap) @@ -139,7 +139,7 @@ public static Future> makeAsyncGetCallString(String apiToCa } public static Future> makeAsyncGetCall(String apiToCall, MapheaderMap){ - logger.info("CertificateUtil:makePostCall:get request to make post call for API:"+apiToCall); + logger.info("CertificateUtil:makeAsyncGetCall:get request to make get call for API:"+apiToCall); Future> jsonResponse = Unirest.get(apiToCall) .headers(headerMap) From 20f88652fa0864ab06fea4eeaf858da6a9ed18c5 Mon Sep 17 00:00:00 2001 From: Harikumar Palemkota Date: Fri, 8 Apr 2022 11:13:27 +0530 Subject: [PATCH 32/39] SB-29463 added logs --- .../org/sunbird/serviceimpl/CertsServiceImpl.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java b/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java index e2e5649..fa25b37 100644 --- a/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java +++ b/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java @@ -258,12 +258,22 @@ public Response downloadV2(Request request) throws BaseException { logger.info("CertsServiceImpl:downloadV2: templateUrl: "+templateUrl); headerMap.put(JsonKeys.ACCEPT, JsonKeys.IMAGE_SVG_XML); headerMap.put("template", templateUrl); + String responseBody = null; Future> rcDownloadResFuture = CertificateUtil.makeAsyncGetCallString(rcApi, headerMap); if (rcDownloadResFuture != null && rcDownloadResFuture.get().getStatus() == HttpStatus.SC_OK) { logger.info("CertsServiceImpl:downloadV2: success: "); HttpResponse rcDownloadJsonResponse = rcDownloadResFuture.get(); logger.info("CertsServiceImpl:downloadV2: success: rcDownloadJsonResponse :"+rcDownloadJsonResponse.getBody()); - String responseBody = rcDownloadJsonResponse.getBody(); + if(rcDownloadJsonResponse.getBody() == null || rcDownloadJsonResponse.getBody().isEmpty()) { + Future> rcDownloadResFuture1 = CertificateUtil.makeAsyncGetCall(rcApi, headerMap); + HttpResponse rcDownloadJsonResponse1 = rcDownloadResFuture1.get(); + if(rcDownloadJsonResponse1 != null && rcDownloadJsonResponse1.getStatus() == HttpStatus.SC_OK) { + logger.info("CertsServiceImpl:downloadV2: success: rcDownloadJsonResponse1 :"+rcDownloadJsonResponse1.getBody()); + responseBody = rcDownloadJsonResponse1.getBody().toString(); + } + } else { + responseBody = rcDownloadJsonResponse.getBody(); + } response.put(JsonKeys.PRINT_URI, responseBody); } else { logger.error("CertsServiceImpl:downloadV2:resource image is not found for certificate-id : " +certId); From fc6fc480bd575523e6b38863fa0c8d501518acc1 Mon Sep 17 00:00:00 2001 From: Harikumar Palemkota Date: Fri, 8 Apr 2022 16:09:09 +0530 Subject: [PATCH 33/39] SB-29463 issue while downloading certificate --- .../sunbird/serviceimpl/CertsServiceImpl.java | 22 +++++-------------- .../sunbird/utilities/CertificateUtil.java | 9 ++++---- 2 files changed, 10 insertions(+), 21 deletions(-) diff --git a/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java b/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java index fa25b37..26a0200 100644 --- a/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java +++ b/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java @@ -248,7 +248,6 @@ public Response downloadV2(Request request) throws BaseException { } else { Map headerMap = new HashMap<>(); headerMap.put(JsonKeys.ACCEPT, JsonKeys.APPLICATION_JSON); - String rcApi = RegistryCredential.getSERVICE_BASE_URL().concat(RegistryCredential.getDOWNLOAD_URI())+"/"+certId; Future> rcResponseFuture=CertificateUtil.makeAsyncGetCall(rcApi,headerMap); try { @@ -257,24 +256,13 @@ public Response downloadV2(Request request) throws BaseException { String templateUrl = rcJsonResponse.getBody().getObject().getString(JsonKeys.TEMPLATE_URL); logger.info("CertsServiceImpl:downloadV2: templateUrl: "+templateUrl); headerMap.put(JsonKeys.ACCEPT, JsonKeys.IMAGE_SVG_XML); - headerMap.put("template", templateUrl); - String responseBody = null; - Future> rcDownloadResFuture = CertificateUtil.makeAsyncGetCallString(rcApi, headerMap); - if (rcDownloadResFuture != null && rcDownloadResFuture.get().getStatus() == HttpStatus.SC_OK) { + HttpResponse rcDownloadResFuture = CertificateUtil.makeAsyncGetCallString(rcApi, headerMap); + if (rcDownloadResFuture != null && rcDownloadResFuture.getStatus() == HttpStatus.SC_OK) { logger.info("CertsServiceImpl:downloadV2: success: "); - HttpResponse rcDownloadJsonResponse = rcDownloadResFuture.get(); - logger.info("CertsServiceImpl:downloadV2: success: rcDownloadJsonResponse :"+rcDownloadJsonResponse.getBody()); - if(rcDownloadJsonResponse.getBody() == null || rcDownloadJsonResponse.getBody().isEmpty()) { - Future> rcDownloadResFuture1 = CertificateUtil.makeAsyncGetCall(rcApi, headerMap); - HttpResponse rcDownloadJsonResponse1 = rcDownloadResFuture1.get(); - if(rcDownloadJsonResponse1 != null && rcDownloadJsonResponse1.getStatus() == HttpStatus.SC_OK) { - logger.info("CertsServiceImpl:downloadV2: success: rcDownloadJsonResponse1 :"+rcDownloadJsonResponse1.getBody()); - responseBody = rcDownloadJsonResponse1.getBody().toString(); - } - } else { - responseBody = rcDownloadJsonResponse.getBody(); + String responseBody = rcDownloadResFuture.getBody(); + if(responseBody != null) { + response.put(JsonKeys.PRINT_URI, responseBody); } - response.put(JsonKeys.PRINT_URI, responseBody); } else { logger.error("CertsServiceImpl:downloadV2:resource image is not found for certificate-id : " +certId); throw new BaseException(IResponseMessage.RESOURCE_NOT_FOUND, localizer.getMessage(IResponseMessage.RESOURCE_NOT_FOUND, null), ResponseCode.RESOURCE_NOT_FOUND.getCode()); diff --git a/all-actors/src/main/java/org/sunbird/utilities/CertificateUtil.java b/all-actors/src/main/java/org/sunbird/utilities/CertificateUtil.java index eac8fcd..4659baf 100644 --- a/all-actors/src/main/java/org/sunbird/utilities/CertificateUtil.java +++ b/all-actors/src/main/java/org/sunbird/utilities/CertificateUtil.java @@ -28,6 +28,7 @@ import java.util.HashMap; import java.util.Locale; import java.util.Map; +import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; @@ -129,12 +130,12 @@ public static Future> makeAsyncPostCall(String apiToCall, return jsonResponse; } - public static Future> makeAsyncGetCallString(String apiToCall, MapheaderMap){ + public static HttpResponse makeAsyncGetCallString(String apiToCall, MapheaderMap) throws ExecutionException, InterruptedException { logger.info("CertificateUtil:makeAsyncGetCallString:get request to make get call for API:"+apiToCall); - Future> jsonResponse - = Unirest.get(apiToCall) + HttpResponse jsonResponse + = Unirest.get(apiToCall) .headers(headerMap) - .asStringAsync(); + .asStringAsync().get(); return jsonResponse; } From a40b8e64f0ddf83ca11b1b272d22fbb3d54e9025 Mon Sep 17 00:00:00 2001 From: Harikumar Palemkota Date: Fri, 8 Apr 2022 20:31:20 +0530 Subject: [PATCH 34/39] SB-29463 search api new certificate modified to old format --- .../sunbird/serviceimpl/CertsServiceImpl.java | 33 +++++++++++++++++-- .../src/main/java/org.sunbird/JsonKeys.java | 6 +++- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java b/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java index 26a0200..91d0187 100644 --- a/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java +++ b/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java @@ -431,11 +431,13 @@ public Response search(Request request) throws BaseException{ List> rcSearchApiResp = requestMapper.readValue(rcJsonArray, List.class); if(mappedResponse != null) { mappedResponse.setCount(mappedResponse.getCount() + rcSearchApiResp.size()); - mappedResponse.getContent().addAll(rcSearchApiResp); + List> oldCertMapLst = convertResponseToOldCertFormat(rcSearchApiResp); + mappedResponse.getContent().addAll(oldCertMapLst); } else { mappedResponse = new ESResponseMapper(); mappedResponse.setCount(rcSearchApiResp.size()); - mappedResponse.setContent(rcSearchApiResp); + List> oldCertMapLst = convertResponseToOldCertFormat(rcSearchApiResp); + mappedResponse.setContent(oldCertMapLst); } } else { logger.info("CertsServiceImpl:search:invalid request data"); @@ -450,6 +452,33 @@ public Response search(Request request) throws BaseException{ return response; } + private List> convertResponseToOldCertFormat(List> rcSearchApiResp) { + List> oldCertMapList = new ArrayList<>(); + rcSearchApiResp.forEach(rcMap -> { + Map oldCertMap = new HashMap<>(); + Map sourceMap = new HashMap<>(); + Map dataMap = new HashMap<>(); + Map couserMap = new HashMap<>(); + Map badgeMap = new HashMap<>(); + Map issuerMap = new HashMap<>(); + issuerMap.put(JsonKeys.NAME, ((Map)rcMap.get(JsonKeys.ISSUER)).get(JsonKeys.NAME)); + badgeMap.put(JsonKeys.NAME, rcMap.get(JsonKeys.CERTIFICATE_LABEL)); + badgeMap.put(JsonKeys.ISSUER, issuerMap); + couserMap.put(JsonKeys.COURSE_ID, ((Map)rcMap.get(JsonKeys.TRAINING)).get(JsonKeys.ID)); + dataMap.put(JsonKeys.BADGE, badgeMap); + dataMap.put(JsonKeys.ISSUED_ON, ((Map)rcMap.get(JsonKeys.TRAINING)).get("osCreatedAt")); + sourceMap.put(JsonKeys.PDF_URL, null); + sourceMap.put(JsonKeys.DATA, dataMap); + sourceMap.put(JsonKeys.RELATED, couserMap); + oldCertMap.put("_index", "certv3"); + oldCertMap.put("_type", "_doc"); + oldCertMap.put("_id", rcMap.get(JsonKeys.OSID)); + oldCertMap.put("_score", ""); + oldCertMapList.add(oldCertMap); + }); + return oldCertMapList; + } + @Override public Response searchV2(Request request) throws BaseException{ Response response = new Response(); diff --git a/sb-utils/src/main/java/org.sunbird/JsonKeys.java b/sb-utils/src/main/java/org.sunbird/JsonKeys.java index 17fb57b..23c08cf 100644 --- a/sb-utils/src/main/java/org.sunbird/JsonKeys.java +++ b/sb-utils/src/main/java/org.sunbird/JsonKeys.java @@ -85,5 +85,9 @@ public class JsonKeys { public static final String APPLICATION_JSON = "application/json"; public static final String QUERY_MUST_PHRASE = "/query/bool/must"; public static final String MATCH_PHRASE = "/match_phrase"; - public static final String FILTERS = "filters"; + public static final String FILTERS = "filters"; + public static final String COURSE_ID = "courseId"; + public static final String CERTIFICATE_LABEL = "certificateLabel"; + public static final String OSID = "osid"; + public static final String TRAINING = "training"; } From 29d2b8dd81979d0c7b8b86b973c5613fc850f624 Mon Sep 17 00:00:00 2001 From: Harikumar Palemkota Date: Sun, 10 Apr 2022 22:12:18 +0530 Subject: [PATCH 35/39] SB-29463 search api new certificate modified to old format --- .../src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java b/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java index 91d0187..2847952 100644 --- a/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java +++ b/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java @@ -256,6 +256,7 @@ public Response downloadV2(Request request) throws BaseException { String templateUrl = rcJsonResponse.getBody().getObject().getString(JsonKeys.TEMPLATE_URL); logger.info("CertsServiceImpl:downloadV2: templateUrl: "+templateUrl); headerMap.put(JsonKeys.ACCEPT, JsonKeys.IMAGE_SVG_XML); + headerMap.put("template", templateUrl); HttpResponse rcDownloadResFuture = CertificateUtil.makeAsyncGetCallString(rcApi, headerMap); if (rcDownloadResFuture != null && rcDownloadResFuture.getStatus() == HttpStatus.SC_OK) { logger.info("CertsServiceImpl:downloadV2: success: "); @@ -474,6 +475,7 @@ private List> convertResponseToOldCertFormat(List Date: Wed, 13 Apr 2022 10:09:30 +0530 Subject: [PATCH 36/39] Issue #SB-29463: Added headers for deownloadv2/search Apis - X_AUTHENTICATED_USER_TOKEN,X_AUTHENTICATED_FOR --- .../main/java/org/sunbird/serviceimpl/CertsServiceImpl.java | 5 +++++ .../test/java/org/sunbird/actor/CertificationActorTest.java | 2 ++ sb-utils/src/main/java/org.sunbird/JsonKeys.java | 4 ++++ 3 files changed, 11 insertions(+) diff --git a/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java b/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java index 2847952..94542b7 100644 --- a/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java +++ b/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java @@ -248,6 +248,9 @@ public Response downloadV2(Request request) throws BaseException { } else { Map headerMap = new HashMap<>(); headerMap.put(JsonKeys.ACCEPT, JsonKeys.APPLICATION_JSON); + headerMap.put(JsonKeys.X_AUTHENTICATED_USER_TOKEN, request.getHeaders().get(JsonKeys.X_AUTHENTICATED_USER_TOKEN).toString()); + headerMap.put(JsonKeys.X_AUTHENTICATED_FOR, request.getHeaders().get(JsonKeys.X_AUTHENTICATED_FOR).toString()); + String rcApi = RegistryCredential.getSERVICE_BASE_URL().concat(RegistryCredential.getDOWNLOAD_URI())+"/"+certId; Future> rcResponseFuture=CertificateUtil.makeAsyncGetCall(rcApi,headerMap); try { @@ -407,6 +410,8 @@ public Response search(Request request) throws BaseException{ Map headerMap = new HashMap<>(); ObjectMapper mapper = new ObjectMapper(); headerMap.put(JsonKeys.CONTENT_TYPE, JsonKeys.APPLICATION_JSON); + headerMap.put(JsonKeys.X_AUTHENTICATED_USER_TOKEN, request.getHeaders().get(JsonKeys.X_AUTHENTICATED_USER_TOKEN).toString()); + headerMap.put(JsonKeys.X_AUTHENTICATED_FOR, request.getHeaders().get(JsonKeys.X_AUTHENTICATED_FOR).toString()); try { ESResponseMapper mappedResponse = null; mappedResponse = searchEsPostCall(request); diff --git a/all-actors/src/test/java/org/sunbird/actor/CertificationActorTest.java b/all-actors/src/test/java/org/sunbird/actor/CertificationActorTest.java index 43f76a7..9adbe44 100644 --- a/all-actors/src/test/java/org/sunbird/actor/CertificationActorTest.java +++ b/all-actors/src/test/java/org/sunbird/actor/CertificationActorTest.java @@ -302,6 +302,8 @@ private Request createDownloadV2CertRequest() { Map reqMap = new HashMap<>(); reqMap.put(JsonKeys.ID,"anyMockId"); reqObj.getRequest().putAll(reqMap); + reqObj.getHeaders().put(JsonKeys.X_AUTHENTICATED_FOR, "X_AUTHENTICATED_FOR"); + reqObj.getHeaders().put(JsonKeys.X_AUTHENTICATED_USER_TOKEN, "X_AUTHENTICATED_USER_TOKEN"); return reqObj; } diff --git a/sb-utils/src/main/java/org.sunbird/JsonKeys.java b/sb-utils/src/main/java/org.sunbird/JsonKeys.java index 23c08cf..f529e78 100644 --- a/sb-utils/src/main/java/org.sunbird/JsonKeys.java +++ b/sb-utils/src/main/java/org.sunbird/JsonKeys.java @@ -90,4 +90,8 @@ public class JsonKeys { public static final String CERTIFICATE_LABEL = "certificateLabel"; public static final String OSID = "osid"; public static final String TRAINING = "training"; + public static final String X_AUTHENTICATED_USER_TOKEN = "x-authenticated-user-token"; + public static final String X_AUTHENTICATED_FOR = "x-authenticated-for"; + + } From dd2ec50dd2118db56eca088c4f58c554afe096f5 Mon Sep 17 00:00:00 2001 From: Harikumar Palemkota Date: Wed, 13 Apr 2022 15:46:21 +0530 Subject: [PATCH 37/39] SB-29565 issue with auth-token --- .../sunbird/serviceimpl/CertsServiceImpl.java | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java b/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java index 94542b7..1c00c83 100644 --- a/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java +++ b/all-actors/src/main/java/org/sunbird/serviceimpl/CertsServiceImpl.java @@ -248,8 +248,12 @@ public Response downloadV2(Request request) throws BaseException { } else { Map headerMap = new HashMap<>(); headerMap.put(JsonKeys.ACCEPT, JsonKeys.APPLICATION_JSON); - headerMap.put(JsonKeys.X_AUTHENTICATED_USER_TOKEN, request.getHeaders().get(JsonKeys.X_AUTHENTICATED_USER_TOKEN).toString()); - headerMap.put(JsonKeys.X_AUTHENTICATED_FOR, request.getHeaders().get(JsonKeys.X_AUTHENTICATED_FOR).toString()); + if(request.getHeaders() != null && request.getHeaders().get(JsonKeys.X_AUTHENTICATED_USER_TOKEN) != null) { + headerMap.put(JsonKeys.X_AUTHENTICATED_USER_TOKEN, request.getHeaders().get(JsonKeys.X_AUTHENTICATED_USER_TOKEN).toString()); + } + if(request.getHeaders() != null && request.getHeaders().get(JsonKeys.X_AUTHENTICATED_FOR) != null) { + headerMap.put(JsonKeys.X_AUTHENTICATED_FOR, request.getHeaders().get(JsonKeys.X_AUTHENTICATED_FOR).toString()); + } String rcApi = RegistryCredential.getSERVICE_BASE_URL().concat(RegistryCredential.getDOWNLOAD_URI())+"/"+certId; Future> rcResponseFuture=CertificateUtil.makeAsyncGetCall(rcApi,headerMap); @@ -410,8 +414,12 @@ public Response search(Request request) throws BaseException{ Map headerMap = new HashMap<>(); ObjectMapper mapper = new ObjectMapper(); headerMap.put(JsonKeys.CONTENT_TYPE, JsonKeys.APPLICATION_JSON); - headerMap.put(JsonKeys.X_AUTHENTICATED_USER_TOKEN, request.getHeaders().get(JsonKeys.X_AUTHENTICATED_USER_TOKEN).toString()); - headerMap.put(JsonKeys.X_AUTHENTICATED_FOR, request.getHeaders().get(JsonKeys.X_AUTHENTICATED_FOR).toString()); + if(request.getHeaders() != null && request.getHeaders().get(JsonKeys.X_AUTHENTICATED_USER_TOKEN) != null) { + headerMap.put(JsonKeys.X_AUTHENTICATED_USER_TOKEN, request.getHeaders().get(JsonKeys.X_AUTHENTICATED_USER_TOKEN).toString()); + } + if(request.getHeaders() != null && request.getHeaders().get(JsonKeys.X_AUTHENTICATED_FOR) != null) { + headerMap.put(JsonKeys.X_AUTHENTICATED_FOR, request.getHeaders().get(JsonKeys.X_AUTHENTICATED_FOR).toString()); + } try { ESResponseMapper mappedResponse = null; mappedResponse = searchEsPostCall(request); @@ -468,7 +476,7 @@ private List> convertResponseToOldCertFormat(List badgeMap = new HashMap<>(); Map issuerMap = new HashMap<>(); issuerMap.put(JsonKeys.NAME, ((Map)rcMap.get(JsonKeys.ISSUER)).get(JsonKeys.NAME)); - badgeMap.put(JsonKeys.NAME, rcMap.get(JsonKeys.CERTIFICATE_LABEL)); + badgeMap.put(JsonKeys.NAME, ((Map)rcMap.get(JsonKeys.TRAINING)).get(JsonKeys.NAME)); badgeMap.put(JsonKeys.ISSUER, issuerMap); couserMap.put(JsonKeys.COURSE_ID, ((Map)rcMap.get(JsonKeys.TRAINING)).get(JsonKeys.ID)); dataMap.put(JsonKeys.BADGE, badgeMap); From 2f436937f1bbc0b1d51c9733fe6e5520dc9df48c Mon Sep 17 00:00:00 2001 From: Harikumar Palemkota Date: Tue, 17 May 2022 12:45:19 +0530 Subject: [PATCH 38/39] SB-29887 adding headers to request --- .../src/main/java/org/sunbird/BaseActor.java | 15 ++++++------- service/app/controllers/BaseController.java | 21 +++++++++++++++++++ 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/all-actors/src/main/java/org/sunbird/BaseActor.java b/all-actors/src/main/java/org/sunbird/BaseActor.java index 151fa92..6fea037 100644 --- a/all-actors/src/main/java/org/sunbird/BaseActor.java +++ b/all-actors/src/main/java/org/sunbird/BaseActor.java @@ -9,10 +9,7 @@ import org.sunbird.request.Request; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Locale; -import java.util.Map; +import java.util.*; /** * @author Amit Kumar @@ -29,9 +26,13 @@ public void onReceive(Object message) throws Throwable { Request request = (Request) message; Map trace = new HashMap<>(); if (request.getHeaders().containsKey(JsonKeys.REQUEST_MESSAGE_ID)) { - ArrayList requestIds = - (ArrayList) request.getHeaders().get(JsonKeys.REQUEST_MESSAGE_ID); - trace.put(JsonKeys.REQUEST_MESSAGE_ID, requestIds.get(0)); + if(request.getHeaders().get(JsonKeys.REQUEST_MESSAGE_ID) instanceof String) { + trace.put(JsonKeys.REQUEST_MESSAGE_ID, request.getHeaders().get(JsonKeys.REQUEST_MESSAGE_ID)); + } else { + ArrayList requestIds = + (ArrayList) request.getHeaders().get(JsonKeys.REQUEST_MESSAGE_ID); + trace.put(JsonKeys.REQUEST_MESSAGE_ID, requestIds.get(0)); + } logger.setMDC(trace); // set mdc for non actors new BaseLogger().setReqId(logger.getMDC()); diff --git a/service/app/controllers/BaseController.java b/service/app/controllers/BaseController.java index 77f8305..8af18b7 100644 --- a/service/app/controllers/BaseController.java +++ b/service/app/controllers/BaseController.java @@ -1,5 +1,9 @@ package controllers; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionStage; import javax.inject.Inject; @@ -8,6 +12,7 @@ import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.ObjectNode; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sunbird.BaseException; @@ -20,6 +25,7 @@ import play.libs.Json; import play.libs.concurrent.HttpExecutionContext; import play.mvc.Controller; +import play.mvc.Http; import play.mvc.Result; import play.mvc.Results; import utils.RequestMapper; @@ -101,6 +107,10 @@ public CompletionStage handleRequest(ActorRef actorRef, play.mvc.Http.Re if (req.body() != null && req.body().asJson() != null) { request = (Request) RequestMapper.mapRequest(req, Request.class); } + if (req.getHeaders() != null && request.getHeaders() != null) { + Map map = getAllRequestHeaders(req); + request.setHeaders(map); + } if (validatorFunction != null) { validatorFunction.apply(request); } @@ -120,4 +130,15 @@ private void handleSigTerm() throws BaseException { throw new BaseException(IResponseMessage.SERVICE_UNAVAILABLE, IResponseMessage.SERVICE_UNAVAILABLE, ResponseCode.SERVICE_UNAVAILABLE.getCode()); } } + + public Map getAllRequestHeaders(Http.Request request) { + Map map = new HashMap<>(); + Map> headers = request.getHeaders().toMap(); + Iterator>> itr = headers.entrySet().iterator(); + while (itr.hasNext()) { + Map.Entry> entry = itr.next(); + map.put(entry.getKey(), entry.getValue().get(0)); + } + return map; + } } From 4b7c15bcfe68c0d5a2afd2dd7058fc42759e5924 Mon Sep 17 00:00:00 2001 From: PrasadMoka Date: Fri, 19 Aug 2022 13:25:53 +0530 Subject: [PATCH 39/39] feat:LR-139 for cassandra multiple DC changes --- .../main/resources/cassandra.config.properties | 3 ++- .../main/java/org/sunbird/common/Constants.java | 1 + .../helper/CassandraConnectionManagerImpl.java | 16 +++++++++++----- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/all-actors/src/main/resources/cassandra.config.properties b/all-actors/src/main/resources/cassandra.config.properties index d673fd5..e6ecc19 100644 --- a/all-actors/src/main/resources/cassandra.config.properties +++ b/all-actors/src/main/resources/cassandra.config.properties @@ -5,4 +5,5 @@ maxConnectionsPerHostForRemote=4 maxRequestsPerConnection=32768 heartbeatIntervalSeconds=60 poolTimeoutMillis=0 -queryLoggerConstantThreshold=300 \ No newline at end of file +queryLoggerConstantThreshold=300 +isMultiDCEnabled=false \ No newline at end of file diff --git a/cassandra-utils/src/main/java/org/sunbird/common/Constants.java b/cassandra-utils/src/main/java/org/sunbird/common/Constants.java index 2ce363f..f2740cb 100644 --- a/cassandra-utils/src/main/java/org/sunbird/common/Constants.java +++ b/cassandra-utils/src/main/java/org/sunbird/common/Constants.java @@ -72,4 +72,5 @@ public interface Constants { public static final String OBJECT_TYPE = "objectType"; public static final String INSERT = "insert"; public static final String STANDALONE_MODE = "standalone"; + public static final String IS_MULTI_DC_ENABLED = "isMultiDCEnabled"; } diff --git a/cassandra-utils/src/main/java/org/sunbird/helper/CassandraConnectionManagerImpl.java b/cassandra-utils/src/main/java/org/sunbird/helper/CassandraConnectionManagerImpl.java index 57e445c..9d4140e 100644 --- a/cassandra-utils/src/main/java/org/sunbird/helper/CassandraConnectionManagerImpl.java +++ b/cassandra-utils/src/main/java/org/sunbird/helper/CassandraConnectionManagerImpl.java @@ -12,6 +12,7 @@ import com.datastax.driver.core.QueryOptions; import com.datastax.driver.core.Session; import com.datastax.driver.core.TableMetadata; +import com.datastax.driver.core.policies.DCAwareRoundRobinPolicy; import com.datastax.driver.core.policies.DefaultRetryPolicy; import java.util.Collection; import java.util.HashMap; @@ -102,9 +103,9 @@ private boolean createStandaloneConnection( poolingOptions.setPoolTimeoutMillis( Integer.parseInt(cache.getProperty(Constants.POOL_TIMEOUT))); if (!StringUtils.isBlank(userName) && !StringUtils.isBlank(password)) { - cluster = createCluster(ips, port, userName, password, poolingOptions); + cluster = createCluster(ips, port, userName, password, poolingOptions, Boolean.parseBoolean(cache.getProperty(Constants.IS_MULTI_DC_ENABLED))); } else { - cluster = createCluster(ips, port, poolingOptions); + cluster = createCluster(ips, port, poolingOptions, Boolean.parseBoolean(cache.getProperty(Constants.IS_MULTI_DC_ENABLED))); } QueryLogger queryLogger = QueryLogger.builder() @@ -175,7 +176,7 @@ private static ConsistencyLevel getConsistencyLevel() { * @return Cassandra cluster */ private static Cluster createCluster( - String ips, String port, String userName, String password, PoolingOptions poolingOptions) { + String ips, String port, String userName, String password, PoolingOptions poolingOptions, boolean isMultiDCEnabled) { String[] hosts = null; if (StringUtils.isNotBlank(ips)) { hosts = ips.split(","); @@ -203,6 +204,11 @@ private static Cluster createCluster( builder.withQueryOptions(new QueryOptions().setConsistencyLevel(consistencyLevel)); } + logger.info( + "CassandraConnectionManagerImpl:createCluster: isMultiDCEnabled = " + isMultiDCEnabled); + if (isMultiDCEnabled) { + builder.withLoadBalancingPolicy(DCAwareRoundRobinPolicy.builder().build()); + } return builder.build(); } @@ -214,8 +220,8 @@ private static Cluster createCluster( * @param poolingOptions Pooling options * @return Cassandra cluster */ - private static Cluster createCluster(String ip, String port, PoolingOptions poolingOptions) { - return createCluster(ip, port, null, null, poolingOptions); + private static Cluster createCluster(String ip, String port, PoolingOptions poolingOptions, boolean isMultiDCEnabled) { + return createCluster(ip, port, null, null, poolingOptions, isMultiDCEnabled); } @Override