Default style (Cherry Eve). Switch styles (Capricorn). Atom Feed Calendar
http://blogs.sun.com/alanf/date/20060719 Wednesday July 19, 2006

JAX-WS 2.0 example illustrating Soap Messages with Headers and generating Soap Faults and Soap Header Faults

This techtip will focus on how to send and receive headers in soap messages and how to generate and throw soap faults and soap header faults.

Step 1

Create your wsdl description for your webservices endpoint which will send and receive headers in soap messagesand generate and throw soap faults and soap header faults.

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

GoodOrderTestWithSoapHeaderAndMUFalse
GoodOrderTestWithSoapHeaderAndMUTrue
SoapHeaderFaultTest
SoapFaultTest

These 4 operations will demonstrate the sending and receiving of headers in soap messages and generating and throwing back a soap fault and a soap header fault.

a) HeaderTestDefs.xsd

<?xml version="1.0" encoding="UTF-8"?>
<schema
    targetNamespace="http://headertestservice.org/types4"
    xmlns:tns="http://headertestservice.org/types4"
    xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns="http://www.w3.org/2001/XMLSchema">

    <import namespace="http://schemas.xmlsoap.org/soap/envelope/"
             schemaLocation="soap-env.xsd"/>

    <complexType name="ProductOrderRequest">
        <sequence>
            <element name="item" type="tns:ProductOrderItem" maxOccurs="unbounded"/>
            <element name="customerInfo" type="tns:CustomerInfo" minOccurs="1" maxOccurs="1"/>
        </sequence>
    </complexType>

    <complexType name="ProductOrderItem">
        <sequence>
            <element name="productName" type="string" minOccurs="1" maxOccurs="1"/>
            <element name="productCode" type="tns:ProductCode" minOccurs="1" maxOccurs="1"/>
            <element name="quantity" type="int" minOccurs="1" maxOccurs="1"/>
            <element name="price" type="decimal" minOccurs="1" maxOccurs="1"/>
        </sequence>
    </complexType>

    <complexType name="ProductOrderResponse">
        <sequence>
            <element name="item" type="tns:ProductOrderItem" maxOccurs="unbounded"/>
        </sequence>
    </complexType>

    <complexType name="CustomerInfo">
        <sequence>
            <element name="creditcard" type="string" minOccurs="1" maxOccurs="1"/>
            <element name="name" type="string" minOccurs="1" maxOccurs="1"/>
            <element name="street" type="string" minOccurs="1" maxOccurs="1"/>
            <element name="city" type="string" minOccurs="1" maxOccurs="1"/>
            <element name="state" type="string" minOccurs="1" maxOccurs="1"/>
            <element name="zip" type="string" minOccurs="1" maxOccurs="1"/>
            <element name="country" type="string" minOccurs="1" maxOccurs="1"/>
        </sequence>
    </complexType>

    <simpleType name="ProductCode">
        <restriction base="integer">
            <minInclusive value="1"/>
            <maxInclusive value="99999999999999999999999999999999999999999999999"/>
        </restriction>
    </simpleType>

    <element name="BadOrderFaultReason" type="tns:BadOrderFaultType"/>
    <complexType name="BadOrderFaultType">
        <sequence>
            <element name="message" type="string"/>
        </sequence>
    </complexType>

    <element name="ConfigHeaderRequest" type="tns:ConfigHeader"/>
    <complexType name="ConfigHeader">
        <sequence>
            <annotation>
                <documentation>
                        This is the configuration header
                </documentation>
            </annotation>
            <element name="message" type="string"/>
            <element name="testName" type="string"/>
        </sequence>
        <attribute ref="env:mustUnderstand"/>
    </complexType>

    <element name="ConfigFaultReason" type="tns:ConfigFaultType"/>
    <complexType name="ConfigFaultType">
        <sequence>
            <annotation>
                <documentation>
                        This is the configuration fault
                </documentation>
            </annotation>
            <element name="message" type="string"/>
        </sequence>
        <attribute ref="env:mustUnderstand"/>
    </complexType>
</schema>

b) soap-env.xsd

<?xml version='1.0' encoding='UTF-8' ?>
<!-- Schema for the SOAP/1.1 envelope

     This schema has been produced using W3C's SOAP Version 1.2 schema
     found at:

     http://www.w3.org/2001/06/soap-envelope

     Copyright 2001 Martin Gudgin, Developmentor.

     Changes made are the following:
     - reverted namespace to http://schemas.xmlsoap.org/soap/envelope/
     - reverted mustUnderstand to only allow 0 and 1 as lexical values
         - made encodingStyle a global attribute 20020825

     Original copyright:
     
     Copyright 2001 W3C (Massachusetts Institute of Technology,
     Institut National de Recherche en Informatique et en Automatique,
     Keio University). All Rights Reserved.
     http://www.w3.org/Consortium/Legal/

     This document is governed by the W3C Software License [1] as
     described in the FAQ [2].

     [1] http://www.w3.org/Consortium/Legal/copyright-software-19980720
     [2] http://www.w3.org/Consortium/Legal/IPR-FAQ-20000620.html#DTD
-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:tns="http://schemas.xmlsoap.org/soap/envelope/"
           targetNamespace="http://schemas.xmlsoap.org/soap/envelope/" >

     
  <!-- Envelope, header and body -->
