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.exceptions.BaseException;
013: import org.enhydra.dm.api.handler.AbstractHandler;
014: import org.enhydra.dm.api.loggers.Log;
015: import org.enhydra.dm.util.EnhydraDMConstants;
016:
017: /**
018: * Default implementation of a handler for requests using the WebDAV UNLOCK method.
019: *
020: * @author Slobodan Vujasinovic
021: */
022: public class DefaultUnlockHandler extends AbstractHandler {
023:
024: /**
025: * Services requests which use the WebDAV UNLOCK method.
026: *
027: * @param request The request being serviced.
028: * @param response The servlet response.
029: * @throws ServletException If an application error occurs.
030: * @throws IOException If an IO error occurs while handling the request.
031: */
032:
033: public void service(HttpServletRequest request,
034: HttpServletResponse response) throws ServletException,
035: IOException {
036:
037: response.setHeader(EnhydraDMConstants.HEAD_ALLOW,
038: getAllowedMethods());
039:
040: Map map = this .readParameters(request);
041:
042: /*
043: * User is required for this operation!
044: */
045: String user = getUser(request);
046: if (null == user) {
047: if (null != getLogger()) {
048: getLogger()
049: .log(Log.DEBUG, "(UNLOCK) User not defined.");
050: }
051: response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
052: response.flushBuffer();
053: return;
054:
055: }
056:
057: String id = (String) map.get(EnhydraDMConstants.DOCUMENT_ID);
058: if (null == id) {
059:
060: if (null != getLogger()) {
061: getLogger()
062: .log(Log.DEBUG,
063: "(UNLOCK) Document id parameter does not exist.");
064: }
065:
066: response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
067: response.flushBuffer();
068: return;
069: }
070:
071: if (null == request.getSession(true).getAttribute(
072: SESSION_ATT_LOCKED)) {
073: if (null != getLogger()) {
074: getLogger().log(Log.WARNING,
075: "(UNLOCK) Session Expired.");
076: }
077: response
078: .setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
079: response.flushBuffer();
080: return;
081: }
082:
083: if (null != getLogger()) {
084: getLogger().log(Log.DEBUG,
085: "(UNLOCK) Request for document (id)\"{0}\".", id);
086: }
087:
088: String path = (String) request.getSession().getAttribute(id);
089: try {
090:
091: if (path == null || "".equals(path)) {
092: getDocumentManager().unlock(id, user);
093: response.setStatus(HttpServletResponse.SC_NO_CONTENT);
094: } else {
095: getDocumentManager().update(id, path, user, null);
096: request.getSession().setAttribute(id, null);
097: response.setStatus(HttpServletResponse.SC_NO_CONTENT);
098: }
099: } catch (BaseException e) {
100:
101: response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
102: throw new ServletException(e.getMessage());
103: }
104:
105: }
106:
107: }
|