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 lucee.runtime.PageContext;
022import lucee.runtime.exp.ApplicationException;
023import lucee.runtime.exp.PageException;
024import lucee.runtime.exp.SecurityException;
025import lucee.runtime.ext.tag.TagImpl;
026import lucee.runtime.functions.other.CreateObject;
027import lucee.runtime.net.proxy.ProxyData;
028import lucee.runtime.net.proxy.ProxyDataImpl;
029import lucee.runtime.security.SecurityManager;
030
031/**
032* Lets you call methods in COM, CORBA, and JAVA objects.
033*
034*
035*
036**/
037public final class ObjectTag extends TagImpl {
038
039        /*
040         * Component
041         * - name
042         * - component
043TODO support full functonallity
044Component
045-------------
046name
047component
048
049 Com
050 ----------
051 type
052 action
053 class
054 name
055 context
056 server
057 
058 Corba
059 --------------------
060 type
061 context
062 class
063 name
064 locale
065 
066Java
067--------------------
068type
069action
070class
071name
072
073Webservice
074---------------------------
075webservice
076name
077
078all
079-------------
080name
081component
082type
083action
084class
085context
086server
087locale
088webservice
089*/
090        
091
092
093        private String name;
094        private String component;
095        private String type="";
096        private String action;
097        private String clazz;
098        private String context;
099        private String server;
100        private String locale;
101        private String webservice;
102        private String delimiters=",";
103        
104        private String username;
105        private String password;
106        private String proxyServer;
107        private int proxyPort;
108        private String proxyUser;
109        private String proxyPassword;
110
111        @Override
112        public void release()   {
113                super.release();
114                name=null;
115                component=null;
116                type="";
117                action=null;
118                clazz=null;
119                context=null;
120                server=null;
121                locale=null;
122                webservice=null;
123                delimiters=",";
124                
125
126                username=null;
127                password=null;
128                proxyServer=null;
129                proxyPort=-1;
130                proxyUser=null;
131                proxyPassword=null;
132        }
133        
134        
135
136
137        @Override
138        public int doStartTag() throws PageException    {
139            
140                if(component!=null) {
141                    pageContext.setVariable(name,CreateObject.doComponent(pageContext,component));
142                }
143                else if(type.equals("java")) {
144                    checkAccess(pageContext,type);
145                        checkClass();
146                        pageContext.setVariable(name,CreateObject.doJava(pageContext,clazz,context,delimiters));
147                }
148        else if(type.equals("com")) {
149            checkAccess(pageContext,type);
150            checkClass();
151            pageContext.setVariable(name,CreateObject.doCOM(pageContext,clazz));
152        }
153        else if(type.equals("webservice")) {
154            checkAccess(pageContext,type);
155            checkWebservice();
156            ProxyData proxy=null;
157            if(proxyServer!=null){
158                proxy=new ProxyDataImpl(proxyServer,proxyPort,proxyUser,proxyPassword);
159            }
160            pageContext.setVariable(name,CreateObject.doWebService(pageContext,webservice,username,password,proxy));
161        }
162                else {
163                        if(type==null) throw new ApplicationException("to less attributes defined for tag object");
164                        throw new ApplicationException("wrong value for attribute type", 
165                              "types are com,java,corba and the only supported type (at the moment) are com,component,java");
166                }
167                
168                return SKIP_BODY;
169        }
170        
171    private static void checkAccess(PageContext pc, String type) throws SecurityException {
172        if(pc.getConfig().getSecurityManager().getAccess(SecurityManager.TYPE_TAG_OBJECT)==SecurityManager.VALUE_NO) 
173                        throw new SecurityException("can't access tag [object] with type ["+type+"]",
174                                "access is prohibited by security manager");
175                
176    }
177
178    /**
179     * check if attribute class is defined
180     * @throws ApplicationException
181     */
182    private void checkClass() throws ApplicationException {
183        if(clazz==null)throw new ApplicationException("attribute class must be defined");
184    }
185
186    /**
187     * check if attribute webservice is defined
188     * @throws ApplicationException
189     */
190    private void checkWebservice() throws ApplicationException {
191        if(webservice==null)throw new ApplicationException("attribute webservice must be defined");
192    }
193
194
195        @Override
196        public int doEndTag()   {
197                return EVAL_PAGE;
198        }
199        /**
200         * @param locale The locale to set.
201         */
202        public void setLocale(String locale) {
203                this.locale = locale;
204        }
205        /**
206         * @param name The name to set.
207         */
208        public void setName(String name) {
209                this.name = name;
210        }
211        /**
212         * @param server The server to set.
213         */
214        public void setServer(String server) {
215                this.server = server;
216        }
217        /**
218         * @param type The type to set.
219         */
220        public void setType(String type) {
221                this.type = type.toLowerCase().trim();
222        }
223        /**
224         * @param webservice The webservice to set.
225         */
226        public void setWebservice(String webservice) {
227        this.type="webservice";
228                this.webservice = webservice;
229        }
230        /**
231         * @param action The action to set.
232         */
233        public void setAction(String action) {
234                this.action = action;
235        }
236        /**
237         * @param clazz The clazz to set.
238         */
239        public void setClass(String clazz) {
240                this.clazz = clazz;
241        }
242        /**
243         * @param component The component to set.
244         */
245        public void setComponent(String component) {
246                this.component = component;
247        }
248        /**
249         * @param context The context to set.
250         */
251        public void setContext(String context) {
252                this.context = context;
253        }
254        
255        public void setPassword(String password) {
256                this.password = password;
257        }
258        
259        public void setUsername(String username) {
260                this.username = username;
261        }
262
263        /**
264         * @param proxyServer the proxyServer to set
265         */
266        public void setProxyServer(String proxyServer) {
267                this.proxyServer = proxyServer;
268        }
269
270        /**
271         * @param proxyPort the proxyPort to set
272         */
273        public void setProxyPort(double proxyPort) {
274                this.proxyPort = (int)proxyPort;
275        }
276
277        /**
278         * @param proxyUser the proxyUser to set
279         */
280        public void setProxyUser(String proxyUser) {
281                this.proxyUser = proxyUser;
282        }
283
284        /**
285         * @param proxyPassword the proxyPassword to set
286         */
287        public void setProxyPassword(String proxyPassword) {
288                this.proxyPassword = proxyPassword;
289        }
290
291        /**
292         * @param delimiters the delimiters to set
293         */
294        public void setDelimiters(String delimiters) {
295                this.delimiters = delimiters;
296        }
297        
298}