EBS调用webservice
EBS调用外部系统webservice服务,传递报文的程序包。
包头
CREATE OR REPLACE PACKAGE Xxcus_Int_Set_Send_Pkg IS
-- Author : BRAYDEN.LIU
-- Created : 2021/11/5 15:21:15
-- Purpose :
--发送http报文,会被频繁调用,需要独立出来避免锁
PROCEDURE Send_Http(p_Access_Token IN VARCHAR2
,p_Url IN VARCHAR2
,p_Http_Request IN CLOB
,p_Method IN VARCHAR2 DEFAULT 'POST'
,p_Http_Timeout IN NUMBER DEFAULT NULL
,p_Http_Proxy IN VARCHAR2 DEFAULT NULL
,p_Content_Type IN VARCHAR2 DEFAULT NULL
,p_Wallet_Path IN VARCHAR2 DEFAULT NULL
,Lv_Password IN VARCHAR2 DEFAULT NULL
,p_Wallet_Password IN VARCHAR2 DEFAULT ''
--,p_Auth_String IN VARCHAR2 DEFAULT NULL
,x_Http_Response OUT NOCOPY CLOB
,x_Return_Status OUT NOCOPY VARCHAR2
,x_Return_Message OUT NOCOPY VARCHAR2);
PROCEDURE Send_Rest(Pv_Url IN VARCHAR2
,p_Http_Timeout IN NUMBER DEFAULT NULL
,p_Http_Request IN CLOB
,x_Http_Response OUT NOCOPY CLOB
,x_Return_Status OUT NOCOPY VARCHAR2
,x_Return_Message OUT NOCOPY VARCHAR2);
FUNCTION Get_Url(Pv_Intsys_Code IN VARCHAR2) RETURN VARCHAR2 Result_Cache;
PROCEDURE Send_Interface(Pv_Intsys_Code IN VARCHAR2
,p_Http_Timeout IN NUMBER DEFAULT NULL
,p_Http_Request IN CLOB
,x_Http_Response OUT NOCOPY CLOB
,x_Return_Status OUT NOCOPY VARCHAR2
,x_Return_Message OUT NOCOPY VARCHAR2);
END Xxcus_Int_Set_Send_Pkg;
包体
CREATE OR REPLACE PACKAGE BODY Xxcus_Int_Set_Send_Pkg IS
PROCEDURE Log(Pv_Message IN VARCHAR2) IS
BEGIN
Dbms_Output.Put_Line(Pv_Message);
Fnd_File.Put_Line(Fnd_File.Log, Pv_Message);
END Log;PROCEDURE Log(Pv_Message IN CLOB) IS
Ln_Long NUMBER := 1000; --读取长度
Ln_Start NUMBER := 1; --开始读取位置
Ln_Length NUMBER;
Lv_Str VARCHAR2(2000);
BEGIN
Ln_Length := Dbms_Lob.Getlength(Pv_Message);
FOR i IN 1 .. Ceil(Ln_Length / Ln_Long) LOOP
Ln_Start := (1 + Ln_Long * (i - 1));
Dbms_Lob.Read(Pv_Message, Ln_Long, Ln_Start, Lv_Str);
Dbms_Output.Put_Line(Lv_Str);
Fnd_File.Put_Line(Fnd_File.Log, Lv_Str);
Ln_Long := Least(Ln_Long, (Ln_Length - Ln_Long));
END LOOP;
Dbms_Output.Put_Line(' ');
END Log;--发送http报文
PROCEDURE Send_Http(p_Access_Token IN VARCHAR2
,p_Url IN VARCHAR2
,p_Http_Request IN CLOB
,p_Method IN VARCHAR2 DEFAULT 'POST'
,p_Http_Timeout IN NUMBER DEFAULT NULL
,p_Http_Proxy IN VARCHAR2 DEFAULT NULL
,p_Content_Type IN VARCHAR2 DEFAULT NULL
,p_Wallet_Path IN VARCHAR2 DEFAULT NULL
,Lv_Password IN VARCHAR2 DEFAULT NULL
,p_Wallet_Password IN VARCHAR2 DEFAULT ''
--,p_Auth_String IN VARCHAR2 DEFAULT NULL
,x_Http_Response OUT NOCOPY CLOB
,x_Return_Status OUT NOCOPY VARCHAR2
,x_Return_Message OUT NOCOPY VARCHAR2) IS
Http_Req Utl_Http.Req;
Http_Resp Utl_Http.Resp;
l_Pos NUMBER := 1;
l_Leng NUMBER := 0;
TYPE Rec_Req IS RECORD(
p_Req VARCHAR2(4000));
TYPE Rec_Req_Array IS TABLE OF Rec_Req INDEX BY BINARY_INTEGER;
l_Res_Line VARCHAR2(32767);
l_Req_Array Rec_Req_Array;
l_End_Flag VARCHAR2(1);
l_Uuid VARCHAR2(100);
l_Guid VARCHAR2(100);
l_Auth VARCHAR2(100);
l_Db_Name VARCHAR2(100);
l_Password VARCHAR2(100);
Lc_Http_Response CLOB;
BEGIN
x_Return_Status := Fnd_Api.g_Ret_Sts_Success;
--Log('Send_Http:' || p_Http_Request);
BEGIN
SELECT Upper(Instance_Name)
INTO l_Db_Name
FROM V$instance;
EXCEPTION
WHEN OTHERS THEN
l_Db_Name := 'EBSDEV';
END;
IF Lv_Password IS NULL THEN
IF l_Db_Name = 'PROD' THEN
l_Password := 'ERP:4cDQ8$*M';
ELSE
l_Password := 'ERP:ERP123456';
END IF;
ELSE
l_Password := Lv_Password;
END IF;
IF p_Wallet_Path IS NOT NULL THEN
Utl_Http.Set_Wallet(Path => p_Wallet_Path, Password => p_Wallet_Password);
END IF;
IF p_Http_Timeout IS NOT NULL THEN
Utl_Http.Set_Transfer_Timeout(p_Http_Timeout);
ELSE
Utl_Http.Set_Transfer_Timeout(600);
END IF;
IF p_Http_Proxy IS NOT NULL THEN
Utl_Http.Set_Proxy(p_Http_Proxy);
END IF;
IF p_Http_Request IS NOT NULL THEN
FOR i IN 1 .. Ceil(Dbms_Lob.Getlength(p_Http_Request) / 2400) LOOP
l_Req_Array(i).p_Req := Dbms_Lob.Substr(p_Http_Request, 2400, l_Pos);
l_Pos := l_Pos + 2400;
l_Leng := l_Leng + Lengthb(l_Req_Array(i).p_Req);
END LOOP;
END IF;
--self.log('开始发送http请求');
Utl_Http.Set_Body_Charset('UTF-8');
Http_Req := Utl_Http.Begin_Request(p_Url, p_Method, 'HTTP/1.1');
IF p_Access_Token IS NOT NULL THEN
Utl_Http.Set_Header(r => Http_Req
,NAME => 'Authorization'
,VALUE => 'Bearer ' || p_Access_Token);
END IF;
--ESB第二版规范要求
l_Guid := Lower(Rawtohex(Sys_Guid()));
l_Uuid := Substr(l_Guid, 1, 8) || '-' || Substr(l_Guid, 9, 4) || '-' || Substr(l_Guid, 13, 4) || '-' ||
Substr(l_Guid, 17, 4) || '-' || Substr(l_Guid, 21, 12);
--生成BASIC认证信息,规则"Basic"+"空格"+“Base64(用户名+英文冒号+密码)”
l_Auth := Utl_Raw.Cast_To_Varchar2(Utl_Encode.Base64_Encode(Utl_Raw.Cast_To_Raw(l_Password)));
l_Auth := 'Basic ' || l_Auth;
--在HTTP协议头增加四个集成平台要求字段
Utl_Http.Set_Header(Http_Req, 'reqFrom', 'ERP'); --服务消费方ID
Utl_Http.Set_Header(Http_Req, 'Authorization', l_Auth); --认证字段,HttpBasic认证头信息
Utl_Http.Set_Header(Http_Req, 'reqTime', ''); --提交时间
Utl_Http.Set_Header(Http_Req, 'reqId', l_Uuid); --实例ID
--END 第二版规范
--self.log('1');
Utl_Http.Set_Header(Http_Req
,'Content-Type'
,Nvl(p_Content_Type, 'application/xml; charset=UTF-8'));
Utl_Http.Set_Header(Http_Req, 'Content-Length', l_Leng);
IF p_Http_Request IS NOT NULL THEN
FOR i IN 1 .. Ceil(Dbms_Lob.Getlength(p_Http_Request) / 2400) LOOP
Utl_Http.Write_Text(Http_Req, l_Req_Array(i).p_Req);
END LOOP;
END IF;
Http_Resp := Utl_Http.Get_Response(Http_Req);
Dbms_Output.Put_Line(Http_Resp.Reason_Phrase || '' || Http_Resp.Http_Version);
Dbms_Lob.Createtemporary(Lc_Http_Response, FALSE);
BEGIN
LOOP
l_Res_Line := NULL;
Utl_Http.Read_Text(Http_Resp, l_Res_Line, 800);
Dbms_Lob.Writeappend(Lc_Http_Response, Length(l_Res_Line), l_Res_Line);
END LOOP;
EXCEPTION
WHEN Utl_Http.End_Of_Body THEN
l_End_Flag := 'Y';
Utl_Http.End_Response(Http_Resp);
END;
x_Http_Response := Lc_Http_Response;
IF Nvl(l_End_Flag, 'N') <> 'Y' THEN
Utl_Http.End_Response(Http_Resp);
END IF;
--Log('send_http send success!!!');
EXCEPTION
WHEN OTHERS THEN
x_Return_Status := Fnd_Api.g_Ret_Sts_Error;
x_Return_Message := 'http send error:' || SQLERRM;
--Log('Send_Http error :' || x_Return_Status || ',' || x_Return_Message);
END Send_Http;PROCEDURE Send_Rest(Pv_Url IN VARCHAR2
,p_Http_Timeout IN NUMBER DEFAULT NULL
,p_Http_Request IN CLOB
,x_Http_Response OUT NOCOPY CLOB
,x_Return_Status OUT NOCOPY VARCHAR2
,x_Return_Message OUT NOCOPY VARCHAR2) IS
Lv_Url VARCHAR2(2000) := Pv_Url;
BEGIN
Send_Http(p_Access_Token => NULL
,p_Url => Lv_Url
,p_Http_Timeout => p_Http_Timeout
,p_Http_Request => p_Http_Request
,p_Content_Type => NULL --'application/json'
,x_Http_Response => x_Http_Response
,x_Return_Status => x_Return_Status
,x_Return_Message => x_Return_Message);
--Log('Send_Rest:' || x_Return_Status || ',' || x_Return_Message);
END Send_Rest;FUNCTION Get_Url(Pv_Intsys_Code IN VARCHAR2) RETURN VARCHAR2 Result_Cache IS
Lv_Url VARCHAR2(2000);
BEGIN
SELECT Env_Set.To_System_Url
INTO Lv_Url
FROM V$database Databse
--,Xxcus_Int_Set_Intsys_t Xisi
,Xxcus_Int_Set_Intenv_t Env_Set
,Xxcus_Int_Set_Environment_t Env_To
,Xxcus_Int_Set_Environment_t Env_From
,Xxcus_Int_Set_Intsys_t Xisi
WHERE 1 = 1
AND Env_From.Environment_Code = Databse.Name
AND Env_Set.To_Environment_Id = Env_To.Environment_Id
AND Env_Set.From_Environment_Id = Env_From.Environment_Id
AND Env_Set.Intsys_Id = Xisi.Intsys_Id
AND Xisi.Intsys_Code = Pv_Intsys_Code;
RETURN Lv_Url;
EXCEPTION
WHEN OTHERS THEN
RETURN NULL;
END Get_Url;PROCEDURE Send_Interface(Pv_Intsys_Code IN VARCHAR2
,p_Http_Timeout IN NUMBER DEFAULT NULL
,p_Http_Request IN CLOB
,x_Http_Response OUT NOCOPY CLOB
,x_Return_Status OUT NOCOPY VARCHAR2
,x_Return_Message OUT NOCOPY VARCHAR2) IS
Lv_Url VARCHAR2(2000);
BEGIN
x_Return_Status := Fnd_Api.g_Ret_Sts_Success;
Lv_Url := Get_Url(Pv_Intsys_Code);
IF Lv_Url IS NULL THEN
x_Return_Status := Fnd_Api.g_Ret_Sts_Error;
x_Return_Message := '获取接口“' || Pv_Intsys_Code || '”的url 异常';
END IF;
Send_Rest(Pv_Url => Lv_Url
,p_Http_Timeout => p_Http_Timeout
,p_Http_Request => p_Http_Request
,x_Http_Response => x_Http_Response
,x_Return_Status => x_Return_Status
,x_Return_Message => x_Return_Message);
--Log('Send_Interface:' || x_Return_Status || ',' || x_Return_Message);
END Send_Interface;BEGIN
-- Initialization
NULL;
END Xxcus_Int_Set_Send_Pkg;