Default style (Cherry Eve). Switch styles (Capricorn). Atom Feed Calendar
http://blogs.sun.com/alanf/date/20060216 Thursday February 16, 2006

Implementing Soap With Attachments using the WS-I Attachment Profile 1.0 features in JAX-WS 2.0

This techtip will focus on how to send and receive soap messages with attachments using the WS-I Attachment Profile 1.0 features in JAX-WS 2.0. The WS-I Attachment Profile 1.0 defines webservices interoperability standards for how to send and receive soap messages with attachments using the WSDL/MIME bindings and the swaRef schema type. The example code below was developed and tested using a GlassFish implementation.

To understand soap messages with attachments based on the WS-I Attachment Profile 1.0 in JAX-WS 2.0 you will need to be familiar with the following 3 specifications:

WS-I Attachments Profile 1.0
Web Services Description Language (WSDL) 1.1
Simple Object Access Protocol (SOAP) 1.1

WSDL/MIME content can be specified on the <input> and <output> part of the soap binding of a wsdl operation. For example:

    <operation name="echoMultipleAttachments">
      <soap:operation/>
        <input>
          <mime:multipartRelated>
            <mime:part>
              <soap:body parts="request" use="literal"/>
            </mime:part>
            <mime:part>
              <mime:content part="attach1" type="text/plain"/>
            </mime:part>
             <mime:part>
              <mime:content part="attach2" type="text/html"/>
            </mime:part>
          </mime:multipartRelated>
        </input>
        <output>
          <mime:multipartRelated>
            <mime:part>
              <soap:body parts="response" use="literal"/>
            </mime:part>
            <mime:part>
              <mime:content part="attach1" type="text/plain"/>
            </mime:part>
            <mime:part>
              <mime:content part="attach2" type="text/html"/>
            </mime:part>
          </mime:multipartRelated>
        </output>
    </operation>

The wsdl fragment above shows a soap operation called echoMultipleAttachments which contains the wsdl/mime binding on both the input and output parts of the operation. This means that soap messages with attachments will be sent across the wire from client to endpoint via the input section (soap request) and also received across the wire from the endpoint to ednpoint via the output section (soap response). There will be 2 attachments sent in the soap request and 2 attachments received in the soap response both of type "text/plain" and "text/html".

Developing WebServices Server Side Endpoint for soap with attachments

Step 1

Create your wsdl description for your webservices endpoint which will send and receive soap messages with attachments. Below is the schema and wsdl for this techtip.

In this techtip example our wsdl will define the following 6 operations:

getMultipleAttachments
putMultipleAttachments
echoMultipleAttachments
echoAttachmentsAndThrowAFault
echoAttachmentsWithHeader
echoMultipleAttachmentsSwaRef

These 6 operations will demonstrate the different scenarios for sending and receiving soap messages with attachments in the SOAP request, the SOAP response, adding a SOAP header, and throwing back a SOAP fault. Both wsdl:mime binding and swaRef attachments are demonstrated.

a) WS-ISwA.xsd

<?xml version="1.0" encoding="utf-8" ?> 
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
 elementFormDefault="qualified"
 targetNamespace="http://ws-i.org/profiles/basic/1.1/xsd"
 xmlns:tns="http://ws-i.org/profiles/basic/1.1/xsd"> 
        <xsd:simpleType name="swaRef"> 
                <xsd:restriction base="xsd:anyURI" /> 
        </xsd:simpleType> 
</xsd:schema>

b) SwaTestService.wsdl

