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.io.IOException;
023:
024: import javax.servlet.ServletConfig;
025: import javax.servlet.ServletException;
026: import javax.servlet.http.HttpServletRequest;
027: import javax.servlet.http.HttpServletResponse;
028:
029: import org.apache.commons.lang.time.StopWatch;
030: import org.apache.log4j.Logger;
031:
032: import com.ecyrd.jspwiki.WikiEngine;
033: import com.ecyrd.jspwiki.dav.methods.DavMethod;
034: import com.ecyrd.jspwiki.dav.methods.GetMethod;
035: import com.ecyrd.jspwiki.dav.methods.PropFindMethod;
036:
037: /**
038: * @author jalkanen
039: *
040: * @since
041: */
042: public class WikiDavServlet extends WebdavServlet {
043: private static final long serialVersionUID = 1L;
044:
045: private WikiEngine m_engine;
046: Logger log = Logger.getLogger(this .getClass().getName());
047: private DavProvider m_rawProvider;
048: private DavProvider m_rootProvider;
049: private DavProvider m_htmlProvider;
050:
051: public void init(ServletConfig config) throws ServletException {
052: super .init(config);
053:
054: m_engine = WikiEngine.getInstance(config);
055:
056: m_rawProvider = new RawPagesDavProvider(m_engine);
057: m_rootProvider = new WikiRootProvider(m_engine);
058: m_htmlProvider = new HTMLPagesDavProvider(m_engine);
059: }
060:
061: private DavProvider pickProvider(String context) {
062: if (context.equals("raw"))
063: return m_rawProvider;
064: else if (context.equals("html"))
065: return m_htmlProvider;
066:
067: return m_rootProvider;
068: }
069:
070: public void doPropFind(HttpServletRequest req,
071: HttpServletResponse res) throws IOException,
072: ServletException {
073: StopWatch sw = new StopWatch();
074: sw.start();
075:
076: // Do the "sanitize url" trick
077: String p = new String(req.getPathInfo().getBytes("ISO-8859-1"),
078: "UTF-8");
079:
080: DavPath path = new DavPath(p);
081: if (path.isRoot()) {
082: DavMethod dm = new PropFindMethod(m_rootProvider);
083: dm.execute(req, res, path);
084: } else {
085: String context = path.get(0);
086:
087: PropFindMethod m = new PropFindMethod(pickProvider(context));
088: m.execute(req, res, path.subPath(1));
089: }
090:
091: sw.stop();
092:
093: log.debug("Propfind done for path " + path + ", took " + sw);
094: }
095:
096: protected void doOptions(HttpServletRequest req,
097: HttpServletResponse res) {
098: log.debug("DAV doOptions for path " + req.getPathInfo());
099:
100: res.setHeader("DAV", "1"); // We support only Class 1
101: res
102: .setHeader("Allow",
103: "GET, PUT, POST, OPTIONS, PROPFIND, PROPPATCH, MOVE, COPY, DELETE");
104: res.setStatus(HttpServletResponse.SC_OK);
105: }
106:
107: public void doMkCol(HttpServletRequest request,
108: HttpServletResponse response) throws ServletException,
109: IOException {
110: if (request.getContentLength() > 0) {
111: response.sendError(
112: HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE,
113: "Message may contain no body");
114: } else {
115: response.sendError(HttpServletResponse.SC_UNAUTHORIZED,
116: "JSPWiki is read-only.");
117: }
118: }
119:
120: public void doPropPatch(HttpServletRequest request,
121: HttpServletResponse response) throws ServletException,
122: IOException {
123: //DavMethod dm = new PropPatchMethod( m_rawProvider );
124:
125: //dm.execute( request, response );
126: }
127:
128: public void doCopy(HttpServletRequest request,
129: HttpServletResponse response) throws ServletException,
130: IOException {
131: response.sendError(HttpServletResponse.SC_UNAUTHORIZED,
132: "JSPWiki is read-only.");
133: }
134:
135: public void doMove(HttpServletRequest request,
136: HttpServletResponse response) throws ServletException,
137: IOException {
138: response.sendError(HttpServletResponse.SC_UNAUTHORIZED,
139: "JSPWiki is read-only.");
140:
141: }
142:
143: protected void doDelete(HttpServletRequest arg0,
144: HttpServletResponse response) throws ServletException,
145: IOException {
146: response.sendError(HttpServletResponse.SC_UNAUTHORIZED,
147: "JSPWiki is read-only.");
148: }
149:
150: protected void doPost(HttpServletRequest arg0,
151: HttpServletResponse response) throws ServletException,
152: IOException {
153: response.sendError(HttpServletResponse.SC_UNAUTHORIZED,
154: "JSPWiki is read-only.");
155: }
156:
157: protected void doPut(HttpServletRequest arg0,
158: HttpServletResponse response) throws ServletException,
159: IOException {
160: response.sendError(HttpServletResponse.SC_UNAUTHORIZED,
161: "JSPWiki is read-only.");
162: }
163:
164: /*
165: * GET /dav/raw/WikiPage.txt
166: * GET /dav/html/WikiPage.html
167: * GET /dav/pdf/WikiPage.pdf
168: * GET /dav/raw/WikiPage/attachment1.png
169: *
170: */
171: protected void doGet(HttpServletRequest req, HttpServletResponse res)
172: throws ServletException, IOException {
173: // Do the "sanitize url" trick
174: // String p = new String(req.getPathInfo().getBytes("ISO-8859-1"), "UTF-8");
175:
176: req.setCharacterEncoding(m_engine.getContentEncoding());
177: String p = req.getPathInfo();
178:
179: DavPath path = new DavPath(p);
180:
181: if (path.isRoot()) {
182: DavMethod dm = new GetMethod(m_rootProvider);
183: dm.execute(req, res, path);
184: } else {
185: DavMethod dm = new GetMethod(pickProvider(path.get(0)));
186:
187: dm.execute(req, res, path.subPath(1));
188: }
189: }
190: }
|