001 package railo.runtime.tag; 002 003 import railo.commons.io.res.Resource; 004 import railo.runtime.exp.ApplicationException; 005 import railo.runtime.exp.PageException; 006 import railo.runtime.ext.tag.TagImpl; 007 import railo.runtime.net.mail.MailClient; 008 import railo.runtime.op.Caster; 009 import railo.runtime.type.util.ArrayUtil; 010 import railo.runtime.type.util.ListUtil; 011 012 /** 013 * Retrieves and deletes e-mail messages from a POP mail server. 014 */ 015 public abstract class _Mail extends TagImpl { 016 017 private String server; 018 private int port=-1; 019 020 private String username; 021 private String password; 022 private String action="getheaderonly"; 023 private String name; 024 private String[] messageNumber; 025 private String[] uid; 026 private Resource attachmentPath; 027 private int timeout=60; 028 private int startrow=1; 029 private int maxrows=-1; 030 private boolean generateUniqueFilenames=false; 031 //private boolean debug=false; 032 033 @Override 034 public void release() { 035 port=-1; 036 username=null; 037 password=null; 038 action="getheaderonly"; 039 name=null; 040 messageNumber=null; 041 uid=null; 042 attachmentPath=null; 043 timeout=60; 044 startrow=1; 045 maxrows=-1; 046 generateUniqueFilenames=false; 047 //debug=false; 048 super.release(); 049 050 } 051 052 /** 053 * @param server The server to set. 054 */ 055 public void setServer(String server) { 056 this.server = server; 057 } 058 059 /** 060 * @param port The port to set. 061 */ 062 public void setPort(double port) { 063 this.port = (int)port; 064 } 065 066 /** 067 * @param username The username to set. 068 */ 069 public void setUsername(String username) { 070 this.username = username; 071 } 072 073 /** 074 * @param password The password to set. 075 */ 076 public void setPassword(String password) { 077 this.password = password; 078 } 079 080 /** 081 * @param action The action to set. 082 */ 083 public void setAction(String action) { 084 this.action = action.trim().toLowerCase(); 085 } 086 087 /** 088 * @param name The name to set. 089 */ 090 public void setName(String name) { 091 this.name = name; 092 } 093 094 /** 095 * @param messageNumber The messageNumber to set. 096 * @throws PageException 097 */ 098 public void setMessagenumber(String messageNumber) throws PageException { 099 this.messageNumber = ArrayUtil.trim(ListUtil.toStringArray(ListUtil.listToArrayRemoveEmpty(messageNumber,','))); 100 if(this.messageNumber.length==0)this.messageNumber=null; 101 } 102 103 /** 104 * @param uid The uid to set. 105 * @throws PageException 106 */ 107 public void setUid(String uid) throws PageException { 108 this.uid = ArrayUtil.trim(ListUtil.toStringArray(ListUtil.listToArrayRemoveEmpty(uid,','))); 109 if(this.uid.length==0)this.uid=null; 110 } 111 112 /** 113 * @param attachmentPath The attachmentPath to set. 114 * @throws PageException 115 */ 116 public void setAttachmentpath(String attachmentPath) throws PageException { 117 //try { 118 Resource attachmentDir=pageContext.getConfig().getResource(attachmentPath); 119 if(!attachmentDir.exists() && !attachmentDir.mkdir()) { 120 attachmentDir=pageContext.getConfig().getTempDirectory().getRealResource(attachmentPath); 121 if(!attachmentDir.exists() && !attachmentDir.mkdir()) 122 throw new ApplicationException("directory ["+attachmentPath+"] doesent exist and can't created"); 123 } 124 if(!attachmentDir.isDirectory())throw new ApplicationException("file ["+attachmentPath+"] is not a directory"); 125 pageContext.getConfig().getSecurityManager().checkFileLocation(attachmentDir); 126 this.attachmentPath = attachmentDir; 127 /*} 128 catch(IOException ioe) { 129 throw Caster.toPageException(ioe); 130 }*/ 131 } 132 133 /** 134 * @param maxrows The maxrows to set. 135 */ 136 public void setMaxrows(double maxrows) { 137 this.maxrows = (int)maxrows; 138 } 139 140 /** 141 * @param startrow The startrow to set. 142 */ 143 public void setStartrow(double startrow) { 144 this.startrow = (int)startrow; 145 } 146 147 /** 148 * @param timeout The timeout to set. 149 */ 150 public void setTimeout(double timeout) { 151 this.timeout = (int)timeout; 152 } 153 154 /** 155 * @param generateUniqueFilenames The generateUniqueFilenames to set. 156 */ 157 public void setGenerateuniquefilenames(boolean generateUniqueFilenames) { 158 this.generateUniqueFilenames = generateUniqueFilenames; 159 } 160 161 /** 162 * @param debug The debug to set. 163 */ 164 public void setDebug(boolean debug) { 165 //this.debug = debug; 166 } 167 168 @Override 169 public int doStartTag() throws PageException { 170 171 // check attrs 172 if(port==-1)port=getDefaultPort(); 173 174 //PopClient client = new PopClient(server,port,username,password); 175 MailClient client = MailClient.getInstance(getType(),server,port,username,password); 176 client.setTimeout(timeout*1000); 177 client.setMaxrows(maxrows); 178 if(startrow>1)client.setStartrow(startrow-1); 179 client.setUniqueFilenames(generateUniqueFilenames); 180 if(attachmentPath!=null)client.setAttachmentDirectory(attachmentPath); 181 182 if(uid!=null)messageNumber=null; 183 184 try { 185 client.connect(); 186 187 if(action.equals("getheaderonly")) { 188 required(getTagName(),action,"name",name); 189 pageContext.setVariable(name,client.getMails(messageNumber,uid,false)); 190 } 191 else if(action.equals("getall")) { 192 required(getTagName(),action,"name",name); 193 pageContext.setVariable(name,client.getMails(messageNumber,uid,true)); 194 } 195 else if(action.equals("delete")) { 196 client.deleteMails(messageNumber,uid); 197 } 198 else throw new ApplicationException("invalid value for attribute action, valid values are [getHeaderOnly,getAll,delete]"); 199 } 200 catch(Exception e) { 201 throw Caster.toPageException(e); 202 } 203 finally{ 204 client.disconnectEL(); 205 } 206 return SKIP_BODY; 207 } 208 209 protected abstract int getType(); 210 protected abstract int getDefaultPort(); 211 protected abstract String getTagName(); 212 }