NutzCN Logo
问答 关于自己实现一个适配器的问题
发布于 1940天前 作者 文涛(wentao) 1408 次浏览 复制 上一个帖子 下一个帖子
标签:

现在有个情况是需要接收外部post过来的xml,于是有了如下实现
1、日志部分,新增了xml的解析方式:

/**
 * 日志处理类,用于打印所有接口出入参日志
 */
public class LogTimeProcessor extends AbstractProcessor {

    private static final Log log = Logs.get();

    @Override
    public void process(ActionContext ac) throws Throwable {
        Stopwatch sw = Stopwatch.begin();
        try {
            doNext(ac);
        } finally {
            sw.stop();
            HttpServletRequest req = ac.getRequest();
            String ip = Lang.getIP(req);
            String path = ac.getPath();
            String result = Json.toJson(ac.getMethodReturn(), JsonFormat.tidy());
            switch (req.getMethod().trim()) {
                case "GET":
                    log.infof("[%s] [clientIP: %s] [url:%s] params:%s result:%s time:%sms", "GET", ip, path, getParameters(req), result, sw.getDuration());
                    break;
                case "POST":
                    if (req.getContentType().contains("json")) {
                        log.infof("[%s] [clientIP: %s] [url:%s] params:%s result:%s time:%sms", "POST JSON", ip, path, Json.toJson(ac.getMethodArgs(), JsonFormat.tidy()), result, sw.getDuration());
                    } else if(req.getContentType().contains("xml")) {
                        byte[] bytes = new byte[req.getInputStream().available()];
                        req.getInputStream().read(bytes);
                        String xml = new String(bytes, "UTF-8");
                        log.infof("[%s] [clientIP:%s] [url:%s] params:%s result:%s time:%sms", "POST XML", ip, path, xml, result, sw.getDuration());
                    } else {
                        log.infof("[%s] [clientIP: %s] [url:%s] params:%s result:%s time:%sms", "POST FORM", ip, path, getParameters(req), result, sw.getDuration());
                    }
                    break;
            }
        }
    }

    private String getParameters(HttpServletRequest req) {
        // 按json格式返回参数
        // return Json.toJson(req.getParameterMap(), JsonFormat.tidy());
        // 这里是按pv模式组织参数
        List<String> params = new ArrayList<>();
        Map map = req.getParameterMap();
        for (Object o : map.keySet()) {
            params.add(o.toString() + "=" + req.getParameter(o.toString()));
        }
        return Strings.join("&", params);
    }
}

2、自定义适配器:

public class StringAdaptor implements HttpAdaptor {
    @Override
    public void init(Method method) {

    }

    @Override
    public Object[] adapt(ServletContext sc, HttpServletRequest req, HttpServletResponse resp, String[] pathArgs) {
        byte[] bytes = new byte[0];
        try {
            bytes = new byte[req.getInputStream().available()];
            req.getInputStream().read(bytes);
            String xml = new String(bytes, "UTF-8");
            return new Object[] { xml };
        } catch (IOException e) {
            e.printStackTrace();
        }

        return new Object[0];
    }
}

3、接口部分:

    @At("/order/test")
    @AdaptBy(type = StringAdaptor.class)  //这一行如果增加了,就能正确打印value,但是日志部分就没有打印了,如果不加,日志就能打印
    public String test(String value) {
        System.out.println("--------------------------------value:" + value);
        return "test result";
    }

不知道是哪里还漏掉了什么处理,然后日志部分的req就取不到inputStream了, 是不是因为在Adaptor里面已经read了

4 回复
byte[] bytes = new byte[0];

这也能跑通??

try里面重新定义了大小,呵呵

    @Override
    public Object[] adapt(ServletContext sc, HttpServletRequest req, HttpServletResponse resp, String[] pathArgs) {
        byte[] bytes = new byte[0];
        try {
            String str = Streams.read(Streams.utf8r(req.getInputStream());
            req.setAttribute("req_xml", str);
            return new Object[]{str};
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new Object[0];
    }
                case "POST":
                    if (req.getAttribute("req_xml") != null) {
                         log.infof("[%s] [clientIP:%s] [url:%s] params:%s result:%s time:%sms", "POST XML", ip, path, xml, req.getAttribute("req_xml"), sw.getDuration());
                    }
                    else if (req.getContentType().contains("json")) {
                        log.infof("[%s] [clientIP: %s] [url:%s] params:%s result:%s time:%sms", "POST JSON", ip, path, Json.toJson(ac.getMethodArgs(), JsonFormat.tidy()), result, sw.getDuration());
                    } else if(req.getContentType().contains("xml")) {
                        byte[] bytes = new byte[req.getInputStream().available()];
                        req.getInputStream().read(bytes);
                        String xml = new String(bytes, "UTF-8");
                        log.infof("[%s] [clientIP:%s] [url:%s] params:%s result:%s time:%sms", "POST XML", ip, path, xml, result, sw.getDuration());
                    } else {
                        log.infof("[%s] [clientIP: %s] [url:%s] params:%s result:%s time:%sms", "POST FORM", ip, path, getParameters(req), result, sw.getDuration());
                    }

嗯,可以了,感谢兽哥!

添加回复
请先登陆
回到顶部