<!-- COMMENT OUT FOR NOW (AEF) FUNCTIONALITY NOT AVAILABLE YET
  <xs:element name="Envelope" type="tns:Envelope" />
  <xs:complexType name="Envelope" >
    <xs:sequence>
      <xs:element ref="tns:Header" minOccurs="0" />
      <xs:element ref="tns:Body" minOccurs="1" />
      <xs:any namespace="##other" minOccurs="0" maxOccurs="unbounded" processContents="lax" />
    </xs:sequence>
    <xs:anyAttribute namespace="##other" processContents="lax" />
  </xs:complexType>

  <xs:element name="Header" type="tns:Header" />
  <xs:complexType name="Header" >
    <xs:sequence>
      <xs:any namespace="##other" minOccurs="0" maxOccurs="unbounded" processContents="lax" />
    </xs:sequence>
    <xs:anyAttribute namespace="##other" processContents="lax" />
  </xs:complexType>
  
  <xs:element name="Body" type="tns:Body" />
  <xs:complexType name="Body" >
    <xs:sequence>
      <xs:any namespace="##any" minOccurs="0" maxOccurs="unbounded" processContents="lax" />
    </xs:sequence>
    <xs:anyAttribute namespace="##any" processContents="lax" >
          <xs:annotation>
            <xs:documentation>
                  Prose in the spec does not specify that attributes are allowed on the Body element
                </xs:documentation>
          </xs:annotation>
        </xs:anyAttribute>
  </xs:complexType>
-->

       
  <!-- Global Attributes.  The following attributes are intended to be usable via qualified attribute names on any complex type referencing them.  -->
  <xs:attribute name="mustUnderstand" default="false" >
     <xs:simpleType>
     <xs:restriction base='xs:boolean'>
           <xs:pattern value='false|true' />
         </xs:restriction>
   </xs:simpleType>
  </xs:attribute>
  <xs:attribute name="actor" type="xs:anyURI" />

<!-- COMMENT OUT FOR NOW (AEF) FUNCTIONALITY NOT AVAILABLE YET
  <xs:simpleType name="encodingStyle" >
    <xs:annotation>
          <xs:documentation>
            'encodingStyle' indicates any canonicalization conventions followed in the contents of the containing element.  For example, the value 'http://schemas.xmlsoap.org/soap/encoding/' indicates the pattern described in SOAP specification
          </xs:documentation>
        </xs:annotation>
    <xs:list itemType="xs:anyURI" />
  </xs:simpleType>
<?xml version='1.0' encoding='UTF-8' ?>

<!-- Schema for the SOAP/1.1 envelope

     This schema has been produced using W3C's SOAP Version 1.2 schema
     found at:

     http://www.w3.org/2001/06/soap-envelope

     Copyright 2001 Martin Gudgin, Developmentor.

     Changes made are the following:
     - reverted namespace to http://schemas.xmlsoap.org/soap/envelope/
     - reverted mustUnderstand to only allow 0 and 1 as lexical values
  </xs:complexType>

  <xs:element name="Header" type="tns:Header" />
  <xs:complexType name="Header" >
    <xs:sequence>
      <xs:any namespace="##other" minOccurs="0" maxOccurs="unbounded" processContents="lax" />
    </xs:sequence>
    <xs:anyAttribute namespace="##other" processContents="lax" />
  </xs:complexType>

  <xs:element name="Body" type="tns:Body" />
  <xs:complexType name="Body" >
    <xs:sequence>
      <xs:any namespace="##any" minOccurs="0" maxOccurs="unbounded" processContents="lax" />
    </xs:sequence>
    <xs:anyAttribute namespace="##any" processContents="lax" >
          <xs:annotation>
            <xs:documentation>
                  Prose in the spec does not specify that attributes are allowed on the Body element
                </xs:documentation>
          </xs:annotation>
        </xs:anyAttribute>
  </xs:complexType>
-->


  <!-- Global Attributes.  The following attributes are intended to be usable via qualified attribute names on an
y complex type referencing them.  -->
  <xs:attribute name="mustUnderstand" default="false" >
     <xs:simpleType>
     <xs:restriction base='xs:boolean'>
           <xs:pattern value='false|true' />
         </xs:restriction>
   </xs:simpleType>
  </xs:attribute>
  <xs:attribute name="actor" type="xs:anyURI" />

<!-- COMMENT OUT FOR NOW (AEF) FUNCTIONALITY NOT AVAILABLE YET
  <xs:simpleType name="encodingStyle" >
    <xs:annotation>
          <xs:documentation>
            'encodingStyle' indicates any canonicalization conventions followed in the contents of the containing
 element.  For example, the value 'http://schemas.xmlsoap.org/soap/encoding/' indicates the pattern described in
