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