001: package org.claros.commons.mail.utility;
002:
003: import java.io.UnsupportedEncodingException;
004: import java.util.StringTokenizer;
005:
006: import javax.mail.Address;
007: import javax.mail.internet.AddressException;
008: import javax.mail.internet.InternetAddress;
009:
010: import org.claros.commons.configuration.PropertyFile;
011:
012: /**
013: * @author Umut Gokbayrak
014: *
015: */
016: public class Utility {
017: private Utility() {
018: super ();
019: }
020:
021: /**
022: *
023: * @param addr
024: * @return
025: */
026: public static String[] addressArrToStringArr(Address[] addr) {
027: if (addr != null && addr.length > 0) {
028: String[] str = new String[addr.length];
029: for (int j = 0; j < addr.length; j++) {
030: InternetAddress add = (InternetAddress) addr[j];
031: String personal = org.claros.commons.utility.Utility
032: .doCharsetCorrections(add.getPersonal());
033: String address = org.claros.commons.utility.Utility
034: .doCharsetCorrections(add.getAddress());
035:
036: if (personal != null && personal.length() > 0) {
037: if (address != null && address.length() > 0) {
038: str[j] = personal + " <" + address + ">";
039: } else {
040: str[j] = personal;
041: }
042: } else {
043: if (address != null && address.length() > 0) {
044: str[j] = address;
045: } else {
046: str[j] = "";
047: }
048: }
049: }
050: return str;
051: }
052: return null;
053: }
054:
055: /**
056: *
057: * @param addr
058: * @return
059: */
060: public static String addressArrToString(Address[] addr) {
061: String addrStr = "";
062: String str[] = addressArrToStringArr(addr);
063: if (str != null && str.length > 0) {
064: addrStr = "";
065: for (int i = 0; i < str.length; i++) {
066: addrStr += str[i] + ", ";
067: }
068: }
069: String msg = org.claros.commons.utility.Utility.extendedTrim(
070: addrStr, ",");
071: return org.claros.commons.utility.Utility
072: .doCharsetCorrections(msg);
073: }
074:
075: /**
076: *
077: * @param addr
078: * @return
079: */
080: public static String[] addressArrToStringArrShort(Address[] addr) {
081: if (addr != null && addr.length > 0) {
082: String[] str = new String[addr.length];
083: for (int j = 0; j < addr.length; j++) {
084: InternetAddress add = (InternetAddress) addr[j];
085: String personal = org.claros.commons.utility.Utility
086: .doCharsetCorrections(add.getPersonal());
087: String address = org.claros.commons.utility.Utility
088: .doCharsetCorrections(add.getAddress());
089:
090: if (personal != null && personal.length() > 0) {
091: str[j] = personal;
092: } else if (address != null && address.length() > 0) {
093: str[j] = address;
094: } else {
095: str[j] = "Unknown";
096: }
097: }
098: return str;
099: }
100: return null;
101: }
102:
103: /**
104: *
105: * @param addr
106: * @return
107: */
108: public static String addressArrToStringShort(Address[] addr) {
109: String addrStr = "";
110: String str[] = addressArrToStringArrShort(addr);
111: if (str != null && str.length > 0) {
112: addrStr = "";
113: for (int i = 0; i < str.length; i++) {
114: addrStr += str[i] + ", ";
115: }
116: }
117: String msg = org.claros.commons.utility.Utility.extendedTrim(
118: addrStr, ",");
119: msg = org.claros.commons.utility.Utility
120: .doCharsetCorrections(msg);
121: return msg;
122: }
123:
124: /**
125: *
126: * @param str
127: * @return
128: * @throws Exception
129: */
130: public static Address[] stringToAddressArray(String str)
131: throws Exception {
132: if (str == null)
133: return null;
134:
135: str = org.claros.commons.utility.Utility.extendedTrim(str, ",");
136: StringTokenizer token = new StringTokenizer(str, ",");
137: if (token.countTokens() != 0) {
138: Address[] outAddr = new Address[token.countTokens()];
139: int counter = 0;
140: while (token.hasMoreTokens()) {
141: String addr = token.nextToken().trim();
142: addr = org.claros.commons.utility.Utility
143: .replaceAllOccurances(addr, "<", "<");
144: addr = org.claros.commons.utility.Utility
145: .replaceAllOccurances(addr, ">", ">");
146: String fullname = "";
147: String email = "";
148: int j;
149: try {
150: if ((j = addr.indexOf("<")) > 0) {
151: fullname = org.claros.commons.utility.Utility
152: .extendedTrim(addr.substring(0, j)
153: .trim(), "\"");
154: email = org.claros.commons.utility.Utility
155: .extendedTrim(
156: org.claros.commons.utility.Utility
157: .extendedTrim(
158: addr
159: .substring(j + 1),
160: ">"), "\"")
161: .trim();
162: String charset = PropertyFile.getConfiguration(
163: "/config/config.xml").getString(
164: "common-params.charset");
165: outAddr[counter] = new InternetAddress(email,
166: fullname, charset);
167: } else {
168: outAddr[counter] = new InternetAddress(addr);
169: }
170: } catch (UnsupportedEncodingException e) {
171: e.printStackTrace();
172: } catch (AddressException e) {
173: e.printStackTrace();
174: }
175: counter++;
176: }
177: return outAddr;
178: } else {
179: return null;
180: }
181: }
182:
183: /**
184: *
185: * @param size
186: * @return
187: */
188: public static String sizeToHumanReadable(long size) {
189: String out = Math.round(size / 1024) + "K";
190: if (out.equals("0K")) {
191: out = size + "B";
192: }
193: return out;
194: }
195:
196: /**
197: *
198: * @param message
199: * @return
200: */
201: public static String stripHTMLTags(String message) {
202: StringBuffer returnMessage = new StringBuffer(message);
203: try {
204: int startPosition = message.indexOf("<"); // encountered the first opening brace
205: int endPosition = message.indexOf(">"); // encountered the first closing braces
206: while (startPosition != -1) {
207: returnMessage.delete(startPosition, endPosition + 1); // remove the tag
208: returnMessage.insert(startPosition, " ");
209: startPosition = (returnMessage.toString()).indexOf("<"); // look for the next opening brace
210: endPosition = (returnMessage.toString()).indexOf(">"); // look for the next closing brace
211: }
212: } catch (Throwable e) {
213: // do nothing sier
214: }
215: return returnMessage.toString();
216: }
217: }
|