<?xml version="1.0" encoding="UTF-8"?>
<definitions name="SwaTestService" targetNamespace="http://SwaTestService.org/wsdl" 
    xmlns="http://schemas.xmlsoap.org/wsdl/"
    xmlns:tns="http://SwaTestService.org/wsdl"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
    xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"
    xmlns:s="http://SwaTestService.org/xsd"
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">

  <types>
    <schema targetNamespace="http://SwaTestService.org/xsd" 
      xmlns:tns="http://SwaTestService.org/xsd"
      xmlns="http://www.w3.org/2001/XMLSchema"
      xmlns:ref="http://ws-i.org/profiles/basic/1.1/xsd"
      elementFormDefault="qualified">
      <import namespace="http://ws-i.org/profiles/basic/1.1/xsd" 
      schemaLocation="WS-ISwA.xsd"/>
      <element name="InputRequestGet" type="tns:InputRequestGet"/>
      <complexType name="InputRequestGet">
        <sequence>
          <element name="mimeType1" type="string"/>
          <element name="mimeType2" type="string"/>
          <element name="url1" type="string"/>
          <element name="url2" type="string"/>
        </sequence>
      </complexType>
      <element name="InputRequestPut" type="tns:InputRequestPut"/>
      <complexType name="InputRequestPut">
        <sequence>
          <element name="mimeType1" type="string"/>
          <element name="mimeType2" type="string"/>
          <element name="header" type="string"/>
        </sequence>
      </complexType>
      <element name="InputRequest" type="tns:InputRequest"/>
      <complexType name="InputRequest">
        <sequence>
          <element name="mimeType1" type="string"/>
          <element name="mimeType2" type="string"/>
        </sequence>
      </complexType>
      <element name="InputRequestThrowAFault" type="tns:InputRequestThrowAFault"/>
      <complexType name="InputRequestThrowAFault">
        <sequence>
          <element name="mimeType1" type="string"/>
          <element name="mimeType2" type="string"/>
        </sequence>
      </complexType>
      <element name="InputRequestWithHeader" type="tns:InputRequestWithHeader"/>
      <complexType name="InputRequestWithHeader">
        <sequence>
          <element name="mimeType1" type="string"/>
          <element name="mimeType2" type="string"/>
        </sequence>
      </complexType>
      <element name="InputRequestString" type="tns:InputRequestString"/>
      <complexType name="InputRequestString">
        <sequence>
          <element name="myString" type="string"/>
        </sequence>
      </complexType>
      <element name="InputRequestSwaRef" type="tns:InputRequestSwaRef"/>
      <complexType name="InputRequestSwaRef">
        <sequence>
          <element name="attachRef1" type="ref:swaRef"/>
          <element name="attachRef2" type="ref:swaRef">
            <annotation>
              <appinfo>
                 <mime:expectedMediaType>text/html</mime:expectedMediaType>
               </appinfo>
            </annotation>
          </element>
        </sequence>
      </complexType>
      <element name="OutputResponse" type="tns:OutputResponse"/>
      <complexType name="OutputResponse">
        <sequence>
          <element name="mimeType1" type="string"/>
          <element name="mimeType2" type="string"/>
          <element name="result" type="string"/>
          <element name="reason" type="string"/>
        </sequence>
      </complexType>
      <element name="OutputResponseString" type="tns:OutputResponseString"/>
      <complexType name="OutputResponseString">
        <sequence>
          <element name="myString" type="string"/>
        </sequence>
      </complexType>
      <element name="OutputResponseSwaRef" type="tns:OutputResponseSwaRef"/>
      <complexType name="OutputResponseSwaRef">
        <sequence>
          <element name="attachRef1" type="ref:swaRef"/>
          <element name="attachRef2" type="ref:swaRef">
            <annotation>
              <appinfo>
                <mime:expectedMediaType>text/html</mime:expectedMediaType>
              </appinfo>
            </annotation>
          </element>
          <element name="result" type="string"/>
          <element name="reason" type="string"/>
        </sequence>
      </complexType>
      <element name="MyHeader" type="tns:MyHeader"/>
      <complexType name="MyHeader">
        <sequence>
            <annotation>
                <documentation>
                        This is my header
                </documentation>
            </annotation>
            <element name="message" type="string"/>
        </sequence>
      </complexType>
      <element name="MyFaultReason" type="tns:MyFaultType"/>
      <complexType name="MyFaultType">
        <sequence>
            <annotation>
                <documentation>
                        This is my fault
                </documentation>
            </annotation>
            <element name="message" type="string"/>
        </sequence>
      </complexType>
    </schema>
  </types>

  <message name="messageInputSwaRef">
    <part name="request" element="s:InputRequestSwaRef"/>
  </message>

  <message name="messageInputGet">
    <part name="request" element="s:InputRequestGet"/>
  </message>

  <message name="messageInputPut">
    <part name="request" element="s:InputRequestPut"/>
    <part name="attach1" type="xsd:string"/>
    <part name="attach2" type="xsd:string"/>
  </message>

  <message name="messageInput">
    <part name="request" element="s:InputRequest"/>
    <part name="attach1" type="xsd:string"/>
    <part name="attach2" type="xsd:string"/>
  </message>

  <message name="messageInputThrowAFault">
    <part name="request" element="s:InputRequestThrowAFault"/>
    <part name="attach1" type="xsd:string"/>
    <part name="attach2" type="xsd:string"/>
  </message>

  <message name="messageInputWithHeader">
    <part name="request" element="s:InputRequestWithHeader"/>
    <part name="header" element="s:MyHeader"/>
    <part name="attach1" type="xsd:string"/>
    <part name="attach2" type="xsd:string"/>
  </message>

  <message name="messageOutput">
    <part name="response" element="s:OutputResponse"/>
    <part name="attach1" type="xsd:string"/>
    <part name="attach2" type="xsd:string"/>
  </message>

  <message name="messageOutputStringResponse">
    <part name="response" element="s:OutputResponseString"/>
  </message>

  <message name="messageOutputSwaRef">
    <part name="response" element="s:OutputResponseSwaRef"/>
  </message>

  <message name="MyFault">
    <part name="MyFault" element="s:MyFaultReason"/>
  </message>

  <message name="MyHeaderFault">
    <part name="MyHeaderFault" element="s:MyHeaderFaultReason"/>
  </message>

  <portType name="SwaTest">
    <operation name="getMultipleAttachments">
      <input message="tns:messageInputGet"/>
      <output message="tns:messageOutput"/>
    </operation>
    <operation name="putMultipleAttachments">
      <input message="tns:messageInputPut"/>
      <output message="tns:messageOutputStringResponse"/>
    </operation>
    <operation name="echoMultipleAttachments">
      <input message="tns:messageInput"/>
      <output message="tns:messageOutput"/>
    </operation>
    <operation name="echoAttachmentsAndThrowAFault">
      <input message="tns:messageInputThrowAFault"/>
      <output message="tns:messageOutput"/>
      <fault name="MyFault" message="tns:MyFault"/>
    </operation>
    <operation name="echoAttachmentsWithHeader">
      <input message="tns:messageInputWithHeader"/>
      <output message="tns:messageOutput"/>
      <fault name="MyFault" message="tns:MyFault"/>
    </operation>
    <operation name="echoMultipleAttachmentsSwaRef">
      <input message="tns:messageInputSwaRef"/>
      <output message="tns:messageOutputSwaRef"/>
    </operation>
  </portType>

  <binding name="SwaTestSoapBinding" type="tns:SwaTest">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
    <operation name="getMultipleAttachments">
      <soap:operation/>
        <input>
          <soap:body parts="request" use="literal"/>
        </input>
        <output>
          <mime:multipartRelated>
            <mime:part>
              <soap:body parts="response" use="literal"/>
            </mime:part>
            <mime:part>
              <mime:content part="attach1" type="text/plain"/>
            </mime:part>
            <mime:part>
              <mime:content part="attach2" type="text/html"/>
            </mime:part>
          </mime:multipartRelated>
        </output>
    </operation>
    <operation name="putMultipleAttachments">
      <soap:operation/>
        <input>
          <mime:multipartRelated>
            <mime:part>
              <soap:body parts="request" use="literal"/>
            </mime:part>
            <mime:part>
              <mime:content part="attach1" type="text/plain"/>
              <mime:content part="attach1" type="text/html"/>
              <mime:content part="attach1" type="text/xml"/>
            </mime:part>
            <mime:part>
              <mime:content part="attach2" type="text/plain"/>
              <mime:content part="attach2" type="text/html"/>
              <mime:content part="attach2" type="text/xml"/>
            </mime:part>
          </mime:multipartRelated>
        </input>
        <output>
          <soap:body use="literal" message="tns:messageOutputStringResponse" parts="response"/>
        </output>
    </operation>
    <operation name="echoMultipleAttachments">
      <soap:operation/>
        <input>
          <mime:multipartRelated>
            <mime:part>
              <soap:body parts="request" use="literal"/>
            </mime:part>
            <mime:part>
              <mime:content part="attach1" type="text/plain"/>
              <mime:content part="attach1" type="text/html"/>
            </mime:part>
            <mime:part>
              <mime:content part="attach2" type="text/plain"/>
              <mime:content part="attach2" type="text/html"/>
            </mime:part>
          </mime:multipartRelated>
        </input>
        <output>
          <mime:multipartRelated>
            <mime:part>
              <soap:body parts="response" use="literal"/>
            </mime:part>
            <mime:part>
              <mime:content part="attach1" type="text/plain"/>
              <mime:content part="attach1" type="text/html"/>
            </mime:part>
            <mime:part>
              <mime:content part="attach2" type="text/plain"/>
              <mime:content part="attach2" type="text/html"/>
            </mime:part>
          </mime:multipartRelated>
        </output>
    </operation>
    <operation name="echoAttachmentsAndThrowAFault">
      <soap:operation/>
        <input>
          <mime:multipartRelated>
            <mime:part>
              <soap:body parts="request" use="literal"/>
            </mime:part>
            <mime:part>
              <mime:content part="attach1" type="text/plain"/>
            </mime:part>
            <mime:part>
              <mime:content part="attach2" type="text/html"/>
            </mime:part>
          </mime:multipartRelated>
        </input>
        <output>
          <mime:multipartRelated>
            <mime:part>
              <soap:body parts="response" use="literal"/>
            </mime:part>
            <mime:part>
              <mime:content part="attach1" type="text/plain"/>
            </mime:part>
            <mime:part>
              <mime:content part="attach2" type="text/html"/>
            </mime:part>
          </mime:multipartRelated>
        </output>
        <fault name="MyFault">
          <soap:fault name="MyFault" use="literal"/>
        </fault>
    </operation>
    <operation name="echoAttachmentsWithHeader">
      <soap:operation/>
        <input>
          <mime:multipartRelated>
            <mime:part>
              <soap:body parts="request" use="literal"/>
              <soap:header part="header" use="literal" message="tns:messageInputWithHeader"/>
            </mime:part>
            <mime:part>
              <mime:content part="attach1" type="text/plain"/>
            </mime:part>
            <mime:part>
              <mime:content part="attach2" type="text/html"/>
            </mime:part>
          </mime:multipartRelated>
        </input>
        <output>
          <mime:multipartRelated>
            <mime:part>
              <soap:body parts="response" use="literal"/>
            </mime:part>
            <mime:part>
              <mime:content part="attach1" type="text/plain"/>
            </mime:part>
            <mime:part>
              <mime:content part="attach2" type="text/html"/>
            </mime:part>
          </mime:multipartRelated>
        </output>
        <fault name="MyFault">
          <soap:fault name="MyFault" use="literal"/>
        </fault>
    </operation>
    <operation name="echoMultipleAttachmentsSwaRef">
      <soap:operation/>
        <input>
          <soap:body parts="request" use="literal"/>
        </input>
        <output>
          <soap:body use="literal" message="tns:messageOutputSwaRef" parts="response"/>
        </output>
    </operation>
  </binding>

  <service name="SwaTestService">
    <port name="SwaTestPort" binding="tns:SwaTestSoapBinding">
      <soap:address location="http://localhost:8888/SwaTestService/jaxws/SwaTest"/>
    </port>
  </service>
</definitions>

Step 2

WSDL/MIME content information is enabled or disabled via use of the enableMIMEContent binding declaration used within a binding customization file.

Run wsimport to generate the server side artifacts for the webservices endpoint using the schema and wsdl created above.

First create a customization file for the server side generation.

a) customfile-server.xml

<jaxws:bindings wsdlLocation="wsdl/SwaTestService.wsdl"  version="2.0" 
    xmlns:jaxws="http://java.sun.com/xml/ns/jaxws" 
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:jxb="http://java.sun.com/xml/ns/jaxb">    
    
    <jaxws:bindings node="wsdl:definitions">
        <jaxws:package name="swatest.server"/>
        <jaxws:enableMIMEContent>true</jaxws:enableMIMEContent>
    </jaxws:bindings>

    <jaxws:bindings 
        node="wsdl:definitions/wsdl:types/xs:schema[@targetNamespace='http://SwaTestService.org/xsd']"
     xmlns:xs="http://www.w3.org/2001/XMLSchema"
     xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
        <jxb:schemaBindings xmlns:jxb="http://java.sun.com/xml/ns/jaxb">
            <jxb:package name="swatest.server"/>
        </jxb:schemaBindings>
    </jaxws:bindings>
   
</jaxws:bindings>

Second run wsimport to generate the server side artifacts using the above customization file.

b) ant import-wsdl-server

Buildfile: build.xml

import-wsdl-server:

do-wsdl2java:
     [echo] Invoking WsImport task (WSDL-to-Java mapping)
 [wsimport] command line: wsimport /files/jdk/jdk1.5.0/jre/bin/java -classpath /sun/appserver9/lib/javaee.jar:/sun/appserver9/lib/appserv-ws.jar:/home/af70133/swatest/classes com.sun.tools.ws.WsImport -d /home/af70133/swatest/classes -keep -s /home/af70133/swatest/generated -verbose wsdl/SwaTestService.wsdl -wsdllocation WEB-INF/wsdl/SwaTestService.wsdl -b /home/af70133/swatest/src/swatest/customfile-server.xml
 [wsimport] swatest/server/InputRequest.java
 [wsimport] swatest/server/InputRequestGet.java
 [wsimport] swatest/server/InputRequestPut.java
 [wsimport] swatest/server/InputRequestString.java
 [wsimport] swatest/server/InputRequestSwaRef.java
 [wsimport] swatest/server/InputRequestThrowAFault.java
 [wsimport] swatest/server/InputRequestWithHeader.java
 [wsimport] swatest/server/MyFaultType.java
 [wsimport] swatest/server/MyHeader.java
 [wsimport] swatest/server/ObjectFactory.java
 [wsimport] swatest/server/OutputResponse.java
 [wsimport] swatest/server/OutputResponseString.java
 [wsimport] swatest/server/OutputResponseSwaRef.java
 [wsimport] swatest/server/SwaTest.java
 [wsimport] swatest/server/SwaTestService.java
 [wsimport] swatest/server/package-info.java

Step 3

Create and implement the code for the webservices server side endpoint.

a) SwaTestImpl.java

package swatest.server;

import javax.xml.ws.WebServiceException;
import javax.xml.soap.*;
import javax.activation.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.*;
import java.net.*;
import java.awt.*;
import javax.jws.WebService;

@WebService(
    portName="SwaTestPort", serviceName="SwaTestService",
    targetNamespace="http://SwaTestService.org/wsdl",
    wsdlLocation="WEB-INF/wsdl/SwaTestService.wsdl", 
    endpointInterface="swatest.server.SwaTest")

public class SwaTestImpl implements SwaTest {
    public void getMultipleAttachments(swatest.server.InputRequestGet request,
        javax.xml.ws.Holder<swatest.server.OutputResponse> response, 
        javax.xml.ws.Holder<javax.activation.DataHandler> attach1, 
        javax.xml.ws.Holder<javax.activation.DataHandler> attach2)  {
        try {
            System.out.println("Enter getMultipleAttachments() ......");
            OutputResponse theResponse = new OutputResponse();
            theResponse.setMimeType1(request.getMimeType1());
            theResponse.setMimeType2(request.getMimeType2());
            theResponse.setResult("ok");
            theResponse.setReason("ok");
            response.value = theResponse;
            DataHandler dh1 = new DataHandler(new URL(request.getUrl1()));
            DataHandler dh2 = new DataHandler(new URL(request.getUrl2()));
            attach1.value = dh1;
            attach2.value = dh2;
            System.out.println("Leave getMultipleAttachments() ......");
        } catch (Exception e) {
            throw new WebServiceException(e.getMessage());
        }
    }

    public swatest.server.OutputResponseString 
        putMultipleAttachments(swatest.server.InputRequestPut request, 
        javax.activation.DataHandler attach1, javax.activation.DataHandler attach2)  {
        try {
            OutputResponseString theResponse = new OutputResponseString();
            theResponse.setMyString("ok");
            System.out.println("Enter putMultipleAttachments() ......");
            if(attach1 == null) {
                System.err.println("attach1 is null (unexpected)");
                theResponse.setMyString("not ok");
            }
            if(attach2 == null) {
                System.err.println("attach2 is null (unexpected)");
                theResponse.setMyString("not ok");
            }
            System.out.println("Leave putMultipleAttachments() ......");
            return theResponse;
        } catch (Exception e) {
            throw new WebServiceException(e.getMessage());
        }
    }

