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