卓望MISC1.6 的provision --无web服务器

根据那个图片上传处理服务器的思路,小改了一个处理器.尚未测试.
使用dom4j解析收到的XML 并返回处理结果

需要JDK6

/**
 * provision
 * @author rikugun
 */
public class CMPP3Provision {

    private static HttpServer hs;
    private static Logger logger = Logger.getLogger(CMPP3Provision.class.getName());

    static {
        try {
            logger.debug("初始化Http Server");
            hs = HttpServer.create(new InetSocketAddress(1234), 0); //设置HttpServer的端口为1234
            hs.createContext("/provision", new ProvisionHandler());
        } catch (IOException ex) {
            logger.warn("初始化Provision出错!", ex);
        }

    }

    public CMPP3Provision() {
    }

    public void Start() {
        hs.start();
    }

    public void Stop() {
        hs.stop(1);
    }
}


处理模块
/**
 * provision处理模块
 * @author rikugun
 */
public class ProvisionHandler implements HttpHandler {

    private static Logger logger = Logger.getLogger(ProvisionHandler.class.getName());
    private static Document dom, repDom;
    private static SAXReader saxReader = new SAXReader();

    @Override
    public void handle(HttpExchange t) throws IOException {
        logger.debug("获取同步信息,来自:"+t.getRemoteAddress().toString());
        logger.debug(t.getRequestHeaders().entrySet().toString());
        OutputStream os = t.getResponseBody();
        try {
            dom = saxReader.read(t.getRequestBody());
            repDom = doSync(dom);
            t.sendResponseHeaders(200, repDom.asXML().length());
            os.write(repDom.asXML().getBytes());
        } catch (DocumentException ex) {
            logger.warn("解析XML错误", ex);
            
        }

        os.close();
    }

    private Document doSync(Document dom) {
       logger.debug("--------获取到的XML--------");
        logger.debug(dom.asXML());
        logger.debug("--------获取到的XML--------");

        Element header = (Element) dom.getRootElement().element("Header");
        //transactionID
        String transactionID = header.elementText("TransactionID");
        Element body = (Element) dom.getRootElement().element("Body");
        Element syncOrderRelationReq = (Element) body.element("SyncOrderRelationReq");
        //UserId
        List<Element> msidn = body.selectNodes("ActionID");
        List<Element> user = body.selectNodes("//FeeUser_ID");

        //动作类型
        String actionId = syncOrderRelationReq.elementText("ActionID");

        //动作原因
        String actionReasonId = syncOrderRelationReq.elementText("ActionReasonID");

        //业务ID
        String serviceId = syncOrderRelationReq.elementText("SPServiceID");
        
        //附加参数
        String featureStr = syncOrderRelationReq.elementText("FeatureStr");

        //TODO 更新数据库中的用户订购信息

        SyncOrderRelationResp ret = new SyncOrderRelationResp(transactionID,"0");
        logger.debug("--------返回的XML--------");
        logger.debug(ret.getDocument().asXML());
        logger.debug("--------返回的XML--------");
        //返回执行成功的XML
        return ret.getDocument();
    }
}



辅助类
/**
 * 同步信息响应
 * @author rikugun
 */
public class SyncOrderRelationResp {
    //原始XML
    private static String raw = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
            "<SOAP-ENV:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\n" +
            " <SOAP-ENV:Header>\n" +
            "<TransactionID xmlns=\"http://www.monternet.com/dsmp/schemas/\">00240301659556</TransactionID>\n" +
            "</SOAP-ENV:Header>\n" +
            "<SOAP-ENV:Body>\n" +
            "<SyncOrderRelationResp xmlns:=\"http://www.monternet.com/dsmp/schemas/\">\n" +
            "<Version xsi:type=\"xsd:string\">1.5.0</Version>\n" +
            "<MsgType xsi:type=\"xsd:string\">SyncOrderRelationResp</MsgType>\n" +
            "<hRet xsi:type=\"xsd:integer\">1</hRet>\n" +
            "</SyncOrderRelationResp>\n" +
            "</SOAP-ENV:Body>\n" +
            "</SOAP-ENV:Envelope>";
    private static Document dom;
    private static Element transaction, hRet;

    static {
        try {
            dom = DocumentHelper.parseText(raw);
        } catch (DocumentException ex) {
            Logger.getLogger(SyncOrderRelationResp.class.getName()).warn("生成响应XML出错!", ex);
        }
        transaction = dom.getRootElement().element("Header").element("TransactionID");
        hRet = dom.getRootElement().element("Header").element("hRet");
    }

    /**
     * 定制消息反馈
     * @param transactionID 事务ID
     */
    public SyncOrderRelationResp(String transactionID) {
        transaction.setText(transactionID);
    }

    /**
     * 定制消息反馈
     * @param transactionID 事务ID
     * @param ret 返回值
     */
    public SyncOrderRelationResp(String transactionID, String ret) {
        transaction.setText(transactionID);
        hRet.setText(ret);
    }

    /**
     * 获取生成的结果Document
     * @return
     */
    public Document getDocument() {
        return dom;
    }
}

你可能感兴趣的:(xml,Web,OS)