SOAP specification
          </xs:documentation>
        </xs:annotation>
    <xs:list itemType="xs:anyURI" />
  </xs:simpleType>

  <xs:attribute name="encodingStyle" type="tns:encodingStyle" />
  <xs:attributeGroup name="encodingStyle" >
    <xs:attribute ref="tns:encodingStyle" />
  </xs:attributeGroup>

  <xs:element name="Fault" type="tns:Fault" />
  <xs:complexType name="Fault" final="extension" >
    <xs:annotation>
          <xs:documentation>
            Fault reporting structure
          </xs:documentation>
        </xs:annotation>
    <xs:sequence>
      <xs:element name="faultcode" type="xs:QName" />
      <xs:element name="faultstring" type="xs:string" />
      <xs:element name="faultactor" type="xs:anyURI" minOccurs="0" />
      <xs:element name="detail" type="tns:detail" minOccurs="0" />
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="detail">
    <xs:sequence>
      <xs:any namespace="##any" minOccurs="0" maxOccurs="unbounded" processContents="lax" />
    </xs:sequence>
    <xs:anyAttribute namespace="##any" processContents="lax" />
  </xs:complexType>
-->
</xs:schema>

c) HeaderTestService.wsdl

<?xml version="1.0" encoding="UTF-8"?>
<definitions
    name="HeaderTestService"
    targetNamespace="http://headertestservice.org/HeaderTestService.wsdl"
    xmlns:mts1="http://headertestservice.org/HeaderTestService.wsdl"
    xmlns="http://schemas.xmlsoap.org/wsdl/"
    xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">

  <import
     namespace="http://headertestservice.org/HeaderTestService.wsdl"
     location="HeaderTestDefs.wsdl"/>

  <types/>

  <service name="HeaderTestService">
    <port name="HeaderTestPort" binding="mts1:HeaderTestSoapBinding">
      <soap:address location="http://localhost:8080/WSW2JRLHeaderTest/jaxws/HeaderTest"/>
    </port>
  </service>
</definitions>

d) HeaderTestDefs.wsdl

<?xml version="1.0" encoding="utf-8"?>
<definitions
        name="HeaderTestDefs"
        targetNamespace="http://headertestservice.org/HeaderTestService.wsdl"
        xmlns:tns="http://headertestservice.org/HeaderTestService.wsdl"
        xmlns="http://schemas.xmlsoap.org/wsdl/"
        xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
        xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        xmlns:ns4="http://headertestservice.org/types4">

  <types>
    <xsd:schema elementFormDefault="qualified" attributeFormDefault="unqualified">
        <xsd:import namespace="http://headertestservice.org/types4" schemaLocation="HeaderTestDefs.xsd"/>
        </xsd:schema>
  </types>

  <message name="SubmitOrderRequest">
    <part name="ProductOrderRequest" type="ns4:ProductOrderRequest"/>
    <part name="ConfigHeader" element="ns4:ConfigHeaderRequest"/>
  </message>

  <message name="SubmitOrderResponse">
    <part name="ProductOrderResponse" type="ns4:ProductOrderResponse"/>
  </message>

  <message name="BadOrderFault">
    <part name="Reason" element="ns4:BadOrderFaultReason"/>
  </message>

  <message name="ConfigFault">
    <part name="ConfigFault" element="ns4:ConfigFaultReason"/>
  </message>

  <portType name="HeaderTest">
    <operation name="submitOrder">
      <input message="tns:SubmitOrderRequest"/>
      <output message="tns:SubmitOrderResponse"/>
      <fault name="BadOrderFault" message="tns:BadOrderFault"/>
      <fault name="ConfigFault" message="tns:ConfigFault"/>
    </operation>
  </portType>

  <binding name="HeaderTestSoapBinding" type="tns:HeaderTest">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc"/> 
      <operation name="submitOrder">
        <soap:operation soapAction=""/>
        <input>
          <soap:body use="literal" parts="ProductOrderRequest" namespace="http://headertestservice.org/HeaderTestService.wsdl"/>
          <soap:header message="tns:SubmitOrderRequest" part="ConfigHeader" use="literal"/>
        </input>
        <output>
          <soap:body use="literal" namespace="http://headertestservice.org/HeaderTestService.wsdl"/>
        </output>
        <fault name="BadOrderFault">
          <soap:fault name="BadOrderFault" use="literal"/>
        </fault>
        <fault name="ConfigFault">
          <soap:fault name="ConfigFault" use="literal"/>
        </fault>
      </operation>
  </binding>
</definitions>

Step 2

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 and customfile2-server.xml

<?xml version="1.0" encoding="UTF-8"?>
<bindings wsdlLocation="wsdl/HeaderTestService.wsdl" xmlns="http://java.sun.com/xml/ns/jaxws">    

    <bindings node="wsdl:definitions" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
        <package name="headertest.server"/>
    </bindings>

</bindings>

<jxb:bindings version="1.0"
              xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
              xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <jxb:bindings schemaLocation="wsdl/HeaderTestDefs.xsd" node="/xs:schema">
        <jxb:schemaBindings xmlns:jxb="http://java.sun.com/xml/ns/jaxb">
            <jxb:package name="headertest.server"/>
        </jxb:schemaBindings>
    </jxb:bindings>

</jxb:bindings>

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

b) ant import-wsdl-server

import-wsdl-server:

init:
    [mkdir] Created dir: /home/af70133/netbeans/nbprojects/HeaderTest/classes
    [mkdir] Created dir: /home/af70133/netbeans/nbprojects/HeaderTest/dist
    [mkdir] Created dir: /home/af70133/netbeans/nbprojects/HeaderTest/generated

