From 8407846598fec9d9e3787d75fb4f1591795ed85e Mon Sep 17 00:00:00 2001 From: Ian Date: Wed, 23 Oct 2024 13:32:24 -0400 Subject: [PATCH] Refactoring NotificationsPullPointClientImpl --- .../NotificationsPullPointClientImpl.java | 77 +++++++++---------- 1 file changed, 37 insertions(+), 40 deletions(-) diff --git a/api/src/main/java/org/openmrs/module/xdssender/api/notificationspullpoint/impl/NotificationsPullPointClientImpl.java b/api/src/main/java/org/openmrs/module/xdssender/api/notificationspullpoint/impl/NotificationsPullPointClientImpl.java index eda9f6a..4bfbb67 100644 --- a/api/src/main/java/org/openmrs/module/xdssender/api/notificationspullpoint/impl/NotificationsPullPointClientImpl.java +++ b/api/src/main/java/org/openmrs/module/xdssender/api/notificationspullpoint/impl/NotificationsPullPointClientImpl.java @@ -3,7 +3,7 @@ import java.io.IOException; import java.math.BigInteger; import java.net.HttpURLConnection; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; @@ -13,7 +13,6 @@ import javax.xml.transform.TransformerException; import org.apache.commons.codec.binary.Base64; -import org.apache.commons.io.IOUtils; import org.openmrs.Location; import org.openmrs.LocationAttribute; import org.openmrs.LocationTag; @@ -57,7 +56,7 @@ public class NotificationsPullPointClientImpl extends WebServiceGatewaySupport i private static final Logger log = LoggerFactory.getLogger(NotificationsPullPointClientImpl.class); public static final String FACILITY_QNAME = "facility"; - private BigInteger MAX_MESSAGES_PER_REQUEST = BigInteger.valueOf(100); + private static final BigInteger MAX_MESSAGES_PER_REQUEST = BigInteger.valueOf(100); @Autowired private XdsSenderConfig config; @@ -66,7 +65,7 @@ public class NotificationsPullPointClientImpl extends WebServiceGatewaySupport i public List getNewMessages() { LocationTag loginLocationTag = Context.getLocationService().getLocationTagByName(LOCATION_TAG_NAME); List locations = Context.getLocationService().getLocationsByTag(loginLocationTag); - List returnMessages = new ArrayList(); + List returnMessages = new ArrayList<>(); for (Location location : locations) { returnMessages.addAll(this.getNewMessages(location)); } @@ -93,30 +92,30 @@ public List getNewMessages(Location currentLocation) { List result = new ArrayList<>(); GetMessagesResponse response; try { - // response = (GetMessagesResponse) getResponse(request); - response = (GetMessagesResponse) getResponseHttpClient(request); + response = getResponseHttpClient(request); HL7Service hl7Service = Context.getHL7Service(); - for (NotificationMessageHolderType notification : response.getNotificationMessage()) { - Element el = (Element) notification.getMessage().getAny(); - String decodedMessage = new String(Base64.decodeBase64(el.getTextContent().getBytes())); - // Replace new line character with it's ASCII equivalent - // Remove the time component from the birthdate to fix a HL7 parsing error - String parsedMessage = OruR01Util - .changeMessageVersionFrom251To25(decodedMessage.replace("\n", Character.toString((char) 13)) - .replaceAll("\\[[0-9]{4}\\]", "")); - - log.debug(parsedMessage); - Message message = hl7Service.parseHL7String(parsedMessage); - - result.add(message); + if (response != null) { + for (NotificationMessageHolderType notification : response.getNotificationMessage()) { + Element el = (Element) notification.getMessage().getAny(); + String decodedMessage = new String(Base64.decodeBase64(el.getTextContent().getBytes())); + // Replace new line character with it's ASCII equivalent + // Remove the time component from the birthdate to fix a HL7 parsing error + String parsedMessage = OruR01Util + .changeMessageVersionFrom251To25(decodedMessage.replace("\n", Character.toString((char) 13)) + .replaceAll("\\[[0-9]{4}\\]", "")); + + log.debug(parsedMessage); + Message message = hl7Service.parseHL7String(parsedMessage); + + result.add(message); + } } } catch (Exception e) { - log.debug("Error getting response in NotificationsPullPointClientImpl: ", e); - e.printStackTrace(); - } finally { - return result; + log.error("Error getting response in NotificationsPullPointClientImpl: ", e); } + + return result; } private Object getResponse(Object requestPayload) throws Exception { @@ -140,7 +139,7 @@ public void doWithMessage(WebServiceMessage message) throws IOException, Transfo addAuthorizationHeader); } - private Object getResponseHttpClient(GetMessages requestPayload) throws Exception { + private GetMessagesResponse getResponseHttpClient(GetMessages requestPayload) throws Exception { Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); marshaller.setContextPath("org.openmrs.module.xdssender.notificationspullpoint"); marshaller.afterPropertiesSet(); @@ -150,8 +149,7 @@ private Object getResponseHttpClient(GetMessages requestPayload) throws Exceptio OkHttpClient client = new OkHttpClient().newBuilder().build(); MediaType mediaType = MediaType.parse("text/xml; charset=utf-8"); String facilitySiteCode = requestPayload.getOtherAttributes().get(new QName(FACILITY_QNAME)); - String getMessagesPayload = String.format("" - + "\r\n" + " \r\n" + " \r\n" @@ -168,25 +166,24 @@ private Object getResponseHttpClient(GetMessages requestPayload) throws Exceptio + "", facilitySiteCode); log.debug(getMessagesPayload); - RequestBody body = RequestBody.create( - getMessagesPayload, - mediaType); + + RequestBody body = RequestBody.create(getMessagesPayload, mediaType); Request request = new Request.Builder().url(config.getNotificationsPullPointEndpoint()).method("POST", body) .addHeader("Content-Type", "text/xml; charset=utf-8") - .addHeader("Accept", "text/xml, text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2") + .addHeader("Accept", "text/xml, text/html") .addHeader("Authorization", generateBasicAuthenticationHeader(config.getNotificationsPullPointUsername(), config.getNotificationsPullPointPassword())) .build(); - Response response = client.newCall(request).execute(); - - JAXBContext jaxbContext = JAXBContext.newInstance("org.openmrs.module.xdssender.notificationspullpoint"); - Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); - String responseText = response.body().string(); - log.debug(responseText); - Object res = unmarshaller.unmarshal(IOUtils.toInputStream(responseText)); - return res; + try (Response response = client.newCall(request).execute()) { + JAXBContext jaxbContext = JAXBContext.newInstance("org.openmrs.module.xdssender.notificationspullpoint"); + Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); + if (response.body() != null) { + return (GetMessagesResponse) unmarshaller.unmarshal(response.body().byteStream()); + } + return null; + } } private void addAuthorizationHeader() { @@ -211,8 +208,8 @@ private void addAuthorizationHeader(Object requestPayload) { } private static String generateBasicAuthenticationHeader(String userName, String userPassword) { - byte[] bytesEncoded = Base64.encodeBase64((userName + ":" + userPassword).getBytes(Charset.forName("UTF-8"))); - return "Basic " + new String(bytesEncoded, Charset.forName("UTF-8")); + byte[] bytesEncoded = Base64.encodeBase64((userName + ":" + userPassword).getBytes(StandardCharsets.UTF_8)); + return "Basic " + new String(bytesEncoded, StandardCharsets.UTF_8); } }