001: /**
002: *
003: */package org.enhydra.dm.handler;
004:
005: import java.io.IOException;
006: import java.util.Map;
007:
008: import javax.servlet.ServletException;
009: import javax.servlet.http.HttpServletRequest;
010: import javax.servlet.http.HttpServletResponse;
011:
012: import org.enhydra.dm.api.Document;
013: import org.enhydra.dm.api.exceptions.BaseException;
014: import org.enhydra.dm.api.handler.AbstractHandler;
015: import org.enhydra.dm.api.loggers.Log;
016: import org.enhydra.dm.util.EnhydraDMConstants;
017:
018: /**
019: * Default implementation of a handler for requests using the WebDAV PROPFIND method.
020: *
021: * @author Slobodan Vujasinovic
022: */
023: public class DefaultPropfindHandler extends AbstractHandler {
024:
025: /**
026: * Services requests which use the WebDAV PROPFIND method. This implementation builds
027: * and returns an XML document containing an appropriate PROPFIND result. <br>
028: * If the specified resource does not exist, a 404 (Not Found) error is sent to the
029: * client. <br>
030: * If the PROPFIND request is not properly formed, a 400 (Bad Request) error is sent to
031: * the client.
032: *
033: * @param request The request being serviced.
034: * @param response The servlet response.
035: * @throws ServletException If an application error occurs.
036: * @throws IOException If an IO error occurs while handling the request.
037: */
038:
039: public void service(HttpServletRequest request,
040: HttpServletResponse response) throws ServletException,
041: IOException {
042: try {
043: setResponse(request, response);
044: } catch (BaseException e) {
045:
046: throw new ServletException(e.getMessage());
047: }
048:
049: }
050:
051: private void setResponse(HttpServletRequest request,
052: HttpServletResponse response) throws IOException,
053: ServletException, BaseException {
054:
055: Map map = readParameters(request);
056:
057: /*
058: * User is required for this operation!
059: */
060: String user = getUser(request);
061: if (null == user) {
062: if (null != getLogger()) {
063: getLogger().log(Log.DEBUG,
064: "(PROPFIND)User not defined.");
065: }
066: response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
067: response.flushBuffer();
068: return;
069:
070: }
071:
072: String mod = (String) map.get(EnhydraDMConstants.ACTION);
073: if (null == mod) {
074: if (null != getLogger()) {
075: getLogger()
076: .log(Log.DEBUG,
077: "(PROPFIND)Action parameter isn't defined parameter does not exist.");
078: }
079:
080: response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
081: response.flushBuffer();
082: return;
083: }
084: String id = (String) map.get(EnhydraDMConstants.DOCUMENT_ID);
085: if (null == id) {
086:
087: if (null != getLogger()) {
088: getLogger()
089: .log(Log.DEBUG,
090: "(PROPFIND)Document id parameter does not exist.");
091: }
092:
093: response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
094: response.flushBuffer();
095: return;
096: }
097:
098: if (null != getLogger()) {
099: getLogger().log(Log.DEBUG,
100: "PROPFIND Request for document (id)\"{0}\".");
101: }
102: Document document = getDocumentManager().getDocument(id);
103:
104: if (null == document) {
105:
106: if (null != getLogger()) {
107: getLogger()
108: .log(
109: Log.DEBUG,
110: "(PROPFIND)Document does not exist for id.",
111: id);
112: }
113:
114: response.sendError(HttpServletResponse.SC_NOT_FOUND);
115: return;
116: }
117: String content = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"
118: + "<multistatus xmlns=\"DAV:\" xmlns:a=\"DAV:\" >";
119: if (mod.equals(EnhydraDMConstants.ACTION_EDIT)) {
120:
121: content = content + "<response>" + "<href>"
122: + document.getFilepath() + "</href>" + "<propstat>"
123: + "<status>HTTP/1.1 200 MultiStatus</status>"
124: + "<prop>" + "<getcontenttype>"
125: + document.getMimeType() + "</getcontenttype>"
126: + "<resourcetype/>" + "<displayname>"
127: + document.getName() + "</displayname>"
128: + " <supportedlock>" + " <lockentry>"
129: + " <lockscope>" + "<exclusive/>" + " </lockscope>"
130: + " <locktype>" + " <write/>" + " </locktype>"
131: + "</lockentry>" + " <lockentry>" + " <lockscope>"
132: + "<shared/>" + " </lockscope>" + " <locktype>"
133: + " <write/>" + " </locktype>" + "</lockentry>"
134: + "</supportedlock>" + "<lockdiscovery>"
135: + "<activelock>" + "<locktype>" + " <write/>"
136: + "</locktype>" + "<lockscope>" + "<exclusive/>"
137: + "<shared/>" + "</lockscope>" + "<depth>0</depth>"
138: + "<owner>" + user + "</owner>"
139: + "<timeout>Second-180</timeout>" + "<locktoken>"
140: + document.getNumber() + "</locktoken>"
141: + "</activelock>" + "</lockdiscovery>"
142: + "<getcontentlength w:dt=\"int\">"
143: + document.getSize() + "</getcontentlength>"
144: + "<getetag>" + document.getETag() + "</getetag>"
145: + "</prop>" + "</propstat>" + "</response>"
146: + "</multistatus>";
147: } else {
148: content = content + "<response>" + "<href></href>"
149: + "<propstat>"
150: + "<status>HTTP/1.1 200 MultiStatus</status>"
151: + "<prop>"
152: + "<a:isreadonly w:dt=\"boolean\">1</a:isreadonly>"
153: + " </prop>" + "</propstat>" + "</response>"
154: + "</multistatus>";
155: }
156:
157: response.getWriter().write(content);
158: response.setStatus(SC_MULTISTATUS);
159: response.setHeader(EnhydraDMConstants.HEAD_ALLOW,
160: getAllowedMethods());
161: response.setContentType("text/xml; charset=\"utf-8\"");
162: response.flushBuffer();
163: }
164: }
|