Thursday, January 31, 2019

Disabling and Enabling Minfying of JS and CSS resources in Liferay

 

 Disabling minification of JS and CSS


The minifiation and comibing all the JS and CSS resources is done at two levels or at two places.

1. First are the JS and CSS files which you have included in the portlet JSPs i.e by header portlet css or header portlet js flies or the script tags which you have included in the JSP files
2. Second JS and CSS files are the ones which Liferay uses like, jquery, lodash, alloy ui and all these resources also gets minfied and bundled into one JS or CSS file and loaded in a single HTTP call.

Now there are two scenarios one is if you want to debug the Javascript code you have included in the Portlet View JSP, and the other one is when you load a page and you wan to see all the individual JS and CSS files getting loaded instead of a single mindifed bundle.

Disabling minification of JS and CSS in portlet view JSPs


When you include any JS files or CSS files inside the portlet jsps, by default they get minified when the JSP is rendered in the browser.

It will be hard to debug the JS issues when the minifier is enabled. If you want to disable the minification of JS and CSS resouces which are included in the Portlet Views you can set the below attribute in the portal-ext.properties. Restart the server to take effect.

minifier.enabled=fasle 


Disabling minification of JS and CSS during page load


1. One simple way is to add the &js_fast_load=0&css_fast_load=0&strip=0 to the page URL for which you want to see all the JS and CSS files being used.
2. Second one is via portal-ext.properites.

javascript.fast.load=false


By default minification is enabled at portlet level and portal level. For some reason if it is not happening changing the value of "false" to "true" will work.

Thanks



Sunday, April 3, 2016

Liferay UploadRequest getFiles does not create temp files which has size lessthan ~ 1.5 KB or ~ 1 KB

Hello,

If you are using the UploadRequest.getFileNames(“testFiles”) and if you upload file size less than around 1.5 KB Liferay is not creating the temp files and the we can not use those files for saving it in the document library. So the solution would be using the below code.

InputStream[] streams = uploadReq.getFilesAsStream(“testFiles”);
for(InputStream is : streams){
                File file = FileUtil.createTempFile(inputStream);
}

Monday, February 22, 2016

Adding custom Eh Cache entry and changing default time out in Liferay

Hello,

By default all the cache objects specified in the liferay-multi-vm-clustered.xml or in the liferay-single-vm.xml are set to default idle time out for 600 seconds (5 mins). Which means if a key in the cahe is not accessed for 5 minutes will be removed from the cache. 

If you want to extends the time out or want to add your your own cache you can follow the below steps.

I am doing it for the liferay-multi-vm-clustered.xml

1.     Goto liferay-portal-6.2-ee-sp10\tomcat-7.0.42\webapps\ROOT\WEB-INF\classes
2.     Inside the classes create a folder, i have created a folder with the name ehcache.
3.     Now copy liferay-multi-vm-clustered.xml file from the portal source. If you don't have the poral source code available with you, you can download it from here. 


4.     Rename the file to your desired name, lets say custom-liferay-multi-vm-clustered.xml
5.     Edit the file and add the below entry.

                        eternal="true"
                        maxElementsInMemory="10000"
                        name="testCache"
                        overflowToDisk="false"
                        timeToIdleSeconds="0"
            >
                       
                                    class="com.liferay.portal.cache.ehcache.LiferayCacheEventListenerFactory"
                                    properties="replicatePuts=false,replicateUpdatesViaCopy=false"
                                    propertySeparator=","
                        />
           
6.     Now copy the modified file into the location liferay-portal-6.2-ee-sp10\tomcat-7.0.42\webapps\ROOT\WEB-INF\classes\ehcache
7.     In the portal-ext.properties which is inside liferay-portal-6.2-ee-sp10 folder add this entry ehcache.multi.vm.config.location=/ehcache/ custom -liferay-multi-vm-clustered.xml
8.     Now restart the server.

If you observe the entry the highlighted are the changes we made.

Making the eternal=”true” makes the elements in the cache to never expire. 

These will stay in the cache until you restart the server.

Changed timeToIdleSeconds=”0”

Monday, February 8, 2016

Using Lifeay Cache API with liferay 6.2 in a Custom Portlet or in Service Layer


Hello,

If you want to use the liferay's Cache mechanism for storing redundant network calls for improving the performance of the portal, Liferay has given api to store those values in the cache, below is thesample program .



Sunday, October 18, 2015

Dynamic Query with PortletClassLoader using PortletBeanLocatorUtil


Hello,

Recently I faced some issue with dynamic query. Even though my service layer and Portlet are in the same war  I got class not found exception for the model class. Liferay could not able to load Model class while using the dynamic query So I used the Portlet Class loader like below.


             


Then my issue got resolved.

Checking in Liferay permissions for a User in JSON WS or in a normal Java Class


Hello,

In this post we will see how to check if a user has a specific permission assigned to him. If you want to do it in a JSP you can use the below code. In JSP permissionChecker,layout and portletroot id objects are implicitly available.

boolean showOwnerType = PortletPermissionUtil.contains(permissionChecker, layout, portletDisplay.getRootPortletId(), “your permission name”);

Suppose if you want to do it in a class which does not have these implicit objects or it does not have access to HttpServletRequest or PortletRequest then you can use the below code.

PermissionChecker permissionChecker=PermissionThreadLocal.getPermissionChecker();
boolean status = PortletPermissionUtil.contains(permissionChecker, “your portlet root id”, “your permission”);


You can find the portlet root id from Liferay page. Goto the page where the portlet resides and click on

Settings icon- > Look and Feel -> and goto Advanced Styling tab. There you can see the portlet root id.


Saturday, October 10, 2015

Sending List and Map values to Liferay JSON WebService


If you want to pass a list object or map object to a Liferay JSON WebService use the below code

Sending a List of String and getting it in the ServiceLayer:

Use the below code in the ServiceImpl

public List getList(List list){
       System.out.println("list: "+list);
       return list;
}
      
From browser use the link like below to send the list object

http://localhost:8080/api/jsonws/MyPortlet.myview/get-list/list/["Liferay","WebSphere","SAP"]

And you  can see the output like below


Sending a Map of String,String object and getting it in the ServiceLayer:

Use the below code in the ServiceImpl

public Map getMap(Map map){
       System.out.println(map.size());
       Set keySet = map.keySet();
       Iterator itrtr = keySet.iterator();
       while(itrtr.hasNext()){
              System.out.println(map.get(itrtr.next().toString()));
       }
       return map;
}

From browser use the  below link to send a Map object
http://localhost:8080/api/jsonws/MyPortlet.myview/get-map/map/{"one":"one value","two":"two value"}

The output is displayed like below image