13 June, 2017

Captcha & reCaptcha with Liferay 7

If you are planning to integrate captcha in you liferay custom portlet then use below stuff


view.jsp:
<%@taglib uri="http://liferay.com/tld/captcha" prefix="liferay-captcha" %>
<portlet:resourceURL id="/login/captcha" var="captchaURL"/>
<liferay-captcha:captcha url="<%=captchaURL%>" />


Write CaptchaMVCResourceCommand.java :

@Component(
    property = {
        "javax.portlet.name=test_portlet",
        "mvc.command.name=/login/captcha"
    },
    service = MVCResourceCommand.class
)
public class CaptchaMVCResourceCommand extends BaseMVCResourceCommand {

    @Override
    public boolean serveResource(
        ResourceRequest resourceRequest, ResourceResponse resourceResponse) {

        try {
            CaptchaUtil.serveImage(resourceRequest, resourceResponse);
            return false;
        }
        catch (Exception e) {
            _log.error(e, e);

            return true;
        }
    }

And you are done...

Make sure you use <liferay-captcha:captcha url="<%=captchaURL%>" />  instead of <liferay-ui:captcha url="<%=captchaURL%>" /> as its not support in custom module somehow.

23 November, 2016

Spring MVC Portlet Migration - DXP

Friends,

Do you want to migrate your Spring MVC portlet into DXP ? yooo here are the steps you will have to follow to get it migrate.

Before we jump into steps, let me light up regarding spring mvc. Actually Spring MVC is purely used for web project architect and there is no any specific intention to get it convert into OSGi module jar. So here we may not be able to convert it into module jar but  such kind of portlets will get deployed as a war file only and Liferay container will convert into OSGi compatible WAB (Web Application Bundle) project by  Liferay WAB Generator.

Follow below steps to get it convert


22 November, 2016

Custom portlet configuration - DXP

Hello Folks,

Here I have tried to put all the possible steps which we would required to introduce custom portlet configuration.


1) Create new Liferay module using mvcportlet template
2) Define below highlighted property in your controller class:
@Component(
        immediate = true,
        property = {
                "javax.portlet.name=SamplePortlet",
                "javax.portlet.supports.mime-type=text/html",
                "javax.portlet.display-name=SamplePortlet",
                "javax.portlet.init-param.template-path=/",
                "javax.portlet.init-param.view-template=/html/view.jsp",
                "javax.portlet.init-param.config-template=/html/config.jsp",

                "javax.portlet.portlet-info.title=SamplePortlet",
                "javax.portlet.portlet-info.short-title=SamplePortlet",
                "javax.portlet.portlet-info.keywords=SamplePortlet",
                "javax.portlet.resource-bundle=content.Language",
                "javax.portlet.security-role-ref=administrator,guest,power-user,user",
                "com.liferay.portlet.display-category=SamplePortlet",
                "com.liferay.portlet.instanceable=false",
                "com.liferay.portlet.header-portlet-css=/css/main.css",
                "com.liferay.portlet.footer-portlet-javascript=/js/main.js"
        },
        service = Portlet.class
    )
public class SamplePortletextends MVCPortlet {
 
}

3) Introduce custom interface for models with unique id as highlighted

@Meta.OCD(
            id = "com.sample.action.SamplePortletConfig "        )
        public interface SamplePortletConfig {
 @Meta.AD(
        deflt = "blue",
        required = false
    )
    public String favoriteColor();

    @Meta.AD(
       deflt = "red|green|blue",
       required = false
    )
    public String[] validLanguages();

    @Meta.AD(required = false)
    public int itemsPerPage();

}
Note :
  1. Meta.OCD: Registers this class as a configuration with a specific id. You can choose any string you want, but make sure it’s unique. A common pattern is to use the fully qualified class name.
  2. Meta.AD: Specifies the default value of a configuration field as well as whether it’s required or not. Note that if you set a field as required and don’t specify a default value, the system administrator must specify a value in order for your application to work properly. Use the deflt property to specify a default value.

4) Create custom config action class

@Component(
        configurationPid = "com.sample.action.SamplePortletConfig",
        configurationPolicy = ConfigurationPolicy.OPTIONAL, immediate = true,
        property = {
                "javax.portlet.name=SamplePortlet"
        },
        service = ConfigurationAction.class
    )
public class SamplePortletConfigAction extends DefaultConfigurationAction {

// processaction,render and include methods
}

5) Add below property in module bnd file
-metatype: *
  
 6) Introduce config.jsp with all possible fields which you want to configure

 Now check your configuration icon and see if custom configuration is available or not for that specific portlet.


Interesting right !!!!!!!  Enjoy Learning Osgi :)

Overriding classes and properties using OSGi - DXP

Hello Friends,

Here i have published all possible ways to override Liferay classes and properties for which we were using hook in older Liferay version.

 

1) Create Custom portlet data handler

 @Component(
        property = {
            "javax.portlet.name=com.sample.portlet"
        },
        service = PortletDataHandler.class
    )
public class SamplePortletDataHandler extends BasePortletDataHandler
{
// Override methods which you required to override
}

2) Create Custom Model Listner

 @Component(
        immediate = true,
        service = ModelListener.class
    )
public class LayoutModelListener extends BaseModelListener<Layout>
{
 // Override methods which you required
}

3) Create Custom workflow handler

 @Component(
        property = {"model.class.name=com.sample.model.SampleEntity"},
        service = WorkflowHandler.class
    )
public abstract class SampleWorkflowHandler extends BaseWorkflowHandler
{
 // Override methods which you required
}

4) Custom Action

@Component(
        property = {
            "javax.portlet.name=" + PollsPortletKeys.POLLS,
            "javax.portlet.name=" + PollsPortletKeys.POLLS_DISPLAY,
            "mvc.command.name=/polls/edit_question"
        },
        service = MVCActionCommand.class
    )
public class CustomMVCActionCommand  extends BaseMVCActionCommand
{
 // Override methods which you required
}

5) Custom Render

@Component(
        property = {
            "javax.portlet.name=" + PollsPortletKeys.POLLS_DISPLAY,
            "mvc.command.name=/polls_display/view"
        },
        service = MVCRenderCommand.class
)
public class CustomActionMVCRenderCommand implements MVCRenderCommand
{
 // Override methods which you required
}

6) Override Liferay Language Properties

@Component(
        property = { "language.id=en_US" },
        service = ResourceBundle.class
    )
public class ENResourceBundleComponent  extends ResourceBundle {

    @Override
    protected Object handleGetObject(String key) {
        return _resourceBundle.getObject(key);
    }

    @Override
    public Enumeration<String> getKeys() {
        return _resourceBundle.getKeys();
    }

    private final ResourceBundle _resourceBundle = ResourceBundle.getBundle(
        "content.Language", UTF8Control.INSTANCE);
}

7) Override Liferay Module Language Properties

@Component(
    immediate = true,
    property = {
        "bundle.symbolic.name=com.liferay.polls.web",
        "resource.bundle.base.name=content.Language",
        "servlet.context.name=polls-web"
    }
)
public class ResourceBundleLoaderComponent  implements ResourceBundleLoader {

    @Override
    public ResourceBundle loadResourceBundle(String languageId) {
        return _resourceBundleLoader.loadResourceBundle(languageId);
    }

    @Reference(target = "(bundle.symbolic.name=com.liferay.polls.web)")
    public void setResourceBundleLoader(
        ResourceBundleLoader resourceBundleLoader) {

        _resourceBundleLoader = new AggregateResourceBundleLoader(
            new CacheResourceBundleLoader(
                new ClassResourceBundleLoader(
                    "content.Language",
                    ResourceBundleLoaderComponent.class.getClassLoader())),
            resourceBundleLoader);
    }

    private AggregateResourceBundleLoader _resourceBundleLoader;

}

8) Override Liferay services

@Component(service = ServiceWrapper.class)
public class CustomUserLocalService extends UserLocalServiceWrapper {
// Override methods which you require   
}


Note:
Portal-ext.prioperties cannot be override through OSGi and you will have to manually update portal-ext.properties which we are keeping inside WEB-INF/classes




Liferay MVC Portlet with Service Builder - DXP

Friends,

Here you will see how you can create Liferay MVC portlet with best practice.

If you remember, we were creating portlets in 6.X by selecting specific portlet type from IDE and choosing weather we have to use service builder or not. So there it was creating only one single portlet including service layer.

Now in DXP, you will have to separately create service layer and web portlet layer .
Now here we will have to keep 2 things in our mind.
1) Portlet specific service layer
2) Generic service layer (Which can be used across all the modules)

Identically both are same and the way of accessing the service also would be the same only difference is, if service is portlet specific then you can keep service and web layer altogether in single module project which will help for CI and build whole project.

1) Now here first step would be create new Liferay module by selecting servicebuilder as template and create.

You can also create this portlet using blade-cli tool as well.
You can use below command to create if you have install blade-cli in your local.

11 November, 2016

Issues During 6.X to DXP migration

There are couple of common problem which we might facing during migration or upgration from 6.X to Liferay DXP.

We will discuss all possible items over here.

27 September, 2016

Overriding LPKG Files : Liferay DXP

Liferay provides a way to update modules without modifying the original LPKG file they're packaged in. You can do this by overriding the LPKG file.
To override a JAR or WAR from any LPKG other than the Static LPKG, 

1) first create a folder named override in the Liferay instance's osgi/marketplace folder. 2) Drop the updated JAR into this folder, making sure its name is the same as the JAR in the original LPKG, minus the version info. For example, if you want to override the com.liferay.amazon.rankings.web-1.0.5.jar from the Liferay CE Amazon Rankings.lpkg, you would insert a JAR named com.liferay.amazon.rankings.web.jar into the osgi/marketplace/override folder.
3) Overriding JARs from the Static LPKG works the same way, but the updated JARs go into the osgi/static folder instead. For example, if you want to override the com.liferay.portal.profile-1.0.3.jar, you would insert a JAR named com.liferay.portal.profile.jarinto the osgi/static folder.

Note: To undo these changes, delete the overriding JAR; portal will use the original LPKG JAR on its next startup. Note that adding and removing JARs must be done while the portal is shut down. Changes take effect the next time the portal is started.

Source : https://github.com/liferay/liferay-portal/blob/master/tools/osgi-marketplace-override-README.markdown 

I am sure this would come across someday when you would like to override LPKG files for small/large scale changes.

22 September, 2016

6.X Portlet to DXP Module Convesion

Follow below steps to convert Liferay MVC Portlet to Module:

1)  First Step is, you will have to download plugin-sdk for DXP/Liferay 7 because Liferay has provided one tool which gives you facility to show breaking API changes in your portlet.


2) Once you run this tool, it will give you all the breaking Liferay API (Not Custom code) report for Java and JSP files.
 Note: Sometimes it doesn't work to generate report. So restart your IDE and try. it should start generating report after restart.

3) Now you can modify these files according to report and can check in plugin SDK it self weather this portlet is deploy-able or not.

4) Once you are done with this activity, you will have to convert your portlet into OSGi module.

5) Create Liferay MVC Portlet from Liferay developer studio (For DXP).
Note: If your portlet have service builder code then you will have to create module by selecting service-builder template.


 6) Now what you need to do is, you will have to copy manually your java classes into generated bundle/module.




 All converted Java classes will go to java folder and all jsp/jspf files will go to resource folder under META-INF.
If you have any Language property then it will be inside content folder.

7) Once you copy your code into generated bundle, you will have to check if any dependency it required and showing any import packages error. If it is then you will have to manually define dependency inside build.gradle file.
like I have added log4j in below snippet,
dependencies {
    compileOnly group: "com.liferay.portal", name: "com.liferay.portal.kernel", version: "2.6.0"
    compileOnly group: "com.liferay.portal", name: "com.liferay.util.taglib", version: "2.0.0"
    compileOnly group: "javax.portlet", name: "portlet-api", version: "2.0"
    compileOnly group: "javax.servlet", name: "servlet-api", version: "2.5"
    compileOnly group: "jstl", name: "jstl", version: "1.2"
    compileOnly group: "org.osgi", name: "org.osgi.compendium", version: "5.0.0"
    compileOnly group: "log4j", name: "log4j", version: "2.1.17"
}

8) You also will have to make sure that, if you have used any specific imports in JSP file then you need to modify that as well. Breaking API changes tool will give you this report if its liferay specific but if you have imported some custom code then need to check manually.

9) Once you able to compile your code successfully, just deploy that converted module by running gradle deploy task.

10) Now the most important thing is, though you are able to compile module, its not guarantee that your portlet will be deployed into container successfully and available to use.
 Note:You will have to check OSGi log from gogo shell and also will have to look into tomcat log if its throwing any specific import packages error or other errors.It should be in active stage.

11) If its deploy into container without any error then, you are done with the migration.

There are couple of items or checkpoints which you will be facing during module compilation or deployment.
I will try to collect all such kind of points ans solutions and will publish into separate post.

You can post your comment here for any such kind issue which we can discuss.

Stay tune with LiferaySolution.

DB Upgrade 6.X to DXP

Image result for Liferay upgradeAs you know Liferay is moved to DXP version now and all existing Liferay customer will migrate or upgrade to newer version of DXP.


Here in these series of  blogs, you will see all step by step guide which will help you to follow all the necessary steps for the migration to DXP.

As a part of migration/up-gradation, Database comes first everywhere which you must require to migrate to make it compatible with new version.

Here we will only talk about migration from 6.X to DXP as we will not consider the older version like 5.X.


20 September, 2016

Liferay DXP Setup



Image result for Liferay DXP
Follow below steps to setup local Liferay DXP :

Below Tools and software are required to setup Liferay DXP in local machine  :
A.    JDK-8
B.    JPM
C.    Blade
D.    Liferay DXP (Tomcat bundle)
E.    Liferay Plugin-SDK


16 August, 2016

Know Module And Component

The main reason for LIFERAY to move into module framework is,

  • Creating small modules (Tightly coupled)
  • Publish and consume other modules features (Inflexible)
  • Working without static dependencies (Dynamic dependencies)
To use module framework,
You mainly need to understand below 3 concepts :
  1. Modules
  2. Components
  3. Application Lifecycle 






Module:

Module is something which is group of Java classes, other optional resources, and a MANIFEST.MF file, packaged together as into a  single JAR file. Liferay 7 uses three modules/bundles.
Modules can have multiple components.

Component:

Component is nothing but implementation of interface like the way we were doing with *LocalServiceImpl class in older fashion. So here what you need to do is, you just will have to load components through annotations in implementation class.These loaded/declared components would be part of modules.

Here Lifecycle of modules and component will be handled through Liferay's OSGi based module framework.

Popular Posts

Featured Post

Liferay 7.3 compatibility matrix

Compatibility Matrix Liferay's general policy is to test Liferay Portal CE against newer major releases of operating systems, open s...