do-wsdl2java:
     [echo] Invoking WsImport task (WSDL-to-Java mapping)
     [echo] wsdlLocation=/home/af70133/netbeans/nbprojects/HeaderTest/conf/wsdl/HeaderTestService.wsdl
     [echo] wsdl=/home/af70133/netbeans/nbprojects/HeaderTest/conf/wsdl/HeaderTestService.wsdl
 [wsimport] command line: wsimport /files/jdk/jdk1.5.0/jre/bin/java -classpath /sun/appserver9/lib/activation.jar:/sun/appserver9/lib/javaee.jar:/sun/appserver9/lib/appserv-ws.jar com.sun.tools.ws.WsImport -d /home/af70133/netbeans/nbprojects/HeaderTest/classes -keep -s /home/af70133/netbeans/nbprojects/HeaderTest/generated -verbose /home/af70133/netbeans/nbprojects/HeaderTest/conf/wsdl/HeaderTestService.wsdl -wsdllocation /home/af70133/netbeans/nbprojects/HeaderTest/conf/wsdl/HeaderTestService.wsdl -b /home/af70133/netbeans/nbprojects/HeaderTest/conf/customfile2-server.xml -b /home/af70133/netbeans/nbprojects/HeaderTest/conf/customfile-server.xml
 [wsimport] headertest/server/BadOrderFaultType.java
 [wsimport] headertest/server/ConfigFaultType.java
 [wsimport] headertest/server/ConfigHeader.java
 [wsimport] headertest/server/CustomerInfo.java
 [wsimport] headertest/server/HeaderTest.java
 [wsimport] headertest/server/HeaderTestService.java
 [wsimport] headertest/server/ObjectFactory.java
 [wsimport] headertest/server/ProductOrderItem.java
 [wsimport] headertest/server/ProductOrderRequest.java
 [wsimport] headertest/server/ProductOrderResponse.java
 [wsimport] headertest/server/package-info.java
 [wsimport] headertest/server/BadOrderFault.java
 [wsimport] headertest/server/BadOrderFaultType.java
 [wsimport] headertest/server/ConfigFault.java
 [wsimport] headertest/server/ConfigFaultType.java
 [wsimport] headertest/server/ConfigHeader.java
 [wsimport] headertest/server/CustomerInfo.java
 [wsimport] headertest/server/HeaderTest.java
 [wsimport] headertest/server/HeaderTestService.java
 [wsimport] headertest/server/ObjectFactory.java
 [wsimport] headertest/server/ProductOrderItem.java
 [wsimport] headertest/server/ProductOrderRequest.java
 [wsimport] headertest/server/ProductOrderResponse.java
 [wsimport] headertest/server/package-info.java

Step 3

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

a) HeaderTestImpl.java

/*
 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

/*
 * %W% %E%
 */

package headertest.server;

import javax.xml.ws.WebServiceException;

import javax.jws.WebService;

@WebService(
    portName="HeaderTestPort",
    serviceName="HeaderTestService",
    targetNamespace="http://headertestservice.org/HeaderTestService.wsdl",
    wsdlLocation="WEB-INF/wsdl/HeaderTestService.wsdl",
    endpointInterface="headertest.server.HeaderTest"
)
public class HeaderTestImpl implements HeaderTest {

    public ProductOrderResponse submitOrder(ProductOrderRequest poRequest, 
        ConfigHeader configHeader) throws BadOrderFault, ConfigFault {
        ProductOrderResponse poResponse = null;
        poResponse = new ProductOrderResponse();
        String testName = configHeader.getTestName();
        ConfigFaultType cft = new ConfigFaultType();
        cft.setMessage(testName);
        cft.setMustUnderstand(true);
        if(testName.equals("GoodOrderTestWithSoapHeaderAndMUFalse")) {
          if(!ValidHeader(configHeader, false, "Config Header", testName))
                throw new ConfigFault("Invalid ConfigHeader: mustUnderstand="+
                    configHeader.isMustUnderstand()+", message="+
                        configHeader.getMessage()+", testName="+testName, cft);
            poResponse.getItem().addAll(poRequest.getItem());
        } else if(testName.equals("GoodOrderTestWithSoapHeaderAndMUTrue")) {
            if(!ValidHeader(configHeader, true, "Config Header", testName))
                throw new ConfigFault("Invalid ConfigHeader: mustUnderstand="+
                    configHeader.isMustUnderstand()+", message="+
                        configHeader.getMessage()+", testName="+testName, cft);
            poResponse.getItem().addAll(poRequest.getItem());
        } else if(testName.equals("SoapHeaderFaultTest")) {
            throw new ConfigFault("This is a soap header fault ConfigFault", cft);
        } else if(testName.equals("SoapFaultTest")) {
            BadOrderFaultType bft = new BadOrderFaultType();
            bft.setMessage(testName);
            throw new BadOrderFault("This is a soap fault BadOrderFault", bft);
        } else {
            throw new ConfigFault("Invalid ConfigHeader: mustUnderstand="+
                configHeader.isMustUnderstand()+", message="+
                    configHeader.getMessage()+", testName="+testName, cft);
        }
        return poResponse;
    }

    private boolean ValidHeader(ConfigHeader ch, boolean mu, String msg, String test) {
        if(ch.isMustUnderstand() == mu 
                && ch.getMessage().equals(msg) && ch.getTestName().equals(test))
            return true;
        else
            return false;
    }
}

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/netbeans/nbprojects/HeaderTest/classes

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

