001    package de.deepamehta.plugins.files;
002    
003    import de.deepamehta.core.JSONEnabled;
004    import de.deepamehta.core.util.JavaUtils;
005    
006    import org.codehaus.jettison.json.JSONArray;
007    import org.codehaus.jettison.json.JSONObject;
008    
009    import java.io.File;
010    import java.util.ArrayList;
011    import java.util.List;
012    
013    
014    
015    public class DirectoryListing implements JSONEnabled {
016    
017        // ------------------------------------------------------------------------------------------------------- Constants
018    
019        private static final String FILE_REPOSITORY_PATH = System.getProperty("dm4.filerepo.path", "");
020        // Note: the default value is required in case no config file is in effect. This applies when DM is started
021        // via feature:install from Karaf. The default value must match the value defined in global POM.
022    
023        // ---------------------------------------------------------------------------------------------- Instance Variables
024    
025        private FileItem dirInfo;
026        private List<FileItem> fileItems = new ArrayList<FileItem>();
027    
028        // ---------------------------------------------------------------------------------------------------- Constructors
029    
030        public DirectoryListing(File directory) {
031            dirInfo = new FileItem(directory.getName(), directory.getPath());
032            for (File file : directory.listFiles()) {
033                String name = file.getName();
034                String path = file.getPath();
035                if (file.isDirectory()) {
036                    fileItems.add(new FileItem(name, path));
037                } else {
038                    fileItems.add(new FileItem(name, path, file.length(), JavaUtils.getFileType(file.getName())));
039                }
040            }
041        }
042    
043        // -------------------------------------------------------------------------------------------------- Public Methods
044    
045        public List<FileItem> getFileItems() {
046            return fileItems;
047        }
048    
049        // ---
050    
051        @Override
052        public JSONObject toJSON() {
053            try {
054                JSONObject dir = dirInfo.toJSON();
055                JSONArray items = new JSONArray();
056                for (FileItem item : fileItems) {
057                    items.put(item.toJSON());
058                }
059                dir.put("items", items);
060                return dir;
061            } catch (Exception e) {
062                throw new RuntimeException("Serialization failed (" + this + ")", e);
063            }
064        }
065    
066        // --------------------------------------------------------------------------------------------------- Inner Classes
067    
068        public class FileItem implements JSONEnabled {
069    
070            ItemKind kind;  // FILE or DIRECTORY
071            String name;
072            String path;
073            long size;      // for files only
074            String type;    // for files only
075    
076            /**
077             * Constructs a DIRECTORY item.
078             */
079            FileItem(String name, String path) {
080                this.kind = ItemKind.DIRECTORY;
081                this.name = name;
082                this.path = truncate(path);
083            }
084    
085            /**
086             * Constructs a FILE item.
087             */
088            FileItem(String name, String path, long size, String type) {
089                this.kind = ItemKind.FILE;
090                this.name = name;
091                this.path = truncate(path);
092                this.size = size;
093                this.type = type;
094            }
095    
096            // ---
097    
098            public ItemKind getItemKind() {
099                return kind;
100            }
101    
102            public String getName() {
103                return name;
104            }
105    
106            public String getPath() {
107                return path;
108            }
109    
110            public long getSize() {
111                return size;
112            }
113    
114            public String getMediaType() {
115                return type;
116            }
117    
118            // ---
119    
120            private String truncate(String path) {
121                // error check
122                if (!path.startsWith(FILE_REPOSITORY_PATH)) {
123                    throw new RuntimeException("Path \"" + path + "\" is not a file repository path");
124                }
125                //
126                return JavaUtils.stripDriveLetter(path.substring(FILE_REPOSITORY_PATH.length()));
127            }
128    
129            // ---
130    
131            @Override
132            public JSONObject toJSON() {
133                try {
134                    JSONObject item = new JSONObject();
135                    item.put("kind", kind.stringify());
136                    item.put("name", name);
137                    item.put("path", path);
138                    if (kind.equals("file")) {
139                        item.put("size", size);
140                        item.put("type", type);
141                    }
142                    return item;
143                } catch (Exception e) {
144                    throw new RuntimeException("Serialization failed (" + this + ")", e);
145                }
146            }
147        }
148    }