Featured post

Docker setup for Liferay 7 with MySQL

Thursday 3 May 2018

Background Task Liferay 7 or DXP Part 1


Executing or scheduling background task is always an important activity for developers.

There are so many scenarios where background tasks are used like import/export, sending bulk notification or email, processing orders etc.

You can find this interesting link here which talks in length for background tasks.
What it does not offer is implementation example and changes for background task in Liferay 7/DXP

By following few Liferay conventions, we can easily use this weapon.
In this example I will explain creation of background task, pass parameters and handle errors if any.

In next example I will demonstrate check status, progress and display it over UI.


We will be creating two osgi components for this activity.
1. Create osgi module for background task creation and register it to handler
2. Use above module to create background tasks


Render method to create background task every time portlet loads.

@Overridepublic void doView(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException {
    Random random = new Random(12);

    HttpServletRequest request = PortalUtil
            .getHttpServletRequest(renderRequest);

    ThemeDisplay themeDisplay = (ThemeDisplay) renderRequest
            .getAttribute(WebKeys.THEME_DISPLAY);

    ServiceContext serviceContext = null;
    try {
        serviceContext = ServiceContextFactory.getInstance(renderRequest);
    } catch (PortalException e) {
        logger.error("Eror in getting service context", e.getCause());
    }

    // This taskContextMap can be used as transporter to background job
    Map taskContextMap = new HashMap<>();
    taskContextMap.put("processName", "testing " + random.nextInt());
    taskContextMap.put("totalNodes", String.valueOf(random.nextInt()));
    //taskContextMap.put("serviceContext", serviceContext);    try {
        // Adding the job to liferay background manager
        com.liferay.portal.kernel.backgroundtask.BackgroundTask backgroundTask = backgroundTaskmanager.addBackgroundTask(themeDisplay.getUserId(),
                themeDisplay.getScopeGroupId(), SampleBackgroundTaskExecutor.class.getName(),SampleBackgroundTaskExecutor.class.getName(),taskContextMap, serviceContext);
        // With returned background object you can check status, id etc.
        renderRequest.setAttribute("backgroundTaskId",
                backgroundTask.getBackgroundTaskId());

    } catch (PortalException e) {
        logger.error(e.getCause());
    } catch (SystemException e) {
        logger.error(e.getCause());
    }




Create Background Task Executor

@Component(
        immediate = true,
        property = {"background.task.executor.class.name=com.netcracker.cabinet.background.executor.MigrationBackgroundTaskExecutor"}, 
        // Without this property osgi will not register this as background executor/handler
        service = BackgroundTaskExecutor.class)
public class SampleBackgroundTaskExecutor extends BaseBackgroundTaskExecutor {


isSerial - True

// if it's not serial then multiple instances of this executor can run parallel, to run it in queue mode, we use isSerial true
@Overridepublic boolean isSerial() {
    return true;
}



Main execute method for the Job

public BackgroundTaskResult execute(BackgroundTask backgroundTask)
      throws Exception {
      // taskContextMap which is sent by the caller
      Map taskContextMap = backgroundTask.getTaskContextMap();

      String taskName = (String)taskContextMap.get("processName") ;
      String totalNodes = (String)taskContextMap.get("totalNodes");

      //ServiceContext serviceContext  = (ServiceContext) taskContextMap.get("serviceContext");
   if(LOGGER.isDebugEnabled()){
      LOGGER.debug("Task Name : "+ taskName);
   }

   BackgroundTaskVO messageContent = new BackgroundTaskVO();
   messageContent.setTotalNodes(totalNodes);

   // Sending the data to util for MessageBus
   SampleDataHandlerStatusMessageSenderUtil.sendStatusMessage(messageContent);

  // Telling the system if, background task is successful or not
   BackgroundTaskResult backgroundTaskResult = new BackgroundTaskResult(
      BackgroundTaskConstants.STATUS_SUCCESSFUL);
   backgroundTaskResult.setStatusMessage("Wonder full");
   return backgroundTaskResult;
}



Message Bus update

public static void sendStatusMessage(BackgroundTaskVO messageContent) {
   // Leave if no background task
   if (!BackgroundTaskThreadLocal.hasBackgroundTask()) {
      return;
   }

   // Message Creation
   Message message = createMessage(messageContent);

   // Send message to message bus
   MessageBusUtil.sendMessage(DestinationNames.BACKGROUND_TASK_STATUS,
         message);
}


You can download the source code from here

You are just done, Try & Enjoy the function.............:)

Tuesday 10 April 2018

Osgi Target, Property attributes and Multiple Implementations

Osgi Target and Property attributes

In Spring you can use @Qualifier to get the correct class for respective instances.
How can we achieve this in OSGI framework is the question?

With help of Osgi and it's SCR(Service Component Runtime) you can use dynamic instantiation of Declarative Services.
Correct implementation should be selected for each instance to develop and use IS-A relationships.

Developers can instantiate a interface with different implementations by utlizing "target" attribute inside @Reference annotation. All you need is to enable the classes to get it from SCR with "property" attribute inside @Component annotation.

Below is the sample code to achieve the described matter

Problem Statement : We need to convert Numbers from string in different formats like Integer, Double, Float etc. and use them in different osgi components.


We will create interface and two classes which will implement this interface and consume them in another component for demonstration purpose.



public interface OsgiTarget {

 public Number convert(String str);
}

As you can see we defined multiple properties inside @Component annotation.

@Component(property = {
        "osgi.target.example.integer=true",
        "osgi.target.example.integer.again=true" // Dummy entry for multiple target example, you can get this instance with this property as well
})
public class OsgiTargetIntegerImpl implements OsgiTarget {
 @Override
 public Integer convert(String str) {
  return Integer.valueOf(str);
 }
}

Here we have single property for the same.

@Component(property = "osgi.target.example.double=true")
public class OsgiTargetDoubleImpl implements OsgiTarget{

 @Override
 public Double convert(String str) {
  return Double.valueOf(str);
 }
}


Now we have basic converters with different implementations.
We will use them in our component to show how it's going to work.


@Component(immediate = true)
public class OsgiTargetExampleConsumer {
 
 // Osgi target property for OsgiTargetIntegerImpl
 @Reference(target = "(osgi.target.example.integer=true)")
 OsgiTarget osgiTargetInteger;
 
 // Osgi target property for OsgiTargetIntegerImpl with another property but same instance 
 @Reference(target = "(osgi.target.example.integer.again=true)")
 OsgiTarget osgiTargetIntegerAgain;
 
 // Osgi target property for OsgiTargetDoubleImpl
 @Reference(target = "(osgi.target.example.double=true)")
 OsgiTarget osgiTargetDouble;

 
 @Activate
 public void run() {
  System.out.println("__________________" + osgiTargetInteger.convert("21"));
  System.out.println("__________________");
  System.out.println("__________________" + osgiTargetInteger.convert("12"));
  System.out.println("__________________");
  System.out.println("__________________" + osgiTargetDouble.convert("1987"));
 }
}


In the above class we are using the interface with @Reference annotation to get the object from SCR with respective classes just by adding target . If you look closely, you can see one class can be invoked by multiple properties as well.

Output will be similar like this :



You are just done, Try & Enjoy the function.............:)

