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.List;
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            /**
048            * @see javax.servlet.jsp.tagext.Tag#release()
049            */
050            public void release()   {
051                    super.release();
052                    value="";
053                    file=null;
054                    name=null;
055                    type="";
056            disposition=null;
057            contentID=null;
058            remove=null;
059            content=null;
060            }
061            
062            /**
063             * @param remove the remove to set
064             */
065            public void setRemove(boolean remove) {
066                    this.remove = Caster.toBoolean(remove);
067            }
068    
069    
070            /**
071             * @param content the content to set
072             * @throws ExpressionException 
073             */
074            public void setContent(Object content) throws PageException {
075                    if(content instanceof String)this.content = ((String)content).getBytes();
076                    else this.content = Caster.toBinary(content);
077            }
078    
079            /**
080             * @param type
081             */
082            public void setType(String type)        {
083                    type=type.toLowerCase().trim();
084                    
085                    if(type.equals("text"))type="text/plain";
086                    else if(type.equals("plain"))type="text/plain";
087                    else if(type.equals("html"))type="text/html";
088                    
089                    this.type=type;
090            }
091    
092            /** set the value value
093            *  Indicates the value of the header.
094            * @param value value to set
095            **/
096            public void setValue(String value)      {
097                    this.value=value;
098            }
099    
100            /** set the value file
101            *  Attaches the specified file to the message. This attribute is mutually exclusive with the 
102            *               name attribute.
103            * @param strFile value to set
104             * @throws PageException 
105            **/
106            public void setFile(String strFile) throws PageException        {
107                    this.file=strFile;
108            }
109    
110            /** set the value name
111            *  Specifies the name of the header. Header names are case insensitive. This attribute is mutually 
112            *               exclusive with the file attribute.
113            * @param name value to set
114            **/
115            public void setName(String name)        {
116                    this.name=name;
117            }
118    
119        /**
120         * @param disposition The disposition to set.
121         * @throws ApplicationException 
122         */
123        public void setDisposition(String disposition) throws ApplicationException {
124            disposition=disposition.trim().toLowerCase();
125            if(disposition.equals("attachment")) this.disposition=EmailAttachment.ATTACHMENT;
126            else if(disposition.equals("inline"))this.disposition=EmailAttachment.INLINE;
127            else 
128            throw new ApplicationException("disposition must have one of the following values (attachment,inline)");
129            
130        }
131        /**
132         * @param contentID The contentID to set.
133         */
134        public void setContentid(String contentID) {
135            this.contentID = contentID;
136        }
137    
138    
139            /**
140            * @throws PageException 
141             * @see javax.servlet.jsp.tagext.Tag#doStartTag()
142            */
143            public int doStartTag() throws PageException    {
144                    
145                    if(content!=null){
146                            required("mailparam", "file", file);
147                            String filename = List.last(file, "/\\",true);
148                            Resource res = SystemUtil.getTempDirectory().getRealResource(filename);
149                            if(res.exists())ResourceUtil.removeEL(res, true);
150                            try {
151                                    IOUtil.write(res, content);
152                            } catch (IOException e) {
153                                    throw Caster.toPageException(e);
154                            } 
155                            this.file=ResourceUtil.getCanonicalPathEL(res);
156                            remove=true;
157                    }
158                    else if(!StringUtil.isEmpty(this.file)) {
159                            Resource res=ResourceUtil.toResourceNotExisting(pageContext,this.file);
160                    if(res!=null) {
161                        if(res.exists())pageContext.getConfig().getSecurityManager().checkFileLocation(res);
162                        this.file=ResourceUtil.getCanonicalPathEL(res);
163                    } 
164                    }
165                    
166                    
167                    
168                    
169                    // check attributes
170                    boolean hasFile=!StringUtil.isEmpty(file);
171                    boolean hasName=!StringUtil.isEmpty(name);
172                    // both attributes
173                    if(hasName && hasFile) {
174                            throw new ApplicationException("Wrong Context for tag MailParam, you cannot use attribute file and name together");
175                    }
176                    // no attributes
177                    if(!hasName && !hasFile) {
178                            throw new ApplicationException("Wrong Context for tag MailParam, you must use attribute file or attribute name for this tag");
179                    }
180                    
181                    // get Mail Tag
182                    Tag parent=getParent();
183                    while(parent!=null && !(parent instanceof Mail)) {
184                            parent=parent.getParent();
185                    }
186                    
187                    if(parent instanceof Mail) {
188                            Mail mail = (Mail)parent;
189                            mail.setParam(type,file,name,value,disposition,contentID,remove);
190                    }
191                    else {
192                            throw new ApplicationException("Wrong Context, tag MailParam must be inside a Mail tag");       
193                    }
194                    return SKIP_BODY;
195            }
196    
197            /**
198            * @see javax.servlet.jsp.tagext.Tag#doEndTag()
199            */
200            public int doEndTag()   {
201                    return EVAL_PAGE;
202            }
203    
204    }