ヘッダーをスキップ
Oracle® Fusion Middleware Oracle Web Services Manager拡張ガイド
11gリリース1 (11.1.1.6)
B66193-01
  ドキュメント・ライブラリへ移動
ライブラリ
製品リストへ移動
製品
目次へ移動
目次

前へ
前へ
 
次へ
次へ
 

4 カスタム・アサーションの高度な機能の実装

Oracle Web Services Manager Java APIリファレンスでは、カスタム・アサーションに高度な機能を実装するために使用するパッケージ、インタフェースおよびメソッドを指定します。この章では、APIを使用して一部の共通の機能を実装する方法について説明します。この章では、APIの使用および例外処理について説明します。内容は、次のとおりです。

4.1 カスタム・アサーションへのパラメータの入力

次のインタフェースおよびメソッドを使用して、カスタム・アサーションにパラメータを入力できます。

カスタム・アサーションにパラメータを入力するには、次の手順を実行します。

  1. カスタム・アサーション内部に、パラメータをプロパティとして指定します。この例では、valid_ipsという名前が付いたorawsp:PropertySetがプロパティのグループを定義します。orawsp:Property要素は単一のプロパティを定義します。orawsp:Valueではプロパティの有効な値のリストを定義します。

    <orawsp:PropertySet orawsp:name="valid_ips">
        <orawsp:Property orawsp:name="valid_ips" orawsp:type="string"
     orawsp:contentType="constant">
         <orawsp:Value>140.87.6.143,10.178.93.107</orawsp:Value>
         </orawsp:Property>
     </orawsp:PropertySet>
    
  2. 対応するポリシーのカスタム・エグゼキュータ内部のパラメータにアクセスします。たとえば、カスタム・アサーションのExecutorクラスの実行メソッドの次のコードは、プロパティvalid_ipsにアクセスします。

    IAssertionBindings bindings =
     ((SimpleAssertion)(this.assertion)).getBindings();
    IConfig config = bindings.getConfigs().get(0);
    IPropertySet propertyset = config.getPropertySets().get(0);   
    String valid_ips = propertyset.getPropertyByName("valid_ips").getValue();
    

4.2 Oracle WSMコンテキスト・プロパティへのアクセス

MessageContextインタフェースを使用して、Oracle WSMコンテキスト・プロパティにアクセスします。

次のインタフェースおよびメソッドを使用して、カスタム・アサーションへのパラメータにアクセスします。

Oracle WSMコンテキスト・プロパティにアクセスするには、次の手順を実行します。

  1. カスタム・アサーション・エグゼキュータ内部のOracle WSMコンテキスト・プロパティにアクセスするには、IMessageContextインタフェースを使用します。次に例を示します。

    IMessageContext messagecontext = (IMessageContext) context;         
    messagecontext.getServiceURL();
    
  2. カスタム・アサーション・エグゼキュータ内部にある特定のプロパティの値にアクセスするには、IMessageContextインタフェースを使用します。次に例を示します。

    messagecontext.getProperty("<property name>");

  3. カスタム・アサーション・エグゼキュータ内部から実行中に使用されるすべてのプロパティにアクセスするには、IMessageContextインタフェースを使用します。次に例を示します。

    msgContextProperties = messagecontext.getAllProperties();

4.3 リクエスト、レスポンスおよびフォルト・メッセージ・オブジェクトへのアクセス

Oracle WSMカスタム・セキュリティ・アサーションには、リクエスト、レスポンスおよびフォルトの3つのステージがあります。

コンテキスト情報(ステージやメッセージなど)はコンテキスト・プロパティを使用して渡され、IMessageContextインタフェースによって取得されます。次のインタフェースとメソッドは、コンテンツ・プロパティにアクセスするために使用します。