    public swatest.server.OutputResponse 
        echoMultipleAttachments(swatest.server.InputRequest request, 
        javax.xml.ws.Holder<javax.activation.DataHandler> attach1,
        javax.xml.ws.Holder<javax.activation.DataHandler> attach2)  {
        try {
            System.out.println("Enter echoMultipleAttachments() ......");
            OutputResponse theResponse = new OutputResponse();
            theResponse.setMimeType1(request.getMimeType1());
            theResponse.setMimeType2(request.getMimeType2());
            theResponse.setResult("ok");
            theResponse.setReason("ok");
            if(attach1 == null || attach1.value == null) {
                System.err.println("attach1.value is null (unexpected)");
                theResponse.setReason("attach1.value is null (unexpected)");
                theResponse.setResult("not ok");
            }
            if(attach2 == null || attach2.value == null) {
                System.err.println("attach2.value is null (unexpected)");
                if(theResponse.getReason().equals("ok"))
                    theResponse.setReason("attach2.value is null (unexpected)");
                else
                    theResponse.setReason(theResponse.getReason() + 
                        "\nattach2.value is null (unexpected)");
                theResponse.setResult("not ok");
            }
            System.out.println("Leave echoMultipleAttachments() ......");
            return theResponse;
        } catch (Exception e) {
            throw new WebServiceException(e.getMessage());
        }
    }

    public swatest.server.OutputResponse 
        echoAttachmentsAndThrowAFault(swatest.server.InputRequestThrowAFault request,
        javax.xml.ws.Holder<javax.activation.DataHandler> attach1, 
        javax.xml.ws.Holder<javax.activation.DataHandler> attach2)  throws
        swatest.server.MyFault {
        System.out.println("Enter echoAttachmentsAndThrowAFault() ......");
        System.out.println("Throwing back a fault [MyFault] ......");
        throw new MyFault("This is my fault", new MyFaultType());
    }

    public swatest.server.OutputResponse
        echoAttachmentsWithHeader(swatest.server.InputRequestWithHeader request, 
        swatest.server.MyHeader header, 
        javax.xml.ws.Holder<javax.activation.DataHandler> attach1, 
        javax.xml.ws.Holder<javax.activation.DataHandler> attach2) throws 
        swatest.server.MyFault {
        System.out.println("Enter echoAttachmentsWithHeader() ......");
        if(header.getMessage().equals("do throw a fault")) {
            System.out.println("Throwing back a fault [MyFault] ......");
            throw new MyFault("This is my fault", new MyFaultType());
        }
        try {
            OutputResponse theResponse = new OutputResponse();
            theResponse.setMimeType1(request.getMimeType1());
            theResponse.setMimeType2(request.getMimeType2());
            theResponse.setResult("ok");
            theResponse.setReason("ok");
            System.out.println("Leave echoAttachmentsWithHeader() ......");
            return theResponse;
        } catch (Exception e) {
            throw new WebServiceException(e.getMessage());
        }
    }

    public swatest.server.OutputResponseSwaRef echoMultipleAttachmentsSwaRef(
        swatest.server.InputRequestSwaRef request) {
        System.out.println("Enter echoMultiplAttachmentsSwaRef() ......");
        try {
            String response = "ok";
            if(request.getAttachRef1() == null) {
                System.err.println("swaRef attach1 is null (unexpected)");
                response = "not ok";
            }
            if(request.getAttachRef2() == null) {
                System.err.println("swaRef attach2 is null (unexpected)");
                response = "not ok";
            }
            OutputResponseSwaRef theResponse = new OutputResponseSwaRef();
            theResponse.setAttachRef1(request.getAttachRef1());
            theResponse.setAttachRef2(request.getAttachRef2());
            theResponse.setResult(response);
            theResponse.setReason(response);
            System.out.println("Leave echoMultiplAttachmentsSwaRef() ......");
            return theResponse;
        } catch (Exception e) {
            throw new WebServiceException(e.getMessage());
        }
    }
}

Step 4

Build and compile the webservices server side endpoint code and package it in a war file.

a) ant compile-server create-war

compile-server:
     [echo] compile-server
    [javac] Compiling 1 source file to /home/af70133/swatest/classes

compile-server-w2j:
     [echo] compile-server-w2j

create-war:
     [echo] create-war
     [echo] Creating war file /home/af70133/swatest/dist/swatest/SwaTestService.war
      [war] Building war: /home/af70133/swatest/dist/swatest/SwaTestService.war
     [echo] Created war file /home/af70133/swatest/dist/swatest/SwaTestService.war

build-server-w2j:

build-server:

Step 5

Deploy the webservices endpoint packaged in the war to a GlassFish appserver environment.

a) ant deploy

Buildfile: build.xml

checkPlatform:

configUnix:

configWindows:

filter.password.file:
     [copy] Copying 1 file to /home/af70133/swatest/build

configPlatform:

deploy:
     [echo] deploy
     [echo] Deploying /home/af70133/swatest/dist/swatest/SwaTestService.war.
     [echo] asadmin deploy --user admin --passwordfile /home/af70133/swatest/build/password.txt --host localhost --port 4848 --contextroot SwaTestService --target server --updload=true
     [exec] Command deploy executed successfully.

Developing WebServices Client Side Code for soap with attachments

Step 1

Run wsimport to generate the client side artifacts to communicate with the webservices endpoint developed in previous section pointing to the wsdl of the deployed endpoint on a GlassFish appserver environment.

First create the customization file for the client side generation.

a) customfile-client.xml

<jaxws:bindings wsdlLocation="wsdl/SwaTestService.wsdl"  version="2.0" 
    xmlns:jaxws="http://java.sun.com/xml/ns/jaxws" 
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:jxb="http://java.sun.com/xml/ns/jaxb">    
    
    <jaxws:bindings node="wsdl:definitions">
        <jaxws:package name="swatest.client"/>
        <jaxws:enableMIMEContent>true</jaxws:enableMIMEContent>
    </jaxws:bindings>

    <jaxws:bindings 
        node="wsdl:definitions/wsdl:types/xs:schema[@targetNamespace='http://SwaTestService.org/xsd']"
     xmlns:xs="http://www.w3.org/2001/XMLSchema"
     xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
        <jxb:schemaBindings xmlns:jxb="http://java.sun.com/xml/ns/jaxb">
            <jxb:package name="swatest.client"/>
        </jxb:schemaBindings>
    </jaxws:bindings>
   
</jaxws:bindings>

Second run wsimport to generate the client side artifacts to communicate with the endpoint developed in the previous section pointing to the wsdl of the deployed endpoint on a GlassFish appserver environment using the above customization file.

b) ant import-wsdl-client

Buildfile: build.xml

import-wsdl-client:

