001: /*
002: JSPWiki - a JSP-based WikiWiki clone.
003:
004: Copyright (C) 2001-2002 Janne Jalkanen (Janne.Jalkanen@iki.fi)
005:
006: This program is free software; you can redistribute it and/or modify
007: it under the terms of the GNU Lesser General Public License as published by
008: the Free Software Foundation; either version 2.1 of the License, or
009: (at your option) any later version.
010:
011: This program is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: GNU Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public License
017: along with this program; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020: package com.ecyrd.jspwiki.dav;
021:
022: import java.util.ArrayList;
023: import java.util.Collection;
024: import java.util.Collections;
025: import java.util.Iterator;
026:
027: import org.apache.log4j.Logger;
028:
029: import com.ecyrd.jspwiki.WikiContext;
030: import com.ecyrd.jspwiki.WikiEngine;
031: import com.ecyrd.jspwiki.WikiPage;
032: import com.ecyrd.jspwiki.dav.items.DavItem;
033: import com.ecyrd.jspwiki.dav.items.DirectoryItem;
034: import com.ecyrd.jspwiki.dav.items.PageDavItem;
035: import com.ecyrd.jspwiki.providers.ProviderException;
036: import com.opensymphony.oscache.base.Cache;
037: import com.opensymphony.oscache.base.NeedsRefreshException;
038:
039: /**
040: * Implements something for the pages.
041: *
042: * @author jalkanen
043: *
044: * @since
045: */
046: public class RawPagesDavProvider extends WikiDavProvider {
047: protected static final Logger log = Logger
048: .getLogger(RawPagesDavProvider.class);
049:
050: private Cache m_davItemCache = new Cache(true, false, false);
051:
052: private int m_refreshPeriod = 30 * 1000; // In millisseconds
053:
054: public RawPagesDavProvider(WikiEngine engine) {
055: super (engine);
056: }
057:
058: protected Collection listAlphabeticals(DavPath path) {
059: ArrayList charList = new ArrayList();
060:
061: try {
062: Collection allPages = m_engine.getPageManager()
063: .getAllPages();
064:
065: for (Iterator i = allPages.iterator(); i.hasNext();) {
066: String pageName = ((WikiPage) i.next()).getName();
067:
068: Character firstChar = new Character(Character
069: .toLowerCase(pageName.charAt(0)));
070:
071: if (!charList.contains(firstChar)) {
072: charList.add(firstChar);
073: }
074: }
075: } catch (ProviderException e) {
076: log.error("Could not fetch a list of all pages:", e);
077: }
078:
079: Collections.sort(charList);
080:
081: ArrayList result = new ArrayList();
082:
083: for (Iterator i = charList.iterator(); i.hasNext();) {
084: Character c = (Character) i.next();
085:
086: result.add(new DirectoryItem(this ,
087: new DavPath(c.toString())));
088: }
089:
090: return result;
091: }
092:
093: // FIXME: This is wasteful; this should really keep a structure of its
094: // own in memory
095:
096: private Collection listDirContents(DavPath path) {
097: String st = path.getName();
098:
099: log.info("Listing contents for dir " + st);
100: ArrayList davItems = new ArrayList();
101:
102: try {
103: Collection allPages = m_engine.getPageManager()
104: .getAllPages();
105:
106: for (Iterator i = allPages.iterator(); i.hasNext();) {
107: WikiPage p = (WikiPage) i.next();
108:
109: if (p.getName().toLowerCase().startsWith(st)) {
110: DavPath np = new DavPath(path);
111: np.append(p.getName() + ".txt");
112:
113: DavItem di = new PageDavItem(this , np, p);
114:
115: davItems.add(di);
116: }
117: }
118: } catch (ProviderException e) {
119: log.error("Unable to fetch a list of all pages", e);
120: // FIXME
121: }
122: return davItems;
123: }
124:
125: public Collection listItems(DavPath path) {
126: log.info("Listing dav path " + path + ", size=" + path.size());
127:
128: switch (path.size()) {
129: case 1:
130: return listAlphabeticals(path);
131:
132: case 2:
133: return listDirContents(path);
134:
135: default:
136: return null;
137: }
138: }
139:
140: protected String getRelativePath(String path) {
141: if (path.length() > 0) {
142: char c = Character.toLowerCase(path.charAt(0));
143:
144: return Character.toString(c);
145: }
146:
147: return "";
148: }
149:
150: public String getURL(DavPath path) {
151: return m_engine.getURL(WikiContext.NONE, DavUtil.combineURL(
152: "dav/raw/", path.getPath()), null, true);
153: }
154:
155: public DavItem getItem(DavPath dp) {
156: DavItem di = null;
157:
158: try {
159: di = (DavItem) m_davItemCache.getFromCache(dp.toString(),
160: m_refreshPeriod);
161:
162: if (di == null) {
163: di = getItemNoCache(dp);
164: }
165: } catch (NeedsRefreshException e) {
166: DavItem old = (DavItem) e.getCacheContent();
167:
168: if (old != null && old instanceof PageDavItem) {
169: WikiPage cached = ((PageDavItem) old).getPage();
170:
171: if (cached != null) {
172: WikiPage current = m_engine.getPage(cached
173: .getName());
174:
175: if (cached.getLastModified().equals(
176: current.getLastModified())) {
177: di = old;
178: }
179: }
180: } else {
181: di = getItemNoCache(dp);
182: }
183: }
184:
185: m_davItemCache.putInCache(dp.toString(), di);
186:
187: return di;
188: }
189:
190: protected DavItem getItemNoCache(DavPath path) {
191: String pname = path.filePart();
192:
193: //
194: // Lists top-level elements
195: //
196: if (path.isRoot()) {
197: log.info("Adding DAV items from path " + path);
198: DirectoryItem di = new DirectoryItem(this , path);
199:
200: di.addDavItems(listAlphabeticals(path));
201:
202: return di;
203: }
204:
205: //
206: // Lists each item in each subdir
207: //
208: if (path.isDirectory()) {
209: log.info("Listing pages in path " + path);
210:
211: DirectoryItem di = new DirectoryItem(this , path);
212:
213: di.addDavItems(listDirContents(path));
214:
215: return di;
216: }
217:
218: if (pname.endsWith(".txt") && pname.length() > 4) {
219: pname = pname.substring(0, pname.length() - 4);
220: }
221:
222: WikiPage page = m_engine.getPage(pname);
223:
224: if (page != null) {
225: return new PageDavItem(this , path, page);
226: }
227:
228: return null;
229: }
230:
231: public void setItem(DavPath path, DavItem item) {
232: // TODO Auto-generated method stub
233:
234: }
235:
236: }
|