Web Services Addressing を使用する JAX-RPC Web サービス・アプリケーションの作成
Web サービス・アドレッシング (WS-Addressing) は、Web サービスをアドレス指定してメッセージ内でアドレッシング情報を提供するための標準の方法を定義することで、Web サービス間のインターオペラビリティーを補助します。このタスクでは、WS-Addressing エンドポイント参照を使用してアクセスされる JAX-RPC Web サービスの作成に必要なステップについて説明します。このタスクはまた、Web サービスの一部としてステートフル・リソースを使用するために必要な追加のステップも説明しています。
始める前に
このタスクで説明されているステップは、WebSphere® Application Server 上で実行されるサーバーおよびクライアントに適用されます。
このタスクについて
このタスクは、WS-Addressing 仕様を使用する Web サービスを作成する場合に実行します。
手順
タスクの結果
エンドポイント参照をターゲット・サービスに戻す Web サービス・インターフェースの提供
以下の例は、この手順のステップ 1 から 4 に対応しています。 これらの例では、IT 組織が Web サービスを使用してプリンターのネットワークを管理する方法を示しています。 この組織では、各プリンターを、 エンドポイント参照によって指定するリソースとしています。 以下の例は、WebSphere Application Server および JAX-WS が提供する、IBM 専有の Web Services Addressing (WS-Addressing) アプリケーション・プログラミング・インターフェース (API) を使用したサービスのコード化の方法を示しています。
この IT 組織では、CreatePrinter portType エレメントを提供する PrinterFactory サービスを実装します。 この portType エレメントは、CreatePrinterRequest メッセージを受け入れ、論理プリンターを示すリソースを作成し、 そのリソースへの参照となるエンドポイント参照をつけて応答します。
<wsdl:definitions targetNamespace="http://example.org/printer" ...
xmlns:pr="http://example.org/printer">
<wsdl:types>
...
<xsd:schema...>
<xsd:element name="CreatePrinterRequest"/>
<xsd:element name="CreatePrinterResponse"
type="wsa:EndpointReferenceType"/>
</xsd:schema> </wsdl:types>
<wsdl:message name="CreatePrinterRequest">
<wsdl:part name="CreatePrinterRequest"
element="pr:CreatePrinterRequest" />
</wsdl:message>
<wsdl:message name="CreatePrinterResponse">
<wsdl:part name="CreatePrinterResponse"
element="pr:CreatePrinterResponse" />
</wsdl:message>
<wsdl:portType name="CreatePrinter">
<wsdl:operation name="createPrinter">
<wsdl:input name="CreatePrinterRequest"
message="pr:CreatePrinterRequest" />
<wsdl:output name="CreatePrinterResponse"
message="pr:CreatePrinterResponse" />
</wsdl:operation>
</wsdl:portType>
</wsdl:definitions>
上記の例の CreatePrinter オペレーションは、 新規に作成されたプリンター・リソースを 示す wsa:EndpointReference オブジェクトを戻します。 クライアントは、このエンドポイント参照を使用して、プリンターを示すサービス・インスタンスにメッセージを送信します。
以下の例にある createPrinter メソッドは、 プリンター・サービスへのエンドポイント参照を作成します。 次に、そのオペレーションは、個々のプリンター・リソース・インスタンスの ID を取得し、 その ID をエンドポイント参照に関連付けます。 最後に、createPrinter メソッドは EndpointReference オブジェクトを W3CEndpointReference オブジェクトに変換し、変換したエンドポイント参照を戻します。このオブジェクトが、新規プリンターとなります。
import com.ibm.websphere.wsaddressing.EndpointReferenceManager;
import com.ibm.websphere.wsaddressing.EndpointReference;
import com.ibm.websphere.wsaddressing.jaxws.EndpointReferenceConverter;
import com.ibm.websphere.wsaddressing.jaxws.W3CEndpointReference;
import javax.xml.namespace.QName;
public class MyClass {
// Create the printer
...
// Define the printer resource ID as a static constant as it is required in later steps
public static final QName PRINTER_ID_PARAM_QNAME = new QName("example.printersample","IBM_WSRF_PRINTERID", "ws-rf-pr" );
public static final QName PRINTER_SERVICE_QNAME = new QName("example.printer.com", "printer", "...");
public static final String PRINTER_ENDPOINT_NAME = new String("PrinterService");
public W3CEndpointReference createPrinter(java.lang.Object createPrinterRequest)
throws Exception {
// Create an EndpointReference that targets the appropriate WebService URI and port name.
EndpointReference epr = EndpointReferenceManager.createEndpointReference(PRINTER_SERVICE_QNAME, PRINTER_ENDPOINT_NAME);
// Create or lookup the stateful resource and derive a resource
// identifier string.
String resource_identifier = "...";
// Associate this resource identifier with the EndpointReference as
// a reference parameter.
// The choice of name is arbitrary, but should be unique
// to the service.
epr.setReferenceParameter(PRINTER_ID_PARAM_QNAME,resource_identifier);
// The endpoint reference now targets the resource rather than the service.
...
return EndpointReferenceConverter.createW3CEndpointReference(epr);
}
}
これまでに説明した Web サービス実装によって、 プリンター・リソース・インスタンスには、 そのエンドポイント参照で固有の ID が組み込まれます。 この ID は、Web サービスをターゲットとする、 後続のメッセージの SOAP ヘッダーで、 参照パラメーターになります。 また、Web サービスが着信メッセージを 適切なプリンターとマッチングさせるために、 この ID を使用できます。
web サービスが、WS-Addressing メッセージのアドレッシング・プロパティーを含むメッセージを 受信すると、WebSphere Application Server は、メッセージがアプリケーションのエンドポイントに ディスパッチされる前に、これらのプロパティーを処理し、スレッド上のメッセージ・コンテキストの中に、このプロパティーを設定します。 以下の例で説明されているとおり、 Printer Web サービス・アプリケーションは、WebServiceContext オブジェクトの ターゲット・エンドポイントに関連付けられている、 参照パラメーターにアクセスします。
import com.ibm.websphere.wsaddressing.EndpointReferenceManager;
...
// Initialize the reference parameter name
QName name = new QName(..);
// Extract the String value.
String resource_identifier =
EndpointReferenceManager.getReferenceParameterFromMessageContext(PRINTER_ID_PARAM_QNAME);
Web サービス実装は、getReferenceParameterFromMessageContext メソッドから獲得したプリンター ID に基づいて、メッセージを適切なプリンター・インスタンスに転送できます。
import javax.xml.ws.BindingProvider;
...
javax.xml.ws.Service service= ...;
Printer myPrinterProxy = service.getPort(portName, Printer.class);
javax.xml.ws.BindingProvider bp = (javax.xml.ws.BindingProvider)myPrinterProxy;
// Retrieve the request context for the BindingProvider object
Map myMap = myBindingProvider.getRequestContext();
// Associate the endpoint reference that represents the new printer to the request context
// so that the BindingProvider object now represents a specific printer instance.
myMap.put(WSADDRESSING_DESTINATION_EPR, destinationEpr);
...
これで、BindingProvider オブジェクトは、新規プリンター・リソース・インスタンスとなります。
クライアントはこのオブジェクトを使用して、Printer Web サービス経由でメッセージをプリンターに送信することができます。
クライアントが BindingProvider オブジェクトを起動するときに、WebSphere
Application Server は該当するメッセージ・アドレッシング・プロパティーをメッセージ・ヘッダーに追加します。
この場合、このプロパティーが、ターゲット・プリンター・リソースを識別する
エンドポイント参照内に含まれる参照パラメーターになります。代わりに、クライアントは JAX-RPC Stub オブジェクトまたは Call オブジェクトを使用することができます。クライアントはこれらを、新規プリンターを表すように構成します。 Call オブジェクトの使用方法は、以下の例のようになります。
import javax.xml.rpc.Call;
...
:
// Associate the endpoint reference that represents the new printer with the call.
call.setProperty(
"com.ibm.websphere.wsaddressing.WSAConstants.
WSADDRESSING_DESTINATION_EPR ", epr);
クライアント側から見ると、エンドポイント参照は不透明です。 クライアントは、いずれのエンドポイント参照パラメーターの内容も解釈できず、 完全に使用できません。 クライアントは、エンドポイント参照のインスタンスを直接作成できません。 これは、参照パラメーターが、サービス・プロバイダー専用になっているためです。 クライアントは、サービス・プロバイダーからエンドポイント参照を取得し (例えば、プロバイダー・ファクトリー・サービスを通じて)、 そのエンドポイント参照を使用して、 エンドポイント参照によって表されている エンドポイントに、Web サービス・オペレーションを送信する必要があります。
次のタスク
- WS-Addressing を使用するセキュリティーについての詳細は、Web サービス・アドレッシングのセキュリティーを参照してください。
- アプリケーションをデプロイします。 このシナリオの場合、クライアントに WS-Addressing プロパティーが指定されているため、追加のステップを実行して WebSphere Application Server で WS-Addressing サポートを使用可能に設定する必要はありません。 詳細情報、および追加のステップを必要とするその他のシナリオについては、 JAX-RPC アプリケーションに対する Web サービス・アドレッシング・サポートの使用可能化を参照してください。