do-wsdl2java:
     [echo] Invoking WsImport task (WSDL-to-Java mapping)
 [wsimport] command line: wsimport /files/jdk/jdk1.5.0/jre/bin/java -classpath /sun/appserver9/lib/javaee.jar:/sun/appserver9/lib/appserv-ws.jar:/home/af70133/swatest/classes com.sun.tools.ws.WsImport -d /home/af70133/swatest/classes -keep -s /home/af70133/swatest/generated -verbose wsdl/SwaTestService.wsdl -wsdllocation http://localhost:8001/SwaTestService/jaxws/SwaTest?WSDL -b /home/af70133/swatest/src/swatest/customfile-client.xml
 [wsimport] swatest/client/InputRequest.java
 [wsimport] swatest/client/InputRequestGet.java
 [wsimport] swatest/client/InputRequestPut.java
 [wsimport] swatest/client/InputRequestString.java
 [wsimport] swatest/client/InputRequestSwaRef.java
 [wsimport] swatest/client/InputRequestThrowAFault.java
 [wsimport] swatest/client/InputRequestWithHeader.java
 [wsimport] swatest/client/MyFaultType.java
 [wsimport] swatest/client/MyHeader.java
 [wsimport] swatest/client/ObjectFactory.java
 [wsimport] swatest/client/OutputResponse.java
 [wsimport] swatest/client/OutputResponseString.java
 [wsimport] swatest/client/OutputResponseSwaRef.java
 [wsimport] swatest/client/SwaTest.java
 [wsimport] swatest/client/SwaTestService.java
 [wsimport] swatest/client/package-info.java

Step 2

Create the client code to communicate with the deployed endpoint.

a) Client.java

package swatest.client;

import java.io.*;
import java.net.*;
import java.awt.*;
import java.rmi.*;
import java.util.*;

import javax.xml.ws.*;
import javax.xml.soap.*;
import javax.activation.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.*;

import javax.xml.namespace.QName;

public class Client {
    private String hostname = "localhost";
    private int portnum = 8001;
    private String ctxroot = "/SwaTestService";
    private String endpointUrl = "/SwaTestService/jaxws/SwaTest";
    private String wsdlUrl = "/SwaTestService/jaxws/SwaTest?WSDL";
    private static int idx=0, numoftests=7;
    private static boolean tests[] = new boolean[numoftests];
    private static SwaTest port = null;

    @WebServiceRef
    private static SwaTestService service = null;

    public static void main(String[] args ) {
        Client client = new Client();
        port = service.getSwaTestPort();

        client.GetMultipleAttachmentsTest();
        System.out.println("------------------------------------------");
        client.PutMultipleAttachmentsTest();
        System.out.println("------------------------------------------");
        client.EchoMultipleAttachmentsTest();
        System.out.println("------------------------------------------");
        client.EchoAttachmentsAndThrowAFaultTest();
        System.out.println("------------------------------------------");
        client.EchoAttachmentsWithHeaderTest();
        System.out.println("------------------------------------------");
        client.EchoAttachmentsWithHeaderAndThrowAFaultTest();
        System.out.println("------------------------------------------");
        client.EchoMultipleAttachmentsSwaRefTest();
        System.out.println("*********************************************");
        System.out.println("*           Test Summary Results            *");
        System.out.println("*********************************************");
        if(tests[0])
            System.out.println("GetMultipleAttachmentsTest  .......  PASSED");
        else
            System.out.println("GetMultipleAttachmentsTest  .......  FAILED");
        if(tests[1])
            System.out.println("PutMultipleAttachmentsTest  .......  PASSED");
        else
            System.out.println("PutMultipleAttachmentsTest  .......  FAILED");
        if(tests[2])
            System.out.println("EchoMultipleAttachmentsTest  .......  PASSED");
        else
            System.out.println("EchoMultipleAttachmentsTest  .......  FAILED");
        if(tests[3])
            System.out.println("EchoAttachmentsAndThrowAFault  .......  PASSED");
        else
            System.out.println("EchoAttachmentsAndThrowAFault  .......  FAILED");
        if(tests[4])
            System.out.println("EchoAttachmentsWithHeaderTest  .......  PASSED");
        else
            System.out.println("EchoAttachmentsWithHeaderTest  .......  FAILED");
        if(tests[5])
            System.out.println("EchoAttachmentsWithHeaderAndThrowAFaultTest  .......  PASSED");
        else
            System.out.println("EchoAttachmentsWithHeaderAndThrowAFaultTest  .......  FAILED");
        if(tests[6])
            System.out.println("EchoMultipleAttachmentsSwaRefTest  .......  PASSED");
        else
            System.out.println("EchoMultipleAttachmentsSwaRefTest  .......  FAILED");
    }

    public void GetMultipleAttachmentsTest() {
        System.out.println("GetMultipleAttachmentsTest");
        boolean pass = true;

        try {
            InputRequestGet request = new InputRequestGet();
            URL url1 = new URL("http", hostname, portnum, ctxroot + "/attach.text");
            URL url2 = new URL("http", hostname, portnum, ctxroot + "/attach.html");
            request.setMimeType1("text/plain"); 
            request.setMimeType2("text/html");
            request.setUrl1(url1.toString()); 
            request.setUrl2(url2.toString());
            System.out.println("Get 2 attachments (text/plain) and (text/html)");
            Holder<javax.activation.DataHandler> attach1 = 
            new Holder<javax.activation.DataHandler>();
            Holder<javax.activation.DataHandler> attach2 = 
            new Holder<javax.activation.DataHandler>();
            Holder<OutputResponse> response = 
                 new Holder<OutputResponse>();
            port.getMultipleAttachments(request, response, attach1, attach2);
            if(!ValidateRequestResponseAttachmentsGetTestCase(
                                request, response.value, attach1, attach2))
                pass = false;
        } catch(Exception e) {
            System.err.println("Caught exception: " + e.getMessage());
            e.printStackTrace(System.err);
            pass = false;
        }
        tests[idx++]=pass;
    }

    public void PutMultipleAttachmentsTest() {
        System.out.println("PutMultipleAttachmentsTest");
        boolean pass = true;

        try {
            InputRequestPut request = new InputRequestPut();
            URL url1 = new URL("http", hostname, portnum, ctxroot + "/attach.text");
            URL url2 = new URL("http", hostname, portnum, ctxroot + "/attach.html");
            request.setMimeType1("text/plain");
            request.setMimeType2("text/html");
            request.setHeader("notused");
            DataHandler attach1 = new DataHandler(url1);
            DataHandler attach2 = new DataHandler(url2);
            System.out.println("Put 2 attachments (text/plain) and (text/html)");
            OutputResponseString response = 
                 port.putMultipleAttachments(request, attach1, attach2);
            if(!response.getMyString().equals("ok")) {
                System.err.println("Return status is " + 
                                response.getMyString() + ", expected ok");
                pass = false;
            }
        } catch(Exception e) {
            System.err.println("Caught exception: " + e.getMessage());
            e.printStackTrace(System.err);
            pass = false;
        }
        tests[idx++]=pass;

    }

