001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.tck.wma.sms;
028:
029: import com.sun.tck.wma.Message;
030: import java.util.Date;
031:
032: /**
033: * A message for a message connection, composed of an address and data. There
034: * are get and set methods for manipulating the address and data components of
035: * the message. The data part can be the text, binary or multipart format. The
036: * address part assumes this format:
037: * <p>
038: * <code>protocol://[<em>phone_number</em>:][<em>port_number/ID</em>]</code>
039: * <p>
040: * and represents the address of a port/ID that can accept or receive messages.
041: *<p>
042: * <code>MessageObject</code>s are instantiated when they are received from the
043: * {@link com.sun.tck.wma.MessageConnection MessageConnection} or by using the
044: * {@link MessageConnection#newMessage(String type)
045: * MessageConnection.newMessage} message factory. Instances are freed when they
046: * are garbage-collected or when they go out of scope.
047: */
048: public class MessageObject implements Message {
049:
050: /** High-level message type. */
051: protected String msgType = null;
052:
053: /** High-level message address. */
054: protected String msgAddress = null;
055:
056: /** The time stamp, indicating when the message was sent. */
057: protected long sentAt = -1;
058:
059: /**
060: * Creates a Message object without a buffer.
061: *
062: * @param type The message type: TEXT, BINARY or MULTIPART.
063: * @param address The destination address of the message.
064: */
065: public MessageObject(String type, String address) {
066: setType(type);
067: setAddress(address);
068: }
069:
070: /**
071: * Sets the message type.
072: *
073: * @param type The message type: TEXT, BINARY or MULTIPART.
074: *
075: * @exception IllegalArgumentException if the type is not valid.
076: */
077: public void setType(String type) {
078: if (type == null) {
079: throw new IllegalArgumentException("Null message type.");
080: }
081: msgType = type;
082: }
083:
084: /**
085: * Gets the message type.
086: *
087: * @return The current message type or <code>null</code> if no message type
088: * has been set.
089: */
090: public String getType() {
091: return msgType;
092: }
093:
094: /**
095: * Sets the address part of the message object. The address is a
096: * <code>String</code> and must be in the format:
097: * <code>protocol://<em>phone_number</em>:[<em>port</em>]</code>
098: * The following code sample assigns an address to the <code>Message</code>
099: * object.
100: * <pre>
101: * ...
102: * String addr = "protocol://+123456789";
103: * Message msg = newMessage(MessageConnection.TEXT_MESSAGE);
104: * msg.setAddress(addr);
105: * ...
106: * </pre>
107: *
108: * @param address The address of the target device.
109: *
110: * @throws IllegalArgumentException if the address is not valid.
111: *
112: * @see #getAddress
113: */
114: public void setAddress(String address) {
115:
116: msgAddress = address;
117: }
118:
119: /**
120: * Gets the address from the message object as a <code>String</code>. If no
121: * address is found in the message, this method returns <code>null</code>.
122: * If the method is applied to an inbound message, the source address is
123: * returned. If it is applied to an outbound message, the destination
124: * address is returned.
125: * <p>
126: * The following code sample retrieves the address from a received message.
127: * <pre>
128: * ...
129: * Message msg = conn.receive();
130: * String addr = msg.getAddress();
131: * ...
132: * </pre>
133: * ...
134: * @return The address in string form, or <code>null</code> if no
135: * address was set.
136: *
137: * @see #setAddress
138: */
139: public String getAddress() {
140: return msgAddress;
141: }
142:
143: /**
144: * Sets the timestamp for inbound SMS messages.
145: *
146: * @param timestamp The time stamp in the message.
147: *
148: * @see #getTimeStamp
149: */
150: public void setTimeStamp(long timestamp) {
151: sentAt = timestamp;
152: }
153:
154: /**
155: * Returns the timestamp indicating when this message has been sent.
156: *
157: * @return Date indicating the time stamp in the message or
158: * <code>null</code> if the time stamp was not set.
159: *
160: * @see #setTimeStamp
161: */
162: public Date getTimestamp() {
163: return new Date(sentAt);
164: }
165:
166: }
|