Sunday, November 15, 2015

How To Use Soap UI For Testing Web Service

SoapUI  Tool:

SoapUI is an open-source web service testing application for service-oriented architectures (SOA) and representational state transfers (REST). Its functionality covers web service inspection, invoking, development, simulation and mocking, functional testing, load and compliance testing

Sample wsdl : 
http://webservices.oorsprong.org/websamples.countryinfo/CountryInfoService.wso?wsdl 

STEPS:

Step 1: Open SoapUI Tool, as shown in below screen


 Step 2: Click on File and create one new soap project, as shown in below screen


Step 3: Provide project name and wsdl url, as shown in below screen.
http://webservices.oorsprong.org/websamples.countryinfo/CountryInfoService.wso?wsdl 

 Step 4: Newly created soap project will be shown in SoapUI Tool,
Step 5: Expanding created project CountryInfoService, we will get getCapitalCity method,
In getCapital city request by passing country code we will get capital of that country.
Result: Getting Delhi as capital after passing country code IN.



Web Services

Web Service Basics:
 
A web service is a way of calling a function which is inside a software from some other software. lets say there are two softwares- 1.Company management , 2.Employee management. Software one(Company management) gives a call to function which is inside software two(Employee management).The software which gives a call to a web service is called a client(Here client is software one-Company management). The software which gives a  service to the request as a response is called server(Here server is software two- Employee management). This two software(Company management and Employee management) need not to be written in same language. software one(Company management) can be written in java, software two(Employee management) can be written in dot net.  This softwares need not to be on the same machine. software one(Company management) can be on machine one , software two(Employee management) can be on machine two. But there should be a network(LAN,Internet) connecting these two softwares.
Lets say-
Company application gives a call to Employee application and says provide me (listOfEmployees()), Employee
application receives this call and response back sending (listofEmployees()).
In web service there are two things, server-(service provider/publisher/exposer) and client-(service consumer).
In above explained example Employee application is service provider/exposer and Company application is service consumer.
Web Service can be classified as:
1.    Soap Based Web Service
2.    Restful  Web  Service

Tuesday, November 10, 2015

send log messages to different log file

We often come across to situation where we need to spearate our log based on different scenario.
Here i will explain how to create another log file and use it for logging.
we can do this in two way either using xml or using properties. xml is better approach.

Using log4j.properties
cretae a log4j.properties file inside your resources folder of your project and paste below properties

# Root logger option
log4j.rootLogger=INFO, file

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender

#Redirect to Tomcat logs folder
#log4j.appender.file.File=${catalina.home}/logs/ekf.log C:\\EKF.log

log4j.appender.file.File=${catalina.home}/logs/Sample.log
log4j.appender.file.MaxFileSize=1024MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n


As we have use org.apache.log4j so we need to add the respecive dependency in our pom.xml

               <dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>

</dependency>

Now all you to do is, write below code in class which logging message you want to send to Sample.log

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

final static Log logger = LogFactory.getLog(Test.class);

As in our properties we have defined INFO as rootLogger so when you use

logger.info("test your logger");

this inside your method log message will come under your Sample.log.

the time you will deploy your portlet that time itself it will create Sample.log file inside tomcat/log folder of your application.
And if you have use final static Log logger = LogFactory.getLog(Test.class); inside your class for logging all the log message will go to Sample.log

Using log4j.xml
cretae a log4j.xml file inside your resources folder of your project and paste below xml code

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
xmlns:log4j='http://jakarta.apache.org/log4j/'>

<appender name="file" class="org.apache.log4j.RollingFileAppender">
  <param name="append" value="false" />
  <param name="maxFileSize" value="10KB" />
  <param name="maxBackupIndex" value="5" />
  <!-- For Tomcat -->
  <param name="file" value="${catalina.home}/logs/XMLBaseSample.log" />
  <layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" 
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
  </layout>
</appender>

<root>
<level value="INFO" />
<appender-ref ref="file" />
</root>

</log4j:configuration>

add below dependency in your pom.xml

        <dependency>
<groupId>log4j</groupId>
               <artifactId>log4j</artifactId>
               <version>1.2.16</version>

</dependency>


in this case it will create different file as  XMLBaseSample.log inside tomcat/log folder of your application.

in your class write below code to send logging messages to XMLBaseSample.log file

import org.apache.log4j.Logger;

private static Logger LOG = Logger.getLogger(Test.class);

and in class if  you have use this logger

private static Logger LOG = Logger.getLogger(Test.class);

and inside method if you use logger.info("test your logger using xml based approach");
 then you will see this log message inside XMLBaseSample.log

i have use different logger in both the case just to explain that we can achieve it with many ways.



AOP Arround Advice to calculate execution time of methods in your project



In real time we often come across a situation where we have to log the execution time of our methods,
It will be a hectic job to update all the method in our project to log execution of each method, Spring make it simple using its AOP Around Advice Concept.

For this we have to update our spring configuration file in general case it is Portlet-Name.xml

write below code in your Portlet-Name.xml


<aop:config proxy-target-class="true">
<aop:aspect id="myAspect" ref="log">
<aop:pointcut id="myArroundAdviceOperation" expression="execution(* com.foo.liferay.controller.Foo..*(..))" />
<aop:around pointcut-ref="myArroundAdviceOperation" method="aroundAdvice" />
</aop:aspect>
</aop:config>
<bean id="log" class="com.foo.liferay.logger.Log" />

note: attribute proxy-target-class="true" is to be provide only if we are getting exception related to proxy other wise without this attribute we can proceed.

In the above code at  <aop:pointcut we have to give our class name with its full package and the point cut id(in this example id="myArroundAdviceOperation" ) has to provide to
<aop:around pointcut-ref as shown above

we need to create a Log class, as in above code i have created inside package com.foo.liferay.logger.Log

package com.foo.liferay.logger;

import com.liferay.portal.kernel.log.LogFactoryUtil;

import org.aspectj.lang.ProceedingJoinPoint;

public class Log {

private static final com.liferay.portal.kernel.log.Log LOG = LogFactoryUtil.getLog(Log.class);

/**
     * This is a first method which is being called
     * before any method defined in controller and service layer.
     * 
     * @param proceedingJoinPoint the proceedingJoinPoint is a point where method is binding.
     * @return Object This is a type of return parameter of the method.
     * @exception Throwable During the joint point of the method.
     * 
     */

public Object aroundAdvice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
LOG.info("aroundAdvice is calling");
   long startTime = System.currentTimeMillis();
   LOG.info("START METHOD : "+proceedingJoinPoint.getSignature().getName());
   Object obj=proceedingJoinPoint.proceed();
   LOG.info("END METHOD   : "+proceedingJoinPoint.getSignature().getName());
   long timeTaken = System.currentTimeMillis() - startTime;
   LOG.info("EXECUTION TIME : " + timeTaken + " MILISECONDS.");
   LOG.info("--------------------------------------------------------------");
   return obj;
   }
}

In this above class one method called aroundAdvice is created ,The @Around has more power and provides more control for end-user to get deal  with ProceedingJoinPoint.

now we have to add some dependency to use this spring tool in our pom.xml if you have created maven project

<dependency>
                     <groupId>org.aspectj</groupId>
                     <artifactId>aspectjrt</artifactId>
                     <version>1.6.11</version>
              </dependency>
              <dependency>
                     <groupId>org.aspectj</groupId>
                     <artifactId>aspectjweaver</artifactId>
                     <version>1.6.11</version>
              </dependency>

well thats enough for checking your method execution time save all the changes and try loading your portlet when your class Foo will load then inside Foo class all method execution time you will get in your log in my case it is catalina.log.


Thursday, May 28, 2015

LDAP Integration with Liferay

LDAP Integration with Liferay

Introduction about LDAP:


The Lightweight Directory Access Protocol (LDAP) is a directory service protocol that runs on a layer above the TCP/IP stack. It provides a mechanism used to connect to, search, and modify Internet directories. The LDAP directory service is based on a client-server model.

 Directory services play an important role in developing intranet and Internet applications by allowing the sharing of information about users, systems, networks, services, and applications throughout the network.[2] As examples, directory services may provide any organized set of records, often with a hierarchical structure, such as a corporate email directory. Similarly, a telephone directory is a list of subscribers with an address and a phone number.

