| 1 | |
package org.webslinger.container; |
| 2 | |
|
| 3 | |
import java.io.InputStream; |
| 4 | |
import java.io.IOException; |
| 5 | |
import java.io.OutputStream; |
| 6 | |
import java.util.HashMap; |
| 7 | |
import java.util.Map; |
| 8 | |
|
| 9 | |
import org.apache.commons.vfs.FileObject; |
| 10 | |
import org.apache.commons.vfs.FileType; |
| 11 | |
|
| 12 | |
import org.webslinger.commons.vfs.VFSUtil; |
| 13 | |
import org.webslinger.util.GeneratedResult; |
| 14 | |
import org.webslinger.util.TTLObject; |
| 15 | |
import org.webslinger.vfs.VFSDelegate; |
| 16 | |
import org.webslinger.vfs.VFSDelegateProxy; |
| 17 | |
|
| 18 | 1239 | public class CommonsVfsFileInfo extends FileInfo { |
| 19 | |
static { |
| 20 | 1 | TTLObject.setDefaultTTLForClass(CommonsVfsFileInfo.class, 1000); |
| 21 | 1 | } |
| 22 | |
|
| 23 | |
public CommonsVfsFileInfo(WebslingerContainer container, FileObject servletFile) { |
| 24 | 890 | super(container, servletFile); |
| 25 | 890 | } |
| 26 | |
|
| 27 | |
protected FileCache createFileCache(FileObject defaultsFile) throws IOException { |
| 28 | 493 | return createFileCache(defaultsFile, null); |
| 29 | |
} |
| 30 | |
|
| 31 | |
public String getContentType() throws IOException { |
| 32 | 12 | return ((CommonsVfsFileCache) getObject()).contentType; |
| 33 | |
} |
| 34 | |
|
| 35 | |
protected long getTimestamp(FileCache old) throws IOException { |
| 36 | 837 | FileObject file = findFile(); |
| 37 | 837 | if (old == null) return file.exists() ? file.getContent().getLastModifiedTime() : NOT_EXISTANT_TIMESTAMP; |
| 38 | 837 | if (((CommonsVfsFileCache) old).file.equals(file)) return file.exists() ? file.getContent().getLastModifiedTime() : NOT_EXISTANT_TIMESTAMP; |
| 39 | 3 | return FORCE_REGEN; |
| 40 | |
} |
| 41 | |
|
| 42 | |
protected FileCache createFileCache(FileObject defaultsFile, String contentType) throws IOException { |
| 43 | 895 | FileObject file = findFile(); |
| 44 | |
String newContentType; |
| 45 | 895 | if (file.exists()) { |
| 46 | 792 | newContentType = file.getContent().getContentInfo().getContentType(); |
| 47 | 792 | if (newContentType == null) { |
| 48 | 684 | if (file.getType().hasChildren()) { |
| 49 | 2 | newContentType = "inode/directory"; |
| 50 | |
} else { |
| 51 | 682 | String extension = file.getName().getExtension(); |
| 52 | 682 | newContentType = getContainer().getMimeType(extension); |
| 53 | 682 | if (newContentType == null) newContentType = getContainer().getDefaultMimeType(); |
| 54 | 682 | } |
| 55 | |
} |
| 56 | |
} else { |
| 57 | 103 | String extension = file.getName().getExtension(); |
| 58 | 103 | newContentType = getContainer().getMimeType(extension); |
| 59 | 103 | if (newContentType == null) newContentType = getContainer().getDefaultMimeType(); |
| 60 | |
} |
| 61 | |
|
| 62 | 895 | if (contentType != null && contentType.equals(newContentType)) { |
| 63 | |
|
| 64 | 497 | } else if (contentType == null && newContentType == null) { |
| 65 | |
|
| 66 | |
} else { |
| 67 | |
|
| 68 | 497 | defaultsFile = null; |
| 69 | |
} |
| 70 | 895 | contentType = newContentType; |
| 71 | |
|
| 72 | 895 | if (contentType != null && defaultsFile == null) { |
| 73 | 497 | defaultsFile = getContainer().getWWW().resolveFile("/WEB-INF/DefaultMimeAttributes/" + contentType); |
| 74 | |
} |
| 75 | |
|
| 76 | 895 | return new CommonsVfsFileCache(file, contentType, defaultsFile); |
| 77 | |
} |
| 78 | |
|
| 79 | |
protected GeneratedResult<FileCache> generate(FileCache old) throws IOException { |
| 80 | 402 | CommonsVfsFileCache cache = (CommonsVfsFileCache) createFileCache(old != null ? old.defaultsFile : null, old != null ? ((CommonsVfsFileCache) old).contentType : null); |
| 81 | 402 | return new GeneratedResult<FileCache>(cache.file.exists() ? cache.file.getContent().getLastModifiedTime() : NOT_EXISTANT_TIMESTAMP, cache); |
| 82 | |
} |
| 83 | |
|
| 84 | |
public FileObject getFile() throws IOException { |
| 85 | 6613 | return ((CommonsVfsFileCache) getObject()).file; |
| 86 | |
} |
| 87 | |
|
| 88 | |
public boolean exists() throws IOException { |
| 89 | 51 | return getFile().exists(); |
| 90 | |
} |
| 91 | |
|
| 92 | |
public boolean equals(Object o) { |
| 93 | 1441 | if (!(o instanceof CommonsVfsFileInfo)) return false; |
| 94 | 1443 | CommonsVfsFileInfo other = (CommonsVfsFileInfo) o; |
| 95 | 1447 | return getServletFile().equals(other.getServletFile()); |
| 96 | |
} |
| 97 | |
|
| 98 | |
public int hashCode() { |
| 99 | 2051 | return servletFile.hashCode(); |
| 100 | |
} |
| 101 | |
|
| 102 | |
protected FileObject findFile() throws IOException { |
| 103 | 1732 | FileType type = servletFile.getType(); |
| 104 | 1732 | VFSUtil.refresh(servletFile); |
| 105 | |
|
| 106 | 1732 | if (servletFile.exists() && !type.hasChildren()) return servletFile; |
| 107 | 1138 | String[] extensions = (String[]) getContainer().getExtensionList(); |
| 108 | |
|
| 109 | 1138 | if (extensions == null || extensions.length == 0) return servletFile; |
| 110 | |
FileObject base; |
| 111 | |
String name; |
| 112 | 1120 | if (type.hasChildren()) { |
| 113 | 202 | if (servletFile.getContent().getAttribute("type") instanceof String) return servletFile; |
| 114 | 202 | base = servletFile; |
| 115 | 202 | name = "index"; |
| 116 | 202 | String defaultIndex = (String) servletFile.getContent().getAttribute("default-index"); |
| 117 | 202 | if (defaultIndex != null) { |
| 118 | 3 | FileObject sub = getContainer().parse(servletFile, servletFile, defaultIndex, true).getFile(); |
| 119 | 3 | if (sub.exists()) return sub; |
| 120 | 0 | name = defaultIndex; |
| 121 | |
} |
| 122 | 199 | } else { |
| 123 | 918 | base = servletFile.getParent(); |
| 124 | 918 | name = servletFile.getName().getBaseName(); |
| 125 | 918 | VFSUtil.refresh(base); |
| 126 | |
} |
| 127 | 1117 | if (!base.exists()) return servletFile; |
| 128 | 1101 | FileObject[] children = base.getChildren(); |
| 129 | 9958 | if (children != null) for (String extension: extensions) { |
| 130 | 9810 | String toMatch = name + extension; |
| 131 | 96265 | for (FileObject child: children) { |
| 132 | 87408 | if (child.getName().getBaseName().equals(toMatch)) { |
| 133 | 953 | VFSUtil.refresh(child); |
| 134 | 953 | if (!child.getType().hasChildren()) return child; |
| 135 | |
} |
| 136 | |
} |
| 137 | |
} |
| 138 | 148 | if (type.hasChildren()) { |
| 139 | 17 | if ("true".equals(base.getContent().getAttribute("allow-listing"))) return base; |
| 140 | 13 | return base.resolveFile(name); |
| 141 | |
} |
| 142 | |
|
| 143 | 131 | return servletFile; |
| 144 | |
} |
| 145 | |
|
| 146 | |
public String toString() { |
| 147 | 3 | return "FI<" + servletFile + '>'; |
| 148 | |
} |
| 149 | |
|
| 150 | |
public static class CommonsVfsFileInfoVFSDelegate<C> extends FileInfoVFSDelegate<CommonsVfsFileInfo, C> { |
| 151 | |
public CommonsVfsFileInfoVFSDelegate(VFSDelegate<Object, Object, C> vfsDelegate) { |
| 152 | 26 | super(vfsDelegate); |
| 153 | 26 | } |
| 154 | |
|
| 155 | |
protected Object getReal(FileInfo fi) throws IOException { |
| 156 | 1639 | return ((CommonsVfsFileInfo) fi).getFile(); |
| 157 | |
} |
| 158 | |
} |
| 159 | |
|
| 160 | |
protected class CommonsVfsFileCache extends FileCache { |
| 161 | |
public final FileObject file; |
| 162 | |
public final String contentType; |
| 163 | |
|
| 164 | |
|
| 165 | 895 | protected CommonsVfsFileCache(FileObject file, String contentType, FileObject defaultsFile) { |
| 166 | 895 | super(defaultsFile); |
| 167 | 895 | this.file = file; |
| 168 | 895 | this.contentType = contentType; |
| 169 | 895 | } |
| 170 | |
|
| 171 | |
protected Object getAttributeInternal(String name) throws IOException { |
| 172 | 10853 | if (file != null && file.exists()) { |
| 173 | 10657 | if (file.getContent().attributeExists(name)) return file.getContent().getAttribute(name); |
| 174 | |
} |
| 175 | 9694 | return super.getAttributeInternal(name); |
| 176 | |
} |
| 177 | |
|
| 178 | |
protected Map<String, ?> getAttributes() throws IOException { |
| 179 | 0 | Map<String, ?> attributes = super.getAttributes(); |
| 180 | 0 | if (file != null && file.exists()) attributes.putAll(file.getContent().getAttributes()); |
| 181 | 0 | return attributes; |
| 182 | |
} |
| 183 | |
} |
| 184 | |
} |