?? mmencoder.java
字號(hào):
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Tambur MMS library.
*
* The Initial Developer of the Original Code is FlyerOne Ltd.
* Portions created by the Initial Developer are Copyright (C) 2005
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Anders Lindh <alindh@flyerone.com>
*
* ***** END LICENSE BLOCK ***** */
package net.tambur.mms;
import java.io.ByteArrayOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.Locale;
import java.util.StringTokenizer;
import org.apache.log4j.Logger;
/**
* This class performs conversions of MMMessages to other formats
* (i.e. to encapsulated, to string, etc.)
*
* @author Anders Lindh
* @copyright Copyright FlyerOne Ltd 2005
* @version $Revision: 1.1.1.1 $ $Date: 2005/04/14 09:04:10 $
*/
public class MMEncoder {
/**
* logger
*/
protected transient static Logger log = Logger.getLogger(MMEncoder.class);
/**
* Encode the message, i.e. create a valid mms encapsulated byte array
*
* @param msg MMMmessage to be encoded
* @return byte[] containing the binary representation of this message
* @throws MMEncodingException
*/
public static byte[] encode(MMMessage msg) throws MMEncodingException {
ByteArrayOutputStream res = new ByteArrayOutputStream();
log.debug("Encoding message");
validateMMMessage(msg);
// first X-Mms-Message-Type, X-Mms-Transaction-Id and X-Mms-Version (in this order)
// these codes can be found in [MMSEncapsulation]
try {
encodeInt(res, 0x0C); // message type
encodeInt(res, msg.getMessageType()); // (m-notification-ind)
if (msg.getTransactionId() != null) {
encodeInt(res, 0x18); // transaction-id
encodeString(res, msg.getTransactionId());
}
encodeInt(res, 0x0d); // version
encodeInt(res, msg.version); // 1.0
// add existing headers
if (msg.getBcc() != null) {
// encode all recipients
Iterator i = msg.getBcc().iterator();
while (i.hasNext()) {
String s = (String) i.next();
encodeInt(res, 0x01); // bcc
encodeString(res, s);
}
}
if (msg.getCc() != null) {
// encode all recipients
Iterator i = msg.getCc().iterator();
while (i.hasNext()) {
String s = (String) i.next();
encodeInt(res, 0x02); // cc
encodeString(res, s);
}
}
if (msg.getContentLocation() != null) {
encodeInt(res, 0x03); // content-location
encodeString(res, msg.getContentLocation());
}
if (msg.getDate() != null) {
encodeInt(res, 0x05); // date
encodeDate(res, msg.getDate());
}
if (msg.getFrom() != null) {
encodeInt(res, 0x09); // from
encodeFrom(res, msg.getFrom());
}
if (msg.getTo() != null) {
// encode all recipients
Iterator i = msg.getTo().iterator();
while (i.hasNext()) {
String s = (String) i.next();
encodeInt(res, 0x17); // to
encodeString(res, s);
}
}
if (msg.getSubject() != null) {
encodeInt(res, 0x16); // subject
encodeString(res, msg.getSubject());
}
if (msg.deliveryReport != null) {
encodeInt(res, 0x06); // delivery-report
encodeBoolean(res, msg.deliveryReport);
}
if (msg.getSenderVisibility() != null) {
encodeInt(res, 0x14); // sender-visibility
encodeBoolean(res, msg.getSenderVisibility());
}
if (msg.readReply != null) {
encodeInt(res, 0x10); // read-reply
encodeBoolean(res, msg.readReply);
}
if (msg.getMessageClass() > -1) {
if (msg.getMessageClass() >= MMConstants.MESSAGE_CLASSES.length)
throw new MMEncodingException("Invalid X-MMS-Message-Class");
encodeInt(res, 0x0A); // message-class
encodeInt(res, msg.getMessageClass());
}
if (msg.getExpiry() != null) {
encodeInt(res, 0x08); // expiry
encodeDateVariable(res, msg.getExpiry(), msg.expiryAbsolute);
}
if (msg.getDeliveryTime() != null) {
encodeInt(res, 0x07); // delivery-time
encodeDateVariable(res, msg.getDeliveryTime(), msg.deliveryTimeAbsolute);
}
if (msg.getStatus() > -1) {
if (msg.getStatus() >= MMConstants.STATUSES.length)
throw new MMEncodingException("Invalid X-MMS-Status");
encodeInt(res, 0x15); // status
encodeInt(res, msg.getStatus());
}
if (msg.isMessageIdAvailable()) {
encodeInt(res, 0x0B); // message id
encodeString(res, msg.getMessageId());
}
if (msg.getMessageSize() != -1) {
encodeInt(res, 0x0E); // message-size
encodeLong(res, msg.getMessageSize());
}
if (msg.getPriority() > -1) {
if (msg.getPriority() >= MMConstants.PRIORITIES.length)
throw new MMEncodingException("Invalid X-MMS-Priority");
encodeInt(res, 0x0F); // priority
encodeInt(res, msg.getPriority());
}
if (msg.reportAllowed != null) {
encodeInt(res, 0x11); // report-allowed
encodeBoolean(res, msg.reportAllowed);
}
if (msg.getResponseStatus() > -1) {
if (msg.getResponseStatus() >= MMConstants.RESPONSE_STATUSES.length)
throw new MMEncodingException("Invalid X-MMS-Response-Status");
encodeInt(res, 0x12); // response-status
encodeInt(res, msg.getResponseStatus());
}
if (msg.getResponseText() != null) {
encodeInt(res, 0x13); // response-text
encodeString(res, msg.getResponseText());
}
// encode any custom headers
Iterator i = msg.getAttributes();
while (i.hasNext()) {
String name = (String) i.next();
String value = msg.getAttribute(name);
int a = 0;
for (a = 0; a < MMConstants.knownMMSHeaders.length; a++)
if (((String) MMConstants.knownMMSHeaders[a][0])
.equalsIgnoreCase(name))
break;
if (a < MMConstants.knownMMSHeaders.length)
continue;
encodeMessageHeader(res, name, value);
}
if (msg.getContentType() != null) { // content-type, the last header
encodeInt(res, 0x04);
encodeContentType(res, msg.getContentType());
}
if (!msg.parts.isEmpty()) {
log.debug("\tEncoding parts:");
// a uintvar telling how many parts there are in this message
encodeUintvar(res, msg.parts.size());
// now add each part in message. We'll have to encode the headers if possible
// Parts in the message follow the following rule:
// Size of headers, size of data, headers, data
i = msg.parts.iterator();
int cnt = 1;
while (i.hasNext()) {
//debug(DEBUG_ENABLED, " Part: " + cnt++);
log.debug("\t\tPart: " + cnt++);
MimeMessage.MimePart part = (MimeMessage.MimePart) i.next();
byte[] c = part.getContent();
// encode headers (in different stream - we need the size)
ByteArrayOutputStream headers = new ByteArrayOutputStream();
// first content-type
ArrayList attribNames = part.getAttributeNames();
ArrayList attribValues = part.getAttributeValues();
String value = part.getContentType();
if (value != null) {
encodeContentType(headers, value);
//debug(DEBUG_ENABLED, " Content-Type: " + value);
log.debug("\t\tContent-type:" + value);
}
Iterator penum = attribNames.iterator();
while (penum.hasNext()) {
String name = (String) penum.next();
value = part.getAttribute(name);
if (name.equalsIgnoreCase("Content-type")) continue;
encodeWSPHeader(headers, name, value);
}
byte[] h = headers.toByteArray();
// write sizes
encodeUintvar(res, h.length);
encodeUintvar(res, c.length);
log.debug("\t\tHeaders: " + h.length + ", content: " + c.length);
res.write(h); // write headers
res.write(c); // write content
}
}
res.flush();
} catch (Exception e) {
throw new MMEncodingException("Failed to encapsulate: " + e.toString());
}
return res.toByteArray();
}
/**
* Convert given MMMessage into a valid mime formatted message
*
* @param msg the message to be converted
* @throws MMEncodingException
*/
public static String toString(MMMessage msg) throws MMEncodingException {
MimeMessage mime = new MimeMessage();
validateMMMessage(msg);
// first X-Mms-Message-Type, X-Mms-Transaction-Id and X-Mms-Version (in this order)
try {
mime.setAttribute("X-MMS-Message-Type", msg.getMessageTypeStr()); // (m-notification-ind)
if (msg.getTransactionId() != null)
mime.setAttribute("X-MMS-Transaction-Id", msg.getTransactionId());
mime.setAttribute("X-MMS-Version", MMDecoder.versionToString(msg.version));
// add existing headers
if (msg.isBccAvailable()) {
// encode all recipients
String bcc = "";
Iterator i = msg.getBcc().iterator();
while (i.hasNext()) {
String s = (String) i.next();
if (bcc.equals(""))
bcc += s; else bcc += ", " + s;
}
mime.setAttribute("Bcc", bcc);
}
if (msg.isCcAvailable()) {
// encode all recipients
String cc = "";
Iterator i = msg.getCc().iterator();
while (i.hasNext()) {
String s = (String) i.next();
if (cc.equals(""))
cc += s; else cc += ", " + s;
}
mime.setAttribute("Cc", cc);
}
if (msg.isContentLocationAvailable())
mime.setAttribute("X-MMS-Content-Location", msg.getContentLocation());
if (msg.isDateAvailable())
mime.setAttribute("Date", MMDecoder.formatDate(msg.getDate()));
if (msg.isFromAvailable())
mime.setAttribute("From", msg.getFrom());
if (msg.isToAvailable()) {
// encode all recipients
String to = "";
Iterator i = msg.getTo().iterator();
while (i.hasNext()) {
String s = (String) i.next();
if (to.equals(""))
to += s; else to += ", " + s;
}
mime.setAttribute("To", to);
}
if (msg.isSubjectAvailable())
mime.setAttribute("Subject", msg.getSubject());
if (msg.isDeliveryReportAvailable())
mime.setAttribute("X-MMS-Delivery-Report", String.valueOf(msg.deliveryReport));
if (msg.isSenderVisibilityAvailable())
mime.setAttribute("X-MMS-Sender-Visibility", String.valueOf(msg.getSenderVisibility()));
if (msg.isReadReplyAvailable())
mime.setAttribute("X-MMS-Read-Reply", String.valueOf(msg.readReply));
if (msg.isMessageClassAvailable()) {
if (msg.getMessageClass() >= MMConstants.MESSAGE_CLASSES.length)
throw new MMEncodingException("Invalid X-MMS-Message-Class");
mime.setAttribute("X-MMS-Message-Class", msg.getMessageClassStr());
}
if (msg.isExpiryAvailable())
mime.setAttribute("X-MMS-Expiry", MMDecoder.formatDate(msg.getExpiry()));
if (msg.isDeliveryTimeAvailable())
mime.setAttribute("X-MMS-Delivery-Time", MMDecoder.formatDate(msg.getDeliveryTime()));
if (msg.isStatusAvailable()) {
if (msg.getStatus() >= MMConstants.STATUSES.length)
throw new MMEncodingException("Invalid X-MMS-Status");
mime.setAttribute("X-MMS-Status", msg.getStatusStr());
}
if (msg.isMessageIdAvailable())
mime.setAttribute("Message-ID", msg.getMessageId());
if (msg.getMessageSize() != -1)
mime.setAttribute("Message-Size", String.valueOf(msg.getMessageSize()));
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -