001: /*
002: * Copyright 2004-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: *
016: */
017:
018: package org.jpublish.repository.servletcontext;
019:
020: import java.io.InputStream;
021: import java.io.StringWriter;
022: import java.io.StringReader;
023: import java.io.File;
024: import java.util.Iterator;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028:
029: import com.anthonyeden.lib.util.IOUtilities;
030: import com.anthonyeden.lib.config.Configuration;
031:
032: import org.jpublish.JPublishContext;
033: import org.jpublish.view.ViewRenderer;
034: import org.jpublish.util.PathUtilities;
035: import org.jpublish.util.BreadthFirstPathTreeIterator;
036: import org.jpublish.util.vfs.VFSFile;
037: import org.jpublish.repository.AbstractRepository;
038:
039: /** Repository implementation which pulls content from the servlet
040: context.
041:
042: @author Anthony Eden
043: @since 2.0
044: */
045:
046: public class ServletContextRepository extends AbstractRepository {
047:
048: private static final Log log = LogFactory
049: .getLog(ServletContextRepository.class);
050:
051: /** Get the content from the given path. Implementations of this method
052: should NOT merge the content using view renderer.
053:
054: @param path The relative content path
055: @return The content as a String
056: @throws Exception Any Exception
057: */
058:
059: public String get(String path) throws Exception {
060: InputStream in = null;
061: StringWriter out = null;
062: try {
063: String root = PathUtilities.toResourcePath(getRoot());
064: String relativePath = PathUtilities.toResourcePath(path);
065:
066: in = siteContext.getServletContext().getResourceAsStream(
067: root + relativePath);
068: out = new StringWriter();
069:
070: int c = -1;
071: while ((c = in.read()) != -1) {
072: out.write((char) c);
073: }
074:
075: return out.toString();
076: } finally {
077: IOUtilities.close(in);
078: IOUtilities.close(out);
079: }
080: }
081:
082: /** Get the content from the given path and merge it with
083: the given context.
084:
085: @param path The content path
086: @param context The current context
087: @return The content as a String
088: @throws Exception Any Exception
089: */
090:
091: public String get(String path, JPublishContext context)
092: throws Exception {
093: if (log.isDebugEnabled())
094: log.debug("Getting dynamic content element for path "
095: + path);
096:
097: StringWriter writer = null;
098: StringReader reader = null;
099:
100: try {
101: writer = new StringWriter();
102: reader = new StringReader(get(path));
103:
104: String name = PathUtilities.makeRepositoryURI(getName(),
105: path);
106: ViewRenderer renderer = siteContext.getViewRenderer();
107: renderer.render(context, name, reader, writer);
108:
109: return writer.toString();
110: } finally {
111: IOUtilities.close(writer);
112: IOUtilities.close(reader);
113: }
114: }
115:
116: /** Remove the content at the specified path.
117:
118: @param path The path
119: */
120:
121: public void remove(String path) throws Exception {
122: throw new UnsupportedOperationException(
123: "Cannot remove web content");
124: }
125:
126: /** Make the directory for the specified path. Parent directories
127: will also be created if they do not exist.
128:
129: @param path The directory path
130: */
131:
132: public void makeDirectory(String path) {
133: throw new UnsupportedOperationException(
134: "Make directory not supported");
135: }
136:
137: /** Remove the directory for the specified path. The directory
138: must be empty.
139:
140: @param path The path
141: @throws Exception
142: */
143:
144: public void removeDirectory(String path) throws Exception {
145: throw new UnsupportedOperationException(
146: "Remove directory not supported");
147: }
148:
149: /** Get the last modified time in milliseconds for the given path.
150:
151: @param path The content path
152: @return The last modified time in milliseconds
153: @throws Exception Any exception
154: */
155:
156: public long getLastModified(String path) throws Exception {
157: // unfortunately the servlet API offers no way to determine the
158: // last modified time
159: return -1;
160: }
161:
162: /** Get an Iterator of paths which are known to the repository.
163:
164: @return An iterator of paths
165: @throws Exception
166: */
167:
168: public Iterator getPaths() throws Exception {
169: return getPaths("");
170: }
171:
172: /** Get an Iterator of paths which are known to the repository, starting
173: from the specified base path.
174:
175: @param base The base path
176: @return An iterator of paths
177: @throws Exception
178: */
179:
180: public Iterator getPaths(String path) throws Exception {
181: String root = PathUtilities.toResourcePath(getRoot());
182: String basePath = PathUtilities.toResourcePath(path);
183: return new ServletContextPathIterator(this ,
184: new BreadthFirstPathTreeIterator(root + basePath,
185: siteContext.getServletContext()));
186: }
187:
188: /** Get the Virtual File System root file. The Virtual File System
189: provides a datasource-independent way of navigating through all
190: items known to the Repository.
191:
192: @return The root VFSFile
193: @throws Exception
194: */
195:
196: public VFSFile getVFSRoot() throws Exception {
197: // NYI
198: throw new UnsupportedOperationException();
199: }
200:
201: public File pathToFile(String path) {
202: // NYI
203: throw new UnsupportedOperationException();
204: }
205:
206: /** Load the repository's configuration from the given configuration
207: object.
208:
209: @param element The configuration object
210: @throws Exception
211: */
212:
213: public void loadConfiguration(Configuration configuration)
214: throws Exception {
215: this .name = configuration.getAttribute("name");
216: setRoot(configuration.getChildValue("root"));
217: }
218:
219: }
|