Total Pageviews

Thursday, August 15, 2013

A Note of Liferay Portlet URLs

In a portlet, we get our code executed by two ways - at the time of rendering of the portlet and another on some action which is generally a form submit or some event. For getting the same done, we have three different URLs in Liferay:
  1. Render URL (which is portlet URL and responsible for rendering of the portlet and in typical MVC portlet, the code of method doView() is executed)
  2. Action URL (which is also portlet URL and responsible for performing some action with page reload and in typical MVC portlet, the code method processAction() is executed, and is followed by renderRequest)
  3. Resource URL (which is NOT a portlet URL but it extends the same base URL, is responsible for performing some action without page reload i.e. Ajax and in typical MVC portlet, the code of method serveResource() is executed and is NOT followed bt render request, Resource Serving was not available in JSR168 but its avaible in JSR286.
We can create these URL mainly by four approaches:
  1. Using TagLib
  2. Using Java
  3. Using Javascript (AUI)
  4. Using Velocity

Now we'll see how we can create these three type of URLs with the above four approaches considering that you have done all necessary imports:

1. Using Taglib

Render URL:
here we are setting the window state of the portlet to maximum and a custom parameter(message).
Action URL:
Resource URL:

2. Using Java

Render URL:
Using RenderResponse:
Using PortletUrlFactory:
Action URL:
Using RenderResponse:

Using PortletUrlFactory:
Resource URL:
Using RenderResponse:
Using PortletUrlFactory:

3. Using Javascript (AUI)
Render URL:

Action URL:

Resource URL:

The following can be set on these portlet URL methods:
  • setCopyCurrentRenderParameters: function(copyCurrentRenderParameters);
  • setDoAsUserId: function(doAsUserId);
  • setEncrypt: function(encrypt);
  • setEscapeXML: function(escapeXML);
  • setLifecycle: function(lifecycle);
  • setName: function(name);
  • setParameter: function(key, value);
  • setPlid: function(plid);
  • setPortletConfiguration: function(portletConfiguration);
  • setPortletId: function(portletId);
  • setPortletMode: function(portletMode);
  • setResourceId: function(resourceId);
  • setSecure: function(secure);
  • setWindowState: function(windowState);
  • toString: function();

4. Using velocity: Here we have to replace correct portletId and plid for getting the URL generated correctly.
Render URL:
Action URL:
Resource URL:
Hope you enjoyed the read.

Monday, July 22, 2013

A Note of Liferay Portal Instances

Liferay provides us facility to run more than one portal instance on a single server. Liferay comes with one inbuilt portal instance, which is names 'Liferay' and its web id is 'liferay.com', however it can be modified with following properties in portal-ext.properties file:
Note: There are several other properties regarding portal instances present in portal.properties as well which can be referred in Liferay Portal SRC.

If we login as administrator (generally test@liferay.com), and go to control panel we can see list of all available portal instances in 'Portal Instances' section inside 'Server' category. Here we will see a table with atlest one entry (inbuilt one) , lets know what the column of this table depicts:

  • Instance ID: It’s a numeric Id, generated by code automatically at the time of Instance creation. Internally this Instance ID is referred as 'companyid' in the database table. We can see all available instances in database table 'company'.
  • Web ID: It’s a user-generated ID for the instance. It can be anything relevant but a general convention is to use the domain name.
  • Virtual Host: Domain name which is to be configured in the network kept here. This domain name is referred when users are directed to our Liferay server, and the same enables Liferay to resolve request for proper portal instance.
  • Mail Domain: The domain name to be used as mail host for this instance kept here. Liferay will use this to send email notifications from the portal, and the same is used for creating the default test user, as test@mail-domain.com
  • # of Users: Depicts current number of users on particular portal instance.
  • Max # of Users: Depicts total number of users allowed on particular portal instance.
  • Active: Depicts if the portal instance active or not (Yes / No).

Enough of theory, lets experience it. Startup the server and replicate the following steps:
  1. Login as administrator. (test@liferay.com/test will work)
  2. Navigate to Manage → Control Panel → Portal Instances section in 'Server' category.
  3. By default only one portal instance will be listed. To add new, click on add button fill up the form fields. Say you have created 'newinstance.com'.
  4. Assuming that you are working on your local system, you have to edit the host file (%SYSTEMROOT%\ System32\ drivers\ etc\ hosts). Add the following in the file:
    If you are in network then you can map the entry with the IP address of the server on which Liferay is running and try.
  5. Start the browser (restart in case already running) and put 'newinstance.com:<PORT>' in address bar. What you see now in the new portal instance.
  6. You can login as test@<domainname>, (test@newinstance.com in out case). This is the admin user of the instance. As an administrator you cannot see the 'Server' category in Control Panel, which means new instance can be created only from main portal instance.
If we dig in further, we will come to know that we can have different properties file for each portal instances. To enable this feature, set the "company-id-properties" system property to true. The read order will now be: portal.properties, then portal-ext.properties, and then portal-liferay.com.properties. Note that not all properties can have different values per company. This functionality is only available for legacy reasons. The preferred way to configure a portal instance is through the Control Panel.

Handling Portal Instances in Custom Code:
In real life scenario, we may need to process data differently for each Portal Instance in our custom code. So have a look on small code snippet:
Hope you enjoyed the reading.

Sunday, June 23, 2013

Customizing Database Column Size


In most on the real life implementations, we need to use different datatype and and their sizes in database tables. As in Liferay, tables are created at the time of portlet deployment with the help of SQL generated by building of service.xml file, so we need to put all the table attribute information in configuration so that it can create table with the customized settings. The size of String is 75 by default. For customizing the same, we have to modify the portlet-model-hints.xml file. This file is available in META-INF directory inside src. To customizing the column size, we have to do the following:

Lest see the sample service.xml file:

Now we are going to modify the 'schoolName' and 'schoolDescription' attributes:

Now, after the deployment and creation of the table when you'll see table description, the 'schoolName' attribute will be of String type where as the 'schoolDescription' will be of varchar. Great! Indeed, but How the miracle happened? well, its not the magic, if length given in the model hints is greater than 4000 then the column type is converted in Varchar or Text automatically by Liferay but if length less than or equal to 4000 then column type will be String.

Saturday, June 8, 2013

Enabling SSL in Liferay6.1.1-Jboss7.1.1 Bundle

My last post was about SSL in Liferay-Tomcat bundle. In the array, my this post about enabling SSL in Liferay-Jboss 7 bundle, in standalone mode. (We'll discuss about standalone and domain mode of Jboss in the next post)

Similarly there are two parts, first creating of certificates and another is configuration. For SSL certificate creation you can see my previous post. Now lets talk about settings. Go to the \liferay-portal-6.1.1-ce-ga2\jboss-7.1.1\standalone\configuration\ and open the standalone.xml file and follow the following steps:

  1. Search for "urn:jboss:domain:web:1.1" and replace the <subsystem> ... </subsystem> contents with following:
    Again the password and certificate-key-file value will depend on your inputs at the time of certificate creation and default home directory.
  2. Scroll further down and look for <interfaces> ... </interfaces> entry and replace whole with the following:
  3. Now you the next block you will see will be of <socket-binding-group>, again replace the <socket-binding-group> ... </socket-binding-group> with the following:
That's it. Now start the server and use https://localhost:8181:/web/guest/ and enjoy! :)

You can download sample standalone.xml from here.


Saturday, May 25, 2013

Enabling SSL in Liferay-Tomcat Bundle

For enabling the SSL (https) in Liferay-Tomcat bundle, we need to modify the server.xml file inside \liferay-portal-6.1.1-ce-ga2\tomcat-7.0.27\conf\ directory. By default the following entry is used:
There are basically two steps for enabling SSL. First - we need to create a self signed certificate (if you don't have one from publisher) and secondly setting up tomcat configurations. You can see approach for certificate creation in my previous post, only STEP 1 is sufficient for this purpose.

Now for enabling SSL, just put the following in server.xml
where the keystore password is the password used at the time of certificate creation.

Now start the server. You will be able to use https://localhost:8181/web/guest/ after complete startup. You can manage ports as per requirements.

You can download the sample server.xml file from here.

Sunday, May 12, 2013

Creating Self Signed Certificate using Keytool

Prerequsites: For using the keytool utility, we have to ensure that our environment is configured to use the bin directory of JDK, otherwise the full path to the utility must be present on the command line. Which can be ensured by typing java or javac on command prompt.

There are basically three steps:
1. Generate the certificate in the keystore file


Here tomcat is an unique alias of certificate. change is the default password, you can change it. You now have a .keystore in the current user's home directory

2. Now export the certificate you just generated:

Now the certificate has been exported to server.crt file.

3. Now, add the exported certificate (server.crt) to your JRE's cacerts file

 Yes, now its done.

Sunday, April 28, 2013

Creating Roles in Liferay with Portal-ext.properties

Need of user role is a very common in live scenario. One way is quite easy, just sign in as administrator, go to control panel and add one. But there is a drawback of this approach. If you are working on staging environment, you have to create this role in production environment when the code is deployed there. It means this is database specific and it will not move to production when the build is deployed on the server.

Liferay has provided us a better solution for the same. We can create role by using certain properties in portal-ext.properties file. We have to add the following properties in our portal-ext properties file.
Just restart the box after adding them, and go to the Roles section in Control Panel, you will be able to see both the roles added there.

Now if we want to access these roles in our code programmatically because this is basic need of creating role. So it can be done as following:
For getting users we can use the following methods:
Some other properties of portal-ext.properties that you can work on are:
Hope this will be helpful.

Wednesday, April 17, 2013

Liferay: Uploading User Portrait via Custom Portlet

The following code can be integreate with any MVC portlet extending MVCPortlet in controller class.

view.jsp
Controller class

Tuesday, April 2, 2013

Solution of Duplicate Submission issue of Form on Page Reload

Re submission of form on press of refresh button is an old issue is Liferay. The reason behind this issue is the parameters and action which are available in URL after the form submission.  Earlier some developers used to open some other JSP after completion of Action.

But its solution is a very easy, provided by liferay itself. We have to set the following value in a tag inside liferay-portlet.xml which is set  false by default.