001: /*
002: * FileEngine.java February 2001
003: *
004: * Copyright (C) 2001, Niall Gallagher <niallg@users.sf.net>
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General
016: * Public License along with this library; if not, write to the
017: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
018: * Boston, MA 02111-1307 USA
019: */
020:
021: package simple.http.serve;
022:
023: import simple.http.Response;
024: import simple.http.Request;
025: import java.io.File;
026:
027: /**
028: * The <code>FileEngine</code> is used to produce implementations
029: * of the <code>Resource</code> object to represent files on the
030: * underlying filesystem. This will produce <code>Resource</code>
031: * objects that map on to the suggested URI relative to some
032: * given <code>Context</code>. The <code>Context</code> must be
033: * given as a root so all requests for <code>Resource</code>'s are
034: * retrieved relative to that root.
035: * <p>
036: * The meaning of HTTP URI in this instance is the request URI
037: * from a HTTP/x.x request, as RFC 2616 and RFC 2396 defines it
038: *
039: * <pre>
040: * Request-Line = Method SP Request-URI SP HTTP-Version CRLF
041: *
042: * Request-URI = "*" | absoluteURI | abs_path | authority
043: * absoluteURI = "http:" "//" host [":" port] [abs_path ["?" query]]
044: * abs_path = "/" path_segments
045: * path_segments = segment *( "/" segment )
046: * </pre>
047: *
048: * So the <code>FileEngine</code> object will accept the request URI
049: * that come in the form outlined above. These can include formats
050: * like
051: *
052: * <pre>
053: * http://some.host/pub;param=value/bin/index.html?name=value
054: * http://some.host:8080/index.en_US.html
055: * some.host:8080/index.html
056: * /usr/bin;param=value/README.txt
057: * /usr/bin/compress.tar.gz
058: * </pre>
059: *
060: * The <code>FileEngine</code> object will directly take a request
061: * URI as defined in RFC 2616 and translate this into a system
062: * specific <code>Resource</code>. This keeps the objects semantics
063: * simple and explicit, although at the expense of performance.
064: *
065: * @author Niall Gallagher
066: *
067: * @see simple.http.serve.CacheContext
068: */
069: public class FileEngine implements ResourceEngine {
070:
071: /**
072: * Each <code>FileEngine</code> operates using a context.
073: */
074: protected Context context;
075:
076: /**
077: * Constructor for the <code>FileEngine</code>. This uses the
078: * current working directory to serve the <code>Resource</code>.
079: * This will retrieve and represent OS specific resources such
080: * as files using <code>Resource</code> implementations. This
081: * also keeps a cache of the file contents requested.
082: */
083: public FileEngine() {
084: this (new CacheContext());
085: }
086:
087: /**
088: * Constructor takes a <code>Context</code> implementation and
089: * operates relative to that implementation. This will retrieve
090: * and represent OS specific resources such as files using
091: * <code>Resource</code> implementations. This relies on the
092: * provided context to perform caching for this implementation.
093: *
094: * @param context this is the context the file engine will use
095: */
096: public FileEngine(Context context) {
097: this .context = context;
098: }
099:
100: /**
101: * This will look for and retrieve the requested resource. The
102: * target given must be in the form of a request URI. This will
103: * locate the resource and return the <code>Resource</code>
104: * implementation that will handle the target.
105: *
106: * @param target the URI style path that represents the target
107: *
108: * @return returns the resource instance to handle the target
109: */
110: public Resource resolve(String target) {
111: return resolve(target, context.getFile(target));
112: }
113:
114: /**
115: * This will look for and retrieve the requested resource. The
116: * target given must be in the form of a request URI. This will
117: * locate the resource and return the <code>Resource</code>
118: * implementation that will handle the target.
119: *
120: * @param target the URI style path that represents the target
121: * @param file this is the file that represents the resource
122: *
123: * @return returns the resource instance to handle the target
124: */
125: protected Resource resolve(String target, File file) {
126: if (file.isDirectory()) {
127: return new DirectoryComponent(context, target);
128: } else if (file.isFile()) {
129: return new FileComponent(context, target);
130: } else
131: return new Component(context) {
132: protected void process(Request req, Response resp) {
133: handle(req, resp, 404);
134: }
135: };
136: }
137: }
|