001: /*
002: JSPWiki - a JSP-based WikiWiki clone.
003:
004: Copyright (C) 2001-2007 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.attachment.Attachment;
033: import com.ecyrd.jspwiki.dav.items.AttachmentItem;
034: import com.ecyrd.jspwiki.dav.items.DavItem;
035: import com.ecyrd.jspwiki.dav.items.DirectoryItem;
036: import com.ecyrd.jspwiki.providers.ProviderException;
037:
038: public class AttachmentDavProvider implements DavProvider {
039: protected WikiEngine m_engine;
040: protected static final Logger log = Logger
041: .getLogger(AttachmentDavProvider.class);
042:
043: public AttachmentDavProvider(WikiEngine engine) {
044: m_engine = engine;
045: }
046:
047: public WikiEngine getEngine() {
048: return m_engine;
049: }
050:
051: private Collection listAllPagesWithAttachments() {
052: ArrayList pageNames = new ArrayList();
053:
054: try {
055: Collection atts = m_engine.getAttachmentManager()
056: .getAllAttachments();
057:
058: for (Iterator i = atts.iterator(); i.hasNext();) {
059: Attachment att = (Attachment) i.next();
060:
061: String pageName = att.getParentName();
062:
063: if (!pageNames.contains(pageName))
064: pageNames.add(pageName);
065: }
066: } catch (ProviderException e) {
067: log.error("Unable to get all attachments", e);
068: }
069:
070: Collections.sort(pageNames);
071:
072: ArrayList result = new ArrayList();
073:
074: for (Iterator i = pageNames.iterator(); i.hasNext();) {
075: DirectoryItem di = new DirectoryItem(this , new DavPath(
076: (String) i.next()));
077:
078: result.add(di);
079: }
080: return result;
081: }
082:
083: protected Collection listAttachmentsOfPage(DavPath path) {
084: String pageName = path.getName();
085:
086: log.debug("Listing attachments for page " + pageName);
087:
088: ArrayList result = new ArrayList();
089: try {
090: WikiPage page = m_engine.getPage(pageName);
091: Collection attachments = m_engine.getAttachmentManager()
092: .listAttachments(page);
093:
094: for (Iterator i = attachments.iterator(); i.hasNext();) {
095: Attachment att = (Attachment) i.next();
096:
097: DavPath this Path = new DavPath("/");
098:
099: this Path.append(att.getName());
100:
101: AttachmentItem ai = new AttachmentItem(this , this Path,
102: att);
103:
104: result.add(ai);
105: }
106: } catch (ProviderException e) {
107: log.error(
108: "Unable to list attachments, returning what I got",
109: e);
110: // FIXME: Not a good way to handle errors
111: }
112:
113: return result;
114: }
115:
116: public DavItem getItem(DavPath path) {
117: if (path.isRoot()) {
118: DirectoryItem di = new DirectoryItem(this , new DavPath(""));
119:
120: di.addDavItems(listAllPagesWithAttachments());
121: return di;
122: } else if (path.isDirectory()) {
123: DirectoryItem di = new DirectoryItem(this , path);
124:
125: di.addDavItems(listAttachmentsOfPage(path));
126:
127: return di;
128: } else {
129: String attName = path.getPath();
130:
131: try {
132: Attachment att = m_engine.getAttachmentManager()
133: .getAttachmentInfo(attName);
134:
135: if (att != null) {
136: AttachmentItem ai = new AttachmentItem(this , path,
137: att);
138:
139: return ai;
140: }
141: } catch (ProviderException e) {
142: log.error("Unable to get the attachment data for "
143: + attName, e);
144: }
145: }
146: return null;
147: }
148:
149: public void setItem(DavPath path, DavItem item) {
150: // TODO Auto-generated method stub
151:
152: }
153:
154: public String getURL(DavPath path) {
155: String p = path.getPath();
156:
157: if (p.startsWith("/"))
158: p = p.substring(1);
159:
160: return m_engine.getURL(WikiContext.ATTACH, p, null, true);
161: }
162:
163: }
|