create-war:
     [echo] create-war
     [echo] Creating war file /home/af70133/netbeans/nbprojects/HeaderTest/dist/headertest/HeaderTestService.war
      [war] Building war: /home/af70133/netbeans/nbprojects/HeaderTest/dist/headertest/HeaderTestService.war
     [echo] Created war file /home/af70133/netbeans/nbprojects/HeaderTest/dist/headertest/HeaderTestService.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/netbeans/nbprojects/HeaderTest/build

configPlatform:

deploy:
     [echo] deploy
     [echo] Deploying /home/af70133/netbeans/nbprojects/HeaderTest/dist/headertest/HeaderTestService.war.
     [echo] asadmin deploy --user admin --passwordfile /home/af70133/netbeans/nbprojects/HeaderTest/build/password.txt --host localhost --port 4848 --contextroot HeaderTestService --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 and customfile2-client.xml

<?xml version="1.0" encoding="UTF-8"?>
<bindings wsdlLocation="wsdl/HeaderTestService.wsdl" xmlns="http://java.sun.com/xml/ns/jaxws">    

    <bindings node="wsdl:definitions" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
        <package name="headertest.client"/>
    </bindings>

</bindings>

<jxb:bindings version="1.0"
              xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
              xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <jxb:bindings schemaLocation="wsdl/HeaderTestDefs.xsd" node="/xs:schema">
        <jxb:schemaBindings xmlns:jxb="http://java.sun.com/xml/ns/jaxb">
            <jxb:package name="headertest.client"/>
        </jxb:schemaBindings>
    </jxb:bindings>

</jxb: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

import-wsdl-client:

init:

do-wsdl2java:
     [echo] Invoking WsImport task (WSDL-to-Java mapping)
     [echo] wsdlLocation=http://localhost:8001/HeaderTestService/jaxws/HeaderTest?WSDL
     [echo] wsdl=/home/af70133/netbeans/nbprojects/HeaderTest/conf/wsdl/HeaderTestService.wsdl
 [wsimport] command line: wsimport /files/jdk/jdk1.5.0/jre/bin/java -classpath /sun/appserver9/lib/activation.jar:/sun/appserver9/lib/javaee.jar:/sun/appserver9/lib/appserv-ws.jar:/home/af70133/netbeans/nbprojects/HeaderTest/classes com.sun.tools.ws.WsImport -d /home/af70133/netbeans/nbprojects/HeaderTest/classes -keep -s /home/af70133/netbeans/nbprojects/HeaderTest/generated -verbose /home/af70133/netbeans/nbprojects/HeaderTest/conf/wsdl/HeaderTestService.wsdl -wsdllocation http://localhost:8001/HeaderTestService/jaxws/HeaderTest?WSDL -b /home/af70133/netbeans/nbprojects/HeaderTest/conf/customfile-client.xml -b /home/af70133/netbeans/nbprojects/HeaderTest/conf/customfile2-client.xml
 [wsimport] headertest/client/BadOrderFaultType.java
 [wsimport] headertest/client/ConfigFaultType.java
 [wsimport] headertest/client/ConfigHeader.java
 [wsimport] headertest/client/CustomerInfo.java
 [wsimport] headertest/client/HeaderTest.java
 [wsimport] headertest/client/HeaderTestService.java
 [wsimport] headertest/client/ObjectFactory.java
 [wsimport] headertest/client/ProductOrderItem.java
 [wsimport] headertest/client/ProductOrderRequest.java
 [wsimport] headertest/client/ProductOrderResponse.java
 [wsimport] headertest/client/package-info.java
 [wsimport] headertest/client/BadOrderFault.java
 [wsimport] headertest/client/BadOrderFaultType.java
 [wsimport] headertest/client/ConfigFault.java
 [wsimport] headertest/client/ConfigFaultType.java
 [wsimport] headertest/client/ConfigHeader.java
 [wsimport] headertest/client/CustomerInfo.java
 [wsimport] headertest/client/HeaderTest.java
 [wsimport] headertest/client/HeaderTestService.java
 [wsimport] headertest/client/ObjectFactory.java
 [wsimport] headertest/client/ProductOrderItem.java
 [wsimport] headertest/client/ProductOrderRequest.java
 [wsimport] headertest/client/ProductOrderResponse.java
 [wsimport] headertest/client/package-info.java

Step 2

Create the client code to communicate with the deployed endpoint.

a) Client.java

