次のコードおよび疑似コードは、送信側および送信側ハンドラーの実装例の概要を示します。
送信側ハンドラーの実装の概要です。開発者によってプロトコル固有のコードを追加する必要があります。
public class SampleSenderUserExit implements SenderInterface { SenderResult result = new SenderResult (); Connection connection = null; public SampleSenderUserExit() { ... } public void init(Context context, Config deliveryConfig) throws BCGSenderException { //initialization code ... } public SenderResult send(BusinessDocumentInterface document) throws BCGSenderException { //destination url set in the BusinessDocumentInterface //is preferred over url set in the Gateway URL url = (String)document.getAttribute(DocumentConstant.URI); if (url == null) url = (String)config.getConfig(DocumentConstant.URI); //obtain other configuration information from the //DeliveryConfig which holds the configuration information //set in the Gateway File documentFile = document.getDocument(); try { //read contents from File ... } catch(FileIOException exception) { BCGSenderException e = new BCGSenderException(...); throw e; } try { //establish connection with the destination ... //send contents to destination ... //if DocumentConst.GET_SYNC_RESPONSE is true, //read the response if (String.valueOf(document.getAttribute( DocumentConst.GET_SYNC_RESPONSE). equalsIgnoreCase(DocumentConstants.TRUE)) { //read the response ... String uuid = BCGUtil.getDocumentUUID(); String tmpDir = BCGUtil.TMP_DIR_PATH; File responseFile = document.createFile(uuid+".resp"); //write response to file ... result.setResponse(responseFile); ... } else { //read the transport response and //set in the SenderResult object. ... } } catch(Exception exception) { //create an event and add to the SenderResult Object[] params = {exception}; EventInfo eventInfo = EventInfo(eventCode, document, params); result.addEvent(eventInfo); result.setStatus(DocumentConstant.BCG_DOC_FAILED); return result; } //close the connection ... result.setStatus(DocumentConstant.BCG_DOC_SENT); return result; } public SenderResult cancel() throws BCGSenderException { connection.close(); EventInfo eventInfo = EventInfo(eventCode, document, params); result.setEvent(eventInfo); return result; } }
次のコードおよび疑似コードは、送信側の前処理ハンドラーの実装例の概要を示します。設定済みカスタマーが toPartner の場合、それに応じて BusinessDocument に一部のトランスポート属性を設定します。
public class SampleSenderPreProcessUserExit implements SenderPreProcessHandlerInterface { public SampleSenderPreProcessUserExit () { ... } public void init(Config handlerConfig) throws BCGSenderException { //initialization code ... } public boolean applies(Config deliveryConfig, BusinessDocumentInterface doc) throws BCGSenderException { String toProtocol = document.getAttribute(DocumentConstants.TOPROTOCOLNAME); boolean isAS2 = (toProtocol!= null && toProtocol.equalsIgnoreCase(DocumentConstants.AS2)); return isAS2; } public BusinessDocumentInterface process (Config deliveryConfig, BusinessDocumentInterface doc) throws BCGSenderException { String toPartnerId = (String)doc.getAttribute (DocumentConstants.TOBUSINESSID); //check if partner is a PreferredCustomer ... if (preferredCustomer) { //set transport attributes for PreferredCustomer doc.setAttribute(PRIORITY, "HIGH"); doc.setAttribute(ACKNOWLEDGEMENT_REQUIRED, "true"); ... } } }
次のコードおよび疑似コードは、送信側の後処理ハンドラーの実装例の概要を示します。このサンプルでは、SOAP メッセージを想定しています。応答が例外または他のタイプのエラーであるが SOAP 障害ではない場合、ハンドラーは SOAP 障害を作成して、BusinessDocument に応答を設定します。
public class SampleSOAPPostProcessUserExit implements SenderPostProcessHandlerInterface { public void init(Config handlerConfig) throws BCGSenderException { //initialization code ... } public SampleSOAPPostProcessUserExit () { ... } public boolean applies(Config deliveryConfig, BusinessDocumentInterface doc) throws BCGSenderException { String fromProtocol = (String)document.getAttribute( DocumentConst.FRPROTOCOLNAME); boolean isSoap = (fromProtocol != null && fromProtocol.equalsIgnoreCase( DocumentConst.WEB_SERVICE_PROTOCOL)); return isSoap; } public SenderResult process (SenderResult response, BusinessDocumentInterface doc) throws BCGSenderException { SoapFault sf = null; String deliveryStatus= response. getDeliveryStatus(); If (deliveryStatus.equalsIgnoreCase("FAILED")){ //Get transport header, read the http status code, // and create SOAP fault message String faultMessage="Server error"; sf = new SoapFault(faultString); response.setResponse(sf.getBytes()); } return response; } }