apisix.tree:https://download.csdn.net/download/FSWZYC/88541181
apisix.xml2lua:https://download.csdn.net/download/FSWZYC/88541178
--
-- Licensed to the Apache Software Foundation (ASF) under one or more
-- contributor license agreements. See the NOTICE file distributed with
-- this work for additional information regarding copyright ownership.
-- The ASF licenses this file to You under the Apache License, Version 2.0
-- (the "License"); you may not use this file except in compliance with
-- the License. You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
local core = require("apisix.core")
local http = require("resty.http")
local xml2lua = require("apisix.xml2lua")
local handler = require("apisix.tree")
local plugin_name = "webservice-proxy"
local schema = {
type = "object",
properties = {
namespace = {
type = "string",
minLength = 1,
},
method_name = {
type = "string",
minLength = 1,
},
url = {
type = "string",
minLength = 1,
},
},
required = { "namespace", "method_name", "url"},
}
local _M = {
version = 0.1,
priority = 9999,
name = plugin_name,
schema = schema,
}
function _M.check_schema(conf)
return core.schema.check(schema, conf)
end
function _M.access(conf, ctx)
ngx.req.read_body()
--core.log.warn(ngx.req.get_body_data())
local bodyData = ngx.req.get_body_data();
local parameter = ""
if bodyData ~= nil then
for k, v in pairs(core.json.decode(ngx.req.get_body_data())) do
parameter = parameter .. "<web:" .. k .. ">" .. v .. "</web:" .. k .. ">\n"
end
end
-- 示例调用
local method = "POST"
local headers = {
["Content-Type"] = "text/xml;charset=utf-8",
-- 添加其他必要的请求头
}
local body = [[
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="%s">
<soapenv:Header/>
<soapenv:Body>
<web:%s>
%s
</web:%s>
</soapenv:Body>
</soapenv:Envelope>
]]
local request_xml = string.format(body, conf.namespace, conf.method_name, parameter, conf.method_name)
local response, err = Call_webservice(conf.url, method, headers, request_xml)
if err then
core.log.warn("failed to call webservice: ", err)
-- 处理错误情况
return 500, err;
else
-- 处理正常情况
local hadle_new = handler:new()
local parser = xml2lua.parser(hadle_new)
parser:parse(response)
local myTable = hadle_new.root
local envelope = myTable["Envelope"]["Body"];
--handler.root = nil
return 200, core.json.encode(envelope);
end
end
function Call_webservice(url, method, headers, body)
-- 创建 HTTP 客户端
local httpc = http.new()
-- 发起请求
local res, err = httpc:request_uri(url, {
method = method,
headers = headers,
body = body
})
-- 检查请求是否成功
if not res then
core.log.err("failed to request: ", err)
return nil, err
end
-- 返回响应
return res.body, nil
end
return _M