【Axis2】【Eclipse】 Axis2 を使用した Webサービスで、セッションを利用するには... ~ サーバ側 / クライアント側 ~

■ ポイント

サーバ側

 * Webサービスにおけるスコープ「soapsession」(transportsessionもOK?)を設定する必要がある
 * スコープについては、以下の関連記事を参照のこと。
http://blogs.yahoo.co.jp/dk521123/34600183.html

クライアント側

http://space.geocities.jp/nequomame/java/webservice/webservice_93_02.html
より

// Webサービスのセッション管理を有効にする
stub._getServiceClient().getOptions().setManageSession(true);
// scope=soapsessionの場合、addressingモジュールとの関連付けをしないとセッション管理が有効とならない
String addressing = System.getProperty("user.dir") + "\\WebContent\\WEB-INF\\modules\\addressing-1.7.2.mar";
stub._getServiceClient().getAxisConfiguration().deployModule(addressing);
stub._getServiceClient().getAxisConfiguration().engageModule("addressing-1.7.2");

■ サンプル

 * ベースとなるソースは以下の関連記事のWebサービスのものを使用する
http://blogs.yahoo.co.jp/dk521123/36128161.html

サーバ側

 * 実装し終わったら、Tomcatを起動しておく
services.xml (serviceタグ の scope="soapsession" に注目)
<service name="SampleWebMainService" scope="soapsession " class="com.sample.SampleWebMain">
  <Description>
    Please Type your service description here
  </Description>
  <messageReceivers>
    <messageReceiver
    mep="http://www.w3.org/ns/wsdl/in-only"
    class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
    <messageReceiver
    mep="http://www.w3.org/ns/wsdl/in-out"
    class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
  </messageReceivers>
  <parameter name="ServiceClass" locked="false">com.sample.SampleWebMain</parameter>
</service>
SampleWebMain.java (以下の関連記事とほぼ同じ)
http://blogs.yahoo.co.jp/dk521123/34600183.html
import javax.jws.WebMethod;
import javax.jws.WebParam;

import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.description.AxisService;
import org.apache.axis2.engine.ServiceLifeCycle;

import com.sample.entities.SampleEntity;

public class SampleWebMain implements ServiceLifeCycle {

  @WebMethod
  public SampleEntity getSampleEntity(@WebParam(name = "name") String name) {
    return new SampleEntity(name);
  }

  @Override
  public void shutDown(ConfigurationContext arg0, AxisService arg1) {
    System.out.println("shutDown");
  }

  @Override
  public void startUp(ConfigurationContext arg0, AxisService arg1) {
    System.out.println("startUp");
  }
}
SampleEntity.java
package com.sample.entities;

import java.io.Serializable;

public class SampleEntity implements Serializable {
  private static final long serialVersionUID = 1L;

  private String name;

  public SampleEntity() {
  }

  public SampleEntity(String name) {
    this.name = name;
  }

  public String getName() {
    return this.name;
  }

  public void setName(String name) {
    this.name = name;
  }
}

クライアント側

[WSDLファイル] Sample.wsdl ([プロジェクト名]/WebContent/META-INF配下に保存しておく)
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns="http://sample.com" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ax21="http://entities.sample.com/xsd" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" targetNamespace="">http://sample.com">
<wsdl:documentation>Please Type your service description here</wsdl:documentation>
<wsdl:types>
<xs:schema xmlns:ax22="http://entities.sample.com/xsd" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="">http://sample.com">
<xs:import namespace="">http://entities.sample.com/xsd"/>
<xs:element name="getSampleEntity">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="name" nillable="true" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="getSampleEntityResponse">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="return" nillable="true" type="ax22:SampleEntity"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
<xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="">http://entities.sample.com/xsd">
<xs:complexType name="SampleEntity">
<xs:sequence>
<xs:element minOccurs="0" name="name" nillable="true" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<wsdl:message name="getSampleEntityRequest">
<wsdl:part name="parameters" element="ns:getSampleEntity"/>
</wsdl:message>
<wsdl:message name="getSampleEntityResponse">
<wsdl:part name="parameters" element="ns:getSampleEntityResponse"/>
</wsdl:message>
<wsdl:portType name="SampleWebMainServicePortType">
<wsdl:operation name="getSampleEntity">
<wsdl:input message="ns:getSampleEntityRequest" wsaw:Action="urn:getSampleEntity"/>
<wsdl:output message="ns:getSampleEntityResponse" wsaw:Action="urn:getSampleEntityResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="SampleWebMainServiceSoap11Binding" type="ns:SampleWebMainServicePortType">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<wsdl:operation name="getSampleEntity">
<soap:operation soapAction="urn:getSampleEntity" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="SampleWebMainServiceSoap12Binding" type="ns:SampleWebMainServicePortType">
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<wsdl:operation name="getSampleEntity">
<soap12:operation soapAction="urn:getSampleEntity" style="document"/>
<wsdl:input>
<soap12:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap12:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="SampleWebMainServiceHttpBinding" type="ns:SampleWebMainServicePortType">
<http:binding verb="POST"/>
<wsdl:operation name="getSampleEntity">
<http:operation location="getSampleEntity"/>
<wsdl:input>
<mime:content type="application/xml" part="parameters"/>
</wsdl:input>
<wsdl:output>
<mime:content type="application/xml" part="parameters"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="SampleWebMainService">
<wsdl:port name="SampleWebMainServiceHttpSoap11Endpoint" binding="ns:SampleWebMainServiceSoap11Binding">
<soap:address location="">http://localhost:8080/SampleWeb/services/SampleWebMainService.SampleWebMainServiceHttpSoap11Endpoint/"/>
</wsdl:port>
<wsdl:port name="SampleWebMainServiceHttpSoap12Endpoint" binding="ns:SampleWebMainServiceSoap12Binding">
<soap12:address location="">http://localhost:8080/SampleWeb/services/SampleWebMainService.SampleWebMainServiceHttpSoap12Endpoint/"/>
</wsdl:port>
<wsdl:port name="SampleWebMainServiceHttpEndpoint" binding="ns:SampleWebMainServiceHttpBinding">
<http:address location="">http://localhost:8080/SampleWeb/services/SampleWebMainService.SampleWebMainServiceHttpEndpoint/"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
build.xml
 * 以下の関連記事を参考にクライアント側のコードを自動生成する
http://blogs.yahoo.co.jp/dk521123/34445295.html
<?xml version="1.0" encoding="UTF-8"?>
<project basedir="." default="w2j.client" name="SampleWeb">
    <property environment="env"/>
    <!-- !★! AXIS2_HOMEが設定されている場合は下記を使用してもいい -->
    <!-- property name="axis2.home" value="${env.AXIS2_HOME}"/ -->
    <property name="axis2.home" value="C:\work\axis2-1.7.2"/>
    <property name="input.wsdl" value="${basedir}/WebContent/META-INF/Sample.wsdl"/>
    <property name="output.path" value="${basedir}/etc/output"/>
    <path id="axis2.classpath">
        <fileset dir="${axis2.home}">
            <include name="lib/*.jar"/>
        </fileset>
    </path>
    <!-- Client code output -->
    <target name="w2j.client" description="Client code output from WSDL">
        <echo message="Client code output from WSDL ${input.wsdl}"/>
        <java classname="org.apache.axis2.wsdl.WSDL2Java" dir="." fork="yes">
            <classpath refid="axis2.classpath"/>
            <arg line="-uri ${input.wsdl}"/>
            <arg line="-u"/>
            <arg line="-s"/> <!-- 非同期コードは不要 -->
            <arg line="-p com.sample.client"/>
            <arg line="-ns2p ">http://sample.com=com.sample.client"/>
            <arg line="-o ${output.path}"/>
        </java>
    </target>
</project>
[クライアント側メイン処理] Main.java (以下の関連記事を参考に作成)
http://blogs.yahoo.co.jp/dk521123/32003685.html
package com.sample.client;

import com.sample.entities.xsd.SampleEntity;

public class Main {

  public static void main(String[] args) {
    try {
      SampleWebMainServiceStub stub = new SampleWebMainServiceStub(
                           "http://localhost:8080/SampleWeb/services/SampleWebMainService");
   
      // Webサービスのセッション管理を有効にする
      stub._getServiceClient().getOptions().setManageSession(true);
      // scope=soapsessionの場合、addressingモジュールとの関連付けをしないとセッション管理が有効とならない
      String addressing = System.getProperty("user.dir") + "\\WebContent\\WEB-INF\\modules\\addressing-1.7.2.mar";
      stub._getServiceClient().getAxisConfiguration().deployModule(addressing);
      stub._getServiceClient().getAxisConfiguration().engageModule("addressing-1.7.2");
      
      // 1回目の呼び出し
      GetSampleEntity request = new GetSampleEntity();
      request.setName("Mike");
      GetSampleEntityResponse response = stub.getSampleEntity(request);
      SampleEntity result = response.get_return();
      System.out.println(result.getName());
   
      // 2回目の呼び出し
      GetSampleEntity request2 = new GetSampleEntity();
      request2.setName("Ken");
      GetSampleEntityResponse response2 = stub.getSampleEntity(request2);
      SampleEntity result2 = response2.get_return();
      System.out.println(result2.getName());

    } catch (Exception ex) {
      ex.printStackTrace();
      System.err.println("\n\n");
    } finally {
    }
  }
}

実行結果

 * クライアント側のコード実装し終わったら、実行する
クライアント側の出力結果
Mike
Ken
サーバ側の出力結果
counter = 1
counter = 2
* リクエスト(1回目)
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<soapenv:Envelope xmlns:soapenv="">http://www.w3.org/2003/05/soap-envelope">
<soapenv:Header xmlns:wsa="">http://www.w3.org/2005/08/addressing">
<wsa:To>http://localhost:8888/SampleWeb/services/SampleWebMainService
<wsa:MessageID>urn:uuid:67099a1a-da1e-41e6-9ff8-6dbc1f59c7f0</wsa:MessageID>
<wsa:Action>urn:getSampleEntity</wsa:Action>
</soapenv:Header>
<soapenv:Body>
<ns2:getSampleEntity xmlns:ns2="">http://sample.com">
<ns2:name>Mike</ns2:name>
</ns2:getSampleEntity>
</soapenv:Body>
</soapenv:Envelope>
* レスポンス(1回目)
<?xml version="1.0" encoding="UTF-8"?>
 <soapenv:Envelope xmlns:soapenv="">http://www.w3.org/2003/05/soap-envelope">
 <soapenv:Header xmlns:wsa="">http://www.w3.org/2005/08/addressing">
 <wsa:ReplyTo>
<wsa:Address>http://www.w3.org/2005/08/addressing/none
 <wsa:ReferenceParameters>
<axis2:ServiceGroupId xmlns:axis2="urn:uuid:14b6ac26-6ae8-47ee-8e2c-cbcda01b9936">http://ws.apache.org/namespaces/axis2">urn:uuid:14b6ac26-6ae8-47ee-8e2c-cbcda01b9936
</wsa:ReferenceParameters>
</wsa:ReplyTo>
<wsa:MessageID>urn:uuid:b245a594-c2a4-4f32-bb35-957b31d6de96</wsa:MessageID>
<wsa:Action>urn:getSampleEntityResponse</wsa:Action>
<wsa:RelatesTo>urn:uuid:67099a1a-da1e-41e6-9ff8-6dbc1f59c7f0</wsa:RelatesTo>
</soapenv:Header>
 <soapenv:Body>
 <ns:getSampleEntityResponse xmlns:ns="">http://sample.com">
 <ns:return xsi:type="ax21:SampleEntity" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ax21="">http://entities.sample.com/xsd">
<ax21:name>Mike</ax21:name>
</ns:return>
</ns:getSampleEntityResponse>
</soapenv:Body>
</soapenv:Envelope>


関連記事

クライアント関連

WSDLから クライアントを作成する
http://blogs.yahoo.co.jp/dk521123/32003685.html
Antで、WSDLファイルからソースの自動生成を行う ~クライアント編~
http://blogs.yahoo.co.jp/dk521123/34445295.html

その他

Axis2 で、Tomcat の 開始 / 停止イベントを拾うには
http://blogs.yahoo.co.jp/dk521123/34600183.html