リクエスト、レスポンスおよびフォルトのメッセージやステージにアクセスするには、次の手順を実行します。

  1. ステージにアクセスするには、カスタム・アサーション・エグゼキュータ内で次のコードを使用します。

    IMessageContext.STAGE stage = ((IMessageContext) iContext).getStage();
               if (stage == IMessageContext.STAGE.request) {
                         //handle request
                }
                
                if (stage == IMessageContext.STAGE.response) {
                       //handle response
                }
                
                if (stage == IMessageContext.STAGE.fault) {
                    //handle fault conditions
                }
    
  2. SOAPリクエスト・メッセージを取得するには、次の例に示すように、同じコンテキストのoracle.wsm.common.sdk.IMessageContextを使用します。

    oracle.wsm.common.sdk.SOAPBindingMessageContext soapMsgCtxt=
     (oracle.wsm.common.sdk.SOAPBindingMessageContext) context;
    javax.xml.soap.SOAPMessage soapMessage = soapMsgCtxt.getRequestMessage();
    
  3. SOAPレスポンス・メッセージを取得するには、次の例に示すように、同じコンテキストのoracle.wsm.common.sdk.IMessageContextを使用します。

    oracle.wsm.common.sdk.SOAPBindingMessageContext soapMsgCtxt=
     (oracle.wsm.common.sdk.SOAPBindingMessageContext) context;
    javax.xml.soap.SOAPMessage soapMessage = soapMsgCtxt.getResponseMessage ();
    

4.4 XPathによるメッセージのパーツへのアクセス

カスタム・ポリシー・エグゼキュータ内のXPath式を使用して、SOAPメッセージのパーツにアクセスできます。

次のSOAPメッセージ例では、ノードarg0の値はjohnです。

         <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
           <S:Header/>
             <S:Body>
               <ns2:echo xmlns:ns2="http://project1/">
                <arg0>john</arg0>
               </ns2:echo>
             </S:Body>
         </S:Envelope>

XPathには、要素、属性、テキスト、ネームスペース、処理の指示、コメントおよびドキュメント・ノードの7種類のノードがあります。XPathでは、パス式を使用してXMLドキュメントのノードを選択します。表4-1は、XPath式の例を示しています。

表 4-1 XPath式の例

説明

/S:Envelope

ルート要素S:Envelopeから選択します。

/S:Envelope/S:Body

S:Envelopeの子であるすべてのS:Body要素を選択します。

//S:Body

ドキュメント内のどこにあっても、すべてのS:Body要素を選択します。


たとえば、ノードarg0の値にアクセスするには、前述のSOAPメッセージのarg()のXPath式は次のように定義されます。

//xpath expression that will be used to identify the node arg0
     String xpathStr = "/S:Envelope/S:Body/ns2:echo/arg0";
 

XPath式が参照する任意のネームスペースのためのネームスペースを定義し、これらをネームスペース・コンテキストに追加します。次に例を示します。

     final DefaultNamespaceContext nsContext = new DefaultNamespaceContext();
     nsContext.addEntry("S", "http://schemas.xmlsoap.org/soap/envelope/");
     nsContext.addEntry("ns2", "http://project1/");
              
     XPathFactory xpathFact = XPathFactory.newInstance();
     XPath xpath = xpathFact.newXPath();
     xpath.setNamespaceContext(nsContext);

評価メソッドを使用して、ノードの値を取得します。次に例を示します。

     //This will return node arg0 from SOAP message, here soapElement is  
// org.w3c.dom.Elemet  representation of SOAP message
     org.w3c.dom.Node inputNode = (Node)xpath.evaluate(xpathStr, soapElement,
 XPathConstants.NODE);

4.5 SSLのコンテナで使用される証明書へのアクセス

SSLの証明書を取得するには、次の手順を実行します。

  1. 次の例に示すように、同じコンテキストのoracle.wsm.common.sdk.IMessageContextを使用して、SOAPレスポンス・メッセージを取得します。

    oracle.wsm.common.sdk.SOAPBindingMessageContext soapMsgCtxt=
     (oracle.wsm.common.sdk.SOAPBindingMessageContext) context;
                 
    
  2. HttpServletRequestインタフェースを使用して、サーブレット・リクエスト情報を取得します。

    javax.servlet.http.HttpServletRequest request =
     (javax.servlet.http.HttpServletRequest)
     soapMsgCtxt.getTransportObject(ITransportContext.HTTP_SERVLETREQUEST_OBJ_
    NAME);
    
  3. X.509証明書の属性にアクセスします。次に例を示します。

    X509Certificate[] certificates = (X509Certificate[])
     request.getAttribute(oracle.wsm.security.util.SecurityConstants.SSL_PEER_
    CERTIFICATES);
    

注意:

javax.servlet.http.HttpServletRequestインタフェースを使用するには、JARファイルmodules/javax.servlet_1.0.0.0_2-5.jarをクラスパスに含める必要があります。

4.6 トランスポート・プロパティへのアクセス

HTTPリクエストおよびレスポンスなどのトランスポート・オブジェクトにアクセスするには、次の手順を実行します。

  1. 次の例に示すように、同じコンテキストのoracle.wsm.common.sdk.IMessageContextを使用して、SOAPレスポンス・メッセージを取得します。

    oracle.wsm.common.sdk.SOAPBindingMessageContext soapMsgCtxt=
     (oracle.wsm.common.sdk.SOAPBindingMessageContext) context;
    
  2. HttpServletRequestインタフェースを使用して、次の例に示すように、HTTPサーブレットのサーブレット・リクエスト情報を取得します。

        //to retrieve http servlet request object
         HttpServletRequest request = (HttpServletRequest) soapMsgCtxt
                .getTransportObject(ITransportContext.HTTP_SERVLETREQUEST_OBJ_
    NAME);
    
  3. HttpServletRequestインタフェースを使用して、次の例に示すように、HTTPサーブレットのサーブレット・レスポンス情報を取得します。

        //to retrieve http servlet response object
         HttpServletResponse response = (HttpServletResponse) soapMsgCtxt
               .getTransportObject(ITransportContext.HTTP_SERVLETRESPONSE_OBJ_
    NAME);
    

注意:

javax.servlet.http.HttpServletRequestインタフェースを使用するには、JARファイルmodules/javax.servlet_1.0.0.0_2-5.jarをクラスパスに含める必要があります。

4.7 CSFキーへのアクセス

資格証明ストア・フレームワーク(CSF)を使用して、資格証明を安全に管理し、資格証明を保存、取得および維持できます。