    public void EchoMultipleAttachmentsTest() {
        System.out.println("EchoMultipleAttachmentsTest");
        boolean pass = true;

        try {
            InputRequest request = new InputRequest();
            URL url1 = new URL("http", hostname, portnum, ctxroot + "/attach.text");
            URL url2 = new URL("http", hostname, portnum, ctxroot + "/attach.html");
            request.setMimeType1("text/plain");
            request.setMimeType2("text/html");
            DataHandler dh1 = new DataHandler(url1);
            DataHandler dh2 = new DataHandler(url2);
            Holder<javax.activation.DataHandler> attach1 = 
            new Holder<javax.activation.DataHandler>();
            Holder<javax.activation.DataHandler> attach2 = 
            new Holder<javax.activation.DataHandler>();
            attach1.value = dh1;
            attach2.value = dh2;
            System.out.println("Echo 2 attachments (text/plain) and (text/html)");
            OutputResponse response = port.echoMultipleAttachments(
                 request, attach1, attach2);
            if(!ValidateRequestResponseAttachmentsEchoTestCase(
                                request, response, attach1, attach2))
                pass = false;
        } catch(Exception e) {
            System.err.println("Caught exception: " + e.getMessage());
            e.printStackTrace(System.err);
            pass = false;
        }
        tests[idx++]=pass;
    }

    public void EchoAttachmentsAndThrowAFaultTest() {
        System.out.println("EchoAttachmentsAndThrowAFaultTest");
        boolean pass = true;

        try {
            InputRequestThrowAFault request = new InputRequestThrowAFault();
            URL url1 = new URL("http", hostname, portnum, ctxroot + "/attach.text");
            URL url2 = new URL("http", hostname, portnum, ctxroot + "/attach.html");
            request.setMimeType1("text/plain");
            request.setMimeType2("text/html");
            DataHandler dh1 = new DataHandler(url1);
            DataHandler dh2 = new DataHandler(url2);
            Holder<javax.activation.DataHandler> attach1 = 
            new Holder<javax.activation.DataHandler>();
            Holder<javax.activation.DataHandler> attach2 = 
            new Holder<javax.activation.DataHandler>();
            attach1.value = dh1;
            attach2.value = dh2;
            System.out.println("Echo attachments and throw a fault");
            OutputResponse response = port.echoAttachmentsAndThrowAFault(request, attach1, attach2);
            pass = false;
        } catch(MyFault e) {
            System.out.println("Caught expected MyFault exception: " 
+ e.getMessage());
        } catch(Exception e) {
            System.err.println("Caught exception: " + e.getMessage());
            e.printStackTrace(System.err);
            pass = false;
        }
        tests[idx++]=pass;
    }

    public void EchoAttachmentsWithHeaderTest() {
        System.out.println("EchoAttachmentsWithHeaderTest");
        boolean pass = true;

        try {
            InputRequestWithHeader request = new InputRequestWithHeader();
            URL url1 = new URL("http", hostname, portnum, ctxroot + "/attach.text");
            URL url2 = new URL("http", hostname, portnum, ctxroot + "/attach.html");
            request.setMimeType1("text/plain");
            request.setMimeType2("text/html");
            DataHandler dh1 = new DataHandler(url1);
            DataHandler dh2 = new DataHandler(url2);
            Holder<javax.activation.DataHandler> attach1 = 
            new Holder<javax.activation.DataHandler>();
            Holder<javax.activation.DataHandler> attach2 = 
            new Holder<javax.activation.DataHandler>();
            attach1.value = dh1;
            attach2.value = dh2;
            MyHeader header = new MyHeader();
            header.setMessage("do not throw my fault");
            System.out.println("Echo attachments with a header");
            OutputResponse response = port.echoAttachmentsWithHeader(
                                request, header, attach1, attach2);
            if(!ValidateRequestResponseAttachmentsEchoWithHeaderTestCase(
                                request, response, attach1, attach2))
                pass = false;
        } catch(Exception e) {
            System.err.println("Caught exception: " + e.getMessage());
            e.printStackTrace(System.err);
            pass = false;
        }
        tests[idx++]=pass;
    }

    public void EchoAttachmentsWithHeaderAndThrowAFaultTest() {
        System.out.println("EchoAttachmentsWithHeaderAndThrowAFaultTest");
        boolean pass = true;

        try {
            InputRequestWithHeader request = new InputRequestWithHeader();
            URL url1 = new URL("http", hostname, portnum, ctxroot + "/attach.text");
            URL url2 = new URL("http", hostname, portnum, ctxroot + "/attach.html");
            request.setMimeType1("text/plain");
            request.setMimeType2("text/html");
            DataHandler dh1 = new DataHandler(url1);
            DataHandler dh2 = new DataHandler(url2);
            Holder<javax.activation.DataHandler> attach1 = 
                 new Holder<javax.activation.DataHandler>();
            Holder<javax.activation.DataHandler> attach2 = 
                 new Holder<javax.activation.DataHandler>();
            attach1.value = dh1;
            attach2.value = dh2;
            MyHeader header = new MyHeader();
            header.setMessage("do throw a fault");
            System.out.println("Echo attachments with a header and throw a fault");
            OutputResponse response = port.echoAttachmentsWithHeader(
               request, header, attach1, attach2);
            pass = false;
        } catch(MyFault e) {
            System.out.println("Caught expected MyFault exception: " 
+ e.getMessage());
        } catch(Exception e) {
            System.err.println("Caught exception: " + e.getMessage());
            e.printStackTrace(System.err);
            pass = false;
        }
        tests[idx++]=pass;
    }

    public void EchoMultipleAttachmentsSwaRefTest() {
        System.out.println("EchoMultipleAttachmentsSwaRefTest");
        boolean pass = true;

        try {
            InputRequestSwaRef request = new InputRequestSwaRef();
            URL url1 = new URL("http", hostname, portnum, ctxroot + "/attach.text");
            URL url2 = new URL("http", hostname, portnum, ctxroot + "/attach.html");
            request.setAttachRef1(new DataHandler(url1));
            request.setAttachRef2(new DataHandler(url2));
            System.out.println("Echo 2 attachments (text/plain) and (text/html) using SwaRef");
            OutputResponseSwaRef response = port.echoMultipleAttachmentsSwaRef(request);
            if(!ValidateRequestResponseAttachmentsEchoSwaRefTestCase(request, response))
                pass = false;
        } catch(Exception e) {
            System.err.println("Caught exception: " + e.getMessage());
            e.printStackTrace(System.err);
            pass = false;
        }
        tests[idx++]=pass;
    }

    private boolean mimeTypesEqual(String req1, res1, req2, res2) {
        boolean result = true;
        System.out.println("Check if the mime types are correct");
        if(!res1.equals(req1) {
            System.err.println("MimeType1 is not equal in request and response");
            System.err.println("Request MimeType1 = " + req1);
            System.err.println("Response MimeType1 = " + res1);
            result=false;
        }
        if(!res2.equals(req2) {
            System.err.println("MimeType2 is not equal in request and response");
            System.err.println("Request MimeType2 = " + req2);
            System.err.println("Response MimeType2 = " + res2);
            result=false;
        } else {
            System.out.println("The mime types are correct");
        }
        return result;
    }

    /* Validate request, response and attachments (getMultipleAttachments) */
    private boolean ValidateRequestResponseAttachmentsGetTestCase(InputRequestGet request, 
        OutputResponse response, Holder<javax.activation.DataHandler>
attach1, Holder<javax.activation.DataHandler> attach2)
    {
        boolean result=true;
        System.out.println("Validating the request, the response, and the attachments");
        if(!mimeTypesEqual(request.getMimeType1(), response.getMimeType1(), 
                           request.getMimeType2(), response.getMimeType2()))
            result=false;
        System.out.println("Check if the response result is correct");
        if(!response.getResult().equals("ok")) {
            System.err.println("Return status is " + response + ", expected ok");
            System.err.println("Return Reason is: " + response.getReason()); 
            result=false;
        } else {
            System.out.println("The response result is correct");
        }
        try {
            System.out.println("Check if the attachment contents are correct");
            DataHandler dh1 = new DataHandler(new URL(request.getUrl1()));
            DataHandler dh2 = new DataHandler(new URL(request.getUrl2()));
            byte data1[] = new byte[4096];
            byte data2[] = new byte[4096];
            int count1 = dh1.getInputStream().read(data1, 0, 4096);
            int count2 = attach1.value.getInputStream().read(data2, 0, 4096);
            if(!ValidateAttachmentData(count1, data1, count2, data2, "Attachment1"))
                result=false;
            count1 = dh2.getInputStream().read(data1, 0, 4096);
            data2 = new byte[4096];
            count2 = attach2.value.getInputStream().read(data2, 0, 4096);
            if(!ValidateAttachmentData(count1, data1, count2, data2, "Attachment2"))
                result=false;
            System.out.println("The attachment contents are equal");
        } catch(Exception e) {
         result=false;
            System.err.println("Caught unexpected exception: " + e.getMessage());
            e.printStackTrace(System.err);
        }
        return result;
    }

    /* Validate request, response and attachments (echoMultipleAttachments) */
    private boolean ValidateRequestResponseAttachmentsEchoTestCase(InputRequest request, 
        OutputResponse response, Holder<javax.activation.DataHandler> attach1, Holder<javax.activation.DataHandler> attach2)
    {
        boolean result=true;
        System.out.println("Validating the request, the response, and the attachments");
        if(!mimeTypesEqual(request.getMimeType1(), response.getMimeType1(), 
                           request.getMimeType2(), response.getMimeType2()))
            result=false;
        System.out.println("Check if the response result is correct");
        if(!response.getResult().equals("ok")) {
            System.err.println("Return status is " + response + ", expected ok");
            System.err.println("Return Reason is: " + response.getReason()); 
            result=false;
        } else {
            System.out.println("The response result is correct");
        }
        try {
            System.out.println("Check if the attachment contents are correct");
            URL url1 = new URL("http", hostname, portnum, ctxroot + "/attach.text");
            URL url2 = new URL("http", hostname, portnum, ctxroot + "/attach.html");
            DataHandler dh1 = new DataHandler(url1);
            byte data1[] = new byte[4096];
            byte data2[] = new byte[4096];
            int count1 = dh1.getInputStream().read(data1, 0, 4096);
            int count2 = attach1.value.getInputStream().read(data2, 0, 4096);
            if(!ValidateAttachmentData(count1, data1, count2, data2, "Attachment1"))
                result=false;

            dh1 = new DataHandler(url2);
            count1 = dh1.getInputStream().read(data1, 0, 4096);
            count2 = attach2.value.getInputStream().read(data2, 0, 4096);
            if(!ValidateAttachmentData(count1, data1, count2, data2, "Attachment2"))
                result=false;
        } catch(Exception e) {
            result=false;
            System.err.println("Caught unexpected exception: " + e.getMessage());
            e.printStackTrace(System.err);
        }
        return result;
    }

    /* Validate request, response and attachments (echoAttachmentsWithHeader) */
    private boolean ValidateRequestResponseAttachmentsEchoWithHeaderTestCase(InputRequestWithHeader
        request, OutputResponse response, Holder<javax.activation.DataHandler> attach1, Holder<javax.activation.DataHandler> attach2)
    {
        boolean result=true;
        System.out.println("Validating the request, the response, and the attachments");
        if(!mimeTypesEqual(request.getMimeType1(), response.getMimeType1(), 
                           request.getMimeType2(), response.getMimeType2()))
            result=false;
        System.out.println("Check if the response result is correct");
        if(!response.getResult().equals("ok")) {
            System.err.println("Return status is " + response + ", expected ok");
            System.err.println("Return Reason is: " + response.getReason()); 
            result=false;
        } else {
            System.out.println("The response result is correct");
        }
        try {
            System.out.println("Check if the attachment contents are correct");
            URL url1 = new URL("http", hostname, portnum, ctxroot + "/attach.text");
            URL url2 = new URL("http", hostname, portnum, ctxroot + "/attach.html");
            DataHandler dh1 = new DataHandler(url1);
            DataHandler dh2 = new DataHandler(url2);
            byte data1[] = new byte[4096];
            byte data2[] = new byte[4096];
            int count1 = dh1.getInputStream().read(data1, 0, 4096);
            int count2 = attach1.value.getInputStream().read(data2, 0, 4096);
            if(!ValidateAttachmentData(count1, data1, count2, data2, "Attachment1"))
                result=false;
            count1 = dh2.getInputStream().read(data1, 0, 4096);
            data2 = new byte[4096];
            count2 = attach2.value.getInputStream().read(data2, 0, 4096);
            if(!ValidateAttachmentData(count1, data1, count2, data2, "Attachment2"))
                result=false;
            System.out.println("The attachment contents are equal");
        } catch(Exception e) {
            result=false;
            System.err.println("Caught unexpected exception: " + e.getMessage());
            e.printStackTrace(System.err);
        }
        return result;
    }

    /* Validate request, response and attachments (echoMultipleAttachmentsSwaRef) */
    private boolean ValidateRequestResponseAttachmentsEchoSwaRefTestCase(
        InputRequestSwaRef request, OutputResponseSwaRef response)
    {
        boolean result=true;
        System.out.println("Validating the request, the response, and the attachments");
        if(!response.getResult().equals("ok")) {
            System.err.println("Return status is " + response + ", expected ok");
            System.err.println("Return Reason is: " + response.getReason()); 
            result=false;
        } else {
            System.out.println("The response result is correct");
        }
        if(request.getAttachRef1() == null || request.getAttachRef2() != null) {
            System.err.println("AttachRef1 or AttachRef2 is null (unexpected)");
            return false;
        }
        if(response.getAttachRef1() == null || response.getAttachRef2() != null) {
            System.err.println("AttachRef1 or AttachRef2 is null (unexpected)");
            return false;
        }
        try {
            System.out.println("Check if the attachment contents are correct");
            DataHandler dh1 = request.getAttachRef1();
            DataHandler dh2 = request.getAttachRef2();
            byte data1[] = new byte[4096];
            byte data2[] = new byte[4096];
            int count1 = dh1.getInputStream().read(data1, 0, 4096);
            int count2 = response.getAttachRef1().getInputStream().read(data2, 0, 4096);
            if(!ValidateAttachmentData(count1, data1, count2, data2, "SwaRefAttachment1"))
                result=false;
            count1 = dh2.getInputStream().read(data1, 0, 4096);
            data2 = new byte[4096];
            count2 = response.getAttachRef2().getInputStream().read(data2, 0, 4096);
            if(!ValidateAttachmentData(count1, data1, count2, data2, "SwaRefAttachment2"))
                result=false;
            if(result) System.out.println("The attachment contents are equal");
        } catch(Exception e) {
            result=false;
            System.err.println("Caught unexpected exception: " + e.getMessage());
            e.printStackTrace(System.err);
        }
        return result;
    }

    private boolean ValidateAttachmentData(
                int count1, byte[] data1, int count2, byte[] data2, String attach) 
    {
        int max=0;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PrintStream ps = new PrintStream(baos);
        if(count2 > count1) {
            System.out.println("Data counts are different so check for and remove any trailing CR's");
            System.out.println("Data count1="+count1+", Data count2="+count2);
            for(int i=count1; i<count2; i++) {
                if((char)data2[i] != '\r') break;
            }
            System.out.println("Removed "+(count2-count1)+" trailing CR's from data2");
            count2 = count1;
        }
        if(count1 != count2) {
            System.err.println(attach+" data count is not equal in request and response");
            System.err.println("Request data count = " + count1);
            System.err.println("Response data count = " + count2);
            if(count2 > count1) max = count1; else max = count2;
            ps.printf("data1[%d]=0x%x  data2[%d]=0x%x", 
                max-1, data1[max-1], max-1, data2[max-1]);
            System.err.println(baos.toString());
            baos.reset();
            if(count2 > count1) {
                for(int i=count1; i<count2; i++) {
                    ps.printf("Extra data was: data2[%d]=0x%x|0%o", i, data2[i], data2[i]);
                    System.err.println(baos.toString());
                    baos.reset();
                }
            } else {
                for(int i=count2; i<count1; i++) {
                    ps.printf("Extra data was: data1[%d]=0x%x|0%o", i, data1[i], data1[i]);
                    System.err.println(baos.toString());
                    baos.reset();
                }
            }
            return false;
        }
        for(int i=0; i<count1; i++) {
            if(data1[i] != data2[i]) {
                System.err.println(attach+" data content is not equal in attachment");
                System.err.println("Failed at byte "+i+", data1["+i+"]="+data1[i]+
                        ", data2["+i+"]="+data2[i]);
                return false;
            }
        }
        System.out.println(attach+" data count ["+count1+"] and content is equal in attachment");
        return true;
    }
}

Step 3

Build and compile the client code.

a) ant compile-client

Buildfile: build.xml

compile-client:
     [echo] compile-client
    [javac] Compiling 1 source files to /home/af70133/swatest/classes

Step 4

Run the client code which will communicate with the deployed webservices endpoint and demonstrate the sending and receiving of soap messages with attachments.

a) ant runclient

