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.config;
020
021
022import lucee.commons.lang.ExceptionUtil;
023import lucee.runtime.crypt.CFMXCompat;
024import lucee.runtime.exp.PageException;
025import lucee.runtime.functions.other.Encrypt;
026import lucee.runtime.net.proxy.ProxyData;
027import lucee.runtime.net.rpc.client.WSClient;
028import lucee.runtime.op.Caster;
029import lucee.runtime.spooler.remote.RemoteClientTask;
030import lucee.runtime.type.Struct;
031import lucee.runtime.type.StructImpl;
032import lucee.runtime.type.util.KeyConstants;
033import lucee.runtime.type.util.ListUtil;
034
035public class RemoteClientImpl implements RemoteClient {
036
037        private String url;
038        private String serverUsername;
039        private String serverPassword;
040        private ProxyData proxyData;
041        private String type;
042        private String adminPassword;
043        private String securityKey;
044        private String label;
045        private String usage;
046        private String id;
047
048        public RemoteClientImpl(String label,String type, String url, String serverUsername, String serverPassword,String adminPassword, ProxyData proxyData, String securityKey,String usage) {
049                this.label = label;
050                this.url = url;
051                this.serverUsername = serverUsername;
052                this.serverPassword = serverPassword;
053                this.proxyData = proxyData;
054                this.type = type;
055                this.adminPassword = adminPassword;
056                this.securityKey = securityKey;
057                this.usage = usage;
058        }
059
060        /**
061         * @return the url
062         */
063        public String getUrl() {
064                return url;
065        }
066
067        /**
068         * @return the serverUsername
069         */
070        public String getServerUsername() {
071                return serverUsername;
072        }
073
074        /**
075         * @return the serverPassword
076         */
077        public String getServerPassword() {
078                return serverPassword;
079        }
080
081        /**
082         * @return the proxyData
083         */
084        public ProxyData getProxyData() {
085                return proxyData;
086        }
087
088        /**
089         * @return the type
090         */
091        public String getType() {
092                return type;
093        }
094
095        /**
096         * @return the adminPassword
097         */
098        public String getAdminPassword() {
099                return adminPassword;
100        }
101
102        /**
103         * @return the securityKey
104         */
105        public String getSecurityKey() {
106                return securityKey;
107        }
108
109        public String getAdminPasswordEncrypted() {
110                try {
111                        return Encrypt.invoke( getAdminPassword(), getSecurityKey(), CFMXCompat.ALGORITHM_NAME, "uu", null, 0 );
112                } 
113                catch (PageException e) {
114                        return null;
115                }
116        }
117
118        public String getLabel() {
119                return label;
120        }
121
122        public String getUsage() {
123                return usage;
124        }
125
126        public boolean hasUsage(String usage) {
127                return ListUtil.listFindNoCaseIgnoreEmpty(this.usage,usage,',')!=-1 ;
128        }
129
130        public String getId(Config config) {
131
132                if(id!=null) return id;
133                
134                Struct attrColl = new StructImpl();
135                attrColl.setEL(KeyConstants._action, "getToken");
136                
137                Struct args = new StructImpl();
138                args.setEL(KeyConstants._type, getType());
139                args.setEL(RemoteClientTask.PASSWORD, getAdminPasswordEncrypted());
140                args.setEL(RemoteClientTask.CALLER_ID, "undefined");
141                args.setEL(RemoteClientTask.ATTRIBUTE_COLLECTION, attrColl);
142                
143                
144                
145                try {
146                        WSClient rpc = 
147                                WSClient.getInstance(null,getUrl(),getServerUsername(),getServerPassword(),getProxyData());
148                        
149                        Object result = rpc.callWithNamedValues(config, KeyConstants._invoke, args);
150                        return id=ConfigImpl.getId(securityKey, Caster.toString(result,null),false, null);
151                        
152                } 
153                catch (Throwable t) {
154                ExceptionUtil.rethrowIfNecessary(t);
155                t.printStackTrace();
156                        return null;
157                }
158        }
159        
160
161}