You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2021/09/13 07:01:39 UTC

[GitHub] [apisix] tzssangglass commented on a change in pull request #5016: feat: xml-json-conversion plugin convert xml data from request body to json response, and vice versa

tzssangglass commented on a change in pull request #5016:
URL: https://github.com/apache/apisix/pull/5016#discussion_r707048234



##########
File path: rockspec/apisix-master-0.rockspec
##########
@@ -70,6 +70,8 @@ dependencies = {
     "ext-plugin-proto = 0.3.0",
     "casbin = 1.26.0",
     "api7-snowflake = 2.0-1",
+    "xml2lua = 1.5-2",
+    "lua-cjson = 2.1.0.6"

Review comment:
       `lua-cjson` has installed? refer to:https://github.com/apache/apisix/blob/baf843403461883c1334e63d15a6bb3622c31940/apisix/core/json.lua#L17
   cc @spacewander 

##########
File path: apisix/plugins/xml-json-conversion.lua
##########
@@ -0,0 +1,156 @@
+--
+-- 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 xml2lua = require("xml2lua")
+local handler = require("xmlhandler.tree")
+local cjson   = require('cjson.safe')
+local string  = require("string")
+
+local schema = {
+    type = "object",
+    properties = {
+        from = {
+            type = "string",
+            enum = {"json", "xml"},
+            default = "xml"
+        },
+        to = {
+            type = "string",
+            enum = {"json", "xml"},
+            default = "json"
+        }
+    },
+    additionalProperties = false,
+}
+
+local plugin_name = "xml-json-conversion"
+
+local _M = {
+    version = 0.1,
+    priority = 90,
+    name = plugin_name,
+    schema = schema,
+}
+
+function _M.check_schema(conf)
+    return core.schema.check(schema, conf)
+end
+
+local function xml2json(xml_data)
+    local convertHandler = handler:new()
+    local parser = xml2lua.parser(convertHandler)
+    parser:parse(xml_data)
+    return 200, cjson.encode(convertHandler.root)
+end
+
+local function json2xml(table_data)
+    local xmlStr = xml2lua.toXml(cjson.decode(table_data))
+    xmlStr = string.gsub(xmlStr, "%s+", "")
+    return 200, xmlStr
+end
+
+local _switch_anonymous = {
+    ["json"] = function(content_type, req_body, to)
+        if "application/json" ~= content_type then
+            return 400, {message = "Operation not supported"}
+        end
+
+        local data, err = core.json.decode(req_body)
+        if not data then
+            core.log.error("invalid request body: ", req_body, " err: ", err)
+            return 400, {error_msg = "invalid request body: " .. err,
+                         req_body = req_body}
+        end
+        if to == 'xml' then
+            return json2xml(req_body)
+        else
+            return 400, {message = "Operation not supported"}
+        end
+    end,
+    ["xml"] = function(content_type, req_body, to)
+        if "text/xml" ~= content_type then
+            return 400, {message = "Operation not supported"}
+        end
+        if to == 'json' then
+            return xml2json(req_body)
+        else
+            return 400, {message = "Operation not supported"}
+        end
+    end
+}
+
+function _M.access(conf, ctx)
+    local req_body, err = core.request.get_body()
+    if err or req_body == nil or req_body == '' then
+        core.log.error("failed to read request body: ", err)
+        core.response.exit(400, {error_msg = "invalid request body: " .. err})
+    end
+
+    local from = conf.from
+    local to = conf.to
+    if from == to then
+        return req_body
+    end
+
+    local content_type = core.request.headers()["Content-Type"]
+    local _f_anon = _switch_anonymous[from]
+    if _f_anon then
+        return _f_anon(content_type, req_body, to)
+    else
+        return 400, {message = "Operation not supported"}
+    end

Review comment:
       This code is almost the same as the following `get_json()`, can we extract it into a function?

##########
File path: apisix/plugins/xml-json-conversion.lua
##########
@@ -0,0 +1,156 @@
+--
+-- 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 xml2lua = require("xml2lua")
+local handler = require("xmlhandler.tree")
+local cjson   = require('cjson.safe')
+local string  = require("string")
+
+local schema = {
+    type = "object",
+    properties = {
+        from = {
+            type = "string",
+            enum = {"json", "xml"},
+            default = "xml"
+        },
+        to = {
+            type = "string",
+            enum = {"json", "xml"},
+            default = "json"
+        }
+    },
+    additionalProperties = false,
+}
+
+local plugin_name = "xml-json-conversion"
+
+local _M = {
+    version = 0.1,
+    priority = 90,

Review comment:
       in `config-defalt.yaml`, `priority` is 9, pls keep same.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org