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..2a6ee2d8 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: 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..77a90477 100644 --- a/openerp-atomfeed-service/src/main/resources/jobs-context.xml +++ b/openerp-atomfeed-service/src/main/resources/jobs-context.xml @@ -250,7 +250,8 @@ - + + 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..3d117476 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 @@ -10,24 +10,17 @@ import java.util.UUID; 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 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..4ea90613 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(); @@ -84,9 +81,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) { 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..0eb3f030 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"); } @@ -65,7 +64,7 @@ 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(URI).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)); } 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; }