001: /*
002: *
003: * Copyright (c) 2007, Sun Microsystems, Inc.
004: *
005: * All rights reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * * Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * * Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: * * Neither the name of Sun Microsystems nor the names of its contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032: package example.mms;
033:
034: import java.util.Vector;
035:
036: import javax.wireless.messaging.*;
037:
038: public class MMSMessage {
039: private String destination;
040: private Vector parts = new Vector();
041: private String subject;
042:
043: /**
044: * Check the phone number for validity
045: * Valid phone numbers contain only the digits 0 thru 9, and may contain
046: * a leading '+'.
047: */
048: private static boolean isValidPhoneNumber(String address) {
049: String protocol = "mms://";
050:
051: if (!address.startsWith(protocol)) {
052: return false;
053: }
054:
055: String number = address.substring(protocol.length());
056: char[] chars = number.toCharArray();
057:
058: if (chars.length == 0) {
059: return false;
060: }
061:
062: int startPos = 0;
063:
064: // initial '+' is OK
065: if (chars[0] == '+') {
066: startPos = 1;
067: }
068:
069: for (int i = startPos; i < chars.length; ++i) {
070: if (!Character.isDigit(chars[i])) {
071: return false;
072: }
073: }
074:
075: return true;
076: }
077:
078: public String getSubject() {
079: return subject;
080: }
081:
082: public void setSubject(String subject) {
083: this .subject = subject;
084: }
085:
086: public String getDestination() {
087: return destination;
088: }
089:
090: public void setDestination(String destination) {
091: if (!isValidPhoneNumber(destination)) {
092: throw new IllegalArgumentException("Invalid phone number");
093: }
094:
095: this .destination = destination;
096: }
097:
098: public MessagePart[] getParts() {
099: MessagePart[] partsArray = new MessagePart[parts.size()];
100: parts.copyInto(partsArray);
101:
102: return partsArray;
103: }
104:
105: public void addPart(MessagePart part) {
106: parts.addElement(part);
107: }
108: }
|