From 013f6c27cf52b7c6bfc19d52b2d184921fe74812 Mon Sep 17 00:00:00 2001 From: Ancy Riju Date: Wed, 27 Sep 2023 14:56:25 +0530 Subject: [PATCH 1/3] Biometric changes --- pom.xml | 12 + src/main/environment/common_test.properties | 4 + .../biometric/BiometricController.java | 42 ++++ .../service/biometric/BiometricService.java | 7 + .../biometric/BiometricServiceImpl.java | 229 ++++++++++++++++++ 5 files changed, 294 insertions(+) create mode 100644 src/main/java/com/iemr/common/controller/biometric/BiometricController.java create mode 100644 src/main/java/com/iemr/common/service/biometric/BiometricService.java create mode 100644 src/main/java/com/iemr/common/service/biometric/BiometricServiceImpl.java diff --git a/pom.xml b/pom.xml index 8ff8d6e6..131f6f11 100644 --- a/pom.xml +++ b/pom.xml @@ -49,6 +49,18 @@ + + org.apache.httpcomponents + httpclient + + + + + xmlpull + xmlpull + 1.1.3.1 + + org.springframework.boot spring-boot-starter diff --git a/src/main/environment/common_test.properties b/src/main/environment/common_test.properties index 57e182b6..dba62e8b 100644 --- a/src/main/environment/common_test.properties +++ b/src/main/environment/common_test.properties @@ -97,3 +97,7 @@ avniRegistrationLimit=7 nhm.agent.real.time.data.url= http://175.101.1.83/apps/utility/alive_api.php nhm.agent.real.time.data.cron.scheduler=0 */2 * ? * * nhm.agent.real.time.data.cron.flag=true + +biometric.discover.url = http://127.0.0.1:port/ +biometric.deviceInfo.url = https://127.0.0.1:port/rd/info +biometric.capture.url = https://127.0.0.1:port/rd/capture \ No newline at end of file diff --git a/src/main/java/com/iemr/common/controller/biometric/BiometricController.java b/src/main/java/com/iemr/common/controller/biometric/BiometricController.java new file mode 100644 index 00000000..fa41e914 --- /dev/null +++ b/src/main/java/com/iemr/common/controller/biometric/BiometricController.java @@ -0,0 +1,42 @@ +package com.iemr.common.controller.biometric; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestHeader; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + + + +import com.iemr.common.service.biometric.BiometricService; +import com.iemr.common.utils.response.OutputResponse; + + + +import io.swagger.annotations.ApiOperation; + +@CrossOrigin +@RestController +@RequestMapping(value = "/biometric") +public class BiometricController { + private Logger logger = LoggerFactory.getLogger(this.getClass().getSimpleName()); + @Autowired + private BiometricService biometricService; + @CrossOrigin + @RequestMapping(value = { "/getBiometricData/{pid}" }, method = { RequestMethod.GET }) + public OutputResponse saveBenNurseDataCAC(@RequestParam String pid) { + OutputResponse response = new OutputResponse(); + try { + String pidResponse = biometricService.getBioData(pid); + response.setResponse(pidResponse); + }catch (Exception e) { + logger.error("Error while fetching Biometric data : "+e.getMessage()); + } + return response; + } +} \ No newline at end of file diff --git a/src/main/java/com/iemr/common/service/biometric/BiometricService.java b/src/main/java/com/iemr/common/service/biometric/BiometricService.java new file mode 100644 index 00000000..af3d400d --- /dev/null +++ b/src/main/java/com/iemr/common/service/biometric/BiometricService.java @@ -0,0 +1,7 @@ +package com.iemr.common.service.biometric; + +public interface BiometricService { + + String getBioData(String pid); + +} diff --git a/src/main/java/com/iemr/common/service/biometric/BiometricServiceImpl.java b/src/main/java/com/iemr/common/service/biometric/BiometricServiceImpl.java new file mode 100644 index 00000000..422f396a --- /dev/null +++ b/src/main/java/com/iemr/common/service/biometric/BiometricServiceImpl.java @@ -0,0 +1,229 @@ +package com.iemr.common.service.biometric; + +import java.io.BufferedReader; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.URI; +import java.net.URL; +import java.util.stream.Collectors; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.apache.http.HttpEntity; +import org.apache.http.HttpHeaders; +import org.apache.http.HttpResponse; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; +import org.apache.http.client.methods.HttpRequestBase; +import org.apache.http.client.methods.HttpUriRequest; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; +import org.json.JSONObject; +import org.json.XML; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Text; + +@Service +public class BiometricServiceImpl implements BiometricService { + private Logger logger = LoggerFactory.getLogger(this.getClass().getSimpleName()); + + @Value("${biometric.discover.url}") + String discoverUrl; + + @Value("${biometric.deviceInfo.url}") + private String deviceInfoUrl; + + @Value("${biometric.capture.url}") + private String captureUrl; + + @Override + public String getBioData(String env) { + String pidRes = null; + try { + String portNumber = null; + for (int i = 11100; i <= 11120; i++) { + String port = String.valueOf(i); + String apiUrl = discoverUrl.replace("port", port); + + String httpMethod = "RDSERVICE"; + HttpClient httpClient = HttpClientBuilder.create().build(); + + HttpRequestBase request = new HttpRequestBase() { + @Override + public String getMethod() { + return httpMethod; + } + }; + request.setURI(URI.create(apiUrl)); + request.setHeader(HttpHeaders.CONTENT_TYPE, "TEXT/XML"); + try { + HttpResponse response = httpClient.execute((HttpUriRequest) request); + + HttpEntity entity = response.getEntity(); + String responseString = EntityUtils.toString(entity, "UTF-8"); + if (!responseString.contains("NOTREADY")) { + portNumber = port; + break; + } + } catch (Exception e) { + logger.error("Error while checking biometric status: " + e.getMessage()); + } + } + String xmlResponse = getDeviceInfo(portNumber, env); + if(xmlResponse != null) { + JSONObject xmlJSONObj = XML.toJSONObject(xmlResponse); + String pidData = xmlJSONObj.get("PidData").toString(); + JSONObject obj = new JSONObject(pidData); + String content = obj.get("Data").toString(); + JSONObject obj1 = new JSONObject(content); + pidRes = obj1.get("content").toString(); + } + } catch (Exception e) { + logger.error("Error while getting biometric data: " + e.getMessage()); + } + return pidRes; + } + + private String getDeviceInfo(String portNumber, String env) { + String deviceRes = null; + String deviceInforURL = deviceInfoUrl.replace("port", portNumber); + + String httpMethod = "DEVICEINFO"; + HttpClient httpClient = HttpClientBuilder.create().build(); + + HttpRequestBase request = new HttpRequestBase() { + @Override + public String getMethod() { + return httpMethod; + } + }; + request.setURI(URI.create(deviceInforURL)); + request.setHeader(HttpHeaders.CONTENT_TYPE, "TEXT/XML"); + try { + HttpResponse response = httpClient.execute((HttpUriRequest) request); + + HttpEntity entity = response.getEntity(); + String responseString = EntityUtils.toString(entity, "UTF-8"); + + if (null != response && response.getStatusLine().getStatusCode() == 200) { + deviceRes = getCaptureInfo(portNumber, env); + } + } catch (Exception e) { + logger.error("Error while getting biometric device info : " + e.getMessage()); + } + return deviceRes; + } + + private String getCaptureInfo(String portNumber, String env) throws Exception { + String captureRes = null; + if (null != portNumber) { + try { + // Create a new DocumentBuilder + DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder docBuilder = dbFactory.newDocumentBuilder(); + + // Create a new Document + Document doc = docBuilder.newDocument(); + + // Create the root element + Element pidOptionsElement = doc.createElement("PidOptions"); + pidOptionsElement.setAttribute("ver", "1.0"); + doc.appendChild(pidOptionsElement); + + // Create the element and its attributes + Element optsElement = doc.createElement("Opts"); + optsElement.setAttribute("fCount", "1"); + optsElement.setAttribute("fType", "0"); + optsElement.setAttribute("iCount", "0"); + optsElement.setAttribute("pCount", "0"); + optsElement.setAttribute("pgCount", "2"); + optsElement.setAttribute("format", "0"); + optsElement.setAttribute("pidVer", "2.0"); + optsElement.setAttribute("timeout", "10000"); + optsElement.setAttribute("pTimeout", "20000"); + optsElement.setAttribute("posh", "UNKNOWN"); + optsElement.setAttribute("env", env); + pidOptionsElement.appendChild(optsElement); + + // Create the element + Element custOptsElement = doc.createElement("CustOpts"); + pidOptionsElement.appendChild(custOptsElement); + + // Create the element inside with its attributes + Element paramElement = doc.createElement("Param"); + paramElement.setAttribute("name", "mantrakey"); + paramElement.setAttribute("value", ""); + custOptsElement.appendChild(paramElement); + + // Create a text node for indentation + Text text = doc.createTextNode("\n"); + pidOptionsElement.insertBefore(text, optsElement); + + // Print the XML content to the console + String xml = XMLToString(doc); + + String captureUrl = "http://127.0.0.1:port/rd/capture"; + String capturingURL = captureUrl.replace("port", portNumber); + URL url = new URL(capturingURL); + + System.out.println(capturingURL); + + String httpMethod = "CAPTURE"; + HttpClient httpClient = HttpClients.createDefault(); + + HttpEntityEnclosingRequestBase request = new HttpEntityEnclosingRequestBase() { + @Override + public String getMethod() { + return httpMethod; + } + }; + request.setURI(URI.create(capturingURL)); + StringEntity entity2 = new StringEntity(xml); + request.setEntity(entity2); + request.setHeader(HttpHeaders.CONTENT_TYPE, "TEXT/XML"); + + HttpResponse response = httpClient.execute(request); + response.setHeader("Content-Type", "TEXT/XML"); + InputStream entity1 = response.getEntity().getContent(); + String result = new BufferedReader(new InputStreamReader(entity1)) + .lines().collect(Collectors.joining("\n")); + if(result.contains("Success")) { + captureRes = result; + } else { + throw new Exception("Capture timed out"); + } + + } catch (ParserConfigurationException e) { + logger.error("Error while capturing fingerprint: " + e.getMessage()); + } + } + return captureRes; + } + + private String XMLToString(Document doc) { + String response = null; + try { + javax.xml.transform.TransformerFactory tf = javax.xml.transform.TransformerFactory.newInstance(); + javax.xml.transform.Transformer transformer = tf.newTransformer(); + transformer.setOutputProperty(javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, "yes"); + javax.xml.transform.stream.StreamResult result = new javax.xml.transform.stream.StreamResult( + new java.io.StringWriter()); + javax.xml.transform.dom.DOMSource source = new javax.xml.transform.dom.DOMSource(doc); + transformer.transform(source, result); + response = result.getWriter().toString(); + } catch (Exception e) { + logger.error("Error while converting XML to String: " + e.getMessage()); + } + return response; + } + +} From dac5309d057e8bc2241ffaa594324871c65b23a0 Mon Sep 17 00:00:00 2001 From: Ancy Riju Date: Thu, 28 Sep 2023 15:00:10 +0530 Subject: [PATCH 2/3] biometric device changes --- .../common/controller/biometric/BiometricController.java | 6 ++++-- .../iemr/common/service/biometric/BiometricServiceImpl.java | 3 --- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/iemr/common/controller/biometric/BiometricController.java b/src/main/java/com/iemr/common/controller/biometric/BiometricController.java index fa41e914..c95cad9b 100644 --- a/src/main/java/com/iemr/common/controller/biometric/BiometricController.java +++ b/src/main/java/com/iemr/common/controller/biometric/BiometricController.java @@ -4,6 +4,7 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; @@ -28,8 +29,9 @@ public class BiometricController { @Autowired private BiometricService biometricService; @CrossOrigin - @RequestMapping(value = { "/getBiometricData/{pid}" }, method = { RequestMethod.GET }) - public OutputResponse saveBenNurseDataCAC(@RequestParam String pid) { + @RequestMapping(value = { "/getBiometricData/{pid}" }, method = { RequestMethod.GET }, headers = "Authorization") + public OutputResponse saveBenNurseDataCAC(@PathVariable("pid") String pid, + @RequestHeader(value = "Authorization") String Authorization) { OutputResponse response = new OutputResponse(); try { String pidResponse = biometricService.getBioData(pid); diff --git a/src/main/java/com/iemr/common/service/biometric/BiometricServiceImpl.java b/src/main/java/com/iemr/common/service/biometric/BiometricServiceImpl.java index 422f396a..fec438ea 100644 --- a/src/main/java/com/iemr/common/service/biometric/BiometricServiceImpl.java +++ b/src/main/java/com/iemr/common/service/biometric/BiometricServiceImpl.java @@ -171,12 +171,9 @@ private String getCaptureInfo(String portNumber, String env) throws Exception { // Print the XML content to the console String xml = XMLToString(doc); - String captureUrl = "http://127.0.0.1:port/rd/capture"; String capturingURL = captureUrl.replace("port", portNumber); URL url = new URL(capturingURL); - System.out.println(capturingURL); - String httpMethod = "CAPTURE"; HttpClient httpClient = HttpClients.createDefault(); From d20443f5f412e65eba4963d8ce1af56f78f37c41 Mon Sep 17 00:00:00 2001 From: Ancy Riju Date: Wed, 4 Oct 2023 19:35:30 +0530 Subject: [PATCH 3/3] biometric pid changes --- .../controller/biometric/BiometricController.java | 4 ++-- .../service/biometric/BiometricServiceImpl.java | 11 ++++------- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/iemr/common/controller/biometric/BiometricController.java b/src/main/java/com/iemr/common/controller/biometric/BiometricController.java index c95cad9b..79546c87 100644 --- a/src/main/java/com/iemr/common/controller/biometric/BiometricController.java +++ b/src/main/java/com/iemr/common/controller/biometric/BiometricController.java @@ -30,7 +30,7 @@ public class BiometricController { private BiometricService biometricService; @CrossOrigin @RequestMapping(value = { "/getBiometricData/{pid}" }, method = { RequestMethod.GET }, headers = "Authorization") - public OutputResponse saveBenNurseDataCAC(@PathVariable("pid") String pid, + public String saveBenNurseDataCAC(@PathVariable("pid") String pid, @RequestHeader(value = "Authorization") String Authorization) { OutputResponse response = new OutputResponse(); try { @@ -39,6 +39,6 @@ public OutputResponse saveBenNurseDataCAC(@PathVariable("pid") String pid, }catch (Exception e) { logger.error("Error while fetching Biometric data : "+e.getMessage()); } - return response; + return response.toString(); } } \ No newline at end of file diff --git a/src/main/java/com/iemr/common/service/biometric/BiometricServiceImpl.java b/src/main/java/com/iemr/common/service/biometric/BiometricServiceImpl.java index fec438ea..0789ee86 100644 --- a/src/main/java/com/iemr/common/service/biometric/BiometricServiceImpl.java +++ b/src/main/java/com/iemr/common/service/biometric/BiometricServiceImpl.java @@ -5,6 +5,7 @@ import java.io.InputStreamReader; import java.net.URI; import java.net.URL; +import java.util.Base64; import java.util.stream.Collectors; import javax.xml.parsers.DocumentBuilder; @@ -80,12 +81,7 @@ public String getMethod() { } String xmlResponse = getDeviceInfo(portNumber, env); if(xmlResponse != null) { - JSONObject xmlJSONObj = XML.toJSONObject(xmlResponse); - String pidData = xmlJSONObj.get("PidData").toString(); - JSONObject obj = new JSONObject(pidData); - String content = obj.get("Data").toString(); - JSONObject obj1 = new JSONObject(content); - pidRes = obj1.get("content").toString(); + pidRes = Base64.getEncoder().encodeToString(xmlResponse.getBytes()); } } catch (Exception e) { logger.error("Error while getting biometric data: " + e.getMessage()); @@ -142,7 +138,7 @@ private String getCaptureInfo(String portNumber, String env) throws Exception { // Create the element and its attributes Element optsElement = doc.createElement("Opts"); optsElement.setAttribute("fCount", "1"); - optsElement.setAttribute("fType", "0"); + optsElement.setAttribute("fType", "2"); optsElement.setAttribute("iCount", "0"); optsElement.setAttribute("pCount", "0"); optsElement.setAttribute("pgCount", "2"); @@ -150,6 +146,7 @@ private String getCaptureInfo(String portNumber, String env) throws Exception { optsElement.setAttribute("pidVer", "2.0"); optsElement.setAttribute("timeout", "10000"); optsElement.setAttribute("pTimeout", "20000"); + optsElement.setAttribute("wadh", "E0jzJ/P8UopUHAieZn8CKqS4WPMi5ZSYXgfnlfkWjrc="); optsElement.setAttribute("posh", "UNKNOWN"); optsElement.setAttribute("env", env); pidOptionsElement.appendChild(optsElement);