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.tag;
020
021import javax.servlet.jsp.tagext.Tag;
022
023import lucee.commons.io.res.util.ResourceUtil;
024import lucee.runtime.exp.ApplicationException;
025import lucee.runtime.ext.tag.TagImpl;
026
027/**
028* Required for cfhttp POST operations, cfhttpparam is used to specify the parameters necessary to 
029*        build a cfhttp POST.
030*
031*
032*
033**/
034public final class HttpParam extends TagImpl {
035        
036        HttpParamBean param=new HttpParamBean();
037
038    /**
039     * Applies to FormField and CGI types; ignored for all other types. 
040     * Specifies whether to URLEncode the form field or header.
041     * @param encoded
042     */
043    public void setEncoded(boolean encoded) {
044        param.setEncoded(encoded);
045    }
046
047    /**
048     * Applies to File type; invalid for all other types. 
049     * Specifies the MIME media type of the file contents. 
050     * The content type can include an identifier for the character encoding of the file; 
051     * for example, text/html; charset=ISO-8859-1 indicates that the file is HTML text in 
052     * the ISO Latin-1 character encoding.
053     * @param mimetype
054     */
055    public void setMimetype(String mimetype) {
056        param.setMimeType(mimetype);
057    }
058    
059    
060        /** set the value value
061        *  Specifies the value of the URL, FormField, Cookie, File, or CGI variable being passed.
062        * @param value value to set
063        **/
064        public void setValue(Object value)      {
065                param.setValue(value);
066        }
067
068        /** set the value type
069        *  The transaction type.
070        * @param type value to set
071        **/
072        public void setType(String type)        {
073                param.setType(type);
074        }
075
076        /** set the value file
077        *  Required for type = "File".
078        * @param file value to set
079        **/
080        public void setFile(String file)        {
081                param.setFile(ResourceUtil.toResourceNotExisting(pageContext,file));
082        }
083
084        /** set the value name
085        *  A variable name for the data being passed.
086        * @param name value to set
087        **/
088        public void setName(String name)        {
089                param.setName(name);
090        }
091
092
093        @Override
094        public int doStartTag() throws ApplicationException     {
095        if(param.getName()==null &&
096                (!"body".equalsIgnoreCase(param.getType()) &&
097                !"xml".equalsIgnoreCase(param.getType()))) {
098            throw new ApplicationException("attribute [name] is required for tag [httpparam] if type is not [body or xml]");
099        }
100        
101                // get HTTP Tag
102                Tag parent=getParent();
103                while(parent!=null && !(parent instanceof Http)) {
104                        parent=parent.getParent();
105                }
106                
107                if(parent instanceof Http) {
108                        Http http = (Http)parent;
109                        http.setParam(param);
110                }
111                else {
112                        throw new ApplicationException("Wrong Context, tag HttpParam must be inside a Http tag");       
113                }
114                return SKIP_BODY;
115        }
116
117        @Override
118        public int doEndTag()   {
119                return EVAL_PAGE;
120        }
121
122        @Override
123        public void release()   {
124                super.release();
125                param=new HttpParamBean();
126        }
127}