`

MAS系统的ApiClient源码

    博客分类:
  • Java
阅读更多
此文为转载,权作参考!

package com.jasson.im.api;

import java.io.UnsupportedEncodingException;
import java.sql.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

// Referenced classes of package com.jasson.im.api:
//            MOItem, RPTItem

public class APIClient
{

    public static final int IMAPI_SUCC = 0;
    public static final int IMAPI_CONN_ERR = -1;
    public static final int IMAPI_CONN_CLOSE_ERR = -2;
    public static final int IMAPI_INS_ERR = -3;
    public static final int IMAPI_DEL_ERR = -4;
    public static final int IMAPI_QUERY_ERR = -5;
    public static final int IMAPI_DATA_ERR = -6;
    public static final int IMAPI_API_ERR = -7;
    public static final int IMAPI_DATA_TOOLONG = -8;
    public static final int IMAPI_INIT_ERR = -9;
    public static final int IMAPI_IFSTATUS_INVALID = -10;
    public static final int IMAPI_GATEWAY_CONN_ERR = -11;
    private String dbUser;
    private String dbPwd;
    private String apiCode_;
    private String dbUrl;
    private Connection conn;

    public APIClient()
    {
        dbUser = null;
        dbPwd = null;
        apiCode_ = null;
        dbUrl = null;
        conn = null;
    }

    public int init(String dbIP, String dbUser, String dbPwd, String apiCode)
    {
        release();
        this.dbUser = dbUser;
        this.dbPwd = dbPwd;
        apiCode_ = apiCode;
        dbUrl = "jdbc:mysql://" + dbIP + "/im";
        return testConnect();
    }

    public int init(String dbIP, String dbUser, String dbPwd, String apiCode, String dbName)
    {
        release();
        this.dbUser = dbUser;
        this.dbPwd = dbPwd;
        apiCode_ = apiCode;
        dbUrl = "jdbc:mysql://" + dbIP + "/" + dbName;
        return testConnect();
    }

    public int sendSM(String mobile, String content, long smID)
    {
        return sendSM(new String[] {
            mobile
        }, content, smID, smID);
    }

    public int sendSM(String mobiles[], String content, long smID)
    {
        return sendSM(mobiles, content, smID, smID);
    }

    public int sendSM(String mobiles[], String content, long smID, long srcID)
    {
        return sendSM(mobiles, content, smID, srcID, "");
    }

    public int sendSM(String mobiles[], String content, String sendTime, long smID, long srcID)
    {
        return sendSM(mobiles, content, smID, srcID, "", sendTime);
    }

    public int sendSM(String mobile, String content, long smID, String url)
    {
        return sendSM(new String[] {
            mobile
        }, content, smID, url);
    }

    public int sendSM(String mobiles[], String content, long smID, String url)
    {
        return sendSM(mobiles, content, smID, smID, url);
    }

    public int sendSM(String mobiles[], String content, long smID, long srcID, String url)
    {
        return sendSM(mobiles, content, smID, smID, url, "");
    }

    public int sendSM(String mobiles[], String content, long smID, long srcID, String url, 
            String sendTime)
    {
        if(dbUrl == null)
        {
            return -9;
        }
        if(mobiles == null || mobiles.length == 0)
        {
            return -6;
        }
        StringBuffer mobileBuf = new StringBuffer();
        for(int i = 0; i < mobiles.length; i++)
        {
            mobileBuf.append(",").append(mobiles[i]);
        }

        if(mobileBuf.length() > 1)
        {
            mobileBuf.delete(0, 1);
        } else
        {
            return -6;
        }
        String contenttmp = replaceSpecilAlhpa(content);
        if(contenttmp.length() < 1)
        {
            return -6;
        }
        if(contenttmp.length() > 2000)
        {
            contenttmp = contenttmp.substring(0, 2000);
        }
        if(!checkSmID(smID) || !checkSmID(srcID))
        {
            return -6;
        }
        if(url != null && url.length() > 0)
        {
            if(url.length() > 110)
            {
                return -8;
            }
            if((url + contenttmp).length() > 120)
            {
                return -8;
            }
        }
        int ret = checkGatConn();
        if(ret != 1)
        {
            return ret;
        }
        if(!"".equals(sendTime) && isDateTime(sendTime) == null)
        {
            return -6;
        } else
        {
            return mTInsert(mobileBuf.toString(), contenttmp, smID, srcID, url, sendTime);
        }
    }

    public MOItem[] receiveSM()
    {
        Statement st;
        String getMoSql;
        String delMoSql;
        ArrayList moList;
        StringBuffer snBuf;
        if(dbUrl == null)
        {
            return null;
        }
        if(conn == null)
        {
            int state = initConnect();
            if(state != 0)
            {
                return null;
            }
        }
        st = null;
        ResultSet rs = null;
        getMoSql = "select * from api_mo_" + apiCode_ + " limit 5000";
        delMoSql = "delete from api_mo_" + apiCode_ + " where AUTO_SN in (";
        moList = new ArrayList();
        snBuf = new StringBuffer("-1");
        st = conn.createStatement();
        ResultSet rs;
        MOItem mItemtmp;
        for(rs = st.executeQuery(getMoSql); rs.next(); moList.add(mItemtmp))
        {
            mItemtmp = new MOItem();
            mItemtmp.setSmID(rs.getLong("SM_ID"));
            mItemtmp.setContent(rs.getString("CONTENT"));
            mItemtmp.setMobile(rs.getString("MOBILE"));
            mItemtmp.setMoTime(rs.getString("MO_TIME"));
            snBuf.append(",").append(rs.getString("AUTO_SN"));
        }

        rs.close();
        st.executeUpdate(delMoSql + snBuf.toString() + ")");
        break MISSING_BLOCK_LABEL_291;
        Exception e;
        e;
        MOItem amoitem[];
        releaseConn();
        amoitem = null;
        closeStatment(st);
        return amoitem;
        Exception exception;
        exception;
        closeStatment(st);
        throw exception;
        closeStatment(st);
        MOItem moItem[] = new MOItem[0];
        return (MOItem[])moList.toArray(moItem);
    }

    public MOItem[] receiveSM(long srcID, int amount)
    {
        Statement st;
        String getMoSql;
        String delMoSql;
        ArrayList moList;
        StringBuffer snBuf;
        if(dbUrl == null)
        {
            return null;
        }
        if(conn == null)
        {
            int state = initConnect();
            if(state != 0)
            {
                return null;
            }
        }
        st = null;
        ResultSet rs = null;
        getMoSql = "select * from api_mo_" + apiCode_;
        if(srcID != -1L)
        {
            getMoSql = getMoSql + " where SM_ID=" + srcID;
        }
        if(amount != -1)
        {
            getMoSql = getMoSql + " limit " + amount;
        }
        delMoSql = "delete from api_mo_" + apiCode_ + " where AUTO_SN in (";
        moList = new ArrayList();
        snBuf = new StringBuffer("-1");
        st = conn.createStatement();
        ResultSet rs;
        MOItem mItemtmp;
        for(rs = st.executeQuery(getMoSql); rs.next(); moList.add(mItemtmp))
        {
            mItemtmp = new MOItem();
            mItemtmp.setSmID(rs.getLong("SM_ID"));
            mItemtmp.setContent(rs.getString("CONTENT"));
            mItemtmp.setMobile(rs.getString("MOBILE"));
            mItemtmp.setMoTime(rs.getString("MO_TIME"));
            snBuf.append(",").append(rs.getString("AUTO_SN"));
        }

        rs.close();
        st.executeUpdate(delMoSql + snBuf.toString() + ")");
        break MISSING_BLOCK_LABEL_372;
        Exception e;
        e;
        MOItem amoitem[];
        releaseConn();
        amoitem = null;
        closeStatment(st);
        return amoitem;
        Exception exception;
        exception;
        closeStatment(st);
        throw exception;
        closeStatment(st);
        MOItem moItem[] = new MOItem[0];
        return (MOItem[])moList.toArray(moItem);
    }

    public RPTItem[] receiveRPT()
    {
        Statement st;
        String getRPTSql;
        String delRPTSql;
        ArrayList rptList;
        StringBuffer snBuf;
        if(dbUrl == null)
        {
            return null;
        }
        ResultSet rs = null;
        st = null;
        if(conn == null)
        {
            int state = initConnect();
            if(state != 0)
            {
                return null;
            }
        }
        getRPTSql = "select * from api_rpt_" + apiCode_ + " limit 5000";
        delRPTSql = "delete from api_rpt_" + apiCode_ + " where AUTO_SN in (";
        RPTItem rptItem[] = (RPTItem[])null;
        rptList = new ArrayList();
        snBuf = new StringBuffer("-1");
        st = conn.createStatement();
        ResultSet rs;
        RPTItem rptItemtmp;
        for(rs = st.executeQuery(getRPTSql); rs.next(); rptList.add(rptItemtmp))
        {
            rptItemtmp = new RPTItem();
            rptItemtmp.setSmID(rs.getLong("SM_ID"));
            rptItemtmp.setCode(rs.getInt("RPT_CODE"));
            rptItemtmp.setMobile(rs.getString("MOBILE"));
            rptItemtmp.setDesc(rs.getString("RPT_DESC"));
            rptItemtmp.setRptTime(rs.getString("RPT_TIME"));
            snBuf.append(",").append(rs.getString("AUTO_SN"));
        }

        rs.close();
        st.executeUpdate(delRPTSql + snBuf.toString() + ")");
        break MISSING_BLOCK_LABEL_364;
        SQLException e;
        e;
        RPTItem arptitem[];
        releaseConn();
        if(e.getErrorCode() != 1146 && e.getErrorCode() != 1142)
        {
            break MISSING_BLOCK_LABEL_330;
        }
        arptitem = new RPTItem[0];
        closeStatment(st);
        return arptitem;
        arptitem = null;
        closeStatment(st);
        return arptitem;
        Exception ex;
        ex;
        arptitem = null;
        closeStatment(st);
        return arptitem;
        Exception exception;
        exception;
        closeStatment(st);
        throw exception;
        closeStatment(st);
        RPTItem rptItem[] = new RPTItem[0];
        return (RPTItem[])rptList.toArray(rptItem);
    }

    public RPTItem[] receiveRPT(long smID, int amount)
    {
        Statement st;
        String getRPTSql;
        String delRPTSql;
        ArrayList rptList;
        StringBuffer snBuf;
        if(dbUrl == null)
        {
            return null;
        }
        ResultSet rs = null;
        st = null;
        if(conn == null)
        {
            int state = initConnect();
            if(state != 0)
            {
                return null;
            }
        }
        getRPTSql = "select * from api_rpt_" + apiCode_;
        if(smID != -1L)
        {
            getRPTSql = getRPTSql + " where SM_ID=" + smID;
        }
        if(amount != -1)
        {
            getRPTSql = getRPTSql + " limit " + amount;
        }
        delRPTSql = "delete from api_rpt_" + apiCode_ + " where AUTO_SN in (";
        RPTItem rptItem[] = (RPTItem[])null;
        rptList = new ArrayList();
        snBuf = new StringBuffer("-1");
        st = conn.createStatement();
        ResultSet rs;
        RPTItem rptItemtmp;
        for(rs = st.executeQuery(getRPTSql); rs.next(); rptList.add(rptItemtmp))
        {
            rptItemtmp = new RPTItem();
            rptItemtmp.setSmID(rs.getLong("SM_ID"));
            rptItemtmp.setCode(rs.getInt("RPT_CODE"));
            rptItemtmp.setMobile(rs.getString("MOBILE"));
            rptItemtmp.setDesc(rs.getString("RPT_DESC"));
            rptItemtmp.setRptTime(rs.getString("RPT_TIME"));
            snBuf.append(",").append(rs.getString("AUTO_SN"));
        }

        rs.close();
        st.executeUpdate(delRPTSql + snBuf.toString() + ")");
        break MISSING_BLOCK_LABEL_448;
        SQLException e;
        e;
        RPTItem arptitem[];
        releaseConn();
        if(e.getErrorCode() != 1146 && e.getErrorCode() != 1142)
        {
            break MISSING_BLOCK_LABEL_411;
        }
        arptitem = new RPTItem[0];
        closeStatment(st);
        return arptitem;
        arptitem = null;
        closeStatment(st);
        return arptitem;
        Exception ex;
        ex;
        arptitem = null;
        closeStatment(st);
        return arptitem;
        Exception exception;
        exception;
        closeStatment(st);
        throw exception;
        closeStatment(st);
        RPTItem rptItem[] = new RPTItem[0];
        return (RPTItem[])rptList.toArray(rptItem);
    }

    public void release()
    {
        dbUser = null;
        dbPwd = null;
        apiCode_ = null;
        dbUrl = null;
        releaseConn();
    }

    private int testConnect()
    {
        Statement st;
        st = null;
        ResultSet rs = null;
        try
        {
            if(conn != null)
            {
                releaseConn();
            }
            getConn();
            st = conn.createStatement();
        }
        catch(ClassNotFoundException e)
        {
            e.printStackTrace();
            return -1;
        }
        catch(SQLException e)
        {
            e.printStackTrace();
            return -1;
        }
        try
        {
            String tableName = "api_mo_" + apiCode_;
            ResultSet rs = st.executeQuery("select * from " + tableName + " limit 1");
            rs.close();
        }
        catch(SQLException e)
        {
            try
            {
                st.close();
            }
            catch(Exception exception1) { }
            return -7;
        }
        break MISSING_BLOCK_LABEL_137;
        Exception exception;
        exception;
        try
        {
            st.close();
        }
        catch(Exception exception2) { }
        throw exception;
        try
        {
            st.close();
        }
        catch(Exception exception3) { }
        return 0;
    }

    private int initConnect()
    {
        try
        {
            getConn();
        }
        catch(Exception e)
        {
            e.printStackTrace();
            return -1;
        }
        return 0;
    }

    private void getConn()
        throws ClassNotFoundException, SQLException
    {
        Class.forName("org.gjt.mm.mysql.Driver");
        conn = DriverManager.getConnection(dbUrl, dbUser, dbPwd);
    }

    private void releaseConn()
    {
        if(conn != null)
        {
            try
            {
                conn.close();
            }
            catch(SQLException sqlexception) { }
        }
        conn = null;
    }

    private int mTInsert(String mobile, String content, long smID, long srcID, String url, 
            String sendTime)
    {
        String sendMTSql;
        Statement st;
        if(conn == null)
        {
            int state = initConnect();
            if(state != 0)
            {
                return -1;
            }
        }
        if(sendTime == null || "".equals(sendTime))
        {
            sendTime = getCurDateTime();
        }
        sendMTSql = "";
        if(url == null || url.trim().length() == 0)
        {
            sendMTSql = "insert into api_mt_" + apiCode_ + " (SM_ID,SRC_ID,MOBILES,CONTENT,SEND_TIME) values (" + smID + "," + srcID + ",'" + mobile + "','" + content + "','" + sendTime + "')";
        } else
        {
            sendMTSql = "insert into api_mt_" + apiCode_ + " (SM_ID,SRC_ID,MOBILES,CONTENT,IS_WAP,URL,SEND_TIME) values (" + smID + "," + srcID + ",'" + mobile + "','" + content + "'," + 1 + ",'" + url + "','" + sendTime + "')";
        }
        st = null;
        try
        {
            st = conn.createStatement();
            st.executeUpdate(gb2Iso(sendMTSql));
            break MISSING_BLOCK_LABEL_299;
        }
        catch(SQLException e)
        {
            releaseConn();
        }
        closeStatment(st);
        return -3;
        Exception exception;
        exception;
        closeStatment(st);
        throw exception;
        closeStatment(st);
        return 0;
    }

    private void closeStatment(Statement st)
    {
        try
        {
            st.close();
        }
        catch(Exception exception) { }
    }

    private String replaceSpecilAlhpa(String content)
    {
        if(content == null || content.trim().length() == 0)
        {
            return "";
        }
        String spec_char = "\\'";
        String retStr = "";
        for(int i = 0; i < content.length(); i++)
        {
            if(spec_char.indexOf(content.charAt(i)) >= 0)
            {
                retStr = retStr + "\\";
            }
            retStr = retStr + content.charAt(i);
        }

        return retStr;
    }

    private boolean checkSmID(long smID)
    {
        return smID >= 0L && smID <= 0x5f5e0ffL;
    }

    private String gb2Iso(String str)
    {
        if(str == null)
        {
            return "";
        }
        String temp = "";
        try
        {
            byte buf[] = str.trim().getBytes("GBK");
            temp = new String(buf, "iso8859-1");
        }
        catch(UnsupportedEncodingException e)
        {
            temp = str;
        }
        return temp;
    }

    private int checkGatConn()
    {
        int ret;
        Statement st;
        String sql;
        ret = 1;
        ResultSet rs = null;
        st = null;
        if(conn == null)
        {
            initConnect();
        }
        sql = "select if_status,conn_succ_status from tbl_api_info as api where api.if_code='" + apiCode_ + "' limit 1";
        try
        {
            st = conn.createStatement();
            ResultSet rs;
            for(rs = st.executeQuery(sql); rs.next();)
            {
                if("2".equals(rs.getString("if_status")))
                {
                    ret = -10;
                }
                if("0".equals(rs.getString("conn_succ_status")))
                {
                    ret = -11;
                }
            }

            rs.close();
        }
        catch(SQLException e)
        {
            try
            {
                st.close();
            }
            catch(Exception exception1) { }
            return -7;
        }
        break MISSING_BLOCK_LABEL_160;
        Exception exception;
        exception;
        try
        {
            st.close();
        }
        catch(Exception exception2) { }
        throw exception;
        try
        {
            st.close();
        }
        catch(Exception exception3) { }
        return ret;
    }

    public static String getCurDateTime()
    {
        SimpleDateFormat nowDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return nowDate.format(new Date());
    }

    public static String isDateTime(String str)
    {
        if(str == null)
        {
            return null;
        }
        if(str.length() != 19)
        {
            return null;
        }
        if(str.split(" ")[0].length() != 10)
        {
            return null;
        }
        if(str.split(" ")[1].length() != 8)
        {
            return null;
        }
        int temp = Integer.parseInt(str.substring(5, 7));
        if(12 < temp || temp < 1)
        {
            return null;
        }
        temp = Integer.parseInt(str.substring(8, 10));
        if(31 < temp || temp < 1)
        {
            return null;
        }
        temp = Integer.parseInt(str.substring(11, 13));
        if(23 < temp || temp < 0)
        {
            return null;
        }
        temp = Integer.parseInt(str.substring(14, 16));
        if(59 < temp || temp < 0)
        {
            return null;
        }
        temp = Integer.parseInt(str.substring(17, 19));
        if(59 < temp || temp < 0)
        {
            return null;
        }
        Date returnDate = null;
        DateFormat df = DateFormat.getDateInstance();
        try
        {
            returnDate = df.parse(str);
            return returnDate.toString();
        }
        catch(Exception e)
        {
            return null;
        }
    }
}
分享到:
评论

相关推荐

    移动代理服务器MAS短信API2.2开发手册及DEMO

    移动代理服务器MAS短信API2.2是用于集成短信发送功能到各类应用程序中的一个重要工具,它提供了多种编程语言的接口,包括ASP、VB、JAVA、C/C++、PB(PowerBuilder)、DELPHI以及.NET。本开发手册和DEMO旨在帮助...

    移动代理服务器MAS短信API2.2

    总的来说,移动代理服务器MAS短信API2.2是一个全面的短信解决方案,涵盖了多种开发语言,提供了详尽的文档和示例,以确保开发者能够顺利地将其整合到自己的系统中,实现高效、可靠的短信服务功能。无论是小型项目...

    嘉讯MAS .NET API

    通过.NET API,开发者可以访问操作系统级别的功能,如文件系统操作、网络通信、数据库连接等,而无需直接操作底层代码。 ImApiDotNet.dll是嘉讯MAS .NET API的核心组件,它封装了与MAS服务器通信的接口和方法。这个...

    移动代理服务器MAS短信API2.2开发手册(.Net)

    - **.Net API**:包括APIClient类、MOItem类、RPTItem类,提供高级封装,简化短信功能调用。 #### 4. 使用指导 - **开发步骤**:规划、编码、测试,确保项目顺利推进。 - **调试方法**:利用提供的调试环境,快速...

    嘉讯MAS JAVA API

    嘉讯MAS JAVA API是专为Java开发者设计的一套接口库,用于在应用程序中集成嘉讯公司的消息服务系统。这个API的版本号为JAVA API 2.1,日期为20080317,表明它是2008年3月17日发布的更新版本,可能包含了对之前版本的...

    移动mas机API接口说明(java)

    移动MAS机,全称为Mobile Application Server,是一种专为企业级应用设计的移动通信设备或系统。在Java环境下,移动MAS机提供了API接口,使得开发者能够利用这些接口开发短信收发、数据传输、设备管理等应用程序。...

    移动代理服务器MAS短信API2.2常见问题手册.pdf

    ### 移动代理服务器MAS短信API2.2常见问题解析 #### 1. API支持的版本 - **API 2.0** 支持的版本包括:IM V4.0、IM V4.1、MAS V1.0、MAS V1.0.2 和 MAS V1.1。 - **API 2.1** 支持的版本有:IM V4.0 (需升级至 IM 4...

    华为MAS服务器API接口

    华为MAS服务器API接口是针对中国移动代理服务器进行二次开发的重要工具,它允许开发者通过编程的方式与华为MAS服务器进行交互,实现各种定制化的业务功能。华为MAS(Mobile Agent Server)旨在为企业提供移动信息化...

    通达OA办公系统与移动MAS数据库连接程序源码

    而移动MAS(Mobile Middleware Agent System)数据库连接程序是针对通达OA系统的一个扩展,旨在实现办公系统与移动设备之间的数据同步和交互,以满足现代企业对移动办公的需求。 在移动MAS数据库连接程序中,主要...

    infoX-MAS_API接口说明(DB)

    IT知识点:infoX-MAS_API接口说明(DB) 标题解释 infoX-MAS_API接口说明(DB)是指infoX-EIEAPI接口说明(DB)的简称,意为infoX-MAS_API数据库方式通信适配插件的接口说明。该接口说明旨在为MAS服务器内部各模块和...

    PyPI 官网下载 | butter.mas_api-0.3.4-py3-none-any.whl

    “butter.mas_api”这个名字暗示这是一个与某个名为“butter”的系统或服务相关的API接口库。版本号“0.3.4”表示这是该库的第四个次要版本更新,可能包含了错误修复、新功能或者性能优化。而“py3-none-any”部分则...

    移动短信开发:移动嘉讯MAS服务器开发(C++)API2.2及开发手册

    C++作为一种强类型、面向对象的编程语言,特别适合于系统级和性能敏感的开发任务,因此被选为MAS服务器API的实现语言。 在使用这个API时,开发者需要了解以下几个关键知识点: 1. **连接管理**:API通常提供了连接...

    Python库 | butter.mas_api-0.11.6-py3-none-any.whl

    `butter.mas_api` 库可能是为了与某个名为“MAS”(可能是“管理应用程序服务”或类似含义)的系统进行交互而设计的。API(Application Programming Interface)是一组规则、协议和工具,用于构建软件应用程序。通过...

    嘉讯mas2.0_JAVA_API

    ###### 4.4.1 APIClient工厂类-ApiClientFactory类 **ApiClientFactory**类是创建不同类型的API客户端的基础工厂类。其中,`createSmsApiClient`方法用于创建短信API客户端。 #### 五、短信接口 ##### 5.1 短信API...

    MAS系统的开发和应用

    MAS系统的开发和应用,很金典的 啊希望大家多看看

Global site tag (gtag.js) - Google Analytics