| 1 | |
package org.webslinger.container; |
| 2 | |
|
| 3 | |
import java.io.InputStream; |
| 4 | |
import java.io.OutputStream; |
| 5 | |
import java.io.IOException; |
| 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.util.GeneratedResult; |
| 13 | |
import org.webslinger.util.TTLObject; |
| 14 | |
import org.webslinger.vfs.GeneratedStringVFSDelegate; |
| 15 | |
import org.webslinger.vfs.VFSDelegate; |
| 16 | |
import org.webslinger.vfs.VFSDelegateProxy; |
| 17 | |
|
| 18 | 0 | public final class StringFileInfo extends FileInfo { |
| 19 | |
static { |
| 20 | 0 | TTLObject.setDefaultTTLForClass(StringFileInfo.class, 1000); |
| 21 | 0 | } |
| 22 | |
|
| 23 | |
private FileObject file; |
| 24 | |
private final String contentType; |
| 25 | 0 | private final HashMap attributes = new HashMap(); |
| 26 | |
private final GeneratedStringVFSDelegate.GeneratedString gs; |
| 27 | |
private final FileObject defaultsFile; |
| 28 | |
|
| 29 | |
public StringFileInfo(WebslingerContainer container, FileObject servletFile, GeneratedStringVFSDelegate.GeneratedString gs, String contentType) throws IOException { |
| 30 | 0 | super(container, servletFile); |
| 31 | 0 | this.gs = gs; |
| 32 | 0 | this.contentType = contentType; |
| 33 | 0 | defaultsFile = getContainer().getWWW().resolveFile("/WEB-INF/DefaultMimeAttributes/" + contentType); |
| 34 | |
|
| 35 | 0 | } |
| 36 | |
|
| 37 | |
protected FileCache getInitial() { |
| 38 | 0 | return createFileCache(defaultsFile); |
| 39 | |
} |
| 40 | |
|
| 41 | |
protected FileCache createFileCache(FileObject defaultsFile) { |
| 42 | 0 | return new FileCache(defaultsFile); |
| 43 | |
} |
| 44 | |
|
| 45 | |
protected long getTimestamp(FileCache old) { |
| 46 | 0 | return gs.lastModifiedTime; |
| 47 | |
} |
| 48 | |
|
| 49 | |
protected GeneratedResult<FileCache> generate(FileCache old) { |
| 50 | 0 | return new GeneratedResult<FileCache>(gs.lastModifiedTime, createFileCache(defaultsFile)); |
| 51 | |
} |
| 52 | |
|
| 53 | |
public String getContentType() throws IOException { |
| 54 | 0 | return contentType; |
| 55 | |
} |
| 56 | |
|
| 57 | |
public Map<String, ?> getAttributes() throws IOException { |
| 58 | 0 | Map<String, ?> attributes = getDefaultAttributes(); |
| 59 | 0 | attributes.putAll(gs.getAttributes()); |
| 60 | 0 | return attributes; |
| 61 | |
} |
| 62 | |
|
| 63 | |
protected Object getAttributeInternal(String name) throws IOException { |
| 64 | |
|
| 65 | 0 | if (gs.attributeExists(name)) return gs.getAttribute(name); |
| 66 | 0 | return super.getAttributeInternal(name); |
| 67 | |
} |
| 68 | |
|
| 69 | |
public FileObject getFile() throws IOException { |
| 70 | 0 | return file; |
| 71 | |
} |
| 72 | |
|
| 73 | |
public boolean exists() { |
| 74 | 0 | return true; |
| 75 | |
} |
| 76 | |
|
| 77 | |
public boolean equals(Object o) { |
| 78 | 0 | if (!(o instanceof StringFileInfo)) return false; |
| 79 | 0 | StringFileInfo other = (StringFileInfo) o; |
| 80 | 0 | return getServletFile().equals(other.getServletFile()) && gs.getResult().equals(other.gs.getResult()); |
| 81 | |
} |
| 82 | |
|
| 83 | |
public int hashCode() { |
| 84 | 0 | return servletFile.hashCode() ^ gs.getResult().hashCode(); |
| 85 | |
} |
| 86 | |
|
| 87 | |
public String toString() { |
| 88 | 0 | return "FI<" + servletFile + '>'; |
| 89 | |
} |
| 90 | |
|
| 91 | |
public static class StringFileInfoVFSDelegate<C> extends FileInfoVFSDelegate<StringFileInfo, C> { |
| 92 | |
public StringFileInfoVFSDelegate(VFSDelegate<Object, Object, C> vfsDelegate) { |
| 93 | 26 | super(vfsDelegate); |
| 94 | 26 | } |
| 95 | |
|
| 96 | |
protected Object getReal(FileInfo fi) { |
| 97 | 0 | return ((StringFileInfo) fi).gs; |
| 98 | |
} |
| 99 | |
} |
| 100 | |
} |