📄 mm7sender.java
字号:
/**
* File Name:MM7Sender.java Company: 中国移动集团公司 Date : 2004-1-17
*/
/**
* modified by liuhuafeng on 2006/7/26
* 1:appversion,原来放在sender方法里的,现在作为一个field,减少对象的create
*
*
*/
package com.cmcc.mm7.vasp.service;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.Socket;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import org.apache.log4j.Logger;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import sun.misc.BASE64Encoder;
import com.cmcc.mm7.vasp.common.ConnectionPool;
import com.cmcc.mm7.vasp.common.ConnectionPoolManager;
import com.cmcc.mm7.vasp.common.ConnectionWrap;
import com.cmcc.mm7.vasp.common.MMConstants;
import com.cmcc.mm7.vasp.common.RetriveApiVersion;
import com.cmcc.mm7.vasp.common.SOAPEncoder;
import com.cmcc.mm7.vasp.conf.MM7Config;
import com.cmcc.mm7.vasp.message.MM7CancelReq;
import com.cmcc.mm7.vasp.message.MM7CancelRes;
import com.cmcc.mm7.vasp.message.MM7RSErrorRes;
import com.cmcc.mm7.vasp.message.MM7RSRes;
import com.cmcc.mm7.vasp.message.MM7ReplaceReq;
import com.cmcc.mm7.vasp.message.MM7ReplaceRes;
import com.cmcc.mm7.vasp.message.MM7SubmitReq;
import com.cmcc.mm7.vasp.message.MM7SubmitRes;
import com.cmcc.mm7.vasp.message.MM7VASPReq;
public class MM7Sender {
private static final Logger log = Logger.getLogger(MM7Sender.class);
private MM7Config mm7Config;
private BufferedOutputStream sender;
private BufferedInputStream receiver;
private StringBuffer sb;
private StringBuffer beforAuth;
private String AuthInfor;
// private String DigestInfor;
private StringBuffer afterAuth;
private StringBuffer entityBody;
private MM7RSRes res;
private ByteArrayOutputStream baos;
private ConnectionPool pool;
private ConnectionWrap connWrap;
private int ResendCount;
private Socket client;
private int ReadTimeOutCount = 0;
private ByteArrayOutputStream sendBaos;
private StringBuffer SevereBuffer;
private StringBuffer InfoBuffer;
private StringBuffer FinerBuffer;
private ByteArrayOutputStream Severebaos;
private ByteArrayOutputStream Infobaos;
private ByteArrayOutputStream Finerbaos;
// private long LogTimeBZ;
// private long SameMinuteTime;
private int N;
private SimpleDateFormat sdf;
private DecimalFormat df;
private String logFileName;
private byte[] TimeOutbCount;
private ConnectionWrap TimeOutWrap;
private boolean TimeOutFlag;
public int tempnum = 0;
RetriveApiVersion apiver;
public MM7Sender() // 构造方法
{
reset();
}
private void reset() {
// File f;
mm7Config = new MM7Config();
sender = null;
receiver = null;
AuthInfor = "";
// DigestInfor = "";
sb = new StringBuffer();
beforAuth = new StringBuffer();
afterAuth = new StringBuffer();
entityBody = new StringBuffer();
res = new MM7RSRes();
baos = new ByteArrayOutputStream();
ResendCount = 0;
connWrap = null;
// Modify by hudm 2004-06-09
// pool = ConnectionPool.getInstance();
pool = new ConnectionPool();
// /end Modify by hudm 2004-06-09
client = null;
sendBaos = new ByteArrayOutputStream();
SevereBuffer = new StringBuffer();
InfoBuffer = new StringBuffer();
FinerBuffer = new StringBuffer();
Severebaos = new ByteArrayOutputStream();
Infobaos = new ByteArrayOutputStream();
Finerbaos = new ByteArrayOutputStream();
// LogTimeBZ = System.currentTimeMillis();
// SameMinuteTime = System.currentTimeMillis();
N = 1;
sdf = new SimpleDateFormat("yyyyMMddHHmm");
df = new DecimalFormat();
df.applyLocalizedPattern("0000");
logFileName = "";
TimeOutbCount = null;
TimeOutWrap = null;
TimeOutFlag = false;
apiver = new RetriveApiVersion();
ConnectionPoolManager m=new ConnectionPoolManager(pool);
m.start();
}
/** 构造方法 */
public MM7Sender(MM7Config config) throws Exception {
log.info("MM7Sender对象初始化......");
reset();
mm7Config = config;
pool.setConfig(mm7Config);
}
public void setConfig(MM7Config config) // 设置MM7Config
{
mm7Config = config;
pool.setConfig(mm7Config);
}
public MM7Config getConfig() // 获得MM7Config
{
return (mm7Config);
}
// private void setSameMinuteTime(long time) {
// SameMinuteTime = time;
// }
//
// private long getSameMinuteTime() {
// return SameMinuteTime;
// }
public MM7RSRes send(MM7VASPReq mm7VASPReq) // 发送消息
{
tempnum++;
sb = new StringBuffer();
beforAuth = new StringBuffer();
afterAuth = new StringBuffer();
entityBody = new StringBuffer();
sendBaos = new ByteArrayOutputStream();
SevereBuffer = new StringBuffer();
InfoBuffer = new StringBuffer();
FinerBuffer = new StringBuffer();
this.Severebaos = new ByteArrayOutputStream();
this.Finerbaos = new ByteArrayOutputStream();
this.Infobaos = new ByteArrayOutputStream();
sender = null;
receiver = null;
// reset();
SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
if (mm7VASPReq == null) {
log.warn("待发送的消息为空!!!");
MM7RSErrorRes ErrorRes = new MM7RSErrorRes();
ErrorRes.setStatusCode(-105);
ErrorRes.setStatusText("待发送的消息为空!");
SevereBuffer.append("[Message_Type=MM7RSErrorRes]");
SevereBuffer.append("[Comments={" + ErrorRes.getStatusCode());
SevereBuffer.append(";" + ErrorRes.getStatusText() + "}]");
return ErrorRes;
}
try {
String mmscURL = mm7Config.getMMSCURL();
// int httpindex = -1;
// int index = -1;
if (mmscURL == null)
mmscURL = "";
beforAuth.append("POST " + mmscURL + " HTTP/1.1\r\n");
/** 为了当有多个MMSCIP时,可以进行平均分配 */
if (log.isDebugEnabled())
log.debug("pool.getIPCount=" + pool.getIPCount() + ",mm7Config.getMMSCIP()=" + mm7Config.getMMSCIP());
if (pool.getIPCount() < mm7Config.getMMSCIP().size()) {
beforAuth.append("Host:" + (String) mm7Config.getMMSCIP().get(pool.getIPCount()) + "\r\n");
pool.setIPCount(pool.getIPCount() + 1);
}
else {
pool.setIPCount(0);
beforAuth.append("Host:" + (String) mm7Config.getMMSCIP().get(0) + "\r\n");
pool.setIPCount(pool.getIPCount() + 1);
}
// 设置Host结束
/** 设置ContentType,不带附件为text/xml;带附件为multipart/related */
if (mm7VASPReq instanceof MM7SubmitReq) {
MM7SubmitReq submitReq = (MM7SubmitReq) mm7VASPReq;
InfoBuffer.append("[TransactionID=" + submitReq.getTransactionID() + "]");
InfoBuffer.append("[Message_Type=MM7SubmitReq]");
InfoBuffer.append("[Sender_Address=" + submitReq.getSenderAddress() + "]");
InfoBuffer.append("[Recipient_Address={");
if (submitReq.isToExist()) {
InfoBuffer.append("To={");
List to = new ArrayList();
to = submitReq.getTo();
for (int i = 0; i < to.size(); i++) {
InfoBuffer.append((String) to.get(i) + ",");
}
InfoBuffer.append("}");
}
if (submitReq.isCcExist()) {
InfoBuffer.append("Cc={");
List cc = new ArrayList();
cc = submitReq.getCc();
for (int i = 0; i < cc.size(); i++) {
InfoBuffer.append((String) cc.get(i) + ",");
}
InfoBuffer.append("}");
}
if (submitReq.isBccExist()) {
InfoBuffer.append("Bcc={");
List bcc = new ArrayList();
bcc = submitReq.getBcc();
for (int i = 0; i < bcc.size(); i++) {
InfoBuffer.append((String) bcc.get(i) + ",");
}
InfoBuffer.append("}");
}
InfoBuffer.append("}]\r\n");
if (submitReq.isContentExist())
beforAuth.append("Content-Type:multipart/related; boundary=\"--NextPart_0_2817_24856\";"
+ "type=\"text/xml\";start=\"</tnn-200102/mm7-vasp>\"" + "\r\n");
else
beforAuth.append("Content-Type:text/xml;charset=\"" + mm7Config.getCharSet() + "\"" + "\r\n");
}
else if (mm7VASPReq instanceof MM7ReplaceReq) {
MM7ReplaceReq replaceReq = (MM7ReplaceReq) mm7VASPReq;
InfoBuffer.append("[TransactionID=" + replaceReq.getTransactionID() + "]");
InfoBuffer.append("[Message_Type=MM7ReplaceReq]\r\n");
if (replaceReq.isContentExist())
beforAuth.append("Content-Type:multipart/related; boundary=\"--NextPart_0_2817_24856\";" + "\r\n");
else
beforAuth.append("Content-Type:text/xml;charset=\"" + mm7Config.getCharSet() + "\"" + "\r\n");
}
else if (mm7VASPReq instanceof MM7CancelReq) {
MM7CancelReq cancelReq = (MM7CancelReq) mm7VASPReq;
InfoBuffer.append("[TransactionID=" + cancelReq.getTransactionID() + "]");
InfoBuffer.append("[Message_Type=MM7CancelReq]\r\n");
beforAuth.append("Content-Type:text/xml;charset=\"" + mm7Config.getCharSet() + "\"" + "\r\n");
}
else {
MM7RSErrorRes ErrorRes = new MM7RSErrorRes();
ErrorRes.setStatusCode(-106);
ErrorRes.setStatusText("没有匹配的消息,请确认要发送的消息是否正确!");
SevereBuffer.append("[Message_Type=MM7RSErrorRes]");
SevereBuffer.append("[Comments={" + ErrorRes.getStatusCode());
SevereBuffer.append(";" + ErrorRes.getStatusText() + "}]");
return ErrorRes;
}
// 设置ContentType结束
// 设置Content-Trans-Encoding
beforAuth.append("Content-Transfer-Encoding:8bit" + "\r\n");
AuthInfor = "Authorization:Basic " + getBASE64(mm7Config.getUserName() + ":" + mm7Config.getPassword())
+ "\r\n";
afterAuth.append("SOAPAction:\"\"" + "\r\n");
// RetriveApiVersion apiver=new RetriveApiVersion();
afterAuth.append("MM7APIVersion:" + apiver.getApiVersion() + "\r\n");
/**
* 判断是否是长连接,若是长连接,则将Connection设为keep-alive,
* 否则设为close,以告诉服务器端客户端是长连接还是短连接
*/
if (pool.getKeepAlive().equals("on")) {
afterAuth.append("Connection: Keep-Alive" + "\r\n");
}
else {
afterAuth.append("Connection:Close" + "\r\n");
}
byte[] bcontent = getContent(mm7VASPReq);
if (log.isDebugEnabled())
log.debug("彩信经编码后的大小:" + bcontent.length);
if (bcontent.length > mm7Config.getMaxMsgSize()) {
MM7RSErrorRes ErrorRes = new MM7RSErrorRes();
ErrorRes.setStatusCode(-113);
ErrorRes.setStatusText("消息内容的尺寸超出允许发送的大小!");
SevereBuffer.append("[Message_Type=MM7RSErrorRes]");
SevereBuffer.append("[Comments={" + ErrorRes.getStatusCode());
SevereBuffer.append(";" + ErrorRes.getStatusText() + "}]");
return ErrorRes;
}
this.TimeOutbCount = bcontent;
String env = "";
try {
ByteArrayOutputStream tempbaos = new ByteArrayOutputStream();
tempbaos.write(bcontent);
int envbeg = tempbaos.toString().indexOf(MMConstants.BEGINXMLFLAG);
int envend = tempbaos.toString().indexOf("</env:Envelope>");
env = tempbaos.toString().substring(envbeg, envend);
env = env + "</env:Envelope>";
}
catch (IOException ioe) {
// ioe.printStackTrace();
log.error("内容写入字节流出错:" + ioe);
}
finally {
// 设置消息体长度
afterAuth.append("Content-Length:" + bcontent.length + "\r\n");
afterAuth.append("Mime-Version:1.0" + "\r\n");
afterAuth.append("\r\n");
entityBody.append(new String(bcontent));
sendBaos = getSendMessage(bcontent);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -