Wednesday, May 11, 2016

Customize custom portlet configuration page

Using this feature we can store/change portlet related configuration at run time. Configuration page in liferay uses preferences to store the configuration.

There are two ways using which we can customize configuration page of custom portlet:

1. By using default DefaultConfigurationAction Class.
2. By using own java Class that extend  DefaultConfigurationAction  Class.


1. By using default DefaultConfigurationAction Class.

STEPS: 

Step 1. Create one project of portlet plugin type, name in this example : CustomizedPortletConfigurationPage-portlet

Step 2. Create one portlet inside created project, portlet class name in this example: CustomizedPortletConfiguration.java

Before customizing portlet configuration we will see what is portlet configuration and from where we can find  portlet configuration link.

Follow below screens:

In above last screen we can see that in configuration currently there are two tab links one for permission and one for sharing.

After customizing configuration page we will get one more link(Setup)  in configuration section

Step 3.  Add
<configuration-action-class>com.liferay.portal.kernel.portlet.DefaultConfigurationAction </configuration-action-class> in  liferay-portlet.xml just after <icon>tag,
after adding
liferay-portlet.xml looks like below:

<?xml version="1.0"?>
<!DOCTYPE liferay-portlet-app PUBLIC "-//Liferay//DTD Portlet Application 6.2.0//EN" "http://www.liferay.com/dtd/liferay-portlet-app_6_2_0.dtd">

<liferay-portlet-app>

<portlet>
<portlet-name>customized-portlet-configuration</portlet-name>
<icon>/icon.png</icon>
<configuration-action-class>com.liferay.portal.kernel.portlet.DefaultConfigurationAction </configuration-action-class>
<header-portlet-css>/css/main.css</header-portlet-css>
<footer-portlet-javascript>
/js/main.js
</footer-portlet-javascript>
<css-class-wrapper>
customized-portlet-configuration-portlet
</css-class-wrapper>
</portlet>
<role-mapper>
<role-name>administrator</role-name>
<role-link>Administrator</role-link>
</role-mapper>
<role-mapper>
<role-name>guest</role-name>
<role-link>Guest</role-link>
</role-mapper>
<role-mapper>
<role-name>power-user</role-name>
<role-link>Power User</role-link>
</role-mapper>
<role-mapper>
<role-name>user</role-name>
<role-link>User</role-link>
</role-mapper>
</liferay-portlet-app>

Step 4. Create one jsp name: configuration.jsp inside /html/customizedportletconfiguration
and paste below code in configuration.jsp

configuration.jsp

<%@page import="com.liferay.portal.kernel.util.StringPool"%>
<%@page import="javax.portlet.PortletPreferences"%>
<%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet" %>
<%@ taglib uri="http://alloy.liferay.com/tld/aui" prefix="aui" %>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />

 
 <h2>Select Fields and type message You Want to Show</h2>
 <liferay-portlet:actionURL portletConfiguration="true" var="configurationURL">
 </liferay-portlet:actionURL>

 <aui:form action="<%= configurationURL %>" method="post" name="fm">
<aui:input name="cmd" type="hidden" value="update" />
<aui:input name="preferences--showMessage--" type="checkbox"/>
<aui:input name="preferences--message--" type="text" />
<aui:button type="submit" value="Display Message"></aui:button>

 </aui:form>

Step 5. Give entry for configuration.jsp as config-template   for configuration mode in portlet.xml like by deafult there is view-template for view mode.
<init-param>
       <name>config-template</name>
       <value>/html/customizedportletconfiguration/configuration.jsp</value>
</init-param>

portlet.xml

<?xml version="1.0"?>

<portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" version="2.0">

<portlet>
<portlet-name>customized-portlet-configuration</portlet-name>
<display-name>Customized Portlet Configuration</display-name>
<portlet-class>
com.test.CustomizedPortletConfiguration
</portlet-class>
<init-param>
<name>view-template</name>
<value>/html/customizedportletconfiguration/view.jsp</value>
</init-param>
<init-param>
       <name>config-template</name>
       <value>/html/customizedportletconfiguration/configuration.jsp</value>
        </init-param>
<expiration-cache>0</expiration-cache>
<supports>
<mime-type>text/html</mime-type>
<portlet-mode>view</portlet-mode>
</supports>
<portlet-info>
<title>Customized Portlet Configuration</title>
<short-title>Customized Portlet Configuration</short-title>
<keywords></keywords>
</portlet-info>
<security-role-ref>
<role-name>administrator</role-name>
</security-role-ref>
<security-role-ref>
<role-name>guest</role-name>
</security-role-ref>
<security-role-ref>
<role-name>power-user</role-name>
</security-role-ref>
<security-role-ref>
<role-name>user</role-name>
</security-role-ref>
</portlet>

</portlet-app>

Step 6. Paste below code in view.jsp

 <%@page import="com.liferay.portal.kernel.util.StringPool"%>
<%@page import="com.liferay.portal.kernel.util.Validator"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@page import="javax.portlet.PortletPreferences"%>
<portlet:defineObjects />

<%
   PortletPreferences prefs = renderRequest.getPreferences();

   String showMessage = (String)prefs.getValue("showMessage", "false");
   String message = (String)prefs.getValue("message",StringPool.BLANK);
%>
   <h4>For Show/Hide Message:- <br>Click on Gear->Configuration->Set Up</h4><br>
 
   <p>Configure Today's Message Based On preference</p>
   <table border="1" cellpadding="8">
      <tr>
 <c:if test='<%=showMessage.equalsIgnoreCase("true") %>'>
<th><p style="color:green;">Today's Message: <%=Validator.isNotNull(message) ? message : StringPool.BLANK%></p></th>
 </c:if>
</tr>
   </table>
 
Now deploy and place portlet on portal page.

Output:





















Monday, May 9, 2016

Captcha use and validation using session in liferay

In this blog we will see how to use of captcha and validate that captcha in liferay custom portlet.

In this example for validation we will get loaded captcha text value from session and matched with user entered captcha entered value.

STEPS:

Step1: Create one liferay plugin portlet project, name in this example: CaptchaValidation-portlet
and portlet name: Captcha.

Step 2: Copy below code in view.jsp page,

In below jsp code we have created one  form with captcha field and create one action url for form submission for validating captcha.
Before validating captcha we will load captcha image in that captcha input field so that user can provide captcha value for validation.
For loading captcha we are calling one serve resource url that will load captcha image.

view.jsp

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>

<portlet:defineObjects />
<liferay-theme:defineObjects />

<!-- Serve resource url for loading captcha -->
<portlet:resourceURL var="loadCaptchaURL"/>

<!-- action url for submitting form for doing captcha validation... -->
<portlet:actionURL  var="validateCaptchaURL" name="validateCaptcha"/>

<liferay-ui:error key="Message" message="Invalid captcha text. Please re-Enter"/>

<aui:form action="<%=validateCaptchaURL %>" method="post" name="fm">  
   <liferay-ui:captcha url="<%=loadCaptchaURL%>">
   </liferay-ui:captcha>
   <aui:button-row>
  <aui:button type="submit" value="Validate Captcha"/>
   </aui:button-row>

</aui:form>

Step 3: Paste below code in portlet controller.(java file)

Captcha.java

package com.test;

import com.liferay.portal.kernel.captcha.CaptchaUtil;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.util.bridges.mvc.MVCPortlet;

import java.io.IOException;
import java.util.Enumeration;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletRequest;
import javax.portlet.PortletSession;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;

/**
 * Portlet implementation class Captcha
 */
public class Captcha extends MVCPortlet {


public void validateCaptcha(ActionRequest actionRequest,
ActionResponse actionResponse) throws IOException, PortletException {
      System.out.println("inside validateCaptcha method...");

      String userEnteredCaptcha = ParamUtil.getString(actionRequest, "captchaText");
      System.out.println("userEnteredCaptcha value:::"+userEnteredCaptcha);
      try {
          boolean captchaMatched = checkCaptcha(actionRequest, userEnteredCaptcha);
          if(!captchaMatched){
          SessionErrors.add(actionRequest, "Message");
          }else{
          System.out.println("Captcha Validated Successfully");
          }
         } catch (Exception e) {
  e.printStackTrace();
}
}

/*code for matching userEntered captcha text with loaded captcha text, if matched return true else false*/
private boolean checkCaptcha(PortletRequest request,String enteredCaptchaText) throws Exception {
boolean captchaMatched = false;
PortletSession session = request.getPortletSession();
    String captchaTextFromSession = getCaptchaValueFromSession(session);
   
       if (Validator.isNotNull(captchaTextFromSession)) {
           if(enteredCaptchaText.equals(captchaTextFromSession)){
           captchaMatched = true;
       }
       }
      return captchaMatched;
 }

      /*code for getting loaded captcha text from session*/
 private String getCaptchaValueFromSession(PortletSession session) {
       Enumeration<String> atNames = session.getAttributeNames();
       while (atNames.hasMoreElements()) {
           String name = atNames.nextElement();
           if (name.contains("CAPTCHA_TEXT")) {
               String captchaValueFromSession = (String) session.getAttribute(name);
            return captchaValueFromSession;
           }
       }
       return null;
 }

/*Code inside serve resource method for loading captcha...*/
@Override
public void serveResource(ResourceRequest resourceRequest,
ResourceResponse resourceResponse) throws IOException,
PortletException {
try {
           CaptchaUtil.serveImage(resourceRequest, resourceResponse);
           System.out.println("captcha loaded...");
       } catch (Exception e) {
           System.out.println(e.getMessage());
       }
// TODO Auto-generated method stub
super.serveResource(resourceRequest, resourceResponse);
}

}



Output:
Captcha loaded






 Entered correct captcha value

Captcha use and validation using liferay API in liferay

In this blog we will see how to use of captcha and validate that captcha in liferay custom portlet.
Liferay provides necessary API as well as tag library to implement CAPTCHA.

In this example we will validate  Captcha  using liferay API.
(CaptchaUtil.check(actionRequest);)

STEPS:

Step1: Create one liferay plugin portlet project, name in this example: CaptchaValidation-portlet
and portlet name: Captcha.

Step 2: Copy below code in view.jsp page,

In below jsp code we have created one  form with captcha field and create one action url for form submission for validating captcha.
Before validating captcha we will load captcha image in that captcha input field so that user can provide captcha value for validation.
For loading captcha we are calling one serve resource url that will load captcha image.

view.jsp

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>

<portlet:defineObjects />
<liferay-theme:defineObjects />

<!-- Serve resource url for loading captcha -->
<portlet:resourceURL var="loadCaptchaURL"/>

<!-- action url for submitting form for doing captcha validation... -->
<portlet:actionURL  var="validateCaptchaURL" name="validateCaptcha"/>

<liferay-ui:error key="errorMessage" message="Enter correct data as shown in the image"/>

<aui:form action="<%=validateCaptchaURL %>" method="post" name="fm">
   <liferay-ui:captcha url="<%=loadCaptchaURL%>" />
   <aui:button-row>
  <aui:button type="submit"/>
   </aui:button-row>

</aui:form>

Step 3: Paste below code in portlet controller.(java file)

Captcha.java

package com.test;

import com.liferay.portal.kernel.captcha.CaptchaException;
import com.liferay.portal.kernel.captcha.CaptchaUtil;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.util.bridges.mvc.MVCPortlet;

import java.io.IOException;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;

/**
 * Portlet implementation class Captcha
 */
public class Captcha extends MVCPortlet {


public void validateCaptcha(ActionRequest actionRequest,
ActionResponse actionResponse) throws IOException, PortletException {
      System.out.println("inside validateCaptcha method...");
try {
            CaptchaUtil.check(actionRequest);
            System.out.println("CAPTCHA validated successfully");
            } catch (CaptchaException e) {
            SessionErrors.add(actionRequest, "errorMessage");
         }
}

@Override
public void serveResource(ResourceRequest resourceRequest,
ResourceResponse resourceResponse) throws IOException,
PortletException {
try {
           CaptchaUtil.serveImage(resourceRequest, resourceResponse);
           System.out.println("captcha loaded...");
       } catch (Exception e) {
           System.out.println(e.getMessage());
       }
// TODO Auto-generated method stub
super.serveResource(resourceRequest, resourceResponse);
}

}


Output:
Captcha loaded




 Entered correct captcha value