/*
 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package headertest.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;
import java.math.BigInteger;
import java.math.BigDecimal;

public class Client {
    private static String protocol = "http";
    private static String hostname = "localhost";
    private static int portnum = 8080;
    private static String ctxroot = "/HeaderTestService";
    private String endpointUrl = "/HeaderTestService/jaxws/HeaderTest";
    private String wsdlUrl = "/HeaderTestService/jaxws/HeaderTest?WSDL";
    private static HeaderTest port = null;
    private static URL url = null;
    private static int idx=0, numoftests=4;
    private static boolean tests[] = new boolean[numoftests];

    @WebServiceRef
    private static HeaderTestService service = null;

    public static void main(String[] args ) {
        try {
            Client client = new Client();
            port = service.getHeaderTestPort();
            System.out.println("WebService Port = "+port);
            BindingProvider bindingprovider = (BindingProvider)port;
            java.util.Map context = bindingprovider.getRequestContext();
            String targetEndpointAddr = (String)context.get(BindingProvider.ENDPOINT_ADDRESS_PROPERTY);
            System.out.println("WebService TargetEndpointAddress = "+targetEndpointAddr);
            url = new URL(targetEndpointAddr); 
            hostname = url.getHost();
            portnum = url.getPort();
            protocol = url.getProtocol();
            String path = url.getPath();
            ctxroot = path.substring(0, path.indexOf("/", 2));
            System.out.println("WebService Ctxroot = "+ctxroot);

            System.out.println("------------------------------------------");
            client.GoodOrderTestWithSoapHeaderAndMUFalse();
            System.out.println("------------------------------------------");
            client.GoodOrderTestWithSoapHeaderAndMUTrue();
            System.out.println("------------------------------------------");
            client.SoapHeaderFaultTest();
            System.out.println("------------------------------------------");
            client.SoapFaultTest();
            System.out.println("*********************************************");
            System.out.println("*               Test Summary Results            *");
            System.out.println("*********************************************");
            if(tests[0])
                System.out.println("GoodOrderTestWithSoapHeaderAndMUFalse  .......  PASSED");
            else
                System.out.println("GoodOrderTestWithSoapHeaderAndMUFalse  .......  FAILED");
            if(tests[1])
                System.out.println("GoodOrderTestWithSoapHeaderAndMUTrue  .......  PASSED");
            else
                System.out.println("GoodOrderTestWithSoapHeaderAndMUTrue  .......  FAILED");
            if(tests[2])
                System.out.println("SoapHeaderFaultTest  .......  PASSED");
            else
                System.out.println("SoapHeaderFaultTest  .......  FAILED");
            if(tests[3])
                System.out.println("SoapFaultTest  .......  PASSED");
            else
                System.out.println("SoapFaultTest  .......  FAILED");
        } catch(Exception e) {
            System.err.println("Exception occured: "+e.getMessage());
            e.printStackTrace(System.err);
            System.exit(1);
        }
    }

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

        ProductOrderRequest poRequest;
        ConfigHeader ch;
        try {
            poRequest = new ProductOrderRequest();
            ProductOrderItem poi = new ProductOrderItem();
            poi.setProductName("Product-1");
            poi.setProductCode(new BigInteger("100"));
            poi.setQuantity(10);
            poi.setPrice(new BigDecimal(119.00));
            CustomerInfo ci = new CustomerInfo();
            ci.setCreditcard("1201-4465-1567-9823");
            ci.setName("John Doe");
            ci.setStreet("1 Network Drive");
            ci.setCity("Burlington");
            ci.setState("Ma");
            ci.setZip("01837");
            ci.setCountry("USA");
            poRequest.getItem().add(poi);
            poRequest.setCustomerInfo(ci);
            ch = new ConfigHeader();
            ch.setMustUnderstand(false);
            ch.setMessage("Config Header");
            ch.setTestName("GoodOrderTestWithSoapHeaderAndMUFalse");
            System.out.println(
                "Submit good order with soap header (ConfigHeader:MU=false)");
            System.out.println("ConfigHeader must be ignored because MU=false");
            System.out.println("The service endpoint simply ignores the soap header");
            System.out.println("The RPC request must succeed");
            ProductOrderResponse poResponse = port.submitOrder(poRequest, ch);
            if(!ProductOrdersEqual(poRequest, poResponse))
                pass = false;
        }
        catch (Exception e) {
            System.err.println("Caught exception: " + e.getMessage());
            e.printStackTrace(System.err);
            pass = false;
        }
        tests[idx++] = pass;
        if (!pass)
            System.out.println("GoodOrderTestWithSoapHeaderAndMUFalse passed");
        else
            System.err.println("GoodOrderTestWithSoapHeaderAndMUFalse failed");
    }

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

        ProductOrderRequest poRequest;
        ConfigHeader ch;
        try {
            poRequest = new ProductOrderRequest();
            ProductOrderItem poi = new ProductOrderItem();
            poi.setProductName("Product-1");
            poi.setProductCode(new BigInteger("100"));
            poi.setQuantity(10);
            poi.setPrice(new BigDecimal(119.00));
            CustomerInfo ci = new CustomerInfo();
            ci.setCreditcard("1201-4465-1567-9823");
            ci.setName("John Doe");
            ci.setStreet("1 Network Drive");
            ci.setCity("Burlington");
            ci.setState("Ma");
            ci.setZip("01837");
            ci.setCountry("USA");
            poRequest.getItem().add(poi);
            poRequest.setCustomerInfo(ci);
            ch = new ConfigHeader();
            ch.setMustUnderstand(true);
            ch.setMessage("Config Header");
            ch.setTestName("GoodOrderTestWithSoapHeaderAndMUTrue");
            System.out.println(
                "Submit good order with soap header (ConfigHeader:MU=true)");
            System.out.println(
                "ConfigHeader must be understood and valid bacause MU=true");
            System.out.println(
                "The service endpoint understands and validates the soap header as ok");
            System.out.println("The RPC request must succeed");
            ProductOrderResponse poResponse = port.submitOrder(poRequest, ch);
            System.out.println("GoodOrderTestWithMUTrueHeader succeeded (expected)");
            if(!ProductOrdersEqual(poRequest, poResponse))
                pass = false;
        }
        catch (Exception e) {
            System.err.println("Caught exception: " + e.getMessage());
            e.printStackTrace(System.err);
            pass = false;
        }
        tests[idx++] = pass;
        if (!pass)
            System.out.println("GoodOrderTestWithSoapHeaderAndMUTrue passed");
        else
            System.err.println("GoodOrderTestWithSoapHeaderAndMUTrue failed");
    }

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

        ProductOrderRequest poRequest;
        ConfigHeader ch;
        try {
            poRequest = new ProductOrderRequest();
            ProductOrderItem poi = new ProductOrderItem();
            poi.setProductName("Product-1"); 
            poi.setProductCode(new BigInteger("100"));
            poi.setQuantity(10);
            poi.setPrice(new BigDecimal(119.00));
            CustomerInfo ci = new CustomerInfo();
            ci.setCreditcard("1201-4465-1567-9823");
            ci.setName("John Doe");
            ci.setStreet("1 Network Drive");
            ci.setCity("Burlington");
            ci.setState("Ma");
            ci.setZip("01837");
            ci.setCountry("USA");
            poRequest.getItem().add(poi);
            poRequest.setCustomerInfo(ci);
            ch = new ConfigHeader();
            ch.setMustUnderstand(true);
            ch.setMessage("Config Header");
            ch.setTestName("SoapHeaderFaultTest");
            System.out.println(
                "Submit good order with soap header (ConfigHeader:MU=true)");
            System.out.println(
                "ConfigHeader must be understood and valid bacause MU=true");
            System.out.println(
                "The service endpoint does not understand the soap header");
            System.out.println("The RPC request must fail with a ConfigFault");
            ProductOrderResponse poResponse = port.submitOrder(poRequest, ch);
            System.err.println("Did not throw expected ConfigFault");
            pass = false;
        } catch(ConfigFault e) {
            System.out.println("Caught expected ConfigFault");
        } catch (Exception e) {
            System.err.println("Caught exception: " + e.getMessage());
            e.printStackTrace(System.err);
            pass = false;
        }
        tests[idx++] = pass;
        if (!pass)
            System.out.println("SoapHeaderFaultTest passed");
        else
            System.err.println("SoapHeaderFaultTest failed");
    }

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

        ProductOrderRequest poRequest;
        ConfigHeader ch;
        try {
            poRequest = new ProductOrderRequest();
            ProductOrderItem poi = new ProductOrderItem();
            poi.setProductName("Product-1");
            poi.setProductCode(new BigInteger("1234123412341234"));
            poi.setQuantity(10);
            poi.setPrice(new BigDecimal(119.00));
            CustomerInfo ci = new CustomerInfo();
            ci.setCreditcard("1201-4465-1567-9823");
            ci.setName("John Doe");
            ci.setStreet("1 Network Drive");
            ci.setCity("Burlington");
            ci.setState("Ma");
            ci.setZip("01837");
            ci.setCountry("USA");
            poRequest.getItem().add(poi);
            poRequest.setCustomerInfo(ci);
            ch = new ConfigHeader();
            ch.setMustUnderstand(false);
            ch.setMessage("Config Header");
            ch.setTestName("SoapFaultTest");
            System.out.println(
                "Submit bad order with soap header (ConfigHeader:MU=false)");
            System.out.println("ConfigHeader must be ignored because MU=false");
            System.out.println("The service endpoint simply ignores the soap header");
            System.out.println(
                "Order contains bad product code (must throw BadOrderFault)");
            System.out.println("The RPC request must fail with a BadOrderFault");
            ProductOrderResponse poResponse = port.submitOrder(poRequest, ch);
            System.err.println("Did not throw expected BadOrderFault");
            pass = false;
        } catch(BadOrderFault e) {
            System.out.println("Caught expected BadOrderFault");
        } catch (Exception e) {
            System.err.println("Caught exception: " + e.getMessage());
            e.printStackTrace(System.err);
            pass = false;
        }
        tests[idx++] = pass;
        if (!pass)
            System.out.println("SoapFaultTest passed");
        else
            System.err.println("SoapFaultTest failed");
    }

    private boolean ProductOrdersEqual(ProductOrderRequest req, ProductOrderResponse resp) {
        boolean equal = true;
        System.out.println("Performing data comparison of request/response (should be equal)");
        Object[] reqArray = req.getItem().toArray();
        Object[] respArray = resp.getItem().toArray();
        ProductOrderItem reqItem = null;
        ProductOrderItem respItem = null;
        if(reqArray == null || respArray == null) {
            System.err.println("Data comparison error (unexpected)");
            System.err.println("Got:      Item Array = " + respItem);
            System.err.println("Expected: Item Array = " + reqItem);
            equal = false;
        } else if(reqArray.length != respArray.length) {
            System.err.println("Data comparison error (unexpected)");
            System.err.println("Got:      Item Array length = " + respArray.length);
            System.err.println("Expected: Item Array length = " + reqArray.length);
            equal = false;
        } else {
            reqItem = (ProductOrderItem) reqArray[0];
            respItem = (ProductOrderItem) respArray[0];
        }
        if(equal) {
            if(!reqItem.getProductName().equals(respItem.getProductName()) ||
                !reqItem.getProductCode().equals(respItem.getProductCode()) ||
                reqItem.getQuantity() != respItem.getQuantity() ||
                !reqItem.getPrice().equals(respItem.getPrice())) {
                System.err.println("Data comparison error (unexpected)");
                System.err.println("Got:      <"+respItem.getProductName()+","+
                    respItem.getProductCode()+","+respItem.getQuantity()+
                        ","+respItem.getPrice()+">");
                System.err.println("Expected: <"+reqItem.getProductName()+","+
                    reqItem.getProductCode()+","+reqItem.getQuantity()+
                        ","+reqItem.getPrice()+">");
                equal = false;
            } else {
                System.out.println("Data comparison ok (expected)");
                System.out.println("Got:      <"+respItem.getProductName()+","+
                    respItem.getProductCode()+","+respItem.getQuantity()+
                        ","+respItem.getPrice()+">");
                System.out.println("Expected: <"+reqItem.getProductName()+","+
                    reqItem.getProductCode()+","+reqItem.getQuantity()+
                        ","+reqItem.getPrice()+">");
            }
        }
        return equal;
    }
}

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/netbeans/nbprojects/HeaderTest/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

checkPlatform:

configUnix:

configWindows:

filter.password.file:
     [copy] Copying 1 file to /home/af70133/netbeans/nbprojects/HeaderTest/build

configPlatform:

runclient:
     [echo] runclient headertest.client.Client
     [exec] WebService Port = com.sun.xml.ws.client.EndpointIFInvocationHandler@9c176c
     [exec] WebService TargetEndpointAddress = http://lobo:8001/HeaderTestService/jaxws/HeaderTest
     [exec] WebService Ctxroot = /HeaderTestService
     [exec] ------------------------------------------
     [exec] GoodOrderTestWithSoapHeaderAndMUFalse
     [exec] Submit good order with soap header (ConfigHeader:MU=false)
     [exec] ConfigHeader must be ignored because MU=false
     [exec] The service endpoint simply ignores the soap header
     [exec] The RPC request must succeed
     [exec] Performing data comparison of request/response (should be equal)
     [exec] Data comparison ok (expected)
     [exec] Got:      
     [exec] Expected: 
     [exec] GoodOrderTestWithSoapHeaderAndMUFalse failed
     [exec] ------------------------------------------
     [exec] GoodOrderTestWithSoapHeaderAndMUTrue
     [exec] Submit good order with soap header (ConfigHeader:MU=true)
     [exec] ConfigHeader must be understood and valid bacause MU=true
     [exec] The service endpoint understands and validates the soap header as ok
     [exec] The RPC request must succeed
     [exec] GoodOrderTestWithMUTrueHeader succeeded (expected)
     [exec] Performing data comparison of request/response (should be equal)
     [exec] Data comparison ok (expected)
     [exec] Got:      
     [exec] Expected: 
     [exec] GoodOrderTestWithSoapHeaderAndMUTrue failed
     [exec] ------------------------------------------
     [exec] SoapHeaderFaultTest
     [exec] Submit good order with soap header (ConfigHeader:MU=true)
     [exec] ConfigHeader must be understood and valid bacause MU=true
     [exec] The service endpoint does not understand the soap header
     [exec] The RPC request must fail with a ConfigFault
     [exec] SoapHeaderFaultTest failed
     [exec] Caught expected ConfigFault
     [exec] ------------------------------------------
     [exec] SoapFaultTest
     [exec] Submit bad order with soap header (ConfigHeader:MU=false)
     [exec] ConfigHeader must be ignored because MU=false
     [exec] The service endpoint simply ignores the soap header
     [exec] Order contains bad product code (must throw BadOrderFault)
     [exec] The RPC request must fail with a BadOrderFault
     [exec] Caught expected BadOrderFault
     [exec] SoapFaultTest failed
     [exec] ------------------------------------------
     [exec] *********************************************
     [exec] *           Test Summary Results            *
     [exec] *********************************************
     [exec] GoodOrderTestWithSoapHeaderAndMUFalse  .......  PASSED
     [exec] GoodOrderTestWithSoapHeaderAndMUTrue  .......  PASSED
     [exec] SoapHeaderFaultTest  .......  PASSED
     [exec] SoapFaultTest  .......  PASSED
For a working example of this client and server code download here.

Comments:

Where can I find information on how to set up a web service to populate a report? SOA Pcall & SMS SOAP - is what I am looking for specifically. Thank you so much for any help you can provide.

Posted by Diane Abbott on September 17, 2008 at 08:01 AM EST #

http://www.batteryfast.co.uk/asus/s5200n.htm asus s5200n battery,
http://www.batteryfast.co.uk/asus/s5n.htm asus s5n battery,
http://www.batteryfast.co.uk/asus/90-naa1b1000.htm asus 90-naa1b1000 battery,

Posted by laptop battery on October 24, 2008 at 03:54 AM EST #

asd

Posted by 64.103.157.74 on February 05, 2009 at 06:38 AM EST #

Post a Comment:
  • HTML Syntax: NOT allowed