From 015d7044fb8676e404b0612819244f135eb73add Mon Sep 17 00:00:00 2001 From: Subhash-eGov Date: Mon, 20 Jul 2020 18:25:52 +0530 Subject: [PATCH] Move custom consumer from core services to utilities (#20) * Custom consumer mvn changes (#245) * add new consumer * Adding build config for egov-custom-consumer service * added kafka host * add client for financial signout * Update CustomConsumerApplication.java * Fixed serialization error in zuul * [PBERP-456] - Extracted url to properties. * Merging from old repo to new repo * maven upgrade changes * Jackson version and kafka issue fix Co-authored-by: GhanshyamRawat Co-authored-by: sivaprakash123 Co-authored-by: venki * Merging from core services repo to utilities repo * Adding custom consumer to build configuration Co-authored-by: GhanshyamRawat Co-authored-by: sivaprakash123 Co-authored-by: venki --- build/build-config.yml | 9 +- egov-custom-consumer/CHANGELOG.md | 11 ++ egov-custom-consumer/Dockerfile | 25 ++++ egov-custom-consumer/pom.xml | 117 ++++++++++++++++++ .../org/egov/CustomConsumerApplication.java | 27 ++++ .../java/org/egov/cosumer/CustomConsumer.java | 52 ++++++++ .../java/org/egov/service/SignOutService.java | 53 ++++++++ .../java/org/egov/utils/JsonPathConstant.java | 13 ++ .../src/main/resources/application.properties | 15 +++ egov-custom-consumer/start.sh | 7 ++ 10 files changed, 328 insertions(+), 1 deletion(-) create mode 100644 egov-custom-consumer/CHANGELOG.md create mode 100644 egov-custom-consumer/Dockerfile create mode 100644 egov-custom-consumer/pom.xml create mode 100644 egov-custom-consumer/src/main/java/org/egov/CustomConsumerApplication.java create mode 100644 egov-custom-consumer/src/main/java/org/egov/cosumer/CustomConsumer.java create mode 100644 egov-custom-consumer/src/main/java/org/egov/service/SignOutService.java create mode 100644 egov-custom-consumer/src/main/java/org/egov/utils/JsonPathConstant.java create mode 100644 egov-custom-consumer/src/main/resources/application.properties create mode 100644 egov-custom-consumer/start.sh diff --git a/build/build-config.yml b/build/build-config.yml index c54f44a..110812f 100644 --- a/build/build-config.yml +++ b/build/build-config.yml @@ -53,4 +53,11 @@ config: build: - work-dir: "data-upload" image-name: "data-upload" - dockerfile: "build/maven/Dockerfile" \ No newline at end of file + dockerfile: "build/maven/Dockerfile" + + - name: "builds/utilities/egov-custom-consumer" + build: + - work-dir: "egov-custom-consumer" + image-name: "egov-custom-consumer" + dockerfile: "build/maven/Dockerfile" + diff --git a/egov-custom-consumer/CHANGELOG.md b/egov-custom-consumer/CHANGELOG.md new file mode 100644 index 0000000..baedd15 --- /dev/null +++ b/egov-custom-consumer/CHANGELOG.md @@ -0,0 +1,11 @@ +# Changelog +All notable changes to this module will be documented in this file. + +## 1.1.0 - 2020-06-22 + +- Upgraded to `tracer:2.0.0-SNAPSHOT` +- Upgraded to `Spring boot 2.2.6` + +## 1.0.0 + +- Base version diff --git a/egov-custom-consumer/Dockerfile b/egov-custom-consumer/Dockerfile new file mode 100644 index 0000000..5afe619 --- /dev/null +++ b/egov-custom-consumer/Dockerfile @@ -0,0 +1,25 @@ +FROM egovio/apline-jre:8u121-mdms-1.0 + +MAINTAINER Sivaprakash + + +# INSTRUCTIONS ON HOW TO BUILD JAR: +# Move to the location where pom.xml is exist in project and build project using below command +# "mvn clean package" + +#RUN cd /opt/mdms && git pull origin master + +#RUN cd /opt && wget "https://codeload.github.com/egovernments/egov-mdms-data/zip/master" -O master.zip && unzip master.zip && rm -rf master.zip mdms && mv egov-mdms-data-master mdms + +COPY /target/egov-custom-consumer-0.0.1-SNAPSHOT.jar /opt/egov/egov-custom-consumer.jar + +COPY start.sh /usr/bin/start.sh + +RUN chmod +x /usr/bin/start.sh + +CMD ["/usr/bin/start.sh"] + +# NOTE: the two 'RUN' commands can probably be combined inside of a single +# script (i.e. RUN build-and-install-app.sh) so that we can also clean up the +# extra files created during the `mvn package' command. that step inflates the +# resultant image by almost 1.0GB. diff --git a/egov-custom-consumer/pom.xml b/egov-custom-consumer/pom.xml new file mode 100644 index 0000000..9b7b8c2 --- /dev/null +++ b/egov-custom-consumer/pom.xml @@ -0,0 +1,117 @@ + + 4.0.0 + org.egov + egov-custom-consumer + 1.1.0-SNAPSHOT + + + org.springframework.boot + spring-boot-starter-parent + 2.2.6.RELEASE + + + + + UTF-8 + UTF-8 + 1.8 + 2.6 + 3.3.9 + + + + + org.springframework.boot + spring-boot-starter-web + + + commons-lang + commons-lang + ${commons-lang-version} + + + + org.apache.commons + commons-lang3 + 3.0 + + + + org.projectlombok + lombok + true + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.egov.services + tracer + 2.0.0-SNAPSHOT + + + org.egov.services + services-common + 1.0.0-RELEASE + + + com.jayway.jsonpath + json-path + 2.2.0 + + + + org.json + json + 20160810 + + + + com.fasterxml.jackson.dataformat + jackson-dataformat-yaml + + + com.fasterxml.jackson.core + jackson-databind + + + org.springframework.boot + spring-boot-devtools + runtime + + + + + repo.egovernments.org + eGov ERP Releases Repository + https://nexus-repo.egovernments.org/nexus/content/repositories/releases/ + + + repo.egovernments.org.snapshots + eGov ERP Releases Repository + https://nexus-repo.egovernments.org/nexus/content/repositories/snapshots/ + + + repo.egovernments.org.public + eGov Public Repository Group + https://nexus-repo.egovernments.org/nexus/content/groups/public/ + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + + \ No newline at end of file diff --git a/egov-custom-consumer/src/main/java/org/egov/CustomConsumerApplication.java b/egov-custom-consumer/src/main/java/org/egov/CustomConsumerApplication.java new file mode 100644 index 0000000..28eb039 --- /dev/null +++ b/egov-custom-consumer/src/main/java/org/egov/CustomConsumerApplication.java @@ -0,0 +1,27 @@ +package org.egov; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.web.client.RestTemplate; + +import com.fasterxml.jackson.databind.ObjectMapper; + +@SpringBootApplication +public class CustomConsumerApplication { + + public static void main(String[] args) { + SpringApplication.run(CustomConsumerApplication.class, args); + } + + @Bean + public RestTemplate restTemplate() { + return new RestTemplate(); + } + + @Bean + public ObjectMapper objectMapper() { + return new ObjectMapper(); + } + +} diff --git a/egov-custom-consumer/src/main/java/org/egov/cosumer/CustomConsumer.java b/egov-custom-consumer/src/main/java/org/egov/cosumer/CustomConsumer.java new file mode 100644 index 0000000..0864cf4 --- /dev/null +++ b/egov-custom-consumer/src/main/java/org/egov/cosumer/CustomConsumer.java @@ -0,0 +1,52 @@ +package org.egov.cosumer; + +import java.util.HashMap; + +import org.egov.service.SignOutService; +import org.egov.utils.JsonPathConstant; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.kafka.annotation.KafkaListener; +import org.springframework.kafka.support.KafkaHeaders; +import org.springframework.messaging.handler.annotation.Header; +import org.springframework.stereotype.Component; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.jayway.jsonpath.DocumentContext; +import com.jayway.jsonpath.JsonPath; + +import lombok.extern.slf4j.Slf4j; + +@Component +@Slf4j +public class CustomConsumer { + + @Autowired + private SignOutService signOutService; + + @Autowired + private ObjectMapper objectMapper; + + @KafkaListener(topics = { "${egov.custom.async.filter.topic}" }) + public void listen(final HashMap record, @Header(KafkaHeaders.RECEIVED_TOPIC) String topic) { + + log.info("CustomConsumer received request from topic: " + topic); + log.info("data: " + record); + log.info(record.keySet().toString()); + + try { + //String inputJson = objectMapper.writeValueAsString(record); + DocumentContext documentContext = JsonPath.parse(record); + String sourceUri = documentContext.read(JsonPathConstant.signOutUriJsonPath); + + if(sourceUri.equals(JsonPathConstant.signOutUri)) + signOutService.callFinanceForSignOut(documentContext); + + } catch (Exception ex) { + ex.printStackTrace(); + } + + // ObjectMapper mapper = new ObjectMapper(); + + } + +} diff --git a/egov-custom-consumer/src/main/java/org/egov/service/SignOutService.java b/egov-custom-consumer/src/main/java/org/egov/service/SignOutService.java new file mode 100644 index 0000000..6936f87 --- /dev/null +++ b/egov-custom-consumer/src/main/java/org/egov/service/SignOutService.java @@ -0,0 +1,53 @@ +package org.egov.service; + +import java.util.LinkedHashMap; + +import org.egov.utils.JsonPathConstant; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Service; +import org.springframework.web.client.HttpClientErrorException; +import org.springframework.web.client.RestTemplate; + +import com.jayway.jsonpath.DocumentContext; + +import lombok.extern.slf4j.Slf4j; + +@Service +@Slf4j +public class SignOutService { + + @Autowired + private RestTemplate restTemplate; + + @Value("${egov.coexistence.hostname}") + private String coexistencehost; + + @Value("${egov.coexistence.singout.uri}") + private String coexistencelogoutUri; + + public void callFinanceForSignOut(DocumentContext documentContext) { + ResponseEntity response = null; + try { + ; + String accessToken = documentContext.read(JsonPathConstant.signOutAccessToken); + documentContext = documentContext.delete(JsonPathConstant.userInfo); + documentContext = documentContext.put(JsonPathConstant.requestInfo, "authToken", accessToken); + LinkedHashMap jsonRequest = documentContext.read(JsonPathConstant.request); + + response = restTemplate.exchange(coexistencehost + coexistencelogoutUri, HttpMethod.POST, + new HttpEntity<>(jsonRequest), + ResponseEntity.class); + log.info("SignOutService response :" + response.getStatusCode()); + } catch (HttpClientErrorException ex) { + log.error(ex.getResponseBodyAsString()); + ex.printStackTrace(); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + +} diff --git a/egov-custom-consumer/src/main/java/org/egov/utils/JsonPathConstant.java b/egov-custom-consumer/src/main/java/org/egov/utils/JsonPathConstant.java new file mode 100644 index 0000000..211b318 --- /dev/null +++ b/egov-custom-consumer/src/main/java/org/egov/utils/JsonPathConstant.java @@ -0,0 +1,13 @@ +package org.egov.utils; + +public class JsonPathConstant { + + public static final String signOutAccessToken = "$.queryParamMap.access_token[0]"; + public static final String request = "$.request"; + public static final String userInfo = "$.request.RequestInfo.userInfo"; + public static final String requestInfo = "$.request.RequestInfo"; + public static final String accessTokenKey = "authToken"; + public static final String signOutUri = "/user/_logout"; + public static final String signOutUriJsonPath = "$.sourceUri"; + +} diff --git a/egov-custom-consumer/src/main/resources/application.properties b/egov-custom-consumer/src/main/resources/application.properties new file mode 100644 index 0000000..5418c62 --- /dev/null +++ b/egov-custom-consumer/src/main/resources/application.properties @@ -0,0 +1,15 @@ +spring.main.web-environment=false +server.context-path=/egov-custom-consumer +server.servlet.context-path=/egov-custom-consumer + +# KAFKA SERVER CONFIGURATIONS +kafka.config.bootstrap_server_config=kafka-0.kafka.backbone:9092 +spring.kafka.consumer.value-deserializer=org.egov.tracer.kafka.deserializer.HashMapDeserializer +spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer +spring.kafka.consumer.group-id=egov-api-gateway-1 + +spring.kafka.listener.missing-topics-fatal=false + +egov.custom.async.filter.topic=res-custom-filter +egov.coexistence.hostname=https://jalandhar-dev.egovernments.org +egov.coexistence.singout.uri=/services/EGF/rest/ClearToken \ No newline at end of file diff --git a/egov-custom-consumer/start.sh b/egov-custom-consumer/start.sh new file mode 100644 index 0000000..a0aa3ac --- /dev/null +++ b/egov-custom-consumer/start.sh @@ -0,0 +1,7 @@ +#!/bin/sh + +if [[ -z "${JAVA_OPTS}" ]];then + export JAVA_OPTS="-Xmx64m -Xms64m" +fi + +java ${JAVA_OPTS} -jar /opt/egov/egov-custom-consumer.jar