Skip to Content
Technical Articles

EIPinCPI – Messaging Gateway

Previous – Message Endpoint | Index | Next – Messaging Mapper

This week, we’ll study a pattern known as Messaging Gateway.

When do I use this pattern?

Messaging Gateway refers to the code that separates application code from the code required for invoking the messaging server. In other words, Messaging Gateway wraps the messaging-specific code and exposes the services as domain-specific methods. For example, a service to convert temperature requires multiple steps before the actual converted temperature is available. The steps include authorising the request, invoking the service, and parsing the response. A messaging gateway would hide these steps and only provide the converted temperature value.

Messaging Gateway in CPI

As we discussed above, the Messaging Gateway is coded in the systems that use middleware or messaging systems. Therefore, all we will do in CPI is expose the W3Schools’ Temperature Conversion service as an HTTPS service.

Integration Flow

Temperature%20Conversion

Temperature Conversion

The flow uses SOAP Receiver Adapter to invoke the W3Schools’ Temperature Conversion service and exposes it using HTTPS Sender Adapter.

The table below shows changes to the default configuration of HTTPS Sender Adapter:

Tab Property Value
Connection Address /CelciusToFahrenheit
Connection CSRF Protected Unchecked

The table below shows the changes to the default configuration of SOAP Receiver Adapter:

Tab Property Value
Connection Address https://www.w3schools.com/xml/tempconvert.asmx
Connection URL to WSDL /wsdl/tempconvert.wsdl

Messaging Gateway in Java

Now, let’s see an example end system code that consumes the integration flow above.

There are 2 classes in this example:

  • TemperatureGateway class acts as Messaging Gateway doing all the heavy lifting of authorising the request, invoking the service, and parsing the response. It exposes the celciusToFahrenheit method that accepts the temperature in degree Celcius (°C) and returns the temperature in degree Fahrenheit (°F).
  • Application class contains application-specific code. It accepts temperature in degree Celcius through Command-Line arguments and displays the temperature in degree Celcius (the input) and degree Fahrenheit.

TemperatureGateway Class

This is our messaging gateway. And it has 3 steps:

Authorise the Request

The authorisation is set as part of creating the HttpClient.

HttpClient client = HttpClient.newBuilder().authenticator(new Authenticator() {
	@Override
	protected PasswordAuthentication getPasswordAuthentication() {
		return new PasswordAuthentication("<username>", "<password>".toCharArray());
	}
}).build();

Invoke the Service

Invoking the service has 2 substeps of creating the request and then using the client to invoke the service.

HttpRequest request = HttpRequest
		.newBuilder(URI.create("https://<cpi host>/http/CelciusToFahrenheit"))
		.POST(BodyPublishers
				.ofString("<ns:CelsiusToFahrenheit xmlns:ns=\"https://www.w3schools.com/xml/\"><ns:Celsius>"
						+ fahrenheit + "</ns:Celsius></ns:CelsiusToFahrenheit>"))
		.build();

HttpResponse<String> response;
try {
	response = client.send(request, BodyHandlers.ofString());
} catch (IOException | InterruptedException e) {
	e.printStackTrace();
	throw new Exception("Could not convert temperature", e);
}

Parse the Response

Parsing the response requires evaluating the XPath to find out the temperature in degree Fahrenheit.

XPath xPath = XPathFactory.newInstance().newXPath();
xPath.setNamespaceContext(new NamespaceContext() {

	@Override
	public Iterator<String> getPrefixes(String namespaceURI) {
		return null;
	}

	@Override
	public String getPrefix(String namespaceURI) {
		return null;
	}

	@Override
	public String getNamespaceURI(String prefix) {
		switch (prefix) {
		case "urn":
			return "https://www.w3schools.com/xml/";
		}
		return null;
	}
});

try {
	fahrenheit = (double) xPath.evaluate("/urn:CelsiusToFahrenheitResponse/urn:CelsiusToFahrenheitResult",
			new InputSource(new StringReader(response.body())), XPathConstants.NUMBER);
} catch (XPathExpressionException e) {
	e.printStackTrace();
	throw new Exception("Could not convert temperature", e);
}

Application Class

The Application class is a simple class containing the code to display the temperatures and display the error in case of exception.

try {
	System.out.println("Celcius:\t" + args[0]);
	System.out.println("Fahrenheit:\t" + TemperatureGateway.celciusToFahrenheit(Double.parseDouble(args[0])));
} catch (Exception e) {
	e.printStackTrace();
	System.out.println(e.getMessage());
}

Note that the Temperature class hid all the exceptions related to the service or parsing, and exposed a simple message that it “Could not convert temperature”.

Output

When the user passes the temperature to the Application class, they get back the temperatures in degree Celcius and degree Fahrenheit like so:

Celcius:	25.0
Fahrenheit:	77.0

Conclusion

Messaging Gateway refers to the code that hides the complexity of messaging from application code and provides a domain-specific API to the application. As CPI is an IPaaS, CPI cannot implement the Messaging Gateway. Alternatively, we explored the way of implementing the Messaging Gateway in Java.

References/Further Readings

Hope this helps,
Bala

Previous – Message Endpoint | Index | Next – Messaging Mapper

/
Temperature%20Conversion
Be the first to leave a comment
You must be Logged on to comment or reply to a post.