Hi,

Jay SenSharma

Jay SenSharma

Here is a Very simple demostraton of sending an XML file  From Service Client to JAXRPC webservice which is deployed on WebLogic Server 10.3. We Can send any kind of file from Client to the WebService.

Step1). Create a Directory somewhere like : “F:DELETE25_March_2010SOAP_Attachment”

Step2). Create “src” direcroty inside “F:DELETE25_March_2010SOAP_Attachment”

Step3). Write the WebService Class: “AttachWSService.java” inside “src” directory

.

package service.attach;
import java.io.*;
import javax.activation.DataHandler;
import weblogic.jws.WLHttpTransport;
import javax.jws.soap.SOAPMessageHandlers;
import javax.jws.soap.SOAPMessageHandler;
import javax.jws.soap.SOAPBinding;
import javax.jws.WebService;
import javax.jws.WebMethod;
import weblogic.jws.StreamAttachments;
@WebService(name = "AttachWSPortType", serviceName = "AttachWSService")
@WLHttpTransport(contextPath = "attach", serviceUri = "AttachWSService", portName = "AttachWSPortType")
@SOAPMessageHandlers ({	@SOAPMessageHandler (className="service.attach.AttachmentHandler")})
@weblogic.jws.StreamAttachments
@SOAPBinding(style=SOAPBinding.Style.DOCUMENT,
use=SOAPBinding.Use.LITERAL,
parameterStyle=SOAPBinding.ParameterStyle.WRAPPED)
public class AttachWSService
{

@WebMethod
public String sendClaim(String filename, DataHandler claimForm)
{
System.out.println("AttachWSService t End Point: Received file for customer: " + filename);
System.out.println("AttachWSService t Content Type: " + claimForm.getContentType());
try {
int readBytes;
System.out.println("AttachWSService t Reading File...");
claimForm.writeTo(new FileOutputStream(filename+".CREATED"));
System.out.println("nnt Data Written In the File: "+filename+".CREATED");
}
catch (Exception e)
{
System.out.println ("I/O exception, " + e.getMessage());
}
return "End Point: Received file for customer: " + filename;
}
}

Step4).Now Write the SOAP Handler class “AttachmentHandler.java” inside “src” directory

package service.attach;
import java.io.IOException;
import java.util.Iterator;
import javax.xml.namespace.QName;
import javax.xml.rpc.handler.HandlerInfo;
import javax.xml.rpc.handler.GenericHandler;
import javax.xml.rpc.handler.MessageContext;
import javax.xml.rpc.handler.soap.SOAPMessageContext;
import javax.xml.rpc.JAXRPCException;
import javax.xml.soap.AttachmentPart;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
public class AttachmentHandler extends GenericHandler
{
 private HandlerInfo handlerInfo;

 public void init(HandlerInfo hi)
 {
System.out.println("nnt AttachmentHandler init() called...");
handlerInfo = hi;
 }

 public boolean handleRequest(MessageContext context)
 {
  SOAPMessageContext messageContext = (SOAPMessageContext) context;
  SOAPMessage request = messageContext.getMessage();
  if (request.countAttachments() == 0)
  {
 throw new JAXRPCException("AttachmentHandler:Expected at least one attachment");
 }
 Iterator it = request.getAttachments();
 System.out.println("AttachmentHandler:Request SOAP Message Begins");
 try {
 request.writeTo(System.out);
 } catch (SOAPException e) {
 e.printStackTrace();
 } catch (IOException e) {
 e.printStackTrace();
 }
 System.out.println("AttachmentHandler:Request SOAP Message Ends");
 try {
    while (it.hasNext())
  {
    AttachmentPart part = (AttachmentPart) it.next();
  System.out.println("AttachmentHandler:Received attachment");
  System.out.println("AttachmentHandler:Content Type: "+ part.getContentType());
  }
  }
  catch (Exception ex)
  {
   throw new JAXRPCException(ex);
  }
 return true;
 }

 public boolean handleResponse(MessageContext context)
 {
 SOAPMessageContext messageContext = (SOAPMessageContext) context;
 SOAPMessage response = messageContext.getMessage();
 System.out.println("AttachmentHandler:Response SOAP Message Begins");
 try
  {
 response.writeTo(System.out);
  }
 catch (SOAPException e)
  {
 e.printStackTrace();
  }
 catch (IOException e)
  {
 e.printStackTrace();
  }
 System.out.println("nAttachmentHandler:Response SOAP Message Ends");
 return true;
 }

