diff --git a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/AtomfeedServiceConstants.java b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/AtomfeedServiceConstants.java new file mode 100644 index 00000000..ca9eab5b --- /dev/null +++ b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/AtomfeedServiceConstants.java @@ -0,0 +1,15 @@ +package org.bahmni.feed.openerp; + +public enum AtomfeedServiceConstants { + IS_REST_ENABLED("IS_ODOO_16"); + + private final String atomfeedServiceConstants; + + AtomfeedServiceConstants(String atomfeedServiceConstants) { + this.atomfeedServiceConstants = atomfeedServiceConstants; + } + + public String getAtomfeedServiceConstants() { + return atomfeedServiceConstants; + } +} \ No newline at end of file diff --git a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/OpenERPAtomFeedProperties.java b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/OpenERPAtomFeedProperties.java index 6c7e6ecf..753500a5 100644 --- a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/OpenERPAtomFeedProperties.java +++ b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/OpenERPAtomFeedProperties.java @@ -67,8 +67,8 @@ public String getFeedUriForJob(Jobs feedJob) { throw new RuntimeException("Can not identify feed URI for requested Job."); } - public String getOdooURIForJob(Jobs feedJob, boolean isOdoo16) { - if (isOdoo16) { + public String getOdooURIForJob(Jobs feedJob, boolean isRestEnabled) { + if (isRestEnabled) { switch (feedJob) { case CUSTOMER_FEED: case SALEORDER_FEED: @@ -79,7 +79,7 @@ public String getOdooURIForJob(Jobs feedJob, boolean isOdoo16) { } } else { - return "xmlrpc/object"; + return "/xmlrpc/object"; } } @Value("${openerp.host}") diff --git a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/client/AtomFeedClientHelper.java b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/client/AtomFeedClientHelper.java index 5afd2ba5..1c4dc81c 100644 --- a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/client/AtomFeedClientHelper.java +++ b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/client/AtomFeedClientHelper.java @@ -1,5 +1,6 @@ package org.bahmni.feed.openerp.client; +import org.bahmni.feed.openerp.AtomfeedServiceConstants; import org.bahmni.feed.openerp.FeedException; import org.bahmni.feed.openerp.OpenERPAtomFeedProperties; import org.bahmni.feed.openerp.job.Jobs; @@ -16,6 +17,7 @@ import org.ict4h.atomfeed.client.repository.jdbc.AllMarkersJdbcImpl; import org.ict4h.atomfeed.client.service.FeedClient; import org.ict4h.atomfeed.server.transaction.AtomFeedSpringTransactionSupport; +import org.springframework.core.env.Environment; public class AtomFeedClientHelper { private final OpenERPAtomFeedProperties atomFeedProperties; @@ -24,15 +26,17 @@ public class AtomFeedClientHelper { private final OdooRESTClient odooRESTClient; private FeedClientFactory feedClientFactory; private final WebClientProvider webClientProvider; + private final Environment environment; - public AtomFeedClientHelper(OpenERPAtomFeedProperties atomFeedProperties, AtomFeedSpringTransactionSupport transactionManager, OpenERPXMLClient openERPXMLClient, OdooRESTClient odooRESTClient) { + public AtomFeedClientHelper(OpenERPAtomFeedProperties atomFeedProperties, Environment environment, AtomFeedSpringTransactionSupport transactionManager, OpenERPXMLClient openERPXMLClient, OdooRESTClient odooRESTClient) { this.atomFeedProperties = atomFeedProperties; this.transactionManager = transactionManager; this.openERPXMLClient = openERPXMLClient; - this.odooRESTClient = odooRESTClient; this.webClientProvider = new WebClientProvider(atomFeedProperties); + this.odooRESTClient = odooRESTClient; + this.environment = environment; } - + public FeedClient getAtomFeedClient(Jobs jobName) throws FeedException { if(this.feedClientFactory == null){ WorkerFactory workerFactory = new WorkerFactory(webClientProvider); @@ -46,9 +50,10 @@ FeedClient getAtomFeedClient(Jobs jobName, FeedClientFactory feedClientFactory) AllFeeds allFeeds = getAllFeeds(atomFeedProperties, cookies); AllMarkers allMarkers = new AllMarkersJdbcImpl(transactionManager); AllFailedEvents allFailedEvents = new AllFailedEventsJdbcImpl(transactionManager); - //TODO: Create a toggle between rest and xml client - OpenERPContext openERPContext = new OpenERPContext(openERPXMLClient); - return feedClientFactory.getFeedClient(atomFeedProperties, transactionManager, openERPContext, allFeeds, allMarkers, allFailedEvents, jobName); + String isRestEnabled = environment.getProperty(AtomfeedServiceConstants.IS_REST_ENABLED.getAtomfeedServiceConstants()); + boolean isRestEnabledValue = Boolean.parseBoolean(isRestEnabled); + OpenERPContext openERPContext = isRestEnabledValue ? new OpenERPContext(odooRESTClient) : new OpenERPContext(openERPXMLClient); + return feedClientFactory.getFeedClient(atomFeedProperties, transactionManager, openERPContext, allFeeds, allMarkers, allFailedEvents, jobName, isRestEnabledValue); } static AllFeeds getAllFeeds(OpenERPAtomFeedProperties atomFeedProperties, ClientCookies cookies) { @@ -59,4 +64,4 @@ static AllFeeds getAllFeeds(OpenERPAtomFeedProperties atomFeedProperties, Client return new AllFeeds(feedProperties, cookies); } -} +} \ No newline at end of file diff --git a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/client/FeedClientFactory.java b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/client/FeedClientFactory.java index 08fa620a..70574ff2 100644 --- a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/client/FeedClientFactory.java +++ b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/client/FeedClientFactory.java @@ -31,9 +31,9 @@ public FeedClientFactory(WorkerFactory workerFactory) { this.workerFactory = workerFactory; } - public AtomFeedClient getFeedClient(OpenERPAtomFeedProperties openERPAtomFeedProperties, AtomFeedSpringTransactionSupport transactionManager, OpenERPContext openERPContext, AllFeeds allFeeds, AllMarkers allMarkers, AllFailedEvents allFailedEvents, Jobs jobName) { + public AtomFeedClient getFeedClient(OpenERPAtomFeedProperties openERPAtomFeedProperties, AtomFeedSpringTransactionSupport transactionManager, OpenERPContext openERPContext, AllFeeds allFeeds, AllMarkers allMarkers, AllFailedEvents allFailedEvents, Jobs jobName, Boolean isRestEnabled) { String feedUri = openERPAtomFeedProperties.getFeedUriForJob(jobName); - String odooURL = openERPAtomFeedProperties.getOdooURIForJob(jobName,false); + String odooURL = openERPAtomFeedProperties.getOdooURIForJob(jobName, isRestEnabled); if (StringUtils.isBlank(feedUri)) { String message = String.format("No feed-uri defined for Job [%s][%s]", jobName, jobName.getFeedUriRef()); logger.warn(message); diff --git a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/OpenERPDrugServiceEventWorker.java b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/OpenERPDrugServiceEventWorker.java index adba81f5..aa78fa11 100644 --- a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/OpenERPDrugServiceEventWorker.java +++ b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/OpenERPDrugServiceEventWorker.java @@ -51,31 +51,33 @@ private OpenERPRequest mapRequest(Event event) throws IOException { return new OpenERPRequest("atom.event.worker", "process_event", buildParameters(event,drug)); } - private List buildParameters(Event event,OpenMRSDrug drug) { + private List buildParameters(Event event, OpenMRSDrug drug) { List parameters = new ArrayList<>(); - parameters.add(new Parameter("name",drug.getName())); - parameters.add(new Parameter("shortName",drug.getShortName())); - parameters.add(new Parameter("uuid",drug.getUuid())); - parameters.add(new Parameter("combination",drug.getCombination())); - parameters.add(new Parameter("strength",drug.getStrength())); - parameters.add(new Parameter("dosageForm",drug.getDosageForm())); - parameters.add(new Parameter("genericName",drug.getGenericName())); - parameters.add(new Parameter("maximumDose",drug.getMaximumDose())); - parameters.add(new Parameter("minimumDose",drug.getMinimumDose())); - - parameters.add(new Parameter("last_read_entry_id",event.getId())); - parameters.add(new Parameter("category", "create.drug")); - parameters.add(new Parameter("feed_uri_for_last_read_entry",event.getFeedUri())); - + addToParametersIfNotEmpty(parameters, "name", drug.getName()); + addToParametersIfNotEmpty(parameters, "shortName", drug.getShortName()); + addToParametersIfNotEmpty(parameters, "uuid", drug.getUuid()); + addToParametersIfNotEmpty(parameters, "combination", drug.getCombination()); + addToParametersIfNotEmpty(parameters, "strength", drug.getStrength()); + addToParametersIfNotEmpty(parameters, "dosageForm", drug.getDosageForm()); + addToParametersIfNotEmpty(parameters, "genericName", drug.getGenericName()); + addToParametersIfNotEmpty(parameters, "maximumDose", drug.getMaximumDose()); + addToParametersIfNotEmpty(parameters, "minimumDose", drug.getMinimumDose()); + addToParametersIfNotEmpty(parameters, "last_read_entry_id", event.getId()); + addToParametersIfNotEmpty(parameters, "category", "create.drug"); + addToParametersIfNotEmpty(parameters, "feed_uri_for_last_read_entry", event.getFeedUri()); if (event.getFeedUri() == null) { - parameters.add(new Parameter("is_failed_event","1","boolean")); + parameters.add(new Parameter("is_failed_event", "1", "boolean")); } - return parameters; } + private void addToParametersIfNotEmpty(List parameters, String name, String value) { + if (value != null && !value.isEmpty()) { + parameters.add(new Parameter(name, value)); + } + } + @Override public void cleanUp(Event event) { - } -} +} \ No newline at end of file diff --git a/openerp-atomfeed-service/src/main/resources/jobs-context.xml b/openerp-atomfeed-service/src/main/resources/jobs-context.xml index 31480461..2eb0d195 100644 --- a/openerp-atomfeed-service/src/main/resources/jobs-context.xml +++ b/openerp-atomfeed-service/src/main/resources/jobs-context.xml @@ -248,9 +248,10 @@ + - + diff --git a/openerp-client/src/main/java/org/bahmni/openerp/web/client/strategy/OpenERPClientStrategy.java b/openerp-client/src/main/java/org/bahmni/openerp/web/client/strategy/OpenERPClientStrategy.java index ee690b92..4637f78a 100644 --- a/openerp-client/src/main/java/org/bahmni/openerp/web/client/strategy/OpenERPClientStrategy.java +++ b/openerp-client/src/main/java/org/bahmni/openerp/web/client/strategy/OpenERPClientStrategy.java @@ -5,6 +5,5 @@ import java.util.Vector; public interface OpenERPClientStrategy { - Object execute(OpenERPRequest openERPRequest, String URI); - + Object execute(OpenERPRequest openERPRequest, String URL); } \ No newline at end of file diff --git a/openerp-client/src/main/java/org/bahmni/openerp/web/client/strategy/OpenERPContext.java b/openerp-client/src/main/java/org/bahmni/openerp/web/client/strategy/OpenERPContext.java index aecc8ad4..9a13aff2 100644 --- a/openerp-client/src/main/java/org/bahmni/openerp/web/client/strategy/OpenERPContext.java +++ b/openerp-client/src/main/java/org/bahmni/openerp/web/client/strategy/OpenERPContext.java @@ -17,8 +17,8 @@ public OpenERPContext(OpenERPClientStrategy openERPClient) { this.openERPClient = openERPClient; } - public Object execute(OpenERPRequest openERPRequest, String URI) { - return openERPClient.execute(openERPRequest, URI); + public Object execute(OpenERPRequest openERPRequest, String URL) { + return openERPClient.execute(openERPRequest, URL); } } \ No newline at end of file diff --git a/openerp-client/src/main/java/org/bahmni/openerp/web/client/strategy/implementation/OdooRESTClient.java b/openerp-client/src/main/java/org/bahmni/openerp/web/client/strategy/implementation/OdooRESTClient.java index 201c79a4..f414685e 100644 --- a/openerp-client/src/main/java/org/bahmni/openerp/web/client/strategy/implementation/OdooRESTClient.java +++ b/openerp-client/src/main/java/org/bahmni/openerp/web/client/strategy/implementation/OdooRESTClient.java @@ -6,33 +6,28 @@ import org.bahmni.openerp.web.request.OpenERPRequest; import org.bahmni.openerp.web.request.builder.RequestBuilder; import org.springframework.beans.factory.annotation.Autowired; - +import org.springframework.context.annotation.Lazy; +import org.springframework.stereotype.Service; import java.util.UUID; +@Service +@Lazy public class OdooRESTClient implements OpenERPClientStrategy { - private final int connectionTimeoutInMilliseconds; - private final int replyTimeoutInMilliseconds; - private String host; - private final int port; - private final String user; - private final String password; private final RestClient restClient; - @Autowired public OdooRESTClient(OpenERPProperties openERPProperties) { - host = openERPProperties.getHost(); - port = openERPProperties.getPort(); - user = openERPProperties.getUser(); - password = openERPProperties.getPassword(); - restClient = new RestClient("http://" + host + ":" + port, user, password); - connectionTimeoutInMilliseconds = openERPProperties.getConnectionTimeoutInMilliseconds(); - replyTimeoutInMilliseconds = openERPProperties.getReplyTimeoutInMilliseconds(); + final String host = openERPProperties.getHost(); + final int port = openERPProperties.getPort(); + final String user = openERPProperties.getUser(); + final String password = openERPProperties.getPassword(); + final int connectionTimeoutInMilliseconds = openERPProperties.getConnectionTimeoutInMilliseconds(); + restClient = new RestClient("http://" + host + ":" + port, user, password, connectionTimeoutInMilliseconds); } @Override - public Object execute(OpenERPRequest openERPRequest, String URI) { + public Object execute(OpenERPRequest openERPRequest, String URL) { String requestBody = RequestBuilder.buildNewRestRequest(openERPRequest, UUID.randomUUID().toString()); - return restClient.post(URI, requestBody); + return restClient.post(URL, requestBody); } } diff --git a/openerp-client/src/main/java/org/bahmni/openerp/web/client/strategy/implementation/OpenERPXMLClient.java b/openerp-client/src/main/java/org/bahmni/openerp/web/client/strategy/implementation/OpenERPXMLClient.java index 83d6f630..01bd0d35 100644 --- a/openerp-client/src/main/java/org/bahmni/openerp/web/client/strategy/implementation/OpenERPXMLClient.java +++ b/openerp-client/src/main/java/org/bahmni/openerp/web/client/strategy/implementation/OpenERPXMLClient.java @@ -4,8 +4,8 @@ import org.bahmni.openerp.web.OpenERPProperties; import org.bahmni.openerp.web.client.OpenERPResponseErrorValidator; import org.bahmni.openerp.web.client.strategy.OpenERPClientStrategy; +import org.bahmni.openerp.web.http.client.XMLClient; import org.bahmni.openerp.web.request.OpenERPRequest; -import org.bahmni.openerp.web.http.client.HttpClient; import org.apache.xmlrpc.XmlRpcException; import org.apache.xmlrpc.client.XmlRpcClient; import org.apache.xmlrpc.client.XmlRpcClientConfigImpl; @@ -24,7 +24,6 @@ public class OpenERPXMLClient implements OpenERPClientStrategy { public static final String XML_RPC_OBJECT_ENDPOINT = "/xmlrpc/object"; public static final String XML_RPC_COMMON_ENDPOINT = "/xmlrpc/common"; - private final int connectionTimeoutInMilliseconds; private final int replyTimeoutInMilliseconds; private final String host; @@ -32,15 +31,13 @@ public class OpenERPXMLClient implements OpenERPClientStrategy { private final String database; private final String user; private final String password; - private Object id; - private XmlRpcClient xmlRpcClient; - private final HttpClient httpClient; + private final XMLClient XMLClient; @Autowired - public OpenERPXMLClient(HttpClient httpClient, OpenERPProperties openERPProperties) { - this.httpClient = httpClient; + public OpenERPXMLClient(XMLClient XMLClient, OpenERPProperties openERPProperties) { + this.XMLClient = XMLClient; host = openERPProperties.getHost(); port = openERPProperties.getPort(); database = openERPProperties.getDatabase(); @@ -53,12 +50,10 @@ public OpenERPXMLClient(HttpClient httpClient, OpenERPProperties openERPProperti private void login() { if (id == null) { XmlRpcClient loginRpcClient = xmlRpcClient(XML_RPC_COMMON_ENDPOINT); - Vector params = new Vector(); params.addElement(database); params.addElement(user); params.addElement(password); - Object loginId = executeRPC(loginRpcClient, params, "login"); if(loginId == null || loginId.getClass() != Integer.class) throw new OpenERPException(String.format("Failed to login. The login id is : %s", loginId)); @@ -67,12 +62,16 @@ private void login() { } @Override - public Object execute(OpenERPRequest openERPRequest, String URI) { + public Object execute(OpenERPRequest openERPRequest, String URL) { login(); String request = RequestBuilder.buildNewXMLRequest(openERPRequest, id, database, password); - String response = httpClient().post("http://" + host + ":" + port + URI, request); - new OpenERPResponseErrorValidator().checkForError(response); - return response; + try { + String response = httpClient().post("http://" + host + ":" + port + XML_RPC_OBJECT_ENDPOINT, request); + new OpenERPResponseErrorValidator().checkForError(response); + return response; + } catch (Exception e) { + throw new OpenERPException(e); + } } @@ -84,9 +83,9 @@ private Object executeRPC(XmlRpcClient loginRpcClient, Vector params, String met } } - private HttpClient httpClient() { - httpClient.setTimeout(replyTimeoutInMilliseconds); - return httpClient; + private XMLClient httpClient() { + XMLClient.setTimeout(replyTimeoutInMilliseconds); + return XMLClient; } private XmlRpcClient xmlRpcClient(String endpoint) { @@ -95,7 +94,7 @@ private XmlRpcClient xmlRpcClient(String endpoint) { } XmlRpcClientConfigImpl clientConfig = (XmlRpcClientConfigImpl) xmlRpcClient.getClientConfig(); try { - clientConfig.setServerURL(new URL("http", host, port, endpoint)); + clientConfig.setServerURL(new URL("http", host, port, endpoint)); } catch (MalformedURLException e) { throw new OpenERPException(e); } diff --git a/openerp-client/src/main/java/org/bahmni/openerp/web/http/client/RestClient.java b/openerp-client/src/main/java/org/bahmni/openerp/web/http/client/RestClient.java index db613a5c..ead3aafb 100644 --- a/openerp-client/src/main/java/org/bahmni/openerp/web/http/client/RestClient.java +++ b/openerp-client/src/main/java/org/bahmni/openerp/web/http/client/RestClient.java @@ -12,6 +12,7 @@ import org.springframework.http.MediaType; import org.springframework.web.reactive.function.client.WebClient; +import java.time.Duration; import java.util.Arrays; import java.util.UUID; import java.util.function.Consumer; @@ -19,30 +20,28 @@ public class RestClient { private static final Logger logger = LogManager.getLogger(RestClient.class); private WebClient webClient; - private String accessToken; - private final String baseURL; - private final String username; - private final String password; + private final int connectionTimeout; - public RestClient(String baseURL, String username, String password) { + public RestClient(String baseURL, String username, String password, int connectionTimeout) { this.baseURL = baseURL; this.username = username; this.password = password; + this.connectionTimeout = connectionTimeout; } private void login() { if (accessToken == null) { - OpenERPRequest openERPRequest = new OpenERPRequest("res.users", "login", Arrays.asList(new Parameter("username", username, "String"), new Parameter("password", password, "String"))); + OpenERPRequest openERPRequest = new OpenERPRequest("res.users", "login", Arrays.asList(new Parameter("username", username), new Parameter("password", password))); String requestBody = RequestBuilder.buildNewRestRequest(openERPRequest, UUID.randomUUID().toString()); WebClient client = getWebClient(baseURL); HttpHeaders headers = getHttpHeaders(); Consumer consumer = httpHeaders -> httpHeaders.addAll(headers); try{ - String response = client.post().uri("api/odoo-login").headers(consumer).bodyValue(requestBody).retrieve().bodyToMono(String.class).block(); + String response = client.post().uri("api/odoo-login").headers(consumer).bodyValue(requestBody).retrieve().bodyToMono(String.class).timeout(Duration.ofMillis(connectionTimeout)).block(); if (response == null) { throw new OpenERPException("Login failed"); } @@ -57,7 +56,7 @@ private void login() { } } - public String post(String URI, String requestBody) { + public String post(String URL, String requestBody) { try { login(); logger.debug("Post Data: {}", requestBody); @@ -65,15 +64,15 @@ public String post(String URI, String requestBody) { HttpHeaders headers = getHttpHeaders(); headers.set(HttpHeaders.AUTHORIZATION, accessToken); Consumer consumer = httpHeaders -> httpHeaders.addAll(headers); - String response = client.post().uri(URI).headers(consumer).bodyValue(requestBody).retrieve().bodyToMono(String.class).block(); + String response = client.post().uri(URL).headers(consumer).bodyValue(requestBody).retrieve().bodyToMono(String.class).timeout(Duration.ofMillis(connectionTimeout)).block(); if (response == null) { - throw new OpenERPException(String.format("Could not post to %s", URI)); + throw new OpenERPException(String.format("Could not post to %s", URL)); } logger.debug("Post Data output: {}", response); logger.debug("Post Data output: {}", response); return response; } catch (Exception e) { - logger.error("Could not post to {}", URI, e); + logger.error("Could not post to {}", URL, e); logger.error("Post data: {}", requestBody); throw new RuntimeException("Could not post message", e); } diff --git a/openerp-client/src/main/java/org/bahmni/openerp/web/http/client/HttpClient.java b/openerp-client/src/main/java/org/bahmni/openerp/web/http/client/XMLClient.java similarity index 91% rename from openerp-client/src/main/java/org/bahmni/openerp/web/http/client/HttpClient.java rename to openerp-client/src/main/java/org/bahmni/openerp/web/http/client/XMLClient.java index 7165ba29..731032d9 100644 --- a/openerp-client/src/main/java/org/bahmni/openerp/web/http/client/HttpClient.java +++ b/openerp-client/src/main/java/org/bahmni/openerp/web/http/client/XMLClient.java @@ -12,14 +12,14 @@ import org.springframework.web.client.RestTemplate; @Component -public class HttpClient { - private static final Logger logger = LogManager.getLogger(HttpClient.class); - private RestTemplate restTemplate; +public class XMLClient { + private static final Logger logger = LogManager.getLogger(XMLClient.class); + private final RestTemplate restTemplate; private boolean isTimeoutSet; @Autowired - public HttpClient(RestTemplate restTemplate) { + public XMLClient(RestTemplate restTemplate) { this.restTemplate = restTemplate; } diff --git a/pom.xml b/pom.xml index a71b52be..1eea9fe8 100644 --- a/pom.xml +++ b/pom.xml @@ -171,6 +171,17 @@ + + org.springframework.boot + spring-boot-starter-webflux + 3.0.0 + + + org.springframework.boot + spring-boot-starter-logging + + + org.quartz-scheduler quartz