Consuming SOAP Web Service Sample

A custom state can consume an external SOAP Web service.

The Web service provider in this sample is the United States Consumer Product Safety Commission. The WSDL file (CPSCUpcSvc.wsdl) is embedded with the bundle. Alternately, you can retrieve the WSDL file in real time using the <wsdlUrls> configuration. The JAX-WS Maven plug-in reads the WSDL file and generates all the required artifacts for Web service development, deployment, and invocation.

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
  http://maven.apache.org/xsd/maven-4.0.0.xsd">        
      
<build>        
[…]         
 <!-- Create an OSGi Bundle Manifest -->         
 <plugins>
  <plugin>          
  […]           
    <configuration>             
    […]               
      <Private-Package>                  
       com.sap.example                 
       ,org.tempuri               
      </Private-Package>             
    […]           
    </configuration>         
  </plugin>       
 </plugins>    
</build>        
      
<profiles>        

<!-- Required Javax Persistence dependency -->       
 <profile>        
    <activation>          
     <jdk>[1.5, 1.7)</jdk>        
    </activation>        
  <dependencies>          
    <dependency>           
      <groupId>org.eclipse.persistence</groupId>           
      <artifactId>javax.persistence</artifactId>           
      <version>2.0.4.v201112161009</version>           
      <scope>provided</scope>          
    </dependency>        
  </dependencies>        
  <repositories>          
    <repository>           
      <id>EclipseLink</id>           
      <url>http://download.eclipse.org/rt/eclipselink/maven.repo</url>          
    </repository>        
  </repositories>       
 </profile>           

<!-- Required SOAP Web Service JAX-WS only on JDK 6 -->      
 <profile>       
  <id>jdk6</id>       
  <activation>         
    <jdk>1.6</jdk>       
  </activation>       
  <build>         
   <plugins>          
    <plugin>            
      <groupId>org.jvnet.jax-ws-commons</groupId>            
      <artifactId>jaxws-maven-plugin</artifactId>            
      <version>2.1</version>            
      <executions>              
        <execution>                
         <id>import-wsdld</id>                
         <phase>generate-sources</phase>                
         <goals>                  
          <goal>wsimport</goal>                
         </goals>                     
        <configuration>                   
          <wsdlFiles>                    
           <wsdlFile>CPSCUpcSvc.wsdl</wsdlFile>                  
          </wsdlFiles>                  
         <extension>true</extension>                  
         <xdebug>true</xdebug>                
        </configuration>              
       </execution>            
      </executions>          
     </plugin>         
    </plugins>       
   </build>      
  </profile>           

<!-- Required SOAP Web Service JAX-WS only on JDK 7 -->      
  <profile>       
   <id>jdk7</id>       
   <activation>         
     <jdk>1.7</jdk>       
   </activation>       
   <build>         
     <plugins>          
      <plugin>            
       <groupId>org.jvnet.jax-ws-commons</groupId>            
       <artifactId>jaxws-maven-plugin</artifactId>            
       <version>2.2</version>            
       <executions>              
        <execution>                
         <id>import-wsdld</id>                
         <phase>generate-sources</phase>                
         <goals>                  
          <goal>wsimport</goal>                
         </goals>                
         <configuration>                  
           <wsdlFiles>                    
             <wsdlFile>CPSCUpcSvc.wsdl</wsdlFile>                  
           </wsdlFiles>                  
           <extension>true</extension>                   
           <xdebug>true</xdebug>                 
         </configuration>              
        </execution>            
       </executions>          
     </plugin>         
    </plugins>       
   </build>      
  </profile> 
 </profiles>
</project>   

SampleSOAPState.java

package com.sap.example;       
[…]       
import org.tempuri.CPSCUpcSvc;   
import org.tempuri.GetRecallByWordResponse.GetRecallByWordResult;       

public class SampleSOAPState extends SmappStatePlugin {         

  @Override 
  protected SmappState processStateLogic(SmappStateProcessingContext context,                                    
                                       SmappStateProcessingAction action)             
    throws MwizProcessingException, DBException {          
    CPSCUpcSvc recallService = null;      
    String serviceUrl = "http://www.cpsc.gov/cgibin/CPSCUpcWS/CPSCUpcSvc.asmx?WSDL";          
    try {              
      recallService = new CPSCUpcSvc(new URL(serviceUrl),                         
      new QName("http://tempuri.org/", "CPSCUpcSvc"));          
    } catch (MalformedURLException mfue) {         
        […]      
    }          
    if (null == recallService) {          
       return continueFail();      
    }          
    String keyword = "booster";      
    GetRecallByWordResult recallServiceResult = 
               recallService.getCPSCUpcSvcSoap12().getRecallByWord(keyword, "", "");          
    
    if (null == recallServiceResult) {          
      return continueDyn(1);      
    }          
    return continueOk();     
  }       
}