KNOWLEDGEBASE

We pride ourselves on bringing a fresh perspective and effective insight knowledge of each technology.

  • Rest Extender and JAX-RS Restful Web Service in Liferay 7 DXP

    Rest Extender and JAX-RS Restful Web Service in Liferay 7 DXP


    Rest Extender and JAX-RS Restful Web Service in Liferay 7 Let's us to implement REST Web Service for any table in Liferay's database, Using which we can access various tables in liferay

    Environment
    Liferay IDE 3.1.2 GA3
    Liferay CE Portal Tomcat 7 GA4
    JDK 8
    MySql 5.7

    Step 1 : Create a New Liferay Module Project With Rest Template 




    Step 2 : Give Package name and Component Class Name



    Click on Finish.

    Module Project With following directory structure will be created.


    In my example i am going consume the REST api to retrieve the user tables data.

    com.liferay.portal.remote.cxf.common.configuration.CXFEndpointPublisherConfiguration-cxf.properties
    contextPath=/my-rest-service
    authVerifierProperties=auth.verifier.BasicAuthHeaderAuthVerifier.urls.includes=*
    

    com.liferay.portal.remote.rest.extender.configuration.RestExtenderConfiguration-rest.properties
    contextPaths=/my-rest-service
    jaxRsServiceFilterStrings=(component.name=com.liferaystack.application.MyRestServiceApplication)
    

    Note
    1. If you want to change the context path of your module, you can use the above properties files.
    2. Include compileOnly group: "com.liferay.portal", name: "com.liferay.portal.kernel", version: "2.0.0" in build.gradle to access User Table API from Liferay's Services.
    3. If You want to implement same REST Service for custom table (entity) please add the dependency in build.gradle

    MyRestServiceApplication.java
    This is the REST Controller Where we can write out REST API Code, I have removed the created template code and added the below code into this file
    package com.liferaystack.application;
    
    import com.liferay.portal.kernel.dao.orm.QueryUtil;
    import com.liferay.portal.kernel.exception.PortalException;
    import com.liferay.portal.kernel.log.Log;
    import com.liferay.portal.kernel.log.LogFactoryUtil;
    import com.liferay.portal.kernel.model.User;
    import com.liferay.portal.kernel.service.UserLocalServiceUtil;
    import com.liferay.portal.kernel.util.ContentTypes;
    import com.liferay.portal.kernel.util.Validator;
    
    import java.util.Collections;
    import java.util.List;
    import java.util.Set;
    
    import javax.ws.rs.ApplicationPath;
    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.PathParam;
    import javax.ws.rs.Produces;
    import javax.ws.rs.QueryParam;
    import javax.ws.rs.core.Application;
    
    import org.osgi.service.component.annotations.Component;
    
    /**
     * @author Syed Ali
     */
    
    @ApplicationPath("/users")
    @Component(immediate = true, service = Application.class)
    public class MyRestServiceApplication extends Application {
    
     public Set<Object> getSingletons() {
      return Collections.<Object>singleton(this);
     }
    
     @GET
     @Produces("text/plain")
     public String working() {
      return "Web Service Works!";
     }
    
     @GET
     @Path("/getalluser")
     @Produces(ContentTypes.TEXT_PLAIN)
     public String hello() {
      List<User> users = UserLocalServiceUtil.getUsers(QueryUtil.ALL_POS,QueryUtil.ALL_POS);
      return users.toString();
     }
    
     @GET
     @Path("/getuser/{userId}")
     @Produces(ContentTypes.TEXT_PLAIN)
     public String morning(
      @PathParam("userId") long userId,
      @QueryParam("param") String param) {
    
      User user = null;
      try {
       user = UserLocalServiceUtil.getUser(userId);
      } catch (PortalException e) {
       _log.error("PortalException : "+e.getMessage()); 
      }
    
      if(Validator.isNull(user)){
       return "[No User Found With User Id : "+userId+"]";
      }
      return user.toString();
     }
    
     private static Log _log = LogFactoryUtil.getLog(MyRestServiceApplication.class);
    }
    

    Deploy the module to your and Test the REST Web Service

    Testing the Web Service


    Accessing the Root Context
    Goto http://localhost:8080/o/my-rest-service/ it will show the list of services available

    Accessing Rest Controller
    Goto http://localhost:8080/o/my-rest-service/users it will show the list of services available

    Get All Users Using Rest Controller
    Goto http://localhost:8080/o/my-rest-service/users/getalluser you can fetch all users

    Get Users with userId Using Rest Controller
    Goto http://localhost:8080/o/my-rest-service/users/getuser/20156 you can fetch users with userId 20156

    Source Code


  • Layout In Liferay 7 DXP

    Layout In Liferay 7 DXP



    Layout in Liferay Lets us define the Grids into the Liferay Page, Which allows us to systematical arrange the portlet applications and contents into the page, To Create a layout in Liferay 7 DXP, I have already created a Liferay Gradle Workspace in my machine.

    Environment
    Liferay Developer Studio/ IDE 3.1.2 GA3
    Liferay 7 CE/DXP Portal Tomcat 7 GA4
    JDK 8
    MySql 5.7

    Step 1 : Create New Liferay Module Project

     


    Step 2 : Select layout-template and click next




    Step 3 : Give Component Class Name and package name and click finish

     


    A Layout Module will be created with following directory structure



    my-layout.tpl
    This is the main file in Liferay's Layout, I am going to customize this layout into my required layout as shown below.
    Lets See how we can create the Layout s shown above, below is the simple code snippet for my layout

    my-layout.tpl

    <div class="my-layout" id="main-content" role="main">
     
     <div class="portlet-layout row" style="background-color: red;">
     
      <div class="col-md-6 portlet-column portlet-column-first" id="column-1" style="background-color: pink;">
       $processor.processColumn("column-1", "portlet-column-content portlet-column-content-first")
      </div>
    
      <div class="col-md-6 portlet-column portlet-column-last" id="column-2" style="background-color: green;">
       $processor.processColumn("column-2", "portlet-column-content portlet-column-content-last")
      </div>
      
     </div>
     
     <div class="portlet-layout row" >
      
      <div class="col-md-6 portlet-column portlet-column-first" id="column-3" style="background-color: yellow;">
       $processor.processColumn("column-3", "portlet-column-content portlet-column-content-first")
      </div>
    
      <div class="col-md-6 portlet-column portlet-column-last" id="column-4" style="background-color:aqua;">
       $processor.processColumn("column-4", "portlet-column-content portlet-column-content-last")
      </div>
      
     </div>
     
    </div>
    

    Components and Classes In Layout


    portlet-layout row : This class indicates each row container in the Layout Grid System.

    $processor.processColumn : It have 2 arguments, id and Classes
    Id : It Must be an unique identifier for each column

    col-md-6 : We can use different Boostrap Grid Classes in our layout, in our case this class indicates 50% of the available parent DIV size.

    portlet-column : Its the container which holds the Portlet column.

    portlet-column-first : use this class if you have multiple columns in the layout, it indicates first column of the layout.

    portlet-column-last : use this class if you have multiple columns in the layout, it indicates Last column of the layout.

    portlet-column-only : use this class if you have only one column in the layout.

    portlet-column-content : this class is used to indicate the portlet content in the column.

    my-layout.png :  image for the layout, we can define this image indicating layout grid.

    liferay-layout-templates.xml 
    we can define the layout template file path, layout image file path, layout id, layout name and etc

    <?xml version="1.0"?>
    <!DOCTYPE layout-templates PUBLIC "-//Liferay//DTD Layout Templates 7.0.0//EN" "http://www.liferay.com/dtd/liferay-layout-templates_7_0_0.dtd">
    
    <layout-templates>
     <custom>
      <layout-template id="my-layout" name="my-layout">
       <template-path>/my-layout.tpl</template-path>
       <thumbnail-path>/my-layout.png</thumbnail-path>
      </layout-template>
     </custom>
    </layout-templates>
    

    After applying the my-layout to an empty page, The final layout outcome must look like this.

    Note : Every Column DIV must have $processor.processColumn, If you don't provide the $processor.processColumn then that DIV will not be considered as portlet container and the DIV cannot hold any portlet.

    Just to identify the rows and columns, i have used the different colors, make sure you remove them.


  • Roles In Liferay 7 DXP

    Roles In Liferay 7 DXP


    Roles In Liferay Determines what an user can do and what an user can access in the portal, let's have a closer look at the Roles System of Liferay.

    What is a Role In Liferay ?

    Roles of an User in Liferay defines
    1. What are the actions User can perform in the Portal (action permissions) in the given scopes
    2. What are the contents the User can access in the portal (resource permissions) in the given scopes

    Types of Roles In Liferay 


    Regular Role or Portal Role

    When these roles are assigned to the user, the role's permissions are available to the User across the Portal Instance, the sub hierarchies such as Sites, Organization and User Groups.

    Site Role

    When a Site Role is assigned to an Site User, The permissions of the Role to the User Will be applicable only inside the Site and will not applicable for the User in other sites.


    Organization Role

    When an Organization Role is assigned to an Organization User, The permissions of the Role to the User Will be applicable only inside the Organization and will not applicable for the User in other Organizations.

    Regular Roles Avaialble In Liferay 


    Guest

    Guest User will be having very minimal set of permissions in the portal and are non authenticated users


    Owner

    This is can be applied to the User who is the owner of certain entities in Liferay

    Administrator

    Administrator User can access almost anything in the portal and any action in the portal and can access Sites organization and any object without any constarints 


    User

    This is the basic Role Given to an User when the user is authenticated into the Portal. 


    Power User

    This Role have the same permissions as that of the User Role, we can use this Role Just to distinguish the normal user from this previllaged role and give more access.

    Site Roles Avaialble In Liferay 


    Site Administrator

    It enables the Site Administrator User the ability to manage different assets of the sites such Site Pages, Site Membership, Site Contents and Site Settings. 


    Site Member

    This Role grants permissions to visit the privates pages of the sites and its contents including public pages


    Site Owner

    This Role is same as that of the Site Administrator Role except that it provides the ability to manage the different aspects of the sites such as deleting the site membership or roles including Site Administrator


    Organization Roles Avaialble In Liferay 


    Organization Administrator

    It enables the Organization Administrator User the ability to manage different assets of the Organization such Site Pages, Organization Membership, Organization Site Contents and Organization Site Settings.


    Organization Owner

    This Role is same as that of the Organization Administrator Role except that it provides the ability to manage the different aspects of the Organization such as deleting the Organization membership or roles including Organization Administrator


    Organization User

    This Role grants user the ability to visit the Organization Site Pages when the user belongs to the Organization

    Apart from all the above roles, we can create our own Regular, Site and Organization Roles with our own permissions in the control panel.


  • Sites In Liferay 7 DXP

    Sites In Liferay 7 DXP


    Multi Tenancy of Liferay gives flexibility to host number of sites in a single instance of Liferay Server, lets explore the sites and their hierarchy in Liferay 7 DXP in Detail.

    Global Site

    When we setup a new Liferay instance, Liferay gives us a default Global Site, If you want another site you can create it from control panel, all the newly created sites will be the sub-sites of the parent global sites. All contents of the Global Sites are accessible to the sub-sites.


    What is a Site in Liferay ?

    Sites can have its own pages which in turn can publish the web contents or portlets in the pages. Users can be assigned to the sites.

    Types of Site Membership 


    Open 

    User can become member of Open site at any point of time, any user who is having an account to the portal can join Open Site.

    Restricted

    User having an account in the portal can join this site by sending a joining request to the site, Site Administrator can approve or deny the request of the User.

    Private

    Users cannot join this site or request for the site membership of a private site, rather Site admin have to manually add the users to the site.

    Optional Sites in Liferay 7 DXP


    Parent Site

    A site can have a parent site which is already being created and inherit the behaviors from the parent Site, a site can only have one parent site, A site can have any number of sub sites.

    Personal Site

    When an user is created in Liferay, Liferay  provides the user with his own personal site as an option which can be accessible only to that user.

    Organization Site

    An Organization in Liferay can have an optional site associated with it, with public and private pages and we can define the site template for them. User must be assigned to the Organization in order to access this site.

    Location Site

    An Organization can have its Locations which also can have a site, the user must be assigned to the Location and its parent organization in order to access this site.

    User Group Site

    Users can be grouped together into teams based on which allows us to create site for the User Groups, User must belongs to the User Group In Order to access this site.


    Its our responsibility to identify whether we have to create a Site or Organization based on the requirement


  • Portlet Modes In Liferay 7 DXP

    Portlet Modes In Liferay 7 DXP


    Portlet Mode of a Portlet Indicates the function that a portlet performs and generate different content based on the function they perform. Some Specific features are provided in each portlet mode to enhance the accessibility, Each Portlet Mode have its own Portlet Controller Class and Views defined in each specific Portlet mode.

    Standard Portlet Modes of JSR-286 Portlet Specification


    View Mode

    Portlet renders markup text fragment (outcome of view) in view mode, its the most commonly used mode.

    Edit Mode

    Used to change per-user settings to customize rendering in the Portlet, We can use this mode when we want to let the User to perform actions in edit Mode

    Help Mode

    Used to display helpful information to the end User, Usually we can use this mode to give Help information to the user. 

    Additional Liferay portlet modes


    About

    About Mode Used to display the information about the Application (portlet) such as Application owner, Author etc.

    Config

    We can use this mode to Configure the Portlet Application for User Personalization and We can Store the User preferences in Portlet Preferences for giving User Specific Application behavior.

    Edit default

    This mode is used when we want to let User to Edit the default configuration and preferences of the Portlet Application.

    Edit guest

    Using This Mode we can let the Guest User to Perform edit operation (function) in the Portlet Application.

    Print

    This Mode is Used When we want to let user to print the Content of the Portlet Application Using the Printer, User can be given some additional features related to printing the content using this mode.

    Preview Mode

    We can Use This mode when we want the Ability or the User to Preview the Content or document in the Portlet Application.

    Note : Each Mode Must have the Portlet Controller Class and Views Defined for the mode.  

    Please refer the Portlet specifications for more details.


    JSR 286: Portlet Specification 2.0
    Understanding the Java Portlet Specification 2.0 (JSR 286)
    Java Portlet Specification

    I Will be covering the implementation of different portlet modes in Liferay 7 DXP in the future posts.


  • Overriding Module Apps JSP In Liferay 7 DXP

    Overriding Module Apps JSP In Liferay 7 DXP


     
    Overriding Module JSP Apps JSPs In Liferay 7 DXP, Since Liferay 7 is completely restructured its OOB Portlets into Modularized OSGi Bunles of Modules, OOB Portlets are referred as Apps or Independent Modules. In my example i am going to Override the JSP of The Famous Login Portlet.

    Environment
    Liferay Developer Studio/ IDE 3.1.2 GA3
    Liferay 7 CE/DXP Portal Tomcat 7 GA4
    JDK 8
    MySql 5.7

    Step 1 : Create a New Liferay Module Project Fragment


    Click On the New Wizard Button of the Liferay In The Liferay Developer Studio as in the above image.



    Give a Project Name, Build Type and Select Liferay Run time Environment and Click Next

    Step 2 : Select The OSGi Host Bundle To Override


    I am Overriding Login Portlet Which is available in the com.liferay.login.web Bundle, so Select the Module and Click OK

    Step 3 : Add Files From OSGi Bundle To Override



    There is an "Add Files From OSGi Bundle" Button in the right side of the window as shown above, Click on that button that List you all the JSPs in that Bundle to override as below.



    Select META-INF/resources/login.jsp as shown above, as we are overriding the login.jsp, If you want override any other jsp you can select the jsp from the list.


    Click Finish Button, This Will Create a Liferay Fragment Module Project For Login Portlet as shown in the below directory structure.


    Step 4 : Modify The JSP Of The OSGi Bundle

    Lets add some text into the login.jsp, i have added the text LIFERAY STACK LOGIN FRAGMENT at the top of the jsp page, lets deploy the login fragment module and test it. When you deploy the fragment bundle we can clearly see the com.liferay.login.web bundle is getting restarted


    Reload the Portal and Click on the Sign In Button of the Portal to See the Changes


    Awesome, Now you can modify the JSP of any App Module of Liferay DXP.
    Source Code


  • Adding Portlet To Product Menu In Liferay 7 DXP

    Adding Portlet To Product Menu In Liferay 7 DXP

    Adding Portlet To Product Menu In Liferay 7 DXP

    Adding Portlet To Product Menu In Liferay 7 DXP, In Earlier versions of liferay Before Liferay 7 we used to mention the control panel category in liferay-portlet.xml, In Liferay 7 DXP Portlet can be added to Product menu by extending  PanelApp Service

    Environment
    Liferay IDE 3.1.2 GA3
    Liferay CE/EE Portal Tomcat 7 GA4
    JDK 8
    MySql 5.7

    In Liferay 7 DXP The Layout of the Portal Have Been Changed, The New Layout of the Liferay 7 DXP Portal is Shown Below







    Step 1 : Create A Portlet

    Create A Portlet Which needs to added to the Product Menu
    In My Case I have Created  a Portlet Named "Product Admin Portlet", make the category of this portlet to hidden using "com.liferay.portlet.display-category=category.hidden"
    The Complete Portlet Class of the portlet is shown below.
    package com.liferaystack.admin.panel.app.portlet;
    
    import com.liferaystack.admin.panel.app.constants.ProductAdminPortletKeys;
    import com.liferay.portal.kernel.portlet.bridges.mvc.MVCPortlet;
    import javax.portlet.Portlet;
    import org.osgi.service.component.annotations.Component;
    
    /**
     * @author Syed Ali
     */
    
    @Component(
     immediate = true,
     property = {
      "com.liferay.portlet.display-category=category.hidden",
      "com.liferay.portlet.instanceable=true",
      "javax.portlet.display-name=Product Admin Portlet",
      "javax.portlet.init-param.template-path=/",
      "javax.portlet.init-param.view-template=/view.jsp",
      "javax.portlet.name=" + ProductAdminPortletKeys.ProductAdmin,
      "javax.portlet.resource-bundle=content.Language",
      "javax.portlet.security-role-ref=power-user,user"
     },
     service = Portlet.class
    )
    public class ProductAdminPortlet extends MVCPortlet {
    }
    


    Step 2 : Add Dependency

    Add the below Gradle Dependency to the Portlet, This Dependency is mandatory.
    compileOnly group: "com.liferay", name: "com.liferay.application.list.api", version: "2.0.0"
    to your build.gradle


    Step 3 : Create ProductAdminPanelApp Component Class

    Create a Class named ProductAdminPanelApp, this class must extend the BasePanelApp Class, implement the methods of super class as shown below and declare it as a service using service = PanelApp.class

    @Override
    public String getPortletId() {
     return ProductAdminPortletKeys.ProductAdmin;
    }
    
        @Override
        @Reference(target = "(javax.portlet.name=" + ProductAdminPortletKeys.ProductAdmin + ")",unbind = "-")
        public void setPortlet(Portlet portlet) {
            super.setPortlet(portlet);
        }
    

    The Complete ProductAdminPanelApp Will be

    package com.liferaystack.admin.panel.app.portlet;
    
    import com.liferay.application.list.BasePanelApp;
    import com.liferay.application.list.PanelApp;
    import com.liferay.application.list.constants.PanelCategoryKeys;
    import com.liferay.portal.kernel.model.Portlet;
    import com.liferaystack.admin.panel.app.constants.ProductAdminPortletKeys;
    
    import org.osgi.service.component.annotations.Component;
    import org.osgi.service.component.annotations.Reference;
    
    /**
     * @author Syed Ali
     */
    
    @Component(
         immediate = true,
         property = {
             "panel.app.order:Integer=100",
             "panel.category.key=" + PanelCategoryKeys.CONTROL_PANEL_USERS
         },
        service = PanelApp.class
    )
    public class ProductAdminPanelApp extends BasePanelApp {
    
        @Override
        public String getPortletId() {
     return ProductAdminPortletKeys.ProductAdmin;
        }
    
        @Override
        @Reference(target = "(javax.portlet.name=" + ProductAdminPortletKeys.ProductAdmin + ")",unbind = "-")
        public void setPortlet(Portlet portlet) {
            super.setPortlet(portlet);
        }
    }
    

    You are done just deploy the portlet. After Deploying the created portlet you can see your portlet under Users Category Of Control Panel Section of the Product Menu. as shown below.



    Source Code


  • OSGI In Liferay 7 DXP

    OSGI In Liferay 7 DXP

    OSGI In Liferay 7 DXP


    OSGi In Liferay 7 DXP is a new concept introduced In Liferay 7, The main intention behind OSGi is to split the Liferay 7 Portal  Modules independent of each other. I am gonna introduce you to some of the basic concepts of OSGI such OSGi Modules, OSGi Components and OSGi Services.

    What is Modularity?

    A Software application is Consists of different parts, they are typically called as software components. These components interact with each other using the API's. API's are nothing but the Classes, Interfaces and Methods defined in a Module.

    What is a Module?

    A module can be defined a part of Software application composed of different Components and Services, Service can be used to perform any action. In OSGI a Module is collection of classes and interfaces as a Bundle, Finally Its a JAR file with some meta data. In older versions of Liferay we were using the concept of plugins, now in Liferay 7 DXP any new project we create is called as module.

    What is OSGi?

    OSGI Is  service oriented platform on top of unique class loading mechanism, its enables features such as versioning of modules, encapsulation of API's (Classes) and run-time dynamics(starting stopping updating modules at run-time)

    Problems with Non Modular Applications

    Blurring the responsibility
    Increases complexity
    maintenance becomes harder over time

    Benefits of OSGi

    Instead of gigantic big application, using OSGI we can separate them as independent modules.
    Easy maintenance
    Each Module with its own clear responsibility
    Restrict and Expose the visibility of classes properly

    OSGi Component

    If you want the lifecycle of your object to be managed by the OSGi container,  you can declare it as a component. Using Declarative Service annotations,  you can make a POJO Class a OSGi component by annotating it with @Component With this,  you'll get the ability to start, stop and configure the components.

    OSGi Service

    OSGi components classes can be made as OSGi services just by annotating it with @Service annotation. Services should implement atleast an interface. When you mark a component as service, you can use (call) this service from other osgi components.
    Any class defined as a component can be used only inside the module, where as when you declare class as service, it can be accessible outside the module, you can export them, the other modules just have to import them in bnd.bnd files in order to use the service. 

    What is Dependency In OSGI?

    When a module uses the services of another module we can say the Module 1 is Dependent on Module 2.

    What is OSGI Container?

    OSGI Container manages the Lifecycle of  Components, Services and Bundle or Modules

    What is Bundle?

    OSGI Modules (Projects) are called as bundles. 


    Bundle Lifecycle

    The life cycle of a bundle is shown in the below picture, once the module is published(deployed), it will go through the Life cycle as shown below.


    Publish Find Bind Model

    OSGi Works on the model called publish-find-bind If a module wants to consume the service of the other module, the other module have to be published, the consumer module have to find the published module and bind it to consume its services


    OSGI Framework Architecture


    Bundles – Bundles are the OSGi components that we create in module project.

    Services – Services connects bundles dynamically using a publish-find-bind model for POJO Objects (Services and components).

    Life Cycle – An Interfce to install, start, stop, update, and uninstall bundles.

    Modules – It's a layer that defines how a bundle can import and export packges,classes codes.

    Security – A layer to the handles the security features.

    Execution Environment – Tells Which methods and Classes are available in the specific platorm.


    I know most of us will not understand OSGi when we read it for the first time, i will give more information on OSGi with examples in detail soon.


  • Portlet Filters In Liferay 7 DXP

    Portlet Filters In Liferay 7 DXP

    Portlet Filters In Liferay 7 DXP

    Portlet Filter is used to intercept and manipulate the request and response before it is delivered to the portlet at given life cycle such as action, render and resource

    Environment
    Liferay IDE 3.1.2 GA3
    Liferay CE Portal Tomcat 7 GA4
    JDK 8
    MySql 5.7

    What is Portlet Filter?

    Portlet Filter is used to intercept and manipulate the request and response before it is delivered to the portlet at given life cycle such as action, render and resource

    Types Of Filters For A Liferay Portlet

    Action Filter
    Render Filter
    Resource Filter
    Base Filter

    Which Filter To Use?

    Action Filter : To Intercept only The Action Requests
    Render Filter : To Intercept only The Render Requests
    Resource Filter : To Intercept only The Resource Requests
    Base Filter : To Intercept all the http request comes to your portlet.

    How to Implement Portlet Filter In liferay 7 DXP?


    Step 1 : Create A Module Project for Portlet
    If you want to know how to create a portlet module project in Liferay 7 DXP, please refer my earlier blog, Below is my Portlet Controller Class.


    Step 2 : Create A Portlet Filter Component Class
    You can create Portlet Filter Component Class Using Eclipse, by right clicking on you portlet module project and select select "New Component Class" and Select "Portlet Filter" Component Template, I prefer to create a class manually for now, I Am creating a Class named "MyRenderFilter"

    Step 3 : Component Declaration
    Declare the Class as an OSGI Component Using the Below Code Snippet

    import org.osgi.service.component.annotations.Component;
    import javax.portlet.filter.PortletFilter;
    @Component(immediate = true, 
     property = {
      "javax.portlet.name="+MyPortletKeys.My, 
     },
     service = PortletFilter.class
    )
    
    Note : Make sure you give correct portlet name and service must be PortletFilter.class

    Step 4 : Extend The RenderFilter
    import javax.portlet.filter.RenderFilter;
    public class MyRenderFilter implements RenderFilter{}
    

    Step 5 : Implement Unimplemented Methods of the Parent Class RenderFilter
    public void init();
    public void destroy();
    public void doFilter();
    

    Complete MyRenderFilter.java Code

    Each time when you Hit a Render URL doFilter() method the MyRenderFilter will be triggered.

    Portlet Filter For Action Request


    Similarly You can create the filter for all Action Request By extending ActionFilter Class, as shown below


    Portlet Filter For Resource Request


    For all Resource request you can extend your class with ResourceFilter Class, as shown below

    Servlet Filter In Liferay 7 DXP

    This Filter Intercepts all the requests comes to the portlet, whether it is action, render, resource and it also can be HttpServletRequest , if you want filter all the Http Servlet Request comes to your portlet please use this filter, your filter component just have to extend "BaseFilter" Class, refer the below code

    Note : This BaseFilter Will be Triggered Before all the other filters such as ActionFilter, RenderFilter and ResourceFilter.
    I have created a portlet which can easily make you understand flow of Portlet Filters and Portlet Requests In detail, please download the portlet and deploy to test, it can give more insight into the portlet Filters In Liferay, when you add this portlet to the page init() and doFilter() method will be triggered.


    Source Code


  • Logging In Liferay 7 Using Loggers

    Logging In Liferay 7 Using Loggers

    Logging In Liferay 7 Using Loggers

    Logging In Liferay 7, Liferay Uses Log4J Library for Logging Java Classes, In this blog lets see the Liferay Log Configuration  In Liferay, Liferay Portlet Logging and Log Levels in detail.

    Before Going Into The actual Logging and Loggers in liferay, Lets answer few questions on Logging.

    What is a Log?
    In our Java Code we were using System.out.println("message"); to log (print) the information end error messages, These logs will be printed in the Console and log files, These logs are used to debug the code.

    Why do need a Logging System using Loggers?
    The Logs printed using the System.out.println (sysout) are not more informative, when code snippet System.out.println("message"); gets executed, the only thing it will print on the console screen and log file is message. No other informations.
    There will be NO information about
    1. From Which Class the Log is Generated
    2. At What Time The Log Is Generated
    3. What is the Severity of the message

    If We Use the Loggers to Log the Messages In Java Classes, The log message will holds the information, From Which Class The Log Is Generated, At What Time The Log Is Generated and What is the Severity of the message. Which is not available in sysout.
    Logging In Liferay 7 Using Loggers

    What are the Log Levels Supported By Liferay?
    There are 6 log Levels supported by Liferay, you can decide the level based on the log severity.
    INFO : To Log the useful  information.
    DEBUG : To Log Debug information, use it to debug the code.
    WARN : To Log the warning messages, which are not critical
    ERROR : To Log the error message which blocks a behavior (functionality) being
    FATAL : To Log the messages which can be a severe or vital issue that harms the complete system.
    TRACE : To Log the exception call traces of the code

    Enabling  And Disabling Log Level For A Class
    We can define and configure the Log levels for Each and Every Java Class In the Server Administration section of Control Panel.

    Control Panel  >> Configuration >> Server Administration >> Log Levels

    Liferay 7 Loggers Server Administration

    If we Set The Log Level to ALL, Logs with all Levels will be printed.
    If we Set The Log Level to OFF, No Logs will be printed at all for the specified class.
    If we Set The Log Level to To Info, Only Logs printed using Info Level will be printed, Other Log Levels In Same Class Will not be printed.

    Creating a Logger In Liferay 7
    You can define a Logger for a Class using the below code snippet.
    import com.liferay.portal.kernel.log.LogFactoryUtil;
    private static Log _log = LogFactoryUtil.getLog(LoggerPortlet.class);
    

    To Log The Messages With Different Log Levels.
    _log.info("Use INFO To Log Useful Information");
    _log.warn("Use WARN To Log Warning Message");
    _log.error("Use ERROR To Log Error Message ");
    _log.debug("Use DEBUG To Log Debug Message ");
    _log.fatal("Use FATAL To Log Fatal Message");
    _log.trace("Use TRACE To Log Trace Message of the exception");
    

    How To Check The Log Level of the class
    A Simple _log.info("Use INFO To Log Useful Information"); Will print the message even if you disable(turn OFF) the Log Level for the class in the server administration. As A Best Practice, we must check the Log Level before Logging a message in that level.
    if(_log.isInfoEnabled()){
     _log.info("This Messge Will be printed only if the info log level is enbled"
     + " for the class 'LoggerExampleClass' from Liferay Server Administration");
    }
    

    To Check The Log Other Levels of a Logger.
    _log.isInfoEnabled();
    _log.isWarnEnabled();
    _log.isErrorEnabled();
    _log.isDebugEnabled();
    _log.isFatalEnabled();
    _log.isTraceEnabled();
    

    Liferay 7 Logging Example
    Summing up all the parts into a Java class for an easy reference, this Liferay Portlet Example can give you clear idea of the Logging System in Liferay, Use the below code snippet to know more about the loggers in liferay.


  • Liferay 7 Interview Questions

    Liferay 7 Interview Questions


    Liferay 7 Interview Questions, There are number of Liferay 7 DXP Interview Questions asked In Liferay Portal Interview, There can be different variance in the Questions for Beginners, Experienced Developers and  Architects, Lets see some of them.

    What is Liferay?

    What is Portlet?

    What is Service Builder and What are its uses?

    What are the different module types supported in Liferay 7?

    What is IPC, What are the types of IPC?

    Explain Different IPC's In detail?

    Explain The Permission System in Liferay ?

    What is the difference between Site and an Organization?

    What are the types of Roles available in Liferay?

    What are the differences between Organization and Location?

    What are the New Features Available in Liferay 7?

    What is Kaleo Workflow?

    How to implement Workflow for Custom entity?

    What is the default Role assigned to an user When an user is created?

    What are the types of Pages in Liferay?

    What are the differences between Servlet and Portlet?

    What are the Implicit Objects available in Liferay's Portlet JSP?

    What is OSGI?

    What is a Service and a component in Liferay 7?

    What is the use of OSGI in Liferay 7?

    What are the important Files in Liferay Theme?

    What are the view Template Languages Supported in Liferay Theme?

    Explain The Different types of URL's available in Liferay?

    What is Asset Framework In Liferay?

    How to make a Custom Entity as an Asset In Liferay 7?

    What are the component class Template types available in Liferay 7?

    What is Model Listener How to Implement Model Listener In Liferay 7?

    What is Message Bus, How to Implement Message Bus in Liferay 7?

    How to Configure Multiple Databases in Liferay 7?

    Explain the steps to create Friendly in Liferay 7 Portlet?

    Explain the Steps to Integrate LDAP In Liferay 7?

    Explain Site Template In Liferay 7?

    Explain Administration In Liferay 7?

    Explain Dynamic Query In Liferay with example?

    How to Implement Custom SQL in Liferay 7?

    How To Override A Module's JSP and Portal JSP In Liferay 7?

    Explain all the options available in Liferay 7 Service Builder Module?

    What are the Password Encryption types supported in Liferay 7?

    What is Expando in Liferay, Explain The Tables and Taglibs Used to access the Expandos?

    Explain Spring MVC Portlet Annotations?

    Explain the Configuration Files in Spring MVC Portlet?

    What is Structure and Template Liferay 7?

    How To Make an Entity With Composite Key?

    What are the Differences between JSR-168 and JSR-286?

    What are the types portlet supported by Liferay Other than Liferay MVC Portlet?

    Explain the different Properties in the Manifest File In Liferay 7 Bundle?

    What is product menu in Liferay 7, How To customize the control panel in Liferay 7?

    What is Elastic Search in Liferay 7? What are its uses?

    What is Boolean Query in Liferay 7?

    What are Out of Box Portlet(OOB) available in Liferay 7?

    What are best Practices in Liferay 7?

    What is Document and Media Portlet and What are its uses?

    What is Dynamic Data List(DDL) in Liferay 7?

    How to implement Recycle Bin(Trash) for a Custom Entity in Liferay 7?

    How yo add Portlet to Control Panel category?

    What is a Role in Liferay 7? Explain in detail?

    What is User Group in Liferay 7?

    How to implement Import/Export functionality for custom model Portlet?

    What is Portlet Preferences?

    What is Portal Preferences?

    What is Window State and What are all Window States available in Liferay?

    Explain the Portlet Lifecycle in Liferay?

    Explain the Portlet Phases in Liferay?

    Mention few Table in Liferay related to User?

    What are Login types supported by Liferay other than Email Id?

    What is Staging in Liferay and What is uses?

    How to Configure a Default Landing Page for a Site in Liferay?

    What are the analytics supported in Liferay?

    What are the Document Management Repositories supported by Liferay 7?

    What are steps to integrate Alfresco DMS in Liferay 7?

    What is an Application Display Template and What are its uses?

    What are Portlet Bridges supported by Liferay 7?

    What are Document stores supported by Liferay 7?

    Explain the Technical Architecture of Liferay 7 in detail?

    What are steps to Integrate Solr 5 in Liferay 7?

    How to implement SSO in Liferay 7?

    What is Page Template?

    Explain the Classes & Files Generated By the Service Builder Lieray 7?

    Explain the Database Cache Mechanism in Liferay?

    What is Service Component Number in Liferay?

    Explain Search Container and Its Tags?

    What is the role of a Portlet Container Explain In Details?

    How To implement Configuration Mode for a Liferay MVC Portlet?

    Explain the types of Portal?

    What are the Pre-requisite to Install Liferay 7 In Local Machine?

    How to Embed a Portlet Inside a Portlet In Liferay 7?

    How to Embed a Portlet Inside Theme In Liferay 7?

    How to Embed a Portlet Inside Layout In Liferay 7?

    Explain The Anatomy of Portlet In Liferay 7?

    Explain The Anatomy of Theme In Liferay 7?

    Explain The Anatomy of Layout In Liferay 7?

    What are the types of exceptions thrown while using the Service Builder API?

    What is Colour Scheme In Liferay Theme?

    What is a Site in Liferay?

    What is a Role in Liferay?

    What is a Organization in Liferay?

    What is a User in Liferay?

    What is a UserGroup in Liferay?

    How to Integrate Sonar Cube In Liferay 7 Module?

    How to COnfigure Jenkin For the Liferay Projects?

    How To Configure ReCaptcha In Liferay 7?

    What is a Upgrade Processor In Liferay and How to Implement it?

    What is a Startup Action In Liferay and How to Implement Startup Action?

    How to Implement ShutDown Hook in Liferay 7?

    What is PortletConfig Object In Liferay ?

    How to Implement Localization In Liferay 7?

    How to access files from Language.properties files?

    How to fetch values from Expando tables?

    What are the Important Utility Classes available in Liferay 7?

    How to Implement Error Message and Success Messages In Liferay 7?

    What is an IFrame Portlet?

    What is an Proxy Portlet?

    How to enable web services for an Entity?

    What is GoGo Shell and How to Use It In Liferay 7, What are the gogo shell commands in Liferay 7?

    What the Gradle commands available in Liferay 7 ?

    What the Maven commands available in Liferay 7 ?

    What is portal-ext.properties file?

    What is portal-setup-wizard.properties file?

    How to Index(add) Custom Entity Into Elastic Search or Solr?

    How to Embed a Web Content(Journal Article) In Theme?

    How to Embed a Web Content(Journal Article) In Layout?

    How to Embed a Web Content(Journal Article) In Portlet?

    How to create a new Struts Action for a Existing OOB Portlet In Liferay 7?

    How Provide Model(Entity) level Permission for a Custom Entity in Liferay 7?

    How to Restrict User To Perform Certain Action Inside a Portlet?

    What is Resource Permission And Resource Action Liferay 7?

    How to Configure EH Cache In Liferay 7?

    How to Configure CDN In Liferay 7, What are its uses?

    How to Configure Domain Name (Virtual Host) In Liferay 7, What are its uses?

    What are the Differences between Liferay 7 CE(Community Edition) and Liferay 7 DXP(Enterprise
    Edition)?

    What is IndexPostProcessor In Liferay 7?

    What is ActionCommand, ResourceCommand and RenderCommand In Liferay 7?

    What is Portlet Filter in Liferay 7, How to Implement it?

    What is Class Loader ? What are the types of ClassLoaders available in Liferay 7?

    What are the Portlet Modes Available in Liferay 7 and Explain them?

    What is the Directory (Folder) Structure of a Portlet Module In liferay 7?

    What are the Databases Supported by Liferay 7?

    What is HotFix Patching and How to do HotFix Patching in Liferay 7 DXP?

    What are the steps to migrate plug-in from Liferay 6.2 to Liferay 7 DXP?

    How to Configure Tomcat Clustering in Liferay 7? What are its Uses?

    Explain The Flow on How an HTTP request is Converted to Portlet Request In Liferay ?

    How to Use JUnit In Liferay Portlet Classes?

    Explain the Parameters available in the Portlet URL and their uses?

    What is Multi tenancy In Liferay ?

    Currently there are around 150 Questions,  I am planning to increase the questions count to 500, then I Will Categorize the Questions and based on difficulty level and categories, As Soon Soon as Possible.



  • AUI Popup and AUI Modal Dialog In Liferay 7

    AUI Popup and AUI Modal Dialog In Liferay 7

    AUI Popup and AUI Modal Dialog In Liferay 7

    Liferay has provided AUI Popup and AUI Modal Dialog Using them we can create the aui popup window in Liferay, If you are planning to perform any render or actions inside the popup the AUI Popup Window is ideal, as it is rendered Inside an IFrame action success message and error messages can be shown in the popup , if you're not performing actions rather showing confirm box or Ajax is involved in the interaction you can go ahead with modal Dialog, it is not rendered in IFrame.

    Environment
    Liferay IDE 3.1.2 GA3
    Liferay CE Portal Tomcat 7 GA4
    JDK 8
    MySql 5.7

    There are many ways in which you can create a popup in JSP, and render the content, content can be  JSP or The content of a DIV, Lets see how to render a JSP Inside the Popup First

    AUI Popup Using 'UseDialog'

    Liferay 7 has come up with one of the coolest and simplest way which allows you to render JSP in other words a Render URL without any AUI Script for the popup.


    AUI Popup and AUI Modal Dialog In Liferay 7






    Using AUI Script

    If you want to Render a JSP in your popup and you want to define your own popup size, title, and other attributes then you can use the AUI Script Popup to render Customized Popup in your portlet 


    AUI Popup and AUI Modal Dialog In Liferay 7





    Using Modal Dialog

    If You Just Want to render the content of a DIV into the Popup you can use the Modal Dialog 
    Note : Don't perform any action in the Modal Dialog Popup, it is not suitable for performing any action or render in the popup.



    AUI Popup and AUI Modal Dialog In Liferay 7

    You can download the complete source code from the below url and please deploy and let me know if it have any issues.


    Source Code


  • Implicit Objects In Liferay 7 JSP

    Implicit Objects In Liferay 7 JSP

    Implicit Objects In Liferay 7 JSP

    Implicit Objects In Liferay JSP, We Can access Various Information related to user, portlet, layout, theme, etc in the JSP files Using Liferay JSP Implicit Objects . Lets see what are all Complete List of Liferay Implicit Objects available in Liferay 7.

    Environment
    Liferay IDE 3.1.1 GA2
    Liferay CE Portal Tomcat 7 GA4
    JDK 8
    MySql 5.7


    In Liferay have two taglibraries which grant access to Liferay JSP Implicit Objects.

    Implicit Objects From portlet:defineObjects Taglib

     

    To Get all these implicit objects, <portlet:defineObjects /> must be added In the JSP, these objects holds the information about the portlet parameters and  related data.

    •  actionRequest
    • actionResponse
    • eventRequest
    • eventResponse
    • liferayPortletRequest
    • liferayPortletResponse
    • portletConfig
    • portletName
    • portletPreferences
    • portletPreferencesValues
    • portletSession
    • portletSessionScope
    • renderRequest
    • renderResponse
    • resourceRequest
    • resourceResponse
    • searchContainerReference


    Implicit Objects From liferay-theme:defineObjects Taglib

     

    To Get all these implicit objects, <liferay-theme:defineObjects /> must be added In the JSP, these objects holds various information related user, portlet, layout, theme, portal, permissions etc.

    • account
    • colorScheme
    • company
    • contact
    • layout
    • layouts
    • layoutTypePortlet
    • locale
    • permissionChecker
    • plid
    • portletDisplay
    • portletGroupId
    • realUser
    • scopeGroupId
    • theme
    • themeDisplay
    • timeZone
    • user

     

    Implicit Objects From JSP

    These are the default implicit objects available from the JSP, you dont have to include any taglib for them 
    • application
    • config
    • out
    • page
    • pageContext
    • request
    • response
    • session


    All Implicit Objects



    Use The above JSP a Reference for Taglibs and Imports


  • Portlet Window States In Liferay

    Portlet Window States In Liferay

    Portlet Window States In Liferay

    A Portlet Window State Indicates area of the portlet to be rendered by the portlet, Basically 3 Portlet States are supported by the JSR Portlet Specification Standard, Liferay Provides 2 Additional States For more flexibility on the Render State Controll.

    1. Normal
    2. Maximized
    3. Minimised
    4. Popup
    5 .Extended


    <%@page import="com.liferay.portal.kernel.portlet.LiferayWindowState"%> <%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%> <%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui"%> <%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet"%> <%@ taglib uri="http://liferay.com/tld/theme" prefix="theme"%> <%@ taglib uri="http://alloy.liferay.com/tld/aui" prefix="aui"%> <portlet:defineObjects /> <theme:defineObjects /> This is the <b>Window State</b> portlet in View mode. <!-- JSR Specific Portlet Window States --> <portlet:renderURL var="normarURL" windowState="<%=LiferayWindowState.NORMAL.toString()%>"/> <portlet:renderURL var="maximizedURL" windowState="<%=LiferayWindowState.MAXIMIZED.toString()%>"/> <portlet:renderURL var="minimisedURL" windowState="<%=LiferayWindowState.MINIMIZED.toString()%>"/> <!-- Liferay Specific Portlet Window States --> <portlet:renderURL var="popupURL" windowState="<%=LiferayWindowState.POP_UP.toString()%>"/> <portlet:renderURL var="exclusiveURL" windowState="<%=LiferayWindowState.EXCLUSIVE.toString()%>"/> <br> <aui:button href="${normarURL}" value="Normal"/> <br> <aui:button href="${maximizedURL}" value="Maximized"/> <br> <aui:button href="${minimisedURL}" value="Minimised"/> <br> <aui:button href="${popupURL}" value="Popup"/> <br> <aui:button href="${exclusiveURL}" value="Exclusive"/>


    Deploy The portlet to know how Window States Works in Liferay Portlet


  • Spring Portlet In Liferay 7 DXP

    Spring Portlet In Liferay 7 DXP

    Spring Portlet In Liferay 7 DXP

    Creating Spring Portlet was a nightmare In Liferay 7 DXP, in the Latest version of the Liferay IDE it has been made so simple to create a Spring Portlet Using Liferay IDE

    Environment
    Liferay IDE 3.1.1 GA2
    Liferay CE Portal Tomcat 7 GA4
    JDK 8
    MySql 5.7

    Step 1: Create A Module Project

    Select "spring-mvc-portlet" project Template.


    Click Next

    Step 2 : Portlet Class Name and  Package Name



    Click Finish, Then You can see the project directory structure as shown below.


    Congrats, You have just created a Spring Portlet in Liferay 7.

    Portlet Controller Class

    Annotations in Spring MVC Portlet


    • @Controller

    When we use this annotation in the class The Spring Bean Container Considers the Class as a Portlet Controller which handles all the portlet requests comes to the portlet

    • @RequestMapping

    As we know there are different modes of portlet such as VIEW, EDIT and HELP, We can configure the portlet to handle the request only for a specific mode, in our case we are going to use the Portlet Controller only for handling requests in VIEW mode,  so it will be @RequestMapping("VIEW")

    • @ActionMapping

    In Liferay Portlet MVC we have processAction() method to handle the Action Requests comes to the portlet, similarly we can define Action Methods for Action URLs using this  Annotation

    • @RenderMapping

    We can use the @RenderMapping annotation to handle the Render Requests for Render URLs, We can Return the Page Name in the Render Methods, the returned page will be rendered in the portlet.

    • @ResourceMapping

    In Liferay Portlet MVC we have  serveResource() method to handle the Resource Requests, similarly  we can define Resource Methods for Resource URLs using this annotation with an id


    Web.xml

    web.xml is the root file for spring portlet, Spring Portlet Configurations starts from this file.

    portlet-application-context.xml

    View Resolver Configurations can be made in this file

    • contentType : view character encoding
    • prefix : view Files Path
    • suffix : view (File)Type
    • viewClass : View Resolver class for Markup language