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.rest;
020
021import java.io.IOException;
022import java.io.InputStream;
023import java.io.ObjectOutputStream;
024import java.io.OutputStream;
025import java.net.HttpURLConnection;
026import java.net.MalformedURLException;
027import java.net.URL;
028import java.util.Date;
029
030import lucee.commons.io.cache.CacheEntry;
031import lucee.loader.util.Util;
032import lucee.runtime.cache.eh.remote.Converter;
033import lucee.runtime.cache.eh.remote.rest.sax.CacheFactory;
034import lucee.runtime.cache.eh.remote.rest.sax.CacheMeta;
035
036import org.xml.sax.SAXException;
037
038public class RESTClient {
039        
040        private URL url;
041        private String strUrl;
042        
043        public RESTClient(URL url) {
044                this.url=url;
045                this.strUrl=url.toExternalForm();
046                if(!strUrl.endsWith("/")){
047                        strUrl+="/";
048                        try {
049                                url=new URL(strUrl);
050                        } catch (MalformedURLException e) {}
051                }
052                
053        }
054        
055        public Object getMetaRaw(String cacheName) throws IOException {
056                HttpURLConnection connection = (HttpURLConnection) toURL(cacheName).openConnection();
057            connection.setRequestMethod("GET");
058            connection.setRequestProperty("id", "getKeysWithExpiryCheck");
059            connection.connect();
060            try {
061                return getContent(connection);
062                } 
063        finally {
064                disconnectEL(connection);
065        }
066        }       
067        
068        public CacheMeta getMeta(String cacheName) throws IOException, SAXException {
069                HttpURLConnection connection = (HttpURLConnection) toURL(cacheName).openConnection();
070            connection.setRequestMethod("GET");
071            connection.setRequestProperty("id", "getKeysWithExpiryCheck");
072            connection.connect();
073            InputStream is=null;
074            try {
075                is=connection.getInputStream();
076                return new CacheFactory(is).getMeta();
077                } 
078        finally {
079                Util.closeEL(is);
080                disconnectEL(connection);
081        }
082        }       
083        
084        public CacheEntry getMeta2(String cacheName) throws IOException {
085                HttpURLConnection connection = (HttpURLConnection) toURL(cacheName).openConnection();
086            connection.setRequestMethod("HEAD");
087            connection.connect();
088            InputStream is=null;
089            try {
090                //is=connection.getInputStream();
091                //obj=getContent(connection);
092                //CacheFactory cf = new CacheFactory(is);
093                
094                } 
095        finally {
096                Util.closeEL(is);
097                disconnectEL(connection);
098        }
099        return null;
100        }       
101
102        /*private Object listCachesRaw() throws IOException {
103                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
104                Object obj;
105        conn.setRequestMethod("GET");
106        try{
107                obj = getContent(conn);
108        }
109        finally{
110                disconnectEL(conn);
111        }
112        return obj;
113        }*/
114        
115        /*private Object featuresRaw() throws IOException {
116                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
117                Object obj;
118        conn.setRequestMethod("OPTIONS");
119        try{
120                obj = getContent(conn);
121        }
122        finally{
123                disconnectEL(conn);
124        }
125        return obj;
126        }*/
127        
128        /*private void createCache(String cacheName) throws IOException {
129                HttpURLConnection urlConnection = (HttpURLConnection) toURL(cacheName).openConnection();
130        urlConnection.setRequestMethod("PUT");
131
132        urlConnection.getResponseCode();
133        urlConnection.disconnect();
134        }*/
135        
136        private void createEntry(String cacheName,String key, String value) throws IOException {
137
138            HttpURLConnection connection = (HttpURLConnection) toURL(cacheName,key).openConnection();
139            connection.setRequestProperty("Content-Type", "application/x-java-serialized-object");
140            connection.setDoOutput(true);
141            connection.setRequestMethod("PUT");
142            connection.connect();
143            
144         // Write the message to the servlet
145            
146              OutputStream os = connection.getOutputStream(); // returns
147              ObjectOutputStream oos = new ObjectOutputStream(os);
148              oos.writeObject(value);
149              oos.flush();
150              
151            connection.disconnect();
152        }
153
154        /*private void removeEntry(String cacheName,String key) throws IOException {
155                
156            HttpURLConnection connection = (HttpURLConnection) toURL(cacheName,key).openConnection();
157            connection.setRequestProperty("Content-Type", "application/x-java-serialized-object");
158            connection.setDoOutput(true);
159            connection.setRequestMethod("DELETE");
160            connection.connect();
161            
162            disconnectEL(connection);
163            
164        }*/
165
166        
167
168        public boolean contains(String cacheName,String key) throws IOException {
169                HttpURLConnection connection = (HttpURLConnection) toURL(cacheName, key).openConnection();
170            connection.setRequestMethod("HEAD");
171            connection.connect();
172            try{
173                return connection.getResponseCode()==200;
174            }
175            finally{
176                disconnectEL(connection);
177            }
178        }
179        
180        
181        public Object getValue(String cacheName,String key) throws IOException {
182                HttpURLConnection connection = (HttpURLConnection) toURL(cacheName, key).openConnection();
183            connection.setRequestMethod("GET");
184            connection.connect();
185            try{
186            return getContent(connection);
187            }
188            finally{
189                disconnectEL(connection);
190            }
191        }
192        
193        
194
195
196        public CacheEntry getEntry(String cacheName,String key) throws IOException {
197                Object obj=null;
198                HttpURLConnection connection = (HttpURLConnection) toURL(cacheName, key).openConnection();
199            connection.setRequestMethod("GET");
200            connection.connect();
201            try {
202                connection.getContentLength();
203                connection.getHeaderField("Expires");
204                connection.getHeaderField("Last-Modified");
205                
206                obj=getContent(connection);
207                
208                }
209        finally {
210                disconnectEL(connection);
211        }
212            return new RESTCacheEntry(key,obj);
213        }       
214
215        
216        private static Object getContent(HttpURLConnection conn) {
217                InputStream is=null;
218                try     {
219                        return Converter.toObject(conn.getContentType(),conn.getInputStream());
220                }
221            catch(Exception e){
222                return null;
223            }
224        finally {
225                Util.closeEL(is);
226        }
227                
228        }
229        
230        
231        private static void disconnectEL(HttpURLConnection connection) {
232                if(connection!=null){
233                        connection.disconnect();
234                }
235        }
236
237
238        private URL toURL(String cacheName, String key) {
239                try{
240                        return new URL(strUrl+cacheName+"/"+key);
241                }
242                catch(MalformedURLException e){
243                        return null;
244                }
245        }
246
247
248        private URL toURL(String cacheName) {
249                try{
250                        return new URL(strUrl+cacheName);
251                }
252                catch(MalformedURLException e){
253                        return null;
254                }
255        }
256        
257        public static void main(String[] args) throws Exception {
258                RESTClient client81 = new RESTClient(new URL("http://localhost:8181/rest/"));
259                //client81.createCache("sample");
260                client81.createEntry("sample", "mx81","81 "+new Date());
261                
262                RESTClient client82 = new RESTClient(new URL("http://localhost:8282/rest/"));
263                //client82.createCache("sample");
264                client82.createEntry("sample", "mx82","82 "+new Date());
265                
266                
267                /*
268                RESTClient client82 = new RESTClient(new URL("http://localhost:8282/rest/"));
269                client82.createCache("sample");
270                client82.createEntry("sample", "resti","RESTFull82");
271                */
272                
273                
274                
275                
276        }
277
278}