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: import javax.servlet.http.HttpSession;
012:
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 LOCK method.
020: *
021: * @author Slobodan Vujasinovic
022: */
023: public class DefaultLockHandler extends AbstractHandler {
024:
025: /**
026: * Services requests which use the WebDAV LOCK method.
027: *
028: * @param request The request being serviced.
029: * @param response The servlet response.
030: * @throws ServletException If an application error occurs.
031: * @throws IOException If an IO error occurs while handling the request.
032: */
033:
034: public void service(HttpServletRequest request,
035: HttpServletResponse response) throws ServletException,
036: IOException {
037:
038: Map map = this .readParameters(request);
039:
040: /*
041: * User is required for this operation!
042: */
043: String user = getUser(request);
044: if (null == user) {
045: if (null != getLogger()) {
046: getLogger().log(Log.DEBUG, "(LOCK)User not defined.");
047: }
048: response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
049: response.flushBuffer();
050: return;
051: }
052:
053: String mod = (String) map.get(EnhydraDMConstants.ACTION);
054: if (null == mod) {
055: if (null != getLogger()) {
056: getLogger()
057: .log(Log.DEBUG,
058: "(LOCK)Action parameter isn't defined parameter does not exist.");
059: }
060:
061: response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
062: response.flushBuffer();
063: return;
064: }
065:
066: String id = (String) map.get(EnhydraDMConstants.DOCUMENT_ID);
067:
068: try {
069: if (mod.equals(EnhydraDMConstants.ACTION_READ)
070: || mod
071: .equals(EnhydraDMConstants.ACTION_READ_VERSION)) {
072:
073: response.setStatus(HttpServletResponse.SC_OK);
074: response.flushBuffer();
075: response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
076: response.flushBuffer();
077: if (null != getLogger()) {
078: getLogger().log(Log.INFORMATION,
079: "(LOCK)File is opened in read mode!");
080: }
081:
082: } else {
083:
084: if (null == id) {
085: if (null != getLogger()) {
086: getLogger()
087: .log(Log.DEBUG,
088: "(LOCK)Document id parameter does not exist.");
089: }
090: response
091: .setStatus(HttpServletResponse.SC_BAD_REQUEST);
092: response.flushBuffer();
093: return;
094: }
095:
096: if (null != getLogger()) {
097: getLogger().log(Log.DEBUG,
098: "LOCK Request for document (id)\"{0}\".",
099: id);
100: }
101:
102: getDocumentManager().lock(id, user);
103:
104: setLockResponse(user, response);
105:
106: HttpSession session = request.getSession(true);
107: session.setAttribute(SESSION_ATT_LOCKED, "true");
108:
109: }
110: } catch (BaseException e) {
111:
112: response.flushBuffer();
113: throw new ServletException(e.getMessage());
114: }
115: response.flushBuffer();
116: }
117:
118: private void setLockResponse(String user,
119: HttpServletResponse response) throws ServletException,
120: IOException, BaseException {
121:
122: String content = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"
123: + "<prop xmlns=\"DAV:\">";
124:
125: content = content + "<lockdiscovery>" + "<activelock>"
126: + "<locktype>" + " <write/>" + "</locktype>"
127: + "<lockscope>" + "<exclusive/>" + "<shared/>"
128: + "</lockscope>" + "<depth>0</depth>" + "<owner>"
129: + user + "</owner>" + "<locktoken/>"
130: + "<timeout>Second-" + getLockRequestTimeout()
131: + "</timeout>" + "</activelock>" + "</lockdiscovery>"
132: + "</prop>";
133:
134: response.getWriter().write(content);
135: response.setStatus(HttpServletResponse.SC_OK);
136:
137: response.setContentType("text/xml; charset=\"utf-8\"");
138: }
139:
140: }
|