CSFを構成するには、次の手順を実行します。

  1. ps-config.xmlでCSFを構成します。

    WLSTを使用した資格証明ストアの構成の詳細は、『Oracle WebLogic Server WebLogic Webサービスの保護』のWLSTを使用した資格情報ストアの構成に関する項を参照してください。

  2. カスタム・アサーション・エグゼキュータ内部のCSFからCSFキーを追加、更新または取得できます。例4-1および例4-2では、資格証明ストアからCSFキーにアクセスするために使用できるサンプル・コードを示しています。

    例4-1 CSFキーへのアクセス

       final String mapName = "oracle.wsm.security";
       final csfKey = "user.credentials";
       final oracle.security.jps.service.credstore.PasswordCredential userCreds =
     getCredentialsFromCSF(mapName, csfKey);
     
              if (userCreds != null) {
                    System.out.println("name:"+ userCreds.getName());
                    System.out.println("name:"+ new String(userCreds.getPassword()));
                                      }
        
    

    例4-2 getCredentialsFromCSFメソッド

    private static oracle.security.jps.service.credstore.PasswordCredential 
    getCredentialsFromCSF(
            final String mapName, final String csfKey) {
            oracle.security.jps.service.credstore.PasswordCredential
            passwordCredential = null;
            try {
                if (csfKey != null) {
                final oracle.security.jps.service.credstore.CredentialStore credStore
                = getCredStore();
                if (credStore != null) {
                passwordCredential = (oracle.security.jps.service.credstore.PasswordCredential)  java.security.AccessController
                .doPrivileged(new
    java.security.PrivilegedExceptionAction<oracle.security.jps.service.credstore.
    Credential>() { 
                 public
     oracle.security.jps.service.credstore.Credential run() throws Exception {
                  return (credStore .getCredential(mapName, csfKey));
                                }
                            });
                    } else {
                        // failure obtaining csf credentials
                    }
                }
            } catch (final java.security.PrivilegedActionException ex) {
                //handle excpetion
            } catch (final oracle.security.jps.JpsException jpse) {
                //handle excpetion
            }
            return passwordCredential;
        }
     
     
        private static oracle.security.jps.service.credstore.CredentialStore
     getCredStore() throws oracle.security.jps.JpsException {
            oracle.security.jps.service.credstore.CredentialStore csfStore;
            oracle.security.jps.service.credstore.CredentialStore appCsfStore = null;
            oracle.security.jps.service.credstore.CredentialStore systemCsfStore =
     null;
     
            final oracle.security.jps.internal.api.runtime.ServerContextFactory
     factory = (oracle.security.jps.internal.api.runtime.ServerContextFactory)
     oracle.security.jps.JpsContextFactory
                .getContextFactory();
     
            final oracle.security.jps.JpsContext jpsCtxSystemDefault =
     factory.getContext(oracle.security.jps.internal.api.runtime.ServerContextFactory.S
    cope.SYSTEM);
     
            final oracle.security.jps.JpsContext jpsCtxAppDefault = factory
                .getContext(oracle.security.jps.internal.api.runtime.ServerContextFactory.Scope.AP
    PLICATION);
     
            appCsfStore = (jpsCtxAppDefault != null) ? jpsCtxAppDefault
                .getServiceInstance(oracle.security.jps.service.credstore.CredentialStore.class) :
     null;
     
            if (appCsfStore == null) {
                systemCsfStore = jpsCtxSystemDefault
                    .getServiceInstance(oracle.security.jps.service.credstore.CredentialStore.class);
                csfStore = systemCsfStore;
            } else {
                //use Credential Store defined in app-level jps-config.xml
                csfStore = appCsfStore;
            }
            return csfStore;
        }
    

注意:

JARファイルoracle.jps_11.1.1/jps-api.jar、oracle.jps_11.1.1/jps-unsupported-api.jarは、クラスパスに含まれている必要があります。

system-jazn-data.xmlファイルで、カスタム・ポリシー・エグゼキュータに対して、必要な権限を指定する必要があります。次に例を示します。

   <grant>
            <grantee>
                <codesource>
                    <url>file:{location of custom policy executor jar}</url>
                </codesource>
            </grantee>
            <permissions>
                <permission>
                    <class>oracle.security.jps.service.credstore.CredentialAccessPermission</class>
                    <name>context=SYSTEM,mapName=*,keyName=*</name>
                    <actions>*</actions>
                </permission>
               </permissions>
        </grant>
 

4.8 カスタム・アサーションでの例外処理

カスタム・アサーション実行中の例外は、すべてWSMExceptionがカスタム・アサーション・エグゼキュータで処理する必要があります。

IResult execute(IContext mcontext) throws WSMException

このメソッドは、常に、null以外のIResultオブジェクトを返します。ステータス・フィールドは、成功、失敗または他の状態を示します。IResult.getFault()メソッドは失敗の詳細な原因を返すために使用され、成功の場合はnullを返します。

次の例に示すように、カスタム・アサーション・エグゼキュータの実行メソッドから発生している例外は、まず、WSMExceptionでラップする必要があります。実行ステータスがIResult.FAILEDに設定され、generateFault methodがWSMExceptionをスローします。

IResult execute(IContext mcontext) throws WSMException {
   IResult result = new Result();
   try {
      ....
      .....
 
   } catch (Exception e) {
      WSMException wsmException = new WSMException(e);
      result.setStatus(IResult.FAILED);
      generateFault(wsmException);
   }
}