001: /*
002: * FileIndex.java December 2005
003: *
004: * Copyright (C) 2005, 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.util.parse.URIParser;
024: import simple.util.net.URI;
025: import simple.util.net.Path;
026: import java.util.Locale;
027: import java.io.File;
028:
029: /**
030: * The <code>FileIndex</code> provides an implementation of an index
031: * that makes use of the OS file system to acquire meta data. This
032: * will acquire information directly from the URI target, as well as
033: * a MIME database maintained within the <code>Content.xml</code> file.
034: * This caches all meta data acquired so that there is no expense in
035: * re-acquiring the data. This allows for faster meta data retrieval
036: * and facilitates the implementation of the meta data cache used.
037: *
038: * @author Niall Gallagher
039: *
040: * @see simple.http.serve.FileIndexer
041: */
042: final class FileIndex implements Index {
043:
044: /**
045: * This is the source indexer used to acquire the meta data.
046: */
047: private Indexer indexer;
048:
049: /**
050: * This is the path portion of the specified URI target.
051: */
052: private Path path;
053:
054: /**
055: * This is the OS specific file referencing the resource.
056: */
057: private File file;
058:
059: /**
060: * This is the MIME type resolved for the resource.
061: */
062: private String type;
063:
064: /**
065: * This is the locale specific to the URI specified.
066: */
067: private Locale locale;
068:
069: /**
070: * This contains all the information regarding the URI.
071: */
072: private URI target;
073:
074: /**
075: * Constructor for the <code>FileIndex</code> object. This uses a
076: * URI target to acquire the meta data for the resource. The URI
077: * provides the resource name and path and also provides a hint
078: * for the MIME type of the resource from the file extension.
079: *
080: * @param indexer this is the source indexer for this instance
081: * @param target this is the URI target that is to be indexed
082: */
083: public FileIndex(Indexer indexer, String target) {
084: this (indexer, new URIParser(target));
085: }
086:
087: /**
088: * Constructor for the <code>FileIndex</code> object. This uses a
089: * URI target to acquire the meta data for the resource. The URI
090: * provides the resource name and path and also provides a hint
091: * for the MIME type of the resource from the file extension.
092: *
093: * @param indexer this is the source indexer for this instance
094: * @param target this is the URI target that is to be indexed
095: */
096: public FileIndex(Indexer indexer, URI target) {
097: this .indexer = indexer;
098: this .target = target;
099: }
100:
101: /**
102: * This is used to get the path that this object refers to.
103: * This should be the fully qualified normalized path. This
104: * refers to the OS system specific path that this represents.
105: *
106: * @return this returns the OS specific path for the target
107: */
108: public String getContentType() {
109: if (type == null) {
110: type = getContentType(target);
111: }
112: return type;
113: }
114:
115: /**
116: * This is used to get the path that this object refers to.
117: * This should be the fully qualified normalized path. This
118: * refers to the OS system specific path that this represents.
119: *
120: * @param target the index target to get the real path for
121: *
122: * @return this returns the OS specific path for the target
123: */
124: public String getContentType(URI target) {
125: return indexer.getContentType(target);
126: }
127:
128: /**
129: * This gets the locale for this index object the locale is
130: * set to the <code>Locale.getDefault</code> if there is no
131: * locale information available for the index target. This
132: * will provide the <code>Context.getLocale</code> object.
133: *
134: * @return this returns the locale for this index target
135: */
136: public Locale getLocale() {
137: if (locale == null) {
138: locale = getLocale(target);
139: }
140: return locale;
141: }
142:
143: /**
144: * This gets the locale for this index object the locale is
145: * set to the <code>Locale.getDefault</code> if there is no
146: * locale information available for the index target. This
147: * will provide the <code>Context.getLocale</code> object.
148: *
149: * @param target the index target to get the locale for
150: *
151: * @return this returns the locale for this index target
152: */
153: public Locale getLocale(URI target) {
154: return indexer.getLocale(target);
155: }
156:
157: /**
158: * This is used to acquire the <code>File</code> reference
159: * for the index target. This is typically rooted at a
160: * base path, for instance the <code>Context</code> root
161: * is typically used. This allows the file to be opened,
162: * deleted, or read should the need arise in a service.
163: *
164: * @return this returns the OS file for the resource
165: */
166: public File getFile() {
167: if (file == null) {
168: file = getFile(target);
169: }
170: return file;
171: }
172:
173: /**
174: * This is used to acquire the <code>File</code> reference
175: * for the index target. This is typically rooted at a
176: * base path, for instance the <code>Context</code> root
177: * is typically used. This allows the file to be opened,
178: * deleted, or read should the need arise in a service.
179: *
180: * @param target the index target to get the OS file for
181: *
182: * @return this returns the OS file for the resource
183: */
184: public File getFile(URI target) {
185: return indexer.getFile(target);
186: }
187:
188: /**
189: * This is used to acquire the <code>Path</code> object that
190: * exposes various parts of the URI path. This can be used
191: * to extract the individual path segments as strings as
192: * well as the file extension and various other details.
193: *
194: * @return this returns a path object with various details
195: */
196: public Path getPath() {
197: if (path == null) {
198: path = getPath(target);
199: }
200: return path;
201: }
202:
203: /**
204: * This is used to acquire the <code>Path</code> object that
205: * exposes various parts of the URI path. This can be used
206: * to extract the individual path segments as strings as
207: * well as the file extension and various other details.
208: *
209: * @param target the index target to get the URI path for
210: *
211: * @return this returns a path object with various details
212: */
213: public Path getPath(URI target) {
214: return indexer.getPath(target);
215: }
216:
217: /**
218: * This is used to get the path that this object refers to.
219: * This should be the fully qualified normalized path. This
220: * refers to the OS system specific path that this represents.
221: *
222: * @return this returns the OS specific path for the target
223: */
224: public String getRealPath() {
225: return getFile().getAbsolutePath();
226: }
227:
228: /**
229: * This is used to acquire the <code>File</code> directory
230: * for the index target. This is typically rooted at a
231: * base path, for instance the <code>Context</code> root
232: * is typically used. This allows resources within the
233: * same directory to be acquired easily.
234: *
235: * @return this returns the OS file for the directory
236: */
237: public File getDirectory() {
238: return getFile().getParentFile();
239: }
240:
241: /**
242: * This is used to acquire the normalized URI style path for
243: * the index target. This allows the path to be used within
244: * the <code>Mapper</code> and other such objects that need
245: * a normalized URI style path to resolve resources.
246: *
247: * @return this returns the normalized path for the target
248: */
249: public String getRequestPath() {
250: return getPath().getPath();
251: }
252:
253: /**
254: * This allows the name for this object to be acquired. The
255: * name usually refers to the last entry in the path. So if
256: * the index target path was "/usr/bin/" the name is "bin".
257: *
258: * @return this returns the name of this index target
259: */
260: public String getName() {
261: return getPath().getName();
262: }
263: }
|