 public boolean handleFault(MessageContext context)
  {
 SOAPMessageContext messageContext = (SOAPMessageContext) context;
 System.out.println("AttachmentHandler:Fault: "+ messageContext.getMessage().
toString());
 return true;
 }

 public QName[] getHeaders()
 {
 return handlerInfo.getHeaders();
 }
}

Step 5). Write the WebService Client “Main.java” inside “src” directory

package client;
import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException;
import javax.xml.rpc.Stub;
import javax.activation.DataHandler;
import javax.activation.*;
public class Main
{
 public static void main(String[] ar) throws ServiceException, RemoteException
 {
     String filename = ar[1];
     AttachWSService service = new AttachWSService_Impl(ar[0] + "?WSDL");
     AttachWSPortType port = service.getAttachWSPortType();
     System.out.println("Reading a file from ..." + filename);
     FileDataSource ds = new FileDataSource(filename);
     DataHandler dh = new DataHandler(ds);
     System.out.println("Calling service to send a file from a customer...");
     //port.sendClaim("FlightCrank.mp3",dh);
     String result = port.sendClaim(filename,dh);
     System.out.println(result);
     System.out.println("end client");
 }
}

Step6). Now Write the “build.xml” file inside “F:DELETE25_March_2010SOAP_Attachment” directory.

<project name="DwAttach" default="all">
 <!-- set global properties for this build -->
 <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" />
 <property name="ear.deploy.name" value="SOAPAttachmentEar" />
 <property name="client-output" value="clientArtifacts" />
 <property name="tempjar-dir" value="${client-output}/tempjardir" />
 <property name="ear.dir" value="${client-output}/SOAPAttachmentEar" />
 <property name="clientclass-dir" value="${client-output}/clientclasses" />

 <path id="client.class.path">
    <pathelement path="${clientclass-dir}"/>
    <pathelement path="${java.class.path}"/>
  <pathelement path="src"/>
 </path>

 <taskdef name="jwsc" classname="weblogic.wsee.tools.anttasks.JwscTask" />
 <taskdef name="clientgen" classname="weblogic.wsee.tools.anttasks.ClientGenTask" />

 <target name="all" depends="clean,build-service,deploy-attach,client,run" />
 <target name="clean" depends="undeploy-attach">
 <delete dir="${client-output}"/>
 </target>

 <target name="build-service">
  <javac srcdir="src" destdir="src" classpath="${client.class.path}" includes="*Attach*.java"/>
 <jwsc srcdir="src" destdir="${ear.dir}" debug="true" classpathref="client.class.path">
 <jws file="AttachWSService.java" type="JAXRPC" />
 </jwsc>
 <war destfile="${ear.dir}/AttachWSService.war" update="true">
  <classes dir="src/"/>
</war>
 </target>

 <target name="deploy-attach">
  <wldeploy action="deploy" name="${ear.deploy.name}"   source="${ear.dir}"
      user="${wls.username}"  password="${wls.password}"
    verbose="true"   adminurl="t3://${wls.hostname}:${wls.port}"
    targets="${wls.server.name}" />
  </target>

 <target name="undeploy-attach">
 <wldeploy action="undeploy" name="${ear.deploy.name}"   failonerror="false"
                 user="${wls.username}" password="${wls.password}" verbose="true"
                 adminurl="t3://${wls.hostname}:${wls.port}"  targets="${wls.server.name}" />
 </target>

 <target name="client">
 <clientgen  wsdl="http://${wls.hostname}:${wls.port}/attach/AttachWSService?WSDL"
                 destDir="${clientclass-dir}"  packageName="client"/>
                <javac   debug="true"  srcdir="${clientclass-dir}" destdir="${clientclass-dir}"
                             includes="**/*.java"/>
                 <javac  debug="true"  srcdir="client" destdir="${clientclass-dir}"
                            includes="client/**/*.java"/>
 </target>

 <target name="run">
  <javac srcdir="src" destdir="${clientclass-dir}" classpath="${client.class.path}" includes="Main.java"/>
   <java classname="client.Main"   fork="true"  failonerror="true">
 <classpath refid="client.class.path"/>
 <arg line="http://${wls.hostname}:${wls.port}/attach/AttachWSService    C:bea816registry.xml" />
 <!-- <jvmarg value="-Xms512M -Xmx512M" /> -->
 </java>
 </target>
</project>

NOTE: in the above ANT file when we are running the Client application you need to provide your own file name with absolute Path…
<arg line=”http://${wls.hostname}:${wls.port}/attach/AttachWSService    C:someLocationsendToWebService.xml” />
.
.
Thanks
Jay SenSharma
If you enjoyed this post, please considerleaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.