JAX-WS WEBSERVICE

Using JAX-WS handlers on Weblogic 10.3

Create a JAX-WS class JaxWsImpl.java as mentioned below:

——————-

package demo;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.xml.ws.WebServiceContext;
import javax.annotation.Resource;
import javax.jws.HandlerChain;

@WebService(serviceName =”JaxWsImplService”,portName = “JaxWsImplPort”)
@HandlerChain(file=”handler-chain.xml”)

/**
* This JWS file forms the basis of simple JAX-WS WebLogic Web Service
*
*/

public class JaxWsImpl {

@Resource
private WebServiceContext context;
@WebMethod()
public String sayHello(String message) {
String principal = context.getUserPrincipal().getName();
System.out.println(“Hello! ” + message + ” principal: ” + principal + “.”);
return “message is : ‘” + message + “‘. principal is: ‘” + principal + “‘.” ;

}

}

———————————

Now create a Handler Class: SOAPLogHandler.java

package demo;

import java.util.Set;
import java.util.Collections;

import javax.xml.namespace.QName;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPException;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/*
* This simple SOAPHandler will output the SOAP Headers.
* @author Copyright (c) 2008 by BEA Systems, Inc. All Rights Reserved.
*/

public class SOAPLogHandler implements SOAPHandler<SOAPMessageContext> {

//lifecycle method invoked when this Handler is constructed.
@PostConstruct
void init() {
System.out.println(“SOAPHandler:\t” + this.getClass() + ” has been constructed”);
}

//lifecycle method  invoked this Handler is to be destroyed.
@PreDestroy
void destroy() {
System.out.println(“SOAPHandler:\t” + this.getClass() + ” will be destroyed”);

}

public Set<QName> getHeaders() {
return Collections.emptySet();
}

public boolean handleMessage(SOAPMessageContext smc) {
Boolean outboundProperty = (Boolean)smc.get (MessageContext.MESSAGE_OUTBOUND_PROPERTY);

if (outboundProperty.booleanValue()) {
System.out.println(“\nOutbound message:”);
} else {
System.out.println(“\nInbound message:”);
}

System.out.println(“** Response: “+smc.getMessage().toString());
return true;
}

public boolean handleFault(SOAPMessageContext smc) {

return true;
}

// nothing to clean up
public void close(MessageContext messageContext) {
}

private static void print(Source payload) {
try {
TransformerFactory factory = TransformerFactory.newInstance();
Transformer tf = factory.newTransformer();
tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, “yes”);
tf.setOutputProperty(OutputKeys.METHOD, “xml”);
tf.setOutputProperty(OutputKeys.INDENT, “yes”);
Result result = new StreamResult(System.out);
tf.transform(payload, result);
} catch (TransformerException e) {
e.printStackTrace();
}
}
}

———————————————–

Create a Handler-chain.xml file:

<?xml version=”1.0″ encoding=”UTF-8″?>
<handler-chains xmlns=”http://java.sun.com/xml/ns/javaee”>
<handler-chain>
<handler>
<handler-class>demo.SOAPLogHandler</handler-class>
</handler>
</handler-chain>

</handler-chains>

————————————————————————–

Use the below build.xml file to create the service:

<?xml version=”1.0″ encoding=”ISO-8859-1″?>
<project name=”JAXWS” default=”all” basedir=”.”>

<!– set global properties for this build –>
<property name=”source.dir” value=”${basedir}”/>
<property name=”build.dir” value=”${basedir}/service”/>
<property name=”service.name” value=”SampleHandle”/>
<property name=”wls.username” value=”weblogic”/>
<property name=”wls.password” value=”weblogic”/>
<property name=”wls.hostname” value=”localhost”/>
<property name=”wls.port” value=”7001″/>
<property name=”wls.server.name” value=”AdminServer”/>

<!– Web Service WLS Ant task definitions –>
<taskdef name=”jwsc” classname=”weblogic.wsee.tools.anttasks.JwscTask”/>
<taskdef name=”clientgen” classname=”weblogic.wsee.tools.anttasks.ClientGenTask”/>

<target name=”all” depends=”build, deploy”/>

<target name=”build” depends=”init,build.handler,build.service”/>

<target name=”init”>
<mkdir dir=”${build.dir}”/>
</target>

<target name=”clean” >
<delete dir=”${build.dir}”/>
<delete dir=”${source.dir}/demo”/>
</target>

<path id=”class.path”>
<pathelement path=”${source.dir}/demo/SOAPLogHandler.class”/>
<pathelement path=”${java.class.path}”/>
<pathelement path=”.;”/>
</path>

<!– Target that builds the target Web Service –>

<!– Target that compiles the handler class–>
<target name=”build.handler” description=”Target that  compiles the handler class”>
<javac srcdir=”${source.dir}” destdir=”${source.dir}” includes=”SOAPLogHandler.java”/>
</target>

<!– Target that builds the target Web Service –>
<target name=”build.service” description=”Target that builds the target Web Service”>
<property name=”myclasspath” refid=”class.path”/>
<echo message=”${myclasspath}”/>

<jwsc
srcdir=”${source.dir}”
destdir=”${build.dir}”
keepGenerated=”true”
classpath=”${class.path}”
debug=”true”
verbose=”false”>
<jws file=”JaxWsImpl.java” type=”JAXWS” explode=”true”/>
<!– <module contextPath=”JaxWsImpl” name=”JaxWsImplServiceWebApp” explode=”true”>
</module>–>

</jwsc>
<copy todir=”${build.dir}\JaxWsImpl\WEB-INF\classes\demo”>
<fileset dir=”${source.dir}\demo” includes=”*Handler*.class”></fileset>
<fileset dir=”${source.dir}” includes=”handler-chain.xml”></fileset>
</copy>

</target>

<target name=”deploy”>
<wldeploy
action=”deploy”
verbose=”true”
debug=”true”
name=”${service.name}”
source=”${build.dir}”
user=”${wls.username}”
password=”${wls.password}”
adminurl=”t3://${wls.hostname}:${wls.port}”
targets=”${wls.server.name}”
failonerror=”true”/>
</target>

<target name=”undeploy”>
<wldeploy
action=”undeploy”
verbose=”true”
debug=”true”
name=”${service.name}”
user=”${wls.username}”
password=”${wls.password}”
adminurl=”t3://${wls.hostname}:${wls.port}”
targets=”${wls.server.name}”
failonerror=”false”/>
</target>

</project>

—————————————-

Steps:

1: Create a source directory say handler and put all the four files:

JaxWsImpl.java

SOAPLogHandler.java

handler-chain.xml

build.xml in that handler dir.

2: on a command prompt run the setDomainEnv.cmd file present in the Weblogic server installation dir at the below mentioned location:

%BEA_HOME%/user_projects/domains/your_domain/bin dir

This is required to set up the environment.

3: Run the Task as below one by one from the command prompt from the handler dir you created above:

ant init

ant build.handler

ant build.service

ant deploy

4: now you test the deployed SampleHandle service from the WLS Admin Console using the testing tab:

You can in the server stdout the messages printed by the handler classes.

5: In order to debug webservice related issues on WLS you use the following flag in the server startup script as a JAVA_OPTION:

-Dweblogic.wsee.verbose=*

Leave a comment