001/** 002 * 003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 017 * 018 **/ 019package lucee.runtime.net.mail; 020 021import java.io.Serializable; 022 023import lucee.runtime.op.Caster; 024import lucee.runtime.type.Array; 025import lucee.runtime.type.util.ListUtil; 026 027 028/** 029 * 030 */ 031public final class MailPart implements Serializable { 032 @Override 033 public String toString() { 034 return "lucee.runtime.mail.MailPart(wraptext:"+wraptext+";type:"+type+";charset:"+charset+";body:"+body+";)"; 035 } 036 /** IThe MIME media type of the part */ 037 private boolean isHTML; 038 039 /** Specifies the maximum line length, in characters of the mail text */ 040 private int wraptext=-1; 041 042 /** The character encoding in which the part text is encoded */ 043 private String charset; 044 045 private String body; 046 private String type; 047 048 /** 049 * 050 */ 051 public void clear() { 052 isHTML=false; 053 type=null; 054 wraptext=-1; 055 charset=null; 056 body="null"; 057 } 058 059 060 061 /** 062 * 063 */ 064 public MailPart() { 065 } 066 067 /** 068 * @param charset 069 */ 070 public MailPart(String charset) { 071 this.charset = charset; 072 } 073 /** 074 * @return Returns the body. 075 */ 076 public String getBody() { 077 return wrap(body); 078 } 079 /** 080 * @param body The body to set. 081 */ 082 public void setBody(String body) { 083 this.body = body; 084 } 085 /** 086 * @return Returns the charset. 087 */ 088 public String getCharset() { 089 return charset; 090 } 091 /** 092 * @param charset The charset to set. 093 */ 094 public void setCharset(String charset) { 095 this.charset = charset; 096 } 097 /** 098 * @return Returns the isHTML. 099 */ 100 public boolean isHTML() { 101 return isHTML; 102 } 103 /** 104 * @param isHTML The type to set. 105 */ 106 public void isHTML(boolean isHTML) { 107 this.isHTML = isHTML; 108 } 109 /** 110 * @return Returns the wraptext. 111 */ 112 public int getWraptext() { 113 return wraptext; 114 } 115 116 117 /** 118 * @return the type 119 */ 120 public String getType() { 121 return type; 122 } 123 124 125 126 /** 127 * @param type the type to set 128 */ 129 public void setType(String type) { 130 this.type = type; 131 } 132 133 134 135 /** 136 * @param wraptext The wraptext to set. 137 */ 138 public void setWraptext(int wraptext) { 139 this.wraptext = wraptext; 140 } 141 142 143 /** 144 * wraps a String to specified length 145 * @param str string to erap 146 * @return wraped String 147 */ 148 private String wrap(String str) { 149 if(body==null || wraptext<=0)return str; 150 151 StringBuffer rtn=new StringBuffer(); 152 String ls=System.getProperty("line.separator"); 153 Array arr = ListUtil.listToArray(str,ls); 154 int len=arr.size(); 155 156 for(int i=1;i<=len;i++) { 157 rtn.append(wrapLine(Caster.toString(arr.get(i,""),""))); 158 if(i+1<len)rtn.append(ls); 159 } 160 return rtn.toString(); 161 } 162 163 /** 164 * wrap a single line 165 * @param str 166 * @return wraped Line 167 */ 168 private String wrapLine(String str) { 169 int wtl=wraptext; 170 171 if(str.length()<=wtl) return str; 172 173 String sub=str.substring(0,wtl); 174 String rest=str.substring(wtl); 175 char firstR=rest.charAt(0); 176 String ls=System.getProperty("line.separator"); 177 178 if(firstR==' ' || firstR=='\t') return sub+ls+wrapLine(rest.length()>1?rest.substring(1):""); 179 180 181 int indexSpace = sub.lastIndexOf(' '); 182 int indexTab = sub.lastIndexOf('\t'); 183 int index=indexSpace<=indexTab?indexTab:indexSpace; 184 185 if(index==-1) return sub+ls+wrapLine(rest); 186 return sub.substring(0,index) + ls + wrapLine(sub.substring(index+1)+rest); 187 188 } 189}