001    package railo.runtime.tag;
002    
003    
004    import java.io.IOException;
005    
006    import javax.servlet.jsp.tagext.Tag;
007    
008    import org.apache.commons.mail.EmailAttachment;
009    
010    import railo.commons.io.IOUtil;
011    import railo.commons.io.SystemUtil;
012    import railo.commons.io.res.Resource;
013    import railo.commons.io.res.util.ResourceUtil;
014    import railo.commons.lang.StringUtil;
015    import railo.runtime.exp.ApplicationException;
016    import railo.runtime.exp.ExpressionException;
017    import railo.runtime.exp.PageException;
018    import railo.runtime.ext.tag.TagImpl;
019    import railo.runtime.op.Caster;
020    import railo.runtime.type.util.ListUtil;
021    /**
022    * Can either attach a file or add a header to a message. It is nested within a cfmail tag. You can 
023    *   use more than one cfmailparam tag within a cfmail tag.
024    *
025    *
026    *
027    **/
028    public final class MailParam extends TagImpl {
029    
030            /** Indicates the value of the header. */
031            private String value="";
032    
033            /** Attaches the specified file to the message. This attribute is mutually exclusive with the 
034            **              name attribute. */
035            private String file;
036    
037            /** Specifies the name of the header. Header names are case insensitive. This attribute is mutually 
038            **              exclusive with the file attribute. */
039            private String name;
040            
041            private String type="";
042        private String disposition=null;
043        private String contentID=null;
044        private Boolean remove=false;
045        private byte[] content=null;
046    
047            @Override
048            public void release()   {
049                    super.release();
050                    value="";
051                    file=null;
052                    name=null;
053                    type="";
054            disposition=null;
055            contentID=null;
056            remove=null;
057            content=null;
058            }
059            
060            /**
061             * @param remove the remove to set
062             */
063            public void setRemove(boolean remove) {
064                    this.remove = Caster.toBoolean(remove);
065            }
066    
067    
068            /**
069             * @param content the content to set
070             * @throws ExpressionException 
071             */
072            public void setContent(Object content) throws PageException {
073                    if(content instanceof String)this.content = ((String)content).getBytes();
074                    else this.content = Caster.toBinary(content);
075            }
076    
077            /**
078             * @param type
079             */
080            public void setType(String type)        {
081                    type=type.toLowerCase().trim();
082                    
083                    if(type.equals("text"))type="text/plain";
084                    else if(type.equals("plain"))type="text/plain";
085                    else if(type.equals("html"))type="text/html";
086                    
087                    this.type=type;
088            }
089    
090            /** set the value value
091            *  Indicates the value of the header.
092            * @param value value to set
093            **/
094            public void setValue(String value)      {
095                    this.value=value;
096            }
097    
098            /** set the value file
099            *  Attaches the specified file to the message. This attribute is mutually exclusive with the 
100            *               name attribute.
101            * @param strFile value to set
102             * @throws PageException 
103            **/
104            public void setFile(String strFile) throws PageException        {
105                    this.file=strFile;
106            }
107    
108            /** set the value name
109            *  Specifies the name of the header. Header names are case insensitive. This attribute is mutually 
110            *               exclusive with the file attribute.
111            * @param name value to set
112            **/
113            public void setName(String name)        {
114                    this.name=name;
115            }
116    
117        /**
118         * @param disposition The disposition to set.
119         * @throws ApplicationException 
120         */
121        public void setDisposition(String disposition) throws ApplicationException {
122            disposition=disposition.trim().toLowerCase();
123            if(disposition.equals("attachment")) this.disposition=EmailAttachment.ATTACHMENT;
124            else if(disposition.equals("inline"))this.disposition=EmailAttachment.INLINE;
125            else 
126            throw new ApplicationException("disposition must have one of the following values (attachment,inline)");
127            
128        }
129        /**
130         * @param contentID The contentID to set.
131         */
132        public void setContentid(String contentID) {
133            this.contentID = contentID;
134        }
135    
136    
137            @Override
138            public int doStartTag() throws PageException    {
139                    
140                    if(content!=null){
141                            required("mailparam", "file", file);
142                            String filename = ListUtil.last(file, "/\\",true);
143                            Resource res = SystemUtil.getTempDirectory().getRealResource(filename);
144                            if(res.exists())ResourceUtil.removeEL(res, true);
145                            try {
146                                    IOUtil.write(res, content);
147                            } catch (IOException e) {
148                                    throw Caster.toPageException(e);
149                            } 
150                            this.file=ResourceUtil.getCanonicalPathEL(res);
151                            remove=true;
152                    }
153                    else if(!StringUtil.isEmpty(this.file)) {
154                            Resource res=ResourceUtil.toResourceNotExisting(pageContext,this.file);
155                    if(res!=null) {
156                        if(res.exists())pageContext.getConfig().getSecurityManager().checkFileLocation(res);
157                        this.file=ResourceUtil.getCanonicalPathEL(res);
158                    } 
159                    }
160                    
161                    
162                    
163                    
164                    // check attributes
165                    boolean hasFile=!StringUtil.isEmpty(file);
166                    boolean hasName=!StringUtil.isEmpty(name);
167                    // both attributes
168                    if(hasName && hasFile) {
169                            throw new ApplicationException("Wrong Context for tag MailParam, you cannot use attribute file and name together");
170                    }
171                    // no attributes
172                    if(!hasName && !hasFile) {
173                            throw new ApplicationException("Wrong Context for tag MailParam, you must use attribute file or attribute name for this tag");
174                    }
175                    
176                    // get Mail Tag
177                    Tag parent=getParent();
178                    while(parent!=null && !(parent instanceof Mail)) {
179                            parent=parent.getParent();
180                    }
181                    
182                    if(parent instanceof Mail) {
183                            Mail mail = (Mail)parent;
184                            mail.setParam(type,file,name,value,disposition,contentID,remove);
185                    }
186                    else {
187                            throw new ApplicationException("Wrong Context, tag MailParam must be inside a Mail tag");       
188                    }
189                    return SKIP_BODY;
190            }
191    
192            @Override
193            public int doEndTag()   {
194                    return EVAL_PAGE;
195            }
196    
197    }