📄 mm7sender.java
字号:
/**File Name:MM7Sender.java
* Company: 中国移动集团公司
* Date : 2004-1-17
* */
package com.hxyh.sanny.mms.cmcc.mm7.vasp.service;
import java.io.*;
import java.net.*;
import java.util.*;
import java.text.*;
import sun.misc.*;
import org.jdom.*;
import org.jdom.input.*;
import java.security.*;
import com.hxyh.sanny.mms.cmcc.mm7.vasp.common.*;
import com.hxyh.sanny.mms.cmcc.mm7.vasp.conf.*;
import com.hxyh.sanny.mms.cmcc.mm7.vasp.message.*;
public class MM7Sender {
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;
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;
}
/** 构造方法 */
public MM7Sender(MM7Config config) throws Exception {
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) {
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 (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 (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();
} 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);
String time = "["
+ simple.format(new Date(System.currentTimeMillis()))
+ "]";
if (sendBaos != null) {
res = SendandReceiveMessage(sendBaos);
} else {
MM7RSErrorRes ErrorRes = new MM7RSErrorRes();
ErrorRes.setStatusCode(-104);
ErrorRes.setStatusText("Socket不通!");
SevereBuffer.append("[Message_Type=MM7RSErrorRes]");
SevereBuffer.append("[Comments={"
+ ErrorRes.getStatusCode());
SevereBuffer.append(";" + ErrorRes.getStatusText() + "}]");
return ErrorRes;
}
FinerBuffer.append(InfoBuffer);
InfoBuffer.append(env);
SevereBuffer.insert(0, "\r\n\r\n" + time + "[1]");
InfoBuffer.insert(0, "\r\n\r\n" + time + "[3]");
FinerBuffer.insert(0, "\r\n\r\n" + time + "[6]");
Severebaos.write(SevereBuffer.toString().getBytes());
Infobaos.write(SevereBuffer.toString().getBytes());
Infobaos.write(InfoBuffer.toString().getBytes());
Finerbaos.write(SevereBuffer.toString().getBytes());
Finerbaos.write(InfoBuffer.toString().getBytes());
Finerbaos.write(FinerBuffer.toString().getBytes());
Finerbaos.write(bcontent);
/** ********** */
InfoBuffer = new StringBuffer();
FinerBuffer = new StringBuffer();
time = "["
+ simple.format(new Date(System.currentTimeMillis()))
+ "]";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -