Monday, 4 June 2018

Delete files with long file/folder name

mkdir empty_dir
robocopy empty_dir the_dir_to_delete /s /mir
rmdir empty_dir
rmdir the_dir_to_delete

Thursday, 8 January 2015

Liferay Spring MVC portlet Architecture


Monday, 8 December 2014

Maven portlet development - Liferay service builder portlet


1. Navigate to the folder where you want to create Liferay plugin
2. Execute the command mvn archetype:generate. This will take some time for the first time. You might need to rerun this command for twice to get it complete.
3. It will ask to choose a number or apply filter. Enter filter for Liferay, simply type liferay and hit enter.
4. Select archetype for buil-service portlet. I.e. liferay-servicebuilder-archetype
5. Enter liferay portlet archetype version. E. g. Version of Liferay, for our case we need to choose 6.2.0-RC5.
6. Define value for property 'groupId': :org.wellOfJava.portlet. i. e. groupId will identify your project uniquely across all projects.
7. Define value for property 'artifactId': : sample-portlet. i. e. Name of the portlet. 
8. Define value for property 'version':  1.0-SNAPSHOT: : 1.0-SNAPSHOT  (or here you can just press enter)
9. Define value for property 'package':  org.wellOfJava.portlet: : org.wellOfJava.portlet. That will be package structure of your portlet.
10. Add properties tag in pom.xml after </dependencies> tag and before </project> end tag
11. Now if you open the directory you would find 2 directory created inside and one POM.xml file. This POM is parent file for both of the sab modules.
a) {portlet-name}-portlet
i. This directory contains all necessary files for Liferay portlet.
b) {portlet-name}-portlet-service
i. This directory is specifically for developing service builder classes. 
12. Now the portlet is ready to add service builder layer. You would find service.xml file in {portlet-name}-portlet\src\main\webapp\WEB-INF. Modify the file as per your requirement. 
13. Now execute following command on the parent POM.
a) mvn liferay:build-service
b) mvn clean install
14. Import the parent POM file in eclipse and both of the sub module will be imported to eclipse. As well portlet project will also have dependency for service builder jar file. 

The portlet is ready for further development. You can carry on as you develop portlet in liferay-sdk.

Monday, 8 September 2014

Spring Web Service Client Using Maven with Liferay Portlet


  • Create one simple maven base project in Eclipse.
  • Save your wsdl file in the resource directory with WSDL extension. Make sure about the extension
  • Copy following build and dependencies tag. 
<build>

    <plugins>

        <plugin>

            <groupId>org.apache.cxf</groupId>

            <artifactId>cxf-codegen-plugin</artifactId>

            <version>${cxf.version}</version>

            <executions>

                <execution>

                    <id>generate-sources</id>

                    <phase>generate-sources</phase>

                    <configuration>

                    <sourceRoot>src/main/java</sourceRoot>

                    <wsdlOptions>

                        <wsdlOption>

                            <wsdl>${basedir}/src/main/resources/TestService.wsdl</wsdl>

                        </wsdlOption>

                    </wsdlOptions>

                    </configuration>

                    <goals>

                        <goal>wsdl2java</goal>

                    </goals>

                </execution>

            </executions>

            <dependencies>

                <dependency>

                    <groupId>xerces</groupId>

                    <artifactId>xercesImpl</artifactId>

                    <version>2.9.1</version>

                </dependency>

            </dependencies>

        </plugin>

    </plugins>

</build>

    <dependencies>

        <dependency>

            <groupId>org.apache.cxf</groupId>

            <artifactId>cxf-tools-common</artifactId>

            <version>${cxf.version}</version>

        </dependency>

        <dependency>

            <groupId>org.apache.cxf</groupId>

            <artifactId>cxf-rt-frontend-simple</artifactId>

            <version>${cxf.version}</version>

        </dependency>

        <dependency>

            <groupId>org.apache.cxf</groupId>

            <artifactId>cxf-rt-transports-http</artifactId>

            <version>${cxf.version}</version>

        </dependency>

    </dependencies>

    <properties>

        <cxf.version>2.7.7</cxf.version>

    </properties>
  • build maven project “mvn clean install -DskipTests
  • Use service class to get ServiceImplPort class. Then call your method. E.g. testService.getTestServiceImplPort().getTest();
  • In your portlet pom file. Add dependency for your maven project your have created in first step. 
  • Build your portlet with maven. Jar file for the services will be copied in the lib of portlet war file.
