Skip to content

Commit

Permalink
Merge pull request #132 from areklalo/SYNCT-295
Browse files Browse the repository at this point in the history
SYNCT-295: FHIR - support of OpenMRS 1.x
  • Loading branch information
Kamil Madej committed Jan 15, 2019
2 parents 8f2bab9 + f9e7bda commit 51a662b
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 86 deletions.
@@ -1,7 +1,7 @@
package org.openmrs.module.sync2.api.model;

import org.openmrs.module.fhir.api.client.ClientHttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.RequestEntity;

import java.io.Serializable;
import java.net.URI;
Expand All @@ -18,7 +18,7 @@ public class InnerRequest implements Serializable {
public InnerRequest() {
}

public InnerRequest(RequestEntity<?> entity) {
public InnerRequest(ClientHttpEntity<?> entity) {
this.method = entity.getMethod();
this.url = entity.getUrl();
this.body = (entity.getBody() != null) ? entity.getBody().toString() : null;
Expand Down
Expand Up @@ -3,6 +3,8 @@
import org.openmrs.User;
import org.openmrs.api.AdministrationService;
import org.openmrs.api.context.Context;
import org.openmrs.module.fhir.api.client.ClientHttpEntity;
import org.openmrs.module.fhir.api.client.ClientHttpRequestInterceptor;
import org.openmrs.module.fhir.api.helper.ClientHelper;
import org.openmrs.module.sync2.api.model.RequestWrapper;
import org.openmrs.module.sync2.api.service.SyncConfigurationService;
Expand All @@ -12,7 +14,6 @@
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.HttpClientErrorException;
Expand All @@ -30,9 +31,10 @@ public class SyncRequestWrapperServiceImpl implements SyncRequestWrapperService
@Override
public ResponseEntity<String> getObject(RequestWrapper wrapper) {
RestTemplate restTemplate = prepareRestTemplate(wrapper.getClientName());
ClientHelper helper = ClientHelperFactory.createClient(wrapper.getClientName());
try {
RequestEntity<String> req = new RequestEntity<>(wrapper.getRequest().getMethod(), wrapper.getRequest().getUrl());
return copyResponseWithContentType(restTemplate.exchange(req, String.class));
ClientHttpEntity<String> req = new ClientHttpEntity<>(wrapper.getRequest().getMethod(), wrapper.getRequest().getUrl());
return copyResponseWithContentType(exchange(restTemplate, helper, req, String.class));
}
catch (HttpClientErrorException e) {
return new ResponseEntity<>(e.getMessage(), e.getStatusCode());
Expand All @@ -46,8 +48,8 @@ public ResponseEntity<String> sendObject(RequestWrapper wrapper) {
ClientHelper helper = ClientHelperFactory.createClient(wrapper.getClientName());
Object object = helper.convertToObject(wrapper.getRequest().getBody(), wrapper.getClazz());

RequestEntity req = new RequestEntity<>(object, wrapper.getRequest().getMethod(), wrapper.getRequest().getUrl());
ResponseEntity<String> res = restTemplate.exchange(req, String.class);
ClientHttpEntity req = new ClientHttpEntity<>(object, wrapper.getRequest().getMethod(), wrapper.getRequest().getUrl());
ResponseEntity<String> res = exchange(restTemplate, helper, req, String.class);

return new ResponseEntity<>(res.getBody(), res.getStatusCode());
}
Expand All @@ -62,11 +64,14 @@ public ResponseEntity<String> sendObject(RequestWrapper wrapper) {
@Override
public ResponseEntity<String> deleteObject(RequestWrapper wrapper) {
RestTemplate restTemplate = prepareRestTemplate(wrapper.getClientName());
ClientHelper helper = ClientHelperFactory.createClient(wrapper.getClientName());
HttpHeaders headers = new HttpHeaders();
setRequestHeaders(helper, headers);
try {
return restTemplate.exchange(
wrapper.getRequest().getUrl(),
wrapper.getRequest().getMethod(),
new HttpEntity<Object>(wrapper.getRequest().getBody()),
new HttpEntity<Object>(wrapper.getRequest().getBody(), headers),
String.class);
}
catch (HttpClientErrorException e) {
Expand Down Expand Up @@ -98,15 +103,27 @@ private boolean isWhitelistDisabled() {

private RestTemplate prepareRestTemplate(String client) {
RestTemplate restTemplate = new RestTemplate();
ClientHelper helper = ClientHelperFactory.createClient(client);
restTemplate.setMessageConverters(helper.getCustomMessageConverter());
return restTemplate;
}

private HttpHeaders setRequestHeaders(ClientHelper clientHelper, HttpHeaders headers) {
AdministrationService adminService = Context.getAdministrationService();
String username = adminService.getGlobalProperty(LOCAL_USERNAME_PROPERTY);
String password = adminService.getGlobalProperty(LOCAL_PASSWORD_PROPERTY);
for (ClientHttpRequestInterceptor interceptor :
clientHelper.getCustomInterceptors(username, password)) {
interceptor.addToHeaders(headers);
}
return headers;
}

ClientHelper helper = ClientHelperFactory.createClient(client);
restTemplate.setInterceptors(helper.getCustomInterceptors(username, password));
restTemplate.setMessageConverters(helper.getCustomMessageConverter());

return restTemplate;
private ResponseEntity exchange(RestTemplate restTemplate, ClientHelper helper, ClientHttpEntity request, Class clazz) {
HttpHeaders headers = new HttpHeaders();
setRequestHeaders(helper, headers);
HttpEntity entity = new HttpEntity<>(request.getBody(), headers);
return restTemplate.exchange(request.getUrl(), request.getMethod(), entity, clazz);
}

private ResponseEntity<String> copyResponseWithContentType(ResponseEntity<?> response) {
Expand Down
51 changes: 33 additions & 18 deletions api/src/main/java/org/openmrs/module/sync2/api/sync/SyncClient.java
@@ -1,5 +1,7 @@
package org.openmrs.module.sync2.api.sync;

import org.openmrs.module.fhir.api.client.ClientHttpEntity;
import org.openmrs.module.fhir.api.client.ClientHttpRequestInterceptor;
import org.openmrs.module.fhir.api.helper.ClientHelper;
import org.openmrs.module.sync2.api.exceptions.SyncException;
import org.openmrs.module.sync2.api.model.InnerRequest;
Expand All @@ -11,9 +13,10 @@
import org.openmrs.module.sync2.client.RequestWrapperConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.client.HttpClientErrorException;
Expand Down Expand Up @@ -113,8 +116,6 @@ private void setUpCredentials(String clientName, OpenMRSSyncInstance instance) {
}

private void prepareRestTemplate(ClientHelper clientHelper) {
restTemplate.setInterceptors(clientHelper.getCustomInterceptors(this.username, this.password));

List<HttpMessageConverter<?>> converters = new ArrayList<>(clientHelper.getCustomMessageConverter());
converters.add(new RequestWrapperConverter());
restTemplate.setMessageConverters(converters);
Expand All @@ -126,51 +127,65 @@ private Object retrieveObject(CategoryEnum category, String resourceUrl, String
ClientHelper helper = ClientHelperFactory.createClient(clientName);
Class<?> clazz = helper.resolveClassByCategory(category.getCategory());

RequestEntity request = helper.retrieveRequest(resourceUrl);
ClientHttpEntity request = helper.retrieveRequest(resourceUrl);
if (shouldWrappMessage(clientName, instance)) {
request = sendRequest(category, destinationUrl, clientName, new InnerRequest(request));
}
return exchange(request, clazz).getBody();
return exchange(helper, request, clazz).getBody();
}

private ResponseEntity<String> createObject(CategoryEnum category, String resourceUrl, String destinationUrl, Object object,
private ResponseEntity<String> createObject(CategoryEnum category, String resourceUrl, String destinationUrl,
Object object,
String clientName, OpenMRSSyncInstance instance) throws RestClientException, URISyntaxException {
ClientHelper helper = ClientHelperFactory.createClient(clientName);

RequestEntity request = helper.createRequest(resourceUrl, object);
ClientHttpEntity request = helper.createRequest(resourceUrl, object);
if (shouldWrappMessage(clientName, instance)) {
request = sendRequest(category, destinationUrl, clientName, new InnerRequest(request));
}
return exchange(request, String.class);
return exchange(helper, request, String.class);
}

private ResponseEntity<String> deleteObject(CategoryEnum category, String resourceUrl, String destinationUrl, String uuid,
private ResponseEntity<String> deleteObject(CategoryEnum category, String resourceUrl, String destinationUrl,
String uuid,
String clientName, OpenMRSSyncInstance instance) throws URISyntaxException {
ClientHelper helper = ClientHelperFactory.createClient(clientName);

RequestEntity request = helper.deleteRequest(resourceUrl, uuid);
ClientHttpEntity request = helper.deleteRequest(resourceUrl, uuid);
if (shouldWrappMessage(clientName, instance)) {
request = sendRequest(category, destinationUrl, clientName, new InnerRequest(request));
}
return exchange(request, String.class);
return exchange(helper, request, String.class);
}

private ResponseEntity<String> updateObject(CategoryEnum category, String resourceUrl, String destinationUrl, Object object,
private ResponseEntity<String> updateObject(CategoryEnum category, String resourceUrl, String destinationUrl,
Object object,
String clientName, OpenMRSSyncInstance instance) throws URISyntaxException {
ClientHelper helper = ClientHelperFactory.createClient(clientName);

RequestEntity request = helper.updateRequest(resourceUrl, object);
ClientHttpEntity request = helper.updateRequest(resourceUrl, object);
if (shouldWrappMessage(clientName, instance)) {
request = sendRequest(category, destinationUrl, clientName, new InnerRequest(request));
}
return exchange(request, String.class);
return exchange(helper, request, String.class);
}

private HttpHeaders setRequestHeaders(ClientHelper clientHelper, HttpHeaders headers) {
for (ClientHttpRequestInterceptor interceptor :
clientHelper.getCustomInterceptors(this.username, this.password)) {
interceptor.addToHeaders(headers);
}
return headers;
}

private ResponseEntity exchange(RequestEntity request, Class clazz) {
return restTemplate.exchange(request, clazz);
private ResponseEntity exchange(ClientHelper helper, ClientHttpEntity request, Class clazz) {
HttpHeaders headers = new HttpHeaders();
setRequestHeaders(helper, headers);
HttpEntity entity = new HttpEntity(request.getBody(), headers);
return restTemplate.exchange(request.getUrl(), request.getMethod(), entity, clazz);
}

private RequestEntity<RequestWrapper> sendRequest(CategoryEnum category, String destinationUrl, String clientName,
private ClientHttpEntity<RequestWrapper> sendRequest(CategoryEnum category, String destinationUrl, String clientName,
InnerRequest request) throws URISyntaxException {
ClientHelper clientHelper = ClientHelperFactory.createClient(clientName);
Class<?> clazz = clientHelper.resolveClassByCategory(category.getCategory());
Expand All @@ -182,7 +197,7 @@ private RequestEntity<RequestWrapper> sendRequest(CategoryEnum category, String
wrapper.setClientName(clientName);
wrapper.setRequest(request);

return new RequestEntity<>(wrapper, HttpMethod.POST, new URI(destinationUrl));
return new ClientHttpEntity<>(wrapper, HttpMethod.POST, new URI(destinationUrl));
}

private boolean shouldWrappMessage(String clientName, OpenMRSSyncInstance instance) {
Expand Down

This file was deleted.

Expand Up @@ -3,7 +3,9 @@
import org.openmrs.Patient;
import org.openmrs.api.context.Context;
import org.openmrs.module.fhir.api.client.BasicAuthInterceptor;
import org.openmrs.module.fhir.api.client.HeaderClientHttpRequestInterceptor;
import org.openmrs.module.fhir.api.client.BasicHttpRequestInterceptor;
import org.openmrs.module.fhir.api.client.ClientHttpEntity;
import org.openmrs.module.fhir.api.client.ClientHttpRequestInterceptor;
import org.openmrs.module.fhir.api.helper.ClientHelper;
import org.openmrs.module.sync2.api.model.audit.AuditMessage;
import org.openmrs.module.sync2.api.model.enums.CategoryEnum;
Expand All @@ -17,8 +19,6 @@
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.RequestEntity;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;

Expand All @@ -40,35 +40,35 @@ public class RESTClientHelper implements ClientHelper {
private final SimpleObjectMessageConverter simpleConverter = new SimpleObjectMessageConverter();

@Override
public RequestEntity retrieveRequest(String url) throws URISyntaxException {
return new RequestEntity(HttpMethod.GET, new URI(url));
public ClientHttpEntity retrieveRequest(String url) throws URISyntaxException {
return new ClientHttpEntity(HttpMethod.GET, new URI(url));
}

@Override
public RequestEntity createRequest(String url, Object object) throws URISyntaxException {
public ClientHttpEntity createRequest(String url, Object object) throws URISyntaxException {
if (object instanceof SimpleObject) {
getRestResourceConverter().convertObject(url, object);
}

return new RequestEntity(convertToFormattedData(object), HttpMethod.POST, new URI(url));
return new ClientHttpEntity<String>(convertToFormattedData(object), HttpMethod.POST, new URI(url));
}

@Override
public RequestEntity deleteRequest(String url, String uuid) throws URISyntaxException {
public ClientHttpEntity deleteRequest(String url, String uuid) throws URISyntaxException {
url += "/" + uuid;
return new RequestEntity(uuid, HttpMethod.DELETE, new URI(url));
return new ClientHttpEntity<String>(uuid, HttpMethod.DELETE, new URI(url));
}

@Override
public RequestEntity updateRequest(String url, Object object) throws URISyntaxException {
public ClientHttpEntity updateRequest(String url, Object object) throws URISyntaxException {
if (object instanceof AuditMessage) {
url += "/" + ((AuditMessage) object).getUuid();
return new RequestEntity(convertToFormattedData(object), HttpMethod.POST, new URI(url));
return new ClientHttpEntity<String>(convertToFormattedData(object), HttpMethod.POST, new URI(url));
} else {
getRestResourceConverter().convertObject(url, object);
url += "/" + ((SimpleObject) object).get("uuid");
}
return new RequestEntity(convertToFormattedData(object), HttpMethod.POST, new URI(url));
return new ClientHttpEntity<String>(convertToFormattedData(object), HttpMethod.POST, new URI(url));
}

@Override
Expand All @@ -82,7 +82,7 @@ public Class resolveClassByCategory(String category) {
@Override
public List<ClientHttpRequestInterceptor> getCustomInterceptors(String username, String password) {
return Arrays.asList(new BasicAuthInterceptor(username, password),
new HeaderClientHttpRequestInterceptor(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE));
new BasicHttpRequestInterceptor(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE));
}

@Override
Expand Down Expand Up @@ -142,7 +142,7 @@ public Object convertToOpenMrsObject(Object o, String category) throws NotSuppor
// will be implemented.
// Please check also PatientResource1_9 or any other @Resource class which supports current platform version.
if (cat.getClazz().equals(Patient.class) && o instanceof SimpleObject) {
PatientResource1_9 resource = (PatientResource1_9 ) Context.getService(RestService.class)
PatientResource1_9 resource = (PatientResource1_9) Context.getService(RestService.class)
.getResourceBySupportedClass(Patient.class);
return resource.getPatient((SimpleObject) o);
} else {
Expand Down
Expand Up @@ -4,9 +4,9 @@
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import org.openmrs.Patient;
import org.openmrs.api.context.Context;
import org.openmrs.module.fhir.api.client.ClientHttpEntity;
import org.openmrs.module.sync2.api.model.audit.AuditMessage;
import org.openmrs.module.sync2.api.utils.ContextUtils;
import org.openmrs.module.sync2.client.SimpleObjectMessageConverter;
Expand All @@ -17,7 +17,7 @@
import org.powermock.modules.junit4.PowerMockRunner;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.http.HttpMethod;
import org.springframework.http.RequestEntity;

import java.io.IOException;
import java.net.URI;

Expand Down Expand Up @@ -48,7 +48,7 @@ public void setUp() throws Exception {

@Test
public void retrieveRequest() throws Exception {
RequestEntity expected = new RequestEntity(HttpMethod.GET, URI.create(TEST_URI));
ClientHttpEntity expected = new ClientHttpEntity(HttpMethod.GET, URI.create(TEST_URI));

RESTClientHelper restClientHelper = new RESTClientHelper();
assertEquals(expected, restClientHelper.retrieveRequest(TEST_URI));
Expand All @@ -59,14 +59,14 @@ public void createRequest() throws Exception {
Patient patient = new Patient();
SimpleObject simpleObject = getSimpleObject(patient);
String json = (new SimpleObjectMessageConverter()).convertToJson(simpleObject);
RequestEntity expected = new RequestEntity(json, HttpMethod.POST, URI.create(TEST_URI));
ClientHttpEntity expected = new ClientHttpEntity<String>(json, HttpMethod.POST, URI.create(TEST_URI));

assertEquals(expected, restClientHelper.createRequest(TEST_URI, simpleObject));
}

@Test
public void deleteRequest() throws Exception {
RequestEntity expected = new RequestEntity(TEST_PATIENT_UUID, HttpMethod.DELETE,
ClientHttpEntity expected = new ClientHttpEntity<String>(TEST_PATIENT_UUID, HttpMethod.DELETE,
URI.create(TEST_URI + "/" + TEST_PATIENT_UUID));
RESTClientHelper restClientHelper = new RESTClientHelper();
assertEquals(expected, restClientHelper.deleteRequest(TEST_URI, TEST_PATIENT_UUID));
Expand All @@ -78,7 +78,7 @@ public void updateRequest() throws Exception {
patient.setUuid(TEST_PATIENT_UUID);
SimpleObject simpleObject = getSimpleObject(patient);
String json = (new SimpleObjectMessageConverter()).convertToJson(simpleObject);
RequestEntity expected = new RequestEntity(json, HttpMethod.POST,
ClientHttpEntity expected = new ClientHttpEntity<String>(json, HttpMethod.POST,
URI.create(TEST_URI + "/" + TEST_PATIENT_UUID));

assertEquals(expected, restClientHelper.updateRequest(TEST_URI, simpleObject));
Expand Down

0 comments on commit 51a662b

Please sign in to comment.