There are multiple vendors providing implementation of LDAP, But i will mention few of the commonly used LDAP Servers.

1. Apache Directory Server

2. Open LDAP

3. Windows Directory Services etc.

Here i am going to show an example how to configure LDAP and How to integrate it with Liferay 6.1, currently i have implemented in Liferay 6.1 it should work higher versions of liferay also.

I will use Apache Directory Server for LDAP here.

Liferay LDAP Configuration with Apache Directory Server

Steps Involved:

1. First of all we have to download a directory server the given link below is the apache ds download link.



2.  Install the server by followed by the steps given on same website . Now our DS(Directory Server) is installed. 


3.  Now open the Apache DS by double click on the icon on your desktop , you will see Eclipse Type Apache Directory Studio is open in front of You.



4.  Now  Right Click on LDAP Server Tab and create New Server




5.      Now Select one of the following servers available (Select latest one i.e 2.0.0 )





 6.      Click on finish, you have successfully configured your LDAP server. Now we will create 
new connection to server so that we can create users directly in our LDAP server  for 
Creating new Connection our Server should be running. So just Start the Server For that Right Click on Server and Click on Create a Connection.







 7.      Now you will see you have successfully created a connection. Basic Directory Structure Look like this.








 8.    We are ready with our LDAP Server running.  Now we have to configure Liferay to use this server for our purposes. For that navigate to


Control Panel à Portal Settings à Authentication à LDAP



9.      Now Click on Add Server you will have a long screen with some prefilled values , where you have to provide your configuration settings

a.    Server name  (You can give any name as of your choice. )




         b.      Default Values (Choose LDAP Server you are Usinf for us it is Apache DS)

  

  c.     Connection
            Base Provider URL:  This is the URL of your LDAP server (ldap://host:port             ) for us it is  “ldap://localhost:10389” .  to confirm go to your LDAP Browser and Right Click on Root DSE and select properties anf navigate to
Connection à Network Parameter





           Base DN: It is Initial search context of  Users will be searched(For us it is                                            dc=example,dc=com)

Principal:  It is  Binding DN or User (For us it is uid=admin,ou=system)
Credentials: It is the Binding Password (For us it is “secret”)

To confirm go to your LDAP Browser and Right Click on Root DSE and select properties anf navigate to
                                            Connection à Authentication




             Finally our connection settings look like this


              



1.      Now Click on Test LDAP Connection, if every thing is good you will see the message below.








1.      Users  Now you have provide information related to Users. Let Us see  little Description about these fields.


Authentication Search Filter:  It is the field of User Mapping which will be used to check the Validity of User.(For Us We will use cn=@screen_name@)

Import Search Filter:  It is the objectClass which will be used filter users during Importing Users from LDAP to your Portal.(for us it will be as it is i.e objectClass=person )







1.      User Mapping   Here you have to provide required fields of User which will be used in importing and exporting  Users. These are common fields which doesn’t need any description .




Note: The required fields must be filled  either in exporting or importing Users otherwise you will get Error Message.


1.      Now Click on Test LDAP Users, if everything is good  you will see List of Users fetched  from your LDAP Server

Note: To perform Test LDAP Server should be running and LDAP Server Should contain Users otherwise it will show no Users exist.



1.      Groups:  This Section conteins settings for mapping LDAP groups to Liferay.



1.      Export  This Section Contains settings for Exporting Users from LDAP.






1.      Now In our case we are going to use LDAP Password policy for that we have to put a property in our portal-ext.properties file

p                            passwords.passwordpolicytoolkit.generator=static


1.      Now you have to enable your portal to use LDAP as a Authentication Medium for that you have to enable the option in
Control Panel à Portal Settings à Authentication à LDAP

Note: There is another option Required with Enable , Required option is to specify that Users must be authenticated through LDAP Only.

  1. Now check the Use LDAP Password Policy option and import , Export option As per your requirement.

    Now you are all set with all the necessary settings required to use your LDAP Server to Authenticate Users as well as Import and Export Users to and from LDAP