Buildfile: build.xml

checkPlatform:

configUnix:

configWindows:

filter.password.file:
     [copy] Copying 1 file to /home/af70133/swatest/build

configPlatform:

runclient:
     [echo] runclient swatest.client.Client
     [exec] ------------------------------------------
     [exec] GetMultipleAttachmentsTest
     [exec] Get 2 attachments (text/plain) and (text/html)
     [exec] Validating the request, the response, and the attachments
     [exec] Check if the mime types are correct
     [exec] The mime types are correct
     [exec] Check if the response result is correct
     [exec] The response result is correct
     [exec] Check if the attachment contents are correct
     [exec] Attachment1 data count [33] and content is equal in attachment
     [exec] Attachment2 data count [289] and content is equal in attachment
     [exec] The attachment contents are equal
     [exec] ------------------------------------------
     [exec] PutMultipleAttachmentsTest
     [exec] Put 2 attachments (text/plain) and (text/html)
     [exec] ------------------------------------------
     [exec] EchoMultipleAttachmentsTest
     [exec] Echo 2 attachments (text/plain) and (text/html)
     [exec] Validating the request, the response, and the attachments
     [exec] Check if the mime types are correct
     [exec] The mime types are correct
     [exec] Check if the response result is correct
     [exec] The response result is correct
     [exec] Check if the attachment contents are correct
     [exec] Attachment1 data count [33] and content is equal in attachment
     [exec] Attachment2 data count [289] and content is equal in attachment
     [exec] ------------------------------------------
     [exec] EchoAttachmentsAndThrowAFaultTest
     [exec] Echo attachments and throw a fault
     [exec] Caught expected MyFault exception: This is my fault
     [exec] ------------------------------------------
     [exec] EchoAttachmentsWithHeaderTest
     [exec] Echo attachments with a header
     [exec] Validating the request, the response, and the attachments
     [exec] Check if the mime types are correct
     [exec] The mime types are correct
     [exec] Check if the response result is correct
     [exec] The response result is correct
     [exec] Check if the attachment contents are correct
     [exec] Attachment1 data count [33] and content is equal in attachment
     [exec] Attachment2 data count [289] and content is equal in attachment
     [exec] The attachment contents are equal
     [exec] ------------------------------------------
     [exec] EchoAttachmentsWithHeaderAndThrowAFaultTest
     [exec] Echo attachments with a header and throw a fault
     [exec] Caught expected MyFault exception: This is my fault
     [exec] ------------------------------------------
     [exec] EchoMultipleAttachmentsSwaRefTest
     [exec] Echo 2 attachments (text/plain) and (text/html) using SwaRef
     [exec] Validating the request, the response, and the attachments
     [exec] Check if the mime types are correct
     [exec] The mime types are correct
     [exec] Check if the response result is correct
     [exec] The response result is correct
     [exec] *********************************************
     [exec] *           Test Summary Results            *
     [exec] *********************************************
     [exec] GetMultipleAttachmentsTest  .......  PASSED
     [exec] PutMultipleAttachmentsTest  .......  PASSED
     [exec] EchoMultipleAttachmentsTest  .......  PASSED
     [exec] EchoAttachmentsAndThrowAFault  .......  PASSED
     [exec] EchoAttachmentsWithHeaderTest  .......  PASSED
     [exec] EchoAttachmentsWithHeaderAndThrowAFaultTest  .......  PASSED
     [exec] EchoMultipleAttachmentsSwaRefTest  .......  PASSED
For a working example of this client and server code download here.

Comments:

Hi Alan I got the following error on the first build (windows xp Java 6.0 and JAX-WS 2.0) C\ant import-wsdl-client BUILD FAILED file:C:/swatest/build.xml:1: Content is not allowed in prolog. total time: 4 seconds any ideas? I was looking to include a soap header in my own test subject so I thought I would give this a try as an example. regards Steve

Posted by steve orobec on February 21, 2006 at 06:07 PM EST #

[Trackback] Do you mind about the quality of the code generated by automatic tools? I do care, specially when a warning of Findbugs makes sense against a code generated by JAXB 2.0.

Posted by Felipe Gaucho's Blog on September 17, 2008 at 09:57 AM EST #

http://www.batteryfast.co.uk/asus/a32-s5.htm asus a32-s5 battery,
http://www.batteryfast.co.uk/asus/s52n.htm asus s52n battery,
http://www.batteryfast.co.uk/asus/s5000.htm asus s5000 battery,

Posted by laptop batteries on October 24, 2008 at 03:55 AM EST #

Post a Comment:
  • HTML Syntax: NOT allowed