Tag: WebService

RestEasy WebService & RestEasy Client with CDI injection in JBoss AS7


As we are already aware that JBossAS 7.1 is a Fully EE6 certified Application Server. It provides implementations of most needed features like “RestEasy WebService” And “CDI” injection. You might have already seen/read some of the demos of RestEast webservices in our previous articles. But here in this article we are going to focus on following points .

Point-1). How to developand use the JAX-RS resteasy implementations and various annotations associated with it on JBoss AS7.1.1.Final.

Point-2). How to use the CDI Injection of @javax.inject.Named Beans using @javax.enterprise.context.ApplicationScoped inside our RestEasy WebServices. And will see that why do we need “beans.xml” file.

Point-3). How and why to use the annotations like @javax.xml.bind.annotation.XmlRootElement.

Point-4). How to write a Pure RestEasy Standalone Client to access the RestEasy WebService.

The Source Code of this Demo can be Downloaded from the following link: “Github”
https://github.com/jaysensharma/MiddlewareMagicDemos/tree/master/RestfulService_With_CDI_AS7

Writing RestEasy WebService with CDI Injection.

Step-1). Create a directory somewhere in your file system like “home/userone/RestfulService_With_CDI_AS7” then in this directory create a “src” directory where we will place all our source codes and application components.

Step-2). Write a Simple Class “Customer.java” inside “home/userone/RestfulService_With_CDI_AS7/src” with the help of “@javax.xml.bind.annotation.XmlRootElement” annotation sothat its value can be represented as XML element in an XML document.

package test;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Customer {

	private int id;
	private String name;
	private String address;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}
}

Step-3). As above write another Simple Class “Customers.java” inside “home/userone/RestfulService_With_CDI_AS7/src” with the help of “@javax.xml.bind.annotation.XmlRootElement” annotation sothat its value also can be represented as XML element in an XML document.

package test;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.*;
@XmlRootElement
public class Customers {

	private List customer;

	public List getCustomer() {
		return customer;
	}

	public void setCustomer(List customer) {
		this.customer = customer;
	}
}

Step-4). Now we will write “TestBean.java” inorder to populate it’s property “java.util.TreeMap” with the Customer Objects. Write the “TestBean.java” program inside “home/userone/RestfulService_With_CDI_AS7/src”

package beans;
import java.io.Serializable;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;
import java.util.TreeMap;
import test.Customer;

@Named
@ApplicationScoped
public class TestBean implements Serializable
   {
    private static final long serialVersionUID = 1L;
    private TreeMap customerMap;

    public TestBean()
         {
           System.out.println("nt TestBean which is CDI Bean instantiated.");
           customerMap=new TreeMap();
         }

    public TreeMap getCustomerMap()
         {
            System.out.println("nt[TestBean] getCustomerMap() called.");
            return customerMap;
         }

    public void setCustomerMap(TreeMap customerMap)
         {
           System.out.println("nt[TestBean] setCustomerMap(TreeMap) called.");
           this.customerMap = customerMap;
         }
   }

Step-5). As in the above TestBean we have used the @Named annotation and we have declared it’s scope as “ApplicationScoped” so now we will create a “beans.xml” file which is finally going to be placed inside “WEB-INF” directory of our webapplication for the CDI to wrok. So here we will need to create a file with name “beans.xml” inside “home/userone/RestfulService_With_CDI_AS7/src” as following:

*** NOTE: An application that uses CDI must have a file named beans.xml. The file can be completely empty (it has content only in certain limited situations), but it must be present.

<beans xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
      http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">


</beans>

Step-6). In order to develop RestEasy WebService we should provide a class which extends “javax.ws.rs.core.Application” so write a Program “MyRestService.java” inside “home/userone/RestfulService_With_CDI_AS7/src” as following:

package ws.test;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("test")
public class MyRestService  extends Application {

}

Step-7). Now the Most important Step Here we will write a Class “CustomersResource.java” with the help of “javax.ws.rs.*” packages in order to process the Client Requests. so write a Program “CustomersResource.java” inside “home/userone/RestfulService_With_CDI_AS7/src” as following:

package test;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.FormParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import javax.servlet.ServletContext;
import javax.ws.rs.core.Context;
import javax.inject.Inject;

@Produces("application/xml")
@Path("customers")
public class CustomersResource {

        @Inject
        private beans.TestBean testBean;

	private TreeMap customerMap = new TreeMap();

	public CustomersResource() {
                System.out.println("[CustomerResource] Instantiated  "+this);
		Customer customer1 = new Customer();
		customer1.setId(1111);
		customer1.setName("CustomerOne");
		customer1.setAddress("Bombay, India");

		Customer customer2 = new Customer();
		customer2.setId(2222);
		customer2.setName("CustomerTwo");
		customer2.setAddress("Pune, India");

		Customer customer3 = new Customer();
		customer3.setId(3333);
		customer3.setName("CustomerThree");
		customer3.setAddress("Bangalore, India");

                customerMap.put(customer1.getId(),customer1);
                customerMap.put(customer2.getId(),customer2);
                customerMap.put(customer3.getId(),customer3);

                if(testBean!=null)
                    {
                       System.out.println("[CustomersResource] testBean NOT NULL "+testBean);
                       testBean.setCustomerMap(customerMap);
                    }
                else
                    {
                       System.out.println("[CustomersResource] testBean IS NULL");
                    }
	}

	@GET
	@Produces(MediaType.APPLICATION_XML)
	@Path("all")
	public Customers getCustomers() {
		List customers = new ArrayList();
		customers.addAll(testBean.getCustomerMap().values());
                Customers allCustomers=new Customers();
                allCustomers.setCustomer(customers);
                return allCustomers;
	}

	@GET
	@Path("{id}")
	public Customer getCustomer(@PathParam("id") int customerId) {
		return customerMap.get(customerId);
	}

	public String addDefaultCustomer(Customer customer) {
		customerMap.put(customer.getId(), customer);
		return "Customer ID: " +  customer.getId() +" Name: "+ customer.getName()+" Address: "+customer.getAddress();
	}

	@POST
	@Path("add")
	@Produces(MediaType.APPLICATION_XML)
	@Consumes("application/x-www-form-urlencoded")
	public Customer addCustomer(@FormParam("custId")int id,@FormParam("custName")String name,@FormParam("custAddress")String address) {
		Customer customer = new Customer();
                customer.setId(id);
                customer.setName(name);
                customer.setAddress(address);
                customerMap.put(customer.getId(),customer);
                testBean.setCustomerMap(customerMap);
		return customer;
	}

}

Step-8). Now we will write following kind of JSP file “index.jsp” inside “home/userone/RestfulService_With_CDI_AS7/src” in order to allow users to provide/enter the customer details.


    CDI Demo
<table border="10%">
<form action="test/customers/add" method="POST"></form>
<tbody>
<tr>
<td>Customer ID:</td>
<td> <input name="custId" type="text" /></td>
</tr>
<tr>
<td>Customer Name:</td>
<td> <input name="custName" type="text" /></td>
</tr>
<tr>
<td>Customer Address:</td>
<td> <input name="custAddress" type="text" /></td>
</tr>
<tr>
<td><input type="submit" value="Add Customer" /></td>
<td><input type="reset" value="Reset" /></td>
</tr>
</tbody>
</table>

