package com.asiainfo.remind.channel.vasp;
import com.asiainfo.remind.channel.vasp.common.MMConstants;
import com.asiainfo.remind.channel.vasp.common.MMContent;
import com.asiainfo.remind.channel.vasp.conf.MM7Config;
import com.asiainfo.remind.channel.vasp.message.MM7RSRes;
import com.asiainfo.remind.channel.vasp.message.MM7SubmitReq;
import com.asiainfo.remind.channel.vasp.message.MM7SubmitRes;
import com.asiainfo.remind.channel.vasp.service.MM7Sender;
import com.asiainfo.remind.msg.MmsBeanTest;
import com.asiainfo.remind.utils.StringUtil;
import java.io.DataInputStream;
import java.util.HashMap;
import java.util.Map;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class MmsSenderForTest
{
private transient static Log log = LogFactory.getLog(MmsSenderForTest.class);
private static final String MM7_CONFIG = "mm7Config.xml";
private static final String CONN_CONFIG = "ConnConfig.xml";
private static MM7Config mm7Config = null;
static {
if (mm7Config == null) {
try {
mm7Config = new MM7Config(MM7_CONFIG);
mm7Config.setConnConfigName(CONN_CONFIG);
} catch (Exception e) {
log.error("初始化mm7Config失败!", e);
}
}
}
public static void send(MmsBeanTest MmsBeanTest) throws Exception {
MM7Sender mm7Sender = new MM7Sender(mm7Config);
MM7SubmitReq submit = getSubmit(MmsBeanTest);
MM7RSRes res = mm7Sender.send(submit);
if (res instanceof MM7SubmitRes) {
MM7SubmitRes subRes = (MM7SubmitRes) res;
if (log.isErrorEnabled())
log.error("messageid=" + subRes.getMessageID() + ";res.statuscode=" + res.getStatusCode() + ";res.statusText=" + res.getStatusText());
}
}
private static MM7SubmitReq getSubmit(MmsBeanTest MmsBeanTest) throws Exception{
MM7SubmitReq submit = new MM7SubmitReq();
submit.setTransactionID(StringUtil.getSeq7() + "");
submit.addTo(MmsBeanTest.getBillId());
submit.setVASID("10086");
submit.setVASPID("801306");
submit.setServiceCode("106001");
submit.setSenderAddress("801306");
submit.setChargedPartyID("10086");
submit.setSubject(MmsBeanTest.getSubject());
submit.setContent(getContent(MmsBeanTest));
return submit;
}
private static MMContent getContent(MmsBeanTest mmsBean) throws Exception{
String[] subType = mmsBean.getSubType();
DataHandler[] affix = mmsBean.getAffix();
byte[][] subContent = new byte[affix.length][];
for (int i = 0; i < subContent.length; i++) {
byte[] data = (byte[])null;
DataHandler dh = affix[i];
DataInputStream input = new DataInputStream(dh.getInputStream());
data = new byte[input.available()];
input.read(data, 0, data.length);
subContent[i] = data;
}
MMContent content = new MMContent();
//content.setContentType(MMConstants.ContentType.MULTIPART_MIXED);
content.setContentType(MMConstants.ContentType.MULTIPART_RELATED);
content.setContentID("main");
for (int i = 0; i < subType.length; i++) {
MMContent sub = MMContent.createFromBytes(subContent[i]);
subType[i] = subType[i].toLowerCase();
if (subType[i].equals("gif")) {
sub.setContentType(MMConstants.ContentType.GIF);
sub.setContentID("<"+i + ".gif>");
sub.setContentLocation(i + ".gif");
} else if (subType[i].equals("txt")) {
sub.setContentType(MMConstants.ContentType.TEXT);
sub.setCharset("UTF-8");
sub.setContentID("<"+i + ".txt>");
sub.setContentLocation(i + ".txt");
} else if (subType[i].equals("jpg")) {
sub.setContentType(MMConstants.ContentType.JPEG);
sub.setContentID("<"+i + ".jpg>");
sub.setContentLocation(i + ".jpg");
} else if (subType[i].equals("smil")) {
//content.setContentType(MMConstants.ContentType.MULTIPART_RELATED);
sub.setContentType(MMConstants.ContentType.SMIL);
sub.setCharset("UTF-8");
sub.setContentID("<"+i + ".smil>");
sub.setContentLocation(i + ".smil");
} else {
log.error("[1002]subType exist illegal type!");
throw new Exception("subType exist illegal type!");
}
content.addSubContent(sub);
}
return content;
}
public static void main(String[] args) throws Exception {
String url = Thread.currentThread().getContextClassLoader().getResource(".").getPath();
System.out.print(url);
MmsBeanTest mmsBean = new MmsBeanTest();
//mmsBean.setBillId("13810256053");
mmsBean.setBillId("13693652411");
mmsBean.setSubject("测试");
DataHandler dh = new DataHandler(new FileDataSource(url+"1.smil"));
DataHandler dh1 = new DataHandler(new FileDataSource(url+"2.txt"));
DataHandler dh2 = new DataHandler(new FileDataSource(url+"3.jpg"));
DataHandler dh3 = new DataHandler(new FileDataSource(url+"4.txt"));
String type[] = new String[]{"smil","txt","jpg","txt"};
DataHandler[] content = {dh,dh1,dh2,dh3};
mmsBean.setAffix(content);
mmsBean.setSubType(type);
Map map =new HashMap();
map.put("dh",content);
MmsSenderForTest.send(mmsBean);
}
}
smil内容:
<?xml version='1.0' encoding='GB2312'?>