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.cache.eh.remote.soap;
020
021import java.net.MalformedURLException;
022import java.net.URL;
023import java.rmi.RemoteException;
024import java.util.ArrayList;
025import java.util.Date;
026import java.util.List;
027
028import javax.xml.namespace.QName;
029import javax.xml.rpc.ParameterMode;
030import javax.xml.rpc.ServiceException;
031import javax.xml.rpc.encoding.TypeMapping;
032
033import lucee.commons.io.cache.CacheEntry;
034import lucee.loader.engine.CFMLEngineFactory;
035import lucee.runtime.cache.eh.remote.Converter;
036import lucee.runtime.cache.eh.remote.rest.RESTClient;
037import lucee.runtime.net.rpc.TypeMappingUtil;
038import lucee.runtime.util.Cast;
039
040import org.apache.axis.Constants;
041import org.apache.axis.client.Call;
042import org.apache.axis.client.Service;
043import org.apache.axis.encoding.ser.BeanDeserializerFactory;
044import org.apache.axis.encoding.ser.BeanSerializerFactory;
045
046public class SoapClient {
047        //private static QName bool2 = Constants.XSD_BOOLEAN;//new QName("http://www.w3.org/2001/XMLSchema", "boolean");
048        private static QName string2 = Constants.XSD_STRING;//new QName("http://www.w3.org/2001/XMLSchema", "string");
049        
050        private static QName element = new QName("http://soap.server.ehcache.sf.net/", "element");
051        private static QName cache = new QName("http://soap.server.ehcache.sf.net/", "cache");
052        private static QName cacheConfiguration = new QName("http://soap.server.ehcache.sf.net/", "cacheConfiguration");
053        
054        
055        private String endpoint;
056
057        public SoapClient(URL endpoint) {
058                this.endpoint=endpoint.toExternalForm();
059        }
060
061        public static void main(String [] args) throws Exception {
062                 RESTClient.main(null);
063                 
064                 SoapClient client = new SoapClient(new URL("http://localhost:8181/soap/EhcacheWebServiceEndpoint?wsdl"));
065                 
066                 Element e = new Element();
067                 e.setEternal(Boolean.TRUE);
068                 e.setExpirationDate(new Long(new Date().getTime()+1000000));
069                 e.setKey("lami");
070                 e.setMimeType("application/x-java-serialized-object");
071                 e.setValue(Converter.toBytes("Lama"));
072                 e.setTimeToIdleSeconds(new Integer(10000));
073                 e.setTimeToLiveSeconds(new Integer(10000));
074                 //e.setResourceUri(resourceUri);
075                 
076                 client.put("susi", e);
077                 client.putQuiet("susi", e);
078                 
079         }
080        
081        public Cache getCache(String cacheName) throws Exception {
082                
083       Service  service = new Service();
084       TypeMapping tm = service.getTypeMappingRegistry().getDefaultTypeMapping();
085       TypeMappingUtil.registerBeanTypeMapping(tm, CacheConfiguration.class, cacheConfiguration);
086       TypeMappingUtil.registerBeanTypeMapping(tm, Cache.class, cache);
087       
088       
089      Call     call    = (Call) service.createCall();
090       
091       
092       call.registerTypeMapping(
093               Cache.class, 
094               cache,
095               BeanSerializerFactory.class,
096               BeanDeserializerFactory.class);
097       
098       
099        
100       call.registerTypeMapping(
101               CacheConfiguration.class, 
102               cacheConfiguration,
103               BeanSerializerFactory.class,
104               BeanDeserializerFactory.class);
105       
106       call.setTargetEndpointAddress( new java.net.URL(endpoint) );
107       call.setOperationName(new QName("http://soap.server.ehcache.sf.net/", "getCache"));
108
109       call.addParameter("arg0", Constants.XSD_STRING, String.class, ParameterMode.IN);
110       call.setReturnClass(Element.class);
111       call.setReturnQName(cache);
112       
113       
114       
115       //Object ret =  call.invoke( new Object[] { } );
116       return (Cache) call.invoke( new Object[] {cacheName } );
117   }
118        
119        public List getKeysWithExpiryCheck(String cacheName) throws MalformedURLException, RemoteException, ServiceException {
120                Object raw=getKeysWithExpiryCheckRaw(cacheName);
121                if(raw==null) return new ArrayList();
122                else if(raw instanceof String) {
123                        List list=new ArrayList();
124                        list.add(raw);
125                        return list;
126                }
127                else if(raw instanceof List) {
128                        return (List) raw;
129                }
130                
131                Cast caster = CFMLEngineFactory.getInstance().getCastUtil();
132                return caster.toList(raw,null);
133        }
134        
135        private Object getKeysWithExpiryCheckRaw(String cacheName) throws MalformedURLException, RemoteException, ServiceException {
136                Service  service = new Service();
137                Call     call    = (Call) service.createCall();
138                QName any = new QName("http://www.w3.org/2001/XMLSchema", "anyType[0,unbounded]");
139                QName string = new QName("http://www.w3.org/2001/XMLSchema", "string");
140       
141       
142       
143       call.setTargetEndpointAddress( new java.net.URL(endpoint) );
144       call.setOperationName(new QName("http://soap.server.ehcache.sf.net/", "getKeysWithExpiryCheck"));
145
146       call.addParameter("arg0", string, String.class, ParameterMode.IN);
147       call.setReturnType(any);
148       return call.invoke( new Object[] {cacheName } );
149   }
150        
151
152        public CacheEntry get(String cacheName,String key) throws MalformedURLException, RemoteException, ServiceException {
153                return _get(cacheName, "get", key);
154        }
155        
156        public CacheEntry getQuiet(String cacheName,String key) throws MalformedURLException, RemoteException, ServiceException {
157                return _get(cacheName, "getQuiet", key);
158        }
159        
160        
161        private CacheEntry _get(String cacheName,String method,String key) throws ServiceException, MalformedURLException, RemoteException  {
162        Service  service = new Service();
163        Call     call    = (Call) service.createCall();
164        
165        call.registerTypeMapping(
166                Element.class, 
167                element,
168                BeanSerializerFactory.class,
169                BeanDeserializerFactory.class);
170        
171        call.setTargetEndpointAddress( new java.net.URL(endpoint) );
172        call.setOperationName(new QName("http://soap.server.ehcache.sf.net/", method));
173
174        call.addParameter("arg0", Constants.XSD_STRING, String.class, ParameterMode.IN);
175        call.addParameter("arg1", Constants.XSD_STRING, String.class, ParameterMode.IN);
176        call.setReturnClass(Element.class);
177        call.setReturnQName(element);
178        
179        return new SoapCacheEntry((Element) call.invoke( new Object[] {cacheName,key } ));
180    }
181        
182        public boolean remove(String cacheName,String key) throws MalformedURLException, RemoteException, ServiceException {
183                return _remove(cacheName, "remove", key);
184        }
185        
186        public boolean removeQuiet(String cacheName,String key) throws MalformedURLException, RemoteException, ServiceException {
187                return _remove(cacheName, "removeQuiet", key);
188        }
189        
190        public int clear(String name) {
191                // MUST implement
192                throw new RuntimeException("not supported!");
193        }
194        
195        
196        private boolean _remove(String cacheName,String method,String key) throws ServiceException, MalformedURLException, RemoteException  {
197        Service  service = new Service();
198        Call     call    = (Call) service.createCall();
199        
200        
201        call.registerTypeMapping(
202                Element.class, 
203                element,
204                BeanSerializerFactory.class,
205                BeanDeserializerFactory.class);
206        
207        call.setTargetEndpointAddress( new java.net.URL(endpoint) );
208        call.setOperationName(new QName("http://soap.server.ehcache.sf.net/", method));
209
210        call.addParameter("arg0", Constants.XSD_STRING, String.class, ParameterMode.IN);
211        call.addParameter("arg1", Constants.XSD_STRING, String.class, ParameterMode.IN);
212        call.setReturnClass(boolean.class);
213        call.setReturnQName(Constants.XSD_BOOLEAN);
214      
215        return ((Boolean)call.invoke( new Object[] {cacheName,key } )).booleanValue();
216
217    }
218        
219
220        
221        public void put(String cacheName,Element element) throws MalformedURLException, RemoteException, ServiceException {
222                _put(cacheName, "put", element);
223        }
224        
225        public void putQuiet(String cacheName,Element element) throws MalformedURLException, RemoteException, ServiceException {
226                _put(cacheName, "putQuiet", element);
227        }
228        
229        
230        private void _put(String cacheName,String method,Element el) throws ServiceException, MalformedURLException, RemoteException  {
231        Service  service = new Service();
232        Call     call    = (Call) service.createCall();
233        
234        el.setResourceUri(endpoint);
235        
236        call.registerTypeMapping(
237                Element.class, 
238                element,
239                BeanSerializerFactory.class,
240                BeanDeserializerFactory.class);
241        
242        call.setTargetEndpointAddress( new java.net.URL(endpoint) );
243        call.setOperationName(new QName("http://soap.server.ehcache.sf.net/", method));
244        
245        
246        call.addParameter("arg0", Constants.XSD_STRING, String.class, ParameterMode.IN);
247        call.addParameter("arg1", element, Element.class, ParameterMode.IN);
248        call.setReturnType(Constants.XSD_ANYSIMPLETYPE);
249        
250        call.invoke( new Object[] {cacheName,el } );
251        //call.invokeOneWay(new Object[] {cacheName,el } );
252        }
253        
254}