www.pudn.com > mobileMms.rar > SOAPEncoder.java
/**File Name:SOAPEncoder.java
* Company: 中国移动集团公司
* Date : 2004-1-8
* */
package com.cmcc.mm7.vasp.common;
import java.io.*;
import java.util.*;
import java.text.*;
import sun.misc.BASE64Encoder;
import com.cmcc.mm7.vasp.message.*;
import com.cmcc.mm7.vasp.conf.*;
public class SOAPEncoder
{
private MM7VASPReq mm7VaspReq;
private boolean bMessageExist;
private boolean bEncoder;
private ByteArrayOutputStream byteOutput;
private MM7Config mm7Config;
/**默认构造方法*/
public SOAPEncoder()
{
reset();
}
public SOAPEncoder(MM7Config config)
{
mm7Config = config;
}
public void reset()
{
mm7VaspReq = null;
bMessageExist = false;
bEncoder = false;
byteOutput = null;
mm7Config = null;
}
/**设置MM7VASPReq类型的消息*/
public void setMessage(MM7VASPReq mm7vaspreq)
{
mm7VaspReq = mm7vaspreq;
bMessageExist = true;
}
/**得到byte[]形式的消息*/
public byte[] getMessage()
{
if(bEncoder)
return(byteOutput.toByteArray());
else
return(null);
}
/**进行消息的编码*/
public void encodeMessage() throws SOAPEncodeException
{
if(!bMessageExist)
throw new SOAPEncodeException("No Multimedia Messages set in the encoder!");
try
{
byteOutput = new ByteArrayOutputStream();
bEncoder = false;
StringBuffer sb = new StringBuffer();
StringBuffer ContentBuffer = new StringBuffer();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
//add by hudm 2004-06-21
if(mm7VaspReq instanceof MM7SubmitReq)
{
MM7SubmitReq req = (MM7SubmitReq)mm7VaspReq;
if(req.isContentExist())
{
sb.append("this is a multi-part message in MIME format" + "\r\n");
sb.append("\r\n");
sb.append("\r\n");
sb.append("----NextPart_0_2817_24856" + "\r\n");
sb.append("Content-Type:text/xml;charset=\"" + mm7Config.getCharSet() +
"\"\r\n");
sb.append("Content-Transfer-Encoding:8bit" + "\r\n");
sb.append("Content-ID:" + "\r\n");
sb.append("\r\n");
}
}
else if(mm7VaspReq instanceof MM7ReplaceReq)
{
MM7ReplaceReq req = (MM7ReplaceReq)mm7VaspReq;
if(req.isContentExist())
{
sb.append("this is a multi-part message in MIME format" + "\r\n");
sb.append("\r\n");
sb.append("\r\n");
sb.append("----NextPart_0_2817_24856" + "\r\n");
sb.append("Content-Type:text/xml;charset=\"" + mm7Config.getCharSet() +
"\"\r\n");
sb.append("Content-Transfer-Encoding:8bit" + "\r\n");
sb.append("Content-ID:" + "\r\n");
sb.append("\r\n");
}
}
//end add by hudm 2004-06-21
sb.append("");//
if(mm7VaspReq.isTransactionIDExist()) //
sb.append(""+mm7VaspReq.getTransactionID()+" ");
else
System.out.println("TransactionID 不许为空!");
sb.append(" ");
/**
* 若发送的消息为MM7SubmitReq
* */
if(mm7VaspReq instanceof MM7SubmitReq){
MM7SubmitReq submitReq = (MM7SubmitReq) mm7VaspReq;
sb.append("");
if(submitReq.isMM7VersionExist())
sb.append(""+submitReq.getMM7Version()+" ");
else
System.out.println("MM7Version 不许为空!");
if(submitReq.isVASPIDExist() || submitReq.isVASIDExist() || submitReq.isSenderAddressExist())
{
sb.append("");
if (submitReq.isVASPIDExist())
sb.append(""+submitReq.getVASPID()+" ");
else
System.out.println("SP代码VASPID不许为空!");
if(submitReq.isVASIDExist())
sb.append(""+submitReq.getVASID()+" ");
else
System.out.println("服务代码VASID不许为空!");
if(submitReq.isSenderAddressExist())
sb.append(""+submitReq.getSenderAddress()+" ");
sb.append(" ");
}
else
System.out.println("SP代码VASPID和服务代码VASID均不允许为空!");
if(submitReq.isToExist() || submitReq.isCcExist() || submitReq.isBccExist())
{
sb.append("");
if (submitReq.isToExist()) {
sb.append("");
List ToList = new ArrayList();
ToList = submitReq.getTo();
for (int i = 0; i < ToList.size(); i++)
{
String strto = (String)ToList.get(i);
if(strto.indexOf('@')>0)
sb.append(""+strto+" ");
else
sb.append("" + strto + " ");
}
sb.append(" ");
}
if(submitReq.isCcExist()){
sb.append("");
List CcList = new ArrayList();
CcList = submitReq.getCc();
for (int i = 0; i < CcList.size(); i++)
{
String strcc = (String)CcList.get(i);
if(strcc.indexOf('@')>0)
sb.append(""+strcc+" ");
else
sb.append("" + strcc + " ");
}
sb.append(" ");
}
if(submitReq.isBccExist()){
sb.append("");
List BccList = new ArrayList();
BccList = submitReq.getBcc();
for(int i=0;i0)
sb.append(""+strbcc+" ");
else
sb.append("" +strbcc+ " ");
}
sb.append(" ");
}
sb.append(" ");
}
else
System.out.println("接收方地址To、抄送方地址Cc和密送方地址Bcc中至少需要有一个不为空!");
if(submitReq.isServiceCodeExist())
sb.append(""+submitReq.getServiceCode()+" ");
else
System.out.println("业务代码ServiceCode不许为空!");
if(submitReq.isLinkedIDExist())
sb.append(""+submitReq.getLinkedID()+" ");
if(submitReq.isMessageClassExist())
sb.append(""+submitReq.getMessageClass()+" ");
if(submitReq.isTimeStampExist())
sb.append(""+sdf.format(submitReq.getTimeStamp())+" ");
if(submitReq.isReplyChargingExist())
{
sb.append(" ");
}
if(submitReq.isEarliestDeliveryTimeExist())
{
Date dd = new Date(submitReq.getEarliestDeliveryTimeRelative());
sb.append(""+sdf.format(dd)+"+08:00"+" ");
}
if(submitReq.isEarliestDeliveryTimeAbsolute())
sb.append(""+
sdf.format(submitReq.getEarliestDeliveryTimeAbsolute())+"+08:00"+
" ");
if(submitReq.isExpiryDateExist())
sb.append(""+
sdf.format(new Date(submitReq.getExpiryDateRelative()))
+"+08:00"+" ");
if(submitReq.isExpiryDateAbsolute())
sb.append(""+sdf.format(submitReq.getExpiryDateAbsolute())+"+08:00"+" ");
if(submitReq.isDeliveryReportExist())
sb.append(""+submitReq.getDeliveryReport()+" ");
if(submitReq.isReadReplyExist())
sb.append(""+submitReq.getReadReply()+" ");
/* if(submitReq.isReplyChargingExist())
sb.append(""+submitReq.getReplyCharging()+" ");
if(submitReq.isReplyDeadlineExist())
sb.append(""+sdf.format(new Date(
submitReq.getReplyDeadlineRelative()))+"+08:00"+" ");
if(submitReq.isReplyDeadlineAbsoluteExist())
sb.append(""+sdf.format(
submitReq.getReplyDeadlineAbsolute())+"+08:00"+" ");
if(submitReq.isReplyChargingSizeExist())
sb.append(""+submitReq.getReplyChargingSize()+" ");*/
if(submitReq.isPriorityExist())
{
// sb.append("" + submitReq.getPriority() + " ");
if(submitReq.getPriority() == (byte)0)
sb.append("" + "Low" + " ");
else if(submitReq.getPriority() == (byte)1)
sb.append("" + "Normal" + " ");
else if(submitReq.getPriority() == (byte)2)
sb.append("" + "High" + " ");
}
if(submitReq.isSubjectExist())
sb.append(""+submitReq.getSubject()+" ");
if(submitReq.isChargedPartyExist())
{
// sb.append(""+submitReq.getChargedParty()+" ");
if(submitReq.getChargedParty() == (byte)0)
sb.append("Sender ");
else if(submitReq.getChargedParty() == (byte)1)
sb.append("Recipient ");
else if(submitReq.getChargedParty() == (byte)2)
sb.append("Both ");
else if(submitReq.getChargedParty() == (byte)3)
sb.append("Neither ");
else if(submitReq.getChargedParty() == (byte)4)
sb.append("ThirdParty ");
}
if(submitReq.isChargedPartyIDExist())
sb.append(""+submitReq.getChargedPartyID()+" ");
if(submitReq.isDistributionIndicatorExist())
sb.append(""+submitReq.getDistributionIndicator()+" ");
if(submitReq.isContentExist())
{
//begin add by hudm 2004-03-09
/*if(submitReq.getContent().isContentIDExist())
sb.append(" ");
else
sb.append("/>");*/
//end add by hudm 2004-03-09*/
//sb.append("");
sb.append(" ");
sb.append(" ");
sb.append("\r\n");
sb.append("----NextPart_0_2817_24856\r\n");
MMContent parentContent = submitReq.getContent();
// sb.append("Content-Type:"+parentContent.getContentType().getPrimaryType()+"/"+
// parentContent.getContentType().getSubType()+
// ";boundary=\"SubPart_7452684322002_77645\""+"\r\n");
if(parentContent.getContentType() != null)
{
String strSubType = "";
String strtempID = "";
strSubType = parentContent.getContentType().getSubType();
if(strSubType.equalsIgnoreCase("related"))
{
if(parentContent.isMultipart())
{
List tempSub = new ArrayList();
tempSub = parentContent.getSubContents();
for(int x=0;x"+"\r\n");
sb.append("Content-Transfer-Encoding:8bit" + "\r\n");
if(parentContent.isContentLocationExist())
sb.append("Content-Location:"+parentContent.getContentLocation()+"\r\n");
sb.append("\r\n");
ByteArrayOutputStream Subbaos = new ByteArrayOutputStream();
if(parentContent.isMultipart())
{
List subContent = new ArrayList();
subContent = parentContent.getSubContents();
for(int i=0;i\r\n");
}
else
{
if (content.isContentIDExist())
ContentBuffer.append("Content-ID:" + content.getContentID() +
"\r\n");
}
if (content.isContentLocationExist())
ContentBuffer.append("Content-Location:" +
content.getContentLocation() + "\r\n");
ContentBuffer.append("\r\n");
try{
Subbaos.write(ContentBuffer.toString().getBytes());
Subbaos.write(content.getContent());
Subbaos.write("\r\n\r\n".getBytes());
}catch(IOException e){
e.printStackTrace();
}
}
Subbaos.write("--SubPart_7452684322002_77645--\r\n".getBytes());
Subbaos.write("----NextPart_0_2817_24856--\r\n".getBytes());
byteOutput.write(sb.toString().getBytes(""+mm7Config.getCharSet()));
byteOutput.write(Subbaos.toByteArray());
}
}
else
{
//sb.append("");
sb.append("");
sb.append("");
//Cancel by hudm 2004-06-21
//sb.append("----NextPart_0_2817_24856--\r\n");
//end Cancel by hudm 2004-06-21
byteOutput.write(sb.toString().getBytes());
}
}
/**
* 若发送的消息为MM7CancelReq
* */
else if(mm7VaspReq instanceof MM7CancelReq){
MM7CancelReq cancelReq = (MM7CancelReq)mm7VaspReq;
sb.append("");
if(cancelReq.isMM7VersionExist())
sb.append(""+cancelReq.getMM7Version()+" ");
else
System.out.println("MM7Version 不许为空!");
if(cancelReq.isVASPIDExist() || cancelReq.isVASIDExist())
{
sb.append("");
if (cancelReq.isVASPIDExist())
sb.append(""+cancelReq.getVASPID()+" ");
if(cancelReq.isVASIDExist())
sb.append(""+cancelReq.getVASID()+" ");
sb.append(" ");
}
if(cancelReq.isSenderAddressExist())
sb.append(""+cancelReq.getSenderAddress()+" ");
if(cancelReq.isMessageIDExist())
sb.append(""+cancelReq.getMessageID()+" ");
else
System.out.println("待取消的消息的标识符MessageID不许为空!");
sb.append(" ");
sb.append("");
//Cancel by hudm 2004-06-21
//sb.append("----NextPart_0_2817_24856--");
//end Cancel by hudm 2004-06-21
byteOutput.write(sb.toString().getBytes());
}
/**
* 若发送的消息为MM7ReplaceReq
* */
else if(mm7VaspReq instanceof MM7ReplaceReq){
MM7ReplaceReq replaceReq = (MM7ReplaceReq) mm7VaspReq;
sb.append("");
if(replaceReq.isMM7VersionExist())
sb.append(""+replaceReq.getMM7Version()+" ");
if(replaceReq.isVASPIDExist() || replaceReq.isVASIDExist())
{
sb.append("");
if (replaceReq.isVASPIDExist())
sb.append(""+replaceReq.getVASPID()+" ");
if(replaceReq.isVASIDExist())
sb.append(""+replaceReq.getVASID()+" ");
sb.append(" ");
}
if(replaceReq.isMessageIDExist())
sb.append(""+replaceReq.getMessageID()+" ");
else
System.out.println("被当前消息所替换的消息的标识符MessageID 不许为空!");
if(replaceReq.isServiceCodeExist())
sb.append(""+replaceReq.getServiceCode()+" ");
if(replaceReq.isTimeStampExist())
sb.append(""+sdf.format(replaceReq.getTimeStamp())+" ");
if(replaceReq.isEarliestDeliveryTimeExist())
{
String earliestTime = sdf.format(new Date(
replaceReq.getEarliestDeliveryTimeRelative()))+"+08:00";
sb.append("" + earliestTime +" ");
}
if(replaceReq.isEarliestDeliveryTimeAbsoluteExist())
sb.append(""+
sdf.format(replaceReq.getEarliestDeliveryTimeAbsolute())+"+08:00"+
" ");
if(replaceReq.isReadReplyExist())
sb.append(""+replaceReq.getReadReply()+" ");
if(replaceReq.isDistributionIndicatorExist())
sb.append(""+replaceReq.getDistributionIndicator()
+" ");
if(replaceReq.isContentExist())
{
/*if(replaceReq.getContent().isContentIDExist())
sb.append(" ");
else
sb.append("/>");*/ //;allowAdaptions=\"False\"*/
sb.append(" ");
sb.append("" + "\r\n");
sb.append("----NextPart_0_2817_24856\r\n");
MMContent parentContent = replaceReq.getContent();
if(parentContent.getContentType()!=null)
{
/*sb.append("Content-Type:" + parentContent.getContentType().getPrimaryType()
+"/" +parentContent.getContentType().getSubType()+
";boundary=\"SubPart_7452684322002_77645\"" + "\r\n");*/
String strSubType = "";
String strtempID = "";
strSubType = parentContent.getContentType().getSubType();
if (strSubType.equalsIgnoreCase("related")) {
if (parentContent.isMultipart()) {
List tempSub = new ArrayList();
tempSub = parentContent.getSubContents();
for (int x = 0; x < tempSub.size(); x++) {
MMContent tempCon = (MMContent) tempSub.get(x);
if (tempCon.getContentType().getSubType().equalsIgnoreCase(
"smil")) {
if (tempCon.isContentIDExist())
strtempID = tempCon.getContentID();
else
strtempID = "";
break;
}
}
}
sb.append("Content-Type:" + "multipart/related;" +
"start=\"" + strtempID + "\";type=\"application/smil\"" +
";boundary=\"SubPart_7452684322002_77645\"" + "\r\n");
}
else {
sb.append("Content-Type:" +
parentContent.getContentType().getPrimaryType()
+ "/" + parentContent.getContentType().getSubType() +
";boundary=\"SubPart_7452684322002_77645\"" + "\r\n");
}
}
else
{
sb.append("Content-Type:" + "multipart/mixed" +
";boundary=\"SubPart_7452684322002_77645\"" + "\r\n");
}
sb.append("Content-Transfer-Encoding:8bit" + "\r\n");
if(parentContent.isContentIDExist())
sb.append("Content-ID:"+parentContent.getContentID()+"\r\n");
else
sb.append("Content-ID:"+"\r\n");
if(parentContent.isContentLocationExist())
sb.append("Content-Location:"+parentContent.getContentLocation()+"\r\n");
sb.append("\r\n");
ByteArrayOutputStream Subbaos = new ByteArrayOutputStream();
if (parentContent.isMultipart()) {
List subContent = new ArrayList();
subContent = parentContent.getSubContents();
for (int i = 0; i < subContent.size(); i++) {
ContentBuffer = new StringBuffer();
ContentBuffer.append("----SubPart_7452684322002_77645"+"\r\n");
MMContent content = (MMContent) subContent.get(i);
if (content.getContentType() != null)
ContentBuffer.append("Content-Type:" +
content.getContentType().getPrimaryType() +
"/" +
content.getContentType().getSubType() +
"\r\n");
else {
if (content.isContentIDExist()) {
String strContentID = content.getContentID();
int index = strContentID.indexOf(".");
String type = strContentID.substring(index + 1);
type = type.toLowerCase();
if (type.equals("txt")) {
ContentBuffer.append("Content-Type:text/plain" + "\r\n");
}
else if (type.equals("jpg")) {
ContentBuffer.append("Content-Type:image/jpeg" + "\r\n");
}
else if (type.equals("gif")) {
ContentBuffer.append("Content-Type:image/gif" + "\r\n");
}
}
}
ContentBuffer.append("Content-Transfer-Encoding:8bit" + "\r\n");
if (content.isContentIDExist())
ContentBuffer.append("Content-ID:" + content.getContentID() +
"\r\n");
if(content.isContentLocationExist())
ContentBuffer.append("Content-Location:"+content.getContentLocation()+"\r\n");
ContentBuffer.append("\r\n");
try{
Subbaos.write(ContentBuffer.toString().getBytes());
Subbaos.write(content.getContent());
Subbaos.write("\r\n\r\n".getBytes());
}catch(IOException e){
e.printStackTrace();
}
}
Subbaos.write("--SubPart_7452684322002_77645--\r\n".getBytes());
Subbaos.write("----NextPart_0_2817_24856--".getBytes());
byteOutput.write(sb.toString().getBytes());
byteOutput.write(Subbaos.toByteArray());
}
}
else
{
sb.append("");
sb.append("");
//Cancel by hudm 2004-06-21
//sb.append("----NextPart_0_2817_24856--");
//end Cancel by hudm 2004-06-21
byteOutput.write(sb.toString().getBytes());
}
}
bEncoder = true;
}
catch (Exception e) {
System.err.println(e);
}
}
/**进行BASE64编码*/
public static String getBASE64(String value)
{
if(value == null)
return null;
BASE64Encoder BaseEncode = new BASE64Encoder();
return(BaseEncode.encode(value.getBytes()));
}
}