Step-9). Now write the following kind of “web.xml” file inside “home/userone/RestfulService_With_CDI_AS7/src” directord make sure that you are using the web.xml 3.0 version XSD (web-app_3_0.xsd) declaration in your web.xml file as following


        javax.ws.rs.core.Application
        javax.ws.rs.core.Application

        javax.ws.rs.core.Application
        /*

Step-10). Now we will write the ANT “build.xml” file inside “home/userone/RestfulService_With_CDI_AS7” directory in order to build and deploy and test our WebService .


           <!-- Need to place at lease one empty "beans.xml" file inside WEB-INF -->

Step-11). Now before running your ANT script to build and deploy the above webapplication you should have the ANT as well as JAVA set in the $PATH variable of the Shell / command prompt as following:

.
For Unix Based OS:
export PATH=/home/userone/jdk1.6.0_21/bin:/home/userone/org.apache.ant_1.6.5/bin:$PATH

For Windows Based OS:
set PATH=C:/jdk1.6.0_21/bin;C:/org.apache.ant_1.6.5/bin;%PATH%
.

Step-12). Now once the PATH is set In the command/Shell prompt you can move inside the directory “home/userone/RestfulService_With_CDI_AS7” and then run the ant to build/deploy the webservice. by running the command “ant”

[userone@localhost RestfulService_With_CDI_AS7]$ ant
Buildfile: build.xml

init:
   [delete] Deleting directory /userone/RestfulService_With_CDI_AS7/build
    [mkdir] Created dir: /userone/RestfulService_With_CDI_AS7/build
    [mkdir] Created dir: /userone/RestfulService_With_CDI_AS7/tmp

build:
    [mkdir] Created dir: /userone/RestfulService_With_CDI_AS7/tmp/WEB-INF/classes
    [javac] Compiling 5 source files to /userone/RestfulService_With_CDI_AS7/tmp/WEB-INF/classes
     [copy] Copying 1 file to /userone/RestfulService_With_CDI_AS7/tmp
     [copy] Copying 1 file to /userone/RestfulService_With_CDI_AS7/tmp
     [copy] Copying 1 file to /userone/RestfulService_With_CDI_AS7/tmp/WEB-INF/classes
     [copy] Copying 1 file to /userone/RestfulService_With_CDI_AS7/tmp/WEB-INF/classes
     [copy] Copying 1 file to /userone/RestfulService_With_CDI_AS7/tmp/WEB-INF
      [jar] Building jar: /userone/RestfulService_With_CDI_AS7/tmp/RestServiceWithCDI.war
     [copy] Copying 1 file to /userone/RestfulService_With_CDI_AS7/build

deploy:
     [echo] *******************  Deploying the WAR file RestServiceWithCDI.war *********************
     [echo] ********** build/RestServiceWithCDI.war to /home/userone/jboss-as-7.1.1.Final/standalone/deployments **********
     [copy] Copying 1 file to /home/userone/jboss-as-7.1.1.Final/standalone/deployments
     [echo] *******************  Deployed Successfully   *********************

BUILD SUCCESSFUL
Total time: 2 seconds

Testing The RestEasy WebService from WebBrowser

Step-13). Once the WebService is deployed successfully you can access it using the following URL: “http://localhost:8080/RestServiceWithCDI//index.jsp” As following:

RestEasyWebService_CDI_Inserting Customer Data

If you want to access all the Customers Details then access te following URL “http://localhost:8080/RestServiceWithCDI/test/customers/all” :

RestEasyWebService_With_CDI_Retrieving all Customers Record

Writing and Testing a Pure RestEasy WebService Client

Step-13). Now we are going to use our own Standalone RestEasy WebService Client in order to access the WebService. So write program with name “RestEasyClient.java” inside “home/userone/RestfulService_With_CDI_AS7/src” directory as following:

package client;
import java.io.*;
import javax.ws.rs.core.MediaType;
import org.jboss.resteasy.client.ClientRequest;
import org.jboss.resteasy.client.ClientResponse;

public class RestEasyClient
{
      public static void main(String ar[]) throws Exception
       {
        String url="http://localhost:8080/RestServiceWithCDI/test/customers/all";
        MediaType mediaType=MediaType.APPLICATION_XML_TYPE;
        String result = "";

        try
        {
            ClientRequest request = new ClientRequest(url);
            request.accept(mediaType);
            ClientResponse response = request.get(String.class);
            if (response.getStatus() != 200)
            {
                System.out.println("***** BAD RESPONSE *****");
                throw new RuntimeException("Request Processing Failed with HTTP status: "+ response.getStatus());
            }

            BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(response.getEntity().getBytes())));
            String output = null;
            while ((output = br.readLine()) != null)
            {
                result = result+output+"n";
            }

            result=result.replaceAll("><",">n<");
            System.out.println("nn********** RESULT **********n"+result);
        }
        catch (Exception e)
        {
            System.err.println("nt Exception Occured: "+e);
            e.printStackTrace();
        }
    }
}

Step-14). Now run the command “ant client” in order compile and run the Client program from the following directory “home/userone/RestfulService_With_CDI_AS7” as following:

[userone@localhost RestfulService_With_CDI_AS7]$ ant client
Buildfile: build.xml

client:
     [echo] *******************  Client Accessing RestEasy Service *********************
    [javac] Compiling 6 source files to /NotBackedUp/Downloads/RestfulService_With_CDI_AS7/build
      log4j:WARN No appenders could be found for logger (org.jboss.resteasy.plugins.providers.DocumentProvider).
      log4j:WARN Please initialize the log4j system properly.
      log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
      
      
      ********** RESULT **********
      <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
      <customers>
      <customer>
      <address>Bombay, India</address>
      <id>1111</id>
      <name>CustomerOne</name>
      </customer>
      <customer>
      <address>Pune, India</address>
      <id>2222</id>
      <name>CustomerTwo</name>
      </customer>
      <customer>
      <address>Bangalore, India</address>
      <id>3333</id>
      <name>CustomerThree</name>
      </customer>
      <customer>
      <address>Pune, India</address>
      <id>8888</id>
      <name>MiddlewareMagic</name>
      </customer>
      </customers>
      

BUILD SUCCESSFUL
Total time: 3 seconds

.
.
Thanks 🙂
Middleware Magic Team


Apache CXF Service with Complex Type in JBoss AS7

Hi,

JBoss AS7 uses the JBossWS-CXF as default webservice provider which provides most of the features coming with Apache CXF (including WS-Security, WS-Policy, WS-Addressing, WS-ReliableMessaging, basic WS-Trust, MTOM) as well as common JBossWS stack features like endpoint metrics, record management, endpoint address rewrite, etc.

In this example we will see how we can use a Complex DataType inside our CXF Based WebService and how we can enable the client side Logging for the incoming and outgoing SOAP Request/Responses, which are most desired for trouble shooting and debugging purpose.

In this example we will mainly focus on following points

Point-1). How to use the JBoss Specific Tools to build the WebServices. The details of these tools like “wsconsume” and “wsprovide” are mentioned in the following link: https://docs.jboss.org/author/display/JBWS/JAX-WS+Tools

Point-2). Where we need to place the “log4j.properties fileat the client side in order to generate a separate Log file containing the SOAP request/response.

Point-3). How to use “org.apache.cxf.interceptor.LoggingInInterceptor” and “org.apache.cxf.interceptor.LoggingOutInterceptor” at client side.

Point-4). Here we are using JBoss AS7.1.0 CR1b which can be downloaded from the following link: http://www.jboss.org/jbossas/downloads. I am using “jboss-as-7.1.0.CR1bb” in this demo.

Point-5). How to use the JAXB Annotations like “XmlAccessorType”, “XmlAccessType” and “XmlType” to define and use the Complex Datatypes inside our webservice using the “SOAPBinding.ParameterStyle.BARE”

Point-6). Source Code of this Demo is available at https://github.com/jaysensharma/MiddlewareMagicDemos

Developing Simple WebService

Step-1). Create a directory somewhere in your file system as “/home/userone/ComplexTypes_CXF_WebService_JBossAS7” then create another directory “src” inside the “/home/userone/ComplexTypes_CXF_WebService_JBossAS7”.

Step-2). Write a simple webservice implementation pojo class “DemoCXF.java” as following inside the directory “/home/userone/ComplexTypes_CXF_WebService_JBossAS7/src”:

package ws;
import javax.jws.WebParam;
import javax.jws.WebMethod;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;

@WebService(name = "DemoCXF", serviceName ="DemoCXFService", portName ="DemoCXFPort", targetNamespace="http://test.org")
@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE) 

public class DemoCXF
{
  @WebMethod()
  @WebResult(targetNamespace="http://test.org",name="updatedEmployee")
  public Employee processEmployeeSalary(@WebParam(partName = "employee", name = "employee", targetNamespace = "http://test.org")
        Employee emp, @WebParam(partName = "incrementAmount", name = "incrementAmount", targetNamespace = "http://test.org") Long incrementAmount)
  {
    System.out.println("[DemoCXF] Method Invoked....processEmployeeSalary");
    System.out.println("[DemoCXF] Before processing: "+emp);
    long incrementedSalary=emp.getEmpSalary()+incrementAmount;
    emp.setEmpSalary(incrementedSalary);
    System.out.println("[DemoCXF] After processing: "+emp);

    // Some Business Logic to Store the Employee's Updated Details in Database or Messaging System.

    return emp;
  }
}

Step-3). Write a simple Complex Type “Employee.java” as following inside the directory “/home/userone/ComplexTypes_CXF_WebService_JBossAS7/src”:

package ws;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "employee", propOrder = {
    "empName",
    "empNo",
    "empSalary"
})

public class Employee
  {
    public String empName;
    public Long empNo;
    public Long empSalary;

    public String getEmpName() {
        return empName;
    }

    public void setEmpName(String value) {
        this.empName = value;
    }

    public Long getEmpNo() {
        return empNo;
    }

    public void setEmpNo(Long value) {
        this.empNo = value;
    }

    public Long getEmpSalary() {
        return empSalary;
    }

    public void setEmpSalary(Long value) {
        this.empSalary = value;
    }

  }

Step-4). Write the “web.xml” file to define the WebService as following inside the directory “/home/userone/ComplexTypes_CXF_WebService_JBossAS7/src”

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <servlet> 
    <servlet-name>DemoCXF</servlet-name>  
    <servlet-class>ws.DemoCXF</servlet-class>  
  </servlet>  

  <servlet-mapping> 
    <servlet-name>DemoCXF</servlet-name>  
    <url-pattern>/*</url-pattern> 
  </servlet-mapping>  
</web-app>

Step-5). Write the WebService client code “Test_CXF_Client.java” as following inside the directory “/home/userone/ComplexTypes_CXF_WebService_JBossAS7/src”:

package client;
import java.net.URL;
import javax.xml.namespace.QName;

// Client side Logging related CXF APIs
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;

public class Test_CXF_Client
  {
     public static void main(String ar[]) throws Exception
      {
        String WSDL_URL=ar[0]+"?wsdl";
        DemoCXFService service=new DemoCXFService(new URL(WSDL_URL));
        DemoCXF port=service.getDemoCXFPort();

        /* Following part of code is needed for client side Logging of Soap request/response */
        /* We need to make sure that we place the "log4j.properties" file inside clients classpath */
        Client client = ClientProxy.getClient(port);
        client.getInInterceptors().add(new LoggingInInterceptor());
        client.getOutInterceptors().add(new LoggingOutInterceptor());


        Employee emp=new Employee();
        emp.setEmpNo(1000L);
        emp.setEmpName("MiddlewaremagicEmployee");
        emp.setEmpSalary(6000L);

        System.out.println("nnBefore  EmpNo: "+emp.getEmpNo()+",  Name:"+emp.getEmpName()+",  Sal:"+emp.getEmpSalary());
        emp=port.processEmployeeSalary(emp,1000L);
        System.out.println("nnAfter   EmpNo: "+emp.getEmpNo()+",  Name:"+emp.getEmpName()+",  Sal:"+emp.getEmpSalary());
      }
  }

Step-5). As we are going to generate the client side logging in a separate log file at client side using CXF APIs so we will need to write a “log4j.properties” file as following inside the directory “/home/userone/ComplexTypes_CXF_WebService_JBossAS7/src”:

# Direct log messages to a log file
log4j.rootLogger=INFO, WSClientAppender

log4j.appender.WSClientAppender=org.apache.log4j.ConsoleAppender
log4j.appender.WSClientAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.WSClientAppender.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

NOTE: In this example we are using the “ConsoleAppender” whihc means in the Clients Console (Shell Pormpt) we will be able to see the Incoming and Outgoing soap request/response.

Step-6). Now we will write simple ANT build script “build.xml” which will build / deploy and run the webservice & client as following inside the directory “/home/userone/ComplexTypes_CXF_WebService_JBossAS7”:

<project name="JBoss_Complex_Service" default="deploy">
<property name="jboss.home" value="/home/userone/jboss-as-7.1.0.CR1b" />
<property name="jboss.module.dir" value="${jboss.home}/modules" />

<property name="basedir" value="." />
<property name="war.dir" value="CXFComplexTypeDemoWAR" />
<property name="war.name" value="CXFComplexTypeDemo.war" />
<property name="src.dir" value="src" />
<property name="client.src.dir" value="${basedir}/clientSrc" />
<property name="output.dir" value="build" />
<property name="client.dir" value="${basedir}/clientStuff" />
<property name="client.jar.name" value="DemoCXFClient.jar" />
 
   <path id="jboss.classpath">
     <fileset dir="${jboss.module.dir}">
        <include name="**/*.jar"/>
     </fileset>  
   </path>

   <path id="client.classpath">
     <fileset dir="${jboss.module.dir}">
        <include name="**/*.jar"/>
     </fileset>  
     <fileset dir="${client.dir}">
        <include name="*.jar"/>
     </fileset>  
   </path>

   <taskdef name="wsprovide" classname="org.jboss.ws.tools.ant.WSProvideTask">
       <classpath refid="jboss.classpath"/>
   </taskdef>

   <taskdef name="wsconsume" classname="org.jboss.ws.tools.ant.WSConsumeTask">
       <classpath refid="jboss.classpath"/>
   </taskdef>

        <target name="init">
           <delete dir="${output.dir}" />
           <mkdir dir="${output.dir}" />
           <mkdir dir="${output.dir}/${war.dir}"/>
           <mkdir dir="${output.dir}/${war.dir}/META-INF"/>
           <mkdir dir="${output.dir}/${war.dir}/WEB-INF"/>
           <mkdir dir="${output.dir}/${war.dir}/WEB-INF/classes"/>
           <delete dir="${client.dir}" />
           <mkdir dir="${client.dir}/META-INF"/>
        </target>
	 
    <target name="build" depends="init">
       <javac srcdir="${src.dir}" destdir="${output.dir}/${war.dir}/WEB-INF/classes"  includes="*.java" excludes="Test_CXF_Client.java" classpathref="jboss.classpath"/>
        <copy todir="${output.dir}/${war.dir}/WEB-INF">
	  <fileset dir="${basedir}/src">
	      <include name="web.xml"/>
	  </fileset>
	</copy>   
       <wsprovide
        	fork="false"
        	keep="true"
        	destdir="${output.dir}"
        	resourcedestdir="${output.dir}/${war.dir}/WEB-INF/wsdl"
        	sourcedestdir="${output.dir}"
        	genwsdl="true" 
        	verbose="true"
        	sei="ws.DemoCXF">
            	<classpath>
                	  <pathelement path="${output.dir}/${war.dir}/WEB-INF/classes"/>
            	</classpath>
      </wsprovide>

      <jar jarfile="${output.dir}/${war.name}" basedir="${output.dir}/${war.dir}" compress="true" /> 
      
    </target>

        <target name="deploy" depends="build">
            <echo message="*******************  Deploying   *********************" />  
            <echo message="********** ${war.name} to ${jboss.home}/standalone/deployments **********" />  
            <copy file="${output.dir}/${war.name}" todir="${jboss.home}/standalone/deployments/"/>

            <echo message="*******************  Deployed Successfully   *********************" />  
        </target>
  
        <target name="post-deploy" >
            <echo message="*******************  NOTE  *********************" />
            <echo message="***** You should be able to access your WSDL using Browser now *****" />
            <echo message="                http://localhost:8080/CXFComplexTypeDemo?wsdl          " />
        </target>  

        <target name="client" depends="post-deploy">
           <delete dir="${client.dir}" />
           <mkdir dir="${client.dir}"/>             
             <wsconsume
                      fork="true"
                      keep="true"
                      destdir="${client.dir}"
                      sourcedestdir="${client.dir}"
                      package="client"
                      wsdlLocation="service.wsdl"
                      wsdl="http://localhost:8080/CXFComplexTypeDemo?wsdl">
            </wsconsume>

            <!-- We need to make sure that log4j.properties is present inside client's classpath-->
            <copy file="${src.dir}/log4j.properties" todir="${client.dir}" />

            <javac srcdir="${src.dir}" destdir="${client.dir}"  includes="Test_CXF_Client.java" classpathref="client.classpath">
	        <classpath>
	            <pathelement location="${client.dir}"/>
	            <path refid="client.classpath"/>
	        </classpath>
            </javac>

            <jar jarfile="${client.dir}/${client.jar.name}" basedir="${client.dir}" compress="true" /> 

            <!-- Cleaning Client Stuff Directory  --> 
            <delete dir="${client.dir}/client"/>
            <delete file="${client.dir}/log4j.properties"/> 
       </target>

        <target name="run" depends="client">
            <java classname="client.Test_CXF_Client" fork="true" >
	        <classpath>
	            <pathelement location="${client.dir}/DemoCXFClient.jar"/>
	            <path refid="client.classpath"/>
	        </classpath>
                <arg value="http://localhost:8080/CXFComplexTypeDemo"/>
            </java>
        </target>  

</project>

NOTE: The only change in the above file you need to do is to change the “jboss.home” directory path in the second line of the above script to point to your own JBoss AS7 directory.

Point-7). Now Start your JBoss Profile as following from inside the directory “/home/userone/jboss-as-7.1.0.CR1b/bin”:

.
 ./standalone.sh -c standalone-full.xml
.

Step-8). Now before running your ANT script to build and deploy the above webapplication you should have the ANT as well as JAVA set in the $PATH variable of the Shell / command prompt as following:

For Unix Based OS:
export PATH=/home/userone/jdk1.6.0_21/bin:/home/userone/org.apache.ant_1.6.5/bin:$PATH

For Windows Based OS:
set PATH=C:/jdk1.6.0_21/bin;C:/org.apache.ant_1.6.5/bin;%PATH%

Step-9). Now once the PATH is set In the command/Shell prompt you can move inside the directory “/home/userone/ComplexTypes_CXF_WebService_JBossAS7” and then run the ant to build and deploy the WebService application on your JBoss Standalone full profile, by running the command “ant deploy”

[userone@localhost ComplexTypes_CXF_WebService_JBossAS7]$ ant deploy
Buildfile: build.xml

init:
   [delete] Deleting directory /home/userone/ComplexTypes_CXF_WebService_JBossAS7/build
    [mkdir] Created dir: /home/userone/ComplexTypes_CXF_WebService_JBossAS7/build
    [mkdir] Created dir: /home/userone/ComplexTypes_CXF_WebService_JBossAS7/build/CXFComplexTypeDemoWAR
    [mkdir] Created dir: /home/userone/ComplexTypes_CXF_WebService_JBossAS7/build/CXFComplexTypeDemoWAR/META-INF
    [mkdir] Created dir: /home/userone/ComplexTypes_CXF_WebService_JBossAS7/build/CXFComplexTypeDemoWAR/WEB-INF
    [mkdir] Created dir: /home/userone/ComplexTypes_CXF_WebService_JBossAS7/build/CXFComplexTypeDemoWAR/WEB-INF/classes
   [delete] Deleting directory /home/userone/ComplexTypes_CXF_WebService_JBossAS7/clientStuff
    [mkdir] Created dir: /home/userone/ComplexTypes_CXF_WebService_JBossAS7/clientStuff/META-INF

build:
    [javac] Compiling 2 source files to /home/userone/ComplexTypes_CXF_WebService_JBossAS7/build/CXFComplexTypeDemoWAR/WEB-INF/classes
     [copy] Copying 1 file to /home/userone/ComplexTypes_CXF_WebService_JBossAS7/build/CXFComplexTypeDemoWAR/WEB-INF
[wsprovide] Generating from endpoint: ws.DemoCXF
[wsprovide] log4j:WARN No appenders could be found for logger (org.apache.cxf.common.logging.LogUtils).
[wsprovide] log4j:WARN Please initialize the log4j system properly.
[wsprovide] log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
[wsprovide] java2ws -s /home/userone/ComplexTypes_CXF_WebService_JBossAS7/build -classdir /home/userone/ComplexTypes_CXF_WebService_JBossAS7/build -d /home/userone/ComplexTypes_CXF_WebService_JBossAS7/build/CXFComplexTypeDemoWAR/WEB-INF/wsdl -verbose -wsdl -cp ::::::::: -wrapperbean -createxsdimports ws.DemoCXF
[wsprovide] java2ws - Apache CXF 2.4.4
[wsprovide] 
      [jar] Building jar: /home/userone/ComplexTypes_CXF_WebService_JBossAS7/build/CXFComplexTypeDemo.war

deploy:
     [echo] *******************  Deploying   *********************
     [echo] ********** CXFComplexTypeDemo.war to /home/userone/jboss-as-7.1.0.CR1b/standalone/deployments **********
     [copy] Copying 1 file to /home/userone/jboss-as-7.1.0.CR1b/standalone/deployments
     [echo] *******************  Deployed Successfully   *********************

BUILD SUCCESSFUL
Total time: 5 seconds

As soon as the WebService will be deployed on the JBoss AS7 you will see the following kind of output on your JBoss Console (STDOUT):

15:53:37,164 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-2) Starting deployment of "CXFComplexTypeDemo.war"
15:53:37,203 INFO  [org.jboss.wsf.stack.cxf.metadata.MetadataBuilder] (MSC service thread 1-5) Add Service
 id=DemoCXF
 address=http://localhost:8080/CXFComplexTypeDemo
 implementor=ws.DemoCXF
 invoker=org.jboss.wsf.stack.cxf.JBossWSInvoker
 serviceName={http://test.org}DemoCXFService
 portName={http://test.org}DemoCXFPort
 wsdlLocation=null
 mtomEnabled=false
15:53:37,204 INFO  [org.jboss.ws.common.management.DefaultEndpointRegistry] (MSC service thread 1-5) register: jboss.ws:context=CXFComplexTypeDemo,endpoint=DemoCXF
15:53:37,213 INFO  [org.apache.cxf.service.factory.ReflectionServiceFactoryBean] (MSC service thread 1-5) Creating Service {http://test.org}DemoCXFService from class ws.DemoCXF
15:53:37,238 INFO  [org.apache.cxf.endpoint.ServerImpl] (MSC service thread 1-5) Setting the server's publish address to be http://localhost:8080/CXFComplexTypeDemo
15:53:37,247 INFO  [org.jboss.wsf.stack.cxf.deployment.WSDLFilePublisher] (MSC service thread 1-5) WSDL published to: file:/home/userone/jboss-as-7.1.0.CR1b/standalone/data/wsdl/CXFComplexTypeDemo.war/DemoCXFService.wsdl
15:53:37,251 INFO  [org.jboss.as.webservices] (MSC service thread 1-8) JBAS015539: Starting service jboss.ws.endpoint."CXFComplexTypeDemo.war".DemoCXF
15:53:37,260 INFO  [org.jboss.web] (MSC service thread 1-8) registering web context: /CXFComplexTypeDemo
15:53:37,268 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 2) JBAS018565: Replaced deployment "CXFComplexTypeDemo.war" with deployment "CXFComplexTypeDemo.war"

Step-10). Now we will try to compile and run the WebService Client application by running the command “ant run”

[userone@localhost ComplexTypes_CXF_WebService_JBossAS7]$ ant run
Buildfile: build.xml

post-deploy:
     [echo] *******************  NOTE  *********************
     [echo] ***** You should be able to access your WSDL using Browser now *****
     [echo]                 http://localhost:8080/CXFComplexTypeDemo?wsdl          

client:
   [delete] Deleting directory /home/userone/ComplexTypes_CXF_WebService_JBossAS7/clientStuff
    [mkdir] Created dir: /home/userone/ComplexTypes_CXF_WebService_JBossAS7/clientStuff
[wsconsume] Consuming wsdl: http://localhost:8080/CXFComplexTypeDemo?wsdl
[wsconsume] log4j:WARN No appenders could be found for logger (org.apache.cxf.common.logging.LogUtils).
[wsconsume] log4j:WARN Please initialize the log4j system properly.
[wsconsume] log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
     [copy] Copying 1 file to /home/userone/ComplexTypes_CXF_WebService_JBossAS7/clientStuff
    [javac] Compiling 1 source file to /home/userone/ComplexTypes_CXF_WebService_JBossAS7/clientStuff
      [jar] Building jar: /home/userone/ComplexTypes_CXF_WebService_JBossAS7/clientStuff/DemoCXFClient.jar
   [delete] Deleting directory /home/userone/ComplexTypes_CXF_WebService_JBossAS7/clientStuff/client
   [delete] Deleting: /home/userone/ComplexTypes_CXF_WebService_JBossAS7/clientStuff/log4j.properties

run:
      Feb 4, 2012 3:54:51 PM client.DemoCXFService <clinit>
      INFO: Can not initialize the default wsdl from service.wsdl
      15:54:52,844  INFO ReflectionServiceFactoryBean:366 - Creating Service {http://test.org}DemoCXFService from WSDL: http://localhost:8080/CXFComplexTypeDemo?wsdl
      
      
      Before  EmpNo: 1000,  Name:MiddlewaremagicEmployee,  Sal:6000
      15:54:53,275  INFO LoggingOutInterceptor:151 - Outbound Message
      ---------------------------
      ID: 1
      Address: http://localhost:8080/CXFComplexTypeDemo
      Encoding: UTF-8
      Content-Type: text/xml
      Headers: {Accept=[*/*], SOAPAction=[""]}
      Payload: 
            <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
             <soap:Body>
                  <ns2:employee xmlns:ns2="http://test.org">
                      <empName>MiddlewaremagicEmployee</empName>
                      <empNo>1000</empNo>
                      <empSalary>6000</empSalary>
                  </ns2:employee>
                  <ns2:incrementAmount xmlns:ns2="http://test.org">1000</ns2:incrementAmount>
              </soap:Body>
            </soap:Envelope>
      --------------------------------------
      15:54:53,289  INFO LoggingInInterceptor:151 - Inbound Message
      ----------------------------
      ID: 1
      Response-Code: 200
      Encoding: UTF-8
      Content-Type: text/xml;charset=UTF-8
      Headers: {Content-Length=[268], content-type=, Date=[Sat, 04 Feb 2012 10:24:52 GMT], Server=[Apache-Coyote/1.1]}
      Payload: 
            <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
               <soap:Body>
                  <ns2:updatedEmployee xmlns:ns2="http://test.org">
                      <empName>MiddlewaremagicEmployee</empName>
                      <empNo>1000</empNo>
                      <empSalary>7000</empSalary>
                  </ns2:updatedEmployee>
               </soap:Body>
            </soap:Envelope>
      --------------------------------------
      
      
      After   EmpNo: 1000,  Name:MiddlewaremagicEmployee,  Sal:7000

BUILD SUCCESSFUL
Total time: 9 seconds

Step-11). The Generated WSDL File will look something like following :
http://localhost:8080/CXFComplexTypeDemo?wsdl

<?xml version='1.0' encoding='UTF-8'?><wsdl:definitions name="DemoCXFService" targetNamespace="http://test.org" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://test.org" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <wsdl:types>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="unqualified" targetNamespace="http://test.org" xmlns="http://test.org" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="employee">
    <xs:sequence>
      <xs:element minOccurs="0" name="empName" type="xs:string"/>
      <xs:element minOccurs="0" name="empNo" type="xs:long"/>
      <xs:element minOccurs="0" name="empSalary" type="xs:long"/>
    </xs:sequence>
  </xs:complexType>
  <xs:element name="employee" nillable="true" type="employee"/>
  <xs:element name="incrementAmount" nillable="true" type="xs:long"/>
  <xs:element name="updatedEmployee" nillable="true" type="employee"/>
</xs:schema>
  </wsdl:types>
  <wsdl:message name="processEmployeeSalaryResponse">
    <wsdl:part element="tns:updatedEmployee" name="updatedEmployee">
    </wsdl:part>
  </wsdl:message>
  <wsdl:message name="processEmployeeSalary">
    <wsdl:part element="tns:employee" name="employee">
    </wsdl:part>
    <wsdl:part element="tns:incrementAmount" name="incrementAmount">
    </wsdl:part>
  </wsdl:message>
  <wsdl:portType name="DemoCXF">
    <wsdl:operation name="processEmployeeSalary">
      <wsdl:input message="tns:processEmployeeSalary" name="processEmployeeSalary">
    </wsdl:input>
      <wsdl:output message="tns:processEmployeeSalaryResponse" name="processEmployeeSalaryResponse">
    </wsdl:output>
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="DemoCXFServiceSoapBinding" type="tns:DemoCXF">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="processEmployeeSalary">
      <soap:operation soapAction="" style="document"/>
      <wsdl:input name="processEmployeeSalary">
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output name="processEmployeeSalaryResponse">
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="DemoCXFService">
    <wsdl:port binding="tns:DemoCXFServiceSoapBinding" name="DemoCXFPort">
      <soap:address location="http://localhost:8080/CXFComplexTypeDemo"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

.
.
Thanks 🙂
Middleware Magic Team


Apache CXF Client Side Logging in JBoss AS7

Hi,

JBoss AS7 uses the JBossWS-CXF as default webservice provider which provides most of the features coming with Apache CXF (including WS-Security, WS-Policy, WS-Addressing, WS-ReliableMessaging, basic WS-Trust, MTOM) as well as common JBossWS stack features like endpoint metrics, record management, endpoint address rewrite, etc.

In this example we will see how we can enable the client side Logging for the incoming and outgoing SOAP Request/Responses, which are most desired for trouble shooting and debugging purpose.

In this example we will mainly focus on following points

Point-1). How to use the JBoss Specific Tools to build the WebServices. The details of these tools like “wsconsume” and “wsprovide” are mentioned in the following link: https://docs.jboss.org/author/display/JBWS/JAX-WS+Tools

Point-2). Where we need to place the “log4j.properties fileat the client side in order to generate a separate Log file containing the SOAP request/response.

Point-3). How to use “org.apache.cxf.interceptor.LoggingInInterceptor” and “org.apache.cxf.interceptor.LoggingOutInterceptor” at client side.

Point-4). Here we are using JBoss AS7.1.0 CR1b which can be downloaded from the following link: http://www.jboss.org/jbossas/downloads. I am using “jboss-as-7.1.0.CR1b” in this demo.

Point-5). Source Code of this Demo is available at https://github.com/jaysensharma/MiddlewareMagicDemos

Developing Simple WebService

Step-1). Create a directory somewhere in your file system as “/home/userone/CXFClientSide_Logging_Demo” then create another directory “src” inside the “/home/userone/CXFClientSide_Logging_Demo”.

Step-2). Write a simple webservice implementation pojo class “DemoCXF.java” as following inside the directory “/home/userone/CXFClientSide_Logging_Demo/src”:


package ws;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.annotation.Resource;
import javax.xml.ws.WebServiceContext;
import java.util.Collection;

@WebService(name = "DemoCXF", serviceName ="DemoCXFService", portName ="DemoCXFPort", targetNamespace="http://test.org")

public class DemoCXF
{
  @Resource
  WebServiceContext ctx;

  @WebMethod()
  public String sayHello(String name)
  {
    System.out.println("nnt Method Invoked....String sayHello("+name+")");
    return "Hello JBossAS7 User: "+name;
  }

  @WebMethod()
  public String getProperty(String propertyName)
  {
    System.out.println("nnt Method Invoked....String getProperty(String propertyName)");
    return "RETURNED: "+(Collection) ctx.getMessageContext().values();
  }
}

Step-3). Write the “web.xml” file to define the WebService as following inside the directory “/home/userone/CXFClientSide_Logging_Demo/src”:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <servlet> 
    <servlet-name>DemoCXF</servlet-name>  
    <servlet-class>ws.DemoCXF</servlet-class>  
  </servlet>  

  <servlet-mapping> 
    <servlet-name>DemoCXF</servlet-name>  
    <url-pattern>/*</url-pattern> 
  </servlet-mapping>  
</web-app>

Step-4). Write the WebService client code “Test_CXF_Client.java” as following inside the directory “/home/userone/CXFClientSide_Logging_Demo/src”:


package client;
import java.net.URL;
import javax.xml.namespace.QName;

// Client side Logging related CXF APIs 
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;

public class Test_CXF_Client
  {
     public static void main(String ar[]) throws Exception
      {
        String WSDL_URL=ar[0]+"?wsdl";
        DemoCXFService service=new DemoCXFService(new URL(WSDL_URL));
        DemoCXF port=service.getDemoCXFPort();

        /* Following part of code is needed for client side Logging of Soap request/response */
        /* We need to make sure that we place the "log4j.properties" file inside clients classpath */
        Client client = ClientProxy.getClient(port);
        client.getInInterceptors().add(new LoggingInInterceptor());
        client.getOutInterceptors().add(new LoggingOutInterceptor()); 

         
        System.out.println("nt port.sayHello("MiddlewareMagic") = "+port.sayHello("MiddlewareMagic"));
      }
  }

Step-5). As we are going to generate the client side logging in a separate log file at client side using CXF APIs so we will need to write a “log4j.properties” file as following inside the directory “/home/userone/CXFClientSide_Logging_Demo/src”:

# Direct log messages to a log file
log4j.rootLogger=INFO, WSClientAppender


log4j.appender.WSClientAppender=org.apache.log4j.RollingFileAppender
log4j.appender.WSClientAppender.File=/NotBackedUp/Downloads/CXFClientDemo/clientStuff/webserviceClient.log
log4j.appender.WSClientAppender.MaxFileSize=10MB
log4j.appender.WSClientAppender.MaxBackupIndex=3
log4j.appender.WSClientAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.WSClientAppender.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

NOTE: Make sure that you have defined the right Location for the Log File and the Directory must exist, which is defined in the above file property “log4j.appender.WSClientAppender.File”.

Step-6). Now we will write simple ANT build script “build.xml” which will build / deploy and run the webservice & client as following inside the directory “/home/userone/CXFClientSide_Logging_Demo”:

<project name="JBoss_Service" default="deploy">
<property name="jboss.home" value="/home/userone/jboss-as-7.1.0.CR1b" />
<property name="jboss.module.dir" value="${jboss.home}/modules" />

<property name="basedir" value="." />
<property name="war.dir" value="CXFClientLoggingDemoWAR" />
<property name="war.name" value="CXFClientLoggingDemo.war" />
<property name="src.dir" value="src" />
<property name="client.src.dir" value="${basedir}/clientSrc" />
<property name="output.dir" value="build" />
<property name="client.dir" value="${basedir}/clientStuff" />
<property name="client.jar.name" value="DemoCXFClient.jar" />
 
   <path id="jboss.classpath">
     <fileset dir="${jboss.module.dir}">
        <include name="**/*.jar"/>
     </fileset>  
   </path>

   <path id="client.classpath">
     <fileset dir="${jboss.module.dir}">
        <include name="**/*.jar"/>
     </fileset>  
     <fileset dir="${client.dir}">
        <include name="*.jar"/>
     </fileset>  
   </path>

   <taskdef name="wsprovide" classname="org.jboss.ws.tools.ant.WSProvideTask">
       <classpath refid="jboss.classpath"/>
   </taskdef>

   <taskdef name="wsconsume" classname="org.jboss.ws.tools.ant.WSConsumeTask">
       <classpath refid="jboss.classpath"/>
   </taskdef>

        <target name="init">
           <delete dir="${output.dir}" />
           <mkdir dir="${output.dir}" />
           <mkdir dir="${output.dir}/${war.dir}"/>
           <mkdir dir="${output.dir}/${war.dir}/META-INF"/>
           <mkdir dir="${output.dir}/${war.dir}/WEB-INF"/>
           <mkdir dir="${output.dir}/${war.dir}/WEB-INF/classes"/>
           <delete dir="${client.dir}" />
           <mkdir dir="${client.dir}/META-INF"/>
        </target>
	 
    <target name="build" depends="init">
       <javac srcdir="${src.dir}" destdir="${output.dir}/${war.dir}/WEB-INF/classes"  includes="*.java" excludes="Test_CXF_Client.java" classpathref="jboss.classpath"/>
        <copy todir="${output.dir}/${war.dir}/WEB-INF">
	  <fileset dir="${basedir}/src">
	      <include name="web.xml"/>
	  </fileset>
	</copy>   
       <wsprovide
        	fork="false"
        	keep="true"
        	destdir="${output.dir}"
        	resourcedestdir="${output.dir}/${war.dir}/WEB-INF/wsdl"
        	sourcedestdir="${output.dir}"
        	genwsdl="true" 
        	verbose="true"
        	sei="ws.DemoCXF">
            	<classpath>
                	  <pathelement path="${output.dir}/${war.dir}/WEB-INF/classes"/>
            	</classpath>
      </wsprovide>

      <jar jarfile="${output.dir}/${war.name}" basedir="${output.dir}/${war.dir}" compress="true" /> 
      
    </target>

        <target name="deploy" depends="build">
            <echo message="*******************  Deploying   *********************" />  
            <echo message="********** ${war.name} to ${jboss.home}/standalone/deployments **********" />  
            <copy file="${output.dir}/${war.name}" todir="${jboss.home}/standalone/deployments/"/>

            <echo message="*******************  Deployed Successfully   *********************" />  
        </target>
  
        <target name="post-deploy" >
            <echo message="*******************  NOTE  *********************" />
            <echo message="***** You should be able to access your WSDL using Browser now *****" />
            <echo message="                http://localhost:8080/CXFClientLoggingDemo?wsdl          " />
        </target>  

        <target name="client" depends="post-deploy">
           <delete dir="${client.dir}" />
           <mkdir dir="${client.dir}"/>             
             <wsconsume
                      fork="true"
                      keep="true"
                      destdir="${client.dir}"
                      sourcedestdir="${client.dir}"
                      package="client"
                      wsdlLocation="service.wsdl"
                      wsdl="http://localhost:8080/CXFClientLoggingDemo?wsdl">
            </wsconsume>


            <!-- We need to make sure that log4j.properties is present inside client's classpath-->
            <copy file="${src.dir}/log4j.properties" todir="${client.dir}" />

            <javac srcdir="${src.dir}" destdir="${client.dir}"  includes="Test_CXF_Client.java" classpathref="client.classpath">
	        <classpath>
	            <pathelement location="${client.dir}"/>
	            <path refid="client.classpath"/>
	        </classpath>
            </javac>

            <jar jarfile="${client.dir}/${client.jar.name}" basedir="${client.dir}" compress="true" /> 


            <!-- Cleaning Client Stuff Directory --> 
            <delete dir="${client.dir}/client"/> 
            <delete file="${client.dir}/log4j.properties"/> 
       </target>

        <target name="run" depends="client">
            <java classname="client.Test_CXF_Client" fork="true" >
	        <classpath>
	            <pathelement location="${client.dir}/DemoCXFClient.jar"/>
	            <path refid="client.classpath"/>
	        </classpath>
                <jvmarg value="-Dorg.apache.cxf.Logger=org.apache.cxf.common.logging.Log4jLogger" />
                <arg value="http://localhost:8080/CXFClientLoggingDemo"/>
            </java>
        </target>  

</project>

NOTE: The only change in the above file you need to do is to change the “jboss.home” directory path in the second line of the above script to point to your own JBoss AS7 directory.

Step-7). Now Start your JBoss Profile as following from inside the directory “/home/userone/jboss-as-7.1.0.CR1/bin”:

.
 ./standalone.sh -c standalone-full.xml
.

Step-8). Now before running your ANT script to build and deploy the above webapplication you should have the ANT as well as JAVA set in the $PATH variable of the Shell / command prompt as following:

For Unix Based OS:
export PATH=/home/userone/jdk1.6.0_21/bin:/home/userone/org.apache.ant_1.6.5/bin:$PATH

For Windows Based OS:
set PATH=C:/jdk1.6.0_21/bin;C:/org.apache.ant_1.6.5/bin;%PATH%

Step-9). Now once the PATH is set In the command/Shell prompt you can move inside the directory “/home/userone/CXFClientSide_Logging_Demo” and then run the ant to build and deploy the WebService application on your JBoss Standalone full profile, by running the command “ant deploy”

[userone@localhost CXFClientSide_Logging_Demo]$ ant deploy
Buildfile: build.xml

init:
   [delete] Deleting directory /home/userone/CXFClientSide_Logging_Demo/build
    [mkdir] Created dir: /home/userone/CXFClientSide_Logging_Demo/build
    [mkdir] Created dir: /home/userone/CXFClientSide_Logging_Demo/build/CXFClientLoggingDemoWAR
    [mkdir] Created dir: /home/userone/CXFClientSide_Logging_Demo/build/CXFClientLoggingDemoWAR/META-INF
    [mkdir] Created dir: /home/userone/CXFClientSide_Logging_Demo/build/CXFClientLoggingDemoWAR/WEB-INF
    [mkdir] Created dir: /home/userone/CXFClientSide_Logging_Demo/build/CXFClientLoggingDemoWAR/WEB-INF/classes
   [delete] Deleting directory /home/userone/CXFClientSide_Logging_Demo/clientStuff
    [mkdir] Created dir: /home/userone/CXFClientSide_Logging_Demo/clientStuff/META-INF

build:
    [javac] Compiling 1 source file to /home/userone/CXFClientSide_Logging_Demo/build/CXFClientLoggingDemoWAR/WEB-INF/classes
     [copy] Copying 1 file to /home/userone/CXFClientSide_Logging_Demo/build/CXFClientLoggingDemoWAR/WEB-INF
[wsprovide] Generating from endpoint: ws.DemoCXF
[wsprovide] log4j:WARN No appenders could be found for logger (org.apache.cxf.common.logging.LogUtils).
[wsprovide] log4j:WARN Please initialize the log4j system properly.
[wsprovide] log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
[wsprovide] java2ws -s /home/userone/CXFClientSide_Logging_Demo/build -classdir /home/userone/CXFClientSide_Logging_Demo/build -d /home/userone/CXFClientSide_Logging_Demo/build/CXFClientLoggingDemoWAR/WEB-INF/wsdl -verbose -wsdl -cp ::::::::: -wrapperbean -createxsdimports ws.DemoCXF
[wsprovide] java2ws - Apache CXF 2.4.4
[wsprovide] 
      [jar] Building jar: /home/userone/CXFClientSide_Logging_Demo/build/CXFClientLoggingDemo.war

deploy:
     [echo] *******************  Deploying   *********************
     [echo] ********** CXFClientLoggingDemo.war to /home/userone/jboss-as-7.1.0.CR1b/standalone/deployments **********
     [copy] Copying 1 file to /home/userone/jboss-as-7.1.0.CR1b/standalone/deployments
     [echo] *******************  Deployed Successfully   *********************

BUILD SUCCESSFUL
Total time: 6 seconds

As soon as the WebService will be deployed on the JBoss AS7 you will see the following kind of output on your JBoss Console (STDOUT):

12:55:32,698 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-7) Starting deployment of "CXFClientLoggingDemo.war"
12:55:32,953 INFO  [org.jboss.wsf.stack.cxf.metadata.MetadataBuilder] (MSC service thread 1-2) Add Service
 id=DemoCXF
 address=http://localhost:8080/CXFClientLoggingDemo
 implementor=ws.DemoCXF
 invoker=org.jboss.wsf.stack.cxf.JBossWSInvoker
 serviceName={http://test.org}DemoCXFService
 portName={http://test.org}DemoCXFPort
 wsdlLocation=null
 mtomEnabled=false
12:55:32,954 INFO  [org.jboss.ws.common.management.DefaultEndpointRegistry] (MSC service thread 1-2) register: jboss.ws:context=CXFClientLoggingDemo,endpoint=DemoCXF
12:55:33,174 INFO  [org.apache.cxf.service.factory.ReflectionServiceFactoryBean] (MSC service thread 1-2) Creating Service {http://test.org}DemoCXFService from class ws.DemoCXF
12:55:33,482 INFO  [org.apache.cxf.endpoint.ServerImpl] (MSC service thread 1-2) Setting the server's publish address to be http://localhost:8080/CXFClientLoggingDemo
12:55:33,605 INFO  [org.jboss.wsf.stack.cxf.deployment.WSDLFilePublisher] (MSC service thread 1-2) WSDL published to: file:/home/userone/jboss-as-7.1.0.CR1b/standalone/data/wsdl/CXFClientLoggingDemo.war/DemoCXFService.wsdl
12:55:33,613 INFO  [org.jboss.as.webservices] (MSC service thread 1-8) JBAS015539: Starting service jboss.ws.port-component-link
12:55:33,622 INFO  [org.jboss.as.webservices] (MSC service thread 1-3) JBAS015539: Starting service jboss.ws.endpoint."CXFClientLoggingDemo.war".DemoCXF
12:55:33,647 INFO  [org.jboss.web] (MSC service thread 1-7) registering web context: /CXFClientLoggingDemo
12:55:33,667 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 2) JBAS018559: Deployed "CXFClientLoggingDemo.war"

Step-10). Now we will try to compile and run the WebService Client application by running the command “ant run”

[userone@localhost CXFClientSide_Logging_Demo]$ ant run
Buildfile: build.xml

post-deploy:
     [echo] *******************  NOTE  *********************
     [echo] ***** You should be able to access your WSDL using Browser now *****
     [echo]                 http://localhost:8080/CXFClientLoggingDemo?wsdl          

client:
   [delete] Deleting directory /home/userone/CXFClientSide_Logging_Demo/clientStuff
    [mkdir] Created dir: /home/userone/CXFClientSide_Logging_Demo/clientStuff
[wsconsume] Consuming wsdl: http://localhost:8080/CXFClientLoggingDemo?wsdl
[wsconsume] log4j:WARN No appenders could be found for logger (org.apache.cxf.common.logging.LogUtils).
[wsconsume] log4j:WARN Please initialize the log4j system properly.
[wsconsume] log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
     [copy] Copying 1 file to /home/userone/CXFClientSide_Logging_Demo/clientStuff
    [javac] Compiling 1 source file to /home/userone/CXFClientSide_Logging_Demo/clientStuff
      [jar] Building jar: /home/userone/CXFClientSide_Logging_Demo/clientStuff/DemoCXFClient.jar
   [delete] Deleting directory /home/userone/CXFClientSide_Logging_Demo/clientStuff/client
   [delete] Deleting: /home/userone/CXFClientSide_Logging_Demo/clientStuff/log4j.properties

run:
      Feb 4, 2012 12:55:45 PM client.DemoCXFService <clinit>
      INFO: Can not initialize the default wsdl from service.wsdl
      
      	 port.sayHello("MiddlewareMagic") = Hello JBossAS7 User: MiddlewareMagic

BUILD SUCCESSFUL
Total time: 9 seconds

Step-11). Once the webservice is invoked successfully then you will be able to see the Client Side log file is generated in the location which is mentioned in the “log4j.properties” file…something like following:

12:55:46,418  INFO ReflectionServiceFactoryBean:366 - Creating Service {http://test.org}DemoCXFService from WSDL: http://localhost:8080/CXFClientLoggingDemo?wsdl
12:55:46,930  INFO LoggingOutInterceptor:151 - Outbound Message
---------------------------
ID: 1
Address: http://localhost:8080/CXFClientLoggingDemo
Encoding: UTF-8
Content-Type: text/xml
Headers: {Accept=[*/*], SOAPAction=[""]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHello xmlns:ns2="http://test.org"><arg0>MiddlewareMagic</arg0></ns2:sayHello></soap:Body></soap:Envelope>
--------------------------------------
12:55:47,061  INFO LoggingInInterceptor:151 - Inbound Message
----------------------------
ID: 1
Response-Code: 200
Encoding: UTF-8
Content-Type: text/xml;charset=UTF-8
Headers: {Content-Length=[235], content-type=, Date=[Sat, 04 Feb 2012 07:25:46 GMT], Server=[Apache-Coyote/1.1]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHelloResponse xmlns:ns2="http://test.org"><return>Hello JBossAS7 User: MiddlewareMagic</return></ns2:sayHelloResponse></soap:Body></soap:Envelope>
--------------------------------------

Enabling Logging On JBoss Server Side

You can add the following System Property (-Dorg.apache.cxf.logging.enabled=true) on JBoss Configuration like “standalone-full.xml” as following:

<server xmlns="urn:jboss:domain:1.3">

    <extensions>
        <extension module="org.jboss.as.clustering.infinispan"/>
        <extension module="org.jboss.as.cmp"/>
       .
       .
       .
        <extension module="org.jboss.as.webservices"/>
        <extension module="org.jboss.as.weld"/>
    </extensions>

    <system-properties>
        <property name="org.apache.cxf.logging.enabled" value="true"/>
    </system-properties>


    <management>
        <security-realms>
       .
       .
        </security-realms>
   </management>

.
.
Thanks 🙂
Middleware Magic Team


Copyright © 2010-2012 Middleware Magic. All rights reserved. |