若使用預設商業物件解析作業,您可以指定可插入的名稱處理常式, 以決定要在從 SOAP 訊息轉換成商業物件的作業中,使用哪個商業物件。 您可變更 MO_DataHandler_DefaultSOAPConfig 屬性來執行這個動作。
MO_DataHandler_DefaultSOAPConfig 具有兩個類型為 string 的屬性,而它們可指定:
public abstract String getBOName(Envelope msgEnv, SOAPProperty prop)
若 SOAPNameHandler 屬性有一個值,SOAP 資料處理常式就會呼叫所指定的名稱處理常式。 若該值不存在,或所指定的名稱處理常式無法取得商業物件名稱, 就會依預設來呼叫 SOAP 資料處理常式,以執行預設的商業物件解析作業。
SOAP DataHandler 會使用在 MO 中指定的 SOAPNameHandler 內容, 來建立自訂名稱處理常式類別的實例。然後它會呼叫 getBOName 來解析商業物件名稱。SOAP DataHanlder 會將它接收的 SOAPProperty 物件, 從連接器傳遞到自訂名稱處理常式實作類別。
這個 SOAPProperty 物件包含潛在候選 BO 的結構式清單以供解析。 清單中包含 BodyName、BodyNamespace 及 BOName 一組三個項目。 這組三個項目是根據 SOAP Config MO 配置資訊而來。 「預設名稱處理常式」會使用這個物件來解析 BO。 自訂名稱處理常式開發人員可自行判斷是否使用這個物件。
您可使用 SOAPPropertyUtils 類別,從 SOAPProperty 擷取商業物件名稱。 若要這樣做,請使用下列方法:
/** * Retrieve the business object name based on the body name and the body * namespace *. * @param soapProp top level SOAPProperty object that is passed by the * connector * @param name body name from the SOAP message * @param uri body namespace from the SOAP message * @return business object name from the SOAPProperty object with the body * name and body namespace. */ java.lang.String findBOName(SOAPProperty soapProp, String name, String uri);
以下是範例 NameHandler:package
com.ibm.adapters.datahandlers.soap.namehandlers; // DOM and Parsers import javax.xml.parsers.DocumentBuilder; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.xml.sax.InputSource; // Apache SOAP import org.apache.soap.Envelope; import org.apache.soap.Header; import org.apache.soap.Body; import org.apache.soap.Constants; import org.apache.soap.util.xml.DOMUtils; import org.apache.soap.util.xml.XMLParserUtils; import org.apache.soap.util.xml.QName; import org.apache.soap.encoding.soapenc.SoapEncUtils; import org.apache.soap.encoding.soapenc.Base64; // java import java.util.Vector; // SOAP data handler import com.ibm.adapters.datahandlers.soap.*; import com.ibm.adapters.datahandlers.soap.exceptions.*; public class MyCustomNameHandler extends SOAPNameHandler { private static final String BOPREFIX = "MyCustomBOPrefix"; private static final char UNDERSCORE = '_'; private static final char EMPTY_STRING = ""; public String getBOName(Envelope msgEnv, SOAPProperty prop) throws SOAPNameHandlerException { // Initialize a String Buffer StringBuffer boName = new StringBuffer(); // Determine the "MyCustomBOPrefix" SOAP data handler // MO property. If it exists, and is populated append // this prefix to the front of the BOName. String pref = dh.getOption(BOPREFIX); if (pref != null) { boName.append(pref.equals(EMPTY_STRING) ? EMPTY_STRING : pref + UNDERSCORE); } // Begin parsing the SOAP msg envelope. Element bodyEl, requestEl; Body msgBody = msgEnv.getBody(); Vector bodyEntries = msgBody.getBodyEntries(); if((bodyEntries == null) || (bodyEntries.size() <= 0)) throw new SOAPNameHandlerException("No Body Entries exist for this SOAP message. Cannot determine BOName to use."); // Grab the first <SOAP-ENV:Body> Element bodyEl = (Element) bodyEntries.elementAt(0); // Grab the first Child Element of the <SOAP-ENV:Body> // Element requestEl = (Element) DOMUtils.getFirstChildElement(bodyEl); // Read the name and namespace of this first child String name = bodyEl.getLocalName(); String uri = bodyEl.getNamespaceURI(); if (uri == null) uri = Constants.NS_URI_SOAP_ENV; // Use the SOAPPropertyUtils findBOName() method to search // the SOAPProperty object for this messages first element // name and namespace. If no match is found, a // SOAPDataHandlerException will be thrown. If a match is // found, and it's not an empty string, append to the boname. String returnedBOName = SOAPPropertyUtils.findBOName(prop, name, uri); if (returnedBOName != null && !returnedBOName.equals(EMPTY_STRING)) boName.append(returnedBOName); return boName.toString() } }