From eeb914635a3c456bfdc0c11f04da59bbba294c5d Mon Sep 17 00:00:00 2001 From: Rahul Ramesh Date: Sat, 2 Dec 2023 21:41:36 +0530 Subject: [PATCH] [Rahul] | BAH-3382 | Add. FreeMarker As Default Template Engine --- openerp-client/pom.xml | 13 +++- .../openerp/web/config/FreeMarkerConfig.java | 20 ++++++ .../web/request/builder/Parameter.java | 11 ++-- .../web/request/builder/RequestBuilder.java | 59 +++++++++++------ .../request/template/rest_template.ftl | 10 +++ .../request/template/xml_template.ftl | 31 +++++++++ .../request/template/xml_template.vm | 31 --------- .../request/builder/RequestBuilderTest.java | 63 +++++++++++++++++-- pom.xml | 5 -- 9 files changed, 173 insertions(+), 70 deletions(-) create mode 100644 openerp-client/src/main/java/org/bahmni/openerp/web/config/FreeMarkerConfig.java create mode 100644 openerp-client/src/main/resources/request/template/rest_template.ftl create mode 100644 openerp-client/src/main/resources/request/template/xml_template.ftl delete mode 100644 openerp-client/src/main/resources/request/template/xml_template.vm diff --git a/openerp-client/pom.xml b/openerp-client/pom.xml index 95f7278a..63ad01ab 100644 --- a/openerp-client/pom.xml +++ b/openerp-client/pom.xml @@ -70,8 +70,15 @@ - org.apache.velocity - velocity-engine-core + org.freemarker + freemarker + 2.3.32 + + + org.slf4j + slf4j-log4j12 + + org.apache.httpcomponents @@ -171,7 +178,7 @@ jackson-dataformat-xml 2.15.2 - + commons-lang commons-lang 2.6 diff --git a/openerp-client/src/main/java/org/bahmni/openerp/web/config/FreeMarkerConfig.java b/openerp-client/src/main/java/org/bahmni/openerp/web/config/FreeMarkerConfig.java new file mode 100644 index 00000000..066ea83d --- /dev/null +++ b/openerp-client/src/main/java/org/bahmni/openerp/web/config/FreeMarkerConfig.java @@ -0,0 +1,20 @@ +package org.bahmni.openerp.web.config; + +import freemarker.template.Configuration; +import freemarker.template.TemplateModelException; + +import java.io.IOException; + +public class FreeMarkerConfig { + + private static Configuration config; + + public static Configuration getConfiguration() throws IOException, TemplateModelException { + if (config == null) { + config = new Configuration(Configuration.VERSION_2_3_23); + config.setClassForTemplateLoading(FreeMarkerConfig.class, "/request/template/"); + config.setDefaultEncoding("UTF-8"); + } + return config; + } +} diff --git a/openerp-client/src/main/java/org/bahmni/openerp/web/request/builder/Parameter.java b/openerp-client/src/main/java/org/bahmni/openerp/web/request/builder/Parameter.java index f471ce5a..bdd79fac 100644 --- a/openerp-client/src/main/java/org/bahmni/openerp/web/request/builder/Parameter.java +++ b/openerp-client/src/main/java/org/bahmni/openerp/web/request/builder/Parameter.java @@ -1,17 +1,14 @@ package org.bahmni.openerp.web.request.builder; - -import org.apache.commons.lang3.StringEscapeUtils; - public class Parameter { - private String name; - private String value; - private String type; + private final String name; + private final String value; + private final String type; public Parameter(String name, String value, String type) { this.name = name; - this.value = StringEscapeUtils.escapeXml(value); + this.value = value; this.type = type; } diff --git a/openerp-client/src/main/java/org/bahmni/openerp/web/request/builder/RequestBuilder.java b/openerp-client/src/main/java/org/bahmni/openerp/web/request/builder/RequestBuilder.java index 01fab54f..033a2982 100644 --- a/openerp-client/src/main/java/org/bahmni/openerp/web/request/builder/RequestBuilder.java +++ b/openerp-client/src/main/java/org/bahmni/openerp/web/request/builder/RequestBuilder.java @@ -1,36 +1,59 @@ package org.bahmni.openerp.web.request.builder; -import org.apache.velocity.Template; -import org.apache.velocity.VelocityContext; -import org.apache.velocity.app.VelocityEngine; -import org.apache.velocity.runtime.RuntimeConstants; -import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader; + +import freemarker.template.Template; import org.bahmni.openerp.web.OpenERPException; +import org.bahmni.openerp.web.config.FreeMarkerConfig; import org.bahmni.openerp.web.request.OpenERPRequest; import org.springframework.stereotype.Service; import java.io.StringWriter; +import java.util.HashMap; @Service public class RequestBuilder { public static String buildNewXMLRequest(OpenERPRequest openERPRequest, Object id, String database, String password) { try { - VelocityEngine velocityEngine = new VelocityEngine(); - velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath"); - velocityEngine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName()); - velocityEngine.init(); - Template template = velocityEngine.getTemplate("request/template/xml_template.vm"); - VelocityContext context = new VelocityContext(); - context.put("parametersList", openERPRequest.getParameters()); - context.put("id", id); - context.put("database", database); - context.put("password", password); - context.put("resource", openERPRequest.getResource()); - context.put("operation", openERPRequest.getOperation()); + HashMap context = getXMLContext(openERPRequest, id, database, password); + return buildRequest("xml_template.ftl", context); + } catch (Exception e) { + throw new OpenERPException(e); + } + } + + private static HashMap getXMLContext(OpenERPRequest openERPRequest, Object id, String database, String password) { + HashMap context = new HashMap<>(); + context.put("parametersList", openERPRequest.getParameters()); + context.put("id", id); + context.put("database", database); + context.put("password", password); + context.put("resource", openERPRequest.getResource()); + context.put("operation", openERPRequest.getOperation()); + return context; + } + + public static String buildNewRestRequest(OpenERPRequest openERPRequest, String id) { + try { + HashMap context = getRestContext(openERPRequest, id); + return buildRequest("rest_template.ftl", context); + } catch (Exception e) { + throw new OpenERPException(e); + } + } + + private static HashMap getRestContext(OpenERPRequest openERPRequest, String id) { + HashMap context = new HashMap<>(); + context.put("parametersList", openERPRequest.getParameters()); + context.put("id", id); + return context; + } + private static String buildRequest(String templateName, HashMap context){ + try { + Template template= FreeMarkerConfig.getConfiguration().getTemplate(templateName); StringWriter writer = new StringWriter(); - template.merge(context, writer); + template.process(context, writer); return writer.toString(); } catch (Exception e) { throw new OpenERPException(e); diff --git a/openerp-client/src/main/resources/request/template/rest_template.ftl b/openerp-client/src/main/resources/request/template/rest_template.ftl new file mode 100644 index 00000000..246ee41e --- /dev/null +++ b/openerp-client/src/main/resources/request/template/rest_template.ftl @@ -0,0 +1,10 @@ +{ + "id": "${id}", + "params": { + "data": { + <#list parametersList as param> + "${param.name}": "${param.value}"<#if param_has_next>, + + } + } +} \ No newline at end of file diff --git a/openerp-client/src/main/resources/request/template/xml_template.ftl b/openerp-client/src/main/resources/request/template/xml_template.ftl new file mode 100644 index 00000000..fbceb0a7 --- /dev/null +++ b/openerp-client/src/main/resources/request/template/xml_template.ftl @@ -0,0 +1,31 @@ + + + execute + + + ${database} + + + ${id} + + + ${password} + + + ${resource} + + + ${operation} + + + + <#list parametersList as param> + + ${param.name} + <${param.type}>${(param.value?xml)!} + + + + + + \ No newline at end of file diff --git a/openerp-client/src/main/resources/request/template/xml_template.vm b/openerp-client/src/main/resources/request/template/xml_template.vm deleted file mode 100644 index 755cce27..00000000 --- a/openerp-client/src/main/resources/request/template/xml_template.vm +++ /dev/null @@ -1,31 +0,0 @@ - - - execute - - - $database - - - $id - - - $password - - - $resource - - - $operation - - - - #foreach( $param in $parametersList ) - - $param.name - <$param.type>$!param.value - - #end - - - - \ No newline at end of file diff --git a/openerp-client/src/test/java/org/bahmni/openerp/web/request/builder/RequestBuilderTest.java b/openerp-client/src/test/java/org/bahmni/openerp/web/request/builder/RequestBuilderTest.java index 2092b4d0..3dff89ee 100644 --- a/openerp-client/src/test/java/org/bahmni/openerp/web/request/builder/RequestBuilderTest.java +++ b/openerp-client/src/test/java/org/bahmni/openerp/web/request/builder/RequestBuilderTest.java @@ -2,16 +2,14 @@ import org.bahmni.openerp.web.request.OpenERPRequest; import org.junit.Test; - import java.util.ArrayList; import java.util.List; - import static org.junit.Assert.assertEquals; public class RequestBuilderTest { @Test - public void shouldCreateNewCustomerRequestWithPatientDataPopulated() throws Exception { + public void shouldCreateNewXMLCustomerRequestWithPatientDataPopulated() throws Exception { int id = 1; String patientName = "Ramu"; @@ -29,7 +27,6 @@ public void shouldCreateNewCustomerRequestWithPatientDataPopulated() throws Exce parameters.add(villageParam); OpenERPRequest request = new OpenERPRequest("res.partner", "execute", parameters); String requestXml = RequestBuilder.buildNewXMLRequest(request, id, database, password); - String requestXmlForComparison = requestXml.replace("", " "); String expected = "\n" + "" + @@ -73,7 +70,29 @@ public void shouldCreateNewCustomerRequestWithPatientDataPopulated() throws Exce } @Test - public void shouldEscapeSpecialCharacters() throws Exception { + public void shouldCreateNewRESTCustomerRequestWithPatientDataPopulated() throws Exception { + + String id = "3a0ee6b3-4e3a-425d-a2ef-806ccb0d1744"; + String patientName = "Ramu"; + String patientId = "13466"; + String village = "Ganiyari"; + String expected = "{\"id\": \"3a0ee6b3-4e3a-425d-a2ef-806ccb0d1744\",\"params\": {\"data\": {\"name\": \"Ramu\",\"ref\": \"13466\",\"village\": \"Ganiyari\"}}\n}"; + + Parameter name = new Parameter("name", patientName, "string"); + Parameter ref = new Parameter("ref", patientId, "string"); + Parameter villageParam = new Parameter("village", village, "string"); + List parameters = new ArrayList(); + parameters.add(name); + parameters.add(ref); + parameters.add(villageParam); + OpenERPRequest request = new OpenERPRequest("res.partner", "execute", parameters); + String requestXml = RequestBuilder.buildNewRestRequest(request, id); + + comparingStringWithoutSpaces(requestXml, expected); + } + + @Test + public void shouldEscapeSpecialCharactersWhenXMLRequestIsCreated() throws Exception { String income = "<=10000"; int id = 1; @@ -122,7 +141,22 @@ public void shouldEscapeSpecialCharacters() throws Exception { } @Test - public void shouldEscapeHindiCharacters() throws Exception { + public void shouldEscapeSpecialCharactersWhenRESTRequestIsCreated() throws Exception { + String income = "<=10000"; + String id = "f1cfccea-6f5e-4d6b-9759-f55e92cd4f8f"; + String expected = "{\"id\": \"f1cfccea-6f5e-4d6b-9759-f55e92cd4f8f\",\"params\": {\"data\": {\"income\": \"<=10000\"}}\n}"; + + Parameter parameter = new Parameter("income", income, "string"); + ArrayList parameters = new ArrayList(); + parameters.add(parameter); + OpenERPRequest request = new OpenERPRequest("res.partner", "execute", parameters); + String requestXml = RequestBuilder.buildNewRestRequest(request, id); + + comparingStringWithoutSpaces(requestXml, expected); + } + + @Test + public void shouldEscapeHindiCharactersWhenXMLRequestIsCreated() throws Exception { String name = "कृपा"; int id = 1; @@ -170,6 +204,23 @@ public void shouldEscapeHindiCharacters() throws Exception { comparingStringWithoutSpaces(requestXml, expected); } + @Test + public void shouldEscapeHindiCharactersWhenRESTRequestIsCreated() throws Exception { + + String name = "कृपा"; + String id = "6397d716-5ec8-47c6-b5c8-dd49e3e09235"; + String expected = "{\"id\": \"6397d716-5ec8-47c6-b5c8-dd49e3e09235\",\"params\": {\"data\": {\"name\": \"कृपा\"}}\n}"; + + Parameter parameter = new Parameter("name", name, "string"); + ArrayList parameters = new ArrayList(); + parameters.add(parameter); + OpenERPRequest request = new OpenERPRequest("res.partner", "execute", parameters); + + String requestXml = RequestBuilder.buildNewRestRequest(request, id); + + comparingStringWithoutSpaces(requestXml, expected); + } + private void comparingStringWithoutSpaces(String requestXml, String expected) { assertEquals(expected.replaceAll("\\s{2,}", ""), requestXml.replaceAll("\\s{2,}", "").trim()); } diff --git a/pom.xml b/pom.xml index f287868b..a4b4ef7c 100644 --- a/pom.xml +++ b/pom.xml @@ -206,11 +206,6 @@ 1.0.0.M1 test - - org.apache.velocity - velocity-engine-core - 2.0 - org.apache.httpcomponents