EAITool.java 5.02 KB
package com.huaheng.api.U8.tools;

import java.io.*;
import java.lang.reflect.Field;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import com.huaheng.api.U8.domain.Ufinterface;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.framework.aspectj.ApiLogAspect;
import com.huaheng.pc.monitor.apilog.domain.ApiLog;

public class EAITool {

    // 以下字段的含义,详见《U8开发之EAI接口.docx》中EAI接口规范->数据交换格式->请求消息格式
    public static final String ROOTTAG = "storeout"; // sql
    public static final String SENDER = "001"; // 发送者
    public static final String RECEIVER = "u8"; // 接收者
    public static final String PROC_ADD = "add"; // 处理——添加
    public static final String PROC_DEL = "delete"; // 处理——删除
    public static final String PROC_EDIT = "edit"; // 处理——编辑
    public static final String PROC_LIST = "query"; // 处理——查询所有
    public static final String CODEEXCHANGED = "N"; // 导入的代码未进行转码
    public static final String EXPORTNEEDEXCH = "N"; // 导出的代码不需要转码
    public static final String VERSION = "1.0"; // 版本

    public static String sendXML(String urlString, String xmlString) throws Exception {
        // 发送xml
        URL url = new URL(urlString);
        HttpURLConnection con = (HttpURLConnection)url.openConnection();
        // 将接口调用超时时间改为20秒
        con.setConnectTimeout(40000);
        con.setReadTimeout(40000);
        StringBuilder sb = new StringBuilder();
        String responseXml = null;
        ApiLog log = null;
        try {
            con.setConnectTimeout(4000000);
            con.setReadTimeout(4000000);
            con.setDoInput(true);
            con.setDoOutput(true);
            con.setAllowUserInteraction(false);
            con.setUseCaches(false);
            con.setRequestMethod("POST");
            con.setRequestProperty("Content-type", "application/json; charset=UTF-8");
            con.setRequestProperty("Accept-Charset", "UTF-8");
            // 初始化日志记录
            log = ApiLogAspect.initApiLog(con, xmlString, QuantityConstant.WAREHOUSECODE);
            // 发送Request消息
            OutputStream out = con.getOutputStream();
            DataOutputStream dos = new DataOutputStream(out);
            dos.write(xmlString.getBytes("UTF-8"));
            // 获取Response消息
            InputStream in = con.getInputStream();
            InputStreamReader reader = new InputStreamReader(in, "UTF-8");
            BufferedReader br = new BufferedReader(reader);

            String s = null;
            while ((s = br.readLine()) != null) {
                sb.append(s);
            }
        } catch (Exception e) {
            ApiLogAspect.setApiLogException(log, e);
            e.printStackTrace();
        } finally {
            responseXml = sb.toString();
            ApiLogAspect.finishApiLog(log, con, responseXml);
        }
        return responseXml;
    }

    public static void setErpDefine(Ufinterface.Storeout.Body.Entry entry, String fieldName, String batch) {
        Class<?> clazz = entry.getClass();
        try {
            Field field = clazz.getDeclaredField(fieldName);
            field.setAccessible(true);
            field.set(entry, batch);
            System.out.println(entry.getDefine32());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取xml指定参数内容
     * @param  xml
     * @param  param
     * @return
     */
    public static String getXmlParamContent(String xml, String param) {
        try {
            // 创建DocumentBuilderFactory实例
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            // 创建DocumentBuilder实例
            DocumentBuilder builder = factory.newDocumentBuilder();
            // 使用StringReader将XML字符串转换为InputSource
            InputSource inputSource = new InputSource(new StringReader(xml));
            // 解析XML字符串,得到Document对象
            Document document = builder.parse(inputSource);
            // 获取根元素
            Element rootElement = document.getDocumentElement();
            // 获取所有param元素
            NodeList paramNodes = rootElement.getElementsByTagName(param);
            // 遍历param元素,提取参数
            for (int i = 0; i < paramNodes.getLength(); i++) {
                Element paramElement = (Element)paramNodes.item(i);
                String value = paramElement.getTextContent().trim();
                return value;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return null;
    }
}