Liferay Integration With MuleESB

blog-banner

What is an ESB and MuleESB?

An Enterprise Service Bus (ESB) is a set of rules and principles for integrating numerous applications together over a bus-like infrastructure. The central concept is that the ESB provides the middleware and interfaces that allow businesses to connect their applications without writing code.

Mule ESB is a lightweight Java-based ESB and integration platform that allows developers to connect applications together quickly and easily, enabling them to exchange data. It’s scalable messaging framework, capability to operate over complex topologies, flexible configuration to run on distributed environment and wide range of connector led us to choose it over other ESB framework.

MuleESB with Liferay

Create Liferay connector for Mule ESB if you want to integrate entire liferay services. That can easily be developed using Liferay JSON API instead creating hook to hit Mule ESB HTTP endpoint. Read more about How to develop connector.

There is another approach mentioned in MuleESB integration Liferay forum thread to integrate Liferay with MuleESB using LMB. I haven’t tried it yet but you can give it a try.

CMIS cloud connector can be used if you wants to synchronize Liferay document library. CMIS connector defines an abstraction layer for managing multiple document management systems and repositories using web protocols. read more about CMIS Connector.

In this article we are going to build simple example of Mule ESB integration with Liferay which post a tweet when a blog is published on Liferay.

Following task need to be performed in order to accomplish this example.

  1. Create hook in liferay which hits Mule ESB HTTP endpoint.

  2. Create Mule App flow to handle request from Liferay.

We are going to use below version of tools for this example

Twitter Connector 3.2.0

Mule ESB Server 3.5.0 CE

Let’s start with creating hook in liferay

We are going to create service wrapper hook to override Liferay’s blog publishing method. HTTPclient from hook will hit Mule HTTP endpoint with username of user who published blog, blog name and blog URL.

Here is a code

	@Override
	public BlogsEntry addEntry(long userId, String title, String description,
				String content, int displayDateMonth, int displayDateDay,
				int displayDateYear, int displayDateHour, int displayDateMinute,
				boolean allowPingbacks, boolean allowTrackbacks,
				String[] trackbacks, boolean smallImage, String smallImageURL,
				String smallImageFileName, InputStream smallImageInputStream,
				ServiceContext serviceContext) 
				throws PortalException,	SystemException {
	
			BlogsEntry blogEntry = super.addEntry(userId, title, description, content, displayDateMonth, displayDateDay, displayDateYear, displayDateHour, displayDateMinute, allowPingbacks, allowTrackbacks, trackbacks, smallImage, smallImageURL, smallImageFileName, smallImageInputStream, serviceContext);
			
			if(blogEntry.isApproved()) {
				
				HttpClientBuilder builder = HttpClientBuilder.create();
				HttpClient client = builder.build();
				HttpPost post = new HttpPost(MULE_ENDPOINT);
				
				JSONObject blogDetail= JSONFactoryUtil.createJSONObject();
				blogDetail.put("name", blogEntry.getUserName());
				blogDetail.put("blogTitle", blogEntry.getTitle());
				blogDetail.put("blogUrl", blogEntry.getUrlTitle());
				
				try {
					StringEntity se= new StringEntity(blogDetail.toString());
					post.setEntity(se);
					post.setHeader("Accept", "application/json");
					post.setHeader("Content-type", "application/json");
				
					HttpResponse response = client.execute(post);
					System.out.println("Response Code : " 
							+ response.getStatusLine().getStatusCode());
					System.out.println("Response Content : "+new BufferedReader(new InputStreamReader(response.getEntity().getContent())).readLine());
				} catch (Exception e) {
					e.printStackTrace();
				}	 
			}
			return blogEntry;
		}
	

You can download war file for hook from attachment.

Second step is to create mule application to handle HTTP request from Liferay and post it to Twitter. Twitter app is necessary in order to communicate with the Twitter account from Mule ESB application. Follow steps described in this article to create Twitter application.

We will need following components and connectors  HTTP endpoint, JSON to Object, Set Payload and Twitter Connector. Watch Video demo on Twitter Connector and read tutorial on Twitter Integration for more detail. Now configure flow as given in the below image to handle request and post tweet to the twitter. You can download mule application from attachment.

blog notification flow
	<twitter:config name="Twitter" accessKey="***************" accessSecret="**************" consumerKey="*********************" consumerSecret="****************" doc:name="Twitter"/>
		<flow name="blogNotificationFlow">
			<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8082" path="senduserdetail" doc:name="HTTP"/>
			<json:json-to-object-transformer returnClass="java.lang.Object" doc:name="JSON to Object"/>
			<set-payload value="#[message.payload.name] has published blog about #[message.payload.blogTitle] on http://surekhatech.com . Check it out. " doc:name="Set Payload"/>
			<twitter:update-status config-ref="Twitter" status="#[payload]" doc:name="Twitter"/>
			<logger message="#[payload]" level="INFO" doc:name="Logger"/>
		</flow>
	</mule>
	

Testing and Observation

  • Deploy the Liferay hook

  • Deploy the Mule application

  • Publish blog into Liferay.

  • If credentials are correct then mule will post tweet to the Twitter with blog name, blog url and user who posted blog. It will look like following image.

Liferay integration with MuleESB
Contact us

For Your Business Requirements

Text to Identify Refresh CAPTCHA
Background Image Close Button

2 - 4 October 2024

Hall: 10, Booth: #B8 Brussels, Belgium