📄 shortmsgcmpp.java
字号:
} catch(Exception e) {
parseResult =CmppConstants.error_msg_structure;
System.out.println(" Cmpp sp loginRep erro"+e);
return ;
}
}
//-----------------------------------------------
/**
* 解析MPCU Fwd MT消息体内容
*/
/**
* 解析MPCA提交(Fwd MT)消息体内容
*
* <p><b>处理流程.</b><p>
* 1 依照协议从字节输入管道中读出相应字段<br>
* 2 判断字段内容的合法性,并将验证结果写入parseResult值 <br>
* @param dataInStream 字节输入流
* @throws IOException 输入输出异常
* @throws NullPointerException 空指针异常
* @throws Exception 其他异常<br>
*/
private void parseBodyDeliver(DataInputStream dataInStream) {
parseResult =CmppConstants.success;
try {
msgSrcAddr=readString(dataInStream,21);
msgDestAddr=readString(dataInStream,21);
msgPkTotal=dataInStream.readByte ();
msgPkNumber=dataInStream.readByte ();
msgDataCoding=dataInStream.readByte ();
msgLength = dataInStream.readInt ();
int msgLen;
if(msgLength < 0) {
msgLen = 256 + msgLength ;
} else {
msgLen = msgLength ;
}
if(msgLen>400) {
parseResult=CmppConstants.error_too_long;
return ;
}
byte[] b = new byte[msgLen];
dataInStream.readFully(b);
if(msgDataCoding== 0)
msgContent = new String(b);
else if(msgDataCoding == 2)//binrary
msgContent = new String(b);
else if(msgDataCoding == 4)//binrary
msgContent = new String(b);
else if(msgDataCoding == 8)//unicode
msgContent = new String(b,"UTF-16BE");
//msgContent = new String(b,"GB2312");
else if(msgDataCoding== 15) //GB2312
msgContent = new String(b,"GB2312");
else if(msgDataCoding == 10)
msgContent = new String(b,"GB2312");
else
msgContent = new String(b);
//msgReserve=readString(dataInStream,8);
msgLinkId = readString(dataInStream, 8);
} catch(Exception e) {
parseResult=CmppConstants.error_msg_structure;
System.out.println("parse MT Msg erro :"+e);
}
return;
}
private void parseBodyReport(DataInputStream dataInStream) {
parseResult =CmppConstants.success;
try {
srcSectionId = dataInStream.readInt();
reportSubmitTime = Integer.toString(dataInStream.readInt());
reportMsgId = (long)dataInStream.readInt(); //消息流水号(可以用来完成消息的确认)
byte reportType = dataInStream.readByte();
reportDestAddr=readString(dataInStream,21);
byte reportStatus = dataInStream.readByte();
byte reportErrorCode = dataInStream.readByte();
//msgReserve=readString(dataInStream,8);
msgLinkId = readString(dataInStream, 8);
reportStat =Byte.toString(reportType) + Byte.toString(reportStatus) + Byte.toString(reportErrorCode);
} catch(Exception e) {
parseResult=CmppConstants.error_msg_structure;
System.out.println("parse Report erro :"+e);
}
return;
}
/**
* 解析MPCA PUSH MO ROUTE消息体内容
*
* <p><b>处理流程.</b><p>
* 依照协议解析各字段 并对部分字段进行验证
* @param DataInputStream dataInStream 输入字节流
* @return none
* @throws IOException 输入输出异常
* @throws NullPointerException 空指针异常
* @throws Exception 其他异常<br>
*/
/**
* 公用方法
* 私有方法convertBytesToString
*/
private static String convertBytesToString(byte bstr[]) {
String desBuff="";
String oa;
try {
for (int i=0;i<bstr.length;i++) {
oa=Integer.toHexString((int) bstr[i]);
if(oa.length()<=1)
oa="0"+oa;
if(bstr[i]<0)
oa=oa.substring(oa.length()-2);
desBuff+=oa;
}
return desBuff;
} catch(Exception e) {
return null;
}
}
/**
* 公用方法
* 私有方法convertStringToBytes
*/
private static byte [] convertStringToBytes(String srcBuff) {
String subBuff;
byte ab;
try {
byte des[]=new byte[srcBuff.length()/2];
int j=0;
for(int i=0; i<srcBuff.length(); i+=2,j++) {
subBuff=srcBuff.substring(i,i+2);
ab=(byte)Integer.parseInt(subBuff,16);
des[j]=ab;
}
return des;
} catch(Exception e) {
return null;
}
}
/**
* 公用方法
* 从流中读取一个字符串,返回的String 不包含'\0'
*/
private static String readString(DataInputStream inStream) {
byte[] readByte = new byte[180];
int i = 0;
try {
while(true) {
readByte[i] = inStream.readByte() ;
if (readByte[i] == '\0')
break;
i++;
if(i >= 180)
return "";
}
} catch(IOException e) {
return CmppConstants.READSTRINGERROR;
}
String result = new String(readByte,0,i);
return result;
}
/**
* 公用方法
* 用MD5算法加密字节数组
*/
private byte[] computeDigest(byte[] b) {
try {
MessageDigest alg = MessageDigest.getInstance("MD5");
alg.reset();
alg.update(b);
byte[] hash = alg.digest(); //得到摘要
return hash ;
} catch (Exception e) {
System.out.println("MessageDigest :"+e);
return null;
}
}
/**
* 公用方法
* 比较字节数组是否完全相同
*/
private static boolean CompBytes( byte[] b1, byte[] b2 ) {
if (b1.length != b2.length )
return false;
for ( int i=0 ; i<b1.length ; i++ ) {
if(b1[i] != b2[i])
return false;
}
return true;
}
/**
* 从流中读取一个字符串,返回的String 不包含'\0'
*/
private static String readString(DataInputStream inStream,int StrLen){
byte[] readByte = new byte[1000];
int i = 0;
try{
while(true){
readByte[i] = inStream.readByte() ;
i++;
if (i>=1000)
return "";
if(i > StrLen-1)
break;
}
} catch(IOException e){
return "erro";
}
String result = new String(readByte,0,i);
return result.trim ();
}
/**
* 公用方法
* 将整型变量转换为字节数组
* @param i
* @return byte[]
*/
private byte[] intToBytes(int i) {
try {
ByteArrayOutputStream byteArrayos = new ByteArrayOutputStream();
DataOutputStream dataOutStream = new DataOutputStream(byteArrayos);
dataOutStream.writeInt(i);
return byteArrayos.toByteArray() ;
} catch (Exception e) {}
return null;
}
/**
* Description:以下是定义公有和私有变量
*/
public long submit_msg_id ;
public int totalSize; //消息总长度
//消息头
public int headCmdID; //消息类型
public int headCmdStatus; //命令状态
public int headSeqcNo; //消息流水号(可以用来完成消息的确认)
//消息体
//login
public String conSourceAddr=""; //SP ID or MPCU ID
public String conSourcePasswd=""; //密码
public byte[] conAuth; //SP/MPCU认证码 使用MD5算法加密
public byte conBindType; //用户登陆类型
public byte conVersion ; //接口版本号
public int conTimestamp; //login时间戳
public byte[] conTimestampBytes; //login时间戳,以byte数组表示
//login respont
public byte lastVersion ; //MPCA/MPCU 支持的最高版本号
public byte[] repAuth ; //MPCA/MPCU 回应的认证码
/*短消息*/
public String spId; //SP标志
public String spUnionCode; //系统在联通的sp编号
public int srcSectionId; //发送消息的网关Id号
public int recvTimeStamp; //消息接受时间戳
public String msgId; //信息标志码
public int fwdFlag; //前转外发标志
public int AccessorId; //短信中心ID
public int smscSeqNo; //本条消息自Smsc发来的流水号
public String moMsgId; //MO信息标志码
public String msgSpId; //信息内容来源(SP_ID)
public String msgSrcAddr; //源终端MSISDN号码(没有可以为空)
public String msgDestAddr;
public String[] msgSubmitDest; //接收业务的MSISDN号码
public String msgServiceType; //业务类型
public byte msgFeeType; //资费类别
public int msgInfoFee; //资费代码(以分为单位)
public byte msgProtocolId; //协议标识
public byte msgMode; //消息模式,是否要状态报告
//是否是控制信息,是否SIM卡操作
public byte msgPriority; //信息级别
public byte msgFeeUserType; //计费用户类型
public byte msgDataCoding; //数据编码
public String msgDeliverTime; //交易请求到达时间
public String msgDigest; //数字摘要
public int msgLength; //消息长度(<320个字节)
public String msgContent; //消息内容
public String msgValidityPeriod; //存活有效期,格式遵循SMPP3.4协议
public String msgSchedule; //定时发送时间,格式遵循SMPP3...4协议
public String msgReserve; //保留字段
public byte TlsFlag; //是否使用TLS协议层
//ACK
public int ackMsgID; //HexString 短消息ID,当该消息是ACK时有效
public byte respResult; //结果
public int parseResult; //返回给sp的结果 add by me
public byte successId; //成功标识(0:成功; 1:失败)
//附加备用
public String spCode; //SP的服务代码
//public int sequnceNo; //smsc发送状态报告的消息序列号
//public int reportMsgId; //Cmpp_Deliver 返回状态应答信息的msg_id;
public byte[] spPasswd; //Sp的公共密钥
public String spUnionUserName; //sp在网关中的用户名
public String spUnionPasswd; //sp在网关中的用密码
public String ismgUnionUserName; //网关在sp中的用户名
public String ismgUnionPasswd; //网关在sp中的用密码
public int msgGivenValue=0; //Sp给用户奖钱或退钱
public byte msgAgentFlag =0;
public byte msgMorelatetoMTFlag=0; //引起MT的原因
public int packetLength; //包长度
public int spIndex; //Sp的索引序列
public int pushType; //路由更新类型
//public byte loginRespResult; //登陆返回值
public String connect_authenticatoricp; //test
public byte activeResult;
public byte submit_destusr_tl; //群发数量
public String feeTerminalId;
public byte msgPkTotal;
public byte msgPkNumber;
public byte sendCount=0; //为重发保留的发送次数
// public byte[] resendPackage; //为重发保留的报文
public long firstSendtime=0; //为重发保留的首次发送时间
public long lastSendtime=0; //为重发保留的末次发送时间
public byte tpPId=0; //GSM 3.4
public byte tpUdhi=0; //GSM 3.4
//
//就我的使用经历来说,TP_pId=0.
//发送普通的短信,TP_udhi=0, msg_fmt=8(UCS2码)或者15(中文GB码)
//如果偷懒干脆就写15,让网关自己帮你转换
//发送二进制文件流或者其他东西,TP_udhi=0x40 or 1, msg_fmt=4
//发送普通的短信,TP_udhi=0, msg_fmt=8
//发送二进制文件流或者其他东西,TP_udhi=1, msg_fmt=4 or 245
//发送免提短信的时候,TP_pId=1, msg_fmt=24, TP_udhi=0
public int operateSeqId=0; //业务处理序列
//以下字段对应状态报告字段。以report打头
public long reportMsgId;
public String reportStat;
public String reportSubmitTime;
public String reportDoneTime;
public String reportDestAddr;
public int reportSmscSequence;
public String msgLinkId;
//////end of report //////////
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -