001    package org.mycore.frontend.fileupload;
002    
003    import java.io.BufferedReader;
004    import java.io.File;
005    import java.io.FileFilter;
006    import java.io.FilenameFilter;
007    import java.io.IOException;
008    import java.io.InputStreamReader;
009    import java.net.MalformedURLException;
010    import java.net.URI;
011    import java.net.URL;
012    import java.net.URLConnection;
013    import java.util.HashMap;
014    import java.util.Iterator;
015    import java.util.Map;
016    import java.util.StringTokenizer;
017    
018    public class MCRRemoteFile extends File {
019        /**
020         * 
021         */
022        private static final long serialVersionUID = 1L;
023    
024        private String remoteServletURL;
025        private String path;
026    
027        File f;
028    
029        public MCRRemoteFile(String url, String pathname) {
030            super("");
031            this.remoteServletURL = url;
032            this.path = pathname;
033    
034        }
035    
036        private String callServlet(Map paramMap) {
037            String paramStr = createParamString(paramMap);
038            String inputLine = null;
039            StringBuffer strBuffer = new StringBuffer();
040    
041            try {
042                URL servletURL = new URL(remoteServletURL + paramStr);
043                URLConnection connection = servletURL.openConnection();
044    
045                connection.setUseCaches(false);
046                BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
047    
048                while ((inputLine = in.readLine()) != null)
049                    strBuffer.append(inputLine);
050                in.close();
051    
052            } catch (MalformedURLException e) {
053                // TODO Auto-generated catch block
054                e.printStackTrace();
055            } catch (IOException e) {
056                // TODO Auto-generated catch block
057                e.printStackTrace();
058            }
059    
060            return strBuffer.toString();
061        }
062    
063        private String createParamString(Map paramMap) {
064            StringBuffer strBuffer = new StringBuffer();
065    
066            for (Iterator iterator = paramMap.keySet().iterator(); iterator.hasNext();) {
067                String key = (String) iterator.next();
068                String value = (String) paramMap.get(key);
069    
070                String symb = strBuffer.indexOf("?") < 0 ? "?" : "&";
071    
072                strBuffer.append(symb + key + "=" + value);
073            }
074            return strBuffer.toString();
075        }
076    
077        private boolean remoteCheck(String remoteMethod) {
078            HashMap paramMap = new HashMap();
079            paramMap.put("pathname", path);
080            paramMap.put("method", remoteMethod);
081    
082            boolean remoteCheck = Boolean.parseBoolean(callServlet(paramMap));
083            return remoteCheck;
084        }
085    
086        /*public boolean canExecute() {
087            return remoteCheck("canExecute");
088        }
089    
090        public boolean canRead() {
091            return remoteCheck("canRead");
092        }
093    
094        public boolean canWrite() {
095            return remoteCheck("canWrite");
096        }
097    
098        public int compareTo(File pathname) {
099            return f.compareTo(pathname);
100        }
101    
102        public boolean createNewFile() throws IOException {
103            return f.createNewFile();
104        }
105    
106        public boolean delete() {
107            return f.delete();
108        }
109    
110        public void deleteOnExit() {
111            f.deleteOnExit();
112        }
113    
114        public boolean equals(Object obj) {
115            return f.equals(obj);
116        }
117    
118        public boolean exists() {
119            boolean exists = remoteCheck("exists");
120            return exists;
121        }
122    
123        public File getAbsoluteFile() {
124            return f.getAbsoluteFile();
125        }
126    
127        public String getAbsolutePath() {
128            return f.getAbsolutePath();
129        }
130    
131        public File getCanonicalFile() throws IOException {
132            String canonPath = getCanonicalPath();
133            return f.getCanonicalFile();
134        }
135    
136        public String getCanonicalPath() throws IOException {
137            return remoteDo("getCanonicalPath");
138        }
139    
140        private String remoteDo(String method) {
141            HashMap paramMap = new HashMap();
142            paramMap.put("pathname", path);
143            paramMap.put("method", method);
144    
145            return callServlet(paramMap);
146        }
147    
148        public long getFreeSpace() {
149            return f.getFreeSpace();
150        }
151    
152        public String getName() {
153            return f.getName();
154        }
155    
156        public String getParent() {
157            return f.getParent();
158        }
159    
160        public File getParentFile() {
161            return f.getParentFile();
162        }
163    
164        public String getPath() {
165            return this.path;
166        }
167    
168        public long getTotalSpace() {
169            return f.getTotalSpace();
170        }
171    
172        public long getUsableSpace() {
173            return f.getUsableSpace();
174        }
175    
176        public int hashCode() {
177            return f.hashCode();
178        }
179    
180        public boolean isAbsolute() {
181            return remoteCheck("isAbsolute");
182        }
183    
184        public boolean isDirectory() {
185            return remoteCheck("isDirectory");
186        }
187    
188        public boolean isFile() {
189            return remoteCheck("isFile");
190        }
191    
192        public boolean isHidden() {
193            return remoteCheck("isHidden");
194        }
195    
196        public long lastModified() {
197            return f.lastModified();
198        }
199    
200        public long length() {
201            return f.length();
202        }
203    
204        public String[] list() {
205            HashMap paramMap = new HashMap();
206            paramMap.put("pathname", path);
207            paramMap.put("method", "list");
208    
209            String fileList = callServlet(paramMap);
210    
211            return fileList.split("\n");
212        }
213    
214        public String[] list(FilenameFilter filter) {
215            return f.list(filter);
216        }
217    
218        public File[] listFiles() {
219            return f.listFiles();
220        }
221    
222        public File[] listFiles(FileFilter filter) {
223            return f.listFiles(filter);
224        }
225    
226        public File[] listFiles(FilenameFilter filter) {
227            return f.listFiles(filter);
228        }
229    
230        public boolean mkdir() {
231            return f.mkdir();
232        }
233    
234        public boolean mkdirs() {
235            return f.mkdirs();
236        }
237    
238        public boolean renameTo(File dest) {
239            return f.renameTo(dest);
240        }
241    
242        public boolean setExecutable(boolean executable, boolean ownerOnly) {
243            return f.setExecutable(executable, ownerOnly);
244        }
245    
246        public boolean setExecutable(boolean executable) {
247            return f.setExecutable(executable);
248        }
249    
250        public boolean setLastModified(long time) {
251            return f.setLastModified(time);
252        }
253    
254        public boolean setReadable(boolean readable, boolean ownerOnly) {
255            return f.setReadable(readable, ownerOnly);
256        }
257    
258        public boolean setReadable(boolean readable) {
259            return f.setReadable(readable);
260        }
261    
262        public boolean setReadOnly() {
263            return f.setReadOnly();
264        }
265    
266        public boolean setWritable(boolean writable, boolean ownerOnly) {
267            return f.setWritable(writable, ownerOnly);
268        }
269    
270        public boolean setWritable(boolean writable) {
271            return f.setWritable(writable);
272        }
273    
274        public String toString() {
275            return f.toString();
276        }
277    
278        public URI toURI() {
279            return f.toURI();
280        }
281    
282        public URL toURL() throws MalformedURLException {
283            return f.toURL();
284        }*/
285    
286    }