Thursday 2 November 2017

Override OOTB javascript in Liferay 7 or LIferay DXP

Liferay becomes modular and it requires more efforts to extend it's functionality. I believe that is for our own good but yeah it becomes complex.

To override a javascript file now requires a bundle with some specific entries.
In this example I am overriding logo editor of Liferay and adding a console log to demonstrate the same.

  1. BND
  2. CONFIG.JS
  3. FILE_TO_OVERRIDE
1.BND

Liferay-JS-Config takes the config file which does the magic to override. Even though this file is useful for multiple occasions, for now we will use it for this purpose only.
Web context path for web resources, in our case it's java script.




Bundle-Name: logo-editor-for-snap
Bundle-SymbolicName: logo-editor-for-snap
Bundle-Version: 1.0.0
Liferay-JS-Config: /META-INF/resources/js/config.js
Web-ContextPath: /logo-editor-web-override



2.CONFIG.JS

  • base: File location to override
  • path: File Name
  • condition: In which scenario this module should be loaded
    • name: Name of the module
    • trigger: Should trigger when a call made to module, e.g. liferay-logo-editor
    • when: This module replace triggered module with value instead. There are multiple values possible for this, subject to explore.
;(function() {
    AUI().applyConfig(
        {
            groups: {
                sessionext: {
                    base: MODULE_PATH + '/js/',
                    combine: Liferay.AUI.getCombine(),
                    filter: Liferay.AUI.getFilterConfig(),
                    modules: {
                        'liferay-logo-editor-override': {
                            path: 'liferay-logo_editor.js',
                            condition: {
                                name: 'liferay-logo-editor-override',
                                trigger: 'liferay-logo-editor',
                                when: 'instead'
                            }
                        }
                    },
                    root: MODULE_PATH + '/js/'
                }
            }
        }
    );
})();



3. FILE TO OVERRIDE [e.g. logo_editor.js renamed to liferay-logo_editor.js]

Change the module name at first line.


AUI.add( 'liferay-logo-editor-override', function(A) { var Lang = A.Lang;
....
....
....
renderUI: function() { var instance = this; console.log("LOGO EDITOR OVERRIDDEN...");
....
....


I took the reference from this post and explained as much I can.
Thanks to Liferay Staff members for the snippets!

You are just done, Try & Enjoy the function.............:)


Monday 11 September 2017

Auto Login Liferay 7 \ Lliferay DXP

Auto Login : Auto login is a feature that allows you to login automatically!


Few of available Auto Login types in Liferay 7 \ Liferay DXP :

  • Basic Authentication
  • Request Header
  • Parameter
  • Remember Me


Basic Authentication Header Auto Login
    To know about basic authentication, please follow this link.

Basic authentication[username + password] can be passed in headers for auto login.
Create a config file named  "com.liferay.portal.security.auto.login.basic.auth.header.module.configuration.BasicAuthHeaderAutoLoginConfiguration.cfg".


enabled=true


To deploy, copy corresponding [.cfg] \ config file to osgi/config or osgi/modules folder.
You can test it through postman like utilities!

Request Header Auto Login

    It requires screen name in request header, header key name would be - "LIFERAY_SCREEN_NAME"
Config file >  "com.liferay.portal.security.auto.login.request.header.module.configuration.RequestHeaderAutoLoginConfiguration.cfg".

We need to make host entries of valid IP's.

enabled=true
authHostsAllowed=127.0.0.1,SERVER_IP
You can also enable ldap import feature from this file!


Test it in similar manner as Basic Authentucation Auto Login.

Request Parameter Auto Login

    For parameter auto login, append these parameters in URL
"?parameterAutoLoginLogin=test@liferay.com&parameterAutoLoginPassword=test"

If you use
"/c/portal/login?parameterAutoLoginLogin=test@liferay.com&parameterAutoLoginPassword=test "
 then it will redirect you to default landing page as well.

Config file >  "com.liferay.portal.security.auto.login.request.parameter.module.configuration.RequestParameterAutoLoginConfiguration.cfg".

enabled=true


To create custom AutoLogin : https://dev.liferay.com/develop/tutorials/-/knowledge_base/7-0/auto-login#creating-an-auto-login-component

You can also enable\disable\update auto login settings from control panel > Configuration > System Settings > Foundation > [search login]




Note: If you’re running Liferay DE 7.0 Fix Pack 7 or earlier, or Liferay CE Portal 7.0 GA3 or earlier, System Settings exports a .cfg file instead of a .config file. The file format was changed to fix a bug with multi-value settings, which the .cfg format didn’t handle properly. See the bug report for more information. Any Liferay Portal system newer than those listed in this note uses the .config file format.

You are just done, Try & Enjoy the function.............:)

Monday 17 April 2017

Spring portlet handler interceptor or spring MVC interceptor Liferay


Today we will be learning about portlet handler interceptor or simply you can say MVC interceptor with spring.


To create an osgi spring portlet please look at the spring osgi portlet.

In this example, we will create an annotation and use that annotation in our portlet class and then by reflection we will call that method in our interceptor.We can do it without all of it, but as we are doing example, let's learn something more out of it.
It has nothing to do with Liferay, but as we are using Liferay environment to run portlet, it's spring osgi liferay portlet.

You can find this source code here - https://github.com/bardiavipin/osgi-spring-interceptor

By Creating this interceptor you create one more layer between View and Controller. You can use filters as well for the same tasks you want to perform with interceptors. Interceptors are more coupled with request/response objects. With Spring interceptor, you can execute before and after the phase executes.

Interceptors are executed in below cases


  • Pre
  • Post
  • After

Methods are available for each of the phase of portlet


  • Render
  • Action
  • Event
  • Resource


I have created two modules for this example, one contains portlet and another contains interceptor and annotation class. Reason behind creating two modules is to demonstrate bundle development where you can interact between these.

Bundle 1

Portlet Interceptor - MyPortletInterceptor

public class MyPortletInterceptor extends HandlerInterceptorAdapter {

    public Gson gson;    

    @Override    public void postHandleRender(RenderRequest request, RenderResponse response, Object portletController, ModelAndView modelAndView) throws Exception {
        Class<?> clazz = portletController.getClass();

        Method[] methods = ReflectionUtils.getUniqueDeclaredMethods(clazz);
        for (Method method : methods) {
            if (method.isAnnotationPresent(LoadJson.class)) {
                Object result = method.invoke(portletController);
                modelAndView.addObject("LoadJson", gson.toJson(result));
                break;
            }
        }
        System.out.println("Your custom spring portlet interceptor called!");
}


I removed Gson setter/getter to make it short.

About MyPortletInterceptor
  • It extends HandlerInterceptorAdapter
  • Override one method postHandleRender
  • Invoke method from annotation
  • Add value to ModelAndView "LoadJson" after invoking the method
  • Even if you don't have annotation in your class, It will print a simple line output

Annotation - LoadJson
You can read about custom annotations from - https://www.javatpoint.com/custom-annotation

We just created a basic annotation with target as method.

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LoadJson {
}

Bundle 2

Controller - PortletViewController

public class PortletViewController {

   @LoadJson   public Map loadJson(){
      Map map = new HashMap<String, String>();

      map.put("name", "vipin bardia");
      return map;
   }

What happens when we add it to our controller

  • After adding dependency of Bundle 1 to our portlet, we can use this LoadJson annotation
  • We wrote a method loadJson and returned a map
  • This method will be called from our interceptor and output will be added to "LoadJson" attribute as interceptor have access to it

View - view.jsp


Json Data : <c:out escapeXml="true" value="${LoadJson}" />

Let's make the main entry which connects this interceptor to our portlet.

spring-portlet.xml - osgi-spring-portlet.xml

<bean name="gsonbean" class="com.google.gson.Gson" />

<bean name="portletHandlerInterceptor" class="com.osgi.spring.interceptor.MyPortletInterceptor">
    <property name="gson" ref="gsonbean" />
</bean>

<!-- Handler mappings for annotation based controllers --><bean class="org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <property name="interceptors">
        <list>
            <ref bean="portletHandlerInterceptor"/>
        </list>
    </property>
</bean>

About spring portlet xml

  • We defined gson bean
  • We defined portletHandlerInterceptor and provided property as gson bean
  • In default annotation handler we added the interceptor


I tried to make it work with bean creation inside interceptor module only and use it inside portlet module, but was unable to. If you know this way please share it here.


Advantage out of this interceptor is you can call this interceptor in any of your portlet by just making an entry in your spring portlet xml.
If you don't want it in one of your portlet, simply remove it.


Note : You need to deploy gson bundle as well to access it!


You are just done, Try & Enjoy the function.............:)

Friday 7 April 2017

Spring OSGI portlet or bundle with Liferay 7


When you see the blog title, The question arises, is what's new in this post?


Everyone knows how to create Spring MVC Portlets! 
But do we know how to create a portlet which is OSGI enabled and can run parallel with other osgi bundles inside Liferay.

If you have been through these links and many more -

https://dev.liferay.com/develop/tutorials/-/knowledge_base/7-0/spring-mvc
https://github.com/vernaillen/liferay7-springmvc-portlet

You must have noticed it's the same old way which includes all required jars inside the application's lib because we do not have support of global library now.
Liferay 7 is OSGI way now and by OSGI convention you do not include library into each other, but you share them from same place :)

Basic diagram of OSGI, copied from  -

https://fredhsu.wordpress.com/2013/05/03/opendaylight-and-osgi-basics/


To make it work for spring, we need to do two things

  • We will be using Apache Service Mix Spring Bundles(OSGI bundles for spring)
  • Support of few maven plugins, major one is Maven Bundle Plugin to add require entries to the manifest file and prepare it like a bundle but in a war type.

You can find the source code here - https://github.com/bardiavipin/osgi-spring
Meanwhile, you are checking out source code, I will explain minimal changes in your spring portlets to make them bundles.

Create a basic Spring portlet from your IDE or from command prompt or manually with Maven.

Copy below dependencies to deploy folder

Apache ServiceMix :: Bundles :: spring-beans (4.3.1.RELEASE_1)
Apache ServiceMix :: Bundles :: spring-web (4.3.1.RELEASE_1)
Apache ServiceMix :: Bundles :: spring-core (4.3.1.RELEASE_1)
Apache ServiceMix :: Bundles :: spring-aop (4.3.1.RELEASE_1)
Apache ServiceMix :: Bundles :: spring-expression (4.3.1.RELEASE_1)
com.github.ben-manes.caffeine (2.3.2)
Apache ServiceMix :: Bundles :: spring-context-support (4.3.1.RELEASE_1)
Apache ServiceMix :: Bundles :: spring-context (4.3.1.RELEASE_1)
Apache ServiceMix :: Bundles :: spring-webmvc (4.3.1.RELEASE_1)
Apache ServiceMix :: Bundles :: commons-configuration (1.9.0.2)
Apache ServiceMix :: Bundles :: spring-webmvc-portlet (4.3.1.RELEASE_1)

Note:Caffeine is a required library for one for the spring modules.

You can check the status of bundles from Gogo Shell

Now we will be going through major changes inside Portlet files-


Context file for sample portlet is - spring portlet xml

<!--Not Working--><!--<context:component-scan base-package="com.osgi.spring" />-->

<bean class="com.osgi.spring.PortletViewController"/> <!-- Handler mappings for annotation based controllers --><bean class="org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping"/> <bean class="org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>


As you can see in the context file, component scan is commented as it is not working for this example. I used the workaround to create simple bean and pass the controller class. Spring players can tell me what is the issue here :)

BND Dependency Fix - BNDDependencyFix

@SuppressWarnings("unused")
public class BNDDependencyFix {

    static {
        System.out.println(ModelAndViewResolver.class);


When you deploy the portlet without this class, you get errors like this -
java.lang.ClassNotFoundException: org.springframework.context.config.ContextNamespaceHandler cannot be found.

In this class we are initializing or loading the necessary classes from static method.
If you find more errors with same type, you can add your class name here.

Major Maven Plugins - pom.xml

  • War plugin to exclude all jars from final war
  • Bundle plugin make necessary changes in Manifest file to create it a bundle
  • Shade plugin to make single files from spring.schemas and spring.handlers and transform  them to a single file independently
  • Truezip plugin to move spring.handlers and  spring.schemas  to classes/META-INF

When you are done with building and deploying this portlet, you can see your portlet is interacting to another Liferay bundle - portal-kernel to fetch release information. Each of the library is deployed as OSGI only and shared between applications.

You can include jar as well inside apps lib with little change in configuration, but until and unless it is necessary don't break the architecture :)

You can use Maven Bundle plugin for any of your independent project and convert it to a osgi bundle.


You are just done, Try & Enjoy the function.............:)

Wednesday 5 April 2017

Docker setup for Liferay 7 with SMTP, document library and more as Part II

In my previous blog we talked about setting up docker with Liferay and MySQL!


In this blog we will integrate SMTP server inside docker and setup mount for document_library.
We will go through content of Dockerfile which we cloned from repo and made necessary changes.

Few terms we need to understand before jumping to the setup

Dockerfile : Docker can build images automatically by reading the instructions from a Dockerfile. A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Using docker build users can create an automated build that executes several command-line instructions in succession.


Volume : data volume is a specially-designated directory within one or more containers that bypasses the Union File System.

Mount : A mount point is a directory in a file system where additional information is logically connected  from a storage location outside the operating system’s root drive and partition.

Cool, Let's move forward and have a look on docker-compose.yml


version: '2'
services:
  portal:
    build: .
    ports:
     - "8080:8080"
     - "11311:11311"
    environment:
      - MYSQL_ROOT_PASSWORD=my-secret-pw
      - MYSQL_DATABASE=lportal
      - MAIL_SERVER=mail-server
      - MAIL_SERVER_SMTP_PORT=25
      - MAIL_SERVER_USER_FROM=admin@mail.vipin.org
      - MAIL_SERVER_USER_FROM_PASSWORD=admin
      - MAIL_SERVER_AUTH=true
    volumes:
    - "./data/deploy:/usr/local/liferay-ce-portal-7.0-ga3/deploy"
    - "./data/document_library:/usr/local/liferay-ce-portal-7.0-ga3/data/document_library"
    depends_on:
     - mysql
     - mail-server
  mail-server:
    image: million12/citadel
    ports:
      - "25:25"
      - "110:110"
      - "143:143"
      - "465:465"
      - "587:587"
      - "993:993"
      - "995:995"
      - "9090:8080"
      - "10022:22"
    environment:
      - ROOT_PASS=root
      - PASSWORD=admin
      - DOMAIN=vipin.org
    hostname: mail.vipin.org
  mysql:
    image: mdelapenya/mysql-utf8
    ports:
     - "3306:3306"
    environment:
      - MYSQL_ROOT_PASSWORD=my-secret-pw
      - MYSQL_DATABASE=lportal
      - character-set-server=utf8mb4
      - collation-server=utf8mb4_unicode_ci

This is the same file which we used earlier with addition of mail server setup and mounting document library.

SMTP

We are using million12/citadel repository for SMTP server. There are so may smtp server available in docker hub but this is a easy one with web ui and authentication support.

As you can see in yml file we have added one more dependency service mail-server.  We have exposed ports for smtp, ssl, web ui etc. hostname is your mail extension here.
So you create a user inside smtp server with web support which is available on port 9090, e.g. 192.168.99.100:9090

You can login as admin/admin for administrator account or create user from outside with Register New User link, e.g. vipin.bardia@mail.vipin.org.
Use the same user and create that account in Liferay portal and check on the same mail id if you get a welcome mail like this or not.



Amazing, now you have your own SMTP server with few lines of configuration :)

Document Library

Let's talk about Volume and Mount again. I've already shared there definition above so will go through the use of it in our case.

Let's say I want to move my server to somewhere else, what is required other than database?
It's my documents which is stored in data/document_library folder.

So what can I do about this?

- "./data/deploy:/usr/local/liferay-ce-portal-7.0-ga3/deploy"
- "./data/document_library:/usr/local/liferay-ce-portal-7.0-ga3/data/document_library"

As you can see here ./data/document_library  is my local folder where [.]  represents current directory and /usr/local/liferay-ce-portal-7.0-ga3/data/document_library is my document library folder which is separated by [:] this line creates a volume inside docker container and mount it our local folder.

Whenever we upload anything like this in Liferay


It will be synced to our local folder like this

In the same manner we mount deploy folder and as soon as we drop the file war/jar inside it, it is copied to volume deploy folder and starts deployment.


Dockerfile: These are instruction which is executed step by step to create a new image.


FROM mdelapenya/liferay-portal:7-ce-ga3-tomcat-hsql
MAINTAINER Manuel de la Peña <manuel.delapenya@liferay.com>

COPY ./configs/portal-ext.properties $LIFERAY_HOME/portal-ext.properties

ENTRYPOINT ["catalina.sh", "run"]


  1. Fetch image from repository
  2. Information about Maintainer
  3. Copy portal-ext to docker image at specified path
  4. $LIFERAY_HOME is environment variable
  5. Entrypoint will execute catalina


You are just done, Try & Enjoy the function.............:)