| 1 | |
package org.webslinger; |
| 2 | |
|
| 3 | |
import java.io.InputStream; |
| 4 | |
import java.io.IOException; |
| 5 | |
import java.io.OutputStream; |
| 6 | |
import java.util.Map; |
| 7 | |
|
| 8 | |
import org.apache.commons.vfs.FileObject; |
| 9 | |
|
| 10 | |
import org.webslinger.container.FileInfo; |
| 11 | |
import org.webslinger.container.PathInfo; |
| 12 | |
import org.webslinger.container.StringFileInfo; |
| 13 | |
import org.webslinger.vfs.GeneratedStringVFSDelegate; |
| 14 | |
import org.webslinger.vfs.VFSDelegate; |
| 15 | |
import org.webslinger.vfs.VFSDelegateProxy; |
| 16 | |
import org.webslinger.servlet.FakeResource; |
| 17 | |
|
| 18 | |
public final class PathContext implements FakeResource { |
| 19 | |
private final PathInfo pathInfo; |
| 20 | |
private final FileInfo info; |
| 21 | |
|
| 22 | 1404 | public PathContext(FileInfo info, PathInfo pathInfo) { |
| 23 | 1404 | this.info = info; |
| 24 | 1404 | this.pathInfo = pathInfo; |
| 25 | 1404 | } |
| 26 | |
|
| 27 | |
public FileInfo getInfo() { |
| 28 | 20803 | return info; |
| 29 | |
} |
| 30 | |
|
| 31 | |
public boolean exists() throws IOException { |
| 32 | 51 | return getInfo().exists(); |
| 33 | |
} |
| 34 | |
|
| 35 | |
public PathContext makeDynamicPC(Webslinger webslinger, String text, Map<String, ? super Object> attributes, long lastModifiedTime) throws IOException { |
| 36 | 0 | return new PathContext(new StringFileInfo(info.getContainer(), info.getServletFile(), new GeneratedStringVFSDelegate.GeneratedString<Webslinger>(webslinger, text, lastModifiedTime, attributes), "application/x-webslinger-capture"), pathInfo); |
| 37 | |
} |
| 38 | |
|
| 39 | |
public boolean attributeExists(String name) throws IOException { |
| 40 | 0 | return getInfo().attributeExists(name); |
| 41 | |
} |
| 42 | |
|
| 43 | |
public Object getAttribute(String name) throws IOException { |
| 44 | 8373 | return getInfo().getAttribute(name); |
| 45 | |
} |
| 46 | |
|
| 47 | |
public String[] getSplitAttribute(String name) throws IOException { |
| 48 | 231 | return getInfo().getSplitAttribute(name); |
| 49 | |
} |
| 50 | |
|
| 51 | |
public String[] getSplitAttribute(String name, String def) throws IOException { |
| 52 | 1604 | return getInfo().getSplitAttribute(name, def); |
| 53 | |
} |
| 54 | |
|
| 55 | |
public String getServletPath() { |
| 56 | 204 | return getInfo().getServletFile().getName().getPath(); |
| 57 | |
} |
| 58 | |
|
| 59 | |
public FileObject getFile() throws IOException { |
| 60 | 4806 | return getInfo().getFile(); |
| 61 | |
} |
| 62 | |
|
| 63 | |
public String getType() throws IOException { |
| 64 | 1565 | return getInfo().getType(); |
| 65 | |
} |
| 66 | |
|
| 67 | |
public String getPathInfo() { |
| 68 | 1300 | return pathInfo.getPathInfo(); |
| 69 | |
} |
| 70 | |
|
| 71 | |
public PathContext resolve(Object item) throws IOException { |
| 72 | 67 | if (item == null) return null; |
| 73 | 67 | if (item instanceof PathContext) return (PathContext) item; |
| 74 | 67 | if (item instanceof String) return resolve((String) item); |
| 75 | 0 | throw new IllegalArgumentException("Can't handle " + item); |
| 76 | |
} |
| 77 | |
|
| 78 | |
public PathContext resolve(String path) throws IOException { |
| 79 | 67 | return getInfo().getContainer().resolve(this, this, path, true); |
| 80 | |
} |
| 81 | |
|
| 82 | |
public boolean equals(Object o) { |
| 83 | 251 | if (!(o instanceof PathContext)) return false; |
| 84 | 251 | PathContext other = (PathContext) o; |
| 85 | 251 | return getInfo().equals(other.info) && getPathInfo().equals(other.getPathInfo()); |
| 86 | |
} |
| 87 | |
|
| 88 | |
public int hashCode() { |
| 89 | 575 | return getInfo().hashCode() ^ getPathInfo().hashCode(); |
| 90 | |
} |
| 91 | |
|
| 92 | |
public String toString() { |
| 93 | 0 | return "PC<" + getServletPath() + ';' + getPathInfo() + '>'; |
| 94 | |
} |
| 95 | |
|
| 96 | 1469 | public static class PathContextVFSDelegate<C> extends VFSDelegateProxy<Object, PathContext, Object, C> { |
| 97 | |
public PathContextVFSDelegate(VFSDelegate<Object, Object, C> vfsDelegate) { |
| 98 | 26 | super(vfsDelegate, Object.class); |
| 99 | 26 | } |
| 100 | |
|
| 101 | |
protected FileInfo getReal(PathContext pc) throws IOException { |
| 102 | 1473 | return pc.getInfo(); |
| 103 | |
} |
| 104 | |
|
| 105 | |
public boolean attributeExists(PathContext pc, String name) throws IOException { |
| 106 | 0 | return pc.attributeExists(name); |
| 107 | |
} |
| 108 | |
|
| 109 | |
public Object getAttribute(PathContext pc, String name) throws IOException { |
| 110 | 0 | return pc.getAttribute(name); |
| 111 | |
} |
| 112 | |
} |
| 113 | |
} |