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