To Read more about CXF, Please visit http://cxf.apache.org/docs/using-cxf-with-maven.html

Tuesday, 2 September 2014

Spring MVC portlet using maven

Create Maven base Liferay portlet
Please following the below link to create a Liferay portlet using maven command prompt.
http://wellofjava.blogspot.com/2014/08/maven-portlet-development-for-command.html

Define Maven dependencies for Sping MVC portlet
Now you will need to add following dependencies in Maven file to make spring portlet.


<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc-portlet</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${spring.version}</version>
</dependency>

Add spring.version as property. You can choose version as per your use.
               
<properties>
    <spring.version>3.1.0.RELEASE</spring.version>
</properties>


Define Portlet for Spring mvc
In portlet.xml file, you need to define portlet class which will act as controller for portlet. When you create portlet in Liferay using SDK or using maven, You will find following line in portlet.xml.

<portlet-class>com.liferay.util.bridges.mvc.MVCPortlet</portlet-class>

You need to change this portlet class to use spring mvc framework.


<portlet-class>org.springframework.web.portlet.DispatcherPortlet</portlet-class>

When a DispatcherServlet or DispatcherPortlet is set up for use and a request comes in for that specific dispatcher, it starts processing the request. The sections below describe the complete process a request goes through when handled by such a dispatcher, from determining the application context right through to rendering the view.

Create Spring application context file
Now you need to create a spring context file for spring mvc which will contains bean definitions of controller and viewResolver. Create one xml file with name of your portlet or application-context.xml or any other name as per your organization standard.
You need to put this file under WEB-INF. Either you can put this file in your directory under WEB-INF e.g. WEB-INF/spring.
Add the following content in it.


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
</beans>

Add following lines within <beans> tag to enable spring annotations.  

<context:annotation-config />
<bean class="org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping" />


Pointing Spring application context file in Portlet configuration

Now once you have to create the context file. You need to point this context file through your portlet configuration to make it effective. Add init-param parameter to portlet definition in portlet.xml file. E.g.
 
<init-param>
    <name>contextConfigLocation</name>
    <value>/WEB-INF/spring/application-context.xml</value>
</init-param> 
   
Put viewrenderservlet entry in web.xml file.
It is necessary to load spring render servlet. Because ViewRendererServlet is a bridge servlet, mainly for the Portlet MVC support. For usage with Portlets, this Servlet is necessary to force the portlet container to convert the PortletRequest to a ServletRequest, which it has to do when including a resource via the PortletRequestDispatcher.
Add following xml snippet in web.xml

<servlet>
    <servlet-name>view-servlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.ViewRendererServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>view-servlet</servlet-name>
    <url-pattern>/WEB-INF/servlet/view</url-pattern>
</servlet-mapping>

Configuring viewResolver
Spring mvc uses view resolver to forward request to JSP. Spring supports many kind of resolvers. Following are the list of all the view resolvers.

  • AbstractCachingViewResolver, 
  • AbstractTemplateViewResolver, 
  • BeanNameViewResolver, 
  • FreeMarkerViewResolver, 
  • InternalResourceViewResolver, 
  • JasperReportsViewResolver, 
  • ResourceBundleViewResolver,
  • UrlBasedViewResolver, 
  • VelocityLayoutViewResolver, 
  • VelocityViewResolver, 
  • XmlViewResolver, 
  • XsltViewResolver
For easy to use purpose you can use InternalResourceViewResolver. This is very easy to use and it can handle JSP. Add following code snippet to add view resolver to your portlet.

<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.InternalResourceView" />
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
    <property name="order" value="1" />
</bean>

In prefix property we have to give the folder path where we kept all JSP files.
In suffix property we have to give as ".jsp".
Create a jsp file in /WEB-INF/jsp directory.

Create Portlet Controller
Now we need to implement controller to handle tasks for portlet. In controller each method is invoked by Request Parameter mapping.
We have used different annotation to implement controller.


@Controller(value = " SpringMVCTestController ")
@RequestMapping("VIEW")
public class SpringMVCTestController {
    @RenderMapping
    public String handleRenderRequest(RenderRequest request,RenderResponse response,Model model){
        return "defaultRender";
    }
}


Define controller
Now once your controller is ready, you need to define this controller as bean in spring configuration file.


<bean class="com.myowncompany.test.springmvc.controller.MyFirstSpringMVCTestController" />

Thursday, 7 August 2014

Maven Portlet Development - For Command Prompt Lovers


  • Navigate to the folder where you want to create Liferay plugin
  • Execute the command mvn archetype:generate. This will take some time for the first time. You might need to rerun this command for twice to get it complete.
  • It will ask to choose a number or apply filter. Enter filter for Liferay, simply type liferay and hit enter.
  • Select proper archetype for particular pulgin e.g for Liferay portlet pulgin select liferay-portlet-archetype
  • Enter liferay portlet archetype version. E. g. Version of Liferay, for our case we need to choose 6.2.0-RC5.
  • Define value for property 'groupId': :org.wellOfJava.portlet. i. e. groupId will identify your project uniquely across all projects.
  • Define value for property 'artifactId': : sample-portlet. i. e. Name of the portlet. 
  • Define value for property 'version':  1.0-SNAPSHOT: : 1.0-SNAPSHOT  (or here you can just press enter)
  • Define value for property 'package':  org.wellOfJava.portlet: : org.wellOfJava.portlet. That will be package structure of your portlet.
  • Add properties tag in pom.xml after </dependencies> tag and before </project> end tag
  • <properties>
    <liferay.version>6.2.0-RC5</liferay.version>
    <liferay.maven.plugin.version>6.2.0-RC5</liferay.maven.plugin.version>
    </properties>
    
  • Navigate to the directory, where pom.xml of the plugin exist. Execute the command mvn clean install –DskipTests. For building war file
  • After successful build import into eclipse

Wednesday, 30 July 2014

Simple Captcha Text Producer Implementation

This blog is in reference to the Liferay hook in Marketplace. Following is the link for the Captcha Internationalization.
Captcha Internationalization Hook

Same Concept can be used to implement multilingual Captcha for any web site.

Simple Captcha is the opensource implementation for captcha service. Please check the following URL for more details about SimpleCaptcha.
http://simplecaptcha.sourceforge.net/

Simple Captcha is providing textProducer for Captcha implementation. The textProducer class produces a string with some random Character or Numbers or Alphanumeric. Just simply add that captcha as attribute into the Session. Give an input Field in the HTML form for user to insert captcha text. Once user submits the form, compare the input field with the value stored in the session. This is how captcha will work.

Simple captcha is providing textProducer for Arabic, Chinese and Digits. Most of the time digit in captcha will work fine. In case customer asks for regional language for the captcha. You need to simply make your own implementation for the textProducer. Please refer the implementation of the ArabicTextProducer from SimpleCaptcha.
http://sourceforge.net/p/simplecaptcha/code/ci/master/tree/Java/src/nl/captcha/text/producer/ArabicTextProducer.java

You just need to implement TextProducer interface from SimpleCaptcha and provide your own implementation for your language. You can refer following code that I have developed for Gujarati text producer. 

package com.wellofjava.captcha.text.producer;

import nl.captcha.text.producer.DefaultTextProducer;
import nl.captcha.text.producer.TextProducer;

public class GujaratiTextProducer implements TextProducer {

    static final int DEFAULT_LENGTH = 5;

 private static char[] GUJARATI_CHARS = { '\u0627', '\u0A85', '\u0A86',
   '\u0A87', '\u0A88', '\u0A89', '\u0A8A', '\u0AE6', '\u0AE7',
   '\u0AE8', '\u0AE9', '\u0AEA', '\u0AEB', '\u0AEC' };
  
    private final TextProducer _txtProd;
    
    public GujaratiTextProducer() {
        this(DEFAULT_LENGTH);
    }
    
    public GujaratiTextProducer(int length) {
        _txtProd = new DefaultTextProducer(length, GUJARATI_CHARS);
    }
    
    @Override
    public String getText() {
        